<div dir="ltr">Dear all,<div>please ignore this one, which was still pending in outgoing, but rolled back by Rob..</div><div><br></div><div>Best regards,</div><div>Moritz</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Jan 3, 2023 at 10:18 PM Moritz C. Weber <<a href="mailto:mo.c.weber@gmail.com">mo.c.weber@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">---<br>
toys/net/host.c | 98 +++++++++++++++++++++++++++++++------------------<br>
1 file changed, 63 insertions(+), 35 deletions(-)<br>
<br>
diff --git a/toys/net/host.c b/toys/net/host.c<br>
index 20331678..933f4fb7 100644<br>
--- a/toys/net/host.c<br>
+++ b/toys/net/host.c<br>
@@ -7,6 +7,7 @@<br>
* See <a href="https://www.ietf.org/rfc/rfc3596.txt" rel="noreferrer" target="_blank">https://www.ietf.org/rfc/rfc3596.txt</a><br>
<br>
USE_HOST(NEWTOY(host, "<1>2avt:", TOYFLAG_USR|TOYFLAG_BIN))<br>
+USE_NSLOOKUP(NEWTOY(nslookup, "<1>2", TOYFLAG_USR|TOYFLAG_BIN))<br>
<br>
config HOST<br>
bool "host"<br>
@@ -20,6 +21,14 @@ config HOST<br>
-a All records<br>
-t TYPE Record TYPE (number or ANY A AAAA CNAME MX NS PTR SOA SRV TXT)<br>
-v Verbose<br>
+<br>
+config NSLOOKUP<br>
+ bool "nslookup"<br>
+ default y<br>
+ help<br>
+ usage: nslookup NAME [SERVER]<br>
+<br>
+ Query Internet name servers interactively<br>
*/<br>
<br>
#define FOR_host<br>
@@ -70,13 +79,11 @@ static void get_nsname(char **pline, long len)<br>
}<br>
}<br>
<br>
-void host_main(void)<br>
+void lookup(int verbose, unsigned *ttl, char* t2, int t2len, int* type, int* rcode)<br>
{<br>
- int verbose = FLAG(a)||FLAG(v), type, abuf_len = 65536, //Largest TCP response<br>
- i, j, sec, rcode, qlen, alen QUIET, pllen = 0, t2len = 2048;<br>
- unsigned count, ttl;<br>
- char *abuf = xmalloc(abuf_len), *name = *toys.optargs, *p, *ss,<br>
- *t2 = toybuf+t2len;<br>
+ int i, j, sec, qlen, alen QUIET, pllen = 0, abuf_len = 65536; //Largest TCP response<br>
+ unsigned count;<br>
+ char *abuf = xmalloc(abuf_len), *name = *toys.optargs, *p, *ss;<br>
struct addrinfo *ai;<br>
<br>
// What kind of query are we doing?<br>
@@ -96,26 +103,20 @@ void host_main(void)<br>
} else if (!TT.t) TT.t = "1";<br>
<br>
// Prepare query packet of appropriate type<br>
- if (TT.t[0]-'0'<10) type = atoi(TT.t); // TODO<br>
- else if (!strcasecmp(TT.t, "any") || strcmp(TT.t, "*")) type = 255;<br>
+ if (TT.t[0]-'0'<10) *type = atoi(TT.t); // TODO<br>
+ else if (!strcasecmp(TT.t, "any") || strcmp(TT.t, "*")) *type = 255;<br>
else {<br>
for (i = 0; i<ARRAY_LEN(rrt); i++) if (!strcasecmp(TT.t, rrt[i].name)) {<br>
- type = rrt[i].type;<br>
+ *type = rrt[i].type;<br>
break;<br>
}<br>
if (i == ARRAY_LEN(rrt)) error_exit("bad -t: %s", TT.t);<br>
}<br>
- qlen = res_mkquery(0, name, 1, type, 0, 0, 0, t2, 280); //t2len);<br>
+ qlen = res_mkquery(0, name, 1, *type, 0, 0, 0, t2, 280); //t2len);<br>
if (qlen<0) error_exit("bad NAME: %s", name);<br>
<br>
- // Grab nameservers<br>
- if (toys.optargs[1]) TT.nsname = toys.optargs+1;<br>
- else do_lines(xopen("/etc/resolv.conf", O_RDONLY), '\n', get_nsname);<br>
- if (!TT.nsname) error_exit("No nameservers");<br>
-<br>
// Send one query packet to each server until we receive response<br>
while (*TT.nsname) {<br>
- if (verbose) printf("Using domain server %s:\n", *TT.nsname);<br>
ai = xgetaddrinfo(*TT.nsname, "53", 0, SOCK_DGRAM, 0, 0);<br>
i = xsocket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);<br>
xconnect(i, ai->ai_addr, ai->ai_addrlen);<br>
@@ -128,14 +129,14 @@ void host_main(void)<br>
}<br>
<br>
// Did it error?<br>
- rcode = abuf[3]&7;<br>
+ *rcode = abuf[3]&7;<br>
if (verbose) {<br>
- printf("rcode = %d, ancount = %d\n", rcode, (int)peek_be(abuf+6, 2));<br>
+ printf("rcode = %d, ancount = %d\n", *rcode, (int)peek_be(abuf+6, 2));<br>
if (!(abuf[2]&4)) puts("The following answer is not authoritative:");<br>
}<br>
- if (rcode) error_exit("Host not found: %s",<br>
+ if (*rcode) error_exit("Host not found: %s",<br>
(char *[]){ "Format error", "Server failure",<br>
- "Non-existant domain", "Not implemented", "Refused", ""}[rcode-1]);<br>
+ "Non-existant domain", "Not implemented", "Refused", ""}[*rcode-1]);<br>
<br>
// Print the result<br>
p = abuf + 12;<br>
@@ -148,20 +149,20 @@ void host_main(void)<br>
for (; count--; p += pllen) {<br>
p += xdn_expand(abuf, abuf+alen, p, toybuf, 4096-t2len);<br>
if (alen-(p-abuf)<10) error_exit("tilt");<br>
- type = peek_be(p, 2);<br>
+ *type = peek_be(p, 2);<br>
p += 4;<br>
if (!sec) continue;<br>
- ttl = peek_be(p, 4);<br>
+ *ttl = peek_be(p, 4);<br>
p += 4;<br>
pllen = peek_be(p, 2);<br>
p += 2;<br>
if ((p-abuf)+pllen>alen) error_exit("tilt");<br>
<br>
- if (type==1 || type == 28)<br>
- inet_ntop(type==1 ? AF_INET : AF_INET6, p, t2, t2len);<br>
- else if (type==2 || type==5) xdn_expand(abuf, abuf+alen, p, t2, t2len);<br>
- else if (type==16) sprintf(t2, "\"%.*s\"", minof(pllen, t2len), p);<br>
- else if (type==6) { <br>
+ if (*type==1 || *type == 28)<br>
+ inet_ntop(*type==1 ? AF_INET : AF_INET6, p, t2, t2len);<br>
+ else if (*type==2 || *type==5) xdn_expand(abuf, abuf+alen, p, t2, t2len);<br>
+ else if (*type==16) sprintf(t2, "\"%.*s\"", minof(pllen, t2len), p);<br>
+ else if (*type==6) { <br>
ss = p+xdn_expand(abuf, abuf+alen, p, t2, t2len-1);<br>
j = strlen(t2);<br>
t2[j++] = ' ';<br>
@@ -172,25 +173,52 @@ void host_main(void)<br>
"\t\t%u\t;default ttl\n\t\t)", (unsigned)peek_be(ss, 4),<br>
(unsigned)peek_be(ss+4, 4), (unsigned)peek_be(ss+8, 4),<br>
(unsigned)peek_be(ss+12, 4), (unsigned)peek_be(ss+16, 4));<br>
- } else if (type==15) {<br>
+ } else if (*type==15) {<br>
j = peek_be(p, 2);<br>
j = sprintf(t2, verbose ? "%d " : "(pri=%d) by ", j);<br>
xdn_expand(abuf, abuf+alen, p+2, t2+j, t2len-j);<br>
- } else if (type==33) {<br>
+ } else if (*type==33) {<br>
j = sprintf(t2, "%u %u %u ", (int)peek_be(p, 2), (int)peek_be(p+2, 2),<br>
(int)peek_be(p+4, 2));<br>
xdn_expand(abuf, abuf+alen, p+6, t2+j, t2len-j);<br>
} else {<br>
- printf("%s unsupported RR type %u\n", toybuf, type);<br>
+ printf("%s unsupported RR type %u\n", toybuf, *type);<br>
continue;<br>
}<br>
-<br>
- for (i = 0; rrt[i].type != type; i++);<br>
- if (verbose) printf("%s\t%u\tIN %s\t%s\n", toybuf, ttl, rrt[i].name, t2);<br>
- else printf("%s %s %s\n", toybuf, rrt[type].msg, t2);<br>
}<br>
}<br>
-<br>
if (CFG_TOYBOX_FREE) free(abuf);<br>
+}<br>
+<br>
+void host_main(void)<br>
+{<br>
+ int verbose = FLAG(a)||FLAG(v), t2len=2048, type,rcode,i=0; <br>
+ unsigned ttl;<br>
+ char *t2 = toybuf+t2len;<br>
+ // Grab nameservers<br>
+ if (toys.optargs[1]) TT.nsname = toys.optargs+1;<br>
+ else do_lines(xopen("/etc/resolv.conf", O_RDONLY), '\n', get_nsname);<br>
+ if (!TT.nsname) error_exit("No nameservers");<br>
+ if (verbose) printf("Using domain server %s:\n", *TT.nsname);<br>
+ lookup(verbose,&ttl,t2,t2len,&type,&rcode);<br>
+ for (i = 0; rrt[i].type != type; i++);<br>
+ if (verbose) printf("%s\t%u\tIN %s\t%s\n", toybuf, ttl, rrt[i].name, t2);<br>
+ else printf("%s %s %s\n", toybuf, rrt[type].msg, t2);<br>
+ toys.exitval = rcode;<br>
+}<br>
+<br>
+#define FOR_nslookup<br>
+#include "generated/flags.h"<br>
+<br>
+void nslookup_main(void){<br>
+ int t2len=2048, type,rcode; <br>
+ unsigned ttl;<br>
+ char *t2 = toybuf+t2len;<br>
+ if (toys.optargs[1]) TT.nsname = toys.optargs+1;<br>
+ else do_lines(xopen("/etc/resolv.conf", O_RDONLY), '\n', get_nsname);<br>
+ if (!TT.nsname) error_exit("No nameservers");<br>
+ printf("Server:\t %s:\nAddress: %s#53\n\n", *TT.nsname, *TT.nsname);//TODO:Not sure if static port is a good idea here<br>
+ lookup(0,&ttl,t2,t2len,&type,&rcode);<br>
+ printf("Non-authorative answer:\nName: %s\nAddress: %s\n", toybuf, t2);//TODO: Autherative answers<br>
toys.exitval = rcode;<br>
}<br>
-- <br>
2.20.1<br>
<br>
</blockquote></div>