blob: 71f50423dbad80e5fcf3f4786424d79b477aad72 [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 {
Denis Vlasenko91f20ab2007-01-20 01:47:44 +0000187#if 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 Vlasenko91f20ab2007-01-20 01:47:44 +0000192 __FUNCTION__, (int)strlen(p), (int)(d->name[0]),
193 p, q, (int)strlen(q));
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000194#endif
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000195 if (type == REQ_A) { /* search by host name */
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000196 for (i = 1; i <= (int)(d->name[0]); i++)
197 if (tolower(qs[i]) != d->name[i])
198 break;
199 if (i > (int)(d->name[0])) {
Denis Vlasenko91f20ab2007-01-20 01:47:44 +0000200#if DEBUG
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000201 fprintf(stderr, " OK");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000202#endif
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000203 strcpy((char *)as, d->ip);
Denis Vlasenko91f20ab2007-01-20 01:47:44 +0000204#if DEBUG
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000205 fprintf(stderr, " as:%s\n", as);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000206#endif
207 return 0;
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000208 }
Denis Vlasenkof7996f32007-01-11 17:20:00 +0000209 } else
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000210 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 */
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000225#define eret(s) do { fputs(s, stderr); return -1; } while (0)
Rob Landleya6e131d2006-05-29 06:43:55 +0000226static int process_packet(uint8_t * buf)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000227{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000228 struct dns_head *head;
229 struct dns_prop *qprop;
230 struct dns_repl outr;
231 void *next, *from, *answb;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000232
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000233 uint8_t answstr[MAX_NAME_LEN + 1];
234 int lookup_result, type, len, packet_len;
235 uint16_t flags;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000236
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000237 answstr[0] = '\0';
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000238
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000239 head = (struct dns_head *)buf;
240 if (head->nquer == 0)
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000241 eret("no queries\n");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000242
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000243 if (head->flags & 0x8000)
244 eret("ignoring response packet\n");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000245
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000246 from = (void *)&head[1]; // start of query string
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000247 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 +0000248
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000249 outr.rlen = 0; // may change later
250 outr.r = NULL;
251 outr.flags = 0;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000252
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000253 qprop = (struct dns_prop *)(answb - 4);
254 type = ntohs(qprop->type);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000255
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000256 // only let REQ_A and REQ_PTR pass
257 if (!(type == REQ_A || type == REQ_PTR)) {
258 goto empty_packet; /* we can't handle the query type */
259 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000260
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000261 if (ntohs(qprop->class) != 1 /* class INET */ ) {
262 outr.flags = 4; /* not supported */
263 goto empty_packet;
264 }
265 /* we only support standard queries */
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000266
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000267 if ((ntohs(head->flags) & 0x7800) != 0)
268 goto empty_packet;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000269
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000270 // We have a standard query
Denis Vlasenko2c916522007-01-12 14:57:37 +0000271 bb_info_msg("%s", (char *)from);
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000272 lookup_result = table_lookup(type, answstr, (uint8_t*)from);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000273 if (lookup_result != 0) {
274 outr.flags = 3 | 0x0400; //name do not exist and auth
275 goto empty_packet;
276 }
277 if (type == REQ_A) { // return an address
278 struct in_addr a;
279 if (!inet_aton((char*)answstr, &a)) {//dotted dec to long conv
280 outr.flags = 1; /* Frmt err */
281 goto empty_packet;
282 }
283 memcpy(answstr, &a.s_addr, 4); // save before a disappears
284 outr.rlen = 4; // uint32_t IP
285 }
286 else
287 outr.rlen = strlen((char *)answstr) + 1; // a host name
288 outr.r = answstr; // 32 bit ip or a host name
289 outr.flags |= 0x0400; /* authority-bit */
290 // we have an answer
291 head->nansw = htons(1);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000292
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000293 // copy query block to answer block
294 len = answb - from;
295 memcpy(answb, from, len);
296 next += len;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000297
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000298 // and append answer rr
299 *(uint32_t *) next = htonl(ttl);
300 next += 4;
301 *(uint16_t *) next = htons(outr.rlen);
302 next += 2;
303 memcpy(next, (void *)answstr, outr.rlen);
304 next += outr.rlen;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000305
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000306 empty_packet:
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000307
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000308 flags = ntohs(head->flags);
309 // clear rcode and RA, set responsebit and our new flags
310 flags |= (outr.flags & 0xff80) | 0x8000;
311 head->flags = htons(flags);
312 head->nauth = head->nadd = htons(0);
313 head->nquer = htons(1);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000314
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000315 packet_len = next - (void *)buf;
316 return packet_len;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000317}
318
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000319/*
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000320 * Exit on signal
321 */
Rob Landleya6e131d2006-05-29 06:43:55 +0000322static void interrupt(int x)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000323{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000324 unlink(LOCK_FILE);
Denis Vlasenko2c916522007-01-12 14:57:37 +0000325 bb_error_msg("interrupt, exiting\n");
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000326 exit(2);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000327}
328
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000329int dnsd_main(int argc, char **argv)
330{
Denis Vlasenko2c916522007-01-12 14:57:37 +0000331 char *listen_interface = NULL;
332 char *sttl, *sport;
333 len_and_sockaddr *lsa;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000334 int udps;
335 uint16_t port = 53;
336 uint8_t buf[MAX_PACK_LEN];
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000337
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000338 getopt32(argc, argv, "i:c:t:p:dv", &listen_interface, &fileconf, &sttl, &sport);
339 //if (option_mask32 & 0x1) // -i
340 //if (option_mask32 & 0x2) // -c
341 if (option_mask32 & 0x4) // -t
Denis Vlasenko2c916522007-01-12 14:57:37 +0000342 ttl = xatou_range(sttl, 1, 0xffffffff);
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000343 if (option_mask32 & 0x8) // -p
Denis Vlasenko2c916522007-01-12 14:57:37 +0000344 port = xatou_range(sttl, 1, 0xffff);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000345
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000346 if (OPT_verbose) {
347 bb_info_msg("listen_interface: %s", listen_interface);
348 bb_info_msg("ttl: %d, port: %d", ttl, port);
349 bb_info_msg("fileconf: %s", fileconf);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000350 }
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000351
Denis Vlasenko2c916522007-01-12 14:57:37 +0000352 if (OPT_daemon) {
353//FIXME: NOMMU will NOT set LOGMODE_SYSLOG!
Bernhard Reutner-Fischerc418d482006-05-31 10:19:51 +0000354#ifdef BB_NOMMU
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000355 /* reexec for vfork() do continue parent */
356 vfork_daemon_rexec(1, 0, argc, argv, "-d");
Bernhard Reutner-Fischerc418d482006-05-31 10:19:51 +0000357#else
Rob Landley86b4d642006-08-03 17:58:17 +0000358 xdaemon(1, 0);
Bernhard Reutner-Fischerc418d482006-05-31 10:19:51 +0000359#endif
Denis Vlasenko2c916522007-01-12 14:57:37 +0000360 logmode = LOGMODE_SYSLOG;
361 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000362
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000363 dnsentryinit();
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000364
365 signal(SIGINT, interrupt);
366 signal(SIGPIPE, SIG_IGN);
367 signal(SIGHUP, SIG_IGN);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000368#ifdef SIGTSTP
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000369 signal(SIGTSTP, SIG_IGN);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000370#endif
371#ifdef SIGURG
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000372 signal(SIGURG, SIG_IGN);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000373#endif
374
Denis Vlasenko2c916522007-01-12 14:57:37 +0000375 lsa = host2sockaddr(listen_interface, port);
376 udps = xsocket(lsa->sa.sa_family, SOCK_DGRAM, 0);
377 xbind(udps, &lsa->sa, lsa->len);
378 // xlisten(udps, 50); - ?!! DGRAM sockets are never listened on I think?
379 bb_info_msg("Accepting UDP packets on %s",
380 xmalloc_sockaddr2dotted(&lsa->sa, lsa->len));
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000381
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000382 while (1) {
383 fd_set fdset;
384 int r;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000385
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000386 FD_ZERO(&fdset);
387 FD_SET(udps, &fdset);
388 // Block until a message arrives
Denis Vlasenko2c916522007-01-12 14:57:37 +0000389// FIXME: Fantastic. select'ing on just one fd??
390// Why no just block on it doing recvfrom() ?
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000391 r = select(udps + 1, &fdset, NULL, NULL, NULL);
392 if (r < 0)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000393 bb_perror_msg_and_die("select error");
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000394 if (r == 0)
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000395 bb_perror_msg_and_die("select spurious return");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000396
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000397 /* Can this test ever be false? - yes */
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000398 if (FD_ISSET(udps, &fdset)) {
Denis Vlasenko2c916522007-01-12 14:57:37 +0000399 socklen_t fromlen = lsa->len;
400// FIXME: need to get *DEST* address (to which of our addresses
401// this query was directed), and reply from the same address.
402// Or else we can exhibit usual UDP ugliness:
403// [ip1.multihomed.ip2] <= query to ip1 <= peer
404// [ip1.multihomed.ip2] => reply from ip2 => peer (confused)
405 r = recvfrom(udps, buf, sizeof(buf), 0, &lsa->sa, &fromlen);
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000406 if (OPT_verbose)
Denis Vlasenko2c916522007-01-12 14:57:37 +0000407 bb_info_msg("Got UDP packet");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000408
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000409 if (r < 12 || r > 512) {
410 bb_error_msg("invalid packet size");
411 continue;
412 }
Denis Vlasenko2c916522007-01-12 14:57:37 +0000413 if (r <= 0)
414 continue;
415 r = process_packet(buf);
416 if (r <= 0)
417 continue;
418 sendto(udps, buf, r, 0, &lsa->sa, fromlen);
419 }
420 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000421}