From 4247dca900e08daa6a8a2333baca6136bdbba31d Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Fri, 6 Sep 2019 22:31:48 -0700 Subject: host: cope with large DNS responses. DNS responses were limited to 512 bytes back when they were UDP only, but if you have a TCP connection you can get up to 64KiB. Also use the existing constant for the size of rrname. Also update the help text. Also consistently use `sizeof(T)` rather than `sizeof T`. Also use consistently use `ARRAY_LEN`. Fixes #56. Test: `toybox host value.testing.express` --- toys/pending/host.c | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) (limited to 'toys') diff --git a/toys/pending/host.c b/toys/pending/host.c index fe0f23aa..1610301e 100644 --- a/toys/pending/host.c +++ b/toys/pending/host.c @@ -16,8 +16,8 @@ config HOST or an IPv4 dotted or IPv6 colon-separated address to reverse lookup. SERVER (if present) is the DNS server to use. - -a no idea - -t not a clue + -a -v -t ANY + -t TYPE query records of type TYPE -v verbose */ @@ -68,10 +68,13 @@ static const char rct[16][32] = { void host_main(void) { int verbose=(toys.optflags & (FLAG_a|FLAG_v)), type, - i, j, ret, sec, count, rcode, qlen, alen, pllen = 0; + i, j, ret, sec, count, rcode, qlen, alen, pllen = 0, + abuf_len = 65536; // Largest TCP response. unsigned ttl, pri, v[5]; - unsigned char qbuf[280], abuf[512], *p; - char *name, *nsname, rrname[256], plname[640], ptrbuf[128]; + unsigned char *abuf = xmalloc(abuf_len); + char *rrname = xmalloc(MAXDNAME); + unsigned char qbuf[280], *p; + char *name, *nsname, plname[640], ptrbuf[128]; struct addrinfo *ai, iplit_hints = { .ai_flags = AI_NUMERICHOST }; name = *toys.optargs; @@ -113,7 +116,7 @@ void host_main(void) if (type < 0) error_exit("Invalid query type: %s", TT.type_str); } - qlen = res_mkquery(0, name, 1, type, 0, 0, 0, qbuf, sizeof qbuf); + qlen = res_mkquery(0, name, 1, type, 0, 0, 0, qbuf, sizeof(qbuf)); if (qlen < 0) error_exit("Invalid query parameters: %s", name); if (nsname) { @@ -127,8 +130,8 @@ void host_main(void) sizeof(struct timeval)); printf("Using domain server %s:\n", nsname); send(s, qbuf, qlen, 0); - alen = recv(s, abuf, sizeof abuf, 0); - } else alen = res_send(qbuf, qlen, abuf, sizeof abuf); + alen = recv(s, abuf, abuf_len, 0); + } else alen = res_send(qbuf, qlen, abuf, abuf_len); if (alen < 12) error_exit("Host not found."); @@ -150,7 +153,7 @@ void host_main(void) : "Additional information:"); for (; count--; p += pllen) { - p += dn_expand(abuf, abuf+alen, p, rrname, sizeof(rrname)); + p += dn_expand(abuf, abuf+alen, p, rrname, MAXDNAME); type = (p[0]<<8) + p[1]; p += 4; if (!sec) continue; @@ -159,18 +162,18 @@ void host_main(void) pllen = (p[0]<<8) + p[1]; p += 2; - switch (type