blob: 6f9dc5d67129df7d97401536a7d5252571ebe5ea [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];
202 int s;
203 int yes = 1;
Rob Landley86b4d642006-08-03 17:58:17 +0000204 s = xsocket(PF_INET, SOCK_DGRAM, 0);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000205#ifdef SO_REUSEADDR
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000206 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&yes, sizeof(yes)) < 0)
207 bb_perror_msg_and_die("setsockopt() failed");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000208#endif
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000209 memset(&a, 0, sizeof(a));
210 a.sin_port = htons(listen_port);
211 a.sin_family = AF_INET;
212 if (!inet_aton(iface_addr, &a.sin_addr))
213 bb_perror_msg_and_die("bad iface address");
Rob Landley86b4d642006-08-03 17:58:17 +0000214 xbind(s, (struct sockaddr *)&a, sizeof(a));
Rob Landley4640b052006-08-15 21:37:55 +0000215 xlisten(s, 50);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000216 sprintf(msg, "accepting UDP packets on addr:port %s:%d\n",
217 iface_addr, (int)listen_port);
218 log_message(LOG_FILE, msg);
219 return s;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000220}
221
222/*
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000223 * Look query up in dns records and return answer if found
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000224 * qs is the query string, first byte the string length
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000225 */
Rob Landleya6e131d2006-05-29 06:43:55 +0000226static int table_lookup(uint16_t type, uint8_t * as, uint8_t * qs)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000227{
228 int i;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000229 struct dns_entry *d=dnsentry;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000230
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000231 do {
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000232#ifdef DEBUG
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000233 char *p,*q;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000234 q = (char *)&(qs[1]);
235 p = &(d->name[1]);
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000236 fprintf(stderr, "\n%s: %d/%d p:%s q:%s %d",
237 __FUNCTION__, strlen(p), (int)(d->name[0]), p, q, strlen(q));
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000238#endif
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000239 if (type == REQ_A) { /* search by host name */
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000240 for (i = 1; i <= (int)(d->name[0]); i++)
241 if (tolower(qs[i]) != d->name[i])
242 break;
243 if (i > (int)(d->name[0])) {
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000244#ifdef DEBUG
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000245 fprintf(stderr, " OK");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000246#endif
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000247 strcpy((char *)as, d->ip);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000248#ifdef DEBUG
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000249 fprintf(stderr, " as:%s\n", as);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000250#endif
251 return 0;
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000252 }
253 } else
254 if (type == REQ_PTR) { /* search by IP-address */
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000255 if (!strncmp((char*)&d->rip[1], (char*)&qs[1], strlen(d->rip)-1)) {
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000256 strcpy((char *)as, d->name);
257 return 0;
258 }
259 }
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000260 d = d->next;
261 } while (d);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000262 return -1;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000263}
264
265
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000266/*
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000267 * Decode message and generate answer
268 */
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000269#define eret(s) do { fputs(s, stderr); return -1; } while (0)
Rob Landleya6e131d2006-05-29 06:43:55 +0000270static int process_packet(uint8_t * buf)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000271{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000272 struct dns_head *head;
273 struct dns_prop *qprop;
274 struct dns_repl outr;
275 void *next, *from, *answb;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000276
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000277 uint8_t answstr[MAX_NAME_LEN + 1];
278 int lookup_result, type, len, packet_len;
279 uint16_t flags;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000280
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000281 answstr[0] = '\0';
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000282
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000283 head = (struct dns_head *)buf;
284 if (head->nquer == 0)
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000285 eret("no queries\n");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000286
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000287 if (head->flags & 0x8000)
288 eret("ignoring response packet\n");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000289
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000290 from = (void *)&head[1]; // start of query string
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000291 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 +0000292
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000293 outr.rlen = 0; // may change later
294 outr.r = NULL;
295 outr.flags = 0;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000296
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000297 qprop = (struct dns_prop *)(answb - 4);
298 type = ntohs(qprop->type);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000299
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000300 // only let REQ_A and REQ_PTR pass
301 if (!(type == REQ_A || type == REQ_PTR)) {
302 goto empty_packet; /* we can't handle the query type */
303 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000304
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000305 if (ntohs(qprop->class) != 1 /* class INET */ ) {
306 outr.flags = 4; /* not supported */
307 goto empty_packet;
308 }
309 /* we only support standard queries */
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000310
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000311 if ((ntohs(head->flags) & 0x7800) != 0)
312 goto empty_packet;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000313
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000314 // We have a standard query
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000315 log_message(LOG_FILE, (char *)from);
316 lookup_result = table_lookup(type, answstr, (uint8_t*)from);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000317 if (lookup_result != 0) {
318 outr.flags = 3 | 0x0400; //name do not exist and auth
319 goto empty_packet;
320 }
321 if (type == REQ_A) { // return an address
322 struct in_addr a;
323 if (!inet_aton((char*)answstr, &a)) {//dotted dec to long conv
324 outr.flags = 1; /* Frmt err */
325 goto empty_packet;
326 }
327 memcpy(answstr, &a.s_addr, 4); // save before a disappears
328 outr.rlen = 4; // uint32_t IP
329 }
330 else
331 outr.rlen = strlen((char *)answstr) + 1; // a host name
332 outr.r = answstr; // 32 bit ip or a host name
333 outr.flags |= 0x0400; /* authority-bit */
334 // we have an answer
335 head->nansw = htons(1);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000336
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000337 // copy query block to answer block
338 len = answb - from;
339 memcpy(answb, from, len);
340 next += len;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000341
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000342 // and append answer rr
343 *(uint32_t *) next = htonl(ttl);
344 next += 4;
345 *(uint16_t *) next = htons(outr.rlen);
346 next += 2;
347 memcpy(next, (void *)answstr, outr.rlen);
348 next += outr.rlen;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000349
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000350 empty_packet:
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000351
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000352 flags = ntohs(head->flags);
353 // clear rcode and RA, set responsebit and our new flags
354 flags |= (outr.flags & 0xff80) | 0x8000;
355 head->flags = htons(flags);
356 head->nauth = head->nadd = htons(0);
357 head->nquer = htons(1);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000358
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000359 packet_len = next - (void *)buf;
360 return packet_len;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000361}
362
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000363/*
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000364 * Exit on signal
365 */
Rob Landleya6e131d2006-05-29 06:43:55 +0000366static void interrupt(int x)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000367{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000368 unlink(LOCK_FILE);
369 write(2, "interrupt exiting\n", 18);
370 exit(2);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000371}
372
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000373int dnsd_main(int argc, char **argv)
374{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000375 int udps;
376 uint16_t port = 53;
377 uint8_t buf[MAX_PACK_LEN];
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000378 char *listen_interface = "0.0.0.0";
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000379 char *sttl, *sport;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000380
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000381 getopt32(argc, argv, "i:c:t:p:dv", &listen_interface, &fileconf, &sttl, &sport);
382 //if (option_mask32 & 0x1) // -i
383 //if (option_mask32 & 0x2) // -c
384 if (option_mask32 & 0x4) // -t
385 if (!(ttl = atol(sttl)))
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000386 bb_show_usage();
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000387 if (option_mask32 & 0x8) // -p
388 if (!(port = atol(sport)))
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000389 bb_show_usage();
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000390
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000391 if (OPT_verbose) {
392 bb_info_msg("listen_interface: %s", listen_interface);
393 bb_info_msg("ttl: %d, port: %d", ttl, port);
394 bb_info_msg("fileconf: %s", fileconf);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000395 }
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000396
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000397 if (OPT_daemon)
Bernhard Reutner-Fischerc418d482006-05-31 10:19:51 +0000398#ifdef BB_NOMMU
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000399 /* reexec for vfork() do continue parent */
400 vfork_daemon_rexec(1, 0, argc, argv, "-d");
Bernhard Reutner-Fischerc418d482006-05-31 10:19:51 +0000401#else
Rob Landley86b4d642006-08-03 17:58:17 +0000402 xdaemon(1, 0);
Bernhard Reutner-Fischerc418d482006-05-31 10:19:51 +0000403#endif
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000404
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000405 dnsentryinit();
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000406
407 signal(SIGINT, interrupt);
408 signal(SIGPIPE, SIG_IGN);
409 signal(SIGHUP, SIG_IGN);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000410#ifdef SIGTSTP
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000411 signal(SIGTSTP, SIG_IGN);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000412#endif
413#ifdef SIGURG
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000414 signal(SIGURG, SIG_IGN);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000415#endif
416
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000417 udps = listen_socket(listen_interface, port);
418 if (udps < 0)
419 exit(1);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000420
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000421 while (1) {
422 fd_set fdset;
423 int r;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000424
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000425 FD_ZERO(&fdset);
426 FD_SET(udps, &fdset);
427 // Block until a message arrives
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000428 r = select(udps + 1, &fdset, NULL, NULL, NULL);
429 if (r < 0)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000430 bb_perror_msg_and_die("select error");
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000431 if (r == 0)
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000432 bb_perror_msg_and_die("select spurious return");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000433
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000434 /* Can this test ever be false? - yes */
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000435 if (FD_ISSET(udps, &fdset)) {
436 struct sockaddr_in from;
437 int fromlen = sizeof(from);
438 r = recvfrom(udps, buf, sizeof(buf), 0,
439 (struct sockaddr *)&from,
440 (void *)&fromlen);
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000441 if (OPT_verbose)
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000442 fprintf(stderr, "\n--- Got UDP ");
443 log_message(LOG_FILE, "\n--- Got UDP ");
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000444
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000445 if (r < 12 || r > 512) {
446 bb_error_msg("invalid packet size");
447 continue;
448 }
449 if (r > 0) {
450 r = process_packet(buf);
451 if (r > 0)
452 sendto(udps, buf,
453 r, 0, (struct sockaddr *)&from,
454 fromlen);
455 }
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000456 } // end if
457 } // end while
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000458 return 0;
459}