shithub: mc

Download patch

ref: 92dac618202997d1080597b377795f9cbba82742
parent: 52865cb63a6676c88e0c2703903741e03e623380
author: Ori Bernstein <ori@eigenstate.org>
date: Fri Jun 9 02:26:11 EDT 2017

Add timeouts to dns resolver.

--- a/lib/std/alloc.myr
+++ b/lib/std/alloc.myr
@@ -170,7 +170,7 @@
 	var phdr
 
 	phdr = ((p : size) - align(sizeof(slheader), Align) : slheader#)
-	std.iassert(phdr.magic == (0xdeadbeefbadf00d : size), "corrupt header")
+	std.iassert(phdr.magic == (0xdeadbeefbadf00d : size), "corrupt memory\n")
 	-> phdr.cap
 }
 
--- a/lib/std/resolve+posixy.myr
+++ b/lib/std/resolve+posixy.myr
@@ -40,6 +40,7 @@
 	;;
 
 	type resolveerr = union
+		`Timeout
 		`Badconn
 		`Badhost
 		`Badsrv
@@ -61,6 +62,7 @@
 
 const Hostfile = "/etc/hosts"
 const Resolvfile = "/etc/resolv.conf"
+const Timeout = 2_000
 
 var hostmap	: htab(byte[:], hostinfo)#
 var search	: byte[:][:]
@@ -316,14 +318,25 @@
 const rquery = {srv, id
 	var pktbuf : byte[1024]
 	var pkt
-	var n
+	var pfd
+	var r, n
 
-	n = sys.read(srv, pktbuf[:])
-	if n < 0
+	pfd = [
+		[.fd=srv, .events=sys.Pollin, .revents=0]
+	][:]
+	r = sys.poll(pfd[:], Timeout)
+	if r < 0
 		-> `Err `Badconn
+	elif r == 0
+		-> `Err `Timeout
+	else
+		n = sys.read(srv, pktbuf[:])
+		if n < 0
+			-> `Err `Badconn
+		;;
+		pkt = pktbuf[:n]
+		-> hosts(pkt, id)
 	;;
-	pkt = pktbuf[:n]
-	-> hosts(pkt, id)
 }
 
 const hosts = {pkt, id : uint16