ndb/dns: cleanup forwarding code (redistrib())

instead of copying the whole packet, just save the
udp header and restore it aftwards. dont call redistrib()
when there are no forwards (this should be almost always
the case).
This commit is contained in:
cinap_lenrek 2015-06-14 21:31:44 +02:00
parent ffd294e0da
commit 65db705481
2 changed files with 15 additions and 13 deletions

View file

@ -523,7 +523,6 @@ void initdnsmsg(DNSmsg *mp, RR *rp, int flags, ushort reqno);
/* dnserver.c */ /* dnserver.c */
void dnserver(DNSmsg*, DNSmsg*, Request*, uchar *, int); void dnserver(DNSmsg*, DNSmsg*, Request*, uchar *, int);
void dnudpserver(char*); void dnudpserver(char*);
void dntcpserver(char*);
/* dnnotify.c */ /* dnnotify.c */
void dnnotify(DNSmsg*, DNSmsg*, Request*); void dnnotify(DNSmsg*, DNSmsg*, Request*);

View file

@ -29,7 +29,7 @@ struct Forwtarg {
ulong lastdial; ulong lastdial;
}; };
Forwtarg forwtarg[10]; Forwtarg forwtarg[10];
int currtarg; int forwtcount;
static char *hmsg = "headers"; static char *hmsg = "headers";
@ -75,11 +75,11 @@ addforwtarg(char *host)
{ {
Forwtarg *tp; Forwtarg *tp;
if (currtarg >= nelem(forwtarg)) { if (forwtcount >= nelem(forwtarg)) {
dnslog("too many forwarding targets"); dnslog("too many forwarding targets");
return -1; return -1;
} }
tp = forwtarg + currtarg; tp = forwtarg + forwtcount;
if (parseip(tp->addr, host) < 0) { if (parseip(tp->addr, host) < 0) {
dnslog("can't parse ip %s", host); dnslog("can't parse ip %s", host);
return -1; return -1;
@ -91,7 +91,7 @@ addforwtarg(char *host)
free(tp->host); free(tp->host);
tp->host = estrdup(host); tp->host = estrdup(host);
currtarg++; forwtcount++;
return 0; return 0;
} }
@ -102,18 +102,18 @@ addforwtarg(char *host)
static void static void
redistrib(uchar *buf, int len) redistrib(uchar *buf, int len)
{ {
static uchar outpkt[Udphdrsize + Maxudp + 1024]; uchar save[Udphdrsize];
Forwtarg *tp; Forwtarg *tp;
Udphdr *uh; Udphdr *uh;
assert(len <= sizeof outpkt); memmove(save, buf, Udphdrsize);
memmove(outpkt, buf, len);
uh = (Udphdr *)outpkt; uh = (Udphdr *)buf;
for (tp = forwtarg; tp < forwtarg + currtarg; tp++) for (tp = forwtarg; tp < forwtarg + forwtcount; tp++)
if (tp->fd > 0) { if (tp->fd > 0) {
memmove(outpkt, tp->addr, sizeof tp->addr); memmove(uh->raddr, tp->addr, sizeof tp->addr);
hnputs(uh->rport, 53); /* dns port */ hnputs(uh->rport, 53); /* dns port */
if (write(tp->fd, outpkt, len) != len) { if (write(tp->fd, buf, len) != len) {
close(tp->fd); close(tp->fd);
tp->fd = -1; tp->fd = -1;
} }
@ -121,6 +121,8 @@ redistrib(uchar *buf, int len)
tp->lastdial = time(nil); tp->lastdial = time(nil);
tp->fd = udpport(mntpt); tp->fd = udpport(mntpt);
} }
memmove(buf, save, Udphdrsize);
} }
/* /*
@ -180,7 +182,8 @@ restart:
if(len <= Udphdrsize) if(len <= Udphdrsize)
goto restart; goto restart;
redistrib(buf, len); if(forwtcount > 0)
redistrib(buf, len);
uh = (Udphdr*)buf; uh = (Udphdr*)buf;
len -= Udphdrsize; len -= Udphdrsize;