diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-03-24 12:13:04 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-03-24 12:13:04 +0000 |
commit | d3bac03988b104998c90dbce50e152668c3e4f5d (patch) | |
tree | 3c140f23588b0eff532e7c9c9431ea12daaaaf87 | |
parent | 9067f13be067f39f6c8586926b190b7dee0def3d (diff) | |
download | busybox-w32-d3bac03988b104998c90dbce50e152668c3e4f5d.tar.gz busybox-w32-d3bac03988b104998c90dbce50e152668c3e4f5d.tar.bz2 busybox-w32-d3bac03988b104998c90dbce50e152668c3e4f5d.zip |
dnsd: fix several buglets, make smaller
-rw-r--r-- | include/usage.h | 2 | ||||
-rw-r--r-- | networking/dnsd.c | 104 |
2 files changed, 42 insertions, 64 deletions
diff --git a/include/usage.h b/include/usage.h index 66f8638f5..615ee4ed7 100644 --- a/include/usage.h +++ b/include/usage.h | |||
@@ -673,7 +673,7 @@ | |||
673 | " -c Config filename\n" \ | 673 | " -c Config filename\n" \ |
674 | " -t TTL in seconds\n" \ | 674 | " -t TTL in seconds\n" \ |
675 | " -p Listening port\n" \ | 675 | " -p Listening port\n" \ |
676 | " -i Listening iface ip (default all)\n" \ | 676 | " -i Listening ip (default all)\n" \ |
677 | " -d Daemonize" | 677 | " -d Daemonize" |
678 | 678 | ||
679 | #define dos2unix_trivial_usage \ | 679 | #define dos2unix_trivial_usage \ |
diff --git a/networking/dnsd.c b/networking/dnsd.c index fb0c56cce..fd2f421d7 100644 --- a/networking/dnsd.c +++ b/networking/dnsd.c | |||
@@ -19,13 +19,6 @@ | |||
19 | 19 | ||
20 | #include "busybox.h" | 20 | #include "busybox.h" |
21 | 21 | ||
22 | static const char *fileconf = "/etc/dnsd.conf"; | ||
23 | #define LOCK_FILE "/var/run/dnsd.lock" | ||
24 | |||
25 | // Must match getopt32 call | ||
26 | #define OPT_daemon (option_mask32 & 0x10) | ||
27 | #define OPT_verbose (option_mask32 & 0x20) | ||
28 | |||
29 | //#define DEBUG 1 | 22 | //#define DEBUG 1 |
30 | #define DEBUG 0 | 23 | #define DEBUG 0 |
31 | 24 | ||
@@ -75,9 +68,16 @@ struct dns_entry { // element of known name, ip address and reversed ip address | |||
75 | char name[MAX_HOST_LEN]; | 68 | char name[MAX_HOST_LEN]; |
76 | }; | 69 | }; |
77 | 70 | ||
78 | static struct dns_entry *dnsentry = NULL; | 71 | static struct dns_entry *dnsentry; |
79 | static uint32_t ttl = DEFAULT_TTL; | 72 | static uint32_t ttl = DEFAULT_TTL; |
80 | 73 | ||
74 | static const char *fileconf = "/etc/dnsd.conf"; | ||
75 | |||
76 | // Must match getopt32 call | ||
77 | #define OPT_daemon (option_mask32 & 0x10) | ||
78 | #define OPT_verbose (option_mask32 & 0x20) | ||
79 | |||
80 | |||
81 | /* | 81 | /* |
82 | * Convert host name from C-string to dns length/string. | 82 | * Convert host name from C-string to dns length/string. |
83 | */ | 83 | */ |
@@ -135,12 +135,12 @@ static int getfileentry(FILE * fp, struct dns_entry *s) | |||
135 | while (*r != ' ' && *r != '\t') | 135 | while (*r != ' ' && *r != '\t') |
136 | r++; | 136 | r++; |
137 | *r++ = '\0'; | 137 | *r++ = '\0'; |
138 | if (sscanf(r, "%u.%u.%u.%u", &a, &b, &c, &d) != 4) { | 138 | if (sscanf(r, ".%u.%u.%u.%u"+1, &a, &b, &c, &d) != 4) { |
139 | free(line); | 139 | free(line); |
140 | goto restart; /* skipping wrong lines */ | 140 | goto restart; /* skipping wrong lines */ |
141 | } | 141 | } |
142 | 142 | ||
143 | sprintf(s->ip, "%u.%u.%u.%u", a, b, c, d); | 143 | sprintf(s->ip, ".%u.%u.%u.%u"+1, a, b, c, d); |
144 | sprintf(s->rip, ".%u.%u.%u.%u", d, c, b, a); | 144 | sprintf(s->rip, ".%u.%u.%u.%u", d, c, b, a); |
145 | undot((uint8_t*)s->rip); | 145 | undot((uint8_t*)s->rip); |
146 | convname(s->name, (uint8_t*)name); | 146 | convname(s->name, (uint8_t*)name); |
@@ -185,7 +185,7 @@ static void dnsentryinit(void) | |||
185 | static int table_lookup(uint16_t type, uint8_t * as, uint8_t * qs) | 185 | static int table_lookup(uint16_t type, uint8_t * as, uint8_t * qs) |
186 | { | 186 | { |
187 | int i; | 187 | int i; |
188 | struct dns_entry *d=dnsentry; | 188 | struct dns_entry *d = dnsentry; |
189 | 189 | ||
190 | do { | 190 | do { |
191 | #if DEBUG | 191 | #if DEBUG |
@@ -201,17 +201,13 @@ static int table_lookup(uint16_t type, uint8_t * as, uint8_t * qs) | |||
201 | if (tolower(qs[i]) != d->name[i]) | 201 | if (tolower(qs[i]) != d->name[i]) |
202 | break; | 202 | break; |
203 | if (i > (int)(d->name[0])) { | 203 | if (i > (int)(d->name[0])) { |
204 | #if DEBUG | ||
205 | fprintf(stderr, " OK"); | ||
206 | #endif | ||
207 | strcpy((char *)as, d->ip); | 204 | strcpy((char *)as, d->ip); |
208 | #if DEBUG | 205 | #if DEBUG |
209 | fprintf(stderr, " as:%s\n", as); | 206 | fprintf(stderr, " OK as:%s\n", as); |
210 | #endif | 207 | #endif |
211 | return 0; | 208 | return 0; |
212 | } | 209 | } |
213 | } else | 210 | } else if (type == REQ_PTR) { /* search by IP-address */ |
214 | if (type == REQ_PTR) { /* search by IP-address */ | ||
215 | if (!strncmp((char*)&d->rip[1], (char*)&qs[1], strlen(d->rip)-1)) { | 211 | if (!strncmp((char*)&d->rip[1], (char*)&qs[1], strlen(d->rip)-1)) { |
216 | strcpy((char *)as, d->name); | 212 | strcpy((char *)as, d->name); |
217 | return 0; | 213 | return 0; |
@@ -226,7 +222,6 @@ static int table_lookup(uint16_t type, uint8_t * as, uint8_t * qs) | |||
226 | /* | 222 | /* |
227 | * Decode message and generate answer | 223 | * Decode message and generate answer |
228 | */ | 224 | */ |
229 | #define eret(s) do { fputs(s, stderr); return -1; } while (0) | ||
230 | static int process_packet(uint8_t * buf) | 225 | static int process_packet(uint8_t * buf) |
231 | { | 226 | { |
232 | struct dns_head *head; | 227 | struct dns_head *head; |
@@ -241,11 +236,15 @@ static int process_packet(uint8_t * buf) | |||
241 | answstr[0] = '\0'; | 236 | answstr[0] = '\0'; |
242 | 237 | ||
243 | head = (struct dns_head *)buf; | 238 | head = (struct dns_head *)buf; |
244 | if (head->nquer == 0) | 239 | if (head->nquer == 0) { |
245 | eret("no queries\n"); | 240 | bb_error_msg("no queries"); |
241 | retunr -1; | ||
242 | } | ||
246 | 243 | ||
247 | if (head->flags & 0x8000) | 244 | if (head->flags & 0x8000) { |
248 | eret("ignoring response packet\n"); | 245 | bb_error_msg("ignoring response packet"); |
246 | retunr -1; | ||
247 | } | ||
249 | 248 | ||
250 | from = (void *)&head[1]; // start of query string | 249 | from = (void *)&head[1]; // start of query string |
251 | next = answb = from + strlen((char *)from) + 1 + sizeof(struct dns_prop); // where to append answer block | 250 | next = answb = from + strlen((char *)from) + 1 + sizeof(struct dns_prop); // where to append answer block |
@@ -286,8 +285,7 @@ static int process_packet(uint8_t * buf) | |||
286 | } | 285 | } |
287 | memcpy(answstr, &a.s_addr, 4); // save before a disappears | 286 | memcpy(answstr, &a.s_addr, 4); // save before a disappears |
288 | outr.rlen = 4; // uint32_t IP | 287 | outr.rlen = 4; // uint32_t IP |
289 | } | 288 | } else |
290 | else | ||
291 | outr.rlen = strlen((char *)answstr) + 1; // a host name | 289 | outr.rlen = strlen((char *)answstr) + 1; // a host name |
292 | outr.r = answstr; // 32 bit ip or a host name | 290 | outr.r = answstr; // 32 bit ip or a host name |
293 | outr.flags |= 0x0400; /* authority-bit */ | 291 | outr.flags |= 0x0400; /* authority-bit */ |
@@ -316,7 +314,7 @@ static int process_packet(uint8_t * buf) | |||
316 | head->nauth = head->nadd = htons(0); | 314 | head->nauth = head->nadd = htons(0); |
317 | head->nquer = htons(1); | 315 | head->nquer = htons(1); |
318 | 316 | ||
319 | packet_len = next - (void *)buf; | 317 | packet_len = (uint8_t *)next - buf; |
320 | return packet_len; | 318 | return packet_len; |
321 | } | 319 | } |
322 | 320 | ||
@@ -325,7 +323,7 @@ static int process_packet(uint8_t * buf) | |||
325 | */ | 323 | */ |
326 | static void interrupt(int x) | 324 | static void interrupt(int x) |
327 | { | 325 | { |
328 | unlink(LOCK_FILE); | 326 | /* unlink("/var/run/dnsd.lock"); */ |
329 | bb_error_msg("interrupt, exiting\n"); | 327 | bb_error_msg("interrupt, exiting\n"); |
330 | exit(2); | 328 | exit(2); |
331 | } | 329 | } |
@@ -333,7 +331,7 @@ static void interrupt(int x) | |||
333 | int dnsd_main(int argc, char **argv); | 331 | int dnsd_main(int argc, char **argv); |
334 | int dnsd_main(int argc, char **argv) | 332 | int dnsd_main(int argc, char **argv) |
335 | { | 333 | { |
336 | char *listen_interface = NULL; | 334 | const char *listen_interface = "0.0.0.0"; |
337 | char *sttl, *sport; | 335 | char *sttl, *sport; |
338 | len_and_sockaddr *lsa; | 336 | len_and_sockaddr *lsa; |
339 | int udps; | 337 | int udps; |
@@ -346,7 +344,7 @@ int dnsd_main(int argc, char **argv) | |||
346 | if (option_mask32 & 0x4) // -t | 344 | if (option_mask32 & 0x4) // -t |
347 | ttl = xatou_range(sttl, 1, 0xffffffff); | 345 | ttl = xatou_range(sttl, 1, 0xffffffff); |
348 | if (option_mask32 & 0x8) // -p | 346 | if (option_mask32 & 0x8) // -p |
349 | port = xatou_range(sttl, 1, 0xffff); | 347 | port = xatou_range(sport, 1, 0xffff); |
350 | 348 | ||
351 | if (OPT_verbose) { | 349 | if (OPT_verbose) { |
352 | bb_info_msg("listen_interface: %s", listen_interface); | 350 | bb_info_msg("listen_interface: %s", listen_interface); |
@@ -355,7 +353,6 @@ int dnsd_main(int argc, char **argv) | |||
355 | } | 353 | } |
356 | 354 | ||
357 | if (OPT_daemon) { | 355 | if (OPT_daemon) { |
358 | //FIXME: NOMMU will NOT set LOGMODE_SYSLOG! | ||
359 | #ifdef BB_NOMMU | 356 | #ifdef BB_NOMMU |
360 | if (!re_execed) | 357 | if (!re_execed) |
361 | vfork_daemon_rexec(1, 0, argv); | 358 | vfork_daemon_rexec(1, 0, argv); |
@@ -368,7 +365,7 @@ int dnsd_main(int argc, char **argv) | |||
368 | dnsentryinit(); | 365 | dnsentryinit(); |
369 | 366 | ||
370 | signal(SIGINT, interrupt); | 367 | signal(SIGINT, interrupt); |
371 | signal(SIGPIPE, SIG_IGN); | 368 | /* why? signal(SIGPIPE, SIG_IGN); */ |
372 | signal(SIGHUP, SIG_IGN); | 369 | signal(SIGHUP, SIG_IGN); |
373 | #ifdef SIGTSTP | 370 | #ifdef SIGTSTP |
374 | signal(SIGTSTP, SIG_IGN); | 371 | signal(SIGTSTP, SIG_IGN); |
@@ -377,50 +374,31 @@ int dnsd_main(int argc, char **argv) | |||
377 | signal(SIGURG, SIG_IGN); | 374 | signal(SIGURG, SIG_IGN); |
378 | #endif | 375 | #endif |
379 | 376 | ||
380 | lsa = xhost2sockaddr(listen_interface, port); | 377 | lsa = xdotted2sockaddr(listen_interface, port); |
381 | udps = xsocket(lsa->sa.sa_family, SOCK_DGRAM, 0); | 378 | udps = xsocket(lsa->sa.sa_family, SOCK_DGRAM, 0); |
382 | xbind(udps, &lsa->sa, lsa->len); | 379 | xbind(udps, &lsa->sa, lsa->len); |
383 | // xlisten(udps, 50); - ?!! DGRAM sockets are never listened on I think? | 380 | /* xlisten(udps, 50); - ?!! DGRAM sockets are never listened on I think? */ |
384 | bb_info_msg("Accepting UDP packets on %s", | 381 | bb_info_msg("Accepting UDP packets on %s", |
385 | xmalloc_sockaddr2dotted(&lsa->sa, lsa->len)); | 382 | xmalloc_sockaddr2dotted(&lsa->sa, lsa->len)); |
386 | 383 | ||
387 | while (1) { | 384 | while (1) { |
388 | fd_set fdset; | ||
389 | int r; | 385 | int r; |
390 | 386 | socklen_t fromlen = lsa->len; | |
391 | FD_ZERO(&fdset); | ||
392 | FD_SET(udps, &fdset); | ||
393 | // Block until a message arrives | ||
394 | // FIXME: Fantastic. select'ing on just one fd?? | ||
395 | // Why no just block on it doing recvfrom() ? | ||
396 | r = select(udps + 1, &fdset, NULL, NULL, NULL); | ||
397 | if (r < 0) | ||
398 | bb_perror_msg_and_die("select error"); | ||
399 | if (r == 0) | ||
400 | bb_perror_msg_and_die("select spurious return"); | ||
401 | |||
402 | /* Can this test ever be false? - yes */ | ||
403 | if (FD_ISSET(udps, &fdset)) { | ||
404 | socklen_t fromlen = lsa->len; | ||
405 | // FIXME: need to get *DEST* address (to which of our addresses | 387 | // FIXME: need to get *DEST* address (to which of our addresses |
406 | // this query was directed), and reply from the same address. | 388 | // this query was directed), and reply from the same address. |
407 | // Or else we can exhibit usual UDP ugliness: | 389 | // Or else we can exhibit usual UDP ugliness: |
408 | // [ip1.multihomed.ip2] <= query to ip1 <= peer | 390 | // [ip1.multihomed.ip2] <= query to ip1 <= peer |
409 | // [ip1.multihomed.ip2] => reply from ip2 => peer (confused) | 391 | // [ip1.multihomed.ip2] => reply from ip2 => peer (confused) |
410 | r = recvfrom(udps, buf, sizeof(buf), 0, &lsa->sa, &fromlen); | 392 | r = recvfrom(udps, buf, sizeof(buf), 0, &lsa->sa, &fromlen); |
411 | if (OPT_verbose) | 393 | if (OPT_verbose) |
412 | bb_info_msg("Got UDP packet"); | 394 | bb_info_msg("Got UDP packet"); |
413 | 395 | if (r < 12 || r > 512) { | |
414 | if (r < 12 || r > 512) { | 396 | bb_error_msg("invalid packet size"); |
415 | bb_error_msg("invalid packet size"); | 397 | continue; |
416 | continue; | ||
417 | } | ||
418 | if (r <= 0) | ||
419 | continue; | ||
420 | r = process_packet(buf); | ||
421 | if (r <= 0) | ||
422 | continue; | ||
423 | sendto(udps, buf, r, 0, &lsa->sa, fromlen); | ||
424 | } | 398 | } |
399 | r = process_packet(buf); | ||
400 | if (r <= 0) | ||
401 | continue; | ||
402 | sendto(udps, buf, r, 0, &lsa->sa, fromlen); | ||
425 | } | 403 | } |
426 | } | 404 | } |