git: 9front

Download patch

ref: 84b272878c38188286aa1368a211becde0ef591e
parent: cdbf59d2152db90da3797575e897ce1bb972c72c
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Sun Jun 14 17:31:44 EDT 2015

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).

--- a/sys/src/cmd/ndb/dns.h
+++ b/sys/src/cmd/ndb/dns.h
@@ -523,7 +523,6 @@
 /* dnserver.c */
 void	dnserver(DNSmsg*, DNSmsg*, Request*, uchar *, int);
 void	dnudpserver(char*);
-void	dntcpserver(char*);
 
 /* dnnotify.c */
 void	dnnotify(DNSmsg*, DNSmsg*, Request*);
--- a/sys/src/cmd/ndb/dnudpserver.c
+++ b/sys/src/cmd/ndb/dnudpserver.c
@@ -29,7 +29,7 @@
 	ulong	lastdial;
 };
 Forwtarg forwtarg[10];
-int currtarg;
+int forwtcount;
 
 static char *hmsg = "headers";
 
@@ -75,11 +75,11 @@
 {
 	Forwtarg *tp;
 
-	if (currtarg >= nelem(forwtarg)) {
+	if (forwtcount >= nelem(forwtarg)) {
 		dnslog("too many forwarding targets");
 		return -1;
 	}
-	tp = forwtarg + currtarg;
+	tp = forwtarg + forwtcount;
 	if (parseip(tp->addr, host) < 0) {
 		dnslog("can't parse ip %s", host);
 		return -1;
@@ -91,7 +91,7 @@
 
 	free(tp->host);
 	tp->host = estrdup(host);
-	currtarg++;
+	forwtcount++;
 	return 0;
 }
 
@@ -102,18 +102,18 @@
 static void
 redistrib(uchar *buf, int len)
 {
-	static uchar outpkt[Udphdrsize + Maxudp + 1024];
+	uchar save[Udphdrsize];
 	Forwtarg *tp;
 	Udphdr *uh;
 
-	assert(len <= sizeof outpkt);
-	memmove(outpkt, buf, len);
-	uh = (Udphdr *)outpkt;
-	for (tp = forwtarg; tp < forwtarg + currtarg; tp++)
+	memmove(save, buf, Udphdrsize);
+
+	uh = (Udphdr *)buf;
+	for (tp = forwtarg; tp < forwtarg + forwtcount; tp++)
 		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 */
-			if (write(tp->fd, outpkt, len) != len) {
+			if (write(tp->fd, buf, len) != len) {
 				close(tp->fd);
 				tp->fd = -1;
 			}
@@ -121,6 +121,8 @@
 			tp->lastdial = time(nil);
 			tp->fd = udpport(mntpt);
 		}
+
+	memmove(buf, save, Udphdrsize);
 }
 
 /*
@@ -180,7 +182,8 @@
 		if(len <= Udphdrsize)
 			goto restart;
 
-		redistrib(buf, len);
+		if(forwtcount > 0)
+			redistrib(buf, len);
 
 		uh = (Udphdr*)buf;
 		len -= Udphdrsize;
--