diff options
author | "Vladimir N. Oleynik" <dzo@simtreas.ru> | 2006-01-25 14:40:24 +0000 |
---|---|---|
committer | "Vladimir N. Oleynik" <dzo@simtreas.ru> | 2006-01-25 14:40:24 +0000 |
commit | 2e5ee8ed93a5346733a7a7d9764f9a6097443950 (patch) | |
tree | 15c823d20bbe7467bdb36566f9503bb0e72be518 /networking/dnsd.c | |
parent | 7b4aa6ffc6a412ebc12cda823d0631abcda6a55f (diff) | |
download | busybox-w32-2e5ee8ed93a5346733a7a7d9764f9a6097443950.tar.gz busybox-w32-2e5ee8ed93a5346733a7a7d9764f9a6097443950.tar.bz2 busybox-w32-2e5ee8ed93a5346733a7a7d9764f9a6097443950.zip |
+#include <sys/socket.h>
remove unused 'int i' form main
whitespacing only
Diffstat (limited to 'networking/dnsd.c')
-rw-r--r-- | networking/dnsd.c | 475 |
1 files changed, 238 insertions, 237 deletions
diff --git a/networking/dnsd.c b/networking/dnsd.c index 77d37d996..433b12443 100644 --- a/networking/dnsd.c +++ b/networking/dnsd.c | |||
@@ -7,10 +7,10 @@ | |||
7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
8 | * | 8 | * |
9 | * Odd Arild Olsen started out with the sheerdns [1] of Paul Sheer and rewrote | 9 | * Odd Arild Olsen started out with the sheerdns [1] of Paul Sheer and rewrote |
10 | * it into a shape which I believe is both easier to understand and maintain. | 10 | * it into a shape which I believe is both easier to understand and maintain. |
11 | * I also reused the input buffer for output and removed services he did not | 11 | * I also reused the input buffer for output and removed services he did not |
12 | * need. [1] http://threading.2038bug.com/sheerdns/ | 12 | * need. [1] http://threading.2038bug.com/sheerdns/ |
13 | * | 13 | * |
14 | * Some bugfix and minor changes was applied by Roberto A. Foglietta who made | 14 | * Some bugfix and minor changes was applied by Roberto A. Foglietta who made |
15 | * the first porting of oao' scdns to busybox also. | 15 | * the first porting of oao' scdns to busybox also. |
16 | */ | 16 | */ |
@@ -19,6 +19,7 @@ | |||
19 | #include <string.h> | 19 | #include <string.h> |
20 | #include <signal.h> | 20 | #include <signal.h> |
21 | #include <arpa/inet.h> | 21 | #include <arpa/inet.h> |
22 | #include <sys/socket.h> | ||
22 | #include <ctype.h> | 23 | #include <ctype.h> |
23 | #include "libbb.h" | 24 | #include "libbb.h" |
24 | 25 | ||
@@ -34,8 +35,8 @@ static const int MAX_NAME_LEN = (IP_STRING_LEN + 13); | |||
34 | 35 | ||
35 | /* Cannot get bigger packets than 512 per RFC1035 | 36 | /* Cannot get bigger packets than 512 per RFC1035 |
36 | In practice this can be set considerably smaller: | 37 | In practice this can be set considerably smaller: |
37 | Length of response packet is header (12B) + 2*type(4B) + 2*class(4B) + | 38 | Length of response packet is header (12B) + 2*type(4B) + 2*class(4B) + |
38 | ttl(4B) + rlen(2B) + r (MAX_NAME_LEN =21B) + | 39 | ttl(4B) + rlen(2B) + r (MAX_NAME_LEN =21B) + |
39 | 2*querystring (2 MAX_NAME_LEN= 42B), all together 90 Byte | 40 | 2*querystring (2 MAX_NAME_LEN= 42B), all together 90 Byte |
40 | */ | 41 | */ |
41 | static const int MAX_PACK_LEN = 512 + 1; | 42 | static const int MAX_PACK_LEN = 512 + 1; |
@@ -45,28 +46,28 @@ static const int MAX_PACK_LEN = 512 + 1; | |||
45 | static const int REQ_A = 1; | 46 | static const int REQ_A = 1; |
46 | static const int REQ_PTR = 12; | 47 | static const int REQ_PTR = 12; |
47 | 48 | ||
48 | struct dns_repl { // resource record, add 0 or 1 to accepted dns_msg in resp | 49 | struct dns_repl { // resource record, add 0 or 1 to accepted dns_msg in resp |
49 | uint16_t rlen; | 50 | uint16_t rlen; |
50 | uint8_t *r; // resource | 51 | uint8_t *r; // resource |
51 | uint16_t flags; | 52 | uint16_t flags; |
52 | }; | 53 | }; |
53 | 54 | ||
54 | struct dns_head { // the message from client and first part of response mag | 55 | struct dns_head { // the message from client and first part of response mag |
55 | uint16_t id; | 56 | uint16_t id; |
56 | uint16_t flags; | 57 | uint16_t flags; |
57 | uint16_t nquer; // accepts 0 | 58 | uint16_t nquer; // accepts 0 |
58 | uint16_t nansw; // 1 in response | 59 | uint16_t nansw; // 1 in response |
59 | uint16_t nauth; // 0 | 60 | uint16_t nauth; // 0 |
60 | uint16_t nadd; // 0 | 61 | uint16_t nadd; // 0 |
61 | }; | 62 | }; |
62 | struct dns_prop { | 63 | struct dns_prop { |
63 | uint16_t type; | 64 | uint16_t type; |
64 | uint16_t class; | 65 | uint16_t class; |
65 | }; | 66 | }; |
66 | struct dns_entry { // element of known name, ip address and reversed ip address | 67 | struct dns_entry { // element of known name, ip address and reversed ip address |
67 | struct dns_entry *next; | 68 | struct dns_entry *next; |
68 | char ip[IP_STRING_LEN]; // dotted decimal IP | 69 | char ip[IP_STRING_LEN]; // dotted decimal IP |
69 | char rip[IP_STRING_LEN]; // length decimal reversed IP | 70 | char rip[IP_STRING_LEN]; // length decimal reversed IP |
70 | char name[MAX_HOST_LEN]; | 71 | char name[MAX_HOST_LEN]; |
71 | }; | 72 | }; |
72 | 73 | ||
@@ -75,15 +76,15 @@ static int daemonmode = 0; | |||
75 | static uint32_t ttl = DEFAULT_TTL; | 76 | static uint32_t ttl = DEFAULT_TTL; |
76 | 77 | ||
77 | /* | 78 | /* |
78 | * Convert host name from C-string to dns length/string. | 79 | * Convert host name from C-string to dns length/string. |
79 | */ | 80 | */ |
80 | static void | 81 | static void |
81 | convname(char *a, uint8_t *q) | 82 | convname(char *a, uint8_t *q) |
82 | { | 83 | { |
83 | int i = (q[0] == '.')?0:1; | 84 | int i = (q[0] == '.') ? 0 : 1; |
84 | for(; i < MAX_HOST_LEN-1 && *q; i++, q++) | 85 | for(; i < MAX_HOST_LEN-1 && *q; i++, q++) |
85 | a[i] = tolower(*q); | 86 | a[i] = tolower(*q); |
86 | a[0] = i - 1; | 87 | a[0] = i - 1; |
87 | a[i] = 0; | 88 | a[i] = 0; |
88 | } | 89 | } |
89 | 90 | ||
@@ -93,7 +94,7 @@ convname(char *a, uint8_t *q) | |||
93 | static void | 94 | static void |
94 | undot(uint8_t * rip) | 95 | undot(uint8_t * rip) |
95 | { | 96 | { |
96 | int i=0, s=0; | 97 | int i = 0, s = 0; |
97 | while(rip[i]) i++; | 98 | while(rip[i]) i++; |
98 | for(--i; i >= 0; i--) { | 99 | for(--i; i >= 0; i--) { |
99 | if(rip[i] == '.') { | 100 | if(rip[i] == '.') { |
@@ -103,23 +104,23 @@ undot(uint8_t * rip) | |||
103 | } | 104 | } |
104 | } | 105 | } |
105 | 106 | ||
106 | /* | 107 | /* |
107 | * Append message to log file | 108 | * Append message to log file |
108 | */ | 109 | */ |
109 | static void | 110 | static void |
110 | log_message(char *filename, char *message) | 111 | log_message(char *filename, char *message) |
111 | { | 112 | { |
112 | FILE *logfile; | 113 | FILE *logfile; |
113 | if (!daemonmode) | 114 | if (!daemonmode) |
114 | return; | 115 | return; |
115 | logfile = fopen(filename, "a"); | 116 | logfile = fopen(filename, "a"); |
116 | if (!logfile) | 117 | if (!logfile) |
117 | return; | 118 | return; |
118 | fprintf(logfile, "%s\n", message); | 119 | fprintf(logfile, "%s\n", message); |
119 | fclose(logfile); | 120 | fclose(logfile); |
120 | } | 121 | } |
121 | 122 | ||
122 | /* | 123 | /* |
123 | * Read one line of hostname/IP from file | 124 | * Read one line of hostname/IP from file |
124 | * Returns 0 for each valid entry read, -1 at EOF | 125 | * Returns 0 for each valid entry read, -1 at EOF |
125 | * Assumes all host names are lower case only | 126 | * Assumes all host names are lower case only |
@@ -132,14 +133,14 @@ static int | |||
132 | getfileentry(FILE * fp, struct dns_entry *s, int verb) | 133 | getfileentry(FILE * fp, struct dns_entry *s, int verb) |
133 | { | 134 | { |
134 | unsigned int a,b,c,d; | 135 | unsigned int a,b,c,d; |
135 | char *r, *name; | 136 | char *r, *name; |
136 | 137 | ||
137 | restart: | 138 | restart: |
138 | if(!(r = bb_get_line_from_file(fp))) | 139 | if(!(r = bb_get_line_from_file(fp))) |
139 | return -1; | 140 | return -1; |
140 | while(*r == ' ' || *r == '\t') { | 141 | while(*r == ' ' || *r == '\t') { |
141 | r++; | 142 | r++; |
142 | if(!*r || *r == '#' || *r == '\n') | 143 | if(!*r || *r == '#' || *r == '\n') |
143 | goto restart; /* skipping empty/blank and commented lines */ | 144 | goto restart; /* skipping empty/blank and commented lines */ |
144 | } | 145 | } |
145 | name = r; | 146 | name = r; |
@@ -153,8 +154,8 @@ restart: | |||
153 | sprintf(s->rip,".%u.%u.%u.%u",d,c,b,a); | 154 | sprintf(s->rip,".%u.%u.%u.%u",d,c,b,a); |
154 | undot((uint8_t*)s->rip); | 155 | undot((uint8_t*)s->rip); |
155 | convname(s->name,(uint8_t*)name); | 156 | convname(s->name,(uint8_t*)name); |
156 | 157 | ||
157 | if(verb) | 158 | if(verb) |
158 | fprintf(stderr,"\tname:%s, ip:%s\n",&(s->name[1]),s->ip); | 159 | fprintf(stderr,"\tname:%s, ip:%s\n",&(s->name[1]),s->ip); |
159 | 160 | ||
160 | return 0; /* warningkiller */ | 161 | return 0; /* warningkiller */ |
@@ -166,28 +167,28 @@ restart: | |||
166 | static void | 167 | static void |
167 | dnsentryinit(int verb) | 168 | dnsentryinit(int verb) |
168 | { | 169 | { |
169 | FILE *fp; | 170 | FILE *fp; |
170 | struct dns_entry *m, *prev; | 171 | struct dns_entry *m, *prev; |
171 | prev = dnsentry = NULL; | 172 | prev = dnsentry = NULL; |
172 | 173 | ||
173 | if(!(fp = fopen(fileconf, "r"))) | 174 | if(!(fp = fopen(fileconf, "r"))) |
174 | bb_perror_msg_and_die("open %s",fileconf); | 175 | bb_perror_msg_and_die("open %s",fileconf); |
175 | 176 | ||
176 | while (1) { | 177 | while (1) { |
177 | if(!(m = (struct dns_entry *)malloc(sizeof(struct dns_entry)))) | 178 | if(!(m = (struct dns_entry *)malloc(sizeof(struct dns_entry)))) |
178 | bb_perror_msg_and_die("malloc dns_entry"); | 179 | bb_perror_msg_and_die("malloc dns_entry"); |
179 | 180 | ||
180 | m->next = NULL; | 181 | m->next = NULL; |
181 | if (getfileentry(fp, m, verb)) | 182 | if (getfileentry(fp, m, verb)) |
182 | break; | 183 | break; |
183 | 184 | ||
184 | if (prev == NULL) | 185 | if (prev == NULL) |
185 | dnsentry = m; | 186 | dnsentry = m; |
186 | else | 187 | else |
187 | prev->next = m; | 188 | prev->next = m; |
188 | prev = m; | 189 | prev = m; |
189 | } | 190 | } |
190 | fclose(fp); | 191 | fclose(fp); |
191 | } | 192 | } |
192 | 193 | ||
193 | 194 | ||
@@ -197,48 +198,48 @@ dnsentryinit(int verb) | |||
197 | static int | 198 | static int |
198 | listen_socket(char *iface_addr, int listen_port) | 199 | listen_socket(char *iface_addr, int listen_port) |
199 | { | 200 | { |
200 | struct sockaddr_in a; | 201 | struct sockaddr_in a; |
201 | char msg[100]; | 202 | char msg[100]; |
202 | int s; | 203 | int s; |
203 | int yes = 1; | 204 | int yes = 1; |
204 | if ((s = socket(PF_INET, SOCK_DGRAM, 0)) < 0) | 205 | if ((s = socket(PF_INET, SOCK_DGRAM, 0)) < 0) |
205 | bb_perror_msg_and_die("socket() failed"); | 206 | bb_perror_msg_and_die("socket() failed"); |
206 | #ifdef SO_REUSEADDR | 207 | #ifdef SO_REUSEADDR |
207 | if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&yes, sizeof(yes)) < 0) | 208 | if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&yes, sizeof(yes)) < 0) |
208 | bb_perror_msg_and_die("setsockopt() failed"); | 209 | bb_perror_msg_and_die("setsockopt() failed"); |
209 | #endif | 210 | #endif |
210 | memset(&a, 0, sizeof(a)); | 211 | memset(&a, 0, sizeof(a)); |
211 | a.sin_port = htons(listen_port); | 212 | a.sin_port = htons(listen_port); |
212 | a.sin_family = AF_INET; | 213 | a.sin_family = AF_INET; |
213 | if (!inet_aton(iface_addr, &a.sin_addr)) | 214 | if (!inet_aton(iface_addr, &a.sin_addr)) |
214 | bb_perror_msg_and_die("bad iface address"); | 215 | bb_perror_msg_and_die("bad iface address"); |
215 | if (bind(s, (struct sockaddr *)&a, sizeof(a)) < 0) | 216 | if (bind(s, (struct sockaddr *)&a, sizeof(a)) < 0) |
216 | bb_perror_msg_and_die("bind() failed"); | 217 | bb_perror_msg_and_die("bind() failed"); |
217 | listen(s, 50); | 218 | listen(s, 50); |
218 | sprintf(msg, "accepting UDP packets on addr:port %s:%d\n", | 219 | sprintf(msg, "accepting UDP packets on addr:port %s:%d\n", |
219 | iface_addr, (int)listen_port); | 220 | iface_addr, (int)listen_port); |
220 | log_message(LOG_FILE, msg); | 221 | log_message(LOG_FILE, msg); |
221 | return s; | 222 | return s; |
222 | } | 223 | } |
223 | 224 | ||
224 | /* | 225 | /* |
225 | * Look query up in dns records and return answer if found | 226 | * Look query up in dns records and return answer if found |
226 | * qs is the query string, first byte the string length | 227 | * qs is the query string, first byte the string length |
227 | */ | 228 | */ |
228 | static int | 229 | static int |
229 | table_lookup(uint16_t type, uint8_t * as, uint8_t * qs) | 230 | table_lookup(uint16_t type, uint8_t * as, uint8_t * qs) |
230 | { | 231 | { |
231 | int i; | 232 | int i; |
232 | struct dns_entry *d=dnsentry; | 233 | struct dns_entry *d=dnsentry; |
233 | 234 | ||
234 | do { | 235 | do { |
235 | #ifdef DEBUG | 236 | #ifdef DEBUG |
236 | char *p,*q; | 237 | char *p,*q; |
237 | q = (char *)&(qs[1]); | 238 | q = (char *)&(qs[1]); |
238 | p = &(d->name[1]); | 239 | p = &(d->name[1]); |
239 | fprintf(stderr, "\ntest: %d <%s> <%s> %d", strlen(p), p, q, strlen(q)); | 240 | fprintf(stderr, "\ntest: %d <%s> <%s> %d", strlen(p), p, q, strlen(q)); |
240 | #endif | 241 | #endif |
241 | if (type == REQ_A) { /* search by host name */ | 242 | if (type == REQ_A) { /* search by host name */ |
242 | for(i = 1; i <= (int)(d->name[0]); i++) | 243 | for(i = 1; i <= (int)(d->name[0]); i++) |
243 | if(tolower(qs[i]) != d->name[i]) | 244 | if(tolower(qs[i]) != d->name[i]) |
244 | continue; | 245 | continue; |
@@ -250,126 +251,126 @@ table_lookup(uint16_t type, uint8_t * as, uint8_t * qs) | |||
250 | fprintf(stderr, " %s ", as); | 251 | fprintf(stderr, " %s ", as); |
251 | #endif | 252 | #endif |
252 | return 0; | 253 | return 0; |
253 | } | 254 | } |
254 | else if (type == REQ_PTR) { /* search by IP-address */ | 255 | else if (type == REQ_PTR) { /* search by IP-address */ |
255 | if (!strncmp((char*)&d->rip[1], (char*)&qs[1], strlen(d->rip)-1)) { | 256 | if (!strncmp((char*)&d->rip[1], (char*)&qs[1], strlen(d->rip)-1)) { |
256 | strcpy((char *)as, d->name); | 257 | strcpy((char *)as, d->name); |
257 | return 0; | 258 | return 0; |
258 | } | 259 | } |
259 | } | 260 | } |
260 | } while ((d = d->next) != NULL); | 261 | } while ((d = d->next) != NULL); |
261 | return -1; | 262 | return -1; |
262 | } | 263 | } |
263 | 264 | ||
264 | 265 | ||
265 | /* | 266 | /* |
266 | * Decode message and generate answer | 267 | * Decode message and generate answer |
267 | */ | 268 | */ |
268 | #define eret(s) do { fprintf (stderr, "%s\n", s); return -1; } while (0) | 269 | #define eret(s) do { fprintf (stderr, "%s\n", s); return -1; } while (0) |
269 | static int | 270 | static int |
270 | process_packet(uint8_t * buf) | 271 | process_packet(uint8_t * buf) |
271 | { | 272 | { |
272 | struct dns_head *head; | 273 | struct dns_head *head; |
273 | struct dns_prop *qprop; | 274 | struct dns_prop *qprop; |
274 | struct dns_repl outr; | 275 | struct dns_repl outr; |
275 | void *next, *from, *answb; | 276 | void *next, *from, *answb; |
276 | 277 | ||
277 | uint8_t answstr[MAX_NAME_LEN + 1]; | 278 | uint8_t answstr[MAX_NAME_LEN + 1]; |
278 | int lookup_result, type, len, packet_len; | 279 | int lookup_result, type, len, packet_len; |
279 | uint16_t flags; | 280 | uint16_t flags; |
280 | 281 | ||
281 | answstr[0] = '\0'; | 282 | answstr[0] = '\0'; |
282 | 283 | ||
283 | head = (struct dns_head *)buf; | 284 | head = (struct dns_head *)buf; |
284 | if (head->nquer == 0) | 285 | if (head->nquer == 0) |
285 | eret("no queries"); | 286 | eret("no queries"); |
286 | 287 | ||
287 | if ((head->flags & 0x8000)) | 288 | if ((head->flags & 0x8000)) |
288 | eret("ignoring response packet"); | 289 | eret("ignoring response packet"); |
289 | 290 | ||
290 | from = (void *)&head[1]; // start of query string | 291 | from = (void *)&head[1]; // start of query string |
291 | next = answb = from + strlen((char *)&head[1]) + 1 + sizeof(struct dns_prop); // where to append answer block | 292 | next = answb = from + strlen((char *)&head[1]) + 1 + sizeof(struct dns_prop); // where to append answer block |
292 | 293 | ||
293 | outr.rlen = 0; // may change later | 294 | outr.rlen = 0; // may change later |
294 | outr.r = NULL; | 295 | outr.r = NULL; |
295 | outr.flags = 0; | 296 | outr.flags = 0; |
296 | 297 | ||
297 | qprop = (struct dns_prop *)(answb - 4); | 298 | qprop = (struct dns_prop *)(answb - 4); |
298 | type = ntohs(qprop->type); | 299 | type = ntohs(qprop->type); |
299 | 300 | ||
300 | // only let REQ_A and REQ_PTR pass | 301 | // only let REQ_A and REQ_PTR pass |
301 | if (!(type == REQ_A || type == REQ_PTR)) { | 302 | if (!(type == REQ_A || type == REQ_PTR)) { |
302 | goto empty_packet; /* we can't handle the query type */ | 303 | goto empty_packet; /* we can't handle the query type */ |
303 | } | 304 | } |
304 | 305 | ||
305 | if (ntohs(qprop->class) != 1 /* class INET */ ) { | 306 | if (ntohs(qprop->class) != 1 /* class INET */ ) { |
306 | outr.flags = 4; /* not supported */ | 307 | outr.flags = 4; /* not supported */ |
307 | goto empty_packet; | 308 | goto empty_packet; |
308 | } | 309 | } |
309 | /* we only support standard queries */ | 310 | /* we only support standard queries */ |
310 | 311 | ||
311 | if ((ntohs(head->flags) & 0x7800) != 0) | 312 | if ((ntohs(head->flags) & 0x7800) != 0) |
312 | goto empty_packet; | 313 | goto empty_packet; |
313 | 314 | ||
314 | // We have a standard query | 315 | // We have a standard query |
315 | 316 | ||
316 | log_message(LOG_FILE, (char *)head); | 317 | log_message(LOG_FILE, (char *)head); |
317 | lookup_result = table_lookup(type, answstr, (uint8_t*)(&head[1])); | 318 | lookup_result = table_lookup(type, answstr, (uint8_t*)(&head[1])); |
318 | if (lookup_result != 0) { | 319 | if (lookup_result != 0) { |
319 | outr.flags = 3 | 0x0400; //name do not exist and auth | 320 | outr.flags = 3 | 0x0400; //name do not exist and auth |
320 | goto empty_packet; | 321 | goto empty_packet; |
321 | } | 322 | } |
322 | if (type == REQ_A) { // return an address | 323 | if (type == REQ_A) { // return an address |
323 | struct in_addr a; | 324 | struct in_addr a; |
324 | if (!inet_aton((char*)answstr, &a)) {//dotted dec to long conv | 325 | if (!inet_aton((char*)answstr, &a)) {//dotted dec to long conv |
325 | outr.flags = 1; /* Frmt err */ | 326 | outr.flags = 1; /* Frmt err */ |
326 | goto empty_packet; | 327 | goto empty_packet; |
327 | } | 328 | } |
328 | memcpy(answstr, &a.s_addr, 4); // save before a disappears | 329 | memcpy(answstr, &a.s_addr, 4); // save before a disappears |
329 | outr.rlen = 4; // uint32_t IP | 330 | outr.rlen = 4; // uint32_t IP |
330 | } | 331 | } |
331 | else | 332 | else |
332 | outr.rlen = strlen((char *)answstr) + 1; // a host name | 333 | outr.rlen = strlen((char *)answstr) + 1; // a host name |
333 | outr.r = answstr; // 32 bit ip or a host name | 334 | outr.r = answstr; // 32 bit ip or a host name |
334 | outr.flags |= 0x0400; /* authority-bit */ | 335 | outr.flags |= 0x0400; /* authority-bit */ |
335 | // we have an answer | 336 | // we have an answer |
336 | head->nansw = htons(1); | 337 | head->nansw = htons(1); |
337 | 338 | ||
338 | // copy query block to answer block | 339 | // copy query block to answer block |
339 | len = answb - from; | 340 | len = answb - from; |
340 | memcpy(answb, from, len); | 341 | memcpy(answb, from, len); |
341 | next += len; | 342 | next += len; |
342 | 343 | ||
343 | // and append answer rr | 344 | // and append answer rr |
344 | *(uint32_t *) next = htonl(ttl); | 345 | *(uint32_t *) next = htonl(ttl); |
345 | next += 4; | 346 | next += 4; |
346 | *(uint16_t *) next = htons(outr.rlen); | 347 | *(uint16_t *) next = htons(outr.rlen); |
347 | next += 2; | 348 | next += 2; |
348 | memcpy(next, (void *)answstr, outr.rlen); | 349 | memcpy(next, (void *)answstr, outr.rlen); |
349 | next += outr.rlen; | 350 | next += outr.rlen; |
350 | 351 | ||
351 | empty_packet: | 352 | empty_packet: |
352 | 353 | ||
353 | flags = ntohs(head->flags); | 354 | flags = ntohs(head->flags); |
354 | // clear rcode and RA, set responsebit and our new flags | 355 | // clear rcode and RA, set responsebit and our new flags |
355 | flags |= (outr.flags & 0xff80) | 0x8000; | 356 | flags |= (outr.flags & 0xff80) | 0x8000; |
356 | head->flags = htons(flags); | 357 | head->flags = htons(flags); |
357 | head->nauth = head->nadd = htons(0); | 358 | head->nauth = head->nadd = htons(0); |
358 | head->nquer = htons(1); | 359 | head->nquer = htons(1); |
359 | 360 | ||
360 | packet_len = next - (void *)buf; | 361 | packet_len = next - (void *)buf; |
361 | return packet_len; | 362 | return packet_len; |
362 | } | 363 | } |
363 | 364 | ||
364 | /* | 365 | /* |
365 | * Exit on signal | 366 | * Exit on signal |
366 | */ | 367 | */ |
367 | static void | 368 | static void |
368 | interrupt(int x) | 369 | interrupt(int x) |
369 | { | 370 | { |
370 | unlink(LOCK_FILE); | 371 | unlink(LOCK_FILE); |
371 | write(2, "interrupt exiting\n", 18); | 372 | write(2, "interrupt exiting\n", 18); |
372 | exit(2); | 373 | exit(2); |
373 | } | 374 | } |
374 | 375 | ||
375 | #define is_daemon() (flags&16) | 376 | #define is_daemon() (flags&16) |
@@ -378,14 +379,14 @@ interrupt(int x) | |||
378 | 379 | ||
379 | int dnsd_main(int argc, char **argv) | 380 | int dnsd_main(int argc, char **argv) |
380 | { | 381 | { |
381 | int i, udps; | 382 | int udps; |
382 | uint16_t port = 53; | 383 | uint16_t port = 53; |
383 | uint8_t buf[MAX_PACK_LEN]; | 384 | uint8_t buf[MAX_PACK_LEN]; |
384 | unsigned long flags = 0; | 385 | unsigned long flags = 0; |
385 | char *listen_interface = "0.0.0.0"; | 386 | char *listen_interface = "0.0.0.0"; |
386 | char *sttl=NULL, *sport=NULL; | 387 | char *sttl=NULL, *sport=NULL; |
387 | 388 | ||
388 | if(argc > 1) | 389 | if(argc > 1) |
389 | flags = bb_getopt_ulflags(argc, argv, "i:c:t:p:dv", &listen_interface, &fileconf, &sttl, &sport); | 390 | flags = bb_getopt_ulflags(argc, argv, "i:c:t:p:dv", &listen_interface, &fileconf, &sttl, &sport); |
390 | if(sttl) | 391 | if(sttl) |
391 | if(!(ttl = atol(sttl))) | 392 | if(!(ttl = atol(sttl))) |
@@ -393,14 +394,14 @@ int dnsd_main(int argc, char **argv) | |||
393 | if(sport) | 394 | if(sport) |
394 | if(!(port = atol(sport))) | 395 | if(!(port = atol(sport))) |
395 | bb_show_usage(); | 396 | bb_show_usage(); |
396 | 397 | ||
397 | if(is_verbose()) { | 398 | if(is_verbose()) { |
398 | fprintf(stderr,"listen_interface: %s\n", listen_interface); | 399 | fprintf(stderr,"listen_interface: %s\n", listen_interface); |
399 | fprintf(stderr,"ttl: %d, port: %d\n", ttl, port); | 400 | fprintf(stderr,"ttl: %d, port: %d\n", ttl, port); |
400 | fprintf(stderr,"fileconf: %s\n", fileconf); | 401 | fprintf(stderr,"fileconf: %s\n", fileconf); |
401 | } | 402 | } |
402 | 403 | ||
403 | if(is_daemon()) | 404 | if(is_daemon()) |
404 | #if defined(__uClinux__) | 405 | #if defined(__uClinux__) |
405 | /* reexec for vfork() do continue parent */ | 406 | /* reexec for vfork() do continue parent */ |
406 | vfork_daemon_rexec(1, 0, argc, argv, "-d"); | 407 | vfork_daemon_rexec(1, 0, argc, argv, "-d"); |
@@ -409,60 +410,60 @@ int dnsd_main(int argc, char **argv) | |||
409 | bb_perror_msg_and_die("daemon"); | 410 | bb_perror_msg_and_die("daemon"); |
410 | } | 411 | } |
411 | #endif /* uClinuvx */ | 412 | #endif /* uClinuvx */ |
412 | |||
413 | dnsentryinit(is_verbose()); | ||
414 | 413 | ||
415 | signal(SIGINT, interrupt); | 414 | dnsentryinit(is_verbose()); |
416 | signal(SIGPIPE, SIG_IGN); | 415 | |
417 | signal(SIGHUP, SIG_IGN); | 416 | signal(SIGINT, interrupt); |
417 | signal(SIGPIPE, SIG_IGN); | ||
418 | signal(SIGHUP, SIG_IGN); | ||
418 | #ifdef SIGTSTP | 419 | #ifdef SIGTSTP |
419 | signal(SIGTSTP, SIG_IGN); | 420 | signal(SIGTSTP, SIG_IGN); |
420 | #endif | 421 | #endif |
421 | #ifdef SIGURG | 422 | #ifdef SIGURG |
422 | signal(SIGURG, SIG_IGN); | 423 | signal(SIGURG, SIG_IGN); |
423 | #endif | 424 | #endif |
424 | 425 | ||
425 | udps = listen_socket(listen_interface, port); | 426 | udps = listen_socket(listen_interface, port); |
426 | if (udps < 0) | 427 | if (udps < 0) |
427 | exit(1); | 428 | exit(1); |
428 | 429 | ||
429 | while (1) { | 430 | while (1) { |
430 | fd_set fdset; | 431 | fd_set fdset; |
431 | int r; | 432 | int r; |
432 | 433 | ||
433 | FD_ZERO(&fdset); | 434 | FD_ZERO(&fdset); |
434 | FD_SET(udps, &fdset); | 435 | FD_SET(udps, &fdset); |
435 | // Block until a message arrives | 436 | // Block until a message arrives |
436 | if((r = select(udps + 1, &fdset, NULL, NULL, NULL)) < 0) | 437 | if((r = select(udps + 1, &fdset, NULL, NULL, NULL)) < 0) |
437 | bb_perror_msg_and_die("select error"); | 438 | bb_perror_msg_and_die("select error"); |
438 | else | 439 | else |
439 | if(r == 0) | 440 | if(r == 0) |
440 | bb_perror_msg_and_die("select spurious return"); | 441 | bb_perror_msg_and_die("select spurious return"); |
441 | 442 | ||
442 | /* Can this test ever be false? */ | 443 | /* Can this test ever be false? */ |
443 | if (FD_ISSET(udps, &fdset)) { | 444 | if (FD_ISSET(udps, &fdset)) { |
444 | struct sockaddr_in from; | 445 | struct sockaddr_in from; |
445 | int fromlen = sizeof(from); | 446 | int fromlen = sizeof(from); |
446 | r = recvfrom(udps, buf, sizeof(buf), 0, | 447 | r = recvfrom(udps, buf, sizeof(buf), 0, |
447 | (struct sockaddr *)&from, | 448 | (struct sockaddr *)&from, |
448 | (void *)&fromlen); | 449 | (void *)&fromlen); |
449 | if(is_verbose()) | 450 | if(is_verbose()) |
450 | fprintf(stderr, "\n--- Got UDP "); | 451 | fprintf(stderr, "\n--- Got UDP "); |
451 | log_message(LOG_FILE, "\n--- Got UDP "); | 452 | log_message(LOG_FILE, "\n--- Got UDP "); |
452 | 453 | ||
453 | if (r < 12 || r > 512) { | 454 | if (r < 12 || r > 512) { |
454 | bb_error_msg("invalid packet size"); | 455 | bb_error_msg("invalid packet size"); |
455 | continue; | 456 | continue; |
456 | } | 457 | } |
457 | if (r > 0) { | 458 | if (r > 0) { |
458 | r = process_packet(buf); | 459 | r = process_packet(buf); |
459 | if (r > 0) | 460 | if (r > 0) |
460 | sendto(udps, buf, | 461 | sendto(udps, buf, |
461 | r, 0, (struct sockaddr *)&from, | 462 | r, 0, (struct sockaddr *)&from, |
462 | fromlen); | 463 | fromlen); |
463 | } | 464 | } |
464 | } // end if | 465 | } // end if |
465 | } // end while | 466 | } // end while |
466 | return 0; | 467 | return 0; |
467 | } | 468 | } |
468 | 469 | ||