51034e6bc4390057f93e77ad7c0d60598220f9e9
[openwrt/staging/chunkeey.git] / package / utils / busybox / patches / 230-add_nslookup_lede.patch
1 From 2285cd400a10b1bde5472fe1cec3311300ee943c Mon Sep 17 00:00:00 2001
2 From: Jo-Philipp Wich <jo@mein.io>
3 Date: Tue, 14 Mar 2017 22:21:34 +0100
4 Subject: [PATCH] networking: add LEDE nslookup applet
5
6 Add a new LEDE nslookup applet which is compatible with musl libc
7 and providing more features like ability to specify query type.
8
9 In contrast to busybox' builtin nslookup applet, this variant does
10 not rely on libc resolver internals but uses explicit send logic
11 and the libresolv primitives to parse received DNS responses.
12
13 Signed-off-by: Jo-Philipp Wich <jo@mein.io>
14 ---
15 networking/nslookup_lede.c | 893 +++++++++++++++++++++++++++++++++++++++++++++
16 1 file changed, 893 insertions(+)
17 create mode 100644 networking/nslookup_lede.c
18
19 diff --git a/networking/nslookup_lede.c b/networking/nslookup_lede.c
20 new file mode 100644
21 index 000000000..6f1f86502
22 --- /dev/null
23 +++ b/networking/nslookup_lede.c
24 @@ -0,0 +1,893 @@
25 +/*
26 + * nslookup_lede - musl compatible replacement for busybox nslookup
27 + *
28 + * Copyright (C) 2017 Jo-Philipp Wich <jo@mein.io>
29 + *
30 + * Permission to use, copy, modify, and/or distribute this software for any
31 + * purpose with or without fee is hereby granted, provided that the above
32 + * copyright notice and this permission notice appear in all copies.
33 + *
34 + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
35 + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
36 + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
37 + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
38 + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
39 + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
40 + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
41 + */
42 +
43 +//config:config NSLOOKUP_LEDE
44 +//config: bool "nslookup_lede"
45 +//config: depends on !NSLOOKUP
46 +//config: default y
47 +//config: help
48 +//config: nslookup is a tool to query Internet name servers (LEDE flavor).
49 +//config:
50 +//config:config FEATURE_NSLOOKUP_LEDE_LONG_OPTIONS
51 +//config: bool "Enable long options"
52 +//config: default y
53 +//config: depends on NSLOOKUP_LEDE && LONG_OPTS
54 +//config: help
55 +//config: Support long options for the nslookup applet.
56 +
57 +//applet:IF_NSLOOKUP_LEDE(APPLET(nslookup, BB_DIR_USR_BIN, BB_SUID_DROP))
58 +
59 +//kbuild:lib-$(CONFIG_NSLOOKUP_LEDE) += nslookup_lede.o
60 +
61 +//usage:#define nslookup_lede_trivial_usage
62 +//usage: "[HOST] [SERVER]"
63 +//usage:#define nslookup_lede_full_usage "\n\n"
64 +//usage: "Query the nameserver for the IP address of the given HOST\n"
65 +//usage: "optionally using a specified DNS server"
66 +//usage:
67 +//usage:#define nslookup_lede_example_usage
68 +//usage: "$ nslookup localhost\n"
69 +//usage: "Server: default\n"
70 +//usage: "Address: default\n"
71 +//usage: "\n"
72 +//usage: "Name: debian\n"
73 +//usage: "Address: 127.0.0.1\n"
74 +
75 +#include <stdio.h>
76 +#include <resolv.h>
77 +#include <string.h>
78 +#include <errno.h>
79 +#include <time.h>
80 +#include <poll.h>
81 +#include <unistd.h>
82 +#include <stdlib.h>
83 +#include <sys/socket.h>
84 +#include <arpa/inet.h>
85 +#include <net/if.h>
86 +#include <netdb.h>
87 +
88 +#include "libbb.h"
89 +
90 +struct ns {
91 + const char *name;
92 + len_and_sockaddr addr;
93 + int failures;
94 + int replies;
95 +};
96 +
97 +struct query {
98 + const char *name;
99 + size_t qlen, rlen;
100 + unsigned char query[512], reply[512];
101 + unsigned long latency;
102 + int rcode, n_ns;
103 +};
104 +
105 +static struct {
106 + int type;
107 + const char *name;
108 +} qtypes[] = {
109 + { ns_t_soa, "SOA" },
110 + { ns_t_ns, "NS" },
111 + { ns_t_a, "A" },
112 +#if ENABLE_FEATURE_IPV6
113 + { ns_t_aaaa, "AAAA" },
114 +#endif
115 + { ns_t_cname, "CNAME" },
116 + { ns_t_mx, "MX" },
117 + { ns_t_txt, "TXT" },
118 + { ns_t_ptr, "PTR" },
119 + { ns_t_any, "ANY" },
120 + { }
121 +};
122 +
123 +static const char *rcodes[] = {
124 + "NOERROR",
125 + "FORMERR",
126 + "SERVFAIL",
127 + "NXDOMAIN",
128 + "NOTIMP",
129 + "REFUSED",
130 + "YXDOMAIN",
131 + "YXRRSET",
132 + "NXRRSET",
133 + "NOTAUTH",
134 + "NOTZONE",
135 + "RESERVED11",
136 + "RESERVED12",
137 + "RESERVED13",
138 + "RESERVED14",
139 + "RESERVED15",
140 + "BADVERS"
141 +};
142 +
143 +static unsigned int default_port = 53;
144 +static unsigned int default_retry = 2;
145 +static unsigned int default_timeout = 5;
146 +
147 +
148 +static int parse_reply(const unsigned char *msg, size_t len)
149 +{
150 + ns_msg handle;
151 + ns_rr rr;
152 + int i, n, rdlen;
153 + const char *format = NULL;
154 + char astr[INET6_ADDRSTRLEN], dname[MAXDNAME];
155 + const unsigned char *cp;
156 +
157 + if (ns_initparse(msg, len, &handle) != 0) {
158 + //fprintf(stderr, "Unable to parse reply: %s\n", strerror(errno));
159 + return -1;
160 + }
161 +
162 + for (i = 0; i < ns_msg_count(handle, ns_s_an); i++) {
163 + if (ns_parserr(&handle, ns_s_an, i, &rr) != 0) {
164 + //fprintf(stderr, "Unable to parse resource record: %s\n", strerror(errno));
165 + return -1;
166 + }
167 +
168 + rdlen = ns_rr_rdlen(rr);
169 +
170 + switch (ns_rr_type(rr))
171 + {
172 + case ns_t_a:
173 + if (rdlen != 4) {
174 + //fprintf(stderr, "Unexpected A record length\n");
175 + return -1;
176 + }
177 + inet_ntop(AF_INET, ns_rr_rdata(rr), astr, sizeof(astr));
178 + printf("Name:\t%s\nAddress: %s\n", ns_rr_name(rr), astr);
179 + break;
180 +
181 +#if ENABLE_FEATURE_IPV6
182 + case ns_t_aaaa:
183 + if (rdlen != 16) {
184 + //fprintf(stderr, "Unexpected AAAA record length\n");
185 + return -1;
186 + }
187 + inet_ntop(AF_INET6, ns_rr_rdata(rr), astr, sizeof(astr));
188 + printf("%s\thas AAAA address %s\n", ns_rr_name(rr), astr);
189 + break;
190 +#endif
191 +
192 + case ns_t_ns:
193 + if (!format)
194 + format = "%s\tnameserver = %s\n";
195 + /* fall through */
196 +
197 + case ns_t_cname:
198 + if (!format)
199 + format = "%s\tcanonical name = %s\n";
200 + /* fall through */
201 +
202 + case ns_t_ptr:
203 + if (!format)
204 + format = "%s\tname = %s\n";
205 + if (ns_name_uncompress(ns_msg_base(handle), ns_msg_end(handle),
206 + ns_rr_rdata(rr), dname, sizeof(dname)) < 0) {
207 + //fprintf(stderr, "Unable to uncompress domain: %s\n", strerror(errno));
208 + return -1;
209 + }
210 + printf(format, ns_rr_name(rr), dname);
211 + break;
212 +
213 + case ns_t_mx:
214 + if (rdlen < 2) {
215 + fprintf(stderr, "MX record too short\n");
216 + return -1;
217 + }
218 + n = ns_get16(ns_rr_rdata(rr));
219 + if (ns_name_uncompress(ns_msg_base(handle), ns_msg_end(handle),
220 + ns_rr_rdata(rr) + 2, dname, sizeof(dname)) < 0) {
221 + //fprintf(stderr, "Cannot uncompress MX domain: %s\n", strerror(errno));
222 + return -1;
223 + }
224 + printf("%s\tmail exchanger = %d %s\n", ns_rr_name(rr), n, dname);
225 + break;
226 +
227 + case ns_t_txt:
228 + if (rdlen < 1) {
229 + //fprintf(stderr, "TXT record too short\n");
230 + return -1;
231 + }
232 + n = *(unsigned char *)ns_rr_rdata(rr);
233 + if (n > 0) {
234 + memset(dname, 0, sizeof(dname));
235 + memcpy(dname, ns_rr_rdata(rr) + 1, n);
236 + printf("%s\ttext = \"%s\"\n", ns_rr_name(rr), dname);
237 + }
238 + break;
239 +
240 + case ns_t_soa:
241 + if (rdlen < 20) {
242 + //fprintf(stderr, "SOA record too short\n");
243 + return -1;
244 + }
245 +
246 + printf("%s\n", ns_rr_name(rr));
247 +
248 + cp = ns_rr_rdata(rr);
249 + n = ns_name_uncompress(ns_msg_base(handle), ns_msg_end(handle),
250 + cp, dname, sizeof(dname));
251 +
252 + if (n < 0) {
253 + //fprintf(stderr, "Unable to uncompress domain: %s\n", strerror(errno));
254 + return -1;
255 + }
256 +
257 + printf("\torigin = %s\n", dname);
258 + cp += n;
259 +
260 + n = ns_name_uncompress(ns_msg_base(handle), ns_msg_end(handle),
261 + cp, dname, sizeof(dname));
262 +
263 + if (n < 0) {
264 + //fprintf(stderr, "Unable to uncompress domain: %s\n", strerror(errno));
265 + return -1;
266 + }
267 +
268 + printf("\tmail addr = %s\n", dname);
269 + cp += n;
270 +
271 + printf("\tserial = %lu\n", ns_get32(cp));
272 + cp += 4;
273 +
274 + printf("\trefresh = %lu\n", ns_get32(cp));
275 + cp += 4;
276 +
277 + printf("\tretry = %lu\n", ns_get32(cp));
278 + cp += 4;
279 +
280 + printf("\texpire = %lu\n", ns_get32(cp));
281 + cp += 4;
282 +
283 + printf("\tminimum = %lu\n", ns_get32(cp));
284 + break;
285 +
286 + default:
287 + break;
288 + }
289 + }
290 +
291 + return i;
292 +}
293 +
294 +static int parse_nsaddr(const char *addrstr, len_and_sockaddr *lsa)
295 +{
296 + char *eptr, *hash, ifname[IFNAMSIZ];
297 + unsigned int port = default_port;
298 + unsigned int scope = 0;
299 +
300 + hash = strchr(addrstr, '#');
301 +
302 + if (hash) {
303 + *hash++ = '\0';
304 + port = strtoul(hash, &eptr, 10);
305 +
306 + if (eptr == hash || *eptr != '\0' || port > 65535) {
307 + errno = EINVAL;
308 + return -1;
309 + }
310 + }
311 +
312 + hash = strchr(addrstr, '%');
313 +
314 + if (hash) {
315 + for (eptr = ++hash; *eptr != '\0' && *eptr != '#'; eptr++) {
316 + if ((eptr - hash) >= IFNAMSIZ) {
317 + errno = ENODEV;
318 + return -1;
319 + }
320 +
321 + ifname[eptr - hash] = *eptr;
322 + }
323 +
324 + ifname[eptr - hash] = '\0';
325 + scope = if_nametoindex(ifname);
326 +
327 + if (scope == 0) {
328 + errno = ENODEV;
329 + return -1;
330 + }
331 + }
332 +
333 +#if ENABLE_FEATURE_IPV6
334 + if (inet_pton(AF_INET6, addrstr, &lsa->u.sin6.sin6_addr)) {
335 + lsa->u.sin6.sin6_family = AF_INET6;
336 + lsa->u.sin6.sin6_port = htons(port);
337 + lsa->u.sin6.sin6_scope_id = scope;
338 + lsa->len = sizeof(lsa->u.sin6);
339 + return 0;
340 + }
341 +#endif
342 +
343 + if (!scope && inet_pton(AF_INET, addrstr, &lsa->u.sin.sin_addr)) {
344 + lsa->u.sin.sin_family = AF_INET;
345 + lsa->u.sin.sin_port = htons(port);
346 + lsa->len = sizeof(lsa->u.sin);
347 + return 0;
348 + }
349 +
350 + errno = EINVAL;
351 + return -1;
352 +}
353 +
354 +static char *make_ptr(const char *addrstr)
355 +{
356 + const char *hexdigit = "0123456789abcdef";
357 + static char ptrstr[73];
358 + unsigned char addr[16];
359 + char *ptr = ptrstr;
360 + int i;
361 +
362 + if (inet_pton(AF_INET6, addrstr, addr)) {
363 + if (memcmp(addr, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12) != 0) {
364 + for (i = 0; i < 16; i++) {
365 + *ptr++ = hexdigit[(unsigned char)addr[15 - i] & 0xf];
366 + *ptr++ = '.';
367 + *ptr++ = hexdigit[(unsigned char)addr[15 - i] >> 4];
368 + *ptr++ = '.';
369 + }
370 + strcpy(ptr, "ip6.arpa");
371 + }
372 + else {
373 + sprintf(ptr, "%u.%u.%u.%u.in-addr.arpa",
374 + addr[15], addr[14], addr[13], addr[12]);
375 + }
376 +
377 + return ptrstr;
378 + }
379 +
380 + if (inet_pton(AF_INET, addrstr, addr)) {
381 + sprintf(ptr, "%u.%u.%u.%u.in-addr.arpa",
382 + addr[3], addr[2], addr[1], addr[0]);
383 + return ptrstr;
384 + }
385 +
386 + return NULL;
387 +}
388 +
389 +static unsigned long mtime(void)
390 +{
391 + struct timespec ts;
392 + clock_gettime(CLOCK_REALTIME, &ts);
393 + return (unsigned long)ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
394 +}
395 +
396 +#if ENABLE_FEATURE_IPV6
397 +static void to_v4_mapped(len_and_sockaddr *a)
398 +{
399 + if (a->u.sa.sa_family != AF_INET)
400 + return;
401 +
402 + memcpy(a->u.sin6.sin6_addr.s6_addr + 12,
403 + &a->u.sin.sin_addr, 4);
404 +
405 + memcpy(a->u.sin6.sin6_addr.s6_addr,
406 + "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12);
407 +
408 + a->u.sin6.sin6_family = AF_INET6;
409 + a->u.sin6.sin6_flowinfo = 0;
410 + a->u.sin6.sin6_scope_id = 0;
411 + a->len = sizeof(a->u.sin6);
412 +}
413 +#endif
414 +
415 +
416 +/*
417 + * Function logic borrowed & modified from musl libc, res_msend.c
418 + */
419 +
420 +static int send_queries(struct ns *ns, int n_ns, struct query *queries, int n_queries)
421 +{
422 + int fd;
423 + int timeout = default_timeout * 1000, retry_interval, servfail_retry = 0;
424 + len_and_sockaddr from = { };
425 +#if ENABLE_FEATURE_IPV6
426 + int one = 1;
427 +#endif
428 + int recvlen = 0;
429 + int n_replies = 0;
430 + struct pollfd pfd;
431 + unsigned long t0, t1, t2;
432 + int nn, qn, next_query = 0;
433 +
434 + from.u.sa.sa_family = AF_INET;
435 + from.len = sizeof(from.u.sin);
436 +
437 +#if ENABLE_FEATURE_IPV6
438 + for (nn = 0; nn < n_ns; nn++) {
439 + if (ns[nn].addr.u.sa.sa_family == AF_INET6) {
440 + from.u.sa.sa_family = AF_INET6;
441 + from.len = sizeof(from.u.sin6);
442 + break;
443 + }
444 + }
445 +#endif
446 +
447 + /* Get local address and open/bind a socket */
448 + fd = socket(from.u.sa.sa_family, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
449 +
450 +#if ENABLE_FEATURE_IPV6
451 + /* Handle case where system lacks IPv6 support */
452 + if (fd < 0 && from.u.sa.sa_family == AF_INET6 && errno == EAFNOSUPPORT) {
453 + fd = socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
454 + from.u.sa.sa_family = AF_INET;
455 + }
456 +#endif
457 +
458 + if (fd < 0)
459 + return -1;
460 +
461 + if (bind(fd, &from.u.sa, from.len) < 0) {
462 + close(fd);
463 + return -1;
464 + }
465 +
466 +#if ENABLE_FEATURE_IPV6
467 + /* Convert any IPv4 addresses in a mixed environment to v4-mapped */
468 + if (from.u.sa.sa_family == AF_INET6) {
469 + setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
470 +
471 + for (nn = 0; nn < n_ns; nn++)
472 + to_v4_mapped(&ns[nn].addr);
473 + }
474 +#endif
475 +
476 + pfd.fd = fd;
477 + pfd.events = POLLIN;
478 + retry_interval = timeout / default_retry;
479 + t0 = t2 = mtime();
480 + t1 = t2 - retry_interval;
481 +
482 + for (; t2 - t0 < timeout; t2 = mtime()) {
483 + if (t2 - t1 >= retry_interval) {
484 + for (qn = 0; qn < n_queries; qn++) {
485 + if (queries[qn].rlen)
486 + continue;
487 +
488 + for (nn = 0; nn < n_ns; nn++) {
489 + sendto(fd, queries[qn].query, queries[qn].qlen,
490 + MSG_NOSIGNAL, &ns[nn].addr.u.sa, ns[nn].addr.len);
491 + }
492 + }
493 +
494 + t1 = t2;
495 + servfail_retry = 2 * n_queries;
496 + }
497 +
498 + /* Wait for a response, or until time to retry */
499 + if (poll(&pfd, 1, t1+retry_interval-t2) <= 0)
500 + continue;
501 +
502 + while (1) {
503 + recvlen = recvfrom(fd, queries[next_query].reply,
504 + sizeof(queries[next_query].reply), 0,
505 + &from.u.sa, &from.len);
506 +
507 + /* read error */
508 + if (recvlen < 0)
509 + break;
510 +
511 + /* Ignore non-identifiable packets */
512 + if (recvlen < 4)
513 + continue;
514 +
515 + /* Ignore replies from addresses we didn't send to */
516 + for (nn = 0; nn < n_ns; nn++)
517 + if (memcmp(&from.u.sa, &ns[nn].addr.u.sa, from.len) == 0)
518 + break;
519 +
520 + if (nn >= n_ns)
521 + continue;
522 +
523 + /* Find which query this answer goes with, if any */
524 + for (qn = next_query; qn < n_queries; qn++)
525 + if (!memcmp(queries[next_query].reply, queries[qn].query, 2))
526 + break;
527 +
528 + if (qn >= n_queries || queries[qn].rlen)
529 + continue;
530 +
531 + queries[qn].rcode = queries[next_query].reply[3] & 15;
532 + queries[qn].latency = mtime() - t0;
533 + queries[qn].n_ns = nn;
534 +
535 + ns[nn].replies++;
536 +
537 + /* Only accept positive or negative responses;
538 + * retry immediately on server failure, and ignore
539 + * all other codes such as refusal. */
540 + switch (queries[qn].rcode) {
541 + case 0:
542 + case 3:
543 + break;
544 +
545 + case 2:
546 + if (servfail_retry && servfail_retry--) {
547 + ns[nn].failures++;
548 + sendto(fd, queries[qn].query, queries[qn].qlen,
549 + MSG_NOSIGNAL, &ns[nn].addr.u.sa, ns[nn].addr.len);
550 + }
551 + /* fall through */
552 +
553 + default:
554 + continue;
555 + }
556 +
557 + /* Store answer */
558 + n_replies++;
559 +
560 + queries[qn].rlen = recvlen;
561 +
562 + if (qn == next_query) {
563 + while (next_query < n_queries) {
564 + if (!queries[next_query].rlen)
565 + break;
566 +
567 + next_query++;
568 + }
569 + }
570 + else {
571 + memcpy(queries[qn].reply, queries[next_query].reply, recvlen);
572 + }
573 +
574 + if (next_query >= n_queries)
575 + return n_replies;
576 + }
577 + }
578 +
579 + return n_replies;
580 +}
581 +
582 +static struct ns *add_ns(struct ns **ns, int *n_ns, const char *addr)
583 +{
584 + char portstr[sizeof("65535")], *p;
585 + len_and_sockaddr a = { };
586 + struct ns *tmp;
587 + struct addrinfo *ai, *aip, hints = {
588 + .ai_flags = AI_NUMERICSERV,
589 + .ai_socktype = SOCK_DGRAM
590 + };
591 +
592 + if (parse_nsaddr(addr, &a)) {
593 + /* Maybe we got a domain name, attempt to resolve it using the standard
594 + * resolver routines */
595 +
596 + p = strchr(addr, '#');
597 + snprintf(portstr, sizeof(portstr), "%hu",
598 + (unsigned short)(p ? strtoul(p, NULL, 10) : default_port));
599 +
600 + if (!getaddrinfo(addr, portstr, &hints, &ai)) {
601 + for (aip = ai; aip; aip = aip->ai_next) {
602 + if (aip->ai_addr->sa_family != AF_INET &&
603 + aip->ai_addr->sa_family != AF_INET6)
604 + continue;
605 +
606 +#if ! ENABLE_FEATURE_IPV6
607 + if (aip->ai_addr->sa_family != AF_INET)
608 + continue;
609 +#endif
610 +
611 + tmp = realloc(*ns, sizeof(**ns) * (*n_ns + 1));
612 +
613 + if (!tmp)
614 + return NULL;
615 +
616 + *ns = tmp;
617 +
618 + (*ns)[*n_ns].name = addr;
619 + (*ns)[*n_ns].replies = 0;
620 + (*ns)[*n_ns].failures = 0;
621 + (*ns)[*n_ns].addr.len = aip->ai_addrlen;
622 +
623 + memcpy(&(*ns)[*n_ns].addr.u.sa, aip->ai_addr, aip->ai_addrlen);
624 +
625 + (*n_ns)++;
626 + }
627 +
628 + freeaddrinfo(ai);
629 +
630 + return &(*ns)[*n_ns];
631 + }
632 +
633 + return NULL;
634 + }
635 +
636 + tmp = realloc(*ns, sizeof(**ns) * (*n_ns + 1));
637 +
638 + if (!tmp)
639 + return NULL;
640 +
641 + *ns = tmp;
642 +
643 + (*ns)[*n_ns].addr = a;
644 + (*ns)[*n_ns].name = addr;
645 + (*ns)[*n_ns].replies = 0;
646 + (*ns)[*n_ns].failures = 0;
647 +
648 + return &(*ns)[(*n_ns)++];
649 +}
650 +
651 +static int parse_resolvconf(struct ns **ns, int *n_ns)
652 +{
653 + int prev_n_ns = *n_ns;
654 + char line[128], *p;
655 + FILE *resolv;
656 +
657 + if ((resolv = fopen("/etc/resolv.conf", "r")) != NULL) {
658 + while (fgets(line, sizeof(line), resolv)) {
659 + p = strtok(line, " \t\n");
660 +
661 + if (!p || strcmp(p, "nameserver"))
662 + continue;
663 +
664 + p = strtok(NULL, " \t\n");
665 +
666 + if (!p)
667 + continue;
668 +
669 + if (!add_ns(ns, n_ns, strdup(p))) {
670 + free(p);
671 + break;
672 + }
673 + }
674 +
675 + fclose(resolv);
676 + }
677 +
678 + return *n_ns - prev_n_ns;
679 +}
680 +
681 +static struct query *add_query(struct query **queries, int *n_queries,
682 + int type, const char *dname)
683 +{
684 + struct query *tmp;
685 + ssize_t qlen;
686 +
687 + tmp = realloc(*queries, sizeof(**queries) * (*n_queries + 1));
688 +
689 + if (!tmp)
690 + return NULL;
691 +
692 + memset(&tmp[*n_queries], 0, sizeof(*tmp));
693 +
694 + qlen = res_mkquery(QUERY, dname, C_IN, type, NULL, 0, NULL,
695 + tmp[*n_queries].query, sizeof(tmp[*n_queries].query));
696 +
697 + tmp[*n_queries].qlen = qlen;
698 + tmp[*n_queries].name = dname;
699 + *queries = tmp;
700 +
701 + return &tmp[(*n_queries)++];
702 +}
703 +
704 +static char *sal2str(len_and_sockaddr *a)
705 +{
706 + static char buf[INET6_ADDRSTRLEN + 1 + IFNAMSIZ + 1 + 5 + 1];
707 + char *p = buf;
708 +
709 +#if ENABLE_FEATURE_IPV6
710 + if (a->u.sa.sa_family == AF_INET6) {
711 + inet_ntop(AF_INET6, &a->u.sin6.sin6_addr, buf, sizeof(buf));
712 + p += strlen(p);
713 +
714 + if (a->u.sin6.sin6_scope_id) {
715 + if (if_indextoname(a->u.sin6.sin6_scope_id, p + 1)) {
716 + *p++ = '%';
717 + p += strlen(p);
718 + }
719 + }
720 + }
721 + else
722 +#endif
723 + {
724 + inet_ntop(AF_INET, &a->u.sin.sin_addr, buf, sizeof(buf));
725 + p += strlen(p);
726 + }
727 +
728 + sprintf(p, "#%hu", ntohs(a->u.sin.sin_port));
729 +
730 + return buf;
731 +}
732 +
733 +
734 +#if ENABLE_FEATURE_NSLOOKUP_LEDE_LONG_OPTIONS
735 +static const char nslookup_longopts[] ALIGN1 =
736 + "type\0" Required_argument "q"
737 + "querytype\0" Required_argument "q"
738 + "port\0" Required_argument "p"
739 + "retry\0" Required_argument "r"
740 + "timeout\0" Required_argument "t"
741 + "stats\0" Required_argument "s"
742 + ;
743 +#endif
744 +
745 +int nslookup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
746 +int nslookup_main(int argc, char **argv)
747 +{
748 + int rc = 1;
749 + char *ptr, *chr;
750 + struct ns *ns = NULL;
751 + struct query *queries = NULL;
752 + llist_t *type_strings = NULL;
753 + int n_ns = 0, n_queries = 0;
754 + int c, opts, option_index = 0;
755 + int stats = 0;
756 + unsigned int types = 0;
757 + HEADER *header;
758 +
759 +#if ENABLE_FEATURE_NSLOOKUP_LEDE_LONG_OPTIONS
760 + applet_long_options = nslookup_longopts;
761 +#endif
762 +
763 + opts = getopt32(argv, "+q:*p:+r:+t:+s",
764 + &type_strings, &default_port,
765 + &default_retry, &default_timeout);
766 +
767 + while (type_strings) {
768 + ptr = llist_pop(&type_strings);
769 +
770 + /* skip leading text, e.g. when invoked with -querytype=AAAA */
771 + if ((chr = strchr(ptr, '=')) != NULL) {
772 + ptr = chr;
773 + *ptr++ = 0;
774 + }
775 +
776 + for (c = 0; qtypes[c].name; c++)
777 + if (!strcmp(qtypes[c].name, ptr))
778 + break;
779 +
780 + if (!qtypes[c].name) {
781 + fprintf(stderr, "Invalid query type \"%s\"\n", ptr);
782 + goto out;
783 + }
784 +
785 + types |= (1 << c);
786 + }
787 +
788 + if (default_port > 65535) {
789 + fprintf(stderr, "Invalid server port\n");
790 + goto out;
791 + }
792 +
793 + if (!default_retry) {
794 + fprintf(stderr, "Invalid retry value\n");
795 + goto out;
796 + }
797 +
798 + if (!default_timeout) {
799 + fprintf(stderr, "Invalid timeout value\n");
800 + goto out;
801 + }
802 +
803 + stats = (opts & 16);
804 +
805 + if (optind >= argc)
806 + bb_show_usage();
807 +
808 + for (option_index = optind;
809 + option_index < ((argc - optind) > 1 ? argc - 1 : argc);
810 + option_index++) {
811 +
812 + /* No explicit type given, guess query type.
813 + * If we can convert the domain argument into a ptr (means that
814 + * inet_pton() could read it) we assume a PTR request, else
815 + * we issue A queries. */
816 + if (types == 0) {
817 + ptr = make_ptr(argv[option_index]);
818 +
819 + if (ptr)
820 + add_query(&queries, &n_queries, T_PTR, ptr);
821 + else
822 + add_query(&queries, &n_queries, T_A, argv[option_index]);
823 + }
824 + else {
825 + for (c = 0; qtypes[c].name; c++)
826 + if (types & (1 << c))
827 + add_query(&queries, &n_queries, qtypes[c].type,
828 + argv[option_index]);
829 + }
830 + }
831 +
832 + /* Use given DNS server if present */
833 + if (option_index < argc) {
834 + if (!add_ns(&ns, &n_ns, argv[option_index])) {
835 + fprintf(stderr, "Invalid NS server address \"%s\": %s\n",
836 + argv[option_index], strerror(errno));
837 + goto out;
838 + }
839 + }
840 + else {
841 + parse_resolvconf(&ns, &n_ns);
842 + }
843 +
844 + /* Fall back to localhost if we could not find NS in resolv.conf */
845 + if (n_ns == 0) {
846 + add_ns(&ns, &n_ns, "127.0.0.1");
847 + }
848 +
849 + for (c = 0; c < n_ns; c++) {
850 + rc = send_queries(&ns[c], 1, queries, n_queries);
851 +
852 + if (rc < 0) {
853 + fprintf(stderr, "Failed to send queries: %s\n", strerror(errno));
854 + goto out;
855 + } else if (rc > 0) {
856 + break;
857 + }
858 + }
859 +
860 + if (c >= n_ns) {
861 + fprintf(stderr,
862 + ";; connection timed out; no servers could be reached\n\n");
863 +
864 + return 1;
865 + }
866 +
867 + printf("Server:\t\t%s\n", ns[c].name);
868 + printf("Address:\t%s\n", sal2str(&ns[c].addr));
869 +
870 + if (stats) {
871 + printf("Replies:\t%d\n", ns[c].replies);
872 + printf("Failures:\t%d\n", ns[c].failures);
873 + }
874 +
875 + printf("\n");
876 +
877 + for (rc = 0; rc < n_queries; rc++) {
878 + if (stats) {
879 + printf("Query #%d completed in %lums:\n", rc, queries[rc].latency);
880 + }
881 +
882 + if (queries[rc].rcode != 0) {
883 + printf("** server can't find %s: %s\n", queries[rc].name,
884 + rcodes[queries[rc].rcode]);
885 + continue;
886 + }
887 +
888 + c = 0;
889 +
890 + if (queries[rc].rlen) {
891 + header = (HEADER *)queries[rc].reply;
892 +
893 + if (!header->aa)
894 + printf("Non-authoritative answer:\n");
895 +
896 + c = parse_reply(queries[rc].reply, queries[rc].rlen);
897 + }
898 +
899 + if (c == 0)
900 + printf("*** Can't find %s: No answer\n", queries[rc].name);
901 + else if (c < 0)
902 + printf("*** Can't find %s: Parse error\n", queries[rc].name);
903 +
904 + printf("\n");
905 + }
906 +
907 + rc = 0;
908 +
909 +out:
910 + if (n_ns)
911 + free(ns);
912 +
913 + if (n_queries)
914 + free(queries);
915 +
916 + return rc;
917 +}
918 --
919 2.11.0
920