blob: f8bd07e1088218c211da959e653f9254d42e0cba [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
20#include <unistd.h>
21#include <string.h>
22#include <signal.h>
23#include <arpa/inet.h>
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000024#include <sys/socket.h>
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000025#include <ctype.h>
Bernhard Reutner-Fischer421d9e52006-04-03 16:39:31 +000026#include "busybox.h"
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000027
28static char *fileconf = "/etc/dnsd.conf";
29#define LOCK_FILE "/var/run/dnsd.lock"
30#define LOG_FILE "/var/log/dnsd.log"
31
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;
79static int daemonmode = 0;
80static uint32_t ttl = DEFAULT_TTL;
81
82/*
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000083 * Convert host name from C-string to dns length/string.
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000084 */
Rob Landleya6e131d2006-05-29 06:43:55 +000085static void convname(char *a, uint8_t *q)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000086{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000087 int i = (q[0] == '.') ? 0 : 1;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000088 for(; i < MAX_HOST_LEN-1 && *q; i++, q++)
89 a[i] = tolower(*q);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000090 a[0] = i - 1;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000091 a[i] = 0;
92}
93
94/*
Bernhard Reutner-Fischerc418d482006-05-31 10:19:51 +000095 * Insert length of substrings instead of dots
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000096 */
Rob Landleya6e131d2006-05-29 06:43:55 +000097static void undot(uint8_t * rip)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000098{
"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 */
Rob Landleya6e131d2006-05-29 06:43:55 +0000112static void log_message(char *filename, char *message)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000113{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000114 FILE *logfile;
115 if (!daemonmode)
116 return;
117 logfile = fopen(filename, "a");
118 if (!logfile)
119 return;
120 fprintf(logfile, "%s\n", message);
121 fclose(logfile);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000122}
123
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000124/*
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000125 * Read one line of hostname/IP from file
126 * Returns 0 for each valid entry read, -1 at EOF
127 * Assumes all host names are lower case only
128 * Hostnames with more than one label is not handled correctly.
129 * Presently the dot is copied into name without
130 * converting to a length/string substring for that label.
131 */
132
Rob Landleya6e131d2006-05-29 06:43:55 +0000133static int getfileentry(FILE * fp, struct dns_entry *s, int verb)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000134{
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 */
Rob Landleya6e131d2006-05-29 06:43:55 +0000167static void dnsentryinit(int verb)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000168{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000169 FILE *fp;
170 struct dns_entry *m, *prev;
171 prev = dnsentry = NULL;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000172
Rob Landleya6e131d2006-05-29 06:43:55 +0000173 fp = bb_xfopen(fileconf, "r");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000174
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000175 while (1) {
Rob Landleya6e131d2006-05-29 06:43:55 +0000176 m = xmalloc(sizeof(struct dns_entry));
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000177
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000178 m->next = NULL;
179 if (getfileentry(fp, m, verb))
180 break;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000181
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000182 if (prev == NULL)
183 dnsentry = m;
184 else
185 prev->next = m;
186 prev = m;
187 }
188 fclose(fp);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000189}
190
191
192/*
193 * Set up UDP socket
194 */
Rob Landleya6e131d2006-05-29 06:43:55 +0000195static int listen_socket(char *iface_addr, int listen_port)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000196{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000197 struct sockaddr_in a;
198 char msg[100];
199 int s;
200 int yes = 1;
Bernhard Reutner-Fischerdac7ff12006-04-12 17:55:51 +0000201 s = bb_xsocket(PF_INET, SOCK_DGRAM, 0);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000202#ifdef SO_REUSEADDR
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000203 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&yes, sizeof(yes)) < 0)
204 bb_perror_msg_and_die("setsockopt() failed");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000205#endif
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000206 memset(&a, 0, sizeof(a));
207 a.sin_port = htons(listen_port);
208 a.sin_family = AF_INET;
209 if (!inet_aton(iface_addr, &a.sin_addr))
210 bb_perror_msg_and_die("bad iface address");
Bernhard Reutner-Fischer67f641e2006-04-12 18:24:37 +0000211 bb_xbind(s, (struct sockaddr *)&a, sizeof(a));
212 listen(s, 50); /* bb_xlisten? */
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000213 sprintf(msg, "accepting UDP packets on addr:port %s:%d\n",
214 iface_addr, (int)listen_port);
215 log_message(LOG_FILE, msg);
216 return s;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000217}
218
219/*
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000220 * Look query up in dns records and return answer if found
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000221 * qs is the query string, first byte the string length
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000222 */
Rob Landleya6e131d2006-05-29 06:43:55 +0000223static int table_lookup(uint16_t type, uint8_t * as, uint8_t * qs)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000224{
225 int i;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000226 struct dns_entry *d=dnsentry;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000227
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000228 do {
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000229#ifdef DEBUG
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000230 char *p,*q;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000231 q = (char *)&(qs[1]);
232 p = &(d->name[1]);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000233 fprintf(stderr, "\ntest: %d <%s> <%s> %d", strlen(p), p, q, strlen(q));
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000234#endif
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000235 if (type == REQ_A) { /* search by host name */
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000236 for(i = 1; i <= (int)(d->name[0]); i++)
237 if(tolower(qs[i]) != d->name[i])
238 continue;
239#ifdef DEBUG
240 fprintf(stderr, " OK");
241#endif
242 strcpy((char *)as, d->ip);
243#ifdef DEBUG
244 fprintf(stderr, " %s ", as);
245#endif
246 return 0;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000247 }
248 else if (type == REQ_PTR) { /* search by IP-address */
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000249 if (!strncmp((char*)&d->rip[1], (char*)&qs[1], strlen(d->rip)-1)) {
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000250 strcpy((char *)as, d->name);
251 return 0;
252 }
253 }
254 } while ((d = d->next) != NULL);
255 return -1;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000256}
257
258
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000259/*
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000260 * Decode message and generate answer
261 */
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000262#define eret(s) do { fprintf (stderr, "%s\n", s); return -1; } while (0)
Rob Landleya6e131d2006-05-29 06:43:55 +0000263static int process_packet(uint8_t * buf)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000264{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000265 struct dns_head *head;
266 struct dns_prop *qprop;
267 struct dns_repl outr;
268 void *next, *from, *answb;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000269
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000270 uint8_t answstr[MAX_NAME_LEN + 1];
271 int lookup_result, type, len, packet_len;
272 uint16_t flags;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000273
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000274 answstr[0] = '\0';
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000275
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000276 head = (struct dns_head *)buf;
277 if (head->nquer == 0)
278 eret("no queries");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000279
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000280 if ((head->flags & 0x8000))
281 eret("ignoring response packet");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000282
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000283 from = (void *)&head[1]; // start of query string
284 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 +0000285
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000286 outr.rlen = 0; // may change later
287 outr.r = NULL;
288 outr.flags = 0;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000289
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000290 qprop = (struct dns_prop *)(answb - 4);
291 type = ntohs(qprop->type);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000292
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000293 // only let REQ_A and REQ_PTR pass
294 if (!(type == REQ_A || type == REQ_PTR)) {
295 goto empty_packet; /* we can't handle the query type */
296 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000297
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000298 if (ntohs(qprop->class) != 1 /* class INET */ ) {
299 outr.flags = 4; /* not supported */
300 goto empty_packet;
301 }
302 /* we only support standard queries */
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000303
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000304 if ((ntohs(head->flags) & 0x7800) != 0)
305 goto empty_packet;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000306
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000307 // We have a standard query
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000308
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000309 log_message(LOG_FILE, (char *)head);
310 lookup_result = table_lookup(type, answstr, (uint8_t*)(&head[1]));
311 if (lookup_result != 0) {
312 outr.flags = 3 | 0x0400; //name do not exist and auth
313 goto empty_packet;
314 }
315 if (type == REQ_A) { // return an address
316 struct in_addr a;
317 if (!inet_aton((char*)answstr, &a)) {//dotted dec to long conv
318 outr.flags = 1; /* Frmt err */
319 goto empty_packet;
320 }
321 memcpy(answstr, &a.s_addr, 4); // save before a disappears
322 outr.rlen = 4; // uint32_t IP
323 }
324 else
325 outr.rlen = strlen((char *)answstr) + 1; // a host name
326 outr.r = answstr; // 32 bit ip or a host name
327 outr.flags |= 0x0400; /* authority-bit */
328 // we have an answer
329 head->nansw = htons(1);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000330
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000331 // copy query block to answer block
332 len = answb - from;
333 memcpy(answb, from, len);
334 next += len;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000335
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000336 // and append answer rr
337 *(uint32_t *) next = htonl(ttl);
338 next += 4;
339 *(uint16_t *) next = htons(outr.rlen);
340 next += 2;
341 memcpy(next, (void *)answstr, outr.rlen);
342 next += outr.rlen;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000343
344 empty_packet:
345
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000346 flags = ntohs(head->flags);
347 // clear rcode and RA, set responsebit and our new flags
348 flags |= (outr.flags & 0xff80) | 0x8000;
349 head->flags = htons(flags);
350 head->nauth = head->nadd = htons(0);
351 head->nquer = htons(1);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000352
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000353 packet_len = next - (void *)buf;
354 return packet_len;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000355}
356
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000357/*
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000358 * Exit on signal
359 */
Rob Landleya6e131d2006-05-29 06:43:55 +0000360static void interrupt(int x)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000361{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000362 unlink(LOCK_FILE);
363 write(2, "interrupt exiting\n", 18);
364 exit(2);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000365}
366
367#define is_daemon() (flags&16)
368#define is_verbose() (flags&32)
369//#define DEBUG 1
370
371int dnsd_main(int argc, char **argv)
372{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000373 int udps;
374 uint16_t port = 53;
375 uint8_t buf[MAX_PACK_LEN];
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000376 unsigned long flags = 0;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000377 char *listen_interface = "0.0.0.0";
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000378 char *sttl=NULL, *sport=NULL;
379
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000380 if(argc > 1)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000381 flags = bb_getopt_ulflags(argc, argv, "i:c:t:p:dv", &listen_interface, &fileconf, &sttl, &sport);
382 if(sttl)
383 if(!(ttl = atol(sttl)))
384 bb_show_usage();
385 if(sport)
386 if(!(port = atol(sport)))
387 bb_show_usage();
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000388
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000389 if(is_verbose()) {
390 fprintf(stderr,"listen_interface: %s\n", listen_interface);
391 fprintf(stderr,"ttl: %d, port: %d\n", ttl, port);
392 fprintf(stderr,"fileconf: %s\n", fileconf);
393 }
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000394
395 if(is_daemon())
Bernhard Reutner-Fischerc418d482006-05-31 10:19:51 +0000396#ifdef BB_NOMMU
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000397 /* reexec for vfork() do continue parent */
398 vfork_daemon_rexec(1, 0, argc, argv, "-d");
Bernhard Reutner-Fischerc418d482006-05-31 10:19:51 +0000399#else
Bernhard Reutner-Fischer2c998512006-04-12 18:09:26 +0000400 bb_xdaemon(1, 0);
Bernhard Reutner-Fischerc418d482006-05-31 10:19:51 +0000401#endif
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000402
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000403 dnsentryinit(is_verbose());
404
405 signal(SIGINT, interrupt);
406 signal(SIGPIPE, SIG_IGN);
407 signal(SIGHUP, SIG_IGN);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000408#ifdef SIGTSTP
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000409 signal(SIGTSTP, SIG_IGN);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000410#endif
411#ifdef SIGURG
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000412 signal(SIGURG, SIG_IGN);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000413#endif
414
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000415 udps = listen_socket(listen_interface, port);
416 if (udps < 0)
417 exit(1);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000418
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000419 while (1) {
420 fd_set fdset;
421 int r;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000422
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000423 FD_ZERO(&fdset);
424 FD_SET(udps, &fdset);
425 // Block until a message arrives
426 if((r = select(udps + 1, &fdset, NULL, NULL, NULL)) < 0)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000427 bb_perror_msg_and_die("select error");
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000428 else
429 if(r == 0)
430 bb_perror_msg_and_die("select spurious return");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000431
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000432 /* Can this test ever be false? */
433 if (FD_ISSET(udps, &fdset)) {
434 struct sockaddr_in from;
435 int fromlen = sizeof(from);
436 r = recvfrom(udps, buf, sizeof(buf), 0,
437 (struct sockaddr *)&from,
438 (void *)&fromlen);
439 if(is_verbose())
440 fprintf(stderr, "\n--- Got UDP ");
441 log_message(LOG_FILE, "\n--- Got UDP ");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000442
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000443 if (r < 12 || r > 512) {
444 bb_error_msg("invalid packet size");
445 continue;
446 }
447 if (r > 0) {
448 r = process_packet(buf);
449 if (r > 0)
450 sendto(udps, buf,
451 r, 0, (struct sockaddr *)&from,
452 fromlen);
453 }
454 } // end if
455 } // end while
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000456 return 0;
457}
458
459