2006-04-12 17:55:51 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
2006-01-25 14:19:11 +00:00
|
|
|
/*
|
|
|
|
* 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 18:09:26 +00:00
|
|
|
*
|
2006-01-25 14:19:11 +00:00
|
|
|
* 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 14:40:24 +00:00
|
|
|
* 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 14:19:11 +00:00
|
|
|
* need. [1] http://threading.2038bug.com/sheerdns/
|
2006-01-25 14:40:24 +00:00
|
|
|
*
|
2006-01-25 14:19:11 +00:00
|
|
|
* Some bugfix and minor changes was applied by Roberto A. Foglietta who made
|
|
|
|
* the first porting of oao' scdns to busybox also.
|
|
|
|
*/
|
|
|
|
|
2007-05-26 19:00:18 +00:00
|
|
|
#include "libbb.h"
|
2008-01-27 12:50:12 +00:00
|
|
|
#include <syslog.h>
|
2006-01-25 14:19:11 +00:00
|
|
|
|
2006-10-06 09:49:47 +00:00
|
|
|
//#define DEBUG 1
|
2007-01-12 14:57:37 +00:00
|
|
|
#define DEBUG 0
|
2006-10-06 09:49:47 +00:00
|
|
|
|
2006-03-10 19:22:06 +00:00
|
|
|
enum {
|
|
|
|
MAX_HOST_LEN = 16, // longest host name allowed is 15
|
|
|
|
IP_STRING_LEN = 18, // .xxx.xxx.xxx.xxx\0
|
2006-01-25 14:19:11 +00:00
|
|
|
|
|
|
|
//must be strlen('.in-addr.arpa') larger than IP_STRING_LEN
|
2006-03-10 19:22:06 +00:00
|
|
|
MAX_NAME_LEN = (IP_STRING_LEN + 13),
|
2006-01-25 14:19:11 +00:00
|
|
|
|
|
|
|
/* Cannot get bigger packets than 512 per RFC1035
|
|
|
|
In practice this can be set considerably smaller:
|
2006-01-25 14:40:24 +00:00
|
|
|
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 14:19:11 +00:00
|
|
|
2*querystring (2 MAX_NAME_LEN= 42B), all together 90 Byte
|
|
|
|
*/
|
2008-03-17 09:02:21 +00:00
|
|
|
MAX_PACK_LEN = 512,
|
2006-01-25 14:19:11 +00:00
|
|
|
|
2006-03-10 19:22:06 +00:00
|
|
|
DEFAULT_TTL = 30, // increase this when not testing?
|
2006-01-25 14:19:11 +00:00
|
|
|
|
2006-03-10 19:22:06 +00:00
|
|
|
REQ_A = 1,
|
|
|
|
REQ_PTR = 12
|
|
|
|
};
|
2006-01-25 14:19:11 +00:00
|
|
|
|
2006-01-25 14:40:24 +00:00
|
|
|
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 14:19:11 +00:00
|
|
|
};
|
|
|
|
struct dns_prop {
|
2006-01-25 14:40:24 +00:00
|
|
|
uint16_t type;
|
|
|
|
uint16_t class;
|
2006-01-25 14:19:11 +00:00
|
|
|
};
|
2006-01-25 14:40:24 +00:00
|
|
|
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 14:19:11 +00:00
|
|
|
char name[MAX_HOST_LEN];
|
|
|
|
};
|
|
|
|
|
2007-03-24 12:13:04 +00:00
|
|
|
static struct dns_entry *dnsentry;
|
2006-01-25 14:19:11 +00:00
|
|
|
static uint32_t ttl = DEFAULT_TTL;
|
|
|
|
|
2007-03-24 12:13:04 +00:00
|
|
|
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 14:19:11 +00:00
|
|
|
/*
|
2006-01-25 14:40:24 +00:00
|
|
|
* Convert host name from C-string to dns length/string.
|
2006-01-25 14:19:11 +00:00
|
|
|
*/
|
2006-05-29 06:43:55 +00:00
|
|
|
static void convname(char *a, uint8_t *q)
|
2006-01-25 14:19:11 +00:00
|
|
|
{
|
2006-01-25 14:40:24 +00:00
|
|
|
int i = (q[0] == '.') ? 0 : 1;
|
2006-10-06 09:49:47 +00:00
|
|
|
for (; i < MAX_HOST_LEN-1 && *q; i++, q++)
|
2006-01-25 14:19:11 +00:00
|
|
|
a[i] = tolower(*q);
|
2006-01-25 14:40:24 +00:00
|
|
|
a[0] = i - 1;
|
2006-01-25 14:19:11 +00:00
|
|
|
a[i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-05-31 10:19:51 +00:00
|
|
|
* Insert length of substrings instead of dots
|
2006-01-25 14:19:11 +00:00
|
|
|
*/
|
2006-05-29 06:43:55 +00:00
|
|
|
static void undot(uint8_t * rip)
|
2006-01-25 14:19:11 +00:00
|
|
|
{
|
2006-01-25 14:40:24 +00:00
|
|
|
int i = 0, s = 0;
|
2006-10-06 09:49:47 +00:00
|
|
|
while (rip[i])
|
|
|
|
i++;
|
|
|
|
for (--i; i >= 0; i--) {
|
|
|
|
if (rip[i] == '.') {
|
2006-01-25 14:19:11 +00:00
|
|
|
rip[i] = s;
|
|
|
|
s = 0;
|
|
|
|
} else s++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read hostname/IP records from file
|
|
|
|
*/
|
2006-10-06 09:49:47 +00:00
|
|
|
static void dnsentryinit(void)
|
2006-01-25 14:19:11 +00:00
|
|
|
{
|
2008-07-20 17:48:59 +00:00
|
|
|
parser_t *parser;
|
2006-01-25 14:40:24 +00:00
|
|
|
struct dns_entry *m, *prev;
|
2006-01-25 14:19:11 +00:00
|
|
|
|
2007-01-22 14:06:03 +00:00
|
|
|
prev = dnsentry = NULL;
|
2008-07-20 17:48:59 +00:00
|
|
|
parser = config_open(fileconf);
|
|
|
|
if (parser) {
|
|
|
|
char *token[2];
|
2008-07-21 23:05:26 +00:00
|
|
|
while (config_read(parser, token, 2, 2, "# \t", 0)) {
|
2008-07-20 17:48:59 +00:00
|
|
|
unsigned int a,b,c,d;
|
|
|
|
/*
|
|
|
|
* Assumes all host names are lower case only
|
|
|
|
* Hostnames with more than one label are not handled correctly.
|
|
|
|
* Presently the dot is copied into name without
|
|
|
|
* converting to a length/string substring for that label.
|
|
|
|
*/
|
2008-07-21 23:05:26 +00:00
|
|
|
// if (!token[1] || sscanf(token[1], ".%u.%u.%u.%u"+1, &a, &b, &c, &d) != 4)
|
|
|
|
if (sscanf(token[1], ".%u.%u.%u.%u"+1, &a, &b, &c, &d) != 4)
|
2008-07-20 17:48:59 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
m = xzalloc(sizeof(*m));
|
|
|
|
/*m->next = NULL;*/
|
|
|
|
sprintf(m->ip, ".%u.%u.%u.%u"+1, a, b, c, d);
|
|
|
|
sprintf(m->rip, ".%u.%u.%u.%u", d, c, b, a);
|
|
|
|
undot((uint8_t*)m->rip);
|
|
|
|
convname(m->name, (uint8_t*)token[0]);
|
|
|
|
|
|
|
|
if (OPT_verbose)
|
|
|
|
fprintf(stderr, "\tname:%s, ip:%s\n", &(m->name[1]), m->ip);
|
|
|
|
|
|
|
|
if (prev == NULL)
|
|
|
|
dnsentry = m;
|
|
|
|
else
|
|
|
|
prev->next = m;
|
|
|
|
prev = m;
|
|
|
|
}
|
|
|
|
config_close(parser);
|
2006-01-25 14:40:24 +00:00
|
|
|
}
|
2006-01-25 14:19:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-01-25 14:40:24 +00:00
|
|
|
* Look query up in dns records and return answer if found
|
2006-01-25 14:19:11 +00:00
|
|
|
* qs is the query string, first byte the string length
|
2006-01-25 14:40:24 +00:00
|
|
|
*/
|
2006-05-29 06:43:55 +00:00
|
|
|
static int table_lookup(uint16_t type, uint8_t * as, uint8_t * qs)
|
2006-01-25 14:19:11 +00:00
|
|
|
{
|
|
|
|
int i;
|
2007-03-24 12:13:04 +00:00
|
|
|
struct dns_entry *d = dnsentry;
|
2006-01-25 14:19:11 +00:00
|
|
|
|
2006-01-25 14:40:24 +00:00
|
|
|
do {
|
2007-01-20 01:47:44 +00:00
|
|
|
#if DEBUG
|
2006-01-25 14:40:24 +00:00
|
|
|
char *p,*q;
|
2006-01-25 14:19:11 +00:00
|
|
|
q = (char *)&(qs[1]);
|
|
|
|
p = &(d->name[1]);
|
2007-01-11 17:20:00 +00:00
|
|
|
fprintf(stderr, "\n%s: %d/%d p:%s q:%s %d",
|
2007-01-20 01:47:44 +00:00
|
|
|
__FUNCTION__, (int)strlen(p), (int)(d->name[0]),
|
|
|
|
p, q, (int)strlen(q));
|
2006-01-25 14:19:11 +00:00
|
|
|
#endif
|
2006-01-25 14:40:24 +00:00
|
|
|
if (type == REQ_A) { /* search by host name */
|
2006-10-06 09:49:47 +00:00
|
|
|
for (i = 1; i <= (int)(d->name[0]); i++)
|
|
|
|
if (tolower(qs[i]) != d->name[i])
|
|
|
|
break;
|
2008-05-22 17:41:01 +00:00
|
|
|
if (i > (int)(d->name[0]) ||
|
|
|
|
(d->name[0] == 1 && d->name[1] == '*')) {
|
2006-10-06 09:49:47 +00:00
|
|
|
strcpy((char *)as, d->ip);
|
2007-01-20 01:47:44 +00:00
|
|
|
#if DEBUG
|
2007-03-24 12:13:04 +00:00
|
|
|
fprintf(stderr, " OK as:%s\n", as);
|
2006-01-25 14:19:11 +00:00
|
|
|
#endif
|
2007-03-24 12:13:04 +00:00
|
|
|
return 0;
|
2006-10-06 09:49:47 +00:00
|
|
|
}
|
2007-03-24 12:13:04 +00:00
|
|
|
} else if (type == REQ_PTR) { /* search by IP-address */
|
2008-05-22 17:41:01 +00:00
|
|
|
if ((d->name[0] != 1 || d->name[1] != '*') &&
|
|
|
|
!strncmp((char*)&d->rip[1], (char*)&qs[1], strlen(d->rip)-1)) {
|
2006-01-25 14:40:24 +00:00
|
|
|
strcpy((char *)as, d->name);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2006-10-06 09:49:47 +00:00
|
|
|
d = d->next;
|
|
|
|
} while (d);
|
2006-01-25 14:40:24 +00:00
|
|
|
return -1;
|
2006-01-25 14:19:11 +00:00
|
|
|
}
|
|
|
|
|
2006-01-25 14:40:24 +00:00
|
|
|
/*
|
2006-01-25 14:19:11 +00:00
|
|
|
* Decode message and generate answer
|
|
|
|
*/
|
2008-03-17 09:02:21 +00:00
|
|
|
static int process_packet(uint8_t *buf)
|
2006-01-25 14:19:11 +00:00
|
|
|
{
|
2008-03-17 09:02:21 +00:00
|
|
|
uint8_t answstr[MAX_NAME_LEN + 1];
|
2006-01-25 14:40:24 +00:00
|
|
|
struct dns_head *head;
|
|
|
|
struct dns_prop *qprop;
|
2008-03-17 09:02:21 +00:00
|
|
|
uint8_t *from, *answb;
|
|
|
|
uint16_t outr_rlen;
|
|
|
|
uint16_t outr_flags;
|
2006-01-25 14:40:24 +00:00
|
|
|
uint16_t flags;
|
2008-03-17 09:02:21 +00:00
|
|
|
int lookup_result, type, packet_len;
|
|
|
|
int querystr_len;
|
2006-01-25 14:40:24 +00:00
|
|
|
|
|
|
|
answstr[0] = '\0';
|
|
|
|
|
|
|
|
head = (struct dns_head *)buf;
|
2007-03-24 12:13:04 +00:00
|
|
|
if (head->nquer == 0) {
|
|
|
|
bb_error_msg("no queries");
|
2007-03-24 13:09:07 +00:00
|
|
|
return -1;
|
2007-03-24 12:13:04 +00:00
|
|
|
}
|
2006-01-25 14:40:24 +00:00
|
|
|
|
2007-03-24 12:13:04 +00:00
|
|
|
if (head->flags & 0x8000) {
|
|
|
|
bb_error_msg("ignoring response packet");
|
2007-03-24 13:09:07 +00:00
|
|
|
return -1;
|
2007-03-24 12:13:04 +00:00
|
|
|
}
|
2006-01-25 14:40:24 +00:00
|
|
|
|
|
|
|
from = (void *)&head[1]; // start of query string
|
2008-03-17 09:02:21 +00:00
|
|
|
//FIXME: strlen of untrusted data??!
|
|
|
|
querystr_len = strlen((char *)from) + 1 + sizeof(struct dns_prop);
|
|
|
|
answb = from + querystr_len; // where to append answer block
|
2006-01-25 14:40:24 +00:00
|
|
|
|
2008-03-17 09:02:21 +00:00
|
|
|
outr_rlen = 0;
|
|
|
|
outr_flags = 0;
|
2006-01-25 14:40:24 +00:00
|
|
|
|
|
|
|
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 */ ) {
|
2008-03-17 09:02:21 +00:00
|
|
|
outr_flags = 4; /* not supported */
|
2006-01-25 14:40:24 +00:00
|
|
|
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 14:57:37 +00:00
|
|
|
bb_info_msg("%s", (char *)from);
|
2008-03-17 09:02:21 +00:00
|
|
|
lookup_result = table_lookup(type, answstr, from);
|
2006-01-25 14:40:24 +00:00
|
|
|
if (lookup_result != 0) {
|
2008-03-17 09:02:21 +00:00
|
|
|
outr_flags = 3 | 0x0400; // name do not exist and auth
|
2006-01-25 14:40:24 +00:00
|
|
|
goto empty_packet;
|
|
|
|
}
|
|
|
|
if (type == REQ_A) { // return an address
|
2008-03-17 09:02:21 +00:00
|
|
|
struct in_addr a; // NB! its "struct { unsigned __long__ s_addr; }"
|
|
|
|
uint32_t v32;
|
|
|
|
if (!inet_aton((char*)answstr, &a)) { //dotted dec to long conv
|
|
|
|
outr_flags = 1; /* Frmt err */
|
2006-01-25 14:40:24 +00:00
|
|
|
goto empty_packet;
|
|
|
|
}
|
2008-03-17 09:02:21 +00:00
|
|
|
v32 = a.s_addr; /* in case long != int */
|
|
|
|
memcpy(answstr, &v32, 4);
|
|
|
|
outr_rlen = 4; // uint32_t IP
|
2007-03-24 12:13:04 +00:00
|
|
|
} else
|
2008-03-17 09:02:21 +00:00
|
|
|
outr_rlen = strlen((char *)answstr) + 1; // a host name
|
|
|
|
outr_flags |= 0x0400; /* authority-bit */
|
2006-01-25 14:40:24 +00:00
|
|
|
// we have an answer
|
|
|
|
head->nansw = htons(1);
|
|
|
|
|
|
|
|
// copy query block to answer block
|
2008-03-17 09:02:21 +00:00
|
|
|
memcpy(answb, from, querystr_len);
|
|
|
|
answb += querystr_len;
|
2006-01-25 14:40:24 +00:00
|
|
|
|
|
|
|
// and append answer rr
|
2008-03-17 09:02:21 +00:00
|
|
|
// FIXME: unaligned accesses??
|
|
|
|
*(uint32_t *) answb = htonl(ttl);
|
|
|
|
answb += 4;
|
|
|
|
*(uint16_t *) answb = htons(outr_rlen);
|
|
|
|
answb += 2;
|
|
|
|
memcpy(answb, answstr, outr_rlen);
|
|
|
|
answb += outr_rlen;
|
2006-01-25 14:19:11 +00:00
|
|
|
|
2006-10-06 09:49:47 +00:00
|
|
|
empty_packet:
|
2006-01-25 14:19:11 +00:00
|
|
|
|
2006-01-25 14:40:24 +00:00
|
|
|
flags = ntohs(head->flags);
|
|
|
|
// clear rcode and RA, set responsebit and our new flags
|
2008-03-17 09:02:21 +00:00
|
|
|
flags |= (outr_flags & 0xff80) | 0x8000;
|
2006-01-25 14:40:24 +00:00
|
|
|
head->flags = htons(flags);
|
2008-03-17 09:02:21 +00:00
|
|
|
head->nauth = head->nadd = 0;
|
2006-01-25 14:40:24 +00:00
|
|
|
head->nquer = htons(1);
|
2006-01-25 14:19:11 +00:00
|
|
|
|
2008-03-17 09:02:21 +00:00
|
|
|
packet_len = answb - buf;
|
2006-01-25 14:40:24 +00:00
|
|
|
return packet_len;
|
2006-01-25 14:19:11 +00:00
|
|
|
}
|
|
|
|
|
2006-01-25 14:40:24 +00:00
|
|
|
/*
|
2006-01-25 14:19:11 +00:00
|
|
|
* Exit on signal
|
|
|
|
*/
|
2008-03-17 09:00:54 +00:00
|
|
|
static void interrupt(int sig)
|
2006-01-25 14:19:11 +00:00
|
|
|
{
|
2007-03-24 12:13:04 +00:00
|
|
|
/* unlink("/var/run/dnsd.lock"); */
|
2007-01-12 14:57:37 +00:00
|
|
|
bb_error_msg("interrupt, exiting\n");
|
2008-03-17 09:00:54 +00:00
|
|
|
kill_myself_with_sig(sig);
|
2006-01-25 14:19:11 +00:00
|
|
|
}
|
|
|
|
|
2007-10-11 10:05:36 +00:00
|
|
|
int dnsd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 09:18:54 +00:00
|
|
|
int dnsd_main(int argc UNUSED_PARAM, char **argv)
|
2006-01-25 14:19:11 +00:00
|
|
|
{
|
2007-03-24 12:13:04 +00:00
|
|
|
const char *listen_interface = "0.0.0.0";
|
2007-01-12 14:57:37 +00:00
|
|
|
char *sttl, *sport;
|
2008-03-17 09:02:21 +00:00
|
|
|
len_and_sockaddr *lsa, *from, *to;
|
|
|
|
unsigned lsa_size;
|
2006-01-25 14:40:24 +00:00
|
|
|
int udps;
|
|
|
|
uint16_t port = 53;
|
2008-03-17 09:02:21 +00:00
|
|
|
/* Paranoid sizing: querystring x2 + ttl + outr_rlen + answstr */
|
|
|
|
/* I'd rather see process_packet() fixed instead... */
|
|
|
|
uint8_t buf[MAX_PACK_LEN * 2 + 4 + 2 + (MAX_NAME_LEN+1)];
|
2006-01-25 14:19:11 +00:00
|
|
|
|
2007-08-18 15:32:12 +00:00
|
|
|
getopt32(argv, "i:c:t:p:dv", &listen_interface, &fileconf, &sttl, &sport);
|
2006-10-06 09:49:47 +00:00
|
|
|
//if (option_mask32 & 0x1) // -i
|
|
|
|
//if (option_mask32 & 0x2) // -c
|
|
|
|
if (option_mask32 & 0x4) // -t
|
2007-01-12 14:57:37 +00:00
|
|
|
ttl = xatou_range(sttl, 1, 0xffffffff);
|
2006-10-06 09:49:47 +00:00
|
|
|
if (option_mask32 & 0x8) // -p
|
2007-03-24 12:13:04 +00:00
|
|
|
port = xatou_range(sport, 1, 0xffff);
|
2006-01-25 14:40:24 +00:00
|
|
|
|
2006-10-06 09:49:47 +00:00
|
|
|
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 14:19:11 +00:00
|
|
|
}
|
2006-01-25 14:40:24 +00:00
|
|
|
|
2007-01-12 14:57:37 +00:00
|
|
|
if (OPT_daemon) {
|
2007-03-26 13:20:54 +00:00
|
|
|
bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv);
|
2007-03-24 14:06:51 +00:00
|
|
|
openlog(applet_name, LOG_PID, LOG_DAEMON);
|
2007-01-12 14:57:37 +00:00
|
|
|
logmode = LOGMODE_SYSLOG;
|
|
|
|
}
|
2006-01-25 14:19:11 +00:00
|
|
|
|
2006-10-06 09:49:47 +00:00
|
|
|
dnsentryinit();
|
2006-01-25 14:40:24 +00:00
|
|
|
|
|
|
|
signal(SIGINT, interrupt);
|
2008-02-16 22:58:56 +00:00
|
|
|
bb_signals(0
|
|
|
|
/* why? + (1 << SIGPIPE) */
|
|
|
|
+ (1 << SIGHUP)
|
2006-01-25 14:19:11 +00:00
|
|
|
#ifdef SIGTSTP
|
2008-02-16 22:58:56 +00:00
|
|
|
+ (1 << SIGTSTP)
|
2006-01-25 14:19:11 +00:00
|
|
|
#endif
|
|
|
|
#ifdef SIGURG
|
2008-02-16 22:58:56 +00:00
|
|
|
+ (1 << SIGURG)
|
2006-01-25 14:19:11 +00:00
|
|
|
#endif
|
2008-02-16 22:58:56 +00:00
|
|
|
, SIG_IGN);
|
2006-01-25 14:19:11 +00:00
|
|
|
|
2007-03-24 12:13:04 +00:00
|
|
|
lsa = xdotted2sockaddr(listen_interface, port);
|
2008-01-29 10:33:34 +00:00
|
|
|
udps = xsocket(lsa->u.sa.sa_family, SOCK_DGRAM, 0);
|
|
|
|
xbind(udps, &lsa->u.sa, lsa->len);
|
2008-03-17 09:02:21 +00:00
|
|
|
socket_want_pktinfo(udps); /* needed for recv_from_to to work */
|
|
|
|
lsa_size = LSA_LEN_SIZE + lsa->len;
|
|
|
|
from = xzalloc(lsa_size);
|
|
|
|
to = xzalloc(lsa_size);
|
|
|
|
|
2007-01-12 14:57:37 +00:00
|
|
|
bb_info_msg("Accepting UDP packets on %s",
|
2008-01-29 10:33:34 +00:00
|
|
|
xmalloc_sockaddr2dotted(&lsa->u.sa));
|
2006-01-25 14:19:11 +00:00
|
|
|
|
2006-01-25 14:40:24 +00:00
|
|
|
while (1) {
|
|
|
|
int r;
|
2008-03-17 09:02:21 +00:00
|
|
|
/* Try 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) */
|
|
|
|
memcpy(to, lsa, lsa_size);
|
|
|
|
r = recv_from_to(udps, buf, MAX_PACK_LEN + 1, 0, &from->u.sa, &to->u.sa, lsa->len);
|
|
|
|
if (r < 12 || r > MAX_PACK_LEN) {
|
2007-03-24 12:13:04 +00:00
|
|
|
bb_error_msg("invalid packet size");
|
|
|
|
continue;
|
2007-01-12 14:57:37 +00:00
|
|
|
}
|
2008-03-17 09:02:21 +00:00
|
|
|
if (OPT_verbose)
|
|
|
|
bb_info_msg("Got UDP packet");
|
|
|
|
buf[r] = '\0'; /* paranoia */
|
2007-03-24 12:13:04 +00:00
|
|
|
r = process_packet(buf);
|
|
|
|
if (r <= 0)
|
|
|
|
continue;
|
2008-05-22 17:41:01 +00:00
|
|
|
send_to_from(udps, buf, r, 0, &from->u.sa, &to->u.sa, lsa->len);
|
2007-01-12 14:57:37 +00:00
|
|
|
}
|
2007-03-24 13:09:07 +00:00
|
|
|
return 0;
|
2006-01-25 14:19:11 +00:00
|
|
|
}
|