blob: 6f1ec0ea4ce125e6e056a073a491dc5a85f4fe56 [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
Denis Vlasenkoc12f5302006-10-06 09:49:47 +000026// Must matct getopt32 call
27#define OPT_daemon (option_mask32 & 0x10)
28#define OPT_verbose (option_mask32 & 0x20)
29
30//#define DEBUG 1
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;
Denis Vlasenkoc12f5302006-10-06 09:49:47 +000079// FIXME! unused! :(
80static int daemonmode;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000081static uint32_t ttl = DEFAULT_TTL;
82
83/*
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000084 * Convert host name from C-string to dns length/string.
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000085 */
Rob Landleya6e131d2006-05-29 06:43:55 +000086static void convname(char *a, uint8_t *q)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000087{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000088 int i = (q[0] == '.') ? 0 : 1;
Denis Vlasenkoc12f5302006-10-06 09:49:47 +000089 for (; i < MAX_HOST_LEN-1 && *q; i++, q++)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000090 a[i] = tolower(*q);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000091 a[0] = i - 1;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000092 a[i] = 0;
93}
94
95/*
Bernhard Reutner-Fischerc418d482006-05-31 10:19:51 +000096 * Insert length of substrings instead of dots
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000097 */
Rob Landleya6e131d2006-05-29 06:43:55 +000098static void undot(uint8_t * rip)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000099{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000100 int i = 0, s = 0;
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000101 while (rip[i])
102 i++;
103 for (--i; i >= 0; i--) {
104 if (rip[i] == '.') {
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000105 rip[i] = s;
106 s = 0;
107 } else s++;
108 }
109}
110
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000111/*
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000112 * Append message to log file
113 */
Rob Landleya6e131d2006-05-29 06:43:55 +0000114static void log_message(char *filename, char *message)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000115{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000116 FILE *logfile;
117 if (!daemonmode)
118 return;
119 logfile = fopen(filename, "a");
120 if (!logfile)
121 return;
122 fprintf(logfile, "%s\n", message);
123 fclose(logfile);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000124}
125
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000126/*
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000127 * Read one line of hostname/IP from file
128 * Returns 0 for each valid entry read, -1 at EOF
129 * Assumes all host names are lower case only
130 * Hostnames with more than one label is not handled correctly.
131 * Presently the dot is copied into name without
132 * converting to a length/string substring for that label.
133 */
134
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000135static int getfileentry(FILE * fp, struct dns_entry *s)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000136{
137 unsigned int a,b,c,d;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000138 char *r, *name;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000139
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000140 restart:
Denis Vlasenko2d5ca602006-10-12 22:43:20 +0000141 r = xmalloc_fgets(fp);
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000142 if (!r)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000143 return -1;
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000144 while (*r == ' ' || *r == '\t') {
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000145 r++;
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000146 if (!*r || *r == '#' || *r == '\n')
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000147 goto restart; /* skipping empty/blank and commented lines */
148 }
149 name = r;
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000150 while (*r != ' ' && *r != '\t')
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000151 r++;
152 *r++ = 0;
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000153 if (sscanf(r, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
154 goto restart; /* skipping wrong lines */
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000155
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000156 sprintf(s->ip, "%u.%u.%u.%u", a, b, c, d);
157 sprintf(s->rip, ".%u.%u.%u.%u", d, c, b, a);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000158 undot((uint8_t*)s->rip);
159 convname(s->name,(uint8_t*)name);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000160
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000161 if (OPT_verbose)
162 fprintf(stderr, "\tname:%s, ip:%s\n", &(s->name[1]),s->ip);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000163
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000164 return 0;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000165}
166
167/*
168 * Read hostname/IP records from file
169 */
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000170static void dnsentryinit(void)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000171{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000172 FILE *fp;
173 struct dns_entry *m, *prev;
174 prev = dnsentry = NULL;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000175
Rob Landley86b4d642006-08-03 17:58:17 +0000176 fp = xfopen(fileconf, "r");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000177
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000178 while (1) {
Rob Landleya6e131d2006-05-29 06:43:55 +0000179 m = xmalloc(sizeof(struct dns_entry));
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000180
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000181 m->next = NULL;
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000182 if (getfileentry(fp, m))
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000183 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 */
Rob Landleya6e131d2006-05-29 06:43:55 +0000198static int listen_socket(char *iface_addr, int listen_port)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000199{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000200 struct sockaddr_in a;
201 char msg[100];
Bernhard Reutner-Fischer28fbd692006-12-19 16:31:09 +0000202 int sck;
203 sck = xsocket(PF_INET, SOCK_DGRAM, 0);
204 if (setsockopt_reuseaddr(sck) < 0)
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000205 bb_perror_msg_and_die("setsockopt() failed");
"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-Fischer28fbd692006-12-19 16:31:09 +0000211 xbind(sck, (struct sockaddr *)&a, sizeof(a));
212 xlisten(sck, 50);
"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);
Bernhard Reutner-Fischer28fbd692006-12-19 16:31:09 +0000216 return sck;
"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]);
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000233 fprintf(stderr, "\n%s: %d/%d p:%s q:%s %d",
234 __FUNCTION__, strlen(p), (int)(d->name[0]), p, q, strlen(q));
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000235#endif
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000236 if (type == REQ_A) { /* search by host name */
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000237 for (i = 1; i <= (int)(d->name[0]); i++)
238 if (tolower(qs[i]) != d->name[i])
239 break;
240 if (i > (int)(d->name[0])) {
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000241#ifdef DEBUG
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000242 fprintf(stderr, " OK");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000243#endif
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000244 strcpy((char *)as, d->ip);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000245#ifdef DEBUG
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000246 fprintf(stderr, " as:%s\n", as);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000247#endif
248 return 0;
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000249 }
250 } else
251 if (type == REQ_PTR) { /* search by IP-address */
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000252 if (!strncmp((char*)&d->rip[1], (char*)&qs[1], strlen(d->rip)-1)) {
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000253 strcpy((char *)as, d->name);
254 return 0;
255 }
256 }
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000257 d = d->next;
258 } while (d);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000259 return -1;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000260}
261
262
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000263/*
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000264 * Decode message and generate answer
265 */
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000266#define eret(s) do { fputs(s, stderr); return -1; } while (0)
Rob Landleya6e131d2006-05-29 06:43:55 +0000267static int process_packet(uint8_t * buf)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000268{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000269 struct dns_head *head;
270 struct dns_prop *qprop;
271 struct dns_repl outr;
272 void *next, *from, *answb;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000273
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000274 uint8_t answstr[MAX_NAME_LEN + 1];
275 int lookup_result, type, len, packet_len;
276 uint16_t flags;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000277
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000278 answstr[0] = '\0';
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000279
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000280 head = (struct dns_head *)buf;
281 if (head->nquer == 0)
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000282 eret("no queries\n");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000283
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000284 if (head->flags & 0x8000)
285 eret("ignoring response packet\n");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000286
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000287 from = (void *)&head[1]; // start of query string
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000288 next = answb = from + strlen((char *)from) + 1 + sizeof(struct dns_prop); // where to append answer block
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000289
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000290 outr.rlen = 0; // may change later
291 outr.r = NULL;
292 outr.flags = 0;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000293
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000294 qprop = (struct dns_prop *)(answb - 4);
295 type = ntohs(qprop->type);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000296
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000297 // only let REQ_A and REQ_PTR pass
298 if (!(type == REQ_A || type == REQ_PTR)) {
299 goto empty_packet; /* we can't handle the query type */
300 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000301
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000302 if (ntohs(qprop->class) != 1 /* class INET */ ) {
303 outr.flags = 4; /* not supported */
304 goto empty_packet;
305 }
306 /* we only support standard queries */
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000307
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000308 if ((ntohs(head->flags) & 0x7800) != 0)
309 goto empty_packet;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000310
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000311 // We have a standard query
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000312 log_message(LOG_FILE, (char *)from);
313 lookup_result = table_lookup(type, answstr, (uint8_t*)from);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000314 if (lookup_result != 0) {
315 outr.flags = 3 | 0x0400; //name do not exist and auth
316 goto empty_packet;
317 }
318 if (type == REQ_A) { // return an address
319 struct in_addr a;
320 if (!inet_aton((char*)answstr, &a)) {//dotted dec to long conv
321 outr.flags = 1; /* Frmt err */
322 goto empty_packet;
323 }
324 memcpy(answstr, &a.s_addr, 4); // save before a disappears
325 outr.rlen = 4; // uint32_t IP
326 }
327 else
328 outr.rlen = strlen((char *)answstr) + 1; // a host name
329 outr.r = answstr; // 32 bit ip or a host name
330 outr.flags |= 0x0400; /* authority-bit */
331 // we have an answer
332 head->nansw = htons(1);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000333
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000334 // copy query block to answer block
335 len = answb - from;
336 memcpy(answb, from, len);
337 next += len;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000338
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000339 // and append answer rr
340 *(uint32_t *) next = htonl(ttl);
341 next += 4;
342 *(uint16_t *) next = htons(outr.rlen);
343 next += 2;
344 memcpy(next, (void *)answstr, outr.rlen);
345 next += outr.rlen;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000346
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000347 empty_packet:
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000348
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000349 flags = ntohs(head->flags);
350 // clear rcode and RA, set responsebit and our new flags
351 flags |= (outr.flags & 0xff80) | 0x8000;
352 head->flags = htons(flags);
353 head->nauth = head->nadd = htons(0);
354 head->nquer = htons(1);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000355
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000356 packet_len = next - (void *)buf;
357 return packet_len;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000358}
359
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000360/*
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000361 * Exit on signal
362 */
Rob Landleya6e131d2006-05-29 06:43:55 +0000363static void interrupt(int x)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000364{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000365 unlink(LOCK_FILE);
366 write(2, "interrupt exiting\n", 18);
367 exit(2);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000368}
369
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000370int dnsd_main(int argc, char **argv)
371{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000372 int udps;
373 uint16_t port = 53;
374 uint8_t buf[MAX_PACK_LEN];
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000375 char *listen_interface = "0.0.0.0";
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000376 char *sttl, *sport;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000377
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000378 getopt32(argc, argv, "i:c:t:p:dv", &listen_interface, &fileconf, &sttl, &sport);
379 //if (option_mask32 & 0x1) // -i
380 //if (option_mask32 & 0x2) // -c
381 if (option_mask32 & 0x4) // -t
382 if (!(ttl = atol(sttl)))
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000383 bb_show_usage();
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000384 if (option_mask32 & 0x8) // -p
385 if (!(port = atol(sport)))
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000386 bb_show_usage();
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000387
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000388 if (OPT_verbose) {
389 bb_info_msg("listen_interface: %s", listen_interface);
390 bb_info_msg("ttl: %d, port: %d", ttl, port);
391 bb_info_msg("fileconf: %s", fileconf);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000392 }
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000393
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000394 if (OPT_daemon)
Bernhard Reutner-Fischerc418d482006-05-31 10:19:51 +0000395#ifdef BB_NOMMU
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000396 /* reexec for vfork() do continue parent */
397 vfork_daemon_rexec(1, 0, argc, argv, "-d");
Bernhard Reutner-Fischerc418d482006-05-31 10:19:51 +0000398#else
Rob Landley86b4d642006-08-03 17:58:17 +0000399 xdaemon(1, 0);
Bernhard Reutner-Fischerc418d482006-05-31 10:19:51 +0000400#endif
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000401
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000402 dnsentryinit();
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000403
404 signal(SIGINT, interrupt);
405 signal(SIGPIPE, SIG_IGN);
406 signal(SIGHUP, SIG_IGN);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000407#ifdef SIGTSTP
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000408 signal(SIGTSTP, SIG_IGN);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000409#endif
410#ifdef SIGURG
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000411 signal(SIGURG, SIG_IGN);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000412#endif
413
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000414 udps = listen_socket(listen_interface, port);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000415
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000416 while (1) {
417 fd_set fdset;
418 int r;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000419
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000420 FD_ZERO(&fdset);
421 FD_SET(udps, &fdset);
422 // Block until a message arrives
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000423 r = select(udps + 1, &fdset, NULL, NULL, NULL);
424 if (r < 0)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000425 bb_perror_msg_and_die("select error");
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000426 if (r == 0)
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000427 bb_perror_msg_and_die("select spurious return");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000428
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000429 /* Can this test ever be false? - yes */
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000430 if (FD_ISSET(udps, &fdset)) {
431 struct sockaddr_in from;
432 int fromlen = sizeof(from);
433 r = recvfrom(udps, buf, sizeof(buf), 0,
434 (struct sockaddr *)&from,
435 (void *)&fromlen);
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000436 if (OPT_verbose)
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000437 fprintf(stderr, "\n--- Got UDP ");
438 log_message(LOG_FILE, "\n--- Got UDP ");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000439
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000440 if (r < 12 || r > 512) {
441 bb_error_msg("invalid packet size");
442 continue;
443 }
444 if (r > 0) {
445 r = process_packet(buf);
446 if (r > 0)
447 sendto(udps, buf,
448 r, 0, (struct sockaddr *)&from,
449 fromlen);
450 }
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000451 } // end if
452 } // end while
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000453 return 0;
454}