shithub: riscv

Download patch

ref: 498d86b9218287b2b5c16aa54a283249fbd4e5a3
parent: c67593125c93d80c281ee7a52ed81595ce2b7b92
author: cinap_lenrek <[email protected]>
date: Mon Nov 1 12:31:39 EDT 2021

ndb/dnsquery: make ! bang work with reverse lookups, document in ndb(8)

--- a/sys/man/8/ndb
+++ b/sys/man/8/ndb
@@ -683,6 +683,9 @@
 .I Ndb/dnsquery
 prompts for commands of the form
 .IP
+[
+.B !
+]
 .I "domain-name request-type"
 .LP
 where
@@ -700,6 +703,11 @@
 will reverse the ip address and tack on the
 .B .in-addr.arpa
 if necessary.
+If the command starts with an exclamation mark
+.B !
+then the response is returned in
+.IR ndb (6)
+format.
 The
 .B -x
 option switches
@@ -707,7 +715,7 @@
 to query the dns server on
 .B /net.alt
 instead of
-.B /net
+.BR /net .
 .PP
 .I Ndb/dnsdebug
 is like
--- a/sys/src/cmd/ndb/dnsquery.c
+++ b/sys/src/cmd/ndb/dnsquery.c
@@ -53,41 +53,47 @@
 query(int fd)
 {
 	int n;
-	char *lp;
+	char *lp, *bang;
 	char buf[1024], line[1024];
 	Biobuf in;
 
 	Binit(&in, 0, OREAD);
 	for(fprint(2, "> "); lp = Brdline(&in, '\n'); fprint(2, "> ")){
-		n = Blinelen(&in) -1;
-		while(isspace(lp[n]))
-			lp[n--] = 0;
-		n++;
-		while(isspace(*lp)){
-			lp++;
+		n = Blinelen(&in);
+		while(n > 0 && isspace(lp[n-1]))
 			n--;
+		lp[n] = 0;
+		while(isspace(*lp))
+			lp++;
+		bang = "";
+		while(*lp == '!'){
+			bang = "!";
+			lp++;
 		}
-		if(!*lp)
+		while(isspace(*lp))
+			lp++;
+		if(*lp == 0)
 			continue;
-		strcpy(line, lp);
 
 		/* default to an "ip" request if alpha, "ptr" if numeric */
-		if(strchr(line, ' ') == nil)
-			if(strcmp(ipattr(line), "ip") == 0) {
-				strcat(line, " ptr");
-				n += 4;
+		if(strchr(lp, ' ') == nil){
+			if(strcmp(ipattr(lp), "ip") == 0) {
+				n = snprint(line, sizeof line, "%s ptr", lp);
 			} else {
-				strcat(line, " ip");
-				n += 3;
+				n = snprint(line, sizeof line, "%s ip", lp);
 			}
+		} else {
+			n = snprint(line, sizeof line, "%s", lp);
+		}
 
 		if(n > 4 && strcmp(" ptr", &line[n-4]) == 0){
 			line[n-4] = 0;
 			mkptrname(line, buf, sizeof buf);
-			n = snprint(line, sizeof line, "%s ptr", buf);
+			snprint(line, sizeof line, "%s ptr", buf);
 		}
 
-		querydns(fd, line, n);
+		n = snprint(buf, sizeof buf, "%s%s", bang, line);
+		querydns(fd, buf, n);
 	}
 	Bterm(&in);
 }