blob: 9ca4105d388c473cad32a94df1bab46b92a45043 [file] [log] [blame]
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +00001/*
2 * Mini DNS server implementation for busybox
3 *
4 * Copyright (C) 2005 Roberto A. Foglietta (me@roberto.foglietta.name)
5 * Copyright (C) 2005 Odd Arild Olsen (oao at fibula dot no)
6 * Copyright (C) 2003 Paul Sheer
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8 *
9 * Odd Arild Olsen started out with the sheerdns [1] of Paul Sheer and rewrote
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000010 * it into a shape which I believe is both easier to understand and maintain.
11 * I also reused the input buffer for output and removed services he did not
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000012 * need. [1] http://threading.2038bug.com/sheerdns/
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000013 *
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000014 * Some bugfix and minor changes was applied by Roberto A. Foglietta who made
15 * the first porting of oao' scdns to busybox also.
16 */
17
18#include <unistd.h>
19#include <string.h>
20#include <signal.h>
21#include <arpa/inet.h>
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000022#include <sys/socket.h>
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000023#include <ctype.h>
24#include "libbb.h"
25
26static char *fileconf = "/etc/dnsd.conf";
27#define LOCK_FILE "/var/run/dnsd.lock"
28#define LOG_FILE "/var/log/dnsd.log"
29
Rob Landleybc68cd12006-03-10 19:22:06 +000030enum {
31 MAX_HOST_LEN = 16, // longest host name allowed is 15
32 IP_STRING_LEN = 18, // .xxx.xxx.xxx.xxx\0
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000033
34//must be strlen('.in-addr.arpa') larger than IP_STRING_LEN
Rob Landleybc68cd12006-03-10 19:22:06 +000035 MAX_NAME_LEN = (IP_STRING_LEN + 13),
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000036
37/* Cannot get bigger packets than 512 per RFC1035
38 In practice this can be set considerably smaller:
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000039 Length of response packet is header (12B) + 2*type(4B) + 2*class(4B) +
40 ttl(4B) + rlen(2B) + r (MAX_NAME_LEN =21B) +
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000041 2*querystring (2 MAX_NAME_LEN= 42B), all together 90 Byte
42*/
Rob Landleybc68cd12006-03-10 19:22:06 +000043 MAX_PACK_LEN = 512 + 1,
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000044
Rob Landleybc68cd12006-03-10 19:22:06 +000045 DEFAULT_TTL = 30, // increase this when not testing?
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000046
Rob Landleybc68cd12006-03-10 19:22:06 +000047 REQ_A = 1,
48 REQ_PTR = 12
49};
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000050
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000051struct dns_repl { // resource record, add 0 or 1 to accepted dns_msg in resp
52 uint16_t rlen;
53 uint8_t *r; // resource
54 uint16_t flags;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000055};
56
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000057struct dns_head { // the message from client and first part of response mag
58 uint16_t id;
59 uint16_t flags;
60 uint16_t nquer; // accepts 0
61 uint16_t nansw; // 1 in response
62 uint16_t nauth; // 0
63 uint16_t nadd; // 0
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000064};
65struct dns_prop {
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000066 uint16_t type;
67 uint16_t class;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000068};
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000069struct dns_entry { // element of known name, ip address and reversed ip address
70 struct dns_entry *next;
71 char ip[IP_STRING_LEN]; // dotted decimal IP
72 char rip[IP_STRING_LEN]; // length decimal reversed IP
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000073 char name[MAX_HOST_LEN];
74};
75
76static struct dns_entry *dnsentry = NULL;
77static int daemonmode = 0;
78static uint32_t ttl = DEFAULT_TTL;
79
80/*
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000081 * Convert host name from C-string to dns length/string.
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000082 */
83static void
84convname(char *a, uint8_t *q)
85{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000086 int i = (q[0] == '.') ? 0 : 1;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000087 for(; i < MAX_HOST_LEN-1 && *q; i++, q++)
88 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/*
94 * Insert length of substrings insetad of dots
95 */
96static void
97undot(uint8_t * rip)
98{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000099 int i = 0, s = 0;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000100 while(rip[i]) i++;
101 for(--i; i >= 0; i--) {
102 if(rip[i] == '.') {
103 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 * Append message to log file
111 */
112static void
113log_message(char *filename, char *message)
114{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000115 FILE *logfile;
116 if (!daemonmode)
117 return;
118 logfile = fopen(filename, "a");
119 if (!logfile)
120 return;
121 fprintf(logfile, "%s\n", message);
122 fclose(logfile);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000123}
124
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000125/*
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000126 * Read one line of hostname/IP from file
127 * Returns 0 for each valid entry read, -1 at EOF
128 * Assumes all host names are lower case only
129 * Hostnames with more than one label is not handled correctly.
130 * Presently the dot is copied into name without
131 * converting to a length/string substring for that label.
132 */
133
134static int
135getfileentry(FILE * fp, struct dns_entry *s, int verb)
136{
137 unsigned int a,b,c,d;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000138 char *r, *name;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000139
140restart:
141 if(!(r = bb_get_line_from_file(fp)))
142 return -1;
143 while(*r == ' ' || *r == '\t') {
144 r++;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000145 if(!*r || *r == '#' || *r == '\n')
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000146 goto restart; /* skipping empty/blank and commented lines */
147 }
148 name = r;
149 while(*r != ' ' && *r != '\t')
150 r++;
151 *r++ = 0;
152 if(sscanf(r,"%u.%u.%u.%u",&a,&b,&c,&d) != 4)
153 goto restart; /* skipping wrong lines */
154
155 sprintf(s->ip,"%u.%u.%u.%u",a,b,c,d);
156 sprintf(s->rip,".%u.%u.%u.%u",d,c,b,a);
157 undot((uint8_t*)s->rip);
158 convname(s->name,(uint8_t*)name);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000159
160 if(verb)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000161 fprintf(stderr,"\tname:%s, ip:%s\n",&(s->name[1]),s->ip);
162
163 return 0; /* warningkiller */
164}
165
166/*
167 * Read hostname/IP records from file
168 */
169static void
170dnsentryinit(int verb)
171{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000172 FILE *fp;
173 struct dns_entry *m, *prev;
174 prev = dnsentry = NULL;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000175
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000176 if(!(fp = fopen(fileconf, "r")))
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000177 bb_perror_msg_and_die("open %s",fileconf);
178
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000179 while (1) {
180 if(!(m = (struct dns_entry *)malloc(sizeof(struct dns_entry))))
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000181 bb_perror_msg_and_die("malloc dns_entry");
182
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000183 m->next = NULL;
184 if (getfileentry(fp, m, verb))
185 break;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000186
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000187 if (prev == NULL)
188 dnsentry = m;
189 else
190 prev->next = m;
191 prev = m;
192 }
193 fclose(fp);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000194}
195
196
197/*
198 * Set up UDP socket
199 */
200static int
201listen_socket(char *iface_addr, int listen_port)
202{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000203 struct sockaddr_in a;
204 char msg[100];
205 int s;
206 int yes = 1;
207 if ((s = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
208 bb_perror_msg_and_die("socket() failed");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000209#ifdef SO_REUSEADDR
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000210 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&yes, sizeof(yes)) < 0)
211 bb_perror_msg_and_die("setsockopt() failed");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000212#endif
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000213 memset(&a, 0, sizeof(a));
214 a.sin_port = htons(listen_port);
215 a.sin_family = AF_INET;
216 if (!inet_aton(iface_addr, &a.sin_addr))
217 bb_perror_msg_and_die("bad iface address");
218 if (bind(s, (struct sockaddr *)&a, sizeof(a)) < 0)
219 bb_perror_msg_and_die("bind() failed");
220 listen(s, 50);
221 sprintf(msg, "accepting UDP packets on addr:port %s:%d\n",
222 iface_addr, (int)listen_port);
223 log_message(LOG_FILE, msg);
224 return s;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000225}
226
227/*
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000228 * Look query up in dns records and return answer if found
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000229 * qs is the query string, first byte the string length
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000230 */
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000231static int
232table_lookup(uint16_t type, uint8_t * as, uint8_t * qs)
233{
234 int i;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000235 struct dns_entry *d=dnsentry;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000236
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000237 do {
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000238#ifdef DEBUG
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000239 char *p,*q;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000240 q = (char *)&(qs[1]);
241 p = &(d->name[1]);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000242 fprintf(stderr, "\ntest: %d <%s> <%s> %d", strlen(p), p, q, strlen(q));
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000243#endif
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000244 if (type == REQ_A) { /* search by host name */
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000245 for(i = 1; i <= (int)(d->name[0]); i++)
246 if(tolower(qs[i]) != d->name[i])
247 continue;
248#ifdef DEBUG
249 fprintf(stderr, " OK");
250#endif
251 strcpy((char *)as, d->ip);
252#ifdef DEBUG
253 fprintf(stderr, " %s ", as);
254#endif
255 return 0;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000256 }
257 else if (type == REQ_PTR) { /* search by IP-address */
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000258 if (!strncmp((char*)&d->rip[1], (char*)&qs[1], strlen(d->rip)-1)) {
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000259 strcpy((char *)as, d->name);
260 return 0;
261 }
262 }
263 } while ((d = d->next) != NULL);
264 return -1;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000265}
266
267
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000268/*
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000269 * Decode message and generate answer
270 */
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000271#define eret(s) do { fprintf (stderr, "%s\n", s); return -1; } while (0)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000272static int
273process_packet(uint8_t * buf)
274{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000275 struct dns_head *head;
276 struct dns_prop *qprop;
277 struct dns_repl outr;
278 void *next, *from, *answb;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000279
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000280 uint8_t answstr[MAX_NAME_LEN + 1];
281 int lookup_result, type, len, packet_len;
282 uint16_t flags;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000283
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000284 answstr[0] = '\0';
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000285
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000286 head = (struct dns_head *)buf;
287 if (head->nquer == 0)
288 eret("no queries");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000289
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000290 if ((head->flags & 0x8000))
291 eret("ignoring response packet");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000292
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000293 from = (void *)&head[1]; // start of query string
294 next = answb = from + strlen((char *)&head[1]) + 1 + sizeof(struct dns_prop); // where to append answer block
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000295
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000296 outr.rlen = 0; // may change later
297 outr.r = NULL;
298 outr.flags = 0;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000299
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000300 qprop = (struct dns_prop *)(answb - 4);
301 type = ntohs(qprop->type);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000302
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000303 // only let REQ_A and REQ_PTR pass
304 if (!(type == REQ_A || type == REQ_PTR)) {
305 goto empty_packet; /* we can't handle the query type */
306 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000307
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000308 if (ntohs(qprop->class) != 1 /* class INET */ ) {
309 outr.flags = 4; /* not supported */
310 goto empty_packet;
311 }
312 /* we only support standard queries */
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000313
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000314 if ((ntohs(head->flags) & 0x7800) != 0)
315 goto empty_packet;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000316
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000317 // We have a standard query
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000318
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000319 log_message(LOG_FILE, (char *)head);
320 lookup_result = table_lookup(type, answstr, (uint8_t*)(&head[1]));
321 if (lookup_result != 0) {
322 outr.flags = 3 | 0x0400; //name do not exist and auth
323 goto empty_packet;
324 }
325 if (type == REQ_A) { // return an address
326 struct in_addr a;
327 if (!inet_aton((char*)answstr, &a)) {//dotted dec to long conv
328 outr.flags = 1; /* Frmt err */
329 goto empty_packet;
330 }
331 memcpy(answstr, &a.s_addr, 4); // save before a disappears
332 outr.rlen = 4; // uint32_t IP
333 }
334 else
335 outr.rlen = strlen((char *)answstr) + 1; // a host name
336 outr.r = answstr; // 32 bit ip or a host name
337 outr.flags |= 0x0400; /* authority-bit */
338 // we have an answer
339 head->nansw = htons(1);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000340
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000341 // copy query block to answer block
342 len = answb - from;
343 memcpy(answb, from, len);
344 next += len;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000345
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000346 // and append answer rr
347 *(uint32_t *) next = htonl(ttl);
348 next += 4;
349 *(uint16_t *) next = htons(outr.rlen);
350 next += 2;
351 memcpy(next, (void *)answstr, outr.rlen);
352 next += outr.rlen;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000353
354 empty_packet:
355
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000356 flags = ntohs(head->flags);
357 // clear rcode and RA, set responsebit and our new flags
358 flags |= (outr.flags & 0xff80) | 0x8000;
359 head->flags = htons(flags);
360 head->nauth = head->nadd = htons(0);
361 head->nquer = htons(1);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000362
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000363 packet_len = next - (void *)buf;
364 return packet_len;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000365}
366
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000367/*
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000368 * Exit on signal
369 */
370static void
371interrupt(int x)
372{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000373 unlink(LOCK_FILE);
374 write(2, "interrupt exiting\n", 18);
375 exit(2);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000376}
377
378#define is_daemon() (flags&16)
379#define is_verbose() (flags&32)
380//#define DEBUG 1
381
382int dnsd_main(int argc, char **argv)
383{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000384 int udps;
385 uint16_t port = 53;
386 uint8_t buf[MAX_PACK_LEN];
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000387 unsigned long flags = 0;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000388 char *listen_interface = "0.0.0.0";
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000389 char *sttl=NULL, *sport=NULL;
390
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000391 if(argc > 1)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000392 flags = bb_getopt_ulflags(argc, argv, "i:c:t:p:dv", &listen_interface, &fileconf, &sttl, &sport);
393 if(sttl)
394 if(!(ttl = atol(sttl)))
395 bb_show_usage();
396 if(sport)
397 if(!(port = atol(sport)))
398 bb_show_usage();
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000399
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000400 if(is_verbose()) {
401 fprintf(stderr,"listen_interface: %s\n", listen_interface);
402 fprintf(stderr,"ttl: %d, port: %d\n", ttl, port);
403 fprintf(stderr,"fileconf: %s\n", fileconf);
404 }
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000405
406 if(is_daemon())
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000407#if defined(__uClinux__)
408 /* reexec for vfork() do continue parent */
409 vfork_daemon_rexec(1, 0, argc, argv, "-d");
410#else /* uClinux */
411 if (daemon(1, 0) < 0) {
412 bb_perror_msg_and_die("daemon");
413 }
414#endif /* uClinuvx */
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000415
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000416 dnsentryinit(is_verbose());
417
418 signal(SIGINT, interrupt);
419 signal(SIGPIPE, SIG_IGN);
420 signal(SIGHUP, SIG_IGN);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000421#ifdef SIGTSTP
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000422 signal(SIGTSTP, SIG_IGN);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000423#endif
424#ifdef SIGURG
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000425 signal(SIGURG, SIG_IGN);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000426#endif
427
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000428 udps = listen_socket(listen_interface, port);
429 if (udps < 0)
430 exit(1);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000431
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000432 while (1) {
433 fd_set fdset;
434 int r;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000435
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000436 FD_ZERO(&fdset);
437 FD_SET(udps, &fdset);
438 // Block until a message arrives
439 if((r = select(udps + 1, &fdset, NULL, NULL, NULL)) < 0)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000440 bb_perror_msg_and_die("select error");
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000441 else
442 if(r == 0)
443 bb_perror_msg_and_die("select spurious return");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000444
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000445 /* Can this test ever be false? */
446 if (FD_ISSET(udps, &fdset)) {
447 struct sockaddr_in from;
448 int fromlen = sizeof(from);
449 r = recvfrom(udps, buf, sizeof(buf), 0,
450 (struct sockaddr *)&from,
451 (void *)&fromlen);
452 if(is_verbose())
453 fprintf(stderr, "\n--- Got UDP ");
454 log_message(LOG_FILE, "\n--- Got UDP ");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000455
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000456 if (r < 12 || r > 512) {
457 bb_error_msg("invalid packet size");
458 continue;
459 }
460 if (r > 0) {
461 r = process_packet(buf);
462 if (r > 0)
463 sendto(udps, buf,
464 r, 0, (struct sockaddr *)&from,
465 fromlen);
466 }
467 } // end if
468 } // end while
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000469 return 0;
470}
471
472