blob: 433b124430c696ba4b7b09e679a22686116e21be [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
30#define MAX_HOST_LEN 16 // longest host name allowed is 15
31#define IP_STRING_LEN 18 // .xxx.xxx.xxx.xxx\0
32
33//must be strlen('.in-addr.arpa') larger than IP_STRING_LEN
34static const int MAX_NAME_LEN = (IP_STRING_LEN + 13);
35
36/* Cannot get bigger packets than 512 per RFC1035
37 In practice this can be set considerably smaller:
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000038 Length of response packet is header (12B) + 2*type(4B) + 2*class(4B) +
39 ttl(4B) + rlen(2B) + r (MAX_NAME_LEN =21B) +
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000040 2*querystring (2 MAX_NAME_LEN= 42B), all together 90 Byte
41*/
42static const int MAX_PACK_LEN = 512 + 1;
43
44#define DEFAULT_TTL 30; // increase this when not testing?
45
46static const int REQ_A = 1;
47static const int REQ_PTR = 12;
48
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000049struct dns_repl { // resource record, add 0 or 1 to accepted dns_msg in resp
50 uint16_t rlen;
51 uint8_t *r; // resource
52 uint16_t flags;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000053};
54
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000055struct dns_head { // the message from client and first part of response mag
56 uint16_t id;
57 uint16_t flags;
58 uint16_t nquer; // accepts 0
59 uint16_t nansw; // 1 in response
60 uint16_t nauth; // 0
61 uint16_t nadd; // 0
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000062};
63struct dns_prop {
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000064 uint16_t type;
65 uint16_t class;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000066};
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000067struct dns_entry { // element of known name, ip address and reversed ip address
68 struct dns_entry *next;
69 char ip[IP_STRING_LEN]; // dotted decimal IP
70 char rip[IP_STRING_LEN]; // length decimal reversed IP
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000071 char name[MAX_HOST_LEN];
72};
73
74static struct dns_entry *dnsentry = NULL;
75static int daemonmode = 0;
76static uint32_t ttl = DEFAULT_TTL;
77
78/*
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000079 * Convert host name from C-string to dns length/string.
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000080 */
81static void
82convname(char *a, uint8_t *q)
83{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000084 int i = (q[0] == '.') ? 0 : 1;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000085 for(; i < MAX_HOST_LEN-1 && *q; i++, q++)
86 a[i] = tolower(*q);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000087 a[0] = i - 1;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000088 a[i] = 0;
89}
90
91/*
92 * Insert length of substrings insetad of dots
93 */
94static void
95undot(uint8_t * rip)
96{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000097 int i = 0, s = 0;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000098 while(rip[i]) i++;
99 for(--i; i >= 0; i--) {
100 if(rip[i] == '.') {
101 rip[i] = s;
102 s = 0;
103 } else s++;
104 }
105}
106
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000107/*
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000108 * Append message to log file
109 */
110static void
111log_message(char *filename, char *message)
112{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000113 FILE *logfile;
114 if (!daemonmode)
115 return;
116 logfile = fopen(filename, "a");
117 if (!logfile)
118 return;
119 fprintf(logfile, "%s\n", message);
120 fclose(logfile);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000121}
122
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000123/*
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000124 * Read one line of hostname/IP from file
125 * Returns 0 for each valid entry read, -1 at EOF
126 * Assumes all host names are lower case only
127 * Hostnames with more than one label is not handled correctly.
128 * Presently the dot is copied into name without
129 * converting to a length/string substring for that label.
130 */
131
132static int
133getfileentry(FILE * fp, struct dns_entry *s, int verb)
134{
135 unsigned int a,b,c,d;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000136 char *r, *name;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000137
138restart:
139 if(!(r = bb_get_line_from_file(fp)))
140 return -1;
141 while(*r == ' ' || *r == '\t') {
142 r++;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000143 if(!*r || *r == '#' || *r == '\n')
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000144 goto restart; /* skipping empty/blank and commented lines */
145 }
146 name = r;
147 while(*r != ' ' && *r != '\t')
148 r++;
149 *r++ = 0;
150 if(sscanf(r,"%u.%u.%u.%u",&a,&b,&c,&d) != 4)
151 goto restart; /* skipping wrong lines */
152
153 sprintf(s->ip,"%u.%u.%u.%u",a,b,c,d);
154 sprintf(s->rip,".%u.%u.%u.%u",d,c,b,a);
155 undot((uint8_t*)s->rip);
156 convname(s->name,(uint8_t*)name);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000157
158 if(verb)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000159 fprintf(stderr,"\tname:%s, ip:%s\n",&(s->name[1]),s->ip);
160
161 return 0; /* warningkiller */
162}
163
164/*
165 * Read hostname/IP records from file
166 */
167static void
168dnsentryinit(int verb)
169{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000170 FILE *fp;
171 struct dns_entry *m, *prev;
172 prev = dnsentry = NULL;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000173
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000174 if(!(fp = fopen(fileconf, "r")))
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000175 bb_perror_msg_and_die("open %s",fileconf);
176
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000177 while (1) {
178 if(!(m = (struct dns_entry *)malloc(sizeof(struct dns_entry))))
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000179 bb_perror_msg_and_die("malloc dns_entry");
180
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000181 m->next = NULL;
182 if (getfileentry(fp, m, verb))
183 break;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000184
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000185 if (prev == NULL)
186 dnsentry = m;
187 else
188 prev->next = m;
189 prev = m;
190 }
191 fclose(fp);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000192}
193
194
195/*
196 * Set up UDP socket
197 */
198static int
199listen_socket(char *iface_addr, int listen_port)
200{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000201 struct sockaddr_in a;
202 char msg[100];
203 int s;
204 int yes = 1;
205 if ((s = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
206 bb_perror_msg_and_die("socket() failed");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000207#ifdef SO_REUSEADDR
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000208 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&yes, sizeof(yes)) < 0)
209 bb_perror_msg_and_die("setsockopt() failed");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000210#endif
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000211 memset(&a, 0, sizeof(a));
212 a.sin_port = htons(listen_port);
213 a.sin_family = AF_INET;
214 if (!inet_aton(iface_addr, &a.sin_addr))
215 bb_perror_msg_and_die("bad iface address");
216 if (bind(s, (struct sockaddr *)&a, sizeof(a)) < 0)
217 bb_perror_msg_and_die("bind() failed");
218 listen(s, 50);
219 sprintf(msg, "accepting UDP packets on addr:port %s:%d\n",
220 iface_addr, (int)listen_port);
221 log_message(LOG_FILE, msg);
222 return s;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000223}
224
225/*
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000226 * Look query up in dns records and return answer if found
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000227 * qs is the query string, first byte the string length
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000228 */
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000229static int
230table_lookup(uint16_t type, uint8_t * as, uint8_t * qs)
231{
232 int i;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000233 struct dns_entry *d=dnsentry;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000234
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000235 do {
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000236#ifdef DEBUG
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000237 char *p,*q;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000238 q = (char *)&(qs[1]);
239 p = &(d->name[1]);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000240 fprintf(stderr, "\ntest: %d <%s> <%s> %d", strlen(p), p, q, strlen(q));
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000241#endif
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000242 if (type == REQ_A) { /* search by host name */
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000243 for(i = 1; i <= (int)(d->name[0]); i++)
244 if(tolower(qs[i]) != d->name[i])
245 continue;
246#ifdef DEBUG
247 fprintf(stderr, " OK");
248#endif
249 strcpy((char *)as, d->ip);
250#ifdef DEBUG
251 fprintf(stderr, " %s ", as);
252#endif
253 return 0;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000254 }
255 else if (type == REQ_PTR) { /* search by IP-address */
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000256 if (!strncmp((char*)&d->rip[1], (char*)&qs[1], strlen(d->rip)-1)) {
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000257 strcpy((char *)as, d->name);
258 return 0;
259 }
260 }
261 } while ((d = d->next) != NULL);
262 return -1;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000263}
264
265
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000266/*
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000267 * Decode message and generate answer
268 */
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000269#define eret(s) do { fprintf (stderr, "%s\n", s); return -1; } while (0)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000270static int
271process_packet(uint8_t * buf)
272{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000273 struct dns_head *head;
274 struct dns_prop *qprop;
275 struct dns_repl outr;
276 void *next, *from, *answb;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000277
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000278 uint8_t answstr[MAX_NAME_LEN + 1];
279 int lookup_result, type, len, packet_len;
280 uint16_t flags;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000281
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000282 answstr[0] = '\0';
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000283
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000284 head = (struct dns_head *)buf;
285 if (head->nquer == 0)
286 eret("no queries");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000287
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000288 if ((head->flags & 0x8000))
289 eret("ignoring response packet");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000290
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000291 from = (void *)&head[1]; // start of query string
292 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 +0000293
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000294 outr.rlen = 0; // may change later
295 outr.r = NULL;
296 outr.flags = 0;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000297
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000298 qprop = (struct dns_prop *)(answb - 4);
299 type = ntohs(qprop->type);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000300
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000301 // only let REQ_A and REQ_PTR pass
302 if (!(type == REQ_A || type == REQ_PTR)) {
303 goto empty_packet; /* we can't handle the query type */
304 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000305
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000306 if (ntohs(qprop->class) != 1 /* class INET */ ) {
307 outr.flags = 4; /* not supported */
308 goto empty_packet;
309 }
310 /* we only support standard queries */
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000311
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000312 if ((ntohs(head->flags) & 0x7800) != 0)
313 goto empty_packet;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000314
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000315 // We have a standard query
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000316
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000317 log_message(LOG_FILE, (char *)head);
318 lookup_result = table_lookup(type, answstr, (uint8_t*)(&head[1]));
319 if (lookup_result != 0) {
320 outr.flags = 3 | 0x0400; //name do not exist and auth
321 goto empty_packet;
322 }
323 if (type == REQ_A) { // return an address
324 struct in_addr a;
325 if (!inet_aton((char*)answstr, &a)) {//dotted dec to long conv
326 outr.flags = 1; /* Frmt err */
327 goto empty_packet;
328 }
329 memcpy(answstr, &a.s_addr, 4); // save before a disappears
330 outr.rlen = 4; // uint32_t IP
331 }
332 else
333 outr.rlen = strlen((char *)answstr) + 1; // a host name
334 outr.r = answstr; // 32 bit ip or a host name
335 outr.flags |= 0x0400; /* authority-bit */
336 // we have an answer
337 head->nansw = htons(1);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000338
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000339 // copy query block to answer block
340 len = answb - from;
341 memcpy(answb, from, len);
342 next += len;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000343
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000344 // and append answer rr
345 *(uint32_t *) next = htonl(ttl);
346 next += 4;
347 *(uint16_t *) next = htons(outr.rlen);
348 next += 2;
349 memcpy(next, (void *)answstr, outr.rlen);
350 next += outr.rlen;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000351
352 empty_packet:
353
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000354 flags = ntohs(head->flags);
355 // clear rcode and RA, set responsebit and our new flags
356 flags |= (outr.flags & 0xff80) | 0x8000;
357 head->flags = htons(flags);
358 head->nauth = head->nadd = htons(0);
359 head->nquer = htons(1);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000360
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000361 packet_len = next - (void *)buf;
362 return packet_len;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000363}
364
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000365/*
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000366 * Exit on signal
367 */
368static void
369interrupt(int x)
370{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000371 unlink(LOCK_FILE);
372 write(2, "interrupt exiting\n", 18);
373 exit(2);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000374}
375
376#define is_daemon() (flags&16)
377#define is_verbose() (flags&32)
378//#define DEBUG 1
379
380int dnsd_main(int argc, char **argv)
381{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000382 int udps;
383 uint16_t port = 53;
384 uint8_t buf[MAX_PACK_LEN];
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000385 unsigned long flags = 0;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000386 char *listen_interface = "0.0.0.0";
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000387 char *sttl=NULL, *sport=NULL;
388
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000389 if(argc > 1)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000390 flags = bb_getopt_ulflags(argc, argv, "i:c:t:p:dv", &listen_interface, &fileconf, &sttl, &sport);
391 if(sttl)
392 if(!(ttl = atol(sttl)))
393 bb_show_usage();
394 if(sport)
395 if(!(port = atol(sport)))
396 bb_show_usage();
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000397
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000398 if(is_verbose()) {
399 fprintf(stderr,"listen_interface: %s\n", listen_interface);
400 fprintf(stderr,"ttl: %d, port: %d\n", ttl, port);
401 fprintf(stderr,"fileconf: %s\n", fileconf);
402 }
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000403
404 if(is_daemon())
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000405#if defined(__uClinux__)
406 /* reexec for vfork() do continue parent */
407 vfork_daemon_rexec(1, 0, argc, argv, "-d");
408#else /* uClinux */
409 if (daemon(1, 0) < 0) {
410 bb_perror_msg_and_die("daemon");
411 }
412#endif /* uClinuvx */
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000413
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000414 dnsentryinit(is_verbose());
415
416 signal(SIGINT, interrupt);
417 signal(SIGPIPE, SIG_IGN);
418 signal(SIGHUP, SIG_IGN);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000419#ifdef SIGTSTP
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000420 signal(SIGTSTP, SIG_IGN);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000421#endif
422#ifdef SIGURG
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000423 signal(SIGURG, SIG_IGN);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000424#endif
425
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000426 udps = listen_socket(listen_interface, port);
427 if (udps < 0)
428 exit(1);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000429
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000430 while (1) {
431 fd_set fdset;
432 int r;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000433
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000434 FD_ZERO(&fdset);
435 FD_SET(udps, &fdset);
436 // Block until a message arrives
437 if((r = select(udps + 1, &fdset, NULL, NULL, NULL)) < 0)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000438 bb_perror_msg_and_die("select error");
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000439 else
440 if(r == 0)
441 bb_perror_msg_and_die("select spurious return");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000442
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000443 /* Can this test ever be false? */
444 if (FD_ISSET(udps, &fdset)) {
445 struct sockaddr_in from;
446 int fromlen = sizeof(from);
447 r = recvfrom(udps, buf, sizeof(buf), 0,
448 (struct sockaddr *)&from,
449 (void *)&fromlen);
450 if(is_verbose())
451 fprintf(stderr, "\n--- Got UDP ");
452 log_message(LOG_FILE, "\n--- Got UDP ");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000453
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000454 if (r < 12 || r > 512) {
455 bb_error_msg("invalid packet size");
456 continue;
457 }
458 if (r > 0) {
459 r = process_packet(buf);
460 if (r > 0)
461 sendto(udps, buf,
462 r, 0, (struct sockaddr *)&from,
463 fromlen);
464 }
465 } // end if
466 } // end while
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000467 return 0;
468}
469
470