--- dnscache.c.orig Fri Dec 22 15:23:32 2000 +++ dnscache.c Fri Dec 22 15:24:28 2000 @@ -20,8 +20,21 @@ #include "okclient.h" #include "droproot.h" +#define FATAL "dnscache: fatal: " + char myipoutgoing[4]; -char myipincoming[4]; + +struct interf { + char ip[4]; + int udp53; + int tcp53; + iopause_fd *udp53io; + iopause_fd *tcp53io; + + struct interf *next; +} ; + +static struct interf *interhead; struct request { struct query q; @@ -30,6 +43,8 @@ char ip[4]; /* send response to this address */ uint16 port; /* send response to this port */ char id[2]; + int udp53; + int tcp53; int tcp; /* TCP socket, or -1 for UDP */ int tcpstate; char *tcpbuf; /* dynamically allocated */ @@ -45,8 +60,6 @@ int nextactive = MAXREQ - 1; int numtcp = 0; -int udp53; -int tcp53; char buf[1024]; static void request_free(struct request *x) @@ -98,7 +111,7 @@ response_id(x->id); if (response_len > 512) response_tc(); - socket_send4(udp53,response,response_len,x->ip,x->port); + socket_send4(x->udp53,response,response_len,x->ip,x->port); log_querydone(x - req,response_len); request_free(x); } @@ -249,7 +262,7 @@ x->io->events = (x->tcpstate > 0) ? IOPAUSE_READ : IOPAUSE_WRITE; } -static void udpquery(void) +static void udpquery(int fd) { struct request *x; int len; @@ -262,8 +275,9 @@ nextactive = 0; while (active[nextactive]); x = req + nextactive; + x->udp53 = fd; - len = socket_recv4(udp53,buf,sizeof buf,x->ip,&x->port); + len = socket_recv4(x->udp53,buf,sizeof buf,x->ip,&x->port); if (len == -1) return; if (len >= sizeof buf) return; if (x->port < 1024) if (x->port != 53) return; @@ -283,7 +297,7 @@ } } -static void tcpconnection(void) +static void tcpconnection(int fd) { struct request *x; struct taia now; @@ -295,8 +309,9 @@ nextactive = 0; while (active[nextactive]); x = req + nextactive; + x->tcp53 = fd; - x->tcp = socket_accept4(tcp53,x->ip,&x->port); + x->tcp = socket_accept4(x->tcp53,x->ip,&x->port); if (x->tcp == -1) return; if (x->port < 1024) if (x->port != 53) { close(x->tcp); return; } if (!okclient(x->ip)) { close(x->tcp); return; } @@ -313,17 +328,21 @@ log_conn(x - req,x->ip,x->port); } -iopause_fd io[3 + MAXREQ]; -iopause_fd *udp53io; -iopause_fd *tcp53io; +iopause_fd *io = 0; +int numio; static void doit(void) { int j; struct taia deadline; struct taia stamp; + struct interf *inter; int iolen; + io = (iopause_fd *) alloc((numio + 1 + MAXREQ) * sizeof(iopause_fd)); + if (!io) + strerr_die2sys(111,FATAL,"can't alloc io: "); + for (;;) { taia_now(&stamp); taia_uint(&deadline,120); @@ -331,18 +350,22 @@ iolen = 0; - udp53io = 0; - tcp53io = 0; - if (numactive < MAXREQ) { - udp53io = io + iolen++; - udp53io->fd = udp53; - udp53io->events = IOPAUSE_READ; - } - if ((numactive < MAXREQ) && (numtcp < MAXTCP)) { - tcp53io = io + iolen++; - tcp53io->fd = tcp53; - tcp53io->events = IOPAUSE_READ; - } + for (inter = interhead; inter != 0; inter = inter->next) { + inter->udp53io = 0; + inter->tcp53io = 0; + + if (numactive < MAXREQ) { + inter->udp53io = io + iolen++; + inter->udp53io->fd = inter->udp53; + inter->udp53io->events = IOPAUSE_READ; + } + + if ((numactive < MAXREQ) && (numtcp < MAXTCP)) { + inter->tcp53io = io + iolen++; + inter->tcp53io->fd = inter->tcp53; + inter->tcp53io->events = IOPAUSE_READ; + } + } for (j = 0;j < MAXREQ;++j) if (active[j]) { @@ -356,46 +379,83 @@ if (active[j]) handle(&req[j],&stamp); - if (udp53io) - if (udp53io->revents) - udpquery(); - - if (tcp53io) - if (tcp53io->revents) - tcpconnection(); + for (inter = interhead; inter != 0; inter = inter->next) { + if (inter->udp53io) + if (inter->udp53io->revents) + udpquery(inter->udp53); + + if (inter->tcp53io) + if (inter->tcp53io->revents) + tcpconnection(inter->tcp53); + } } } -#define FATAL "dnscache: fatal: " char seed[128]; main() { char *x; + int len; + int pos; + int oldpos; + char iptmp[4]; + struct interf *inter; + struct interf *itmp; unsigned long cachesize; x = env_get("IP"); if (!x) strerr_die2x(111,FATAL,"$IP not set"); - if (!ip4_scan(x,myipincoming)) - strerr_die3x(111,FATAL,"unable to parse IP address ",x); - udp53 = socket_udp(); - if (udp53 == -1) - strerr_die2sys(111,FATAL,"unable to create UDP socket: "); - if (socket_bind4_reuse(udp53,myipincoming,53) == -1) - strerr_die2sys(111,FATAL,"unable to bind UDP socket: "); - - tcp53 = socket_tcp(); - if (tcp53 == -1) - strerr_die2sys(111,FATAL,"unable to create TCP socket: "); - if (socket_bind4_reuse(tcp53,myipincoming,53) == -1) - strerr_die2sys(111,FATAL,"unable to bind TCP socket: "); + len = str_len(x); + pos = 0; + oldpos = 0; + + while (pos < len) { + if (pos) oldpos = pos + 1; + pos = oldpos + str_chr(x + oldpos,'/'); + x[pos] = 0; + + if (!ip4_scan(x + oldpos,iptmp)) + strerr_die3x(111,FATAL,"unable to parse IP address ",x + oldpos); + + inter = (struct interf *) alloc(sizeof(struct interf)); + + if (interhead == 0) interhead = inter; + else if (interhead->next == 0) interhead->next = inter; + else { + for (itmp = interhead; itmp->next != 0; itmp = itmp->next); + itmp->next = inter; + } + + inter->next = 0; + + inter->udp53 = socket_udp(); + if (inter->udp53 == -1) + strerr_die2sys(111,FATAL,"unable to create UDP socket: "); + if (socket_bind4_reuse(inter->udp53,iptmp,53) == -1) + strerr_die2sys(111,FATAL,"unable to bind UDP socket: "); + + inter->tcp53 = socket_tcp(); + if (inter->tcp53 == -1) + strerr_die2sys(111,FATAL,"unable to create TCP socket: "); + if (socket_bind4_reuse(inter->tcp53,iptmp,53) == -1) + strerr_die2sys(111,FATAL,"unable to bind TCP socket: "); + + numio++; + log_listen(iptmp); + } + + if (interhead == 0) + strerr_die2x(111,FATAL,"no interfaces to listen on"); droproot(FATAL); - socket_tryreservein(udp53,131072); + for (inter = interhead; inter != 0; inter = inter->next) { + socket_tryreservein(inter->udp53,131072); + } byte_zero(seed,sizeof seed); read(0,seed,sizeof seed); @@ -418,8 +478,10 @@ if (!roots_init()) strerr_die2sys(111,FATAL,"unable to read servers: "); - if (socket_listen(tcp53,20) == -1) - strerr_die2sys(111,FATAL,"unable to listen on TCP socket: "); + for (inter = interhead; inter != 0; inter = inter->next) { + if (socket_listen(inter->tcp53,20) == -1) + strerr_die2sys(111,FATAL,"unable to listen on TCP socket: "); + } log_startup(); doit(); --- log.c.orig Fri Dec 22 15:23:41 2000 +++ log.c Fri Dec 22 14:10:24 2000 @@ -88,6 +88,13 @@ } } +void log_listen(char addr[4]) +{ + string("listening on "); + ip(addr); + line(); +} + void log_startup(void) { string("starting");