X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=networking%2Fdnsd.c;h=fe98400f7d6047fc38c40e02f317afa8e51b4c2d;hb=1f56e51ca1d96b70635eb1b9df1d1ab0edd98a72;hp=91d66edccfa5db5239e3261579ba1815ae96a69e;hpb=5fb38491e3ef093fb9cf642f0f3da5f2ed051f33;p=oweals%2Fbusybox.git diff --git a/networking/dnsd.c b/networking/dnsd.c index 91d66edcc..fe98400f7 100644 --- a/networking/dnsd.c +++ b/networking/dnsd.c @@ -6,7 +6,7 @@ * Copyright (C) 2005 Odd Arild Olsen (oao at fibula dot no) * Copyright (C) 2003 Paul Sheer * - * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. + * Licensed under GPLv2 or later, see file LICENSE in this source tree. * * Odd Arild Olsen started out with the sheerdns [1] of Paul Sheer and rewrote * it into a shape which I believe is both easier to understand and maintain. @@ -17,6 +17,21 @@ * the first porting of oao' scdns to busybox also. */ +//usage:#define dnsd_trivial_usage +//usage: "[-dvs] [-c CONFFILE] [-t TTL_SEC] [-p PORT] [-i ADDR]" +//usage:#define dnsd_full_usage "\n\n" +//usage: "Small static DNS server daemon\n" +//usage: "\n -c FILE Config file" +//usage: "\n -t SEC TTL" +//usage: "\n -p PORT Listen on PORT" +//usage: "\n -i ADDR Listen on ADDR" +//usage: "\n -d Daemonize" +//usage: "\n -v Verbose" +//usage: "\n -s Send successful replies only. Use this if you want" +//usage: "\n to use /etc/resolv.conf with two nameserver lines:" +//usage: "\n nameserver DNSD_SERVER" +//usage: "\n nameserver NORMAL_DNS_SERVER" + #include "libbb.h" #include @@ -44,10 +59,15 @@ struct dns_head { uint16_t nauth; uint16_t nadd; }; +/* Structure used to access type and class fields. + * They are totally unaligned, but gcc 4.3.4 thinks that pointer of type uint16_t* + * is 16-bit aligned and replaces 16-bit memcpy (in move_from_unaligned16 macro) + * with aligned halfword access on arm920t! + * Oh well. Slapping PACKED everywhere seems to help: */ struct type_and_class { - uint16_t type; - uint16_t class; -}; + uint16_t type PACKED; + uint16_t class PACKED; +} PACKED; /* element of known name, ip address and reversed ip address */ struct dns_entry { struct dns_entry *next; @@ -56,7 +76,8 @@ struct dns_entry { char name[1]; }; -#define OPT_verbose (option_mask32) +#define OPT_verbose (option_mask32 & 1) +#define OPT_silent (option_mask32 & 2) /* @@ -351,10 +372,11 @@ static int process_packet(struct dns_entry *conf_data, uint32_t conf_ttl, uint8_t *buf) { - char *answstr; struct dns_head *head; struct type_and_class *unaligned_type_class; + const char *err_msg; char *query_string; + char *answstr; uint8_t *answb; uint16_t outr_rlen; uint16_t outr_flags; @@ -365,13 +387,15 @@ static int process_packet(struct dns_entry *conf_data, head = (struct dns_head *)buf; if (head->nquer == 0) { bb_error_msg("packet has 0 queries, ignored"); - return -1; + return 0; /* don't reply */ } - if (head->flags & htons(0x8000)) { /* QR bit */ bb_error_msg("response packet, ignored"); - return -1; + return 0; /* don't reply */ } + /* QR = 1 "response", RCODE = 4 "Not Implemented" */ + outr_flags = htons(0x8000 | 4); + err_msg = NULL; /* start of query string */ query_string = (void *)(head + 1); @@ -379,33 +403,34 @@ static int process_packet(struct dns_entry *conf_data, query_len = strlen(query_string) + 1; /* may be unaligned! */ unaligned_type_class = (void *)(query_string + query_len); - query_len += sizeof(unaligned_type_class); + query_len += sizeof(*unaligned_type_class); /* where to append answer block */ answb = (void *)(unaligned_type_class + 1); - /* QR = 1 "response", RCODE = 4 "Not Implemented" */ - outr_flags = htons(0x8000 | 4); - - move_from_unaligned16(type, &unaligned_type_class->type); - if (type != htons(REQ_A) && type != htons(REQ_PTR)) { - /* we can't handle the query type */ + /* OPCODE != 0 "standard query"? */ + if ((head->flags & htons(0x7800)) != 0) { + err_msg = "opcode != 0"; goto empty_packet; } move_from_unaligned16(class, &unaligned_type_class->class); if (class != htons(1)) { /* not class INET? */ + err_msg = "class != 1"; goto empty_packet; } - /* OPCODE != 0 "standard query" ? */ - if ((head->flags & htons(0x7800)) != 0) { + move_from_unaligned16(type, &unaligned_type_class->type); + if (type != htons(REQ_A) && type != htons(REQ_PTR)) { + /* we can't handle this query type */ +//TODO: happens all the time with REQ_AAAA (0x1c) requests - implement those? + err_msg = "type is !REQ_A and !REQ_PTR"; goto empty_packet; } /* look up the name */ + answstr = table_lookup(conf_data, type, query_string); #if DEBUG - /* need to convert lengths to dots before we can use it in non-debug */ - bb_info_msg("%s", query_string); + /* Shows lengths instead of dots, unusable for !DEBUG */ + bb_error_msg("'%s'->'%s'", query_string, answstr); #endif - answstr = table_lookup(conf_data, type, query_string); outr_rlen = 4; if (answstr && type == htons(REQ_PTR)) { /* returning a host name */ @@ -417,6 +442,7 @@ static int process_packet(struct dns_entry *conf_data, /* QR = 1 "response" * AA = 1 "Authoritative Answer" * RCODE = 3 "Name Error" */ + err_msg = "name is not found"; outr_flags = htons(0x8000 | 0x0400 | 3); goto empty_packet; } @@ -438,11 +464,23 @@ static int process_packet(struct dns_entry *conf_data, * "** server can't find HOSTNAME" * RCODE = 0 "success" */ + if (OPT_verbose) + bb_error_msg("returning positive reply"); outr_flags = htons(0x8000 | 0x0400 | 0); /* we have one answer */ head->nansw = htons(1); empty_packet: + if ((outr_flags & htons(0xf)) != 0) { /* not a positive response */ + if (OPT_verbose) { + bb_error_msg("%s, %s", + err_msg, + OPT_silent ? "dropping query" : "sending error reply" + ); + } + if (OPT_silent) + return 0; + } head->flags |= outr_flags; head->nauth = head->nadd = 0; head->nquer = htons(1); // why??? @@ -462,23 +500,23 @@ int dnsd_main(int argc UNUSED_PARAM, char **argv) unsigned lsa_size; int udps, opts; uint16_t port = 53; - uint8_t buf[MAX_PACK_LEN + 1]; - - opts = getopt32(argv, "vi:c:t:p:d", &listen_interface, &fileconf, &sttl, &sport); - //if (opts & 0x1) // -v - //if (opts & 0x2) // -i - //if (opts & 0x4) // -c - if (opts & 0x8) // -t + /* Ensure buf is 32bit aligned (we need 16bit, but 32bit can't hurt) */ + uint8_t buf[MAX_PACK_LEN + 1] ALIGN4; + + opts = getopt32(argv, "vsi:c:t:p:d", &listen_interface, &fileconf, &sttl, &sport); + //if (opts & (1 << 0)) // -v + //if (opts & (1 << 1)) // -s + //if (opts & (1 << 2)) // -i + //if (opts & (1 << 3)) // -c + if (opts & (1 << 4)) // -t conf_ttl = xatou_range(sttl, 1, 0xffffffff); - if (opts & 0x10) // -p + if (opts & (1 << 5)) // -p port = xatou_range(sport, 1, 0xffff); - if (opts & 0x20) { // -d + if (opts & (1 << 6)) { // -d bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv); openlog(applet_name, LOG_PID, LOG_DAEMON); logmode = LOGMODE_SYSLOG; } - /* Clear all except "verbose" bit */ - option_mask32 &= 1; conf_data = parse_conf_file(fileconf); @@ -492,7 +530,7 @@ int dnsd_main(int argc UNUSED_PARAM, char **argv) { char *p = xmalloc_sockaddr2dotted(&lsa->u.sa); - bb_info_msg("Accepting UDP packets on %s", p); + bb_error_msg("accepting UDP packets on %s", p); free(p); } @@ -510,7 +548,7 @@ int dnsd_main(int argc UNUSED_PARAM, char **argv) continue; } if (OPT_verbose) - bb_info_msg("Got UDP packet"); + bb_error_msg("got UDP packet"); buf[r] = '\0'; /* paranoia */ r = process_packet(conf_data, conf_ttl, buf); if (r <= 0)