shithub: qk2

Download patch

ref: 11eabae817604b898a02bc4db0b0aaba23d9648d
parent: e685f6329cbe2e8ec1bb5df5769e240b1ad06b98
author: qwx <>
date: Fri Jun 1 08:07:25 EDT 2018

udp: minor improvements

--- a/udp.c
+++ b/udp.c
@@ -104,6 +104,28 @@
 	return s;
 }
 
+static void
+getip(char *a, int len)
+{
+	int fd, n;
+	char buf[128], *f[3];
+
+	if((fd = open("/net/cs", ORDWR)) < 0)
+		sysfatal("open: %r");
+	snprint(buf, sizeof buf, "udp!%s!0", a);
+	n = strlen(buf);
+	if(write(fd, buf, n) != n)
+		sysfatal("translating %s: %r", a);
+	seek(fd, 0, 0);
+	if((n = read(fd, buf, sizeof(buf)-1)) <= 0)
+		sysfatal("reading cs tables: %r");
+	buf[n] = 0;
+	close(fd);
+	if(getfields(buf, f, 3, 1, " !") < 2)
+		sysfatal("bad cs entry %s", buf);
+	strncpy(a, f[1], len);
+}
+
 /*
 =============
 NET_StringToAdr
@@ -120,7 +142,6 @@
 {
 	int i;
 	char s[256], *p, *pp;
-	Ndbtuple *t, *nt;
 
 	if(!strcmp(addr, "localhost")){
 		memset(a, 0, sizeof *a);
@@ -137,18 +158,8 @@
 		a->port = BigShort(atoi(p));
 	}
 
-	if((t = dnsquery(nil, s, "ip")) == nil){
-		fprint(2, "NET_StringToAdr:dnsquery %s: %r\n", s);
-		return 0;
-	}
+	getip(s, sizeof(s)-1);
 
-	for(nt = t; nt != nil; nt = nt->entry)
-		if(!strcmp(nt->attr, "ip")){
-			strncpy(s, nt->val, sizeof(s)-1);
-			break;
-		}
-	ndbfree(t);
-
 	/* FIXMEGASHIT */
 	for(i = 0, pp = s; i < IPv4addrlen; i++){
 		if((p = strchr(pp, '.')) != nil)
@@ -296,12 +307,11 @@
 		if((n = read(fd, m.buf, sizeof m.buf)) <= 0)
 			break;
 		m.n = n;
-		if(send(c, &m) < 0)
-			sysfatal("uproc:send: %r");
-		if(nbsend(fuckchan, nil) < 0)
-			sysfatal("uproc:nbsend; %r");
+		if(send(c, &m) < 0 || nbsend(fuckchan, nil) < 0){
+			fprint(2, "dproc: %r");
+			break;
+		}
 	}
-	fprint(2, "dproc %d: %r\n", threadpid(threadid()));
 	cndel(me);
 }
 
@@ -322,7 +332,7 @@
 		fd = cldfd;
 	for(;;){
 		if((n = read(fd, udpbuf, sizeof udpbuf)) <= 0)
-			sysfatal("uproc:read: %r");
+			break;
 		memcpy(&h, udpbuf, Hdrsz);
 
 		memcpy(u, h.raddr, IPaddrlen);
@@ -341,10 +351,10 @@
 			m.n = n - Hdrsz;
 			memcpy(m.buf, udpbuf+Hdrsz, m.n);
 		}
-		if(send(c, &m) < 0)
-			sysfatal("uproc:send: %r");
-		if(nbsend(fuckchan, nil) < 0)
-			sysfatal("uproc:nbsend: %r");
+		if(send(c, &m) < 0 || nbsend(fuckchan, nil) < 0){
+			fprint(2, "uproc: %r");
+			break;
+		}
 	}
 }
 
@@ -434,7 +444,7 @@
 			p = cnins(fd, addr, u, nil, src);
 			qunlock(&cnlock);
 
-			if(proccreate(dproc, p, 8196) < 0)
+			if(proccreate(dproc, p, 8192) < 0)
 				sysfatal("NET_SendPacket:proccreate: %r");
 		}
 		if(write(fd, data, length) != length)
@@ -507,7 +517,7 @@
 NET_Config(qboolean multiplayer)
 {
 	if(!multiplayer){	/* shut down existing udp connections */
-		threadkillgrp(THnet);
+		threadintgrp(THnet);
 		cnnuke();
 		if(udpchan != nil){
 			chanfree(udpchan);