blob: 78722d6f619c98a3e939b49ce009a5f3cf8af4ad [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
Denis Vlasenko6cd2d2b2007-01-22 14:06:03 +0000113 * Hostnames with more than one label are not handled correctly.
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000114 * 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;
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';
138 if (sscanf(r, "%u.%u.%u.%u", &a, &b, &c, &d) != 4) {
139 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 Vlasenkoc12f5302006-10-06 09:49:47 +0000143 sprintf(s->ip, "%u.%u.%u.%u", a, b, c, d);
144 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;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +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 Vlasenko91f20ab2007-01-20 01:47:44 +0000204#if DEBUG
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000205 fprintf(stderr, " OK");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000206#endif
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000207 strcpy((char *)as, d->ip);
Denis Vlasenko91f20ab2007-01-20 01:47:44 +0000208#if DEBUG
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000209 fprintf(stderr, " as:%s\n", as);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000210#endif
211 return 0;
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000212 }
Denis Vlasenkof7996f32007-01-11 17:20:00 +0000213 } else
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000214 if (type == REQ_PTR) { /* search by IP-address */
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000215 if (!strncmp((char*)&d->rip[1], (char*)&qs[1], strlen(d->rip)-1)) {
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000216 strcpy((char *)as, d->name);
217 return 0;
218 }
219 }
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000220 d = d->next;
221 } while (d);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000222 return -1;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000223}
224
225
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000226/*
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000227 * Decode message and generate answer
228 */
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000229#define eret(s) do { fputs(s, stderr); return -1; } while (0)
Rob Landleya6e131d2006-05-29 06:43:55 +0000230static int process_packet(uint8_t * buf)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000231{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000232 struct dns_head *head;
233 struct dns_prop *qprop;
234 struct dns_repl outr;
235 void *next, *from, *answb;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000236
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000237 uint8_t answstr[MAX_NAME_LEN + 1];
238 int lookup_result, type, len, packet_len;
239 uint16_t flags;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000240
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000241 answstr[0] = '\0';
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000242
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000243 head = (struct dns_head *)buf;
244 if (head->nquer == 0)
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000245 eret("no queries\n");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000246
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000247 if (head->flags & 0x8000)
248 eret("ignoring response packet\n");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000249
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000250 from = (void *)&head[1]; // start of query string
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000251 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 +0000252
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000253 outr.rlen = 0; // may change later
254 outr.r = NULL;
255 outr.flags = 0;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000256
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000257 qprop = (struct dns_prop *)(answb - 4);
258 type = ntohs(qprop->type);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000259
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000260 // only let REQ_A and REQ_PTR pass
261 if (!(type == REQ_A || type == REQ_PTR)) {
262 goto empty_packet; /* we can't handle the query type */
263 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000264
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000265 if (ntohs(qprop->class) != 1 /* class INET */ ) {
266 outr.flags = 4; /* not supported */
267 goto empty_packet;
268 }
269 /* we only support standard queries */
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000270
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000271 if ((ntohs(head->flags) & 0x7800) != 0)
272 goto empty_packet;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000273
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000274 // We have a standard query
Denis Vlasenko2c916522007-01-12 14:57:37 +0000275 bb_info_msg("%s", (char *)from);
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000276 lookup_result = table_lookup(type, answstr, (uint8_t*)from);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000277 if (lookup_result != 0) {
278 outr.flags = 3 | 0x0400; //name do not exist and auth
279 goto empty_packet;
280 }
281 if (type == REQ_A) { // return an address
282 struct in_addr a;
283 if (!inet_aton((char*)answstr, &a)) {//dotted dec to long conv
284 outr.flags = 1; /* Frmt err */
285 goto empty_packet;
286 }
287 memcpy(answstr, &a.s_addr, 4); // save before a disappears
288 outr.rlen = 4; // uint32_t IP
289 }
290 else
291 outr.rlen = strlen((char *)answstr) + 1; // a host name
292 outr.r = answstr; // 32 bit ip or a host name
293 outr.flags |= 0x0400; /* authority-bit */
294 // we have an answer
295 head->nansw = htons(1);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000296
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000297 // copy query block to answer block
298 len = answb - from;
299 memcpy(answb, from, len);
300 next += len;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000301
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000302 // and append answer rr
303 *(uint32_t *) next = htonl(ttl);
304 next += 4;
305 *(uint16_t *) next = htons(outr.rlen);
306 next += 2;
307 memcpy(next, (void *)answstr, outr.rlen);
308 next += outr.rlen;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000309
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000310 empty_packet:
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000311
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000312 flags = ntohs(head->flags);
313 // clear rcode and RA, set responsebit and our new flags
314 flags |= (outr.flags & 0xff80) | 0x8000;
315 head->flags = htons(flags);
316 head->nauth = head->nadd = htons(0);
317 head->nquer = htons(1);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000318
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000319 packet_len = next - (void *)buf;
320 return packet_len;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000321}
322
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000323/*
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000324 * Exit on signal
325 */
Rob Landleya6e131d2006-05-29 06:43:55 +0000326static void interrupt(int x)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000327{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000328 unlink(LOCK_FILE);
Denis Vlasenko2c916522007-01-12 14:57:37 +0000329 bb_error_msg("interrupt, exiting\n");
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000330 exit(2);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000331}
332
Denis Vlasenko06af2162007-02-03 17:28:39 +0000333int dnsd_main(int argc, char **argv);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000334int dnsd_main(int argc, char **argv)
335{
Denis Vlasenko2c916522007-01-12 14:57:37 +0000336 char *listen_interface = NULL;
337 char *sttl, *sport;
338 len_and_sockaddr *lsa;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000339 int udps;
340 uint16_t port = 53;
341 uint8_t buf[MAX_PACK_LEN];
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000342
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000343 getopt32(argc, argv, "i:c:t:p:dv", &listen_interface, &fileconf, &sttl, &sport);
344 //if (option_mask32 & 0x1) // -i
345 //if (option_mask32 & 0x2) // -c
346 if (option_mask32 & 0x4) // -t
Denis Vlasenko2c916522007-01-12 14:57:37 +0000347 ttl = xatou_range(sttl, 1, 0xffffffff);
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000348 if (option_mask32 & 0x8) // -p
Denis Vlasenko2c916522007-01-12 14:57:37 +0000349 port = xatou_range(sttl, 1, 0xffff);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000350
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000351 if (OPT_verbose) {
352 bb_info_msg("listen_interface: %s", listen_interface);
353 bb_info_msg("ttl: %d, port: %d", ttl, port);
354 bb_info_msg("fileconf: %s", fileconf);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000355 }
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000356
Denis Vlasenko2c916522007-01-12 14:57:37 +0000357 if (OPT_daemon) {
358//FIXME: NOMMU will NOT set LOGMODE_SYSLOG!
Bernhard Reutner-Fischerc418d482006-05-31 10:19:51 +0000359#ifdef BB_NOMMU
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000360 /* reexec for vfork() do continue parent */
361 vfork_daemon_rexec(1, 0, argc, argv, "-d");
Bernhard Reutner-Fischerc418d482006-05-31 10:19:51 +0000362#else
Rob Landley86b4d642006-08-03 17:58:17 +0000363 xdaemon(1, 0);
Bernhard Reutner-Fischerc418d482006-05-31 10:19:51 +0000364#endif
Denis Vlasenko2c916522007-01-12 14:57:37 +0000365 logmode = LOGMODE_SYSLOG;
366 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000367
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000368 dnsentryinit();
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000369
370 signal(SIGINT, interrupt);
371 signal(SIGPIPE, SIG_IGN);
372 signal(SIGHUP, SIG_IGN);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000373#ifdef SIGTSTP
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000374 signal(SIGTSTP, SIG_IGN);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000375#endif
376#ifdef SIGURG
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000377 signal(SIGURG, SIG_IGN);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000378#endif
379
Denis Vlasenko42823d52007-02-04 02:39:08 +0000380 lsa = xhost2sockaddr(listen_interface, port);
Denis Vlasenko2c916522007-01-12 14:57:37 +0000381 udps = xsocket(lsa->sa.sa_family, SOCK_DGRAM, 0);
382 xbind(udps, &lsa->sa, lsa->len);
383 // xlisten(udps, 50); - ?!! DGRAM sockets are never listened on I think?
384 bb_info_msg("Accepting UDP packets on %s",
385 xmalloc_sockaddr2dotted(&lsa->sa, lsa->len));
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000386
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000387 while (1) {
388 fd_set fdset;
389 int r;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000390
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000391 FD_ZERO(&fdset);
392 FD_SET(udps, &fdset);
393 // Block until a message arrives
Denis Vlasenko2c916522007-01-12 14:57:37 +0000394// FIXME: Fantastic. select'ing on just one fd??
395// Why no just block on it doing recvfrom() ?
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000396 r = select(udps + 1, &fdset, NULL, NULL, NULL);
397 if (r < 0)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000398 bb_perror_msg_and_die("select error");
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000399 if (r == 0)
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000400 bb_perror_msg_and_die("select spurious return");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000401
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000402 /* Can this test ever be false? - yes */
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000403 if (FD_ISSET(udps, &fdset)) {
Denis Vlasenko2c916522007-01-12 14:57:37 +0000404 socklen_t fromlen = lsa->len;
405// FIXME: need to get *DEST* address (to which of our addresses
406// this query was directed), and reply from the same address.
407// Or else we can exhibit usual UDP ugliness:
408// [ip1.multihomed.ip2] <= query to ip1 <= peer
409// [ip1.multihomed.ip2] => reply from ip2 => peer (confused)
410 r = recvfrom(udps, buf, sizeof(buf), 0, &lsa->sa, &fromlen);
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000411 if (OPT_verbose)
Denis Vlasenko2c916522007-01-12 14:57:37 +0000412 bb_info_msg("Got UDP packet");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000413
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000414 if (r < 12 || r > 512) {
415 bb_error_msg("invalid packet size");
416 continue;
417 }
Denis Vlasenko2c916522007-01-12 14:57:37 +0000418 if (r <= 0)
419 continue;
420 r = process_packet(buf);
421 if (r <= 0)
422 continue;
423 sendto(udps, buf, r, 0, &lsa->sa, fromlen);
424 }
425 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000426}