* 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.
* 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 <syslog.h>
#define DEBUG 0
enum {
- /* Can tweak this */
+ /* can tweak this */
DEFAULT_TTL = 120,
-/* Cannot get bigger packets than 512 per RFC1035.
- * In practice this can be set considerably smaller:
- * Length of response packet is header (12B) + 2*type(4B) + 2*class(4B) +
- * ttl(4B) + rlen(2B) + r (MAX_NAME_LEN = 21B) +
- * 2*querystring (2 MAX_NAME_LEN = 42B), all together 90 Bytes
- */
+ /* cannot get bigger packets than 512 per RFC1035. */
MAX_PACK_LEN = 512,
IP_STRING_LEN = sizeof(".xxx.xxx.xxx.xxx"),
MAX_NAME_LEN = IP_STRING_LEN - 1 + sizeof(".in-addr.arpa"),
uint16_t nauth;
uint16_t nadd;
};
-struct dns_prop {
- uint16_t type;
- uint16_t class;
-};
+/* 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 PACKED;
+ uint16_t class PACKED;
+} PACKED;
/* element of known name, ip address and reversed ip address */
struct dns_entry {
struct dns_entry *next;
char name[1];
};
-#define OPT_verbose (option_mask32)
+#define OPT_verbose (option_mask32 & 1)
+#define OPT_silent (option_mask32 & 2)
/*
/*
* Look query up in dns records and return answer if found.
- * qs is the query string.
*/
-static int table_lookup(uint8_t *as, struct dns_entry *d, uint16_t type, uint8_t *qs)
+static char *table_lookup(struct dns_entry *d,
+ uint16_t type,
+ char* query_string)
{
while (d) {
unsigned len = d->name[0];
/* d->name[len] is the last (non NUL) char */
#if DEBUG
char *p, *q;
- q = (char *)&(qs[1]);
- p = &(d->name[1]);
+ q = query_string + 1;
+ p = d->name + 1;
fprintf(stderr, "%d/%d p:%s q:%s %d\n",
(int)strlen(p), len,
p, q, (int)strlen(q)
if (type == htons(REQ_A)) {
/* search by host name */
if (len != 1 || d->name[1] != '*') {
- if (strcasecmp(d->name, (char*)qs) != 0)
+/* we are lax, hope no name component is ever >64 so that length
+ * (which will be represented as 'A','B'...) matches a lowercase letter.
+ * Actually, I think false matches are hard to construct.
+ * Example.
+ * [31] len is represented as '1', [65] as 'A', [65+32] as 'a'.
+ * [65] <65 same chars>[31]<31 same chars>NUL
+ * [65+32]<65 same chars>1 <31 same chars>NUL
+ * This example seems to be the minimal case when false match occurs.
+ */
+ if (strcasecmp(d->name, query_string) != 0)
goto next;
}
- move_to_unaligned32((uint32_t *)as, d->ip);
+ return (char *)&d->ip;
#if DEBUG
- fprintf(stderr, "OK as:%x\n", (int)d->ip);
+ fprintf(stderr, "Found IP:%x\n", (int)d->ip);
#endif
return 0;
}
/* search by IP-address */
if ((len != 1 || d->name[1] != '*')
- /* assume (do not check) that qs ends in ".in-addr.arpa" */
- && strncmp(d->rip, (char*)qs, strlen(d->rip)) == 0
+ /* we assume (do not check) that query_string
+ * ends in ".in-addr.arpa" */
+ && strncmp(d->rip, query_string, strlen(d->rip)) == 0
) {
- strcpy((char *)as, d->name);
#if DEBUG
- fprintf(stderr, "OK as:%s\n", as);
+ fprintf(stderr, "Found name:%s\n", d->name);
#endif
- return 0;
+ return d->name;
}
next:
d = d->next;
}
- return -1;
+ return NULL;
}
/*
...
4.1.1. Header section format
- 1 1 1 1 1 1
- 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
+ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| ID |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
- |QR| OPCODE |AA|TC|RD|RA| Z | RCODE |
+ |QR| OPCODE |AA|TC|RD|RA| 0 0 0| RCODE |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| QDCOUNT |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| ARCOUNT |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
-ID 16 bit random identifier assigned by query.
+ID 16 bit random identifier assigned by querying peer.
Used to match query/response.
QR message is a query (0), or a response (1).
OPCODE 0 standard query (QUERY)
1 inverse query (IQUERY)
2 server status request (STATUS)
-AA Authoritative Answer - this bit is valid in responses,
- and specifies that the responding name server is an
- authority for the domain name in question section.
- Note that the contents of the answer section may have
- multiple owner names because of aliases. The AA bit
- corresponds to the name which matches the query name, or
- the first owner name in the answer section.
-TC TrunCation - specifies that this message was truncated.
+AA Authoritative Answer - this bit is valid in responses.
+ Responding name server is an authority for the domain name
+ in question section. Answer section may have multiple owner names
+ because of aliases. The AA bit corresponds to the name which matches
+ the query name, or the first owner name in the answer section.
+TC TrunCation - this message was truncated.
RD Recursion Desired - this bit may be set in a query and
is copied into the response. If RD is set, it directs
the name server to pursue the query recursively.
RA Recursion Available - this be is set or cleared in a
response, and denotes whether recursive query support is
available in the name server.
-Z Reserved for future use. Must be zero.
RCODE Response code.
0 No error condition
1 Format error
- 2 Server failure - The name server was
- unable to process this query due to a
- problem with the name server.
- 3 Name Error - Meaningful only for
- responses from an authoritative name
- server, this code signifies that the
- domain name referenced in the query does
- not exist.
+ 2 Server failure - server was unable to process the query
+ due to a problem with the name server.
+ 3 Name Error - meaningful only for responses from
+ an authoritative name server. The referenced domain name
+ does not exist.
4 Not Implemented.
5 Refused.
QDCOUNT number of entries in the question section.
-ANCOUNT number of resource records in the answer section.
-NSCOUNT number of name server resource records in the authority records section.
-ARCOUNT number of resource records in the additional records section.
+ANCOUNT number of records in the answer section.
+NSCOUNT number of records in the authority records section.
+ARCOUNT number of records in the additional records section.
4.1.2. Question section format
-The section contains QDCOUNT (usually 1) entries, each of the following format:
- 1 1 1 1 1 1
- 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
+The section contains QDCOUNT (usually 1) entries, each of this format:
+ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
/ QNAME /
/ /
255 a request for all records
QCLASS a two octet code that specifies the class of the query.
1 the Internet
- (others are historic only)
+ (others are historic only)
255 any class
-4.1.3. Resource record format
+4.1.3. Resource Record format
-The answer, authority, and additional sections all share the same
-format: a variable number of resource records, where the number of
-records is specified in the corresponding count field in the header.
-Each resource record has the following format:
- 1 1 1 1 1 1
- 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
+The answer, authority, and additional sections all share the same format:
+a variable number of resource records, where the number of records
+is specified in the corresponding count field in the header.
+Each resource record has this format:
+ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
/ /
/ NAME /
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
NAME a domain name to which this resource record pertains.
TYPE two octets containing one of the RR type codes. This
- field specifies the meaning of the data in the RDATA
- field.
-CLASS two octets which specify the class of the data in the
- RDATA field.
-TTL a 32 bit unsigned integer that specifies the time
- interval (in seconds) that the resource record may be
- cached before it should be discarded. Zero values are
- interpreted to mean that the RR can only be used for the
- transaction in progress, and should not be cached.
-RDLENGTH an unsigned 16 bit integer that specifies the length in
- octets of the RDATA field.
-RDATA a variable length string of octets that describes the
- resource. The format of this information varies
- according to the TYPE and CLASS of the resource record.
- For example, if the TYPE is A and the CLASS is IN,
- the RDATA field is a 4 octet ARPA Internet address.
+ field specifies the meaning of the data in the RDATA field.
+CLASS two octets which specify the class of the data in the RDATA field.
+TTL a 32 bit unsigned integer that specifies the time interval
+ (in seconds) that the record may be cached.
+RDLENGTH a 16 bit integer, length in octets of the RDATA field.
+RDATA a variable length string of octets that describes the resource.
+ The format of this information varies according to the TYPE
+ and CLASS of the resource record.
+ If the TYPE is A and the CLASS is IN, it's a 4 octet IP address.
4.1.4. Message compression
-In order to reduce the size of messages, the domain system utilizes a
-compression scheme which eliminates the repetition of domain names in a
-message. In this scheme, an entire domain name or a list of labels at
-the end of a domain name is replaced with a pointer to a prior occurance
-of the same name.
+In order to reduce the size of messages, domain names coan be compressed.
+An entire domain name or a list of labels at the end of a domain name
+is replaced with a pointer to a prior occurance of the same name.
The pointer takes the form of a two octet sequence:
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
an offset from the start of the message (i.e., the first octet
of the ID field in the domain header).
A zero offset specifies the first byte of the ID field, etc.
-
-The compression scheme allows a domain name in a message to be
-represented as either:
+Domain name in a message can be represented as either:
- a sequence of labels ending in a zero octet
- a pointer
- a sequence of labels ending with a pointer
*/
-static int process_packet(struct dns_entry *conf_data, uint32_t conf_ttl, uint8_t *buf)
+static int process_packet(struct dns_entry *conf_data,
+ uint32_t conf_ttl,
+ uint8_t *buf)
{
- uint8_t answstr[MAX_NAME_LEN + 1];
struct dns_head *head;
- struct dns_prop *unaligned_qprop;
- uint8_t *from, *answb;
+ 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;
uint16_t type;
uint16_t class;
- int querystr_len;
-
- answstr[0] = '\0';
+ int query_len;
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 */
- from = (void *)(head + 1);
+ query_string = (void *)(head + 1);
/* caller guarantees strlen is <= MAX_PACK_LEN */
- querystr_len = strlen((char *)from) + 1;
+ query_len = strlen(query_string) + 1;
/* may be unaligned! */
- unaligned_qprop = (void *)(from + querystr_len);
+ unaligned_type_class = (void *)(query_string + query_len);
+ query_len += sizeof(*unaligned_type_class);
/* where to append answer block */
- answb = (void *)(unaligned_qprop + 1);
-
- outr_rlen = 0;
- /* QR = 1 "response", RCODE = 4 "Not Implemented" */
- outr_flags = htons(0x8000 | 4);
+ answb = (void *)(unaligned_type_class + 1);
- move_from_unaligned16(type, &unaligned_qprop->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_qprop->class);
+ 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;
}
- bb_info_msg("%s", (char *)from);
- if (table_lookup(answstr, conf_data, type, from) != 0) {
+ /* look up the name */
+ answstr = table_lookup(conf_data, type, query_string);
+#if DEBUG
+ /* Shows lengths instead of dots, unusable for !DEBUG */
+ bb_error_msg("'%s'->'%s'", query_string, answstr);
+#endif
+ outr_rlen = 4;
+ if (answstr && type == htons(REQ_PTR)) {
+ /* returning a host name */
+ outr_rlen = strlen(answstr) + 1;
+ }
+ if (!answstr
+ || (unsigned)(answb - buf) + query_len + 4 + 2 + outr_rlen > MAX_PACK_LEN
+ ) {
/* 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;
}
- /* return an address */
- outr_rlen = 4;
- if (type == htons(REQ_PTR)) {
- /* return a host name */
- outr_rlen = strlen((char *)answstr) + 1;
- }
- /* QR = 1 "response",
- * AA = 1 "Authoritative Answer",
- * RCODE = 0 "success" */
- outr_flags = htons(0x8000 | 0x0400 | 0);
- /* we have one answer */
- head->nansw = htons(1);
- /* copy query block to answer block */
- querystr_len += sizeof(unaligned_qprop);
- memcpy(answb, from, querystr_len);
- answb += querystr_len;
- /* append answer Resource Record */
+
+ /* Append answer Resource Record */
+ memcpy(answb, query_string, query_len); /* name, type, class */
+ answb += query_len;
move_to_unaligned32((uint32_t *)answb, htonl(conf_ttl));
answb += 4;
- move_to_unaligned32((uint16_t *)answb, htons(outr_rlen));
+ move_to_unaligned16((uint16_t *)answb, htons(outr_rlen));
answb += 2;
memcpy(answb, answstr, outr_rlen);
answb += outr_rlen;
+ /* QR = 1 "response",
+ * AA = 1 "Authoritative Answer",
+ * TODO: need to set RA bit 0x80? One user says nslookup complains
+ * "Got recursion not available from SERVER, trying next server"
+ * "** 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???
unsigned lsa_size;
int udps, opts;
uint16_t port = 53;
- /* Paranoid sizing: querystring x2 + ttl + outr_rlen + answstr */
- /* I'd rather see process_packet() fixed instead... */
- uint8_t buf[MAX_PACK_LEN * 2 + 4 + 2 + (MAX_NAME_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);
- bb_signals(0
- /* why? + (1 << SIGPIPE) */
- + (1 << SIGHUP)
-#ifdef SIGTSTP
- + (1 << SIGTSTP)
-#endif
-#ifdef SIGURG
- + (1 << SIGURG)
-#endif
- , SIG_IGN);
-
lsa = xdotted2sockaddr(listen_interface, port);
udps = xsocket(lsa->u.sa.sa_family, SOCK_DGRAM, 0);
xbind(udps, &lsa->u.sa, lsa->len);
{
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);
}
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)