1
0

230-add_nslookup_lede.patch 23 KB

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