From 4c0032c04c3724c19c21cbea654cc4b84845f6a2 Mon Sep 17 00:00:00 2001 From: Erik Ekman Date: Fri, 11 Aug 2006 22:52:36 +0000 Subject: [PATCH] Added support for choosing ip to listen on for DNS --- dns.c | 6 +++--- dns.h | 2 +- iodine.c | 2 +- iodined.c | 19 +++++++++++++++---- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/dns.c b/dns.c index 2cf04ab..035a89c 100644 --- a/dns.c +++ b/dns.c @@ -58,7 +58,7 @@ uint16_t pingid; int -open_dns(const char *domain, int localport) +open_dns(const char *domain, int localport, in_addr_t listen_ip) { int fd; int flag; @@ -67,9 +67,9 @@ open_dns(const char *domain, int localport) bzero(&addr, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(localport); - addr.sin_addr.s_addr = htonl(INADDR_ANY); + addr.sin_addr.s_addr = listen_ip; // This is already network byte order, inet_addr() or constant INADDR_ANY (==0) - fd = socket(AF_INET, SOCK_DGRAM, 0); + fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if(fd < 0) { warn("socket"); return -1; diff --git a/dns.h b/dns.h index ad27b6b..62f6ca1 100644 --- a/dns.h +++ b/dns.h @@ -17,7 +17,7 @@ #ifndef _DNS_H_ #define _DNS_H_ -int open_dns(const char *, int); +int open_dns(const char *, int, in_addr_t); int dns_settarget(const char*); void close_dns(int); diff --git a/iodine.c b/iodine.c index 8944bfc..f5752a0 100644 --- a/iodine.c +++ b/iodine.c @@ -255,7 +255,7 @@ main(int argc, char **argv) if ((tun_fd = open_tun(device)) == -1) goto cleanup1; - if ((dns_fd = open_dns(argv[1], 0)) == -1) + if ((dns_fd = open_dns(argv[1], 0, INADDR_ANY)) == -1) goto cleanup2; if (dns_settarget(argv[0]) == -1) goto cleanup2; diff --git a/iodined.c b/iodined.c index 6d0a347..74ecee8 100644 --- a/iodined.c +++ b/iodined.c @@ -163,7 +163,7 @@ extern char *__progname; static void usage() { - printf("Usage: %s [-v] [-h] [-f] [-u user] [-t chrootdir] [-d device] [-m mtu] " + printf("Usage: %s [-v] [-h] [-f] [-u user] [-t chrootdir] [-d device] [-m mtu] [-l ip address to listen on] " "tunnel_ip topdomain\n", __progname); exit(2); } @@ -171,7 +171,7 @@ usage() { static void help() { printf("iodine IP over DNS tunneling server\n"); - printf("Usage: %s [-v] [-h] [-f] [-u user] [-t chrootdir] [-d device] [-m mtu] " + printf("Usage: %s [-v] [-h] [-f] [-u user] [-t chrootdir] [-d device] [-m mtu] [-l ip address to listen on] " "tunnel_ip topdomain\n", __progname); printf(" -v to print version info and exit\n"); printf(" -h to print this help and exit\n"); @@ -180,6 +180,7 @@ help() { printf(" -t dir to chroot to directory dir\n"); printf(" -d device to set tunnel device name\n"); printf(" -m mtu to set tunnel device mtu\n"); + printf(" -l ip address to listen on for incoming dns traffic (default 0.0.0.0)\n"); printf("tunnel_ip is the IP number of the local tunnel interface.\n"); printf("topdomain is the FQDN that is delegated to this server.\n"); exit(0); @@ -205,19 +206,21 @@ main(int argc, char **argv) int foreground; int mtu; struct passwd *pw; + in_addr_t listen_ip; username = NULL; newroot = NULL; device = NULL; foreground = 0; mtu = 1024; + listen_ip = INADDR_ANY; packetbuf.len = 0; packetbuf.offset = 0; outpacket.len = 0; q.id = 0; - while ((choice = getopt(argc, argv, "vfhu:t:d:m:")) != -1) { + while ((choice = getopt(argc, argv, "vfhu:t:d:m:l:")) != -1) { switch(choice) { case 'v': version(); @@ -240,6 +243,9 @@ main(int argc, char **argv) case 'm': mtu = atoi(optarg); break; + case 'l': + listen_ip = inet_addr(optarg); + break; default: usage(); break; @@ -270,11 +276,16 @@ main(int argc, char **argv) usage(); } + if (listen_ip == INADDR_NONE) { + printf("Bad IP address to listen on.\n"); + usage(); + } + if ((tun_fd = open_tun(device)) == -1) goto cleanup0; if (tun_setip(argv[0]) != 0 || tun_setmtu(mtu) != 0) goto cleanup1; - if ((dnsd_fd = open_dns(argv[1], 53)) == -1) + if ((dnsd_fd = open_dns(argv[1], 53, listen_ip)) == -1) goto cleanup2; my_ip = inet_addr(argv[0]);