2006-04-12 23:25:51 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2006-01-25 19:49:11 +05:30
|
|
|
/*
|
|
|
|
* Mini DNS server implementation for busybox
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 Roberto A. Foglietta (me@roberto.foglietta.name)
|
|
|
|
* Copyright (C) 2005 Odd Arild Olsen (oao at fibula dot no)
|
|
|
|
* Copyright (C) 2003 Paul Sheer
|
2006-04-12 23:39:26 +05:30
|
|
|
*
|
2006-01-25 19:49:11 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
|
|
|
*
|
|
|
|
* Odd Arild Olsen started out with the sheerdns [1] of Paul Sheer and rewrote
|
2006-01-25 20:10:24 +05:30
|
|
|
* it into a shape which I believe is both easier to understand and maintain.
|
|
|
|
* I also reused the input buffer for output and removed services he did not
|
2006-01-25 19:49:11 +05:30
|
|
|
* need. [1] http://threading.2038bug.com/sheerdns/
|
2006-01-25 20:10:24 +05:30
|
|
|
*
|
2006-01-25 19:49:11 +05:30
|
|
|
* Some bugfix and minor changes was applied by Roberto A. Foglietta who made
|
|
|
|
* the first porting of oao' scdns to busybox also.
|
|
|
|
*/
|
|
|
|
|
2007-05-27 00:30:18 +05:30
|
|
|
#include "libbb.h"
|
2008-01-27 18:20:12 +05:30
|
|
|
#include <syslog.h>
|
2006-01-25 19:49:11 +05:30
|
|
|
|
2006-10-06 15:19:47 +05:30
|
|
|
//#define DEBUG 1
|
2007-01-12 20:27:37 +05:30
|
|
|
#define DEBUG 0
|
2006-10-06 15:19:47 +05:30
|
|
|
|
2006-03-11 00:52:06 +05:30
|
|
|
enum {
|
|
|
|
MAX_HOST_LEN = 16, // longest host name allowed is 15
|
|
|
|
IP_STRING_LEN = 18, // .xxx.xxx.xxx.xxx\0
|
2006-01-25 19:49:11 +05:30
|
|
|
|
|
|
|
//must be strlen('.in-addr.arpa') larger than IP_STRING_LEN
|
2006-03-11 00:52:06 +05:30
|
|
|
MAX_NAME_LEN = (IP_STRING_LEN + 13),
|
2006-01-25 19:49:11 +05:30
|
|
|
|
|
|
|
/* Cannot get bigger packets than 512 per RFC1035
|
|
|
|
In practice this can be set considerably smaller:
|
2006-01-25 20:10:24 +05:30
|
|
|
Length of response packet is header (12B) + 2*type(4B) + 2*class(4B) +
|
|
|
|
ttl(4B) + rlen(2B) + r (MAX_NAME_LEN =21B) +
|
2006-01-25 19:49:11 +05:30
|
|
|
2*querystring (2 MAX_NAME_LEN= 42B), all together 90 Byte
|
|
|
|
*/
|
2006-03-11 00:52:06 +05:30
|
|
|
MAX_PACK_LEN = 512 + 1,
|
2006-01-25 19:49:11 +05:30
|
|
|
|
2006-03-11 00:52:06 +05:30
|
|
|
DEFAULT_TTL = 30, // increase this when not testing?
|
2006-01-25 19:49:11 +05:30
|
|
|
|
2006-03-11 00:52:06 +05:30
|
|
|
REQ_A = 1,
|
|
|
|
REQ_PTR = 12
|
|
|
|
};
|
2006-01-25 19:49:11 +05:30
|
|
|
|
2006-01-25 20:10:24 +05:30
|
|
|
struct dns_repl { // resource record, add 0 or 1 to accepted dns_msg in resp
|
|
|
|
uint16_t rlen;
|
|
|
|
uint8_t *r; // resource
|
|
|
|
uint16_t flags;
|
2006-01-25 19:49:11 +05:30
|
|
|
};
|
|
|
|
|
2006-01-25 20:10:24 +05:30
|
|
|
struct dns_head { // the message from client and first part of response mag
|
|
|
|
uint16_t id;
|
|
|
|
uint16_t flags;
|
|
|
|
uint16_t nquer; // accepts 0
|
|
|
|
uint16_t nansw; // 1 in response
|
|
|
|
uint16_t nauth; // 0
|
|
|
|
uint16_t nadd; // 0
|
2006-01-25 19:49:11 +05:30
|
|
|
};
|
|
|
|
struct dns_prop {
|
2006-01-25 20:10:24 +05:30
|
|
|
uint16_t type;
|
|
|
|
uint16_t class;
|
2006-01-25 19:49:11 +05:30
|
|
|
};
|
2006-01-25 20:10:24 +05:30
|
|
|
struct dns_entry { // element of known name, ip address and reversed ip address
|
|
|
|
struct dns_entry *next;
|
|
|
|
char ip[IP_STRING_LEN]; // dotted decimal IP
|
|
|
|
char rip[IP_STRING_LEN]; // length decimal reversed IP
|
2006-01-25 19:49:11 +05:30
|
|
|
char name[MAX_HOST_LEN];
|
|
|
|
};
|
|
|
|
|
2007-03-24 17:43:04 +05:30
|
|
|
static struct dns_entry *dnsentry;
|
2006-01-25 19:49:11 +05:30
|
|
|
static uint32_t ttl = DEFAULT_TTL;
|
|
|
|
|
2007-03-24 17:43:04 +05:30
|
|
|
static const char *fileconf = "/etc/dnsd.conf";
|
|
|
|
|
|
|
|
// Must match getopt32 call
|
|
|
|
#define OPT_daemon (option_mask32 & 0x10)
|
|
|
|
#define OPT_verbose (option_mask32 & 0x20)
|
|
|
|
|
|
|
|
|
2006-01-25 19:49:11 +05:30
|
|
|
/*
|
2006-01-25 20:10:24 +05:30
|
|
|
* Convert host name from C-string to dns length/string.
|
2006-01-25 19:49:11 +05:30
|
|
|
*/
|
2006-05-29 12:13:55 +05:30
|
|
|
static void convname(char *a, uint8_t *q)
|
2006-01-25 19:49:11 +05:30
|
|
|
{
|
2006-01-25 20:10:24 +05:30
|
|
|
int i = (q[0] == '.') ? 0 : 1;
|
2006-10-06 15:19:47 +05:30
|
|
|
for (; i < MAX_HOST_LEN-1 && *q; i++, q++)
|
2006-01-25 19:49:11 +05:30
|
|
|
a[i] = tolower(*q);
|
2006-01-25 20:10:24 +05:30
|
|
|
a[0] = i - 1;
|
2006-01-25 19:49:11 +05:30
|
|
|
a[i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-05-31 15:49:51 +05:30
|
|
|
* Insert length of substrings instead of dots
|
2006-01-25 19:49:11 +05:30
|
|
|
*/
|
2006-05-29 12:13:55 +05:30
|
|
|
static void undot(uint8_t * rip)
|
2006-01-25 19:49:11 +05:30
|
|
|
{
|
2006-01-25 20:10:24 +05:30
|
|
|
int i = 0, s = 0;
|
2006-10-06 15:19:47 +05:30
|
|
|
while (rip[i])
|
|
|
|
i++;
|
|
|
|
for (--i; i >= 0; i--) {
|
|
|
|
if (rip[i] == '.') {
|
2006-01-25 19:49:11 +05:30
|
|
|
rip[i] = s;
|
|
|
|
s = 0;
|
|
|
|
} else s++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-01-25 20:10:24 +05:30
|
|
|
/*
|
2006-01-25 19:49:11 +05:30
|
|
|
* Read one line of hostname/IP from file
|
|
|
|
* Returns 0 for each valid entry read, -1 at EOF
|
|
|
|
* Assumes all host names are lower case only
|
2007-01-22 19:36:03 +05:30
|
|
|
* Hostnames with more than one label are not handled correctly.
|
2006-01-25 19:49:11 +05:30
|
|
|
* Presently the dot is copied into name without
|
|
|
|
* converting to a length/string substring for that label.
|
|
|
|
*/
|
2006-10-06 15:19:47 +05:30
|
|
|
static int getfileentry(FILE * fp, struct dns_entry *s)
|
2006-01-25 19:49:11 +05:30
|
|
|
{
|
|
|
|
unsigned int a,b,c,d;
|
2007-01-22 19:36:03 +05:30
|
|
|
char *line, *r, *name;
|
2006-01-25 19:49:11 +05:30
|
|
|
|
2006-10-06 15:19:47 +05:30
|
|
|
restart:
|
2007-01-22 19:36:03 +05:30
|
|
|
line = r = xmalloc_fgets(fp);
|
2006-10-06 15:19:47 +05:30
|
|
|
if (!r)
|
2006-01-25 19:49:11 +05:30
|
|
|
return -1;
|
2006-10-06 15:19:47 +05:30
|
|
|
while (*r == ' ' || *r == '\t') {
|
2006-01-25 19:49:11 +05:30
|
|
|
r++;
|
2007-01-22 19:36:03 +05:30
|
|
|
if (!*r || *r == '#' || *r == '\n') {
|
|
|
|
free(line);
|
2006-01-25 19:49:11 +05:30
|
|
|
goto restart; /* skipping empty/blank and commented lines */
|
2007-01-22 19:36:03 +05:30
|
|
|
}
|
2006-01-25 19:49:11 +05:30
|
|
|
}
|
|
|
|
name = r;
|
2006-10-06 15:19:47 +05:30
|
|
|
while (*r != ' ' && *r != '\t')
|
2006-01-25 19:49:11 +05:30
|
|
|
r++;
|
2007-01-22 19:36:03 +05:30
|
|
|
*r++ = '\0';
|
2007-03-24 17:43:04 +05:30
|
|
|
if (sscanf(r, ".%u.%u.%u.%u"+1, &a, &b, &c, &d) != 4) {
|
2007-01-22 19:36:03 +05:30
|
|
|
free(line);
|
2006-10-06 15:19:47 +05:30
|
|
|
goto restart; /* skipping wrong lines */
|
2007-01-22 19:36:03 +05:30
|
|
|
}
|
2006-01-25 19:49:11 +05:30
|
|
|
|
2007-03-24 17:43:04 +05:30
|
|
|
sprintf(s->ip, ".%u.%u.%u.%u"+1, a, b, c, d);
|
2006-10-06 15:19:47 +05:30
|
|
|
sprintf(s->rip, ".%u.%u.%u.%u", d, c, b, a);
|
2006-01-25 19:49:11 +05:30
|
|
|
undot((uint8_t*)s->rip);
|
2007-01-22 19:36:03 +05:30
|
|
|
convname(s->name, (uint8_t*)name);
|
2006-01-25 20:10:24 +05:30
|
|
|
|
2006-10-06 15:19:47 +05:30
|
|
|
if (OPT_verbose)
|
|
|
|
fprintf(stderr, "\tname:%s, ip:%s\n", &(s->name[1]),s->ip);
|
2006-01-25 19:49:11 +05:30
|
|
|
|
2007-01-22 19:36:03 +05:30
|
|
|
free(line);
|
2006-10-06 15:19:47 +05:30
|
|
|
return 0;
|
2006-01-25 19:49:11 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read hostname/IP records from file
|
|
|
|
*/
|
2006-10-06 15:19:47 +05:30
|
|
|
static void dnsentryinit(void)
|
2006-01-25 19:49:11 +05:30
|
|
|
{
|
2006-01-25 20:10:24 +05:30
|
|
|
FILE *fp;
|
|
|
|
struct dns_entry *m, *prev;
|
2006-01-25 19:49:11 +05:30
|
|
|
|
2007-01-22 19:36:03 +05:30
|
|
|
prev = dnsentry = NULL;
|
2006-08-03 23:28:17 +05:30
|
|
|
fp = xfopen(fileconf, "r");
|
2006-01-25 19:49:11 +05:30
|
|
|
|
2006-01-25 20:10:24 +05:30
|
|
|
while (1) {
|
2007-01-22 19:36:03 +05:30
|
|
|
m = xzalloc(sizeof(*m));
|
|
|
|
/*m->next = NULL;*/
|
2006-10-06 15:19:47 +05:30
|
|
|
if (getfileentry(fp, m))
|
2006-01-25 20:10:24 +05:30
|
|
|
break;
|
|
|
|
|
|
|
|
if (prev == NULL)
|
|
|
|
dnsentry = m;
|
|
|
|
else
|
|
|
|
prev->next = m;
|
|
|
|
prev = m;
|
|
|
|
}
|
|
|
|
fclose(fp);
|
2006-01-25 19:49:11 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-01-25 20:10:24 +05:30
|
|
|
* Look query up in dns records and return answer if found
|
2006-01-25 19:49:11 +05:30
|
|
|
* qs is the query string, first byte the string length
|
2006-01-25 20:10:24 +05:30
|
|
|
*/
|
2006-05-29 12:13:55 +05:30
|
|
|
static int table_lookup(uint16_t type, uint8_t * as, uint8_t * qs)
|
2006-01-25 19:49:11 +05:30
|
|
|
{
|
|
|
|
int i;
|
2007-03-24 17:43:04 +05:30
|
|
|
struct dns_entry *d = dnsentry;
|
2006-01-25 19:49:11 +05:30
|
|
|
|
2006-01-25 20:10:24 +05:30
|
|
|
do {
|
2007-01-20 07:17:44 +05:30
|
|
|
#if DEBUG
|
2006-01-25 20:10:24 +05:30
|
|
|
char *p,*q;
|
2006-01-25 19:49:11 +05:30
|
|
|
q = (char *)&(qs[1]);
|
|
|
|
p = &(d->name[1]);
|
2007-01-11 22:50:00 +05:30
|
|
|
fprintf(stderr, "\n%s: %d/%d p:%s q:%s %d",
|
2007-01-20 07:17:44 +05:30
|
|
|
__FUNCTION__, (int)strlen(p), (int)(d->name[0]),
|
|
|
|
p, q, (int)strlen(q));
|
2006-01-25 19:49:11 +05:30
|
|
|
#endif
|
2006-01-25 20:10:24 +05:30
|
|
|
if (type == REQ_A) { /* search by host name */
|
2006-10-06 15:19:47 +05:30
|
|
|
for (i = 1; i <= (int)(d->name[0]); i++)
|
|
|
|
if (tolower(qs[i]) != d->name[i])
|
|
|
|
break;
|
|
|
|
if (i > (int)(d->name[0])) {
|
|
|
|
strcpy((char *)as, d->ip);
|
2007-01-20 07:17:44 +05:30
|
|
|
#if DEBUG
|
2007-03-24 17:43:04 +05:30
|
|
|
fprintf(stderr, " OK as:%s\n", as);
|
2006-01-25 19:49:11 +05:30
|
|
|
#endif
|
2007-03-24 17:43:04 +05:30
|
|
|
return 0;
|
2006-10-06 15:19:47 +05:30
|
|
|
}
|
2007-03-24 17:43:04 +05:30
|
|
|
} else if (type == REQ_PTR) { /* search by IP-address */
|
2006-01-25 19:49:11 +05:30
|
|
|
if (!strncmp((char*)&d->rip[1], (char*)&qs[1], strlen(d->rip)-1)) {
|
2006-01-25 20:10:24 +05:30
|
|
|
strcpy((char *)as, d->name);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2006-10-06 15:19:47 +05:30
|
|
|
d = d->next;
|
|
|
|
} while (d);
|
2006-01-25 20:10:24 +05:30
|
|
|
return -1;
|
2006-01-25 19:49:11 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-01-25 20:10:24 +05:30
|
|
|
/*
|
2006-01-25 19:49:11 +05:30
|
|
|
* Decode message and generate answer
|
|
|
|
*/
|
2006-05-29 12:13:55 +05:30
|
|
|
static int process_packet(uint8_t * buf)
|
2006-01-25 19:49:11 +05:30
|
|
|
{
|
2006-01-25 20:10:24 +05:30
|
|
|
struct dns_head *head;
|
|
|
|
struct dns_prop *qprop;
|
|
|
|
struct dns_repl outr;
|
|
|
|
void *next, *from, *answb;
|
|
|
|
|
|
|
|
uint8_t answstr[MAX_NAME_LEN + 1];
|
|
|
|
int lookup_result, type, len, packet_len;
|
|
|
|
uint16_t flags;
|
|
|
|
|
|
|
|
answstr[0] = '\0';
|
|
|
|
|
|
|
|
head = (struct dns_head *)buf;
|
2007-03-24 17:43:04 +05:30
|
|
|
if (head->nquer == 0) {
|
|
|
|
bb_error_msg("no queries");
|
2007-03-24 18:39:07 +05:30
|
|
|
return -1;
|
2007-03-24 17:43:04 +05:30
|
|
|
}
|
2006-01-25 20:10:24 +05:30
|
|
|
|
2007-03-24 17:43:04 +05:30
|
|
|
if (head->flags & 0x8000) {
|
|
|
|
bb_error_msg("ignoring response packet");
|
2007-03-24 18:39:07 +05:30
|
|
|
return -1;
|
2007-03-24 17:43:04 +05:30
|
|
|
}
|
2006-01-25 20:10:24 +05:30
|
|
|
|
|
|
|
from = (void *)&head[1]; // start of query string
|
2006-10-06 15:19:47 +05:30
|
|
|
next = answb = from + strlen((char *)from) + 1 + sizeof(struct dns_prop); // where to append answer block
|
2006-01-25 20:10:24 +05:30
|
|
|
|
|
|
|
outr.rlen = 0; // may change later
|
|
|
|
outr.r = NULL;
|
|
|
|
outr.flags = 0;
|
|
|
|
|
|
|
|
qprop = (struct dns_prop *)(answb - 4);
|
|
|
|
type = ntohs(qprop->type);
|
|
|
|
|
|
|
|
// only let REQ_A and REQ_PTR pass
|
|
|
|
if (!(type == REQ_A || type == REQ_PTR)) {
|
|
|
|
goto empty_packet; /* we can't handle the query type */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ntohs(qprop->class) != 1 /* class INET */ ) {
|
|
|
|
outr.flags = 4; /* not supported */
|
|
|
|
goto empty_packet;
|
|
|
|
}
|
|
|
|
/* we only support standard queries */
|
|
|
|
|
|
|
|
if ((ntohs(head->flags) & 0x7800) != 0)
|
|
|
|
goto empty_packet;
|
|
|
|
|
|
|
|
// We have a standard query
|
2007-01-12 20:27:37 +05:30
|
|
|
bb_info_msg("%s", (char *)from);
|
2006-10-06 15:19:47 +05:30
|
|
|
lookup_result = table_lookup(type, answstr, (uint8_t*)from);
|
2006-01-25 20:10:24 +05:30
|
|
|
if (lookup_result != 0) {
|
|
|
|
outr.flags = 3 | 0x0400; //name do not exist and auth
|
|
|
|
goto empty_packet;
|
|
|
|
}
|
|
|
|
if (type == REQ_A) { // return an address
|
|
|
|
struct in_addr a;
|
|
|
|
if (!inet_aton((char*)answstr, &a)) {//dotted dec to long conv
|
|
|
|
outr.flags = 1; /* Frmt err */
|
|
|
|
goto empty_packet;
|
|
|
|
}
|
|
|
|
memcpy(answstr, &a.s_addr, 4); // save before a disappears
|
|
|
|
outr.rlen = 4; // uint32_t IP
|
2007-03-24 17:43:04 +05:30
|
|
|
} else
|
2006-01-25 20:10:24 +05:30
|
|
|
outr.rlen = strlen((char *)answstr) + 1; // a host name
|
|
|
|
outr.r = answstr; // 32 bit ip or a host name
|
|
|
|
outr.flags |= 0x0400; /* authority-bit */
|
|
|
|
// we have an answer
|
|
|
|
head->nansw = htons(1);
|
|
|
|
|
|
|
|
// copy query block to answer block
|
|
|
|
len = answb - from;
|
|
|
|
memcpy(answb, from, len);
|
|
|
|
next += len;
|
|
|
|
|
|
|
|
// and append answer rr
|
|
|
|
*(uint32_t *) next = htonl(ttl);
|
|
|
|
next += 4;
|
|
|
|
*(uint16_t *) next = htons(outr.rlen);
|
|
|
|
next += 2;
|
|
|
|
memcpy(next, (void *)answstr, outr.rlen);
|
|
|
|
next += outr.rlen;
|
2006-01-25 19:49:11 +05:30
|
|
|
|
2006-10-06 15:19:47 +05:30
|
|
|
empty_packet:
|
2006-01-25 19:49:11 +05:30
|
|
|
|
2006-01-25 20:10:24 +05:30
|
|
|
flags = ntohs(head->flags);
|
|
|
|
// clear rcode and RA, set responsebit and our new flags
|
|
|
|
flags |= (outr.flags & 0xff80) | 0x8000;
|
|
|
|
head->flags = htons(flags);
|
|
|
|
head->nauth = head->nadd = htons(0);
|
|
|
|
head->nquer = htons(1);
|
2006-01-25 19:49:11 +05:30
|
|
|
|
2007-03-24 17:43:04 +05:30
|
|
|
packet_len = (uint8_t *)next - buf;
|
2006-01-25 20:10:24 +05:30
|
|
|
return packet_len;
|
2006-01-25 19:49:11 +05:30
|
|
|
}
|
|
|
|
|
2006-01-25 20:10:24 +05:30
|
|
|
/*
|
2006-01-25 19:49:11 +05:30
|
|
|
* Exit on signal
|
|
|
|
*/
|
2006-05-29 12:13:55 +05:30
|
|
|
static void interrupt(int x)
|
2006-01-25 19:49:11 +05:30
|
|
|
{
|
2007-03-24 17:43:04 +05:30
|
|
|
/* unlink("/var/run/dnsd.lock"); */
|
2007-01-12 20:27:37 +05:30
|
|
|
bb_error_msg("interrupt, exiting\n");
|
2006-01-25 20:10:24 +05:30
|
|
|
exit(2);
|
2006-01-25 19:49:11 +05:30
|
|
|
}
|
|
|
|
|
2007-10-11 15:35:36 +05:30
|
|
|
int dnsd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2006-01-25 19:49:11 +05:30
|
|
|
int dnsd_main(int argc, char **argv)
|
|
|
|
{
|
2007-03-24 17:43:04 +05:30
|
|
|
const char *listen_interface = "0.0.0.0";
|
2007-01-12 20:27:37 +05:30
|
|
|
char *sttl, *sport;
|
|
|
|
len_and_sockaddr *lsa;
|
2006-01-25 20:10:24 +05:30
|
|
|
int udps;
|
|
|
|
uint16_t port = 53;
|
|
|
|
uint8_t buf[MAX_PACK_LEN];
|
2006-01-25 19:49:11 +05:30
|
|
|
|
2007-08-18 21:02:12 +05:30
|
|
|
getopt32(argv, "i:c:t:p:dv", &listen_interface, &fileconf, &sttl, &sport);
|
2006-10-06 15:19:47 +05:30
|
|
|
//if (option_mask32 & 0x1) // -i
|
|
|
|
//if (option_mask32 & 0x2) // -c
|
|
|
|
if (option_mask32 & 0x4) // -t
|
2007-01-12 20:27:37 +05:30
|
|
|
ttl = xatou_range(sttl, 1, 0xffffffff);
|
2006-10-06 15:19:47 +05:30
|
|
|
if (option_mask32 & 0x8) // -p
|
2007-03-24 17:43:04 +05:30
|
|
|
port = xatou_range(sport, 1, 0xffff);
|
2006-01-25 20:10:24 +05:30
|
|
|
|
2006-10-06 15:19:47 +05:30
|
|
|
if (OPT_verbose) {
|
|
|
|
bb_info_msg("listen_interface: %s", listen_interface);
|
|
|
|
bb_info_msg("ttl: %d, port: %d", ttl, port);
|
|
|
|
bb_info_msg("fileconf: %s", fileconf);
|
2006-01-25 19:49:11 +05:30
|
|
|
}
|
2006-01-25 20:10:24 +05:30
|
|
|
|
2007-01-12 20:27:37 +05:30
|
|
|
if (OPT_daemon) {
|
2007-03-26 18:50:54 +05:30
|
|
|
bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv);
|
2007-03-24 19:36:51 +05:30
|
|
|
openlog(applet_name, LOG_PID, LOG_DAEMON);
|
2007-01-12 20:27:37 +05:30
|
|
|
logmode = LOGMODE_SYSLOG;
|
|
|
|
}
|
2006-01-25 19:49:11 +05:30
|
|
|
|
2006-10-06 15:19:47 +05:30
|
|
|
dnsentryinit();
|
2006-01-25 20:10:24 +05:30
|
|
|
|
|
|
|
signal(SIGINT, interrupt);
|
2007-03-24 17:43:04 +05:30
|
|
|
/* why? signal(SIGPIPE, SIG_IGN); */
|
2006-01-25 20:10:24 +05:30
|
|
|
signal(SIGHUP, SIG_IGN);
|
2006-01-25 19:49:11 +05:30
|
|
|
#ifdef SIGTSTP
|
2006-01-25 20:10:24 +05:30
|
|
|
signal(SIGTSTP, SIG_IGN);
|
2006-01-25 19:49:11 +05:30
|
|
|
#endif
|
|
|
|
#ifdef SIGURG
|
2006-01-25 20:10:24 +05:30
|
|
|
signal(SIGURG, SIG_IGN);
|
2006-01-25 19:49:11 +05:30
|
|
|
#endif
|
|
|
|
|
2007-03-24 17:43:04 +05:30
|
|
|
lsa = xdotted2sockaddr(listen_interface, port);
|
2008-01-29 16:03:34 +05:30
|
|
|
udps = xsocket(lsa->u.sa.sa_family, SOCK_DGRAM, 0);
|
|
|
|
xbind(udps, &lsa->u.sa, lsa->len);
|
2007-03-24 17:43:04 +05:30
|
|
|
/* xlisten(udps, 50); - ?!! DGRAM sockets are never listened on I think? */
|
2007-01-12 20:27:37 +05:30
|
|
|
bb_info_msg("Accepting UDP packets on %s",
|
2008-01-29 16:03:34 +05:30
|
|
|
xmalloc_sockaddr2dotted(&lsa->u.sa));
|
2006-01-25 19:49:11 +05:30
|
|
|
|
2006-01-25 20:10:24 +05:30
|
|
|
while (1) {
|
|
|
|
int r;
|
2007-03-24 17:43:04 +05:30
|
|
|
socklen_t fromlen = lsa->len;
|
2007-01-12 20:27:37 +05:30
|
|
|
// FIXME: need to get *DEST* address (to which of our addresses
|
|
|
|
// this query was directed), and reply from the same address.
|
|
|
|
// Or else we can exhibit usual UDP ugliness:
|
|
|
|
// [ip1.multihomed.ip2] <= query to ip1 <= peer
|
|
|
|
// [ip1.multihomed.ip2] => reply from ip2 => peer (confused)
|
2008-01-29 16:03:34 +05:30
|
|
|
r = recvfrom(udps, buf, sizeof(buf), 0, &lsa->u.sa, &fromlen);
|
2007-03-24 17:43:04 +05:30
|
|
|
if (OPT_verbose)
|
|
|
|
bb_info_msg("Got UDP packet");
|
|
|
|
if (r < 12 || r > 512) {
|
|
|
|
bb_error_msg("invalid packet size");
|
|
|
|
continue;
|
2007-01-12 20:27:37 +05:30
|
|
|
}
|
2007-03-24 17:43:04 +05:30
|
|
|
r = process_packet(buf);
|
|
|
|
if (r <= 0)
|
|
|
|
continue;
|
2008-01-29 16:03:34 +05:30
|
|
|
sendto(udps, buf, r, 0, &lsa->u.sa, fromlen);
|
2007-01-12 20:27:37 +05:30
|
|
|
}
|
2007-03-24 18:39:07 +05:30
|
|
|
return 0;
|
2006-01-25 19:49:11 +05:30
|
|
|
}
|