123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934 |
- #include <resolv.h>
- #include <net/if.h> /* for IFNAMSIZ */
- #include "libbb.h"
- #include "common_bufsiz.h"
- #if !ENABLE_FEATURE_NSLOOKUP_BIG
- static int print_host(const char *hostname, const char *header)
- {
-
- struct addrinfo *result = NULL;
- int rc;
- struct addrinfo hint;
- memset(&hint, 0 , sizeof(hint));
-
-
- hint.ai_socktype = SOCK_STREAM;
-
- rc = getaddrinfo(hostname, NULL , &hint, &result);
- if (rc == 0) {
- struct addrinfo *cur = result;
- unsigned cnt = 0;
- printf("%-10s %s\n", header, hostname);
-
- while (cur) {
- char *dotted, *revhost;
- dotted = xmalloc_sockaddr2dotted_noport(cur->ai_addr);
- revhost = xmalloc_sockaddr2hostonly_noport(cur->ai_addr);
- printf("Address %u: %s%c", ++cnt, dotted, revhost ? ' ' : '\n');
- if (revhost) {
- puts(revhost);
- if (ENABLE_FEATURE_CLEAN_UP)
- free(revhost);
- }
- if (ENABLE_FEATURE_CLEAN_UP)
- free(dotted);
- cur = cur->ai_next;
- }
- } else {
- #if ENABLE_VERBOSE_RESOLUTION_ERRORS
- bb_error_msg("can't resolve '%s': %s", hostname, gai_strerror(rc));
- #else
- bb_error_msg("can't resolve '%s'", hostname);
- #endif
- }
- if (ENABLE_FEATURE_CLEAN_UP && result)
- freeaddrinfo(result);
- return (rc != 0);
- }
- static void server_print(void)
- {
- char *server;
- struct sockaddr *sa;
- #if ENABLE_FEATURE_IPV6
- sa = (struct sockaddr*)_res._u._ext.nsaddrs[0];
- if (!sa)
- #endif
- sa = (struct sockaddr*)&_res.nsaddr_list[0];
- server = xmalloc_sockaddr2dotted_noport(sa);
- print_host(server, "Server:");
- if (ENABLE_FEATURE_CLEAN_UP)
- free(server);
- bb_putchar('\n');
- }
- static void set_default_dns(const char *server)
- {
- len_and_sockaddr *lsa;
- if (!server)
- return;
-
- lsa = xhost2sockaddr(server, 53);
- if (lsa->u.sa.sa_family == AF_INET) {
- _res.nscount = 1;
-
- _res.nsaddr_list[0] = lsa->u.sin;
- }
- #if ENABLE_FEATURE_IPV6
-
- if (lsa->u.sa.sa_family == AF_INET6) {
-
-
-
- _res._u._ext.nscount = 1;
-
- _res._u._ext.nsaddrs[0] = &lsa->u.sin6;
-
- }
- #endif
- }
- int nslookup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int nslookup_main(int argc, char **argv)
- {
-
- if (!argv[1] || argv[1][0] == '-' || argc > 3)
- bb_show_usage();
-
- res_init();
-
-
-
- set_default_dns(argv[2]);
- server_print();
-
- set_default_dns(argv[2]);
- return print_host(argv[1], "Name:");
- }
- #else
- #if 0
- # define dbg(...) fprintf(stderr, __VA_ARGS__)
- #else
- # define dbg(...) ((void)0)
- #endif
- struct ns {
- const char *name;
- len_and_sockaddr *lsa;
- int failures;
- int replies;
- };
- struct query {
- const char *name;
- unsigned qlen;
- unsigned char query[512];
- };
- static const struct {
- unsigned char type;
- char name[7];
- } qtypes[] = {
- { ns_t_soa, "SOA" },
- { ns_t_ns, "NS" },
- { ns_t_a, "A" },
- #if ENABLE_FEATURE_IPV6
- { ns_t_aaaa, "AAAA" },
- #endif
- { ns_t_cname, "CNAME" },
- { ns_t_mx, "MX" },
- { ns_t_txt, "TXT" },
- { ns_t_ptr, "PTR" },
- { ns_t_any, "ANY" },
- };
- static const char *const rcodes[] = {
- "NOERROR",
- "FORMERR",
- "SERVFAIL",
- "NXDOMAIN",
- "NOTIMP",
- "REFUSED",
- "YXDOMAIN",
- "YXRRSET",
- "NXRRSET",
- "NOTAUTH",
- "NOTZONE",
- "11",
- "12",
- "13",
- "14",
- "15",
- };
- #if ENABLE_FEATURE_IPV6
- static const char v4_mapped[12] = { 0,0,0,0, 0,0,0,0, 0,0,0xff,0xff };
- #endif
- struct globals {
- unsigned default_port;
- unsigned default_retry;
- unsigned default_timeout;
- unsigned query_count;
- unsigned serv_count;
- struct ns *server;
- struct query *query;
- } FIX_ALIASING;
- #define G (*(struct globals*)bb_common_bufsiz1)
- #define INIT_G() do { \
- setup_common_bufsiz(); \
- G.default_port = 53; \
- G.default_retry = 2; \
- G.default_timeout = 5; \
- } while (0)
- enum {
- OPT_debug = (1 << 0),
- };
- static int parse_reply(const unsigned char *msg, size_t len)
- {
- HEADER *header;
- ns_msg handle;
- ns_rr rr;
- int i, n, rdlen;
- const char *format = NULL;
- char astr[INET6_ADDRSTRLEN], dname[MAXDNAME];
- const unsigned char *cp;
- header = (HEADER *)msg;
- if (!header->aa)
- printf("Non-authoritative answer:\n");
- if (ns_initparse(msg, len, &handle) != 0) {
-
- return -1;
- }
- for (i = 0; i < ns_msg_count(handle, ns_s_an); i++) {
- if (ns_parserr(&handle, ns_s_an, i, &rr) != 0) {
-
- return -1;
- }
- rdlen = ns_rr_rdlen(rr);
- switch (ns_rr_type(rr))
- {
- case ns_t_a:
- if (rdlen != 4) {
- dbg("unexpected A record length %d\n", rdlen);
- return -1;
- }
- inet_ntop(AF_INET, ns_rr_rdata(rr), astr, sizeof(astr));
- printf("Name:\t%s\nAddress: %s\n", ns_rr_name(rr), astr);
- break;
- #if ENABLE_FEATURE_IPV6
- case ns_t_aaaa:
- if (rdlen != 16) {
- dbg("unexpected AAAA record length %d\n", rdlen);
- return -1;
- }
- inet_ntop(AF_INET6, ns_rr_rdata(rr), astr, sizeof(astr));
-
- printf("Name:\t%s\nAddress: %s\n", ns_rr_name(rr), astr);
- break;
- #endif
- case ns_t_ns:
- if (!format)
- format = "%s\tnameserver = %s\n";
-
- case ns_t_cname:
- if (!format)
- format = "%s\tcanonical name = %s\n";
-
- case ns_t_ptr:
- if (!format)
- format = "%s\tname = %s\n";
- if (ns_name_uncompress(ns_msg_base(handle), ns_msg_end(handle),
- ns_rr_rdata(rr), dname, sizeof(dname)) < 0
- ) {
-
- return -1;
- }
- printf(format, ns_rr_name(rr), dname);
- break;
- case ns_t_mx:
- if (rdlen < 2) {
- printf("MX record too short\n");
- return -1;
- }
- n = ns_get16(ns_rr_rdata(rr));
- if (ns_name_uncompress(ns_msg_base(handle), ns_msg_end(handle),
- ns_rr_rdata(rr) + 2, dname, sizeof(dname)) < 0
- ) {
-
- return -1;
- }
- printf("%s\tmail exchanger = %d %s\n", ns_rr_name(rr), n, dname);
- break;
- case ns_t_txt:
- if (rdlen < 1) {
-
- return -1;
- }
- n = *(unsigned char *)ns_rr_rdata(rr);
- if (n > 0) {
- memset(dname, 0, sizeof(dname));
- memcpy(dname, ns_rr_rdata(rr) + 1, n);
- printf("%s\ttext = \"%s\"\n", ns_rr_name(rr), dname);
- }
- break;
- case ns_t_soa:
- if (rdlen < 20) {
- dbg("SOA record too short:%d\n", rdlen);
- return -1;
- }
- printf("%s\n", ns_rr_name(rr));
- cp = ns_rr_rdata(rr);
- n = ns_name_uncompress(ns_msg_base(handle), ns_msg_end(handle),
- cp, dname, sizeof(dname));
- if (n < 0) {
-
- return -1;
- }
- printf("\torigin = %s\n", dname);
- cp += n;
- n = ns_name_uncompress(ns_msg_base(handle), ns_msg_end(handle),
- cp, dname, sizeof(dname));
- if (n < 0) {
-
- return -1;
- }
- printf("\tmail addr = %s\n", dname);
- cp += n;
- printf("\tserial = %lu\n", ns_get32(cp));
- cp += 4;
- printf("\trefresh = %lu\n", ns_get32(cp));
- cp += 4;
- printf("\tretry = %lu\n", ns_get32(cp));
- cp += 4;
- printf("\texpire = %lu\n", ns_get32(cp));
- cp += 4;
- printf("\tminimum = %lu\n", ns_get32(cp));
- break;
- default:
- break;
- }
- }
- return i;
- }
- static int send_queries(struct ns *ns)
- {
- unsigned char reply[512];
- uint8_t rcode;
- len_and_sockaddr *local_lsa;
- struct pollfd pfd;
- int servfail_retry = 0;
- int n_replies = 0;
- unsigned retry_interval;
- unsigned timeout = G.default_timeout * 1000;
- unsigned tstart, tsent, tcur;
- pfd.events = POLLIN;
- pfd.fd = xsocket_type(&local_lsa, ns->lsa->u.sa.sa_family, SOCK_DGRAM);
-
- xbind(pfd.fd, &local_lsa->u.sa, local_lsa->len);
- free(local_lsa);
-
- xconnect(pfd.fd, &ns->lsa->u.sa, ns->lsa->len);
- ndelay_on(pfd.fd);
- retry_interval = timeout / G.default_retry;
- tstart = tcur = monotonic_ms();
- goto send;
- while (tcur - tstart < timeout) {
- int qn;
- int recvlen;
- if (tcur - tsent >= retry_interval) {
- send:
- for (qn = 0; qn < G.query_count; qn++) {
- if (G.query[qn].qlen == 0)
- continue;
- if (write(pfd.fd, G.query[qn].query, G.query[qn].qlen) < 0) {
- bb_perror_msg("write to '%s'", ns->name);
- n_replies = -1;
- goto ret;
- }
- dbg("query %u sent\n", qn);
- }
- tsent = tcur;
- servfail_retry = 2 * G.query_count;
- }
-
- if (poll(&pfd, 1, retry_interval - (tcur - tsent)) <= 0)
- goto next;
- recvlen = read(pfd.fd, reply, sizeof(reply));
- if (recvlen < 0) {
- bb_perror_msg("read");
- next:
- tcur = monotonic_ms();
- continue;
- }
- if (ns->replies++ == 0) {
- printf("Server:\t\t%s\n", ns->name);
- printf("Address:\t%s\n\n",
- auto_string(xmalloc_sockaddr2dotted(&ns->lsa->u.sa))
- );
-
-
- }
-
- if (recvlen < 4) {
- dbg("read is too short:%d\n", recvlen);
- goto next;
- }
-
- qn = 0;
- for (;;) {
- if (memcmp(reply, G.query[qn].query, 2) == 0) {
- dbg("response matches query %u\n", qn);
- break;
- }
- if (++qn >= G.query_count) {
- dbg("response does not match any query\n");
- goto next;
- }
- }
- if (G.query[qn].qlen == 0) {
- dbg("dropped duplicate response to query %u\n", qn);
- goto next;
- }
- rcode = reply[3] & 0x0f;
- dbg("query %u rcode:%s\n", qn, rcodes[rcode]);
-
- if (rcode == 2) {
- ns->failures++;
- if (servfail_retry) {
- servfail_retry--;
- write(pfd.fd, G.query[qn].query, G.query[qn].qlen);
- dbg("query %u resent\n", qn);
- goto next;
- }
- }
-
- G.query[qn].qlen = 0;
- tcur = monotonic_ms();
- #if 1
- if (option_mask32 & OPT_debug) {
- printf("Query #%d completed in %ums:\n", qn, tcur - tstart);
- }
- if (rcode != 0) {
- printf("** server can't find %s: %s\n",
- G.query[qn].name, rcodes[rcode]);
- } else {
- if (parse_reply(reply, recvlen) < 0)
- printf("*** Can't find %s: Parse error\n", G.query[qn].name);
- }
- bb_putchar('\n');
- n_replies++;
- if (n_replies >= G.query_count)
- goto ret;
- #else
- G.query[qn].latency = tcur - tstart;
- n_replies++;
- if (qn != save_idx) {
-
- memcpy(G.query[qn].reply, G.query[save_idx].reply, recvlen);
- continue;
- }
-
- for (;;) {
- save_idx++;
- if (save_idx >= G.query_count)
- goto ret;
- if (!G.query[save_idx].rlen)
- break;
- }
- #endif
- }
- ret:
- close(pfd.fd);
- return n_replies;
- }
- static void add_ns(const char *addr)
- {
- struct ns *ns;
- unsigned count;
- dbg("%s: addr:'%s'\n", __func__, addr);
- count = G.serv_count++;
- G.server = xrealloc_vector(G.server, 3, count);
- ns = &G.server[count];
- ns->name = addr;
- ns->lsa = xhost2sockaddr(addr, G.default_port);
-
-
- }
- static void parse_resolvconf(void)
- {
- FILE *resolv;
- resolv = fopen("/etc/resolv.conf", "r");
- if (resolv) {
- char line[128], *p;
- while (fgets(line, sizeof(line), resolv)) {
- p = strtok(line, " \t\n");
- if (!p || strcmp(p, "nameserver") != 0)
- continue;
- p = strtok(NULL, " \t\n");
- if (!p)
- continue;
- add_ns(xstrdup(p));
- }
- fclose(resolv);
- }
- }
- static void add_query(int type, const char *dname)
- {
- struct query *new_q;
- unsigned count;
- ssize_t qlen;
- count = G.query_count++;
- G.query = xrealloc_vector(G.query, 1, count);
- new_q = &G.query[count];
- dbg("new query#%u type %u for '%s'\n", count, type, dname);
- new_q->name = dname;
- qlen = res_mkquery(QUERY, dname, C_IN, type,
- NULL, 0,
- NULL,
- new_q->query, sizeof(new_q->query)
- );
- new_q->qlen = qlen;
- }
- static char *make_ptr(const char *addrstr)
- {
- unsigned char addr[16];
- #if ENABLE_FEATURE_IPV6
- if (inet_pton(AF_INET6, addrstr, addr)) {
- if (memcmp(addr, v4_mapped, 12) != 0) {
- int i;
- char resbuf[80];
- char *ptr = resbuf;
- for (i = 0; i < 16; i++) {
- *ptr++ = 0x20 | bb_hexdigits_upcase[(unsigned char)addr[15 - i] & 0xf];
- *ptr++ = '.';
- *ptr++ = 0x20 | bb_hexdigits_upcase[(unsigned char)addr[15 - i] >> 4];
- *ptr++ = '.';
- }
- strcpy(ptr, "ip6.arpa");
- return xstrdup(resbuf);
- }
- return xasprintf("%u.%u.%u.%u.in-addr.arpa",
- addr[15], addr[14], addr[13], addr[12]);
- }
- #endif
- if (inet_pton(AF_INET, addrstr, addr)) {
- return xasprintf("%u.%u.%u.%u.in-addr.arpa",
- addr[3], addr[2], addr[1], addr[0]);
- }
- return NULL;
- }
- int nslookup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int nslookup_main(int argc UNUSED_PARAM, char **argv)
- {
- unsigned types;
- int rc;
- int err;
- INIT_G();
-
- types = 0;
- argv++;
- for (;;) {
- const char *options =
- "type\0"
- "querytype\0"
- "port\0"
- "retry\0"
- "debug\0"
- "t\0"
- "timeout\0"
- "";
- int i;
- char *arg;
- char *val;
- if (!*argv)
- bb_show_usage();
- if (argv[0][0] != '-')
- break;
-
- arg = (*argv++) + 1;
- val = strchrnul(arg, '=');
- if (*val)
- *val++ = '\0';
- i = index_in_substrings(options, arg);
-
- if (i < 0)
- bb_show_usage();
- if (i <= 1) {
- for (i = 0;; i++) {
- if (i == ARRAY_SIZE(qtypes))
- bb_error_msg_and_die("invalid query type \"%s\"", val);
- if (strcasecmp(qtypes[i].name, val) == 0)
- break;
- }
- types |= (1 << i);
- continue;
- }
- if (i == 2) {
- G.default_port = xatou_range(val, 1, 0xffff);
- }
- if (i == 3) {
- G.default_retry = xatou_range(val, 1, INT_MAX);
- }
- if (i == 4) {
- option_mask32 |= OPT_debug;
- }
- if (i > 4) {
- G.default_timeout = xatou_range(val, 1, INT_MAX / 1000);
- }
- }
- if (types == 0) {
-
- char *ptr;
- ptr = make_ptr(argv[0]);
- if (ptr) {
- add_query(T_PTR, ptr);
- } else {
- add_query(T_A, argv[0]);
- #if ENABLE_FEATURE_IPV6
- add_query(T_AAAA, argv[0]);
- #endif
- }
- } else {
- int c;
- for (c = 0; c < ARRAY_SIZE(qtypes); c++) {
- if (types & (1 << c))
- add_query(qtypes[c].type, argv[0]);
- }
- }
-
- if (argv[1]) {
- if (argv[2])
- bb_show_usage();
- add_ns(argv[1]);
- } else {
- parse_resolvconf();
-
- if (G.serv_count == 0)
- add_ns("127.0.0.1");
- }
- for (rc = 0; rc < G.serv_count;) {
- int c;
- c = send_queries(&G.server[rc]);
- if (c > 0) {
-
- #if 0
- if (option_mask32 & OPT_debug) {
- printf("Replies:\t%d\n", G.server[rc].replies);
- printf("Failures:\t%d\n\n", G.server[rc].failures);
- }
- #endif
- break;
- }
-
-
- rc++;
- if (rc >= G.serv_count) {
- printf(";; connection timed out; no servers could be reached\n\n");
- return EXIT_FAILURE;
- }
- }
- err = 0;
- for (rc = 0; rc < G.query_count; rc++) {
- if (G.query[rc].qlen) {
- printf("*** Can't find %s: No answer\n", G.query[rc].name);
- err = 1;
- }
- }
- if (err)
- bb_putchar('\n');
- if (ENABLE_FEATURE_CLEAN_UP) {
- free(G.server);
- free(G.query);
- }
- return EXIT_SUCCESS;
- }
- #endif
|