From 30f6d6ca771d0a160ad0ea3f583c895a5d642661 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sat, 16 Nov 2024 17:02:47 -0600 Subject: [PATCH] Detect truncated replies, minor cleanups, better error messages. --- toys/net/host.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/toys/net/host.c b/toys/net/host.c index 1d06159f..c97e2ccb 100644 --- a/toys/net/host.c +++ b/toys/net/host.c @@ -105,7 +105,7 @@ void host_main(void) } if (i == ARRAY_LEN(rrt)) error_exit("bad -t: %s", TT.t); } - qlen = res_mkquery(0, name, 1, type, 0, 0, 0, t2, 280); //t2len); + qlen = res_mkquery(0, name, 1, type, 0, 0, 0, t2, t2len); if (qlen<0) error_exit("bad NAME: %s", name); // Grab nameservers @@ -122,9 +122,10 @@ void host_main(void) setsockopt(i, SOL_SOCKET, SO_RCVTIMEO, &(struct timeval){ .tv_sec = 5 }, sizeof(struct timeval)); send(i, t2, qlen, 0); - if (16 < (alen = recv(i, abuf, abuf_len, 0))) break; - if (!*++TT.nsname) error_exit("Host not found."); + alen = recv(i, abuf, abuf_len, 0); close(i); + if (160 && sec>1) + if (verbose && count && sec>1) puts(sec==2 ? "For authoritative answers, see:" : "Additional information:"); for (; count--; p += pllen) { - p += xdn_expand(abuf, abuf+alen, p, toybuf, 4096-t2len); - if (alen-(p-abuf)<10) error_exit("tilt"); + p += xdn_expand(abuf, abuf+alen, p, toybuf, sizeof(toybuf)-t2len); + if (alen-(p-abuf)<10) error_exit("bad header"); type = peek_be(p, 2); p += 4; if (!sec) continue; @@ -156,7 +158,7 @@ void host_main(void) p += 4; pllen = peek_be(p, 2); p += 2; - if ((p-abuf)+pllen>alen) error_exit("tilt"); + if ((p-abuf)+pllen>alen) error_exit("bad header"); if (type==1 || type == 28) inet_ntop(type==1 ? AF_INET : AF_INET6, p, t2, t2len); else if (type==2 || type==5) xdn_expand(abuf, abuf+alen, p, t2, t2len); -- 2.39.5