xref: /aosp_15_r20/external/toybox/toys/net/host.c (revision cf5a6c84e2b8763fc1a7db14496fd4742913b199)
1 /* host.c - DNS lookup utility
2  *
3  * Copyright 2014 Rich Felker <[email protected]>
4  *
5  * No standard, but there's a version in bind9
6  * See https://www.ietf.org/rfc/rfc1035.txt
7  * See https://www.ietf.org/rfc/rfc3596.txt
8 
9 USE_HOST(NEWTOY(host, "<1>2avt:", TOYFLAG_USR|TOYFLAG_BIN))
10 
11 config HOST
12   bool "host"
13   default y
14   help
15     usage: host [-v] [-t TYPE] NAME [SERVER]
16 
17     Look up DNS records for NAME, either domain name or IPv4/IPv6 address to
18     reverse lookup, from SERVER or default DNS server(s).
19 
20     -a	All records
21     -t TYPE	Record TYPE (number or ANY A AAAA CNAME MX NS PTR SOA SRV TXT)
22     -v	Verbose
23 */
24 
25 #define FOR_host
26 #include "toys.h"
27 #include <resolv.h>
28 
29 GLOBALS(
30   char *t;
31 
32   char **nsname;
33   unsigned nslen;
34 )
35 
36 static const struct rrt {
37   char *name, *msg;
38   int type;
39 } rrt[] = { { "A", "has address", 1 }, { "NS", "name server", 2 },
40   { "CNAME", "is a nickname for", 5 }, { "SOA", "start of authority", 6 },
41   { "PTR", "domain name pointer", 12 }, { "HINFO", "host information", 13 },
42   { "MX", "mail is handled", 15 }, { "TXT", "descriptive text", 16 },
43   { "AAAA", "has IPv6 address", 28 }, { "SRV", "has SRV record", 33 }
44 };
45 
xdn_expand(char * packet,char * endpkt,char * comp,char * expand,int elen)46 int xdn_expand(char *packet, char *endpkt, char *comp, char *expand, int elen)
47 {
48   int i = dn_expand(packet, endpkt, comp, expand, elen);
49 
50   if (i<1) error_exit("bad dn_expand");
51 
52   return i;
53 }
54 
55 // Fetch "nameserve" lines from /etc/resolv.conf. Ignores 'options' lines
get_nsname(char ** pline,long len)56 static void get_nsname(char **pline, long len)
57 {
58   char *line, *p;
59 
60   if (!len) return;
61   line = *pline;
62   if (strstart(&line, "nameserver") && isspace(*line)) {
63     while (isspace(*line)) line++;
64     for (p = line; *p && !isspace(*p) && *p!='#'; p++);
65     if (p == line) return;
66     *p = 0;
67     if (!(TT.nslen&8))
68       TT.nsname = xrealloc(TT.nsname, (TT.nslen+8)*sizeof(void *));
69     TT.nsname[TT.nslen++] = xstrdup(line);
70   }
71 }
72 
host_main(void)73 void host_main(void)
74 {
75   int verbose = FLAG(a)||FLAG(v), type, abuf_len = 65536, //Largest TCP response
76       i, j, sec, rcode, qlen, alen QUIET, pllen = 0, t2len = 2048;
77   unsigned count, ttl;
78   char *abuf = xmalloc(abuf_len), *name = *toys.optargs, *p, *ss,
79        *t2 = toybuf+t2len;
80   struct addrinfo *ai;
81 
82   // What kind of query are we doing?
83   if (!TT.t && FLAG(a)) TT.t = "255";
84   if (!getaddrinfo(name, 0,&(struct addrinfo){.ai_flags=AI_NUMERICHOST}, &ai)) {
85     name = toybuf;
86     if (ai->ai_family == AF_INET) {
87       p = (void *)&((struct sockaddr_in *)ai->ai_addr)->sin_addr;
88       sprintf(name, "%d.%d.%d.%d.in-addr.arpa", p[3], p[2], p[1], p[0]);
89     } else if (ai->ai_family == AF_INET6) {
90       p = (void *)&((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr;
91       for (j = 0, i = 15; i>=0; i--)
92         j += sprintf(name+j, "%x.%x.", p[i]&15, p[i]>>4);
93       strcpy(name+j, "ip6.arpa");
94     }
95     if (!TT.t) TT.t = "12";
96   } else if (!TT.t) TT.t = "1";
97 
98   // Prepare query packet of appropriate type
99   if (TT.t[0]-'0'<10) type = atoi(TT.t); // TODO
100   else if (!strcasecmp(TT.t, "any") || !strcmp(TT.t, "*")) type = 255;
101   else {
102     for (i = 0; i<ARRAY_LEN(rrt); i++) if (!strcasecmp(TT.t, rrt[i].name)) {
103       type = rrt[i].type;
104       break;
105     }
106     if (i == ARRAY_LEN(rrt)) error_exit("bad -t: %s", TT.t);
107   }
108   qlen = res_mkquery(0, name, 1, type, 0, 0, 0, t2, t2len);
109   if (qlen<0) error_exit("bad NAME: %s", name);
110 
111   // Grab nameservers
112   if (toys.optargs[1]) TT.nsname = toys.optargs+1;
113   else do_lines(xopen("/etc/resolv.conf", O_RDONLY), '\n', get_nsname);
114   if (!TT.nsname) error_exit("No nameservers");
115 
116   // Send one query packet to each server until we receive response
117   while (*TT.nsname) {
118     if (verbose) printf("Using domain server %s:\n", *TT.nsname);
119     ai = xgetaddrinfo(*TT.nsname, "53", 0, SOCK_DGRAM, 0, 0);
120     i = xsocket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
121     xconnect(i, ai->ai_addr, ai->ai_addrlen);
122     setsockopt(i, SOL_SOCKET, SO_RCVTIMEO, &(struct timeval){ .tv_sec = 5 },
123       sizeof(struct timeval));
124     send(i, t2, qlen, 0);
125     alen = recv(i, abuf, abuf_len, 0);
126     close(i);
127     if (16<alen) break;
128     if (!*++TT.nsname) error_exit("Host not found.");
129   }
130 
131   // Did it error?
132   rcode = abuf[3]&7;
133   if (verbose) {
134     printf("rcode = %d, ancount = %d\n", rcode, (int)peek_be(abuf+6, 2));
135     if (!(abuf[2]&4)) puts("The following answer is not authoritative:");
136   }
137   if (rcode) error_exit("Host not found: %s",
138     (char *[]){ "Format error", "Server failure",
139     "Non-existant domain", "Not implemented", "Refused", ""}[rcode-1]);
140   if (abuf[2]&2) puts("Truncated");
141 
142   // Print the result
143   p = abuf + 12;
144   qlen = 0;
145   for (sec = 0; sec<(2<<verbose); sec++) {
146     count = peek_be(abuf+4+2*sec, 2);
147     if (verbose && count && sec>1)
148       puts(sec==2 ? "For authoritative answers, see:"
149         : "Additional information:");
150 
151     for (; count--; p += pllen) {
152       p += xdn_expand(abuf, abuf+alen, p, toybuf, sizeof(toybuf)-t2len);
153       if (alen-(p-abuf)<10) error_exit("bad header");
154       type = peek_be(p, 2);
155       p += 4;
156       if (!sec) continue;
157       ttl = peek_be(p, 4);
158       p += 4;
159       pllen = peek_be(p, 2);
160       p += 2;
161       if ((p-abuf)+pllen>alen) error_exit("bad header");
162       if (type==1 || type == 28)
163         inet_ntop(type==1 ? AF_INET : AF_INET6, p, t2, t2len);
164       else if (type==2 || type==5) xdn_expand(abuf, abuf+alen, p, t2, t2len);
165       else if (type==13 || type==16) {
166         if (pllen && pllen-1==*p) p++, pllen--;
167         sprintf(t2, "\"%.*s\"", minof(pllen, t2len), p);
168       } else if (type==6) {
169         ss = p+xdn_expand(abuf, abuf+alen, p, t2, t2len-1);
170         j = strlen(t2);
171         t2[j++] = ' ';
172         ss += xdn_expand(abuf, abuf+alen, ss, t2+j, t2len-j);
173         j += strlen(t2+j);
174         snprintf(t2+j, t2len-j, "(\n\t\t%u\t;serial (version)\n\t\t%u\t"
175           ";refresh period\n\t\t%u\t;retry interval\n\t\t%u\t;expire time\n"
176           "\t\t%u\t;default ttl\n\t\t)", (unsigned)peek_be(ss, 4),
177           (unsigned)peek_be(ss+4, 4), (unsigned)peek_be(ss+8, 4),
178           (unsigned)peek_be(ss+12, 4), (unsigned)peek_be(ss+16, 4));
179       } else if (type==15) {
180         j = peek_be(p, 2);
181         j = sprintf(t2, verbose ? "%d " : "(pri=%d) by ", j);
182         xdn_expand(abuf, abuf+alen, p+2, t2+j, t2len-j);
183       } else if (type==33) {
184         j = sprintf(t2, "%u %u %u ", (int)peek_be(p, 2), (int)peek_be(p+2, 2),
185           (int)peek_be(p+4, 2));
186         xdn_expand(abuf, abuf+alen, p+6, t2+j, t2len-j);
187       } else {
188         printf("%s unsupported RR type %u\n", toybuf, type);
189         continue;
190       }
191       for (i = 0; rrt[i].type != type; i++);
192       if (verbose) printf("%s\t%u\tIN %s\t%s\n", toybuf, ttl, rrt[i].name, t2);
193       else printf("%s %s %s\n", toybuf, rrt[i].msg, t2);
194       qlen++;
195     }
196   }
197   if (TT.t && !qlen) printf("%s has no %s record\n", *toys.optargs, TT.t);
198 
199   if (CFG_TOYBOX_FREE) free(abuf);
200   toys.exitval = rcode;
201 }
202