blob: efb5cfba70a33b1a7bb457dc38f4a260f5de7945 [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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000020#include "libbb.h"
Bernhard Reutner-Fischerf4701962008-01-27 12:50:12 +000021#include <syslog.h>
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000022
Denis Vlasenkoc12f5302006-10-06 09:49:47 +000023//#define DEBUG 1
Denis Vlasenko2c916522007-01-12 14:57:37 +000024#define DEBUG 0
Denis Vlasenkoc12f5302006-10-06 09:49:47 +000025
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*/
Denis Vlasenko081eb712008-03-17 09:02:21 +000039 MAX_PACK_LEN = 512,
"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_head { // the message from client and first part of response mag
48 uint16_t id;
49 uint16_t flags;
50 uint16_t nquer; // accepts 0
51 uint16_t nansw; // 1 in response
52 uint16_t nauth; // 0
53 uint16_t nadd; // 0
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000054};
55struct dns_prop {
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000056 uint16_t type;
57 uint16_t class;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000058};
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000059struct dns_entry { // element of known name, ip address and reversed ip address
60 struct dns_entry *next;
61 char ip[IP_STRING_LEN]; // dotted decimal IP
62 char rip[IP_STRING_LEN]; // length decimal reversed IP
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000063 char name[MAX_HOST_LEN];
64};
65
Denis Vlasenkod3bac032007-03-24 12:13:04 +000066static struct dns_entry *dnsentry;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000067static uint32_t ttl = DEFAULT_TTL;
68
Denis Vlasenkod3bac032007-03-24 12:13:04 +000069static const char *fileconf = "/etc/dnsd.conf";
70
71// Must match getopt32 call
72#define OPT_daemon (option_mask32 & 0x10)
73#define OPT_verbose (option_mask32 & 0x20)
74
75
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000076/*
"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;
Denis Vlasenkoc12f5302006-10-06 09:49:47 +000082 for (; i < MAX_HOST_LEN-1 && *q; i++, q++)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000083 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;
Denis Vlasenkoc12f5302006-10-06 09:49:47 +000094 while (rip[i])
95 i++;
96 for (--i; i >= 0; i--) {
97 if (rip[i] == '.') {
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000098 rip[i] = s;
99 s = 0;
100 } else s++;
101 }
102}
103
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000104/*
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000105 * Read hostname/IP records from file
106 */
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000107static void dnsentryinit(void)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000108{
Denis Vlasenkoa34f1ed2008-07-20 17:48:59 +0000109 parser_t *parser;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000110 struct dns_entry *m, *prev;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000111
Denis Vlasenko6cd2d2b2007-01-22 14:06:03 +0000112 prev = dnsentry = NULL;
Denis Vlasenkoa34f1ed2008-07-20 17:48:59 +0000113 parser = config_open(fileconf);
114 if (parser) {
115 char *token[2];
Denis Vlasenko5415c852008-07-21 23:05:26 +0000116 while (config_read(parser, token, 2, 2, "# \t", 0)) {
Denis Vlasenkoa34f1ed2008-07-20 17:48:59 +0000117 unsigned int a,b,c,d;
118 /*
119 * Assumes all host names are lower case only
120 * Hostnames with more than one label are not handled correctly.
121 * Presently the dot is copied into name without
122 * converting to a length/string substring for that label.
123 */
Denis Vlasenko5415c852008-07-21 23:05:26 +0000124// if (!token[1] || sscanf(token[1], ".%u.%u.%u.%u"+1, &a, &b, &c, &d) != 4)
125 if (sscanf(token[1], ".%u.%u.%u.%u"+1, &a, &b, &c, &d) != 4)
Denis Vlasenkoa34f1ed2008-07-20 17:48:59 +0000126 continue;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000127
Denis Vlasenkoa34f1ed2008-07-20 17:48:59 +0000128 m = xzalloc(sizeof(*m));
129 /*m->next = NULL;*/
130 sprintf(m->ip, ".%u.%u.%u.%u"+1, a, b, c, d);
131 sprintf(m->rip, ".%u.%u.%u.%u", d, c, b, a);
132 undot((uint8_t*)m->rip);
133 convname(m->name, (uint8_t*)token[0]);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000134
Denis Vlasenkoa34f1ed2008-07-20 17:48:59 +0000135 if (OPT_verbose)
136 fprintf(stderr, "\tname:%s, ip:%s\n", &(m->name[1]), m->ip);
137
138 if (prev == NULL)
139 dnsentry = m;
140 else
141 prev->next = m;
142 prev = m;
143 }
144 config_close(parser);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000145 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000146}
147
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000148/*
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000149 * Look query up in dns records and return answer if found
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000150 * qs is the query string, first byte the string length
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000151 */
Rob Landleya6e131d2006-05-29 06:43:55 +0000152static int table_lookup(uint16_t type, uint8_t * as, uint8_t * qs)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000153{
154 int i;
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000155 struct dns_entry *d = dnsentry;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000156
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000157 do {
Denis Vlasenko91f20ab2007-01-20 01:47:44 +0000158#if DEBUG
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000159 char *p,*q;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000160 q = (char *)&(qs[1]);
161 p = &(d->name[1]);
Denis Vlasenkof7996f32007-01-11 17:20:00 +0000162 fprintf(stderr, "\n%s: %d/%d p:%s q:%s %d",
Denis Vlasenko91f20ab2007-01-20 01:47:44 +0000163 __FUNCTION__, (int)strlen(p), (int)(d->name[0]),
164 p, q, (int)strlen(q));
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000165#endif
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000166 if (type == REQ_A) { /* search by host name */
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000167 for (i = 1; i <= (int)(d->name[0]); i++)
168 if (tolower(qs[i]) != d->name[i])
169 break;
Denis Vlasenkoe9b76e12008-05-22 17:41:01 +0000170 if (i > (int)(d->name[0]) ||
171 (d->name[0] == 1 && d->name[1] == '*')) {
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000172 strcpy((char *)as, d->ip);
Denis Vlasenko91f20ab2007-01-20 01:47:44 +0000173#if DEBUG
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000174 fprintf(stderr, " OK as:%s\n", as);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000175#endif
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000176 return 0;
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000177 }
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000178 } else if (type == REQ_PTR) { /* search by IP-address */
Denis Vlasenkoe9b76e12008-05-22 17:41:01 +0000179 if ((d->name[0] != 1 || d->name[1] != '*') &&
180 !strncmp((char*)&d->rip[1], (char*)&qs[1], strlen(d->rip)-1)) {
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000181 strcpy((char *)as, d->name);
182 return 0;
183 }
184 }
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000185 d = d->next;
186 } while (d);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000187 return -1;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000188}
189
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000190/*
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000191 * Decode message and generate answer
192 */
Denis Vlasenko081eb712008-03-17 09:02:21 +0000193static int process_packet(uint8_t *buf)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000194{
Denis Vlasenko081eb712008-03-17 09:02:21 +0000195 uint8_t answstr[MAX_NAME_LEN + 1];
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000196 struct dns_head *head;
197 struct dns_prop *qprop;
Denis Vlasenko081eb712008-03-17 09:02:21 +0000198 uint8_t *from, *answb;
199 uint16_t outr_rlen;
200 uint16_t outr_flags;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000201 uint16_t flags;
Denis Vlasenko081eb712008-03-17 09:02:21 +0000202 int lookup_result, type, packet_len;
203 int querystr_len;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000204
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000205 answstr[0] = '\0';
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000206
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000207 head = (struct dns_head *)buf;
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000208 if (head->nquer == 0) {
209 bb_error_msg("no queries");
Denis Vlasenkob5b45a92007-03-24 13:09:07 +0000210 return -1;
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000211 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000212
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000213 if (head->flags & 0x8000) {
214 bb_error_msg("ignoring response packet");
Denis Vlasenkob5b45a92007-03-24 13:09:07 +0000215 return -1;
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000216 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000217
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000218 from = (void *)&head[1]; // start of query string
Denis Vlasenko081eb712008-03-17 09:02:21 +0000219//FIXME: strlen of untrusted data??!
220 querystr_len = strlen((char *)from) + 1 + sizeof(struct dns_prop);
221 answb = from + querystr_len; // where to append answer block
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000222
Denis Vlasenko081eb712008-03-17 09:02:21 +0000223 outr_rlen = 0;
224 outr_flags = 0;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000225
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000226 qprop = (struct dns_prop *)(answb - 4);
227 type = ntohs(qprop->type);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000228
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000229 // only let REQ_A and REQ_PTR pass
230 if (!(type == REQ_A || type == REQ_PTR)) {
231 goto empty_packet; /* we can't handle the query type */
232 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000233
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000234 if (ntohs(qprop->class) != 1 /* class INET */ ) {
Denis Vlasenko081eb712008-03-17 09:02:21 +0000235 outr_flags = 4; /* not supported */
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000236 goto empty_packet;
237 }
238 /* we only support standard queries */
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000239
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000240 if ((ntohs(head->flags) & 0x7800) != 0)
241 goto empty_packet;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000242
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000243 // We have a standard query
Denis Vlasenko2c916522007-01-12 14:57:37 +0000244 bb_info_msg("%s", (char *)from);
Denis Vlasenko081eb712008-03-17 09:02:21 +0000245 lookup_result = table_lookup(type, answstr, from);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000246 if (lookup_result != 0) {
Denis Vlasenko081eb712008-03-17 09:02:21 +0000247 outr_flags = 3 | 0x0400; // name do not exist and auth
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000248 goto empty_packet;
249 }
250 if (type == REQ_A) { // return an address
Denis Vlasenko081eb712008-03-17 09:02:21 +0000251 struct in_addr a; // NB! its "struct { unsigned __long__ s_addr; }"
252 uint32_t v32;
253 if (!inet_aton((char*)answstr, &a)) { //dotted dec to long conv
254 outr_flags = 1; /* Frmt err */
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000255 goto empty_packet;
256 }
Denis Vlasenko081eb712008-03-17 09:02:21 +0000257 v32 = a.s_addr; /* in case long != int */
258 memcpy(answstr, &v32, 4);
259 outr_rlen = 4; // uint32_t IP
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000260 } else
Denis Vlasenko081eb712008-03-17 09:02:21 +0000261 outr_rlen = strlen((char *)answstr) + 1; // a host name
262 outr_flags |= 0x0400; /* authority-bit */
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000263 // we have an answer
264 head->nansw = htons(1);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000265
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000266 // copy query block to answer block
Denis Vlasenko081eb712008-03-17 09:02:21 +0000267 memcpy(answb, from, querystr_len);
268 answb += querystr_len;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000269
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000270 // and append answer rr
Denis Vlasenko081eb712008-03-17 09:02:21 +0000271// FIXME: unaligned accesses??
272 *(uint32_t *) answb = htonl(ttl);
273 answb += 4;
274 *(uint16_t *) answb = htons(outr_rlen);
275 answb += 2;
276 memcpy(answb, answstr, outr_rlen);
277 answb += outr_rlen;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000278
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000279 empty_packet:
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000280
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000281 flags = ntohs(head->flags);
282 // clear rcode and RA, set responsebit and our new flags
Denis Vlasenko081eb712008-03-17 09:02:21 +0000283 flags |= (outr_flags & 0xff80) | 0x8000;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000284 head->flags = htons(flags);
Denis Vlasenko081eb712008-03-17 09:02:21 +0000285 head->nauth = head->nadd = 0;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000286 head->nquer = htons(1);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000287
Denis Vlasenko081eb712008-03-17 09:02:21 +0000288 packet_len = answb - buf;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000289 return packet_len;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000290}
291
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000292/*
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000293 * Exit on signal
294 */
Denis Vlasenko68404f12008-03-17 09:00:54 +0000295static void interrupt(int sig)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000296{
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000297 /* unlink("/var/run/dnsd.lock"); */
Denis Vlasenko2c916522007-01-12 14:57:37 +0000298 bb_error_msg("interrupt, exiting\n");
Denis Vlasenko68404f12008-03-17 09:00:54 +0000299 kill_myself_with_sig(sig);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000300}
301
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000302int dnsd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000303int dnsd_main(int argc UNUSED_PARAM, char **argv)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000304{
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000305 const char *listen_interface = "0.0.0.0";
Denis Vlasenko2c916522007-01-12 14:57:37 +0000306 char *sttl, *sport;
Denis Vlasenko081eb712008-03-17 09:02:21 +0000307 len_and_sockaddr *lsa, *from, *to;
308 unsigned lsa_size;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000309 int udps;
310 uint16_t port = 53;
Denis Vlasenko081eb712008-03-17 09:02:21 +0000311 /* Paranoid sizing: querystring x2 + ttl + outr_rlen + answstr */
312 /* I'd rather see process_packet() fixed instead... */
313 uint8_t buf[MAX_PACK_LEN * 2 + 4 + 2 + (MAX_NAME_LEN+1)];
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000314
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000315 getopt32(argv, "i:c:t:p:dv", &listen_interface, &fileconf, &sttl, &sport);
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000316 //if (option_mask32 & 0x1) // -i
317 //if (option_mask32 & 0x2) // -c
318 if (option_mask32 & 0x4) // -t
Denis Vlasenko2c916522007-01-12 14:57:37 +0000319 ttl = xatou_range(sttl, 1, 0xffffffff);
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000320 if (option_mask32 & 0x8) // -p
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000321 port = xatou_range(sport, 1, 0xffff);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000322
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000323 if (OPT_verbose) {
324 bb_info_msg("listen_interface: %s", listen_interface);
325 bb_info_msg("ttl: %d, port: %d", ttl, port);
326 bb_info_msg("fileconf: %s", fileconf);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000327 }
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000328
Denis Vlasenko2c916522007-01-12 14:57:37 +0000329 if (OPT_daemon) {
Denis Vlasenko5a142022007-03-26 13:20:54 +0000330 bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv);
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000331 openlog(applet_name, LOG_PID, LOG_DAEMON);
Denis Vlasenko2c916522007-01-12 14:57:37 +0000332 logmode = LOGMODE_SYSLOG;
333 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000334
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000335 dnsentryinit();
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000336
337 signal(SIGINT, interrupt);
Denis Vlasenko25591c32008-02-16 22:58:56 +0000338 bb_signals(0
339 /* why? + (1 << SIGPIPE) */
340 + (1 << SIGHUP)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000341#ifdef SIGTSTP
Denis Vlasenko25591c32008-02-16 22:58:56 +0000342 + (1 << SIGTSTP)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000343#endif
344#ifdef SIGURG
Denis Vlasenko25591c32008-02-16 22:58:56 +0000345 + (1 << SIGURG)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000346#endif
Denis Vlasenko25591c32008-02-16 22:58:56 +0000347 , SIG_IGN);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000348
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000349 lsa = xdotted2sockaddr(listen_interface, port);
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000350 udps = xsocket(lsa->u.sa.sa_family, SOCK_DGRAM, 0);
351 xbind(udps, &lsa->u.sa, lsa->len);
Denis Vlasenko081eb712008-03-17 09:02:21 +0000352 socket_want_pktinfo(udps); /* needed for recv_from_to to work */
353 lsa_size = LSA_LEN_SIZE + lsa->len;
354 from = xzalloc(lsa_size);
355 to = xzalloc(lsa_size);
356
Denis Vlasenko2c916522007-01-12 14:57:37 +0000357 bb_info_msg("Accepting UDP packets on %s",
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000358 xmalloc_sockaddr2dotted(&lsa->u.sa));
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000359
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000360 while (1) {
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000361 int r;
Denis Vlasenko081eb712008-03-17 09:02:21 +0000362 /* Try to get *DEST* address (to which of our addresses
363 * this query was directed), and reply from the same address.
364 * Or else we can exhibit usual UDP ugliness:
365 * [ip1.multihomed.ip2] <= query to ip1 <= peer
366 * [ip1.multihomed.ip2] => reply from ip2 => peer (confused) */
367 memcpy(to, lsa, lsa_size);
368 r = recv_from_to(udps, buf, MAX_PACK_LEN + 1, 0, &from->u.sa, &to->u.sa, lsa->len);
369 if (r < 12 || r > MAX_PACK_LEN) {
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000370 bb_error_msg("invalid packet size");
371 continue;
Denis Vlasenko2c916522007-01-12 14:57:37 +0000372 }
Denis Vlasenko081eb712008-03-17 09:02:21 +0000373 if (OPT_verbose)
374 bb_info_msg("Got UDP packet");
375 buf[r] = '\0'; /* paranoia */
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000376 r = process_packet(buf);
377 if (r <= 0)
378 continue;
Denis Vlasenkoe9b76e12008-05-22 17:41:01 +0000379 send_to_from(udps, buf, r, 0, &from->u.sa, &to->u.sa, lsa->len);
Denis Vlasenko2c916522007-01-12 14:57:37 +0000380 }
Denis Vlasenkob5b45a92007-03-24 13:09:07 +0000381 return 0;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000382}