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.
|
|
|
|
*/
|
|
|
|
|
2006-04-03 22:09:31 +05:30
|
|
|
#include "busybox.h"
|
2006-01-25 19:49:11 +05:30
|
|
|
|
|
|
|
static char *fileconf = "/etc/dnsd.conf";
|
|
|
|
#define LOCK_FILE "/var/run/dnsd.lock"
|
|
|
|
#define LOG_FILE "/var/log/dnsd.log"
|
|
|
|
|
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];
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct dns_entry *dnsentry = NULL;
|
|
|
|
static int daemonmode = 0;
|
|
|
|
static uint32_t ttl = DEFAULT_TTL;
|
|
|
|
|
|
|
|
/*
|
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-01-25 19:49:11 +05:30
|
|
|
for(; i < MAX_HOST_LEN-1 && *q; i++, q++)
|
|
|
|
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-01-25 19:49:11 +05:30
|
|
|
while(rip[i]) i++;
|
|
|
|
for(--i; i >= 0; i--) {
|
|
|
|
if(rip[i] == '.') {
|
|
|
|
rip[i] = s;
|
|
|
|
s = 0;
|
|
|
|
} else s++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-01-25 20:10:24 +05:30
|
|
|
/*
|
2006-01-25 19:49:11 +05:30
|
|
|
* Append message to log file
|
|
|
|
*/
|
2006-05-29 12:13:55 +05:30
|
|
|
static void log_message(char *filename, char *message)
|
2006-01-25 19:49:11 +05:30
|
|
|
{
|
2006-01-25 20:10:24 +05:30
|
|
|
FILE *logfile;
|
|
|
|
if (!daemonmode)
|
|
|
|
return;
|
|
|
|
logfile = fopen(filename, "a");
|
|
|
|
if (!logfile)
|
|
|
|
return;
|
|
|
|
fprintf(logfile, "%s\n", message);
|
|
|
|
fclose(logfile);
|
2006-01-25 19:49:11 +05:30
|
|
|
}
|
|
|
|
|
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
|
|
|
|
* Hostnames with more than one label is not handled correctly.
|
|
|
|
* Presently the dot is copied into name without
|
|
|
|
* converting to a length/string substring for that label.
|
|
|
|
*/
|
|
|
|
|
2006-05-29 12:13:55 +05:30
|
|
|
static int getfileentry(FILE * fp, struct dns_entry *s, int verb)
|
2006-01-25 19:49:11 +05:30
|
|
|
{
|
|
|
|
unsigned int a,b,c,d;
|
2006-01-25 20:10:24 +05:30
|
|
|
char *r, *name;
|
2006-01-25 19:49:11 +05:30
|
|
|
|
|
|
|
restart:
|
|
|
|
if(!(r = bb_get_line_from_file(fp)))
|
|
|
|
return -1;
|
|
|
|
while(*r == ' ' || *r == '\t') {
|
|
|
|
r++;
|
2006-01-25 20:10:24 +05:30
|
|
|
if(!*r || *r == '#' || *r == '\n')
|
2006-01-25 19:49:11 +05:30
|
|
|
goto restart; /* skipping empty/blank and commented lines */
|
|
|
|
}
|
|
|
|
name = r;
|
|
|
|
while(*r != ' ' && *r != '\t')
|
|
|
|
r++;
|
|
|
|
*r++ = 0;
|
|
|
|
if(sscanf(r,"%u.%u.%u.%u",&a,&b,&c,&d) != 4)
|
|
|
|
goto restart; /* skipping wrong lines */
|
|
|
|
|
|
|
|
sprintf(s->ip,"%u.%u.%u.%u",a,b,c,d);
|
|
|
|
sprintf(s->rip,".%u.%u.%u.%u",d,c,b,a);
|
|
|
|
undot((uint8_t*)s->rip);
|
|
|
|
convname(s->name,(uint8_t*)name);
|
2006-01-25 20:10:24 +05:30
|
|
|
|
|
|
|
if(verb)
|
2006-01-25 19:49:11 +05:30
|
|
|
fprintf(stderr,"\tname:%s, ip:%s\n",&(s->name[1]),s->ip);
|
|
|
|
|
|
|
|
return 0; /* warningkiller */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read hostname/IP records from file
|
|
|
|
*/
|
2006-05-29 12:13:55 +05:30
|
|
|
static void dnsentryinit(int verb)
|
2006-01-25 19:49:11 +05:30
|
|
|
{
|
2006-01-25 20:10:24 +05:30
|
|
|
FILE *fp;
|
|
|
|
struct dns_entry *m, *prev;
|
|
|
|
prev = dnsentry = NULL;
|
2006-01-25 19:49:11 +05:30
|
|
|
|
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) {
|
2006-05-29 12:13:55 +05:30
|
|
|
m = xmalloc(sizeof(struct dns_entry));
|
2006-01-25 19:49:11 +05:30
|
|
|
|
2006-01-25 20:10:24 +05:30
|
|
|
m->next = NULL;
|
|
|
|
if (getfileentry(fp, m, verb))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (prev == NULL)
|
|
|
|
dnsentry = m;
|
|
|
|
else
|
|
|
|
prev->next = m;
|
|
|
|
prev = m;
|
|
|
|
}
|
|
|
|
fclose(fp);
|
2006-01-25 19:49:11 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up UDP socket
|
|
|
|
*/
|
2006-05-29 12:13:55 +05:30
|
|
|
static int listen_socket(char *iface_addr, int listen_port)
|
2006-01-25 19:49:11 +05:30
|
|
|
{
|
2006-01-25 20:10:24 +05:30
|
|
|
struct sockaddr_in a;
|
|
|
|
char msg[100];
|
|
|
|
int s;
|
|
|
|
int yes = 1;
|
2006-08-03 23:28:17 +05:30
|
|
|
s = xsocket(PF_INET, SOCK_DGRAM, 0);
|
2006-01-25 19:49:11 +05:30
|
|
|
#ifdef SO_REUSEADDR
|
2006-01-25 20:10:24 +05:30
|
|
|
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&yes, sizeof(yes)) < 0)
|
|
|
|
bb_perror_msg_and_die("setsockopt() failed");
|
2006-01-25 19:49:11 +05:30
|
|
|
#endif
|
2006-01-25 20:10:24 +05:30
|
|
|
memset(&a, 0, sizeof(a));
|
|
|
|
a.sin_port = htons(listen_port);
|
|
|
|
a.sin_family = AF_INET;
|
|
|
|
if (!inet_aton(iface_addr, &a.sin_addr))
|
|
|
|
bb_perror_msg_and_die("bad iface address");
|
2006-08-03 23:28:17 +05:30
|
|
|
xbind(s, (struct sockaddr *)&a, sizeof(a));
|
2006-08-16 03:07:55 +05:30
|
|
|
xlisten(s, 50);
|
2006-01-25 20:10:24 +05:30
|
|
|
sprintf(msg, "accepting UDP packets on addr:port %s:%d\n",
|
|
|
|
iface_addr, (int)listen_port);
|
|
|
|
log_message(LOG_FILE, msg);
|
|
|
|
return s;
|
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;
|
2006-01-25 20:10:24 +05:30
|
|
|
struct dns_entry *d=dnsentry;
|
2006-01-25 19:49:11 +05:30
|
|
|
|
2006-01-25 20:10:24 +05:30
|
|
|
do {
|
2006-01-25 19:49:11 +05:30
|
|
|
#ifdef 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]);
|
2006-01-25 20:10:24 +05:30
|
|
|
fprintf(stderr, "\ntest: %d <%s> <%s> %d", strlen(p), p, q, 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-01-25 19:49:11 +05:30
|
|
|
for(i = 1; i <= (int)(d->name[0]); i++)
|
|
|
|
if(tolower(qs[i]) != d->name[i])
|
|
|
|
continue;
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, " OK");
|
|
|
|
#endif
|
|
|
|
strcpy((char *)as, d->ip);
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, " %s ", as);
|
|
|
|
#endif
|
|
|
|
return 0;
|
2006-01-25 20:10:24 +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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while ((d = d->next) != NULL);
|
|
|
|
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-01-25 20:10:24 +05:30
|
|
|
#define eret(s) do { fprintf (stderr, "%s\n", s); return -1; } while (0)
|
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;
|
|
|
|
if (head->nquer == 0)
|
|
|
|
eret("no queries");
|
|
|
|
|
|
|
|
if ((head->flags & 0x8000))
|
|
|
|
eret("ignoring response packet");
|
|
|
|
|
|
|
|
from = (void *)&head[1]; // start of query string
|
|
|
|
next = answb = from + strlen((char *)&head[1]) + 1 + sizeof(struct dns_prop); // where to append answer block
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
log_message(LOG_FILE, (char *)head);
|
|
|
|
lookup_result = table_lookup(type, answstr, (uint8_t*)(&head[1]));
|
|
|
|
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
|
|
|
|
}
|
|
|
|
else
|
|
|
|
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
|
|
|
|
|
|
|
empty_packet:
|
|
|
|
|
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
|
|
|
|
2006-01-25 20:10:24 +05:30
|
|
|
packet_len = next - (void *)buf;
|
|
|
|
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
|
|
|
{
|
2006-01-25 20:10:24 +05:30
|
|
|
unlink(LOCK_FILE);
|
|
|
|
write(2, "interrupt exiting\n", 18);
|
|
|
|
exit(2);
|
2006-01-25 19:49:11 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
#define is_daemon() (flags&16)
|
|
|
|
#define is_verbose() (flags&32)
|
|
|
|
//#define DEBUG 1
|
|
|
|
|
|
|
|
int dnsd_main(int argc, char **argv)
|
|
|
|
{
|
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
|
|
|
unsigned long flags = 0;
|
2006-01-25 20:10:24 +05:30
|
|
|
char *listen_interface = "0.0.0.0";
|
2006-01-25 19:49:11 +05:30
|
|
|
char *sttl=NULL, *sport=NULL;
|
|
|
|
|
2006-01-25 20:10:24 +05:30
|
|
|
if(argc > 1)
|
2006-01-25 19:49:11 +05:30
|
|
|
flags = bb_getopt_ulflags(argc, argv, "i:c:t:p:dv", &listen_interface, &fileconf, &sttl, &sport);
|
|
|
|
if(sttl)
|
|
|
|
if(!(ttl = atol(sttl)))
|
|
|
|
bb_show_usage();
|
|
|
|
if(sport)
|
|
|
|
if(!(port = atol(sport)))
|
|
|
|
bb_show_usage();
|
2006-01-25 20:10:24 +05:30
|
|
|
|
2006-01-25 19:49:11 +05:30
|
|
|
if(is_verbose()) {
|
|
|
|
fprintf(stderr,"listen_interface: %s\n", listen_interface);
|
|
|
|
fprintf(stderr,"ttl: %d, port: %d\n", ttl, port);
|
|
|
|
fprintf(stderr,"fileconf: %s\n", fileconf);
|
|
|
|
}
|
2006-01-25 20:10:24 +05:30
|
|
|
|
|
|
|
if(is_daemon())
|
2006-05-31 15:49:51 +05:30
|
|
|
#ifdef BB_NOMMU
|
2006-01-25 19:49:11 +05:30
|
|
|
/* reexec for vfork() do continue parent */
|
|
|
|
vfork_daemon_rexec(1, 0, argc, argv, "-d");
|
2006-05-31 15:49:51 +05:30
|
|
|
#else
|
2006-08-03 23:28:17 +05:30
|
|
|
xdaemon(1, 0);
|
2006-05-31 15:49:51 +05:30
|
|
|
#endif
|
2006-01-25 19:49:11 +05:30
|
|
|
|
2006-01-25 20:10:24 +05:30
|
|
|
dnsentryinit(is_verbose());
|
|
|
|
|
|
|
|
signal(SIGINT, interrupt);
|
|
|
|
signal(SIGPIPE, SIG_IGN);
|
|
|
|
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
|
|
|
|
|
2006-01-25 20:10:24 +05:30
|
|
|
udps = listen_socket(listen_interface, port);
|
|
|
|
if (udps < 0)
|
|
|
|
exit(1);
|
2006-01-25 19:49:11 +05:30
|
|
|
|
2006-01-25 20:10:24 +05:30
|
|
|
while (1) {
|
|
|
|
fd_set fdset;
|
|
|
|
int r;
|
2006-01-25 19:49:11 +05:30
|
|
|
|
2006-01-25 20:10:24 +05:30
|
|
|
FD_ZERO(&fdset);
|
|
|
|
FD_SET(udps, &fdset);
|
|
|
|
// Block until a message arrives
|
|
|
|
if((r = select(udps + 1, &fdset, NULL, NULL, NULL)) < 0)
|
2006-01-25 19:49:11 +05:30
|
|
|
bb_perror_msg_and_die("select error");
|
2006-01-25 20:10:24 +05:30
|
|
|
else
|
|
|
|
if(r == 0)
|
|
|
|
bb_perror_msg_and_die("select spurious return");
|
|
|
|
|
|
|
|
/* Can this test ever be false? */
|
|
|
|
if (FD_ISSET(udps, &fdset)) {
|
|
|
|
struct sockaddr_in from;
|
|
|
|
int fromlen = sizeof(from);
|
|
|
|
r = recvfrom(udps, buf, sizeof(buf), 0,
|
|
|
|
(struct sockaddr *)&from,
|
|
|
|
(void *)&fromlen);
|
|
|
|
if(is_verbose())
|
|
|
|
fprintf(stderr, "\n--- Got UDP ");
|
|
|
|
log_message(LOG_FILE, "\n--- Got UDP ");
|
|
|
|
|
|
|
|
if (r < 12 || r > 512) {
|
|
|
|
bb_error_msg("invalid packet size");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (r > 0) {
|
|
|
|
r = process_packet(buf);
|
|
|
|
if (r > 0)
|
|
|
|
sendto(udps, buf,
|
|
|
|
r, 0, (struct sockaddr *)&from,
|
|
|
|
fromlen);
|
|
|
|
}
|
|
|
|
} // end if
|
|
|
|
} // end while
|
2006-01-25 19:49:11 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|