[Toybox] [PATCH 2/2] Added interactive mode to nslookup in host.c

Moritz C. Weber mo.c.weber at gmail.com
Mon Jun 6 13:55:58 PDT 2022


---
 toys/net/host.c | 33 +++++++++++++++++++++++----------
 1 file changed, 23 insertions(+), 10 deletions(-)

diff --git a/toys/net/host.c b/toys/net/host.c
index 933f4fb7..cefac7e2 100644
--- a/toys/net/host.c
+++ b/toys/net/host.c
@@ -7,7 +7,7 @@
  * See https://www.ietf.org/rfc/rfc3596.txt
 
 USE_HOST(NEWTOY(host, "<1>2avt:", TOYFLAG_USR|TOYFLAG_BIN))
-USE_NSLOOKUP(NEWTOY(nslookup, "<1>2", TOYFLAG_USR|TOYFLAG_BIN))
+USE_NSLOOKUP(NEWTOY(nslookup, "<0>2", TOYFLAG_USR|TOYFLAG_BIN))
 
 config HOST
   bool "host"
@@ -79,11 +79,11 @@ static void get_nsname(char **pline, long len)
   }
 }
 
-void lookup(int verbose, unsigned *ttl,  char* t2, int t2len, int* type, int* rcode)
+void lookup(int verbose, unsigned *ttl,  char* t2, int t2len, int* type, int* rcode, char* name)
 {
   int i, j, sec, qlen, alen QUIET, pllen = 0, abuf_len = 65536; //Largest TCP response
   unsigned count;
-  char *abuf = xmalloc(abuf_len), *name = *toys.optargs, *p, *ss;
+  char *abuf = xmalloc(abuf_len), *p, *ss;
   struct addrinfo *ai;
 
   // What kind of query are we doing?
@@ -194,13 +194,13 @@ void host_main(void)
 {
   int verbose = FLAG(a)||FLAG(v), t2len=2048, type,rcode,i=0; 
   unsigned ttl;
-  char *t2 = toybuf+t2len;
+  char *t2 = toybuf+t2len,*name = *toys.optargs;
   // Grab nameservers
   if (toys.optargs[1]) TT.nsname = toys.optargs+1;
   else do_lines(xopen("/etc/resolv.conf", O_RDONLY), '\n', get_nsname);
   if (!TT.nsname) error_exit("No nameservers");
   if (verbose) printf("Using domain server %s:\n", *TT.nsname);
-  lookup(verbose,&ttl,t2,t2len,&type,&rcode);
+  lookup(verbose,&ttl,t2,t2len,&type,&rcode,name);
   for (i = 0; rrt[i].type != type; i++);
   if (verbose) printf("%s\t%u\tIN %s\t%s\n", toybuf, ttl, rrt[i].name, t2);
   else printf("%s %s %s\n", toybuf, rrt[type].msg, t2);
@@ -211,14 +211,27 @@ void host_main(void)
 #include "generated/flags.h"
 
 void nslookup_main(void){
-  int t2len=2048, type,rcode; 
+  int t2len=2048, type,rcode;
   unsigned ttl;
-  char *t2 = toybuf+t2len;
+  char *t2 = toybuf+t2len, *name = *toys.optargs;
   if (toys.optargs[1]) TT.nsname = toys.optargs+1;
   else do_lines(xopen("/etc/resolv.conf", O_RDONLY), '\n', get_nsname);
   if (!TT.nsname) error_exit("No nameservers");
-  printf("Server:\t %s:\nAddress: %s#53\n\n", *TT.nsname, *TT.nsname);//TODO:Not sure if static port is a good idea here
-  lookup(0,&ttl,t2,t2len,&type,&rcode);
-  printf("Non-authorative answer:\nName:   %s\nAddress: %s\n", toybuf, t2);//TODO: Autherative answers
+  if(!name){
+    size_t size;
+    do{
+      printf(">");
+      ssize_t ssize=getline(&name,&size,stdin);
+      name[ssize-1]='\0';
+      if (strcmp("exit",name)==0) break;
+        printf("Server:\t %s:\nAddress: %s#53\n\n", *TT.nsname, *TT.nsname);//TODO:Not sure if static port is a good idea here
+        lookup(0,&ttl,t2,t2len,&type,&rcode,name);
+        printf("Non-authorative answer:\nName:   %s\nAddress: %s\n", toybuf, t2);//TODO: Autherative answers
+    } while (rcode==0);
+  }else{
+    printf("Server:\t %s:\nAddress: %s#53\n\n", *TT.nsname, *TT.nsname);//TODO:Not sure if static port is a good idea here
+    lookup(0,&ttl,t2,t2len,&type,&rcode,name);
+    printf("Non-authorative answer:\nName:   %s\nAddress: %s\n", toybuf, t2);//TODO: Autherative answers
+  }
   toys.exitval = rcode;
 }
-- 
2.20.1




More information about the Toybox mailing list