diff --git a/iodine.c b/iodine.c index 72d3b4b..64ed7f8 100644 --- a/iodine.c +++ b/iodine.c @@ -247,7 +247,7 @@ main(int argc, char **argv) } } - if ((tun_fd = open_tun()) == -1) + if ((tun_fd = open_tun(NULL)) == -1) goto cleanup1; if ((dns_fd = open_dns(argv[1], 0)) == -1) goto cleanup2; diff --git a/iodined.c b/iodined.c index 1820403..1a4c2f6 100644 --- a/iodined.c +++ b/iodined.c @@ -263,7 +263,7 @@ main(int argc, char **argv) usage(); } - if ((tun_fd = open_tun()) == -1) + if ((tun_fd = open_tun(NULL)) == -1) goto cleanup0; if (tun_setip(argv[0]) != 0 || tun_setmtu(mtu) != 0) goto cleanup1; diff --git a/tun.c b/tun.c index 8a7c00f..e9d3b92 100644 --- a/tun.c +++ b/tun.c @@ -31,7 +31,6 @@ #define TUN_MAX_TRY 50 -char *tun_device = NULL; char if_name[50]; #ifdef LINUX @@ -41,17 +40,15 @@ char if_name[50]; #include int -open_tun() +open_tun(const char *tun_device) { int i; int tun_fd; struct ifreq ifreq; + char *tunnel = "/dev/net/tun"; - if (tun_device == NULL) - tun_device = "/dev/net/tun"; - - if ((tun_fd = open(tun_device, O_RDWR)) < 0) { - warn("open_tun: %s: %s", tun_device, strerror(errno)); + if ((tun_fd = open(tunnel, O_RDWR)) < 0) { + warn("open_tun: %s: %s", tunnel, strerror(errno)); return -1; } @@ -59,30 +56,44 @@ open_tun() ifreq.ifr_flags = IFF_TUN; - for (i = 0; i < TUN_MAX_TRY; i++) { - snprintf(ifreq.ifr_name, IFNAMSIZ, "dns%d", i); + if (tun_device != NULL) { + strncpy(ifreq.ifr_name, tun_device, IFNAMSIZ); - if (ioctl(tun_fd, TUNSETIFF, (void *) &ifreq) != -1) { - printf("Opened %s\n", ifreq.ifr_name); - snprintf(if_name, sizeof(if_name), "dns%d", i); - return tun_fd; + if (ioctl(tun_fd, TUNSETIFF, (void *) &ifreq) != -1) { + printf("Opened %s\n", ifreq.ifr_name); + snprintf(if_name, sizeof(if_name), "dns%d", i); + return tun_fd; + } + + if (errno != EBUSY) { + warn("open_tun: ioctl[TUNSETIFF]: %s", strerror(errno)); + return -1; + } + } else { + for (i = 0; i < TUN_MAX_TRY; i++) { + snprintf(ifreq.ifr_name, IFNAMSIZ, "dns%d", i); + + if (ioctl(tun_fd, TUNSETIFF, (void *) &ifreq) != -1) { + printf("Opened %s\n", ifreq.ifr_name); + snprintf(if_name, sizeof(if_name), "dns%d", i); + return tun_fd; + } + + if (errno != EBUSY) { + warn("open_tun: ioctl[TUNSETIFF]: %s", strerror(errno)); + return -1; + } } - if (errno != EBUSY) { - warn("open_tun: ioctl[TUNSETIFF]: %s", strerror(errno)); - return -1; - } + warn("open_tun: Couldn't set interface name.\n"); } - - warn("open_tun: Couldn't set interface name.\n"); - return -1; } #else /* BSD */ int -open_tun() +open_tun(const char *tun_device) { int i; int tun_fd; diff --git a/tun.h b/tun.h index 5ad75a3..0a32dcd 100644 --- a/tun.h +++ b/tun.h @@ -19,7 +19,7 @@ #ifndef _TUN_H_ #define _TUN_H_ -int open_tun(); +int open_tun(const char *); void close_tun(int); int write_tun(int, char *, int); int read_tun(int, char *, int);