shithub: riscv

Download patch

ref: ad7390dda820db424821b19c572a44b4cc0838e8
parent: 03ced8cca1c2c2911ba64e937af8436658d126d5
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Wed May 23 15:44:12 EDT 2018

ndb/dnsdebug: handle .ip6.arpa names

--- a/sys/src/cmd/ndb/dnsdebug.c
+++ b/sys/src/cmd/ndb/dnsdebug.c
@@ -121,6 +121,32 @@
 	return x;
 }
 
+/*
+ *  convert address into a reverse lookup address
+ */
+static void
+mkptrname(char *ip, char *rip, int rlen)
+{
+	uchar a[IPaddrlen];
+	char *p, *e;
+	int i;
+
+	if(cistrstr(ip, "in-addr.arpa") || cistrstr(ip, "ip6.arpa") || parseip(a, ip) == -1)
+		snprint(rip, rlen, "%s", ip);
+	else if(isv4(a))
+		snprint(rip, rlen, "%ud.%ud.%ud.%ud.in-addr.arpa",
+			a[15], a[14], a[13], a[12]);
+	else{
+		p = rip;
+		e = rip + rlen;
+		for(i = 15; i >= 0; i--){
+			p = seprint(p, e, "%ux.", a[i]&0xf);
+			p = seprint(p, e, "%ux.", a[i]>>4);
+		}
+		seprint(p, e, "ip6.arpa");
+	}
+}
+
 int
 prettyrrfmt(Fmt *f)
 {
@@ -372,7 +398,6 @@
 doquery(char *name, char *tstr)
 {
 	int len, type, rooted;
-	char *p, *np;
 	char buf[1024];
 	RR *rr, *rp;
 	Request req;
@@ -387,6 +412,13 @@
 		else
 			tstr = "ip";
 
+	/* look it up */
+	type = rrtype(tstr);
+	if(type < 0){
+		print("!unknown type %s\n", tstr);
+		return;
+	}
+
 	/* if name end in '.', remove it */
 	len = strlen(name);
 	if(len > 0 && name[len-1] == '.'){
@@ -396,34 +428,10 @@
 		rooted = 0;
 
 	/* inverse queries may need to be permuted */
-	strncpy(buf, name, sizeof buf);
-	if(strcmp("ptr", tstr) == 0 && cistrstr(name, ".arpa") == nil){
-		/* TODO: reversing v6 addrs is harder */
-		for(p = name; *p; p++)
-			;
-		*p = '.';
-		np = buf;
-		len = 0;
-		while(p >= name){
-			len++;
-			p--;
-			if(*p == '.'){
-				memmove(np, p+1, len);
-				np += len;
-				len = 0;
-			}
-		}
-		memmove(np, p+1, len);
-		np += len;
-		strcpy(np, "in-addr.arpa");	/* TODO: ip6.arpa for v6 */
-	}
-
-	/* look it up */
-	type = rrtype(tstr);
-	if(type < 0){
-		print("!unknown type %s\n", tstr);
-		return;
-	}
+	if(type == Tptr)
+		mkptrname(name, buf, sizeof buf);
+	else
+		strncpy(buf, name, sizeof buf);
 
 	memset(&req, 0, sizeof req);
 	getactivity(&req, 0);