2006-04-12 23:25:51 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2006-01-25 19:49:11 +05:30
|
|
|
/*
|
|
|
|
* Mini DNS server implementation for busybox
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 Roberto A. Foglietta (me@roberto.foglietta.name)
|
|
|
|
* Copyright (C) 2005 Odd Arild Olsen (oao at fibula dot no)
|
|
|
|
* Copyright (C) 2003 Paul Sheer
|
2006-04-12 23:39:26 +05:30
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2006-01-25 19:49:11 +05:30
|
|
|
*
|
|
|
|
* Odd Arild Olsen started out with the sheerdns [1] of Paul Sheer and rewrote
|
2006-01-25 20:10:24 +05:30
|
|
|
* it into a shape which I believe is both easier to understand and maintain.
|
|
|
|
* I also reused the input buffer for output and removed services he did not
|
2006-01-25 19:49:11 +05:30
|
|
|
* need. [1] http://threading.2038bug.com/sheerdns/
|
2006-01-25 20:10:24 +05:30
|
|
|
*
|
2006-01-25 19:49:11 +05:30
|
|
|
* Some bugfix and minor changes was applied by Roberto A. Foglietta who made
|
|
|
|
* the first porting of oao' scdns to busybox also.
|
|
|
|
*/
|
2016-11-23 13:35:14 +05:30
|
|
|
//config:config DNSD
|
2017-07-19 01:31:24 +05:30
|
|
|
//config: bool "dnsd (9.8 kb)"
|
2016-11-23 13:35:14 +05:30
|
|
|
//config: default y
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: Small and static DNS server daemon.
|
2016-11-23 13:35:14 +05:30
|
|
|
|
|
|
|
//applet:IF_DNSD(APPLET(dnsd, BB_DIR_USR_SBIN, BB_SUID_DROP))
|
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_DNSD) += dnsd.o
|
2006-01-25 19:49:11 +05:30
|
|
|
|
2011-04-11 06:59:49 +05:30
|
|
|
//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"
|
2011-07-08 09:17:49 +05:30
|
|
|
//usage: "\n nameserver NORMAL_DNS_SERVER"
|
2011-04-11 06:59:49 +05:30
|
|
|
|
2007-05-27 00:30:18 +05:30
|
|
|
#include "libbb.h"
|
2008-01-27 18:20:12 +05:30
|
|
|
#include <syslog.h>
|
2006-01-25 19:49:11 +05:30
|
|
|
|
2006-10-06 15:19:47 +05:30
|
|
|
//#define DEBUG 1
|
2007-01-12 20:27:37 +05:30
|
|
|
#define DEBUG 0
|
2006-10-06 15:19:47 +05:30
|
|
|
|
2006-03-11 00:52:06 +05:30
|
|
|
enum {
|
2009-04-13 00:33:01 +05:30
|
|
|
/* can tweak this */
|
2009-04-12 09:39:09 +05:30
|
|
|
DEFAULT_TTL = 120,
|
|
|
|
|
2009-04-13 00:33:01 +05:30
|
|
|
/* cannot get bigger packets than 512 per RFC1035. */
|
2008-03-17 14:32:21 +05:30
|
|
|
MAX_PACK_LEN = 512,
|
2009-04-12 09:39:09 +05:30
|
|
|
IP_STRING_LEN = sizeof(".xxx.xxx.xxx.xxx"),
|
|
|
|
MAX_NAME_LEN = IP_STRING_LEN - 1 + sizeof(".in-addr.arpa"),
|
2006-03-11 00:52:06 +05:30
|
|
|
REQ_A = 1,
|
2009-04-12 09:39:09 +05:30
|
|
|
REQ_PTR = 12,
|
2006-03-11 00:52:06 +05:30
|
|
|
};
|
2006-01-25 19:49:11 +05:30
|
|
|
|
2009-04-12 09:39:09 +05:30
|
|
|
/* the message from client and first part of response msg */
|
|
|
|
struct dns_head {
|
2006-01-25 20:10:24 +05:30
|
|
|
uint16_t id;
|
|
|
|
uint16_t flags;
|
2009-04-12 09:39:09 +05:30
|
|
|
uint16_t nquer;
|
|
|
|
uint16_t nansw;
|
|
|
|
uint16_t nauth;
|
|
|
|
uint16_t nadd;
|
2006-01-25 19:49:11 +05:30
|
|
|
};
|
2010-04-26 17:55:33 +05:30
|
|
|
/* 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: */
|
2010-02-07 03:18:10 +05:30
|
|
|
struct type_and_class {
|
2010-04-26 17:55:33 +05:30
|
|
|
uint16_t type PACKED;
|
|
|
|
uint16_t class PACKED;
|
|
|
|
} PACKED;
|
2009-04-12 09:39:09 +05:30
|
|
|
/* element of known name, ip address and reversed ip address */
|
|
|
|
struct dns_entry {
|
2006-01-25 20:10:24 +05:30
|
|
|
struct dns_entry *next;
|
2009-04-12 09:39:09 +05:30
|
|
|
uint32_t ip;
|
|
|
|
char rip[IP_STRING_LEN]; /* length decimal reversed IP */
|
|
|
|
char name[1];
|
2006-01-25 19:49:11 +05:30
|
|
|
};
|
|
|
|
|
2010-02-07 07:15:03 +05:30
|
|
|
#define OPT_verbose (option_mask32 & 1)
|
|
|
|
#define OPT_silent (option_mask32 & 2)
|
2007-03-24 17:43:04 +05:30
|
|
|
|
|
|
|
|
2006-01-25 19:49:11 +05:30
|
|
|
/*
|
2006-05-31 15:49:51 +05:30
|
|
|
* Insert length of substrings instead of dots
|
2006-01-25 19:49:11 +05:30
|
|
|
*/
|
2009-04-12 09:39:09 +05:30
|
|
|
static void undot(char *rip)
|
2006-01-25 19:49:11 +05:30
|
|
|
{
|
2009-04-12 09:39:09 +05:30
|
|
|
int i = 0;
|
|
|
|
int s = 0;
|
|
|
|
|
2006-10-06 15:19:47 +05:30
|
|
|
while (rip[i])
|
|
|
|
i++;
|
|
|
|
for (--i; i >= 0; i--) {
|
|
|
|
if (rip[i] == '.') {
|
2006-01-25 19:49:11 +05:30
|
|
|
rip[i] = s;
|
|
|
|
s = 0;
|
2009-04-12 09:39:09 +05:30
|
|
|
} else {
|
|
|
|
s++;
|
|
|
|
}
|
2006-01-25 19:49:11 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read hostname/IP records from file
|
|
|
|
*/
|
2009-03-11 20:10:00 +05:30
|
|
|
static struct dns_entry *parse_conf_file(const char *fileconf)
|
2006-01-25 19:49:11 +05:30
|
|
|
{
|
2008-07-27 04:38:31 +05:30
|
|
|
char *token[2];
|
2008-07-20 23:18:59 +05:30
|
|
|
parser_t *parser;
|
2009-04-12 09:39:09 +05:30
|
|
|
struct dns_entry *m, *conf_data;
|
|
|
|
struct dns_entry **nextp;
|
|
|
|
|
|
|
|
conf_data = NULL;
|
|
|
|
nextp = &conf_data;
|
2006-01-25 19:49:11 +05:30
|
|
|
|
2008-07-20 23:18:59 +05:30
|
|
|
parser = config_open(fileconf);
|
2008-07-27 04:38:31 +05:30
|
|
|
while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) {
|
2009-04-12 09:39:09 +05:30
|
|
|
struct in_addr ip;
|
|
|
|
uint32_t v32;
|
2008-07-27 04:38:31 +05:30
|
|
|
|
2009-04-12 09:39:09 +05:30
|
|
|
if (inet_aton(token[1], &ip) == 0) {
|
|
|
|
bb_error_msg("error at line %u, skipping", parser->lineno);
|
|
|
|
continue;
|
|
|
|
}
|
2008-07-27 04:38:31 +05:30
|
|
|
|
|
|
|
if (OPT_verbose)
|
2009-04-12 09:39:09 +05:30
|
|
|
bb_error_msg("name:%s, ip:%s", token[0], token[1]);
|
2008-07-27 04:38:31 +05:30
|
|
|
|
2009-04-12 09:39:09 +05:30
|
|
|
/* sizeof(*m) includes 1 byte for m->name[0] */
|
|
|
|
m = xzalloc(sizeof(*m) + strlen(token[0]) + 1);
|
|
|
|
/*m->next = NULL;*/
|
|
|
|
*nextp = m;
|
|
|
|
nextp = &m->next;
|
|
|
|
|
|
|
|
m->name[0] = '.';
|
|
|
|
strcpy(m->name + 1, token[0]);
|
|
|
|
undot(m->name);
|
|
|
|
m->ip = ip.s_addr; /* in network order */
|
|
|
|
v32 = ntohl(m->ip);
|
|
|
|
/* inverted order */
|
|
|
|
sprintf(m->rip, ".%u.%u.%u.%u",
|
|
|
|
(uint8_t)(v32),
|
|
|
|
(uint8_t)(v32 >> 8),
|
|
|
|
(uint8_t)(v32 >> 16),
|
|
|
|
(v32 >> 24)
|
|
|
|
);
|
|
|
|
undot(m->rip);
|
2006-01-25 20:10:24 +05:30
|
|
|
}
|
2008-07-27 04:38:31 +05:30
|
|
|
config_close(parser);
|
2009-03-11 20:10:00 +05:30
|
|
|
return conf_data;
|
2006-01-25 19:49:11 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2009-04-12 09:39:09 +05:30
|
|
|
* Look query up in dns records and return answer if found.
|
2006-01-25 20:10:24 +05:30
|
|
|
*/
|
2009-04-12 17:46:21 +05:30
|
|
|
static char *table_lookup(struct dns_entry *d,
|
|
|
|
uint16_t type,
|
|
|
|
char* query_string)
|
2006-01-25 19:49:11 +05:30
|
|
|
{
|
2009-04-12 09:39:09 +05:30
|
|
|
while (d) {
|
|
|
|
unsigned len = d->name[0];
|
|
|
|
/* d->name[len] is the last (non NUL) char */
|
2007-01-20 07:17:44 +05:30
|
|
|
#if DEBUG
|
2009-03-11 20:10:00 +05:30
|
|
|
char *p, *q;
|
2009-04-12 17:46:21 +05:30
|
|
|
q = query_string + 1;
|
|
|
|
p = d->name + 1;
|
2009-04-12 09:39:09 +05:30
|
|
|
fprintf(stderr, "%d/%d p:%s q:%s %d\n",
|
|
|
|
(int)strlen(p), len,
|
|
|
|
p, q, (int)strlen(q)
|
|
|
|
);
|
2006-01-25 19:49:11 +05:30
|
|
|
#endif
|
2009-04-12 09:39:09 +05:30
|
|
|
if (type == htons(REQ_A)) {
|
2009-03-11 20:10:00 +05:30
|
|
|
/* search by host name */
|
2009-04-12 09:39:09 +05:30
|
|
|
if (len != 1 || d->name[1] != '*') {
|
2009-04-12 17:46:21 +05:30
|
|
|
/* 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)
|
2009-04-12 09:39:09 +05:30
|
|
|
goto next;
|
|
|
|
}
|
2009-04-12 17:46:21 +05:30
|
|
|
return (char *)&d->ip;
|
2007-01-20 07:17:44 +05:30
|
|
|
#if DEBUG
|
2009-04-12 17:46:21 +05:30
|
|
|
fprintf(stderr, "Found IP:%x\n", (int)d->ip);
|
2006-01-25 19:49:11 +05:30
|
|
|
#endif
|
2009-04-12 09:39:09 +05:30
|
|
|
return 0;
|
2006-01-25 20:10:24 +05:30
|
|
|
}
|
2009-04-12 09:39:09 +05:30
|
|
|
/* search by IP-address */
|
|
|
|
if ((len != 1 || d->name[1] != '*')
|
2009-04-12 17:46:21 +05:30
|
|
|
/* we assume (do not check) that query_string
|
|
|
|
* ends in ".in-addr.arpa" */
|
2015-03-12 22:18:34 +05:30
|
|
|
&& is_prefixed_with(query_string, d->rip)
|
2009-04-12 09:39:09 +05:30
|
|
|
) {
|
|
|
|
#if DEBUG
|
2009-04-12 17:46:21 +05:30
|
|
|
fprintf(stderr, "Found name:%s\n", d->name);
|
2009-04-12 09:39:09 +05:30
|
|
|
#endif
|
2009-04-12 17:46:21 +05:30
|
|
|
return d->name;
|
2009-04-12 09:39:09 +05:30
|
|
|
}
|
|
|
|
next:
|
2006-10-06 15:19:47 +05:30
|
|
|
d = d->next;
|
2009-04-12 09:39:09 +05:30
|
|
|
}
|
|
|
|
|
2009-04-12 17:46:21 +05:30
|
|
|
return NULL;
|
2006-01-25 19:49:11 +05:30
|
|
|
}
|
|
|
|
|
2006-01-25 20:10:24 +05:30
|
|
|
/*
|
2006-01-25 19:49:11 +05:30
|
|
|
* Decode message and generate answer
|
|
|
|
*/
|
2009-04-12 09:39:09 +05:30
|
|
|
/* RFC 1035
|
|
|
|
...
|
|
|
|
Whenever an octet represents a numeric quantity, the left most bit
|
|
|
|
in the diagram is the high order or most significant bit.
|
|
|
|
That is, the bit labeled 0 is the most significant bit.
|
|
|
|
...
|
|
|
|
|
|
|
|
4.1.1. Header section format
|
2009-04-12 17:46:21 +05:30
|
|
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
2009-04-12 09:39:09 +05:30
|
|
|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|
|
|
|
| ID |
|
|
|
|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|
2009-04-12 17:46:21 +05:30
|
|
|
|QR| OPCODE |AA|TC|RD|RA| 0 0 0| RCODE |
|
2009-04-12 09:39:09 +05:30
|
|
|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|
|
|
|
| QDCOUNT |
|
|
|
|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|
|
|
|
| ANCOUNT |
|
|
|
|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|
|
|
|
| NSCOUNT |
|
|
|
|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|
|
|
|
| ARCOUNT |
|
|
|
|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|
2009-04-12 17:46:21 +05:30
|
|
|
ID 16 bit random identifier assigned by querying peer.
|
2009-04-12 09:39:09 +05:30
|
|
|
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)
|
2009-04-12 17:46:21 +05:30
|
|
|
AA Authoritative Answer - this bit is valid in responses.
|
|
|
|
Responding name server is an authority for the domain name
|
2009-04-13 00:33:01 +05:30
|
|
|
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.
|
2009-04-12 17:46:21 +05:30
|
|
|
TC TrunCation - this message was truncated.
|
2009-04-12 09:39:09 +05:30
|
|
|
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.
|
|
|
|
Recursive query support is optional.
|
|
|
|
RA Recursion Available - this be is set or cleared in a
|
|
|
|
response, and denotes whether recursive query support is
|
|
|
|
available in the name server.
|
|
|
|
RCODE Response code.
|
|
|
|
0 No error condition
|
|
|
|
1 Format error
|
2009-04-12 17:46:21 +05:30
|
|
|
2 Server failure - server was unable to process the query
|
2009-04-13 00:33:01 +05:30
|
|
|
due to a problem with the name server.
|
2009-04-12 17:46:21 +05:30
|
|
|
3 Name Error - meaningful only for responses from
|
2009-04-13 00:33:01 +05:30
|
|
|
an authoritative name server. The referenced domain name
|
|
|
|
does not exist.
|
2009-04-12 09:39:09 +05:30
|
|
|
4 Not Implemented.
|
|
|
|
5 Refused.
|
|
|
|
QDCOUNT number of entries in the question section.
|
2009-04-12 17:46:21 +05:30
|
|
|
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.
|
2009-04-12 09:39:09 +05:30
|
|
|
|
|
|
|
4.1.2. Question section format
|
|
|
|
|
2009-04-12 17:46:21 +05:30
|
|
|
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
|
2009-04-12 09:39:09 +05:30
|
|
|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|
|
|
|
/ QNAME /
|
|
|
|
/ /
|
|
|
|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|
|
|
|
| QTYPE |
|
|
|
|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|
|
|
|
| QCLASS |
|
|
|
|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|
|
|
|
QNAME a domain name represented as a sequence of labels, where
|
|
|
|
each label consists of a length octet followed by that
|
|
|
|
number of octets. The domain name terminates with the
|
|
|
|
zero length octet for the null label of the root. Note
|
|
|
|
that this field may be an odd number of octets; no
|
|
|
|
padding is used.
|
|
|
|
QTYPE a two octet type of the query.
|
|
|
|
1 a host address [REQ_A const]
|
|
|
|
2 an authoritative name server
|
|
|
|
3 a mail destination (Obsolete - use MX)
|
|
|
|
4 a mail forwarder (Obsolete - use MX)
|
|
|
|
5 the canonical name for an alias
|
|
|
|
6 marks the start of a zone of authority
|
|
|
|
7 a mailbox domain name (EXPERIMENTAL)
|
|
|
|
8 a mail group member (EXPERIMENTAL)
|
|
|
|
9 a mail rename domain name (EXPERIMENTAL)
|
|
|
|
10 a null RR (EXPERIMENTAL)
|
|
|
|
11 a well known service description
|
|
|
|
12 a domain name pointer [REQ_PTR const]
|
|
|
|
13 host information
|
|
|
|
14 mailbox or mail list information
|
|
|
|
15 mail exchange
|
|
|
|
16 text strings
|
|
|
|
0x1c IPv6?
|
|
|
|
252 a request for a transfer of an entire zone
|
|
|
|
253 a request for mailbox-related records (MB, MG or MR)
|
|
|
|
254 a request for mail agent RRs (Obsolete - see MX)
|
|
|
|
255 a request for all records
|
|
|
|
QCLASS a two octet code that specifies the class of the query.
|
|
|
|
1 the Internet
|
2009-04-13 00:33:01 +05:30
|
|
|
(others are historic only)
|
2009-04-12 09:39:09 +05:30
|
|
|
255 any class
|
|
|
|
|
2010-02-07 03:18:10 +05:30
|
|
|
4.1.3. Resource Record format
|
2009-04-12 09:39:09 +05:30
|
|
|
|
2009-04-12 17:46:21 +05:30
|
|
|
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
|
2009-04-12 09:39:09 +05:30
|
|
|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|
|
|
|
/ /
|
|
|
|
/ NAME /
|
|
|
|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|
|
|
|
| TYPE |
|
|
|
|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|
|
|
|
| CLASS |
|
|
|
|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|
|
|
|
| TTL |
|
|
|
|
| |
|
|
|
|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|
|
|
|
| RDLENGTH |
|
|
|
|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--|
|
|
|
|
/ RDATA /
|
|
|
|
/ /
|
|
|
|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|
|
|
|
NAME a domain name to which this resource record pertains.
|
|
|
|
TYPE two octets containing one of the RR type codes. This
|
2009-04-12 17:46:21 +05:30
|
|
|
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.
|
2009-04-12 09:39:09 +05:30
|
|
|
|
|
|
|
4.1.4. Message compression
|
|
|
|
|
2009-04-12 17:46:21 +05:30
|
|
|
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
|
2017-04-17 19:43:32 +05:30
|
|
|
is replaced with a pointer to a prior occurrence of the same name.
|
2009-04-12 09:39:09 +05:30
|
|
|
|
|
|
|
The pointer takes the form of a two octet sequence:
|
|
|
|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|
|
|
|
| 1 1| OFFSET |
|
|
|
|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|
|
|
|
The first two bits are ones. This allows a pointer to be distinguished
|
|
|
|
from a label, since the label must begin with two zero bits because
|
|
|
|
labels are restricted to 63 octets or less. The OFFSET field specifies
|
|
|
|
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.
|
2009-04-12 17:46:21 +05:30
|
|
|
Domain name in a message can be represented as either:
|
2009-04-12 09:39:09 +05:30
|
|
|
- a sequence of labels ending in a zero octet
|
|
|
|
- a pointer
|
|
|
|
- a sequence of labels ending with a pointer
|
|
|
|
*/
|
2009-04-12 17:46:21 +05:30
|
|
|
static int process_packet(struct dns_entry *conf_data,
|
|
|
|
uint32_t conf_ttl,
|
|
|
|
uint8_t *buf)
|
2006-01-25 19:49:11 +05:30
|
|
|
{
|
2006-01-25 20:10:24 +05:30
|
|
|
struct dns_head *head;
|
2010-02-07 03:18:10 +05:30
|
|
|
struct type_and_class *unaligned_type_class;
|
2010-02-07 07:15:03 +05:30
|
|
|
const char *err_msg;
|
2009-04-12 17:46:21 +05:30
|
|
|
char *query_string;
|
2010-02-07 07:15:03 +05:30
|
|
|
char *answstr;
|
2009-04-12 17:46:21 +05:30
|
|
|
uint8_t *answb;
|
2008-03-17 14:32:21 +05:30
|
|
|
uint16_t outr_rlen;
|
|
|
|
uint16_t outr_flags;
|
2009-04-12 09:39:09 +05:30
|
|
|
uint16_t type;
|
|
|
|
uint16_t class;
|
2010-02-07 03:18:10 +05:30
|
|
|
int query_len;
|
2006-01-25 20:10:24 +05:30
|
|
|
|
|
|
|
head = (struct dns_head *)buf;
|
2007-03-24 17:43:04 +05:30
|
|
|
if (head->nquer == 0) {
|
2009-04-12 09:39:09 +05:30
|
|
|
bb_error_msg("packet has 0 queries, ignored");
|
2010-02-07 07:15:03 +05:30
|
|
|
return 0; /* don't reply */
|
2007-03-24 17:43:04 +05:30
|
|
|
}
|
2009-04-12 09:39:09 +05:30
|
|
|
if (head->flags & htons(0x8000)) { /* QR bit */
|
|
|
|
bb_error_msg("response packet, ignored");
|
2010-02-07 07:15:03 +05:30
|
|
|
return 0; /* don't reply */
|
|
|
|
}
|
|
|
|
/* QR = 1 "response", RCODE = 4 "Not Implemented" */
|
|
|
|
outr_flags = htons(0x8000 | 4);
|
|
|
|
err_msg = NULL;
|
2006-01-25 20:10:24 +05:30
|
|
|
|
2009-04-12 09:39:09 +05:30
|
|
|
/* start of query string */
|
2009-04-12 17:46:21 +05:30
|
|
|
query_string = (void *)(head + 1);
|
2009-04-12 09:39:09 +05:30
|
|
|
/* caller guarantees strlen is <= MAX_PACK_LEN */
|
2010-02-07 03:18:10 +05:30
|
|
|
query_len = strlen(query_string) + 1;
|
2009-04-12 09:39:09 +05:30
|
|
|
/* may be unaligned! */
|
2010-02-07 03:18:10 +05:30
|
|
|
unaligned_type_class = (void *)(query_string + query_len);
|
2010-10-29 04:07:56 +05:30
|
|
|
query_len += sizeof(*unaligned_type_class);
|
2009-04-12 09:39:09 +05:30
|
|
|
/* where to append answer block */
|
2010-02-07 03:18:10 +05:30
|
|
|
answb = (void *)(unaligned_type_class + 1);
|
2006-01-25 20:10:24 +05:30
|
|
|
|
2010-03-06 04:08:54 +05:30
|
|
|
/* OPCODE != 0 "standard query"? */
|
|
|
|
if ((head->flags & htons(0x7800)) != 0) {
|
|
|
|
err_msg = "opcode != 0";
|
|
|
|
goto empty_packet;
|
|
|
|
}
|
2010-02-07 03:18:10 +05:30
|
|
|
move_from_unaligned16(class, &unaligned_type_class->class);
|
2009-04-12 09:39:09 +05:30
|
|
|
if (class != htons(1)) { /* not class INET? */
|
2010-02-07 07:15:03 +05:30
|
|
|
err_msg = "class != 1";
|
2006-01-25 20:10:24 +05:30
|
|
|
goto empty_packet;
|
|
|
|
}
|
2010-02-07 07:15:03 +05:30
|
|
|
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";
|
2006-01-25 20:10:24 +05:30
|
|
|
goto empty_packet;
|
2009-04-12 09:39:09 +05:30
|
|
|
}
|
2006-01-25 20:10:24 +05:30
|
|
|
|
2009-04-12 17:46:21 +05:30
|
|
|
/* look up the name */
|
2010-02-07 05:41:18 +05:30
|
|
|
answstr = table_lookup(conf_data, type, query_string);
|
2009-04-12 17:46:21 +05:30
|
|
|
#if DEBUG
|
2010-02-07 05:41:18 +05:30
|
|
|
/* Shows lengths instead of dots, unusable for !DEBUG */
|
|
|
|
bb_error_msg("'%s'->'%s'", query_string, answstr);
|
2009-04-12 17:46:21 +05:30
|
|
|
#endif
|
|
|
|
outr_rlen = 4;
|
|
|
|
if (answstr && type == htons(REQ_PTR)) {
|
2010-02-07 03:18:10 +05:30
|
|
|
/* returning a host name */
|
2009-04-12 17:46:21 +05:30
|
|
|
outr_rlen = strlen(answstr) + 1;
|
|
|
|
}
|
|
|
|
if (!answstr
|
2010-02-07 03:18:10 +05:30
|
|
|
|| (unsigned)(answb - buf) + query_len + 4 + 2 + outr_rlen > MAX_PACK_LEN
|
2009-04-12 17:46:21 +05:30
|
|
|
) {
|
2009-04-12 09:39:09 +05:30
|
|
|
/* QR = 1 "response"
|
|
|
|
* AA = 1 "Authoritative Answer"
|
|
|
|
* RCODE = 3 "Name Error" */
|
2010-02-07 07:15:03 +05:30
|
|
|
err_msg = "name is not found";
|
2009-04-12 09:39:09 +05:30
|
|
|
outr_flags = htons(0x8000 | 0x0400 | 3);
|
2006-01-25 20:10:24 +05:30
|
|
|
goto empty_packet;
|
|
|
|
}
|
2009-04-12 17:46:21 +05:30
|
|
|
|
2010-02-07 03:18:10 +05:30
|
|
|
/* Append answer Resource Record */
|
|
|
|
memcpy(answb, query_string, query_len); /* name, type, class */
|
|
|
|
answb += query_len;
|
2009-04-12 09:39:09 +05:30
|
|
|
move_to_unaligned32((uint32_t *)answb, htonl(conf_ttl));
|
2008-03-17 14:32:21 +05:30
|
|
|
answb += 4;
|
2010-02-07 03:18:10 +05:30
|
|
|
move_to_unaligned16((uint16_t *)answb, htons(outr_rlen));
|
2008-03-17 14:32:21 +05:30
|
|
|
answb += 2;
|
|
|
|
memcpy(answb, answstr, outr_rlen);
|
|
|
|
answb += outr_rlen;
|
2006-01-25 19:49:11 +05:30
|
|
|
|
2009-04-12 17:46:21 +05:30
|
|
|
/* QR = 1 "response",
|
|
|
|
* AA = 1 "Authoritative Answer",
|
2010-02-07 03:18:10 +05:30
|
|
|
* 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"
|
|
|
|
*/
|
2010-02-07 05:41:18 +05:30
|
|
|
if (OPT_verbose)
|
|
|
|
bb_error_msg("returning positive reply");
|
2009-04-12 17:46:21 +05:30
|
|
|
outr_flags = htons(0x8000 | 0x0400 | 0);
|
|
|
|
/* we have one answer */
|
|
|
|
head->nansw = htons(1);
|
|
|
|
|
2006-10-06 15:19:47 +05:30
|
|
|
empty_packet:
|
2010-02-07 07:15:03 +05:30
|
|
|
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;
|
|
|
|
}
|
2009-04-12 09:39:09 +05:30
|
|
|
head->flags |= outr_flags;
|
2008-03-17 14:32:21 +05:30
|
|
|
head->nauth = head->nadd = 0;
|
2009-04-12 09:39:09 +05:30
|
|
|
head->nquer = htons(1); // why???
|
2006-01-25 19:49:11 +05:30
|
|
|
|
2009-04-12 09:39:09 +05:30
|
|
|
return answb - buf;
|
2006-01-25 19:49:11 +05:30
|
|
|
}
|
|
|
|
|
2007-10-11 15:35:36 +05:30
|
|
|
int dnsd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 14:48:54 +05:30
|
|
|
int dnsd_main(int argc UNUSED_PARAM, char **argv)
|
2006-01-25 19:49:11 +05:30
|
|
|
{
|
2007-03-24 17:43:04 +05:30
|
|
|
const char *listen_interface = "0.0.0.0";
|
2009-03-11 20:10:00 +05:30
|
|
|
const char *fileconf = "/etc/dnsd.conf";
|
|
|
|
struct dns_entry *conf_data;
|
|
|
|
uint32_t conf_ttl = DEFAULT_TTL;
|
2007-01-12 20:27:37 +05:30
|
|
|
char *sttl, *sport;
|
2008-03-17 14:32:21 +05:30
|
|
|
len_and_sockaddr *lsa, *from, *to;
|
|
|
|
unsigned lsa_size;
|
2009-03-11 20:10:00 +05:30
|
|
|
int udps, opts;
|
2006-01-25 20:10:24 +05:30
|
|
|
uint16_t port = 53;
|
2010-04-14 22:44:25 +05:30
|
|
|
/* Ensure buf is 32bit aligned (we need 16bit, but 32bit can't hurt) */
|
|
|
|
uint8_t buf[MAX_PACK_LEN + 1] ALIGN4;
|
2006-01-25 19:49:11 +05:30
|
|
|
|
2010-02-07 07:15:03 +05:30
|
|
|
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
|
2009-03-11 20:10:00 +05:30
|
|
|
conf_ttl = xatou_range(sttl, 1, 0xffffffff);
|
2010-02-07 07:15:03 +05:30
|
|
|
if (opts & (1 << 5)) // -p
|
2007-03-24 17:43:04 +05:30
|
|
|
port = xatou_range(sport, 1, 0xffff);
|
2010-02-07 07:15:03 +05:30
|
|
|
if (opts & (1 << 6)) { // -d
|
2007-03-26 18:50:54 +05:30
|
|
|
bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv);
|
2007-03-24 19:36:51 +05:30
|
|
|
openlog(applet_name, LOG_PID, LOG_DAEMON);
|
2007-01-12 20:27:37 +05:30
|
|
|
logmode = LOGMODE_SYSLOG;
|
|
|
|
}
|
2006-01-25 19:49:11 +05:30
|
|
|
|
2009-03-11 20:10:00 +05:30
|
|
|
conf_data = parse_conf_file(fileconf);
|
2006-01-25 20:10:24 +05:30
|
|
|
|
2007-03-24 17:43:04 +05:30
|
|
|
lsa = xdotted2sockaddr(listen_interface, port);
|
2008-01-29 16:03:34 +05:30
|
|
|
udps = xsocket(lsa->u.sa.sa_family, SOCK_DGRAM, 0);
|
|
|
|
xbind(udps, &lsa->u.sa, lsa->len);
|
2008-03-17 14:32:21 +05:30
|
|
|
socket_want_pktinfo(udps); /* needed for recv_from_to to work */
|
|
|
|
lsa_size = LSA_LEN_SIZE + lsa->len;
|
|
|
|
from = xzalloc(lsa_size);
|
|
|
|
to = xzalloc(lsa_size);
|
|
|
|
|
2009-04-12 09:39:09 +05:30
|
|
|
{
|
|
|
|
char *p = xmalloc_sockaddr2dotted(&lsa->u.sa);
|
2010-02-07 05:41:18 +05:30
|
|
|
bb_error_msg("accepting UDP packets on %s", p);
|
2009-04-12 09:39:09 +05:30
|
|
|
free(p);
|
|
|
|
}
|
2006-01-25 19:49:11 +05:30
|
|
|
|
2006-01-25 20:10:24 +05:30
|
|
|
while (1) {
|
|
|
|
int r;
|
2008-03-17 14:32:21 +05:30
|
|
|
/* Try to get *DEST* address (to which of our addresses
|
|
|
|
* this query was directed), and reply from the same address.
|
|
|
|
* Or else we can exhibit usual UDP ugliness:
|
|
|
|
* [ip1.multihomed.ip2] <= query to ip1 <= peer
|
|
|
|
* [ip1.multihomed.ip2] => reply from ip2 => peer (confused) */
|
|
|
|
memcpy(to, lsa, lsa_size);
|
|
|
|
r = recv_from_to(udps, buf, MAX_PACK_LEN + 1, 0, &from->u.sa, &to->u.sa, lsa->len);
|
|
|
|
if (r < 12 || r > MAX_PACK_LEN) {
|
2009-04-12 09:39:09 +05:30
|
|
|
bb_error_msg("packet size %d, ignored", r);
|
2007-03-24 17:43:04 +05:30
|
|
|
continue;
|
2007-01-12 20:27:37 +05:30
|
|
|
}
|
2008-03-17 14:32:21 +05:30
|
|
|
if (OPT_verbose)
|
2010-02-07 05:41:18 +05:30
|
|
|
bb_error_msg("got UDP packet");
|
2008-03-17 14:32:21 +05:30
|
|
|
buf[r] = '\0'; /* paranoia */
|
2009-03-11 20:10:00 +05:30
|
|
|
r = process_packet(conf_data, conf_ttl, buf);
|
2007-03-24 17:43:04 +05:30
|
|
|
if (r <= 0)
|
|
|
|
continue;
|
2008-05-22 23:11:01 +05:30
|
|
|
send_to_from(udps, buf, r, 0, &from->u.sa, &to->u.sa, lsa->len);
|
2007-01-12 20:27:37 +05:30
|
|
|
}
|
2007-03-24 18:39:07 +05:30
|
|
|
return 0;
|
2006-01-25 19:49:11 +05:30
|
|
|
}
|