blob: c3bd1610b71487571eac3c489f7fd90487e87188 [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
Bernhard Reutner-Fischer421d9e52006-04-03 16:39:31 +000020#include "busybox.h"
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000021
Denis Vlasenko2c916522007-01-12 14:57:37 +000022static const char *fileconf = "/etc/dnsd.conf";
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000023#define LOCK_FILE "/var/run/dnsd.lock"
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000024
Denis Vlasenko2c916522007-01-12 14:57:37 +000025// Must match getopt32 call
Denis Vlasenkoc12f5302006-10-06 09:49:47 +000026#define OPT_daemon (option_mask32 & 0x10)
27#define OPT_verbose (option_mask32 & 0x20)
28
29//#define DEBUG 1
Denis Vlasenko2c916522007-01-12 14:57:37 +000030#define DEBUG 0
Denis Vlasenkoc12f5302006-10-06 09:49:47 +000031
Rob Landleybc68cd12006-03-10 19:22:06 +000032enum {
33 MAX_HOST_LEN = 16, // longest host name allowed is 15
34 IP_STRING_LEN = 18, // .xxx.xxx.xxx.xxx\0
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000035
36//must be strlen('.in-addr.arpa') larger than IP_STRING_LEN
Rob Landleybc68cd12006-03-10 19:22:06 +000037 MAX_NAME_LEN = (IP_STRING_LEN + 13),
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000038
39/* Cannot get bigger packets than 512 per RFC1035
40 In practice this can be set considerably smaller:
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000041 Length of response packet is header (12B) + 2*type(4B) + 2*class(4B) +
42 ttl(4B) + rlen(2B) + r (MAX_NAME_LEN =21B) +
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000043 2*querystring (2 MAX_NAME_LEN= 42B), all together 90 Byte
44*/
Rob Landleybc68cd12006-03-10 19:22:06 +000045 MAX_PACK_LEN = 512 + 1,
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000046
Rob Landleybc68cd12006-03-10 19:22:06 +000047 DEFAULT_TTL = 30, // increase this when not testing?
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000048
Rob Landleybc68cd12006-03-10 19:22:06 +000049 REQ_A = 1,
50 REQ_PTR = 12
51};
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000052
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000053struct dns_repl { // resource record, add 0 or 1 to accepted dns_msg in resp
54 uint16_t rlen;
55 uint8_t *r; // resource
56 uint16_t flags;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000057};
58
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000059struct dns_head { // the message from client and first part of response mag
60 uint16_t id;
61 uint16_t flags;
62 uint16_t nquer; // accepts 0
63 uint16_t nansw; // 1 in response
64 uint16_t nauth; // 0
65 uint16_t nadd; // 0
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000066};
67struct dns_prop {
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000068 uint16_t type;
69 uint16_t class;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000070};
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000071struct dns_entry { // element of known name, ip address and reversed ip address
72 struct dns_entry *next;
73 char ip[IP_STRING_LEN]; // dotted decimal IP
74 char rip[IP_STRING_LEN]; // length decimal reversed IP
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000075 char name[MAX_HOST_LEN];
76};
77
78static struct dns_entry *dnsentry = NULL;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000079static uint32_t ttl = DEFAULT_TTL;
80
81/*
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000082 * Convert host name from C-string to dns length/string.
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000083 */
Rob Landleya6e131d2006-05-29 06:43:55 +000084static void convname(char *a, uint8_t *q)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000085{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000086 int i = (q[0] == '.') ? 0 : 1;
Denis Vlasenkoc12f5302006-10-06 09:49:47 +000087 for (; i < MAX_HOST_LEN-1 && *q; i++, q++)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000088 a[i] = tolower(*q);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000089 a[0] = i - 1;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000090 a[i] = 0;
91}
92
93/*
Bernhard Reutner-Fischerc418d482006-05-31 10:19:51 +000094 * Insert length of substrings instead of dots
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000095 */
Rob Landleya6e131d2006-05-29 06:43:55 +000096static void undot(uint8_t * rip)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000097{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000098 int i = 0, s = 0;
Denis Vlasenkoc12f5302006-10-06 09:49:47 +000099 while (rip[i])
100 i++;
101 for (--i; i >= 0; i--) {
102 if (rip[i] == '.') {
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000103 rip[i] = s;
104 s = 0;
105 } else s++;
106 }
107}
108
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000109/*
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000110 * Read one line of hostname/IP from file
111 * Returns 0 for each valid entry read, -1 at EOF
112 * Assumes all host names are lower case only
113 * Hostnames with more than one label is not handled correctly.
114 * Presently the dot is copied into name without
115 * converting to a length/string substring for that label.
116 */
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;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000121 char *r, *name;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000122
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000123 restart:
Denis Vlasenko2d5ca602006-10-12 22:43:20 +0000124 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 Vlasenkoc12f5302006-10-06 09:49:47 +0000129 if (!*r || *r == '#' || *r == '\n')
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000130 goto restart; /* skipping empty/blank and commented lines */
131 }
132 name = r;
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000133 while (*r != ' ' && *r != '\t')
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000134 r++;
135 *r++ = 0;
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000136 if (sscanf(r, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
137 goto restart; /* skipping wrong lines */
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000138
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000139 sprintf(s->ip, "%u.%u.%u.%u", a, b, c, d);
140 sprintf(s->rip, ".%u.%u.%u.%u", d, c, b, a);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000141 undot((uint8_t*)s->rip);
142 convname(s->name,(uint8_t*)name);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000143
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000144 if (OPT_verbose)
145 fprintf(stderr, "\tname:%s, ip:%s\n", &(s->name[1]),s->ip);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000146
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000147 return 0;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000148}
149
150/*
151 * Read hostname/IP records from file
152 */
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000153static void dnsentryinit(void)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000154{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000155 FILE *fp;
156 struct dns_entry *m, *prev;
157 prev = dnsentry = NULL;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000158
Rob Landley86b4d642006-08-03 17:58:17 +0000159 fp = xfopen(fileconf, "r");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000160
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000161 while (1) {
Rob Landleya6e131d2006-05-29 06:43:55 +0000162 m = xmalloc(sizeof(struct dns_entry));
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000163
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000164 m->next = NULL;
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000165 if (getfileentry(fp, m))
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000166 break;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000167
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000168 if (prev == NULL)
169 dnsentry = m;
170 else
171 prev->next = m;
172 prev = m;
173 }
174 fclose(fp);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000175}
176
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000177/*
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000178 * Look query up in dns records and return answer if found
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000179 * qs is the query string, first byte the string length
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000180 */
Rob Landleya6e131d2006-05-29 06:43:55 +0000181static int table_lookup(uint16_t type, uint8_t * as, uint8_t * qs)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000182{
183 int i;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000184 struct dns_entry *d=dnsentry;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000185
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000186 do {
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000187#ifdef DEBUG
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000188 char *p,*q;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000189 q = (char *)&(qs[1]);
190 p = &(d->name[1]);
Denis Vlasenkof7996f32007-01-11 17:20:00 +0000191 fprintf(stderr, "\n%s: %d/%d p:%s q:%s %d",
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000192 __FUNCTION__, strlen(p), (int)(d->name[0]), p, q, strlen(q));
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000193#endif
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000194 if (type == REQ_A) { /* search by host name */
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000195 for (i = 1; i <= (int)(d->name[0]); i++)
196 if (tolower(qs[i]) != d->name[i])
197 break;
198 if (i > (int)(d->name[0])) {
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000199#ifdef DEBUG
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000200 fprintf(stderr, " OK");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000201#endif
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000202 strcpy((char *)as, d->ip);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000203#ifdef DEBUG
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000204 fprintf(stderr, " as:%s\n", as);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000205#endif
206 return 0;
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000207 }
Denis Vlasenkof7996f32007-01-11 17:20:00 +0000208 } else
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000209 if (type == REQ_PTR) { /* search by IP-address */
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000210 if (!strncmp((char*)&d->rip[1], (char*)&qs[1], strlen(d->rip)-1)) {
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000211 strcpy((char *)as, d->name);
212 return 0;
213 }
214 }
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000215 d = d->next;
216 } while (d);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000217 return -1;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000218}
219
220
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000221/*
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000222 * Decode message and generate answer
223 */
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000224#define eret(s) do { fputs(s, stderr); return -1; } while (0)
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;
239 if (head->nquer == 0)
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000240 eret("no queries\n");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000241
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000242 if (head->flags & 0x8000)
243 eret("ignoring response packet\n");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000244
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000245 from = (void *)&head[1]; // start of query string
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000246 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 +0000247
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000248 outr.rlen = 0; // may change later
249 outr.r = NULL;
250 outr.flags = 0;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000251
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000252 qprop = (struct dns_prop *)(answb - 4);
253 type = ntohs(qprop->type);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000254
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000255 // only let REQ_A and REQ_PTR pass
256 if (!(type == REQ_A || type == REQ_PTR)) {
257 goto empty_packet; /* we can't handle the query type */
258 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000259
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000260 if (ntohs(qprop->class) != 1 /* class INET */ ) {
261 outr.flags = 4; /* not supported */
262 goto empty_packet;
263 }
264 /* we only support standard queries */
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000265
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000266 if ((ntohs(head->flags) & 0x7800) != 0)
267 goto empty_packet;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000268
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000269 // We have a standard query
Denis Vlasenko2c916522007-01-12 14:57:37 +0000270 bb_info_msg("%s", (char *)from);
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000271 lookup_result = table_lookup(type, answstr, (uint8_t*)from);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000272 if (lookup_result != 0) {
273 outr.flags = 3 | 0x0400; //name do not exist and auth
274 goto empty_packet;
275 }
276 if (type == REQ_A) { // return an address
277 struct in_addr a;
278 if (!inet_aton((char*)answstr, &a)) {//dotted dec to long conv
279 outr.flags = 1; /* Frmt err */
280 goto empty_packet;
281 }
282 memcpy(answstr, &a.s_addr, 4); // save before a disappears
283 outr.rlen = 4; // uint32_t IP
284 }
285 else
286 outr.rlen = strlen((char *)answstr) + 1; // a host name
287 outr.r = answstr; // 32 bit ip or a host name
288 outr.flags |= 0x0400; /* authority-bit */
289 // we have an answer
290 head->nansw = htons(1);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000291
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000292 // copy query block to answer block
293 len = answb - from;
294 memcpy(answb, from, len);
295 next += len;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000296
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000297 // and append answer rr
298 *(uint32_t *) next = htonl(ttl);
299 next += 4;
300 *(uint16_t *) next = htons(outr.rlen);
301 next += 2;
302 memcpy(next, (void *)answstr, outr.rlen);
303 next += outr.rlen;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000304
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000305 empty_packet:
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000306
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000307 flags = ntohs(head->flags);
308 // clear rcode and RA, set responsebit and our new flags
309 flags |= (outr.flags & 0xff80) | 0x8000;
310 head->flags = htons(flags);
311 head->nauth = head->nadd = htons(0);
312 head->nquer = htons(1);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000313
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000314 packet_len = next - (void *)buf;
315 return packet_len;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000316}
317
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000318/*
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000319 * Exit on signal
320 */
Rob Landleya6e131d2006-05-29 06:43:55 +0000321static void interrupt(int x)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000322{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000323 unlink(LOCK_FILE);
Denis Vlasenko2c916522007-01-12 14:57:37 +0000324 bb_error_msg("interrupt, exiting\n");
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000325 exit(2);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000326}
327
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000328int dnsd_main(int argc, char **argv)
329{
Denis Vlasenko2c916522007-01-12 14:57:37 +0000330 char *listen_interface = NULL;
331 char *sttl, *sport;
332 len_and_sockaddr *lsa;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000333 int udps;
334 uint16_t port = 53;
335 uint8_t buf[MAX_PACK_LEN];
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000336
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000337 getopt32(argc, argv, "i:c:t:p:dv", &listen_interface, &fileconf, &sttl, &sport);
338 //if (option_mask32 & 0x1) // -i
339 //if (option_mask32 & 0x2) // -c
340 if (option_mask32 & 0x4) // -t
Denis Vlasenko2c916522007-01-12 14:57:37 +0000341 ttl = xatou_range(sttl, 1, 0xffffffff);
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000342 if (option_mask32 & 0x8) // -p
Denis Vlasenko2c916522007-01-12 14:57:37 +0000343 port = xatou_range(sttl, 1, 0xffff);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000344
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000345 if (OPT_verbose) {
346 bb_info_msg("listen_interface: %s", listen_interface);
347 bb_info_msg("ttl: %d, port: %d", ttl, port);
348 bb_info_msg("fileconf: %s", fileconf);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000349 }
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000350
Denis Vlasenko2c916522007-01-12 14:57:37 +0000351 if (OPT_daemon) {
352//FIXME: NOMMU will NOT set LOGMODE_SYSLOG!
Bernhard Reutner-Fischerc418d482006-05-31 10:19:51 +0000353#ifdef BB_NOMMU
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000354 /* reexec for vfork() do continue parent */
355 vfork_daemon_rexec(1, 0, argc, argv, "-d");
Bernhard Reutner-Fischerc418d482006-05-31 10:19:51 +0000356#else
Rob Landley86b4d642006-08-03 17:58:17 +0000357 xdaemon(1, 0);
Bernhard Reutner-Fischerc418d482006-05-31 10:19:51 +0000358#endif
Denis Vlasenko2c916522007-01-12 14:57:37 +0000359 logmode = LOGMODE_SYSLOG;
360 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000361
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000362 dnsentryinit();
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000363
364 signal(SIGINT, interrupt);
365 signal(SIGPIPE, SIG_IGN);
366 signal(SIGHUP, SIG_IGN);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000367#ifdef SIGTSTP
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000368 signal(SIGTSTP, SIG_IGN);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000369#endif
370#ifdef SIGURG
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000371 signal(SIGURG, SIG_IGN);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000372#endif
373
Denis Vlasenko2c916522007-01-12 14:57:37 +0000374 lsa = host2sockaddr(listen_interface, port);
375 udps = xsocket(lsa->sa.sa_family, SOCK_DGRAM, 0);
376 xbind(udps, &lsa->sa, lsa->len);
377 // xlisten(udps, 50); - ?!! DGRAM sockets are never listened on I think?
378 bb_info_msg("Accepting UDP packets on %s",
379 xmalloc_sockaddr2dotted(&lsa->sa, lsa->len));
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000380
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000381 while (1) {
382 fd_set fdset;
383 int r;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000384
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000385 FD_ZERO(&fdset);
386 FD_SET(udps, &fdset);
387 // Block until a message arrives
Denis Vlasenko2c916522007-01-12 14:57:37 +0000388// FIXME: Fantastic. select'ing on just one fd??
389// Why no just block on it doing recvfrom() ?
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000390 r = select(udps + 1, &fdset, NULL, NULL, NULL);
391 if (r < 0)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000392 bb_perror_msg_and_die("select error");
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000393 if (r == 0)
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000394 bb_perror_msg_and_die("select spurious return");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000395
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000396 /* Can this test ever be false? - yes */
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000397 if (FD_ISSET(udps, &fdset)) {
Denis Vlasenko2c916522007-01-12 14:57:37 +0000398 socklen_t fromlen = lsa->len;
399// FIXME: need to get *DEST* address (to which of our addresses
400// this query was directed), and reply from the same address.
401// Or else we can exhibit usual UDP ugliness:
402// [ip1.multihomed.ip2] <= query to ip1 <= peer
403// [ip1.multihomed.ip2] => reply from ip2 => peer (confused)
404 r = recvfrom(udps, buf, sizeof(buf), 0, &lsa->sa, &fromlen);
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000405 if (OPT_verbose)
Denis Vlasenko2c916522007-01-12 14:57:37 +0000406 bb_info_msg("Got UDP packet");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000407
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000408 if (r < 12 || r > 512) {
409 bb_error_msg("invalid packet size");
410 continue;
411 }
Denis Vlasenko2c916522007-01-12 14:57:37 +0000412 if (r <= 0)
413 continue;
414 r = process_packet(buf);
415 if (r <= 0)
416 continue;
417 sendto(udps, buf, r, 0, &lsa->sa, fromlen);
418 }
419 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000420}