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