Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 2 | |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 3 | //config:config NSLOOKUP |
Denys Vlasenko | ae178ce | 2017-07-19 14:32:54 +0200 | [diff] [blame] | 4 | //config: bool "nslookup (4.5 kb)" |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 5 | //config: default y |
| 6 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 7 | //config: nslookup is a tool to query Internet name servers. |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 8 | //config: |
| 9 | //config:config NSLOOKUP_BIG |
| 10 | //config: bool "Use internal resolver code instead of libc" |
| 11 | //config: depends on NSLOOKUP |
| 12 | //config: default y |
| 13 | //config: |
| 14 | //config:config FEATURE_NSLOOKUP_LONG_OPTIONS |
| 15 | //config: bool "Enable long options" |
| 16 | //config: default y |
| 17 | //config: depends on NSLOOKUP_BIG && LONG_OPTS |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 18 | |
| 19 | //applet:IF_NSLOOKUP(APPLET(nslookup, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 20 | |
| 21 | //kbuild:lib-$(CONFIG_NSLOOKUP) += nslookup.o |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 22 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 23 | //usage:#define nslookup_trivial_usage |
| 24 | //usage: "[HOST] [SERVER]" |
| 25 | //usage:#define nslookup_full_usage "\n\n" |
| 26 | //usage: "Query the nameserver for the IP address of the given HOST\n" |
| 27 | //usage: "optionally using a specified DNS server" |
| 28 | //usage: |
| 29 | //usage:#define nslookup_example_usage |
| 30 | //usage: "$ nslookup localhost\n" |
| 31 | //usage: "Server: default\n" |
| 32 | //usage: "Address: default\n" |
| 33 | //usage: "\n" |
| 34 | //usage: "Name: debian\n" |
| 35 | //usage: "Address: 127.0.0.1\n" |
| 36 | |
Eric Andersen | dab3d46 | 2001-06-12 22:21:24 +0000 | [diff] [blame] | 37 | #include <resolv.h> |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 38 | #include <net/if.h> /* for IFNAMSIZ */ |
| 39 | //#include <arpa/inet.h> |
| 40 | //#include <netdb.h> |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 41 | #include "libbb.h" |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 42 | #include "common_bufsiz.h" |
| 43 | |
| 44 | |
| 45 | #if !ENABLE_NSLOOKUP_BIG |
| 46 | |
| 47 | /* |
| 48 | * Mini nslookup implementation for busybox |
| 49 | * |
| 50 | * Copyright (C) 1999,2000 by Lineo, inc. and John Beppu |
| 51 | * Copyright (C) 1999,2000,2001 by John Beppu <beppu@codepoet.org> |
| 52 | * |
| 53 | * Correct default name server display and explicit name server option |
| 54 | * added by Ben Zeckel <bzeckel@hmc.edu> June 2001 |
| 55 | * |
| 56 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
| 57 | */ |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 58 | |
John Beppu | 50bc101 | 2000-01-30 09:47:16 +0000 | [diff] [blame] | 59 | /* |
Denis Vlasenko | 391ffa1 | 2008-11-04 21:44:28 +0000 | [diff] [blame] | 60 | * I'm only implementing non-interactive mode; |
| 61 | * I totally forgot nslookup even had an interactive mode. |
| 62 | * |
| 63 | * This applet is the only user of res_init(). Without it, |
| 64 | * you may avoid pulling in _res global from libc. |
John Beppu | 50bc101 | 2000-01-30 09:47:16 +0000 | [diff] [blame] | 65 | */ |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 66 | |
Denis Vlasenko | ebe578a | 2006-10-26 17:17:59 +0000 | [diff] [blame] | 67 | /* Examples of 'standard' nslookup output |
| 68 | * $ nslookup yahoo.com |
| 69 | * Server: 128.193.0.10 |
| 70 | * Address: 128.193.0.10#53 |
Denis Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 71 | * |
Denis Vlasenko | ebe578a | 2006-10-26 17:17:59 +0000 | [diff] [blame] | 72 | * Non-authoritative answer: |
| 73 | * Name: yahoo.com |
| 74 | * Address: 216.109.112.135 |
| 75 | * Name: yahoo.com |
| 76 | * Address: 66.94.234.13 |
| 77 | * |
| 78 | * $ nslookup 204.152.191.37 |
| 79 | * Server: 128.193.4.20 |
| 80 | * Address: 128.193.4.20#53 |
Denis Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 81 | * |
Denis Vlasenko | ebe578a | 2006-10-26 17:17:59 +0000 | [diff] [blame] | 82 | * Non-authoritative answer: |
| 83 | * 37.191.152.204.in-addr.arpa canonical name = 37.32-27.191.152.204.in-addr.arpa. |
| 84 | * 37.32-27.191.152.204.in-addr.arpa name = zeus-pub2.kernel.org. |
Denis Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 85 | * |
Denis Vlasenko | ebe578a | 2006-10-26 17:17:59 +0000 | [diff] [blame] | 86 | * Authoritative answers can be found from: |
| 87 | * 32-27.191.152.204.in-addr.arpa nameserver = ns1.kernel.org. |
| 88 | * 32-27.191.152.204.in-addr.arpa nameserver = ns2.kernel.org. |
| 89 | * 32-27.191.152.204.in-addr.arpa nameserver = ns3.kernel.org. |
| 90 | * ns1.kernel.org internet address = 140.211.167.34 |
| 91 | * ns2.kernel.org internet address = 204.152.191.4 |
| 92 | * ns3.kernel.org internet address = 204.152.191.36 |
| 93 | */ |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 94 | |
Denis Vlasenko | ebe578a | 2006-10-26 17:17:59 +0000 | [diff] [blame] | 95 | static int print_host(const char *hostname, const char *header) |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 96 | { |
Denis Vlasenko | 42823d5 | 2007-02-04 02:39:08 +0000 | [diff] [blame] | 97 | /* We can't use xhost2sockaddr() - we want to get ALL addresses, |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 98 | * not just one */ |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 99 | struct addrinfo *result = NULL; |
| 100 | int rc; |
| 101 | struct addrinfo hint; |
| 102 | |
| 103 | memset(&hint, 0 , sizeof(hint)); |
| 104 | /* hint.ai_family = AF_UNSPEC; - zero anyway */ |
| 105 | /* Needed. Or else we will get each address thrice (or more) |
| 106 | * for each possible socket type (tcp,udp,raw...): */ |
| 107 | hint.ai_socktype = SOCK_STREAM; |
| 108 | // hint.ai_flags = AI_CANONNAME; |
| 109 | rc = getaddrinfo(hostname, NULL /*service*/, &hint, &result); |
| 110 | |
Vitaly Magerya | 7f4b769 | 2011-03-22 20:14:26 +0100 | [diff] [blame] | 111 | if (rc == 0) { |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 112 | struct addrinfo *cur = result; |
| 113 | unsigned cnt = 0; |
| 114 | |
| 115 | printf("%-10s %s\n", header, hostname); |
Denis Vlasenko | feb7ae7 | 2007-10-01 12:05:12 +0000 | [diff] [blame] | 116 | // puts(cur->ai_canonname); ? |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 117 | while (cur) { |
| 118 | char *dotted, *revhost; |
Denis Vlasenko | a27a11b | 2007-08-18 14:16:39 +0000 | [diff] [blame] | 119 | dotted = xmalloc_sockaddr2dotted_noport(cur->ai_addr); |
| 120 | revhost = xmalloc_sockaddr2hostonly_noport(cur->ai_addr); |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 121 | |
| 122 | printf("Address %u: %s%c", ++cnt, dotted, revhost ? ' ' : '\n'); |
| 123 | if (revhost) { |
| 124 | puts(revhost); |
| 125 | if (ENABLE_FEATURE_CLEAN_UP) |
| 126 | free(revhost); |
| 127 | } |
| 128 | if (ENABLE_FEATURE_CLEAN_UP) |
| 129 | free(dotted); |
| 130 | cur = cur->ai_next; |
| 131 | } |
| 132 | } else { |
| 133 | #if ENABLE_VERBOSE_RESOLUTION_ERRORS |
Denis Vlasenko | 5de9e9c | 2007-01-22 22:46:04 +0000 | [diff] [blame] | 134 | bb_error_msg("can't resolve '%s': %s", hostname, gai_strerror(rc)); |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 135 | #else |
| 136 | bb_error_msg("can't resolve '%s'", hostname); |
| 137 | #endif |
| 138 | } |
Vitaly Magerya | 7f4b769 | 2011-03-22 20:14:26 +0100 | [diff] [blame] | 139 | if (ENABLE_FEATURE_CLEAN_UP && result) |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 140 | freeaddrinfo(result); |
| 141 | return (rc != 0); |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 144 | /* lookup the default nameserver and display it */ |
| 145 | static void server_print(void) |
| 146 | { |
| 147 | char *server; |
Denis Vlasenko | 3f5f246 | 2008-11-16 19:02:26 +0000 | [diff] [blame] | 148 | struct sockaddr *sa; |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 149 | |
Denis Vlasenko | 3f5f246 | 2008-11-16 19:02:26 +0000 | [diff] [blame] | 150 | #if ENABLE_FEATURE_IPV6 |
| 151 | sa = (struct sockaddr*)_res._u._ext.nsaddrs[0]; |
| 152 | if (!sa) |
| 153 | #endif |
| 154 | sa = (struct sockaddr*)&_res.nsaddr_list[0]; |
| 155 | server = xmalloc_sockaddr2dotted_noport(sa); |
| 156 | |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 157 | print_host(server, "Server:"); |
| 158 | if (ENABLE_FEATURE_CLEAN_UP) |
| 159 | free(server); |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 160 | bb_putchar('\n'); |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Robert Griebl | 31a2e20 | 2002-07-24 00:56:56 +0000 | [diff] [blame] | 163 | /* alter the global _res nameserver structure to use |
Denis Vlasenko | 391ffa1 | 2008-11-04 21:44:28 +0000 | [diff] [blame] | 164 | an explicit dns server instead of what is in /etc/resolv.conf */ |
Denis Vlasenko | 3f5f246 | 2008-11-16 19:02:26 +0000 | [diff] [blame] | 165 | static void set_default_dns(const char *server) |
Robert Griebl | 31a2e20 | 2002-07-24 00:56:56 +0000 | [diff] [blame] | 166 | { |
Denis Vlasenko | 3f5f246 | 2008-11-16 19:02:26 +0000 | [diff] [blame] | 167 | len_and_sockaddr *lsa; |
Robert Griebl | 31a2e20 | 2002-07-24 00:56:56 +0000 | [diff] [blame] | 168 | |
Denys Vlasenko | d66eb90 | 2013-06-27 01:09:51 +0200 | [diff] [blame] | 169 | if (!server) |
| 170 | return; |
| 171 | |
Denis Vlasenko | 3f5f246 | 2008-11-16 19:02:26 +0000 | [diff] [blame] | 172 | /* NB: this works even with, say, "[::1]:5353"! :) */ |
| 173 | lsa = xhost2sockaddr(server, 53); |
| 174 | |
| 175 | if (lsa->u.sa.sa_family == AF_INET) { |
Robert Griebl | 31a2e20 | 2002-07-24 00:56:56 +0000 | [diff] [blame] | 176 | _res.nscount = 1; |
Denis Vlasenko | 3f5f246 | 2008-11-16 19:02:26 +0000 | [diff] [blame] | 177 | /* struct copy */ |
| 178 | _res.nsaddr_list[0] = lsa->u.sin; |
Robert Griebl | 31a2e20 | 2002-07-24 00:56:56 +0000 | [diff] [blame] | 179 | } |
Denis Vlasenko | 3f5f246 | 2008-11-16 19:02:26 +0000 | [diff] [blame] | 180 | #if ENABLE_FEATURE_IPV6 |
Denis Vlasenko | 249d948 | 2008-11-17 15:36:36 +0000 | [diff] [blame] | 181 | /* Hoped libc can cope with IPv4 address there too. |
| 182 | * No such luck, glibc 2.4 segfaults even with IPv6, |
| 183 | * maybe I misunderstand how to make glibc use IPv6 addr? |
| 184 | * (uclibc 0.9.31+ should work) */ |
| 185 | if (lsa->u.sa.sa_family == AF_INET6) { |
| 186 | // glibc neither SEGVs nor sends any dgrams with this |
| 187 | // (strace shows no socket ops): |
| 188 | //_res.nscount = 0; |
Denis Vlasenko | 3f5f246 | 2008-11-16 19:02:26 +0000 | [diff] [blame] | 189 | _res._u._ext.nscount = 1; |
| 190 | /* store a pointer to part of malloc'ed lsa */ |
| 191 | _res._u._ext.nsaddrs[0] = &lsa->u.sin6; |
| 192 | /* must not free(lsa)! */ |
Denis Vlasenko | 249d948 | 2008-11-17 15:36:36 +0000 | [diff] [blame] | 193 | } |
Denis Vlasenko | 3f5f246 | 2008-11-16 19:02:26 +0000 | [diff] [blame] | 194 | #endif |
Eric Andersen | 39cdf4e | 2004-01-30 22:40:05 +0000 | [diff] [blame] | 195 | } |
Robert Griebl | 31a2e20 | 2002-07-24 00:56:56 +0000 | [diff] [blame] | 196 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 197 | int nslookup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 198 | int nslookup_main(int argc, char **argv) |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 199 | { |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 200 | /* We allow 1 or 2 arguments. |
| 201 | * The first is the name to be looked up and the second is an |
| 202 | * optional DNS server with which to do the lookup. |
| 203 | * More than 3 arguments is an error to follow the pattern of the |
| 204 | * standard nslookup */ |
Denis Vlasenko | 391ffa1 | 2008-11-04 21:44:28 +0000 | [diff] [blame] | 205 | if (!argv[1] || argv[1][0] == '-' || argc > 3) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 206 | bb_show_usage(); |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 207 | |
| 208 | /* initialize DNS structure _res used in printing the default |
| 209 | * name server and in the explicit name server option feature. */ |
| 210 | res_init(); |
| 211 | /* rfc2133 says this enables IPv6 lookups */ |
Denis Vlasenko | 391ffa1 | 2008-11-04 21:44:28 +0000 | [diff] [blame] | 212 | /* (but it also says "may be enabled in /etc/resolv.conf") */ |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 213 | /*_res.options |= RES_USE_INET6;*/ |
| 214 | |
Denys Vlasenko | d66eb90 | 2013-06-27 01:09:51 +0200 | [diff] [blame] | 215 | set_default_dns(argv[2]); |
Robert Griebl | 31a2e20 | 2002-07-24 00:56:56 +0000 | [diff] [blame] | 216 | |
Eric Andersen | fe9888a | 2001-01-20 21:51:21 +0000 | [diff] [blame] | 217 | server_print(); |
Denys Vlasenko | d66eb90 | 2013-06-27 01:09:51 +0200 | [diff] [blame] | 218 | |
| 219 | /* getaddrinfo and friends are free to request a resolver |
| 220 | * reinitialization. Just in case, set_default_dns() again |
| 221 | * after getaddrinfo (in server_print). This reportedly helps |
| 222 | * with bug 675 "nslookup does not properly use second argument" |
| 223 | * at least on Debian Wheezy and Openwrt AA (eglibc based). |
| 224 | */ |
| 225 | set_default_dns(argv[2]); |
| 226 | |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 227 | return print_host(argv[1], "Name:"); |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 228 | } |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 229 | |
| 230 | |
| 231 | #else /****** A version from LEDE / OpenWRT ******/ |
| 232 | |
| 233 | /* |
| 234 | * musl compatible nslookup |
| 235 | * |
| 236 | * Copyright (C) 2017 Jo-Philipp Wich <jo@mein.io> |
| 237 | * |
| 238 | * Permission to use, copy, modify, and/or distribute this software for any |
| 239 | * purpose with or without fee is hereby granted, provided that the above |
| 240 | * copyright notice and this permission notice appear in all copies. |
| 241 | * |
| 242 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 243 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 244 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 245 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 246 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 247 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 248 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 249 | */ |
| 250 | |
| 251 | #if 0 |
| 252 | # define dbg(...) fprintf(stderr, __VA_ARGS__) |
| 253 | #else |
| 254 | # define dbg(...) ((void)0) |
| 255 | #endif |
| 256 | |
| 257 | struct ns { |
| 258 | const char *name; |
Denys Vlasenko | 55bc8e8 | 2018-04-14 17:54:24 +0200 | [diff] [blame] | 259 | len_and_sockaddr *lsa; |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 260 | int failures; |
| 261 | int replies; |
| 262 | }; |
| 263 | |
| 264 | struct query { |
| 265 | const char *name; |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 266 | unsigned qlen, rlen; |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 267 | // unsigned latency; |
| 268 | // uint8_t rcode; |
| 269 | unsigned char query[512]; |
| 270 | // unsigned char reply[512]; |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 271 | }; |
| 272 | |
| 273 | static const struct { |
Denys Vlasenko | a980109 | 2018-04-15 10:52:11 +0200 | [diff] [blame^] | 274 | unsigned char type; |
| 275 | char name[7]; |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 276 | } qtypes[] = { |
| 277 | { ns_t_soa, "SOA" }, |
| 278 | { ns_t_ns, "NS" }, |
| 279 | { ns_t_a, "A" }, |
| 280 | #if ENABLE_FEATURE_IPV6 |
| 281 | { ns_t_aaaa, "AAAA" }, |
| 282 | #endif |
| 283 | { ns_t_cname, "CNAME" }, |
| 284 | { ns_t_mx, "MX" }, |
| 285 | { ns_t_txt, "TXT" }, |
| 286 | { ns_t_ptr, "PTR" }, |
| 287 | { ns_t_any, "ANY" }, |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 288 | }; |
| 289 | |
| 290 | static const char *const rcodes[] = { |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 291 | "NOERROR", // 0 |
| 292 | "FORMERR", // 1 |
| 293 | "SERVFAIL", // 2 |
| 294 | "NXDOMAIN", // 3 |
| 295 | "NOTIMP", // 4 |
| 296 | "REFUSED", // 5 |
| 297 | "YXDOMAIN", // 6 |
| 298 | "YXRRSET", // 7 |
| 299 | "NXRRSET", // 8 |
| 300 | "NOTAUTH", // 9 |
| 301 | "NOTZONE", // 10 |
| 302 | "11", // 11 not assigned |
| 303 | "12", // 12 not assigned |
| 304 | "13", // 13 not assigned |
| 305 | "14", // 14 not assigned |
| 306 | "15", // 15 not assigned |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 307 | }; |
| 308 | |
| 309 | #if ENABLE_FEATURE_IPV6 |
| 310 | static const char v4_mapped[] = { 0,0,0,0,0,0,0,0,0,0,0xff,0xff }; |
| 311 | #endif |
| 312 | |
| 313 | struct globals { |
| 314 | unsigned default_port; |
| 315 | unsigned default_retry; |
| 316 | unsigned default_timeout; |
Denys Vlasenko | 55bc8e8 | 2018-04-14 17:54:24 +0200 | [diff] [blame] | 317 | unsigned serv_count; |
| 318 | struct ns *server; |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 319 | } FIX_ALIASING; |
| 320 | #define G (*(struct globals*)bb_common_bufsiz1) |
| 321 | #define INIT_G() do { \ |
| 322 | setup_common_bufsiz(); \ |
| 323 | G.default_port = 53; \ |
| 324 | G.default_retry = 2; \ |
| 325 | G.default_timeout = 5; \ |
| 326 | } while (0) |
| 327 | |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 328 | enum { |
| 329 | OPT_stats = (1 << 4), |
| 330 | }; |
| 331 | |
| 332 | static int parse_reply(const unsigned char *msg, size_t len) |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 333 | { |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 334 | HEADER *header; |
| 335 | |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 336 | ns_msg handle; |
| 337 | ns_rr rr; |
| 338 | int i, n, rdlen; |
| 339 | const char *format = NULL; |
| 340 | char astr[INET6_ADDRSTRLEN], dname[MAXDNAME]; |
| 341 | const unsigned char *cp; |
| 342 | |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 343 | header = (HEADER *)msg; |
| 344 | if (!header->aa) |
| 345 | printf("Non-authoritative answer:\n"); |
| 346 | |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 347 | if (ns_initparse(msg, len, &handle) != 0) { |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 348 | //printf("Unable to parse reply: %s\n", strerror(errno)); |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 349 | return -1; |
| 350 | } |
| 351 | |
| 352 | for (i = 0; i < ns_msg_count(handle, ns_s_an); i++) { |
| 353 | if (ns_parserr(&handle, ns_s_an, i, &rr) != 0) { |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 354 | //printf("Unable to parse resource record: %s\n", strerror(errno)); |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 355 | return -1; |
| 356 | } |
| 357 | |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 358 | rdlen = ns_rr_rdlen(rr); |
| 359 | |
| 360 | switch (ns_rr_type(rr)) |
| 361 | { |
| 362 | case ns_t_a: |
| 363 | if (rdlen != 4) { |
Denys Vlasenko | cf950cd | 2018-04-14 23:08:23 +0200 | [diff] [blame] | 364 | dbg("unexpected A record length %d\n", rdlen); |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 365 | return -1; |
| 366 | } |
| 367 | inet_ntop(AF_INET, ns_rr_rdata(rr), astr, sizeof(astr)); |
Denys Vlasenko | cf950cd | 2018-04-14 23:08:23 +0200 | [diff] [blame] | 368 | printf("Name:\t%s\nAddress: %s\n", ns_rr_name(rr), astr); |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 369 | break; |
| 370 | |
| 371 | #if ENABLE_FEATURE_IPV6 |
| 372 | case ns_t_aaaa: |
| 373 | if (rdlen != 16) { |
Denys Vlasenko | cf950cd | 2018-04-14 23:08:23 +0200 | [diff] [blame] | 374 | dbg("unexpected AAAA record length %d\n", rdlen); |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 375 | return -1; |
| 376 | } |
| 377 | inet_ntop(AF_INET6, ns_rr_rdata(rr), astr, sizeof(astr)); |
Denys Vlasenko | cf950cd | 2018-04-14 23:08:23 +0200 | [diff] [blame] | 378 | /* bind-utils-9.11.3 uses the same format for A and AAAA answers */ |
| 379 | printf("Name:\t%s\nAddress: %s\n", ns_rr_name(rr), astr); |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 380 | break; |
| 381 | #endif |
| 382 | |
| 383 | case ns_t_ns: |
| 384 | if (!format) |
| 385 | format = "%s\tnameserver = %s\n"; |
| 386 | /* fall through */ |
| 387 | |
| 388 | case ns_t_cname: |
| 389 | if (!format) |
| 390 | format = "%s\tcanonical name = %s\n"; |
| 391 | /* fall through */ |
| 392 | |
| 393 | case ns_t_ptr: |
| 394 | if (!format) |
| 395 | format = "%s\tname = %s\n"; |
| 396 | if (ns_name_uncompress(ns_msg_base(handle), ns_msg_end(handle), |
Denys Vlasenko | cf950cd | 2018-04-14 23:08:23 +0200 | [diff] [blame] | 397 | ns_rr_rdata(rr), dname, sizeof(dname)) < 0 |
| 398 | ) { |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 399 | //printf("Unable to uncompress domain: %s\n", strerror(errno)); |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 400 | return -1; |
| 401 | } |
| 402 | printf(format, ns_rr_name(rr), dname); |
| 403 | break; |
| 404 | |
| 405 | case ns_t_mx: |
| 406 | if (rdlen < 2) { |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 407 | printf("MX record too short\n"); |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 408 | return -1; |
| 409 | } |
| 410 | n = ns_get16(ns_rr_rdata(rr)); |
| 411 | if (ns_name_uncompress(ns_msg_base(handle), ns_msg_end(handle), |
Denys Vlasenko | cf950cd | 2018-04-14 23:08:23 +0200 | [diff] [blame] | 412 | ns_rr_rdata(rr) + 2, dname, sizeof(dname)) < 0 |
| 413 | ) { |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 414 | //printf("Cannot uncompress MX domain: %s\n", strerror(errno)); |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 415 | return -1; |
| 416 | } |
| 417 | printf("%s\tmail exchanger = %d %s\n", ns_rr_name(rr), n, dname); |
| 418 | break; |
| 419 | |
| 420 | case ns_t_txt: |
| 421 | if (rdlen < 1) { |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 422 | //printf("TXT record too short\n"); |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 423 | return -1; |
| 424 | } |
| 425 | n = *(unsigned char *)ns_rr_rdata(rr); |
| 426 | if (n > 0) { |
| 427 | memset(dname, 0, sizeof(dname)); |
| 428 | memcpy(dname, ns_rr_rdata(rr) + 1, n); |
| 429 | printf("%s\ttext = \"%s\"\n", ns_rr_name(rr), dname); |
| 430 | } |
| 431 | break; |
| 432 | |
| 433 | case ns_t_soa: |
| 434 | if (rdlen < 20) { |
Denys Vlasenko | cf950cd | 2018-04-14 23:08:23 +0200 | [diff] [blame] | 435 | dbg("SOA record too short:%d\n", rdlen); |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 436 | return -1; |
| 437 | } |
| 438 | |
| 439 | printf("%s\n", ns_rr_name(rr)); |
| 440 | |
| 441 | cp = ns_rr_rdata(rr); |
| 442 | n = ns_name_uncompress(ns_msg_base(handle), ns_msg_end(handle), |
| 443 | cp, dname, sizeof(dname)); |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 444 | if (n < 0) { |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 445 | //printf("Unable to uncompress domain: %s\n", strerror(errno)); |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 446 | return -1; |
| 447 | } |
| 448 | |
| 449 | printf("\torigin = %s\n", dname); |
| 450 | cp += n; |
| 451 | |
| 452 | n = ns_name_uncompress(ns_msg_base(handle), ns_msg_end(handle), |
| 453 | cp, dname, sizeof(dname)); |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 454 | if (n < 0) { |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 455 | //printf("Unable to uncompress domain: %s\n", strerror(errno)); |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 456 | return -1; |
| 457 | } |
| 458 | |
| 459 | printf("\tmail addr = %s\n", dname); |
| 460 | cp += n; |
| 461 | |
| 462 | printf("\tserial = %lu\n", ns_get32(cp)); |
| 463 | cp += 4; |
| 464 | |
| 465 | printf("\trefresh = %lu\n", ns_get32(cp)); |
| 466 | cp += 4; |
| 467 | |
| 468 | printf("\tretry = %lu\n", ns_get32(cp)); |
| 469 | cp += 4; |
| 470 | |
| 471 | printf("\texpire = %lu\n", ns_get32(cp)); |
| 472 | cp += 4; |
| 473 | |
| 474 | printf("\tminimum = %lu\n", ns_get32(cp)); |
| 475 | break; |
| 476 | |
| 477 | default: |
| 478 | break; |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | return i; |
| 483 | } |
| 484 | |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 485 | static char *make_ptr(char resbuf[80], const char *addrstr) |
| 486 | { |
| 487 | unsigned char addr[16]; |
| 488 | int i; |
| 489 | |
| 490 | #if ENABLE_FEATURE_IPV6 |
| 491 | if (inet_pton(AF_INET6, addrstr, addr)) { |
| 492 | if (memcmp(addr, v4_mapped, 12) != 0) { |
| 493 | char *ptr = resbuf; |
| 494 | for (i = 0; i < 16; i++) { |
| 495 | *ptr++ = 0x20 | bb_hexdigits_upcase[(unsigned char)addr[15 - i] & 0xf]; |
| 496 | *ptr++ = '.'; |
| 497 | *ptr++ = 0x20 | bb_hexdigits_upcase[(unsigned char)addr[15 - i] >> 4]; |
| 498 | *ptr++ = '.'; |
| 499 | } |
| 500 | strcpy(ptr, "ip6.arpa"); |
| 501 | } |
| 502 | else { |
| 503 | sprintf(resbuf, "%u.%u.%u.%u.in-addr.arpa", |
| 504 | addr[15], addr[14], addr[13], addr[12]); |
| 505 | } |
| 506 | return resbuf; |
| 507 | } |
| 508 | #endif |
| 509 | |
| 510 | if (inet_pton(AF_INET, addrstr, addr)) { |
| 511 | sprintf(resbuf, "%u.%u.%u.%u.in-addr.arpa", |
| 512 | addr[3], addr[2], addr[1], addr[0]); |
| 513 | return resbuf; |
| 514 | } |
| 515 | |
| 516 | return NULL; |
| 517 | } |
| 518 | |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 519 | /* |
| 520 | * Function logic borrowed & modified from musl libc, res_msend.c |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 521 | * n_queries is always > 0. |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 522 | */ |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 523 | static int send_queries(struct ns *ns, struct query *query, int n_queries) |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 524 | { |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 525 | unsigned char reply[512]; |
| 526 | uint8_t rcode; |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 527 | len_and_sockaddr *local_lsa; |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 528 | struct pollfd pfd; |
Denys Vlasenko | 55bc8e8 | 2018-04-14 17:54:24 +0200 | [diff] [blame] | 529 | int servfail_retry = 0; |
| 530 | int n_replies = 0; |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 531 | // int save_idx = 0; |
Denys Vlasenko | 55bc8e8 | 2018-04-14 17:54:24 +0200 | [diff] [blame] | 532 | unsigned retry_interval; |
| 533 | unsigned timeout = G.default_timeout * 1000; |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 534 | unsigned tstart, tsent, tcur; |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 535 | |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 536 | pfd.events = POLLIN; |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 537 | pfd.fd = xsocket_type(&local_lsa, ns->lsa->u.sa.sa_family, SOCK_DGRAM); |
| 538 | /* |
| 539 | * local_lsa has "null" address and port 0 now. |
| 540 | * bind() ensures we have a *particular port* selected by kernel |
| 541 | * and remembered in fd, thus later recv(fd) |
| 542 | * receives only packets sent to this port. |
| 543 | */ |
| 544 | xbind(pfd.fd, &local_lsa->u.sa, local_lsa->len); |
| 545 | free(local_lsa); |
| 546 | /* Make read/writes know the destination */ |
| 547 | xconnect(pfd.fd, &ns->lsa->u.sa, ns->lsa->len); |
| 548 | ndelay_on(pfd.fd); |
Denys Vlasenko | 55bc8e8 | 2018-04-14 17:54:24 +0200 | [diff] [blame] | 549 | |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 550 | retry_interval = timeout / G.default_retry; |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 551 | tstart = tcur = monotonic_ms(); |
| 552 | goto send; |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 553 | |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 554 | while (tcur - tstart < timeout) { |
| 555 | int qn; |
| 556 | int recvlen; |
| 557 | |
| 558 | if (tcur - tsent >= retry_interval) { |
| 559 | send: |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 560 | for (qn = 0; qn < n_queries; qn++) { |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 561 | if (query[qn].rlen) |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 562 | continue; |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 563 | if (write(pfd.fd, query[qn].query, query[qn].qlen) < 0) { |
Denys Vlasenko | db93b21 | 2018-04-14 18:11:35 +0200 | [diff] [blame] | 564 | bb_perror_msg("write to '%s'", ns->name); |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 565 | n_replies = -1; /* "no go, try next server" */ |
| 566 | goto ret; |
Denys Vlasenko | db93b21 | 2018-04-14 18:11:35 +0200 | [diff] [blame] | 567 | } |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 568 | dbg("query %u sent\n", qn); |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 569 | } |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 570 | tsent = tcur; |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 571 | servfail_retry = 2 * n_queries; |
| 572 | } |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 573 | |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 574 | /* Wait for a response, or until time to retry */ |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 575 | if (poll(&pfd, 1, retry_interval - (tcur - tsent)) <= 0) |
| 576 | goto next; |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 577 | |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 578 | recvlen = read(pfd.fd, reply, sizeof(reply)); |
| 579 | if (recvlen < 0) { |
| 580 | bb_perror_msg("read"); |
| 581 | next: |
| 582 | tcur = monotonic_ms(); |
| 583 | continue; |
| 584 | } |
Denys Vlasenko | 55bc8e8 | 2018-04-14 17:54:24 +0200 | [diff] [blame] | 585 | |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 586 | if (ns->replies++ == 0) { |
| 587 | printf("Server:\t\t%s\n", ns->name); |
| 588 | printf("Address:\t%s\n\n", |
| 589 | auto_string(xmalloc_sockaddr2dotted(&ns->lsa->u.sa)) |
| 590 | ); |
| 591 | /* In "Address", bind-utils-9.11.3 show port after a hash: "1.2.3.4#53" */ |
| 592 | /* Should we do the same? */ |
| 593 | } |
| 594 | |
| 595 | /* Non-identifiable packet */ |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 596 | if (recvlen < 4) { |
| 597 | dbg("read is too short:%d\n", recvlen); |
| 598 | goto next; |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 599 | } |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 600 | |
| 601 | /* Find which query this answer goes with, if any */ |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 602 | // qn = save_idx; |
| 603 | qn = 0; |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 604 | for (;;) { |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 605 | if (memcmp(reply, query[qn].query, 2) == 0) { |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 606 | dbg("response matches query %u\n", qn); |
| 607 | break; |
| 608 | } |
| 609 | if (++qn >= n_queries) { |
| 610 | dbg("response does not match any query\n"); |
| 611 | goto next; |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | if (query[qn].rlen) { |
| 616 | dbg("dropped duplicate response to query %u\n", qn); |
| 617 | goto next; |
| 618 | } |
| 619 | |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 620 | rcode = reply[3] & 0x0f; |
| 621 | dbg("query %u rcode:%s\n", qn, rcodes[rcode]); |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 622 | |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 623 | /* Retry immediately on SERVFAIL */ |
| 624 | if (rcode == 2) { |
| 625 | ns->failures++; |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 626 | if (servfail_retry) { |
| 627 | servfail_retry--; |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 628 | write(pfd.fd, query[qn].query, query[qn].qlen); |
| 629 | dbg("query %u resent\n", qn); |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 630 | goto next; |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 631 | } |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 632 | } |
| 633 | |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 634 | /* Process reply */ |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 635 | query[qn].rlen = recvlen; |
| 636 | tcur = monotonic_ms(); |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 637 | #if 1 |
| 638 | if (option_mask32 & OPT_stats) { |
| 639 | printf("Query #%d completed in %ums:\n", qn, tcur - tstart); |
| 640 | } |
| 641 | if (rcode != 0) { |
| 642 | printf("** server can't find %s: %s\n", |
| 643 | query[qn].name, rcodes[rcode]); |
| 644 | } else { |
| 645 | if (parse_reply(reply, recvlen) < 0) |
| 646 | printf("*** Can't find %s: Parse error\n", query[qn].name); |
| 647 | } |
| 648 | bb_putchar('\n'); |
| 649 | n_replies++; |
| 650 | if (n_replies >= n_queries) |
| 651 | goto ret; |
| 652 | #else |
| 653 | //used to store replies and process them later |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 654 | query[qn].latency = tcur - tstart; |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 655 | n_replies++; |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 656 | if (qn != save_idx) { |
| 657 | /* "wrong" receive buffer, move to correct one */ |
| 658 | memcpy(query[qn].reply, query[save_idx].reply, recvlen); |
| 659 | continue; |
| 660 | } |
| 661 | /* query[0..save_idx] have replies, move to next one, if exists */ |
| 662 | for (;;) { |
| 663 | save_idx++; |
| 664 | if (save_idx >= n_queries) |
| 665 | goto ret; /* all are full: we have all results */ |
| 666 | if (!query[save_idx].rlen) |
| 667 | break; /* this one is empty */ |
| 668 | } |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 669 | #endif |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 670 | } /* while() */ |
| 671 | |
Denys Vlasenko | 55bc8e8 | 2018-04-14 17:54:24 +0200 | [diff] [blame] | 672 | ret: |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 673 | close(pfd.fd); |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 674 | |
| 675 | return n_replies; |
| 676 | } |
| 677 | |
Denys Vlasenko | 55bc8e8 | 2018-04-14 17:54:24 +0200 | [diff] [blame] | 678 | static void add_ns(const char *addr) |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 679 | { |
Denys Vlasenko | 55bc8e8 | 2018-04-14 17:54:24 +0200 | [diff] [blame] | 680 | struct ns *ns; |
| 681 | unsigned count; |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 682 | |
| 683 | dbg("%s: addr:'%s'\n", __func__, addr); |
| 684 | |
Denys Vlasenko | 55bc8e8 | 2018-04-14 17:54:24 +0200 | [diff] [blame] | 685 | count = G.serv_count++; |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 686 | |
Denys Vlasenko | 55bc8e8 | 2018-04-14 17:54:24 +0200 | [diff] [blame] | 687 | G.server = xrealloc_vector(G.server, /*8=2^3:*/ 3, count); |
| 688 | ns = &G.server[count]; |
| 689 | ns->name = addr; |
| 690 | ns->lsa = xhost2sockaddr(addr, 53); |
| 691 | /*ns->replies = 0; - already is */ |
| 692 | /*ns->failures = 0; - already is */ |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 693 | } |
| 694 | |
Denys Vlasenko | 55bc8e8 | 2018-04-14 17:54:24 +0200 | [diff] [blame] | 695 | static void parse_resolvconf(void) |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 696 | { |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 697 | FILE *resolv; |
| 698 | |
| 699 | resolv = fopen("/etc/resolv.conf", "r"); |
| 700 | if (resolv) { |
| 701 | char line[128], *p; |
| 702 | |
| 703 | while (fgets(line, sizeof(line), resolv)) { |
| 704 | p = strtok(line, " \t\n"); |
| 705 | |
| 706 | if (!p || strcmp(p, "nameserver") != 0) |
| 707 | continue; |
| 708 | |
| 709 | p = strtok(NULL, " \t\n"); |
| 710 | |
| 711 | if (!p) |
| 712 | continue; |
| 713 | |
Denys Vlasenko | 55bc8e8 | 2018-04-14 17:54:24 +0200 | [diff] [blame] | 714 | add_ns(xstrdup(p)); |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | fclose(resolv); |
| 718 | } |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | static struct query *add_query(struct query **queries, int *n_queries, |
| 722 | int type, const char *dname) |
| 723 | { |
| 724 | struct query *new_q; |
| 725 | int pos; |
| 726 | ssize_t qlen; |
| 727 | |
| 728 | pos = *n_queries; |
| 729 | *n_queries = pos + 1; |
| 730 | *queries = new_q = xrealloc(*queries, sizeof(**queries) * (pos + 1)); |
| 731 | |
| 732 | dbg("new query#%u type %u for '%s'\n", pos, type, dname); |
| 733 | new_q += pos; |
| 734 | memset(new_q, 0, sizeof(*new_q)); |
| 735 | |
| 736 | qlen = res_mkquery(QUERY, dname, C_IN, type, NULL, 0, NULL, |
| 737 | new_q->query, sizeof(new_q->query)); |
| 738 | |
| 739 | new_q->qlen = qlen; |
| 740 | new_q->name = dname; |
| 741 | |
| 742 | return new_q; |
| 743 | } |
| 744 | |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 745 | int nslookup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 746 | int nslookup_main(int argc UNUSED_PARAM, char **argv) |
| 747 | { |
| 748 | struct ns *ns; |
| 749 | struct query *queries; |
| 750 | llist_t *type_strings; |
Denys Vlasenko | 55bc8e8 | 2018-04-14 17:54:24 +0200 | [diff] [blame] | 751 | int n_queries; |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 752 | unsigned types; |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 753 | int opts; |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 754 | int rc; |
| 755 | int err; |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 756 | |
| 757 | INIT_G(); |
| 758 | |
| 759 | type_strings = NULL; |
| 760 | #if ENABLE_FEATURE_NSLOOKUP_LONG_OPTIONS |
| 761 | opts = getopt32long(argv, "^" |
| 762 | "+" /* '+': stop at first non-option (why?) */ |
| 763 | "q:*p:+r:+t:+s" |
| 764 | "\0" |
| 765 | "-1:q::", /* minimum 1 arg, -q is a list */ |
| 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 | &type_strings, &G.default_port, |
| 773 | &G.default_retry, &G.default_timeout |
| 774 | ); |
| 775 | #else |
| 776 | opts = getopt32(argv, "^" |
| 777 | "+" /* '+': stop at first non-option (why?) */ |
| 778 | "q:*p:+r:+t:+s" |
| 779 | "\0" |
| 780 | "-1:q::", /* minimum 1 arg, -q is a list */ |
| 781 | &type_strings, &G.default_port, |
| 782 | &G.default_retry, &G.default_timeout |
| 783 | ); |
| 784 | #endif |
| 785 | if (G.default_port > 65535) |
| 786 | bb_error_msg_and_die("invalid server port"); |
| 787 | if (G.default_retry == 0) |
| 788 | bb_error_msg_and_die("invalid retry value"); |
| 789 | if (G.default_timeout == 0) |
| 790 | bb_error_msg_and_die("invalid timeout value"); |
| 791 | |
| 792 | types = 0; |
| 793 | while (type_strings) { |
| 794 | int c; |
| 795 | char *ptr, *chr; |
| 796 | |
| 797 | ptr = llist_pop(&type_strings); |
| 798 | |
| 799 | /* skip leading text, e.g. when invoked with -querytype=AAAA */ |
| 800 | chr = strchr(ptr, '='); |
| 801 | if (chr) |
| 802 | ptr = chr + 1; |
| 803 | |
| 804 | for (c = 0;; c++) { |
Denys Vlasenko | a980109 | 2018-04-15 10:52:11 +0200 | [diff] [blame^] | 805 | if (c == ARRAY_SIZE(qtypes)) |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 806 | bb_error_msg_and_die("invalid query type \"%s\"", ptr); |
| 807 | if (strcmp(qtypes[c].name, ptr) == 0) |
| 808 | break; |
| 809 | } |
| 810 | |
| 811 | types |= (1 << c); |
| 812 | } |
| 813 | |
| 814 | argv += optind; |
| 815 | |
| 816 | n_queries = 0; |
| 817 | queries = NULL; |
| 818 | do { |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 819 | if (types == 0) { |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 820 | /* No explicit type given, guess query type. |
| 821 | * If we can convert the domain argument into a ptr (means that |
| 822 | * inet_pton() could read it) we assume a PTR request, else |
| 823 | * we issue A+AAAA queries and switch to an output format |
| 824 | * mimicking the one of the traditional nslookup applet. |
| 825 | */ |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 826 | char *ptr; |
| 827 | char buf80[80]; |
| 828 | |
| 829 | ptr = make_ptr(buf80, *argv); |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 830 | if (ptr) { |
Denys Vlasenko | 4e73c0f | 2018-04-14 23:18:34 +0200 | [diff] [blame] | 831 | add_query(&queries, &n_queries, T_PTR, xstrdup(ptr)); |
| 832 | } else { |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 833 | add_query(&queries, &n_queries, T_A, *argv); |
| 834 | #if ENABLE_FEATURE_IPV6 |
| 835 | add_query(&queries, &n_queries, T_AAAA, *argv); |
| 836 | #endif |
| 837 | } |
Denys Vlasenko | a980109 | 2018-04-15 10:52:11 +0200 | [diff] [blame^] | 838 | } else { |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 839 | int c; |
Denys Vlasenko | a980109 | 2018-04-15 10:52:11 +0200 | [diff] [blame^] | 840 | for (c = 0; c < ARRAY_SIZE(qtypes); c++) { |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 841 | if (types & (1 << c)) |
Denys Vlasenko | a980109 | 2018-04-15 10:52:11 +0200 | [diff] [blame^] | 842 | add_query(&queries, &n_queries, qtypes[c].type, *argv); |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 843 | } |
| 844 | } |
| 845 | argv++; |
| 846 | } while (argv[0] && argv[1]); |
| 847 | |
| 848 | /* Use given DNS server if present */ |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 849 | if (argv[0]) { |
Denys Vlasenko | 55bc8e8 | 2018-04-14 17:54:24 +0200 | [diff] [blame] | 850 | add_ns(argv[0]); |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 851 | } else { |
Denys Vlasenko | 55bc8e8 | 2018-04-14 17:54:24 +0200 | [diff] [blame] | 852 | parse_resolvconf(); |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 853 | /* Fall back to localhost if we could not find NS in resolv.conf */ |
Denys Vlasenko | 55bc8e8 | 2018-04-14 17:54:24 +0200 | [diff] [blame] | 854 | if (G.serv_count == 0) |
| 855 | add_ns("127.0.0.1"); |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 856 | } |
| 857 | |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 858 | for (rc = 0; rc < G.serv_count;) { |
| 859 | int c; |
| 860 | |
| 861 | c = send_queries(&G.server[rc], queries, n_queries); |
| 862 | if (c > 0) { |
| 863 | /* more than zero replies received */ |
| 864 | if (opts & OPT_stats) { |
| 865 | printf("Replies:\t%d\n", G.server[rc].replies); |
| 866 | printf("Failures:\t%d\n\n", G.server[rc].failures); |
| 867 | } |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 868 | break; |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 869 | //FIXME: we "break" even though some queries may still be not answered, and other servers may know them? |
| 870 | } |
Denys Vlasenko | db93b21 | 2018-04-14 18:11:35 +0200 | [diff] [blame] | 871 | /* c = 0: timed out waiting for replies */ |
| 872 | /* c < 0: error (message already printed) */ |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 873 | rc++; |
Denys Vlasenko | 55bc8e8 | 2018-04-14 17:54:24 +0200 | [diff] [blame] | 874 | if (rc >= G.serv_count) { |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 875 | // |
| 876 | // NB: bind-utils-9.11.3 behavior (all to stdout, not stderr): |
| 877 | // |
| 878 | // $ nslookup gmail.com 8.8.8.8 |
| 879 | // ;; connection timed out; no servers could be reached |
| 880 | // |
| 881 | // $ nslookup -s gmail.com 8.8.8.8; echo EXITCODE:$? |
| 882 | // <~10 sec> |
| 883 | // ;; Connection to 8.8.8.8#53(8.8.8.8) for gmail.com failed: timed out. |
| 884 | // <~10 sec> |
| 885 | // ;; Connection to 8.8.8.8#53(8.8.8.8) for gmail.com failed: timed out. |
| 886 | // <~10 sec> |
| 887 | // ;; connection timed out; no servers could be reached |
| 888 | // ;; Connection to 8.8.8.8#53(8.8.8.8) for gmail.com failed: timed out. |
| 889 | // <empty line> |
| 890 | // EXITCODE:1 |
| 891 | // $ _ |
| 892 | printf(";; connection timed out; no servers could be reached\n\n"); |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 893 | return EXIT_FAILURE; |
| 894 | } |
| 895 | } |
| 896 | |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 897 | err = 0; |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 898 | for (rc = 0; rc < n_queries; rc++) { |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 899 | if (queries[rc].rlen == 0) { |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 900 | printf("*** Can't find %s: No answer\n", queries[rc].name); |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 901 | err = 1; |
| 902 | } |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 903 | } |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 904 | if (err) |
| 905 | bb_putchar('\n'); /* should this affect exicode too? */ |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 906 | |
| 907 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 908 | free(ns); |
| 909 | if (n_queries) |
| 910 | free(queries); |
| 911 | } |
| 912 | |
| 913 | return EXIT_SUCCESS; |
| 914 | } |
| 915 | |
| 916 | #endif |