blob: f95ba6b405ac1df9693b16218b6c638988e6891b [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 Vlasenkoa19e6492009-03-11 14:40:00 +000066#define OPT_verbose (option_mask32)
Denis Vlasenkod3bac032007-03-24 12:13:04 +000067
68
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000069/*
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000070 * Convert host name from C-string to dns length/string.
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000071 */
Rob Landleya6e131d2006-05-29 06:43:55 +000072static void convname(char *a, uint8_t *q)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000073{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000074 int i = (q[0] == '.') ? 0 : 1;
Denis Vlasenkoc12f5302006-10-06 09:49:47 +000075 for (; i < MAX_HOST_LEN-1 && *q; i++, q++)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000076 a[i] = tolower(*q);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000077 a[0] = i - 1;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000078 a[i] = 0;
79}
80
81/*
Bernhard Reutner-Fischerc418d482006-05-31 10:19:51 +000082 * Insert length of substrings instead of dots
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000083 */
Denis Vlasenkoa19e6492009-03-11 14:40:00 +000084static void undot(uint8_t *rip)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000085{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000086 int i = 0, s = 0;
Denis Vlasenkoc12f5302006-10-06 09:49:47 +000087 while (rip[i])
88 i++;
89 for (--i; i >= 0; i--) {
90 if (rip[i] == '.') {
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000091 rip[i] = s;
92 s = 0;
93 } else s++;
94 }
95}
96
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000097/*
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000098 * Read hostname/IP records from file
99 */
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000100static struct dns_entry *parse_conf_file(const char *fileconf)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000101{
Denis Vlasenko084266e2008-07-26 23:08:31 +0000102 char *token[2];
Denis Vlasenkoa34f1ed2008-07-20 17:48:59 +0000103 parser_t *parser;
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000104 struct dns_entry *m, *prev, *conf_data;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000105
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000106 prev = conf_data = NULL;
Denis Vlasenkoa34f1ed2008-07-20 17:48:59 +0000107 parser = config_open(fileconf);
Denis Vlasenko084266e2008-07-26 23:08:31 +0000108 while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) {
109 unsigned a, b, c, d;
110 /*
111 * Assumes all host names are lower case only
112 * Hostnames with more than one label are not handled correctly.
113 * Presently the dot is copied into name without
114 * converting to a length/string substring for that label.
115 */
116// if (!token[1] || sscanf(token[1], ".%u.%u.%u.%u"+1, &a, &b, &c, &d) != 4)
117 if (sscanf(token[1], ".%u.%u.%u.%u"+1, &a, &b, &c, &d) != 4)
118 continue;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000119
Denis Vlasenko084266e2008-07-26 23:08:31 +0000120 m = xzalloc(sizeof(*m));
121 /*m->next = NULL;*/
122 sprintf(m->ip, ".%u.%u.%u.%u"+1, a, b, c, d);
123 sprintf(m->rip, ".%u.%u.%u.%u", d, c, b, a);
124 undot((uint8_t*)m->rip);
125 convname(m->name, (uint8_t*)token[0]);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000126
Denis Vlasenko084266e2008-07-26 23:08:31 +0000127 if (OPT_verbose)
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000128 bb_error_msg("name:%s, ip:%s", &(m->name[1]), m->ip);
Denis Vlasenkoa34f1ed2008-07-20 17:48:59 +0000129
Denis Vlasenko084266e2008-07-26 23:08:31 +0000130 if (prev == NULL)
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000131 conf_data = m;
Denis Vlasenko084266e2008-07-26 23:08:31 +0000132 else
133 prev->next = m;
134 prev = m;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000135 }
Denis Vlasenko084266e2008-07-26 23:08:31 +0000136 config_close(parser);
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000137 return conf_data;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000138}
139
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000140/*
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000141 * Look query up in dns records and return answer if found
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000142 * qs is the query string, first byte the string length
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000143 */
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000144static int table_lookup(struct dns_entry *d, uint16_t type, uint8_t *as, uint8_t *qs)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000145{
146 int i;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000147
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000148 do {
Denis Vlasenko91f20ab2007-01-20 01:47:44 +0000149#if DEBUG
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000150 char *p, *q;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000151 q = (char *)&(qs[1]);
152 p = &(d->name[1]);
Denis Vlasenkof7996f32007-01-11 17:20:00 +0000153 fprintf(stderr, "\n%s: %d/%d p:%s q:%s %d",
Denis Vlasenko91f20ab2007-01-20 01:47:44 +0000154 __FUNCTION__, (int)strlen(p), (int)(d->name[0]),
155 p, q, (int)strlen(q));
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000156#endif
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000157 if (type == REQ_A) {
158 /* search by host name */
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000159 for (i = 1; i <= (int)(d->name[0]); i++)
160 if (tolower(qs[i]) != d->name[i])
161 break;
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000162 if (i > (int)(d->name[0])
163 || (d->name[0] == 1 && d->name[1] == '*')
164 ) {
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000165 strcpy((char *)as, d->ip);
Denis Vlasenko91f20ab2007-01-20 01:47:44 +0000166#if DEBUG
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000167 fprintf(stderr, " OK as:%s\n", as);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000168#endif
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000169 return 0;
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000170 }
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000171 } else if (type == REQ_PTR) {
172 /* search by IP-address */
173 if ((d->name[0] != 1 || d->name[1] != '*')
174 && !strncmp(d->rip + 1, (char*)qs + 1, strlen(d->rip)-1)
175 ) {
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000176 strcpy((char *)as, d->name);
177 return 0;
178 }
179 }
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000180 d = d->next;
181 } while (d);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000182 return -1;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000183}
184
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000185/*
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000186 * Decode message and generate answer
187 */
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000188static int process_packet(struct dns_entry *conf_data, uint32_t conf_ttl, uint8_t *buf)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000189{
Denis Vlasenko081eb712008-03-17 09:02:21 +0000190 uint8_t answstr[MAX_NAME_LEN + 1];
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000191 struct dns_head *head;
192 struct dns_prop *qprop;
Denis Vlasenko081eb712008-03-17 09:02:21 +0000193 uint8_t *from, *answb;
194 uint16_t outr_rlen;
195 uint16_t outr_flags;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000196 uint16_t flags;
Denis Vlasenko081eb712008-03-17 09:02:21 +0000197 int lookup_result, type, packet_len;
198 int querystr_len;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000199
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000200 answstr[0] = '\0';
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000201
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000202 head = (struct dns_head *)buf;
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000203 if (head->nquer == 0) {
204 bb_error_msg("no queries");
Denis Vlasenkob5b45a92007-03-24 13:09:07 +0000205 return -1;
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000206 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000207
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000208 if (head->flags & 0x8000) {
209 bb_error_msg("ignoring response packet");
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
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000213 from = (void *)&head[1]; // start of query string
Denis Vlasenko081eb712008-03-17 09:02:21 +0000214//FIXME: strlen of untrusted data??!
215 querystr_len = strlen((char *)from) + 1 + sizeof(struct dns_prop);
216 answb = from + querystr_len; // where to append answer block
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000217
Denis Vlasenko081eb712008-03-17 09:02:21 +0000218 outr_rlen = 0;
219 outr_flags = 0;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000220
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000221 qprop = (struct dns_prop *)(answb - 4);
222 type = ntohs(qprop->type);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000223
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000224 // only let REQ_A and REQ_PTR pass
225 if (!(type == REQ_A || type == REQ_PTR)) {
226 goto empty_packet; /* we can't handle the query type */
227 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000228
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000229 if (ntohs(qprop->class) != 1 /* class INET */ ) {
Denis Vlasenko081eb712008-03-17 09:02:21 +0000230 outr_flags = 4; /* not supported */
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000231 goto empty_packet;
232 }
233 /* we only support standard queries */
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000234
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000235 if ((ntohs(head->flags) & 0x7800) != 0)
236 goto empty_packet;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000237
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000238 // We have a standard query
Denis Vlasenko2c916522007-01-12 14:57:37 +0000239 bb_info_msg("%s", (char *)from);
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000240 lookup_result = table_lookup(conf_data, type, answstr, from);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000241 if (lookup_result != 0) {
Denis Vlasenko081eb712008-03-17 09:02:21 +0000242 outr_flags = 3 | 0x0400; // name do not exist and auth
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000243 goto empty_packet;
244 }
245 if (type == REQ_A) { // return an address
Denis Vlasenko081eb712008-03-17 09:02:21 +0000246 struct in_addr a; // NB! its "struct { unsigned __long__ s_addr; }"
247 uint32_t v32;
248 if (!inet_aton((char*)answstr, &a)) { //dotted dec to long conv
249 outr_flags = 1; /* Frmt err */
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000250 goto empty_packet;
251 }
Denis Vlasenko081eb712008-03-17 09:02:21 +0000252 v32 = a.s_addr; /* in case long != int */
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000253 move_to_unaligned32(answstr, v32);
Denis Vlasenko081eb712008-03-17 09:02:21 +0000254 outr_rlen = 4; // uint32_t IP
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000255 } else
Denis Vlasenko081eb712008-03-17 09:02:21 +0000256 outr_rlen = strlen((char *)answstr) + 1; // a host name
257 outr_flags |= 0x0400; /* authority-bit */
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000258 // we have an answer
259 head->nansw = htons(1);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000260
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000261 // copy query block to answer block
Denis Vlasenko081eb712008-03-17 09:02:21 +0000262 memcpy(answb, from, querystr_len);
263 answb += querystr_len;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000264
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000265 // and append answer rr
Denis Vlasenko081eb712008-03-17 09:02:21 +0000266// FIXME: unaligned accesses??
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000267 *(uint32_t *) answb = htonl(conf_ttl);
Denis Vlasenko081eb712008-03-17 09:02:21 +0000268 answb += 4;
269 *(uint16_t *) answb = htons(outr_rlen);
270 answb += 2;
271 memcpy(answb, answstr, outr_rlen);
272 answb += outr_rlen;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000273
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000274 empty_packet:
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000275
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000276 flags = ntohs(head->flags);
277 // clear rcode and RA, set responsebit and our new flags
Denis Vlasenko081eb712008-03-17 09:02:21 +0000278 flags |= (outr_flags & 0xff80) | 0x8000;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000279 head->flags = htons(flags);
Denis Vlasenko081eb712008-03-17 09:02:21 +0000280 head->nauth = head->nadd = 0;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000281 head->nquer = htons(1);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000282
Denis Vlasenko081eb712008-03-17 09:02:21 +0000283 packet_len = answb - buf;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000284 return packet_len;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000285}
286
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000287/*
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000288 * Exit on signal
289 */
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000290//static void interrupt(int sig)
291//{
292// /* unlink("/var/run/dnsd.lock"); */
293// bb_error_msg("interrupt, exiting\n");
294// kill_myself_with_sig(sig);
295//}
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000296
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000297int dnsd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000298int dnsd_main(int argc UNUSED_PARAM, char **argv)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000299{
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000300 const char *listen_interface = "0.0.0.0";
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000301 const char *fileconf = "/etc/dnsd.conf";
302 struct dns_entry *conf_data;
303 uint32_t conf_ttl = DEFAULT_TTL;
Denis Vlasenko2c916522007-01-12 14:57:37 +0000304 char *sttl, *sport;
Denis Vlasenko081eb712008-03-17 09:02:21 +0000305 len_and_sockaddr *lsa, *from, *to;
306 unsigned lsa_size;
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000307 int udps, opts;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000308 uint16_t port = 53;
Denis Vlasenko081eb712008-03-17 09:02:21 +0000309 /* Paranoid sizing: querystring x2 + ttl + outr_rlen + answstr */
310 /* I'd rather see process_packet() fixed instead... */
311 uint8_t buf[MAX_PACK_LEN * 2 + 4 + 2 + (MAX_NAME_LEN+1)];
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000312
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000313 opts = getopt32(argv, "vi:c:t:p:d", &listen_interface, &fileconf, &sttl, &sport);
314 //if (opts & 0x1) // -v
315 //if (opts & 0x2) // -i
316 //if (opts & 0x4) // -c
317 if (opts & 0x8) // -t
318 conf_ttl = xatou_range(sttl, 1, 0xffffffff);
319 if (opts & 0x10) // -p
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000320 port = xatou_range(sport, 1, 0xffff);
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000321 if (opts & 0x20) { // -d
Denis Vlasenko5a142022007-03-26 13:20:54 +0000322 bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv);
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000323 openlog(applet_name, LOG_PID, LOG_DAEMON);
Denis Vlasenko2c916522007-01-12 14:57:37 +0000324 logmode = LOGMODE_SYSLOG;
325 }
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000326 /* Clear all except "verbose" bit */
327 option_mask32 &= 1;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000328
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000329 conf_data = parse_conf_file(fileconf);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000330
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000331// signal(SIGINT, interrupt); - just for one message?
Denis Vlasenko25591c32008-02-16 22:58:56 +0000332 bb_signals(0
333 /* why? + (1 << SIGPIPE) */
334 + (1 << SIGHUP)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000335#ifdef SIGTSTP
Denis Vlasenko25591c32008-02-16 22:58:56 +0000336 + (1 << SIGTSTP)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000337#endif
338#ifdef SIGURG
Denis Vlasenko25591c32008-02-16 22:58:56 +0000339 + (1 << SIGURG)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000340#endif
Denis Vlasenko25591c32008-02-16 22:58:56 +0000341 , SIG_IGN);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000342
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000343 lsa = xdotted2sockaddr(listen_interface, port);
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000344 udps = xsocket(lsa->u.sa.sa_family, SOCK_DGRAM, 0);
345 xbind(udps, &lsa->u.sa, lsa->len);
Denis Vlasenko081eb712008-03-17 09:02:21 +0000346 socket_want_pktinfo(udps); /* needed for recv_from_to to work */
347 lsa_size = LSA_LEN_SIZE + lsa->len;
348 from = xzalloc(lsa_size);
349 to = xzalloc(lsa_size);
350
Denis Vlasenko2c916522007-01-12 14:57:37 +0000351 bb_info_msg("Accepting UDP packets on %s",
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000352 xmalloc_sockaddr2dotted(&lsa->u.sa));
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000353
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000354 while (1) {
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000355 int r;
Denis Vlasenko081eb712008-03-17 09:02:21 +0000356 /* Try to get *DEST* address (to which of our addresses
357 * this query was directed), and reply from the same address.
358 * Or else we can exhibit usual UDP ugliness:
359 * [ip1.multihomed.ip2] <= query to ip1 <= peer
360 * [ip1.multihomed.ip2] => reply from ip2 => peer (confused) */
361 memcpy(to, lsa, lsa_size);
362 r = recv_from_to(udps, buf, MAX_PACK_LEN + 1, 0, &from->u.sa, &to->u.sa, lsa->len);
363 if (r < 12 || r > MAX_PACK_LEN) {
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000364 bb_error_msg("invalid packet size");
365 continue;
Denis Vlasenko2c916522007-01-12 14:57:37 +0000366 }
Denis Vlasenko081eb712008-03-17 09:02:21 +0000367 if (OPT_verbose)
368 bb_info_msg("Got UDP packet");
369 buf[r] = '\0'; /* paranoia */
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000370 r = process_packet(conf_data, conf_ttl, buf);
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000371 if (r <= 0)
372 continue;
Denis Vlasenkoe9b76e12008-05-22 17:41:01 +0000373 send_to_from(udps, buf, r, 0, &from->u.sa, &to->u.sa, lsa->len);
Denis Vlasenko2c916522007-01-12 14:57:37 +0000374 }
Denis Vlasenkob5b45a92007-03-24 13:09:07 +0000375 return 0;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000376}