blob: b0e74d770c88b2e1416e76ea1cacc23f1020bb78 [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
22static char *fileconf = "/etc/dnsd.conf";
23#define LOCK_FILE "/var/run/dnsd.lock"
24#define LOG_FILE "/var/log/dnsd.log"
25
Rob Landleybc68cd12006-03-10 19:22:06 +000026enum {
27 MAX_HOST_LEN = 16, // longest host name allowed is 15
28 IP_STRING_LEN = 18, // .xxx.xxx.xxx.xxx\0
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000029
30//must be strlen('.in-addr.arpa') larger than IP_STRING_LEN
Rob Landleybc68cd12006-03-10 19:22:06 +000031 MAX_NAME_LEN = (IP_STRING_LEN + 13),
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000032
33/* Cannot get bigger packets than 512 per RFC1035
34 In practice this can be set considerably smaller:
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000035 Length of response packet is header (12B) + 2*type(4B) + 2*class(4B) +
36 ttl(4B) + rlen(2B) + r (MAX_NAME_LEN =21B) +
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000037 2*querystring (2 MAX_NAME_LEN= 42B), all together 90 Byte
38*/
Rob Landleybc68cd12006-03-10 19:22:06 +000039 MAX_PACK_LEN = 512 + 1,
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000040
Rob Landleybc68cd12006-03-10 19:22:06 +000041 DEFAULT_TTL = 30, // increase this when not testing?
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000042
Rob Landleybc68cd12006-03-10 19:22:06 +000043 REQ_A = 1,
44 REQ_PTR = 12
45};
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000046
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000047struct dns_repl { // resource record, add 0 or 1 to accepted dns_msg in resp
48 uint16_t rlen;
49 uint8_t *r; // resource
50 uint16_t flags;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000051};
52
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000053struct dns_head { // the message from client and first part of response mag
54 uint16_t id;
55 uint16_t flags;
56 uint16_t nquer; // accepts 0
57 uint16_t nansw; // 1 in response
58 uint16_t nauth; // 0
59 uint16_t nadd; // 0
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000060};
61struct dns_prop {
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000062 uint16_t type;
63 uint16_t class;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000064};
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000065struct dns_entry { // element of known name, ip address and reversed ip address
66 struct dns_entry *next;
67 char ip[IP_STRING_LEN]; // dotted decimal IP
68 char rip[IP_STRING_LEN]; // length decimal reversed IP
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000069 char name[MAX_HOST_LEN];
70};
71
72static struct dns_entry *dnsentry = NULL;
73static int daemonmode = 0;
74static uint32_t ttl = DEFAULT_TTL;
75
76/*
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000077 * Convert host name from C-string to dns length/string.
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000078 */
Rob Landleya6e131d2006-05-29 06:43:55 +000079static void convname(char *a, uint8_t *q)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000080{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000081 int i = (q[0] == '.') ? 0 : 1;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000082 for(; i < MAX_HOST_LEN-1 && *q; i++, q++)
83 a[i] = tolower(*q);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000084 a[0] = i - 1;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000085 a[i] = 0;
86}
87
88/*
Bernhard Reutner-Fischerc418d482006-05-31 10:19:51 +000089 * Insert length of substrings instead of dots
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000090 */
Rob Landleya6e131d2006-05-29 06:43:55 +000091static void undot(uint8_t * rip)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000092{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000093 int i = 0, s = 0;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000094 while(rip[i]) i++;
95 for(--i; i >= 0; i--) {
96 if(rip[i] == '.') {
97 rip[i] = s;
98 s = 0;
99 } else s++;
100 }
101}
102
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000103/*
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000104 * Append message to log file
105 */
Rob Landleya6e131d2006-05-29 06:43:55 +0000106static void log_message(char *filename, char *message)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000107{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000108 FILE *logfile;
109 if (!daemonmode)
110 return;
111 logfile = fopen(filename, "a");
112 if (!logfile)
113 return;
114 fprintf(logfile, "%s\n", message);
115 fclose(logfile);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000116}
117
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000118/*
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000119 * Read one line of hostname/IP from file
120 * Returns 0 for each valid entry read, -1 at EOF
121 * Assumes all host names are lower case only
122 * Hostnames with more than one label is not handled correctly.
123 * Presently the dot is copied into name without
124 * converting to a length/string substring for that label.
125 */
126
Rob Landleya6e131d2006-05-29 06:43:55 +0000127static int getfileentry(FILE * fp, struct dns_entry *s, int verb)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000128{
129 unsigned int a,b,c,d;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000130 char *r, *name;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000131
132restart:
133 if(!(r = bb_get_line_from_file(fp)))
134 return -1;
135 while(*r == ' ' || *r == '\t') {
136 r++;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000137 if(!*r || *r == '#' || *r == '\n')
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000138 goto restart; /* skipping empty/blank and commented lines */
139 }
140 name = r;
141 while(*r != ' ' && *r != '\t')
142 r++;
143 *r++ = 0;
144 if(sscanf(r,"%u.%u.%u.%u",&a,&b,&c,&d) != 4)
145 goto restart; /* skipping wrong lines */
146
147 sprintf(s->ip,"%u.%u.%u.%u",a,b,c,d);
148 sprintf(s->rip,".%u.%u.%u.%u",d,c,b,a);
149 undot((uint8_t*)s->rip);
150 convname(s->name,(uint8_t*)name);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000151
152 if(verb)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000153 fprintf(stderr,"\tname:%s, ip:%s\n",&(s->name[1]),s->ip);
154
155 return 0; /* warningkiller */
156}
157
158/*
159 * Read hostname/IP records from file
160 */
Rob Landleya6e131d2006-05-29 06:43:55 +0000161static void dnsentryinit(int verb)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000162{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000163 FILE *fp;
164 struct dns_entry *m, *prev;
165 prev = dnsentry = NULL;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000166
Rob Landley86b4d642006-08-03 17:58:17 +0000167 fp = xfopen(fileconf, "r");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000168
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000169 while (1) {
Rob Landleya6e131d2006-05-29 06:43:55 +0000170 m = xmalloc(sizeof(struct dns_entry));
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000171
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000172 m->next = NULL;
173 if (getfileentry(fp, m, verb))
174 break;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000175
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000176 if (prev == NULL)
177 dnsentry = m;
178 else
179 prev->next = m;
180 prev = m;
181 }
182 fclose(fp);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000183}
184
185
186/*
187 * Set up UDP socket
188 */
Rob Landleya6e131d2006-05-29 06:43:55 +0000189static int listen_socket(char *iface_addr, int listen_port)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000190{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000191 struct sockaddr_in a;
192 char msg[100];
193 int s;
194 int yes = 1;
Rob Landley86b4d642006-08-03 17:58:17 +0000195 s = xsocket(PF_INET, SOCK_DGRAM, 0);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000196#ifdef SO_REUSEADDR
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000197 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&yes, sizeof(yes)) < 0)
198 bb_perror_msg_and_die("setsockopt() failed");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000199#endif
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000200 memset(&a, 0, sizeof(a));
201 a.sin_port = htons(listen_port);
202 a.sin_family = AF_INET;
203 if (!inet_aton(iface_addr, &a.sin_addr))
204 bb_perror_msg_and_die("bad iface address");
Rob Landley86b4d642006-08-03 17:58:17 +0000205 xbind(s, (struct sockaddr *)&a, sizeof(a));
Rob Landley4640b052006-08-15 21:37:55 +0000206 xlisten(s, 50);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000207 sprintf(msg, "accepting UDP packets on addr:port %s:%d\n",
208 iface_addr, (int)listen_port);
209 log_message(LOG_FILE, msg);
210 return s;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000211}
212
213/*
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000214 * Look query up in dns records and return answer if found
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000215 * qs is the query string, first byte the string length
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000216 */
Rob Landleya6e131d2006-05-29 06:43:55 +0000217static int table_lookup(uint16_t type, uint8_t * as, uint8_t * qs)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000218{
219 int i;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000220 struct dns_entry *d=dnsentry;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000221
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000222 do {
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000223#ifdef DEBUG
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000224 char *p,*q;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000225 q = (char *)&(qs[1]);
226 p = &(d->name[1]);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000227 fprintf(stderr, "\ntest: %d <%s> <%s> %d", strlen(p), p, q, strlen(q));
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000228#endif
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000229 if (type == REQ_A) { /* search by host name */
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000230 for(i = 1; i <= (int)(d->name[0]); i++)
231 if(tolower(qs[i]) != d->name[i])
232 continue;
233#ifdef DEBUG
234 fprintf(stderr, " OK");
235#endif
236 strcpy((char *)as, d->ip);
237#ifdef DEBUG
238 fprintf(stderr, " %s ", as);
239#endif
240 return 0;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000241 }
242 else if (type == REQ_PTR) { /* search by IP-address */
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000243 if (!strncmp((char*)&d->rip[1], (char*)&qs[1], strlen(d->rip)-1)) {
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000244 strcpy((char *)as, d->name);
245 return 0;
246 }
247 }
248 } while ((d = d->next) != NULL);
249 return -1;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000250}
251
252
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000253/*
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000254 * Decode message and generate answer
255 */
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000256#define eret(s) do { fprintf (stderr, "%s\n", s); return -1; } while (0)
Rob Landleya6e131d2006-05-29 06:43:55 +0000257static int process_packet(uint8_t * buf)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000258{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000259 struct dns_head *head;
260 struct dns_prop *qprop;
261 struct dns_repl outr;
262 void *next, *from, *answb;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000263
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000264 uint8_t answstr[MAX_NAME_LEN + 1];
265 int lookup_result, type, len, packet_len;
266 uint16_t flags;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000267
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000268 answstr[0] = '\0';
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000269
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000270 head = (struct dns_head *)buf;
271 if (head->nquer == 0)
272 eret("no queries");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000273
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000274 if ((head->flags & 0x8000))
275 eret("ignoring response packet");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000276
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000277 from = (void *)&head[1]; // start of query string
278 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 +0000279
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000280 outr.rlen = 0; // may change later
281 outr.r = NULL;
282 outr.flags = 0;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000283
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000284 qprop = (struct dns_prop *)(answb - 4);
285 type = ntohs(qprop->type);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000286
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000287 // only let REQ_A and REQ_PTR pass
288 if (!(type == REQ_A || type == REQ_PTR)) {
289 goto empty_packet; /* we can't handle the query type */
290 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000291
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000292 if (ntohs(qprop->class) != 1 /* class INET */ ) {
293 outr.flags = 4; /* not supported */
294 goto empty_packet;
295 }
296 /* we only support standard queries */
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000297
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000298 if ((ntohs(head->flags) & 0x7800) != 0)
299 goto empty_packet;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000300
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000301 // We have a standard query
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000302
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000303 log_message(LOG_FILE, (char *)head);
304 lookup_result = table_lookup(type, answstr, (uint8_t*)(&head[1]));
305 if (lookup_result != 0) {
306 outr.flags = 3 | 0x0400; //name do not exist and auth
307 goto empty_packet;
308 }
309 if (type == REQ_A) { // return an address
310 struct in_addr a;
311 if (!inet_aton((char*)answstr, &a)) {//dotted dec to long conv
312 outr.flags = 1; /* Frmt err */
313 goto empty_packet;
314 }
315 memcpy(answstr, &a.s_addr, 4); // save before a disappears
316 outr.rlen = 4; // uint32_t IP
317 }
318 else
319 outr.rlen = strlen((char *)answstr) + 1; // a host name
320 outr.r = answstr; // 32 bit ip or a host name
321 outr.flags |= 0x0400; /* authority-bit */
322 // we have an answer
323 head->nansw = htons(1);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000324
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000325 // copy query block to answer block
326 len = answb - from;
327 memcpy(answb, from, len);
328 next += len;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000329
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000330 // and append answer rr
331 *(uint32_t *) next = htonl(ttl);
332 next += 4;
333 *(uint16_t *) next = htons(outr.rlen);
334 next += 2;
335 memcpy(next, (void *)answstr, outr.rlen);
336 next += outr.rlen;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000337
338 empty_packet:
339
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000340 flags = ntohs(head->flags);
341 // clear rcode and RA, set responsebit and our new flags
342 flags |= (outr.flags & 0xff80) | 0x8000;
343 head->flags = htons(flags);
344 head->nauth = head->nadd = htons(0);
345 head->nquer = htons(1);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000346
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000347 packet_len = next - (void *)buf;
348 return packet_len;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000349}
350
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000351/*
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000352 * Exit on signal
353 */
Rob Landleya6e131d2006-05-29 06:43:55 +0000354static void interrupt(int x)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000355{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000356 unlink(LOCK_FILE);
357 write(2, "interrupt exiting\n", 18);
358 exit(2);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000359}
360
361#define is_daemon() (flags&16)
362#define is_verbose() (flags&32)
363//#define DEBUG 1
364
365int dnsd_main(int argc, char **argv)
366{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000367 int udps;
368 uint16_t port = 53;
369 uint8_t buf[MAX_PACK_LEN];
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000370 unsigned long flags = 0;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000371 char *listen_interface = "0.0.0.0";
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000372 char *sttl=NULL, *sport=NULL;
373
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000374 if(argc > 1)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000375 flags = bb_getopt_ulflags(argc, argv, "i:c:t:p:dv", &listen_interface, &fileconf, &sttl, &sport);
376 if(sttl)
377 if(!(ttl = atol(sttl)))
378 bb_show_usage();
379 if(sport)
380 if(!(port = atol(sport)))
381 bb_show_usage();
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000382
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000383 if(is_verbose()) {
384 fprintf(stderr,"listen_interface: %s\n", listen_interface);
385 fprintf(stderr,"ttl: %d, port: %d\n", ttl, port);
386 fprintf(stderr,"fileconf: %s\n", fileconf);
387 }
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000388
389 if(is_daemon())
Bernhard Reutner-Fischerc418d482006-05-31 10:19:51 +0000390#ifdef BB_NOMMU
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000391 /* reexec for vfork() do continue parent */
392 vfork_daemon_rexec(1, 0, argc, argv, "-d");
Bernhard Reutner-Fischerc418d482006-05-31 10:19:51 +0000393#else
Rob Landley86b4d642006-08-03 17:58:17 +0000394 xdaemon(1, 0);
Bernhard Reutner-Fischerc418d482006-05-31 10:19:51 +0000395#endif
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000396
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000397 dnsentryinit(is_verbose());
398
399 signal(SIGINT, interrupt);
400 signal(SIGPIPE, SIG_IGN);
401 signal(SIGHUP, SIG_IGN);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000402#ifdef SIGTSTP
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000403 signal(SIGTSTP, SIG_IGN);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000404#endif
405#ifdef SIGURG
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000406 signal(SIGURG, SIG_IGN);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000407#endif
408
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000409 udps = listen_socket(listen_interface, port);
410 if (udps < 0)
411 exit(1);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000412
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000413 while (1) {
414 fd_set fdset;
415 int r;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000416
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000417 FD_ZERO(&fdset);
418 FD_SET(udps, &fdset);
419 // Block until a message arrives
420 if((r = select(udps + 1, &fdset, NULL, NULL, NULL)) < 0)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000421 bb_perror_msg_and_die("select error");
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000422 else
423 if(r == 0)
424 bb_perror_msg_and_die("select spurious return");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000425
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000426 /* Can this test ever be false? */
427 if (FD_ISSET(udps, &fdset)) {
428 struct sockaddr_in from;
429 int fromlen = sizeof(from);
430 r = recvfrom(udps, buf, sizeof(buf), 0,
431 (struct sockaddr *)&from,
432 (void *)&fromlen);
433 if(is_verbose())
434 fprintf(stderr, "\n--- Got UDP ");
435 log_message(LOG_FILE, "\n--- Got UDP ");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000436
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000437 if (r < 12 || r > 512) {
438 bb_error_msg("invalid packet size");
439 continue;
440 }
441 if (r > 0) {
442 r = process_packet(buf);
443 if (r > 0)
444 sendto(udps, buf,
445 r, 0, (struct sockaddr *)&from,
446 fromlen);
447 }
448 } // end if
449 } // end while
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000450 return 0;
451}
452
453