From c83d6d69831eb59122f4f33f432f04c941fc6269 Mon Sep 17 00:00:00 2001 From: Bjorn Andersson Date: Sun, 11 Jun 2006 15:19:07 +0000 Subject: [PATCH] tun has setip and setmtu --- tun.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/tun.c b/tun.c index 2c7d8c3..1184068 100644 --- a/tun.c +++ b/tun.c @@ -24,12 +24,14 @@ #include #include #include +#include #include "tun.h" #define TUN_MAX_TRY 50 char *tun_device = NULL; +char *if_device = NULL; #ifdef LINUX @@ -61,6 +63,7 @@ open_tun() if (ioctl(tun_fd, TUNSETIFF, (void *) &ifreq) != -1) { printf("Opened %s\n", ifreq.ifr_name); + if_device = strdup(ifreq.ifr_name); return tun_fd; } @@ -95,6 +98,7 @@ open_tun() if ((tun_fd = open(tun_name, O_RDWR)) >= 0) { printf("Opened %s\n", tun_name); + if_device = strdup(tun_name); return tun_fd; } @@ -146,3 +150,49 @@ read_tun(int tun_fd, char *buf, int len) return read(tun_fd, buf, len); } +int +tun_setip(const char *ip) +{ + char cmdline[512]; + + if (inet_addr(ip) != 0) { + snprintf(cmdline, sizeof(cmdline), + "/sbin/ifconfig %s %s netmask 255.255.255.0", + tun_device, + ip); +#ifndef LINUX + int r; + + r = system(cmdline); + if(r != 0) { + return r; + } else { + snprintf(cmdline, sizeof(cmdline), + "/sbin/route add %s/24 %s", + ip, ip); + } +#endif + + return system(cmdline); + } + + return 1; +} + +int +tun_setmtu(const int mtu) +{ + char cmdline[512]; + + if (mtu > 200 && mtu < 1500) { + snprintf(cmdline, sizeof(cmdline), + "/sbin/ifconfig %s mtu %d", + tun_device, + mtu); + + return system(cmdline); + } + + return 1; +} +