summaryrefslogtreecommitdiff
blob: 0d65f0f6d68b68da8fc7e484ad30ebfcdb8ea222 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
diff -ru nstx-1.1-beta6.tuntap/Makefile nstx-1.1-beta6/Makefile
--- nstx-1.1-beta6.tuntap/Makefile	2009-03-16 23:22:11.000000000 +0000
+++ nstx-1.1-beta6/Makefile	2009-03-16 23:27:09.000000000 +0000
@@ -1,9 +1,9 @@
 CFLAGS += -ggdb -Wall -Werror -Wsign-compare 
 
-NSTXD_SRCS = nstxd.c nstx_encode.c nstx_pstack.c nstx_dns.c nstx_tuntap.c nstx_queue.c
+NSTXD_SRCS = nstxd.c nstx_encode.c nstx_pstack.c nstx_dns.c nstx_tuntap.c nstx_queue.c nstx_util.c
 NSTXD_OBJS = ${NSTXD_SRCS:.c=.o}
 
-NSTXCD_SRCS = nstxcd.c nstx_encode.c nstx_pstack.c nstx_dns.c nstx_tuntap.o nstx_queue.c
+NSTXCD_SRCS = nstxcd.c nstx_encode.c nstx_pstack.c nstx_dns.c nstx_tuntap.o nstx_queue.c nstx_util.c
 NSTXCD_OBJS = ${NSTXCD_SRCS:.c=.o}
 
 PROGS = nstxd nstxcd
diff -ru nstx-1.1-beta6.tuntap/nstx_util.c nstx-1.1-beta6/nstx_util.c
--- nstx-1.1-beta6.tuntap/nstx_util.c	2004-06-27 21:43:34.000000000 +0000
+++ nstx-1.1-beta6/nstx_util.c	2009-03-16 23:28:37.000000000 +0000
@@ -27,6 +27,10 @@
 #include <stdio.h>
 #include <sys/types.h>
 #include <sys/socket.h>
+#include <net/if.h>
+#include <sys/ioctl.h>
+#include <arpa/inet.h>
+#include <errno.h>
 
 #include "nstxfun.h"
 
@@ -48,6 +52,48 @@
    close(fd);
 }
 
+static int iface_addr(const char * name, in_addr_t * result) {
+    int r, s;
+    struct ifreq ifr;
+    struct sockaddr_in * sin;
+    
+    s = socket(AF_INET, SOCK_DGRAM, 0);
+
+    if (s < 0) {
+        perror("socket");
+        return s;
+    }
+
+    strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
+    ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = 0;
+
+    r = ioctl(s, SIOCGIFADDR, &ifr);
+
+    if (r < 0) {
+        perror("ioctl(SIOCGIFADDR)");
+        return r;
+    }
+
+    sin = (struct sockaddr_in *)&ifr.ifr_addr;
+    *result = sin->sin_addr.s_addr;
+
+    if (*result == INADDR_ANY || *result == INADDR_NONE) {
+        fprintf(stderr, "interface %s has no assigned address\n", name);
+        return -EINVAL;
+    }
+
+    return 0;
+}
+
+int addr_convert(const char * s, in_addr_t * result) {
+    *result = inet_addr(s);
+
+    if (*result != INADDR_NONE)
+        return 0;
+
+    return iface_addr(s, result);
+}
+
 #ifdef WITH_PKTDUMP
 void
 pktdump (const char *prefix, unsigned short id, const char *data,
diff -ru nstx-1.1-beta6.tuntap/nstxd.8 nstx-1.1-beta6/nstxd.8
--- nstx-1.1-beta6.tuntap/nstxd.8	2009-03-16 23:23:46.000000000 +0000
+++ nstx-1.1-beta6/nstxd.8	2009-03-16 23:29:59.000000000 +0000
@@ -22,8 +22,8 @@
 Tun mode (default)
 .IP \-T
 Tap mode
-.IP \-i ipaddr
-Bind to this IP address rather than every available address
+.IP \-i ipaddr|interface
+Bind to this IP address or interface rather than every available address
 .IP \-C dir
 Chroot to this directory on startup
 .IP \-D
diff -ru nstx-1.1-beta6.tuntap/nstxd.c nstx-1.1-beta6/nstxd.c
--- nstx-1.1-beta6.tuntap/nstxd.c	2009-03-16 23:23:46.000000000 +0000
+++ nstx-1.1-beta6/nstxd.c	2009-03-16 23:32:45.000000000 +0000
@@ -61,7 +61,7 @@
 	    "\t-t (tun mode, default)\n"
 	    "\t-T (tap mode)\n"
 #endif
-	    "\t-i ip.to.bi.nd (bind to port 53 on this IP only)\n"
+	    "\t-i ip|interface (bind to port 53 on this IP/interface only)\n"
 	    "\t-C dir (chroot() to this directory after initialization)\n"
 	    "\t-D (call daemon(3) to detach from terminal)\n"
 	    "\t-g (enable debug messages)\n"
@@ -80,14 +80,15 @@
    int		 daemonize = 0;
    int		 logmask = LOG_UPTO(LOG_INFO);
    int tun = 1;
+   int r;
    
    while ((ch = getopt(argc, argv, "gDC:u:hd:I:i:tT")) != -1) {
 	switch(ch) {
 	case 'i':
-		bindto = inet_addr(optarg);
-		if (bindto == INADDR_NONE) {
-			fprintf(stderr, "`%s' is not an IP-address\n",
-			    optarg);
+        r = addr_convert(optarg, &bindto);
+        if (r < 0) {
+            fprintf(stderr, "couldn't use interface %s: %s\n", optarg,
+                    strerror(-r));
 			exit(EX_USAGE);
 		}
 		break;
diff -ru nstx-1.1-beta6.tuntap/nstxfun.h nstx-1.1-beta6/nstxfun.h
--- nstx-1.1-beta6.tuntap/nstxfun.h	2009-03-16 23:23:46.000000000 +0000
+++ nstx-1.1-beta6/nstxfun.h	2009-03-16 23:28:37.000000000 +0000
@@ -102,4 +102,6 @@
 void pktdump (const char *, unsigned short, const char *, size_t, int);
 #endif
 
+int addr_convert(const char *, in_addr_t *);
+
 #endif /* _NSTXHDR_H */