blob: c9c7b3a7c4f5ce664e8134608fb2758f07537a74 [file] [log] [blame]
Bernhard Reutner-Fischerdac7ff12006-04-12 17:55:51 +00001/* vi: set sw=4 ts=4: */
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +00002/*
3 * Mini DNS server implementation for busybox
4 *
5 * Copyright (C) 2005 Roberto A. Foglietta (me@roberto.foglietta.name)
6 * Copyright (C) 2005 Odd Arild Olsen (oao at fibula dot no)
7 * Copyright (C) 2003 Paul Sheer
Bernhard Reutner-Fischer2c998512006-04-12 18:09:26 +00008 *
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +00009 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
10 *
11 * Odd Arild Olsen started out with the sheerdns [1] of Paul Sheer and rewrote
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000012 * it into a shape which I believe is both easier to understand and maintain.
13 * I also reused the input buffer for output and removed services he did not
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000014 * need. [1] http://threading.2038bug.com/sheerdns/
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000015 *
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000016 * Some bugfix and minor changes was applied by Roberto A. Foglietta who made
17 * the first porting of oao' scdns to busybox also.
18 */
19
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +000020#include <syslog.h>
Bernhard Reutner-Fischer421d9e52006-04-03 16:39:31 +000021#include "busybox.h"
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000022
Denis Vlasenkoc12f5302006-10-06 09:49:47 +000023//#define DEBUG 1
Denis Vlasenko2c916522007-01-12 14:57:37 +000024#define DEBUG 0
Denis Vlasenkoc12f5302006-10-06 09:49:47 +000025
Rob Landleybc68cd12006-03-10 19:22:06 +000026enum {
27 MAX_HOST_LEN = 16, // longest host name allowed is 15
28 IP_STRING_LEN = 18, // .xxx.xxx.xxx.xxx\0
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000029
30//must be strlen('.in-addr.arpa') larger than IP_STRING_LEN
Rob Landleybc68cd12006-03-10 19:22:06 +000031 MAX_NAME_LEN = (IP_STRING_LEN + 13),
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000032
33/* Cannot get bigger packets than 512 per RFC1035
34 In practice this can be set considerably smaller:
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000035 Length of response packet is header (12B) + 2*type(4B) + 2*class(4B) +
36 ttl(4B) + rlen(2B) + r (MAX_NAME_LEN =21B) +
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000037 2*querystring (2 MAX_NAME_LEN= 42B), all together 90 Byte
38*/
Rob Landleybc68cd12006-03-10 19:22:06 +000039 MAX_PACK_LEN = 512 + 1,
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000040
Rob Landleybc68cd12006-03-10 19:22:06 +000041 DEFAULT_TTL = 30, // increase this when not testing?
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000042
Rob Landleybc68cd12006-03-10 19:22:06 +000043 REQ_A = 1,
44 REQ_PTR = 12
45};
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000046
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000047struct dns_repl { // resource record, add 0 or 1 to accepted dns_msg in resp
48 uint16_t rlen;
49 uint8_t *r; // resource
50 uint16_t flags;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000051};
52
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000053struct dns_head { // the message from client and first part of response mag
54 uint16_t id;
55 uint16_t flags;
56 uint16_t nquer; // accepts 0
57 uint16_t nansw; // 1 in response
58 uint16_t nauth; // 0
59 uint16_t nadd; // 0
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000060};
61struct dns_prop {
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000062 uint16_t type;
63 uint16_t class;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000064};
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000065struct dns_entry { // element of known name, ip address and reversed ip address
66 struct dns_entry *next;
67 char ip[IP_STRING_LEN]; // dotted decimal IP
68 char rip[IP_STRING_LEN]; // length decimal reversed IP
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000069 char name[MAX_HOST_LEN];
70};
71
Denis Vlasenkod3bac032007-03-24 12:13:04 +000072static struct dns_entry *dnsentry;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000073static uint32_t ttl = DEFAULT_TTL;
74
Denis Vlasenkod3bac032007-03-24 12:13:04 +000075static const char *fileconf = "/etc/dnsd.conf";
76
77// Must match getopt32 call
78#define OPT_daemon (option_mask32 & 0x10)
79#define OPT_verbose (option_mask32 & 0x20)
80
81
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000082/*
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000083 * Convert host name from C-string to dns length/string.
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000084 */
Rob Landleya6e131d2006-05-29 06:43:55 +000085static void convname(char *a, uint8_t *q)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000086{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000087 int i = (q[0] == '.') ? 0 : 1;
Denis Vlasenkoc12f5302006-10-06 09:49:47 +000088 for (; i < MAX_HOST_LEN-1 && *q; i++, q++)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000089 a[i] = tolower(*q);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000090 a[0] = i - 1;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000091 a[i] = 0;
92}
93
94/*
Bernhard Reutner-Fischerc418d482006-05-31 10:19:51 +000095 * Insert length of substrings instead of dots
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000096 */
Rob Landleya6e131d2006-05-29 06:43:55 +000097static void undot(uint8_t * rip)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000098{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000099 int i = 0, s = 0;
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000100 while (rip[i])
101 i++;
102 for (--i; i >= 0; i--) {
103 if (rip[i] == '.') {
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000104 rip[i] = s;
105 s = 0;
106 } else s++;
107 }
108}
109
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000110/*
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000111 * Read one line of hostname/IP from file
112 * Returns 0 for each valid entry read, -1 at EOF
113 * Assumes all host names are lower case only
Denis Vlasenko6cd2d2b2007-01-22 14:06:03 +0000114 * Hostnames with more than one label are not handled correctly.
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000115 * Presently the dot is copied into name without
116 * converting to a length/string substring for that label.
117 */
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000118static int getfileentry(FILE * fp, struct dns_entry *s)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000119{
120 unsigned int a,b,c,d;
Denis Vlasenko6cd2d2b2007-01-22 14:06:03 +0000121 char *line, *r, *name;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000122
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000123 restart:
Denis Vlasenko6cd2d2b2007-01-22 14:06:03 +0000124 line = r = xmalloc_fgets(fp);
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000125 if (!r)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000126 return -1;
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000127 while (*r == ' ' || *r == '\t') {
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000128 r++;
Denis Vlasenko6cd2d2b2007-01-22 14:06:03 +0000129 if (!*r || *r == '#' || *r == '\n') {
130 free(line);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000131 goto restart; /* skipping empty/blank and commented lines */
Denis Vlasenko6cd2d2b2007-01-22 14:06:03 +0000132 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000133 }
134 name = r;
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000135 while (*r != ' ' && *r != '\t')
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000136 r++;
Denis Vlasenko6cd2d2b2007-01-22 14:06:03 +0000137 *r++ = '\0';
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000138 if (sscanf(r, ".%u.%u.%u.%u"+1, &a, &b, &c, &d) != 4) {
Denis Vlasenko6cd2d2b2007-01-22 14:06:03 +0000139 free(line);
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000140 goto restart; /* skipping wrong lines */
Denis Vlasenko6cd2d2b2007-01-22 14:06:03 +0000141 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000142
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000143 sprintf(s->ip, ".%u.%u.%u.%u"+1, a, b, c, d);
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000144 sprintf(s->rip, ".%u.%u.%u.%u", d, c, b, a);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000145 undot((uint8_t*)s->rip);
Denis Vlasenko6cd2d2b2007-01-22 14:06:03 +0000146 convname(s->name, (uint8_t*)name);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000147
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000148 if (OPT_verbose)
149 fprintf(stderr, "\tname:%s, ip:%s\n", &(s->name[1]),s->ip);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000150
Denis Vlasenko6cd2d2b2007-01-22 14:06:03 +0000151 free(line);
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000152 return 0;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000153}
154
155/*
156 * Read hostname/IP records from file
157 */
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000158static void dnsentryinit(void)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000159{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000160 FILE *fp;
161 struct dns_entry *m, *prev;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000162
Denis Vlasenko6cd2d2b2007-01-22 14:06:03 +0000163 prev = dnsentry = NULL;
Rob Landley86b4d642006-08-03 17:58:17 +0000164 fp = xfopen(fileconf, "r");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000165
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000166 while (1) {
Denis Vlasenko6cd2d2b2007-01-22 14:06:03 +0000167 m = xzalloc(sizeof(*m));
168 /*m->next = NULL;*/
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000169 if (getfileentry(fp, m))
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000170 break;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000171
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000172 if (prev == NULL)
173 dnsentry = m;
174 else
175 prev->next = m;
176 prev = m;
177 }
178 fclose(fp);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000179}
180
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000181/*
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000182 * Look query up in dns records and return answer if found
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000183 * qs is the query string, first byte the string length
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000184 */
Rob Landleya6e131d2006-05-29 06:43:55 +0000185static int table_lookup(uint16_t type, uint8_t * as, uint8_t * qs)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000186{
187 int i;
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000188 struct dns_entry *d = dnsentry;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000189
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000190 do {
Denis Vlasenko91f20ab2007-01-20 01:47:44 +0000191#if DEBUG
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000192 char *p,*q;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000193 q = (char *)&(qs[1]);
194 p = &(d->name[1]);
Denis Vlasenkof7996f32007-01-11 17:20:00 +0000195 fprintf(stderr, "\n%s: %d/%d p:%s q:%s %d",
Denis Vlasenko91f20ab2007-01-20 01:47:44 +0000196 __FUNCTION__, (int)strlen(p), (int)(d->name[0]),
197 p, q, (int)strlen(q));
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000198#endif
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000199 if (type == REQ_A) { /* search by host name */
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000200 for (i = 1; i <= (int)(d->name[0]); i++)
201 if (tolower(qs[i]) != d->name[i])
202 break;
203 if (i > (int)(d->name[0])) {
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000204 strcpy((char *)as, d->ip);
Denis Vlasenko91f20ab2007-01-20 01:47:44 +0000205#if DEBUG
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000206 fprintf(stderr, " OK as:%s\n", as);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000207#endif
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000208 return 0;
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000209 }
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000210 } else if (type == REQ_PTR) { /* search by IP-address */
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000211 if (!strncmp((char*)&d->rip[1], (char*)&qs[1], strlen(d->rip)-1)) {
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000212 strcpy((char *)as, d->name);
213 return 0;
214 }
215 }
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000216 d = d->next;
217 } while (d);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000218 return -1;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000219}
220
221
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000222/*
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000223 * Decode message and generate answer
224 */
Rob Landleya6e131d2006-05-29 06:43:55 +0000225static int process_packet(uint8_t * buf)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000226{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000227 struct dns_head *head;
228 struct dns_prop *qprop;
229 struct dns_repl outr;
230 void *next, *from, *answb;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000231
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000232 uint8_t answstr[MAX_NAME_LEN + 1];
233 int lookup_result, type, len, packet_len;
234 uint16_t flags;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000235
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000236 answstr[0] = '\0';
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000237
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000238 head = (struct dns_head *)buf;
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000239 if (head->nquer == 0) {
240 bb_error_msg("no queries");
Denis Vlasenkob5b45a92007-03-24 13:09:07 +0000241 return -1;
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000242 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000243
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000244 if (head->flags & 0x8000) {
245 bb_error_msg("ignoring response packet");
Denis Vlasenkob5b45a92007-03-24 13:09:07 +0000246 return -1;
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000247 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000248
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000249 from = (void *)&head[1]; // start of query string
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000250 next = answb = from + strlen((char *)from) + 1 + sizeof(struct dns_prop); // where to append answer block
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000251
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000252 outr.rlen = 0; // may change later
253 outr.r = NULL;
254 outr.flags = 0;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000255
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000256 qprop = (struct dns_prop *)(answb - 4);
257 type = ntohs(qprop->type);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000258
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000259 // only let REQ_A and REQ_PTR pass
260 if (!(type == REQ_A || type == REQ_PTR)) {
261 goto empty_packet; /* we can't handle the query type */
262 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000263
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000264 if (ntohs(qprop->class) != 1 /* class INET */ ) {
265 outr.flags = 4; /* not supported */
266 goto empty_packet;
267 }
268 /* we only support standard queries */
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000269
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000270 if ((ntohs(head->flags) & 0x7800) != 0)
271 goto empty_packet;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000272
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000273 // We have a standard query
Denis Vlasenko2c916522007-01-12 14:57:37 +0000274 bb_info_msg("%s", (char *)from);
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000275 lookup_result = table_lookup(type, answstr, (uint8_t*)from);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000276 if (lookup_result != 0) {
277 outr.flags = 3 | 0x0400; //name do not exist and auth
278 goto empty_packet;
279 }
280 if (type == REQ_A) { // return an address
281 struct in_addr a;
282 if (!inet_aton((char*)answstr, &a)) {//dotted dec to long conv
283 outr.flags = 1; /* Frmt err */
284 goto empty_packet;
285 }
286 memcpy(answstr, &a.s_addr, 4); // save before a disappears
287 outr.rlen = 4; // uint32_t IP
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000288 } else
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000289 outr.rlen = strlen((char *)answstr) + 1; // a host name
290 outr.r = answstr; // 32 bit ip or a host name
291 outr.flags |= 0x0400; /* authority-bit */
292 // we have an answer
293 head->nansw = htons(1);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000294
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000295 // copy query block to answer block
296 len = answb - from;
297 memcpy(answb, from, len);
298 next += len;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000299
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000300 // and append answer rr
301 *(uint32_t *) next = htonl(ttl);
302 next += 4;
303 *(uint16_t *) next = htons(outr.rlen);
304 next += 2;
305 memcpy(next, (void *)answstr, outr.rlen);
306 next += outr.rlen;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000307
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000308 empty_packet:
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000309
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000310 flags = ntohs(head->flags);
311 // clear rcode and RA, set responsebit and our new flags
312 flags |= (outr.flags & 0xff80) | 0x8000;
313 head->flags = htons(flags);
314 head->nauth = head->nadd = htons(0);
315 head->nquer = htons(1);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000316
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000317 packet_len = (uint8_t *)next - buf;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000318 return packet_len;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000319}
320
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000321/*
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000322 * Exit on signal
323 */
Rob Landleya6e131d2006-05-29 06:43:55 +0000324static void interrupt(int x)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000325{
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000326 /* unlink("/var/run/dnsd.lock"); */
Denis Vlasenko2c916522007-01-12 14:57:37 +0000327 bb_error_msg("interrupt, exiting\n");
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000328 exit(2);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000329}
330
Denis Vlasenko06af2162007-02-03 17:28:39 +0000331int dnsd_main(int argc, char **argv);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000332int dnsd_main(int argc, char **argv)
333{
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000334 const char *listen_interface = "0.0.0.0";
Denis Vlasenko2c916522007-01-12 14:57:37 +0000335 char *sttl, *sport;
336 len_and_sockaddr *lsa;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000337 int udps;
338 uint16_t port = 53;
339 uint8_t buf[MAX_PACK_LEN];
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000340
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000341 getopt32(argc, argv, "i:c:t:p:dv", &listen_interface, &fileconf, &sttl, &sport);
342 //if (option_mask32 & 0x1) // -i
343 //if (option_mask32 & 0x2) // -c
344 if (option_mask32 & 0x4) // -t
Denis Vlasenko2c916522007-01-12 14:57:37 +0000345 ttl = xatou_range(sttl, 1, 0xffffffff);
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000346 if (option_mask32 & 0x8) // -p
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000347 port = xatou_range(sport, 1, 0xffff);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000348
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000349 if (OPT_verbose) {
350 bb_info_msg("listen_interface: %s", listen_interface);
351 bb_info_msg("ttl: %d, port: %d", ttl, port);
352 bb_info_msg("fileconf: %s", fileconf);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000353 }
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000354
Denis Vlasenko2c916522007-01-12 14:57:37 +0000355 if (OPT_daemon) {
Bernhard Reutner-Fischerc418d482006-05-31 10:19:51 +0000356#ifdef BB_NOMMU
Denis Vlasenko9067f132007-03-24 12:11:17 +0000357 if (!re_execed)
358 vfork_daemon_rexec(1, 0, argv);
Bernhard Reutner-Fischerc418d482006-05-31 10:19:51 +0000359#else
Rob Landley86b4d642006-08-03 17:58:17 +0000360 xdaemon(1, 0);
Bernhard Reutner-Fischerc418d482006-05-31 10:19:51 +0000361#endif
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000362 openlog(applet_name, LOG_PID, LOG_DAEMON);
Denis Vlasenko2c916522007-01-12 14:57:37 +0000363 logmode = LOGMODE_SYSLOG;
364 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000365
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000366 dnsentryinit();
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000367
368 signal(SIGINT, interrupt);
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000369 /* why? signal(SIGPIPE, SIG_IGN); */
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000370 signal(SIGHUP, SIG_IGN);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000371#ifdef SIGTSTP
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000372 signal(SIGTSTP, SIG_IGN);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000373#endif
374#ifdef SIGURG
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000375 signal(SIGURG, SIG_IGN);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000376#endif
377
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000378 lsa = xdotted2sockaddr(listen_interface, port);
Denis Vlasenko2c916522007-01-12 14:57:37 +0000379 udps = xsocket(lsa->sa.sa_family, SOCK_DGRAM, 0);
380 xbind(udps, &lsa->sa, lsa->len);
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000381 /* xlisten(udps, 50); - ?!! DGRAM sockets are never listened on I think? */
Denis Vlasenko2c916522007-01-12 14:57:37 +0000382 bb_info_msg("Accepting UDP packets on %s",
383 xmalloc_sockaddr2dotted(&lsa->sa, lsa->len));
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000384
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000385 while (1) {
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000386 int r;
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000387 socklen_t fromlen = lsa->len;
Denis Vlasenko2c916522007-01-12 14:57:37 +0000388// FIXME: need to get *DEST* address (to which of our addresses
389// this query was directed), and reply from the same address.
390// Or else we can exhibit usual UDP ugliness:
391// [ip1.multihomed.ip2] <= query to ip1 <= peer
392// [ip1.multihomed.ip2] => reply from ip2 => peer (confused)
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000393 r = recvfrom(udps, buf, sizeof(buf), 0, &lsa->sa, &fromlen);
394 if (OPT_verbose)
395 bb_info_msg("Got UDP packet");
396 if (r < 12 || r > 512) {
397 bb_error_msg("invalid packet size");
398 continue;
Denis Vlasenko2c916522007-01-12 14:57:37 +0000399 }
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000400 r = process_packet(buf);
401 if (r <= 0)
402 continue;
403 sendto(udps, buf, r, 0, &lsa->sa, fromlen);
Denis Vlasenko2c916522007-01-12 14:57:37 +0000404 }
Denis Vlasenkob5b45a92007-03-24 13:09:07 +0000405 return 0;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000406}