diff -urN djbdns-1.05.orig/dnscache.c djbdns-1.05/dnscache.c --- djbdns-1.05.orig/dnscache.c 2009-03-03 11:32:02.000000000 +0100 +++ djbdns-1.05/dnscache.c 2009-03-03 11:32:32.000000000 +0100 @@ -24,6 +24,7 @@ #include "log.h" #include "okclient.h" #include "droproot.h" +#include "maxclient.h" long interface; @@ -68,7 +69,6 @@ struct interf *interhead = 0; -#define MAXUDP 200 static struct udpclient { struct query q; struct taia start; @@ -145,7 +145,6 @@ } } -#define MAXTCP 20 struct tcpclient { struct query q; struct taia start; diff -urN djbdns-1.05.orig/log.c djbdns-1.05/log.c --- djbdns-1.05.orig/log.c 2009-03-03 11:32:02.000000000 +0100 +++ djbdns-1.05/log.c 2009-03-03 11:32:32.000000000 +0100 @@ -149,6 +149,13 @@ line(); } +void log_tx_piggyback(const char *q, const char qtype[2], const char *control) +{ + string("txpb "); + logtype(qtype); space(); name(q); space(); name(control); + line(); +} + void log_cachedanswer(const char *q,const char type[2]) { string("cached "); logtype(type); space(); diff -urN djbdns-1.05.orig/log.h djbdns-1.05/log.h --- djbdns-1.05.orig/log.h 2009-03-03 11:32:02.000000000 +0100 +++ djbdns-1.05/log.h 2009-03-03 11:32:32.000000000 +0100 @@ -19,6 +19,7 @@ extern void log_cachedns(const char *,const char *); extern void log_tx(const char *,const char *,const char *,const char *,unsigned int); +extern void log_tx_piggyback(const char *,const char *,const char *); extern void log_nxdomain(const char *,const char *,unsigned int); extern void log_nodata(const char *,const char *,const char *,unsigned int); diff -urN djbdns-1.05.orig/Makefile djbdns-1.05/Makefile --- djbdns-1.05.orig/Makefile 2009-03-03 11:32:02.000000000 +0100 +++ djbdns-1.05/Makefile 2009-03-03 11:32:32.000000000 +0100 @@ -342,11 +342,11 @@ ./compile dns_txt.c dnscache: \ -load dnscache.o droproot.o okclient.o log.o cache.o query.o \ +load dnscache.o droproot.o okclient.o log.o cache.o query.o qmerge.o \ response.o dd.o roots.o iopause.o prot.o dns.a env.a alloc.a buffer.a \ libtai.a unix.a byte.a socket.lib ./load dnscache droproot.o okclient.o log.o cache.o \ - query.o response.o dd.o roots.o iopause.o prot.o dns.a \ + query.o qmerge.o response.o dd.o roots.o iopause.o prot.o dns.a \ env.a alloc.a buffer.a libtai.a unix.a byte.a `cat \ socket.lib` @@ -367,7 +367,7 @@ uint16.h uint64.h socket.h uint16.h dns.h stralloc.h gen_alloc.h \ iopause.h taia.h tai.h uint64.h taia.h taia.h byte.h roots.h fmt.h \ iopause.h query.h dns.h uint32.h alloc.h response.h uint32.h cache.h \ -uint32.h uint64.h ndelay.h log.h uint64.h okclient.h droproot.h +uint32.h uint64.h ndelay.h log.h uint64.h okclient.h droproot.h maxclient.h ./compile dnscache.c dnsfilter: \ @@ -745,11 +745,16 @@ compile qlog.c buffer.h qlog.h uint16.h ./compile qlog.c +qmerge.o: \ +compile qmerge.c qmerge.h dns.h stralloc.h gen_alloc.h iopause.h \ +taia.h tai.h uint64.h log.h maxclient.h + ./compile qmerge.c + query.o: \ compile query.c error.h roots.h log.h uint64.h case.h cache.h \ uint32.h uint64.h byte.h dns.h stralloc.h gen_alloc.h iopause.h \ taia.h tai.h uint64.h taia.h uint64.h uint32.h uint16.h dd.h alloc.h \ -response.h uint32.h query.h dns.h uint32.h +response.h uint32.h query.h dns.h uint32.h qmerge.h ./compile query.c random-ip: \ diff -urN djbdns-1.05.orig/maxclient.h djbdns-1.05/maxclient.h --- djbdns-1.05.orig/maxclient.h 1970-01-01 01:00:00.000000000 +0100 +++ djbdns-1.05/maxclient.h 2009-03-03 11:32:32.000000000 +0100 @@ -0,0 +1,7 @@ +#ifndef MAXCLIENT_H +#define MAXCLIENT_H + +#define MAXUDP 200 +#define MAXTCP 20 + +#endif /* MAXCLIENT_H */ diff -urN djbdns-1.05.orig/qmerge.c djbdns-1.05/qmerge.c --- djbdns-1.05.orig/qmerge.c 1970-01-01 01:00:00.000000000 +0100 +++ djbdns-1.05/qmerge.c 2009-03-03 11:32:32.000000000 +0100 @@ -0,0 +1,115 @@ +#include "qmerge.h" +#include "byte.h" +#include "log.h" +#include "maxclient.h" + +#define QMERGE_MAX (MAXUDP+MAXTCP) +struct qmerge inprogress[QMERGE_MAX]; + +static +int qmerge_key_init(struct qmerge_key *qmk, const char *q, const char qtype[2], + const char *control) +{ + if (!dns_domain_copy(&qmk->q, q)) return 0; + byte_copy(qmk->qtype, 2, qtype); + if (!dns_domain_copy(&qmk->control, control)) return 0; + return 1; +} + +static +int qmerge_key_equal(struct qmerge_key *a, struct qmerge_key *b) +{ + return + byte_equal(a->qtype, 2, b->qtype) && + dns_domain_equal(a->q, b->q) && + dns_domain_equal(a->control, b->control); +} + +static +void qmerge_key_free(struct qmerge_key *qmk) +{ + dns_domain_free(&qmk->q); + dns_domain_free(&qmk->control); +} + +void qmerge_free(struct qmerge **x) +{ + struct qmerge *qm; + + qm = *x; + *x = 0; + if (!qm || !qm->active) return; + + qm->active--; + if (!qm->active) { + qmerge_key_free(&qm->key); + dns_transmit_free(&qm->dt); + } +} + +int qmerge_start(struct qmerge **qm, const char servers[64], int flagrecursive, + const char *q, const char qtype[2], const char localip[4], + const char *control) +{ + struct qmerge_key k; + int i; + int r; + + qmerge_free(qm); + + byte_zero(&k, sizeof k); + if (!qmerge_key_init(&k, q, qtype, control)) return -1; + for (i = 0; i < QMERGE_MAX; i++) { + if (!inprogress[i].active) continue; + if (!qmerge_key_equal(&k, &inprogress[i].key)) continue; + log_tx_piggyback(q, qtype, control); + inprogress[i].active++; + *qm = &inprogress[i]; + qmerge_key_free(&k); + return 0; + } + + for (i = 0; i < QMERGE_MAX; i++) + if (!inprogress[i].active) + break; + if (i == QMERGE_MAX) return -1; + + log_tx(q, qtype, control, servers, 0); + r = dns_transmit_start(&inprogress[i].dt, servers, flagrecursive, q, qtype, localip); + if (r == -1) { qmerge_key_free(&k); return -1; } + inprogress[i].active++; + inprogress[i].state = 0; + qmerge_key_free(&inprogress[i].key); + byte_copy(&inprogress[i].key, sizeof k, &k); + *qm = &inprogress[i]; + return 0; +} + +void qmerge_io(struct qmerge *qm, iopause_fd *io, struct taia *deadline) +{ + if (qm->state == 0) { + dns_transmit_io(&qm->dt, io, deadline); + qm->state = 1; + } + else { + io->fd = -1; + io->events = 0; + } +} + +int qmerge_get(struct qmerge **x, const iopause_fd *io, const struct taia *when) +{ + int r; + struct qmerge *qm; + + qm = *x; + if (qm->state == -1) return -1; /* previous error */ + if (qm->state == 0) return 0; /* no packet */ + if (qm->state == 2) return 1; /* already got packet */ + + r = dns_transmit_get(&qm->dt, io, when); + if (r == -1) { qm->state = -1; return -1; } /* error */ + if (r == 0) { qm->state = 0; return 0; } /* must wait for i/o */ + if (r == 1) { qm->state = 2; return 1; } /* got packet */ + return -1; /* bug */ +} diff -urN djbdns-1.05.orig/qmerge.h djbdns-1.05/qmerge.h --- djbdns-1.05.orig/qmerge.h 1970-01-01 01:00:00.000000000 +0100 +++ djbdns-1.05/qmerge.h 2009-03-03 11:32:32.000000000 +0100 @@ -0,0 +1,24 @@ +#ifndef QMERGE_H +#define QMERGE_H + +#include "dns.h" + +struct qmerge_key { + char *q; + char qtype[2]; + char *control; +}; + +struct qmerge { + int active; + struct qmerge_key key; + struct dns_transmit dt; + int state; /* -1 = error, 0 = need io, 1 = need get, 2 = got packet */ +}; + +extern int qmerge_start(struct qmerge **,const char *,int,const char *,const char *,const char *,const char *); +extern void qmerge_io(struct qmerge *,iopause_fd *,struct taia *); +extern int qmerge_get(struct qmerge **,const iopause_fd *,const struct taia *); +extern void qmerge_free(struct qmerge **); + +#endif /* QMERGE_H */ diff -urN djbdns-1.05.orig/query.c djbdns-1.05/query.c --- djbdns-1.05.orig/query.c 2009-03-03 11:32:02.000000000 +0100 +++ djbdns-1.05/query.c 2009-03-03 11:39:26.000000000 +0100 @@ -84,7 +84,7 @@ int j; int k; - dns_transmit_free(&z->dt); + qmerge_free(&z->qm); for (j = 0;j < QUERY_MAXALIAS;++j) dns_domain_free(&z->alias[j]); for (j = 0;j < QUERY_MAXLEVEL;++j) { @@ -619,14 +619,8 @@ if (j == 256) goto SERVFAIL; dns_sortip6(z->servers[z->level],256); - if (z->level) { - log_tx(z->name[z->level],DNS_T_A,z->control[z->level],z->servers[z->level],z->level); - if (dns_transmit_start(&z->dt,z->servers[z->level],flagforwardonly,z->name[z->level],DNS_T_A,z->localip) == -1) goto DIE; - } - else { - log_tx(z->name[0],z->type,z->control[0],z->servers[0],0); - if (dns_transmit_start(&z->dt,z->servers[0],flagforwardonly,z->name[0],z->type,z->localip) == -1) goto DIE; - } + dtype = z->level ? DNS_T_A : z->type; + if (qmerge_start(&z->qm,z->servers[z->level],flagforwardonly,z->name[z->level],dtype,z->localip,z->control[z->level]) == -1) goto DIE; return 0; @@ -640,10 +634,10 @@ HAVEPACKET: if (++z->loop == 100) goto DIE; - buf = z->dt.packet; - len = z->dt.packetlen; + buf = z->qm->dt.packet; + len = z->qm->dt.packetlen; - whichserver = z->dt.servers + 16 * z->dt.curserver; + whichserver = z->qm->dt.servers + 16 * z->qm->dt.curserver; control = z->control[z->level]; d = z->name[z->level]; dtype = z->level ? DNS_T_A : z->type; @@ -1050,7 +1044,7 @@ int query_get(struct query *z,iopause_fd *x,struct taia *stamp) { - switch(dns_transmit_get(&z->dt,x,stamp)) { + switch(qmerge_get(&z->qm,x,stamp)) { case 1: return doit(z,1); case -1: @@ -1061,5 +1055,5 @@ void query_io(struct query *z,iopause_fd *x,struct taia *deadline) { - dns_transmit_io(&z->dt,x,deadline); + qmerge_io(z->qm,x,deadline); } diff -urN djbdns-1.05.orig/query.h djbdns-1.05/query.h --- djbdns-1.05.orig/query.h 2009-03-03 11:32:02.000000000 +0100 +++ djbdns-1.05/query.h 2009-03-03 11:32:32.000000000 +0100 @@ -1,7 +1,7 @@ #ifndef QUERY_H #define QUERY_H -#include "dns.h" +#include "qmerge.h" #include "uint32.h" #define QUERY_MAXLEVEL 5 @@ -21,7 +21,7 @@ uint32 scope_id; char type[2]; char class[2]; - struct dns_transmit dt; + struct qmerge *qm; } ; extern int query_start(struct query *,char *,char *,char *,char *,unsigned int);