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 | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame^] | 4 | //config: bool "nslookup (9.7 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: |
Denys Vlasenko | d4461ef | 2018-04-15 12:01:46 +0200 | [diff] [blame] | 9 | //config:config FEATURE_NSLOOKUP_BIG |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 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 |
Denys Vlasenko | d4461ef | 2018-04-15 12:01:46 +0200 | [diff] [blame] | 17 | //config: depends on FEATURE_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 |
Denys Vlasenko | 6cdc319 | 2018-04-15 12:49:11 +0200 | [diff] [blame] | 24 | //usage: IF_FEATURE_NSLOOKUP_BIG("[-type=QUERY_TYPE] [-debug] ") "HOST [DNS_SERVER]" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 25 | //usage:#define nslookup_full_usage "\n\n" |
Denys Vlasenko | d4461ef | 2018-04-15 12:01:46 +0200 | [diff] [blame] | 26 | //usage: "Query DNS about HOST" |
Denys Vlasenko | 4b6091f | 2018-04-15 12:57:32 +0200 | [diff] [blame] | 27 | //usage: IF_FEATURE_NSLOOKUP_BIG("\n") |
| 28 | //usage: IF_FEATURE_NSLOOKUP_BIG("\nQUERY_TYPE: soa,ns,a,"IF_FEATURE_IPV6("aaaa,")"cname,mx,txt,ptr,any") |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 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 | |
Denys Vlasenko | d4461ef | 2018-04-15 12:01:46 +0200 | [diff] [blame] | 45 | #if !ENABLE_FEATURE_NSLOOKUP_BIG |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 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 | 71e4b3f | 2018-04-15 19:51:42 +0200 | [diff] [blame] | 266 | unsigned qlen; |
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 |
Denys Vlasenko | 6cdc319 | 2018-04-15 12:49:11 +0200 | [diff] [blame] | 310 | static const char v4_mapped[12] = { 0,0,0,0, 0,0,0,0, 0,0,0xff,0xff }; |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 311 | #endif |
| 312 | |
| 313 | struct globals { |
| 314 | unsigned default_port; |
| 315 | unsigned default_retry; |
| 316 | unsigned default_timeout; |
Denys Vlasenko | 58e43a4 | 2018-04-15 14:10:45 +0200 | [diff] [blame] | 317 | unsigned query_count; |
Denys Vlasenko | 55bc8e8 | 2018-04-14 17:54:24 +0200 | [diff] [blame] | 318 | unsigned serv_count; |
| 319 | struct ns *server; |
Denys Vlasenko | 58e43a4 | 2018-04-15 14:10:45 +0200 | [diff] [blame] | 320 | struct query *query; |
Denys Vlasenko | 9408978 | 2018-08-01 19:42:46 +0200 | [diff] [blame] | 321 | char *search; |
| 322 | smalluint have_search_directive; |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 323 | } FIX_ALIASING; |
| 324 | #define G (*(struct globals*)bb_common_bufsiz1) |
| 325 | #define INIT_G() do { \ |
| 326 | setup_common_bufsiz(); \ |
| 327 | G.default_port = 53; \ |
| 328 | G.default_retry = 2; \ |
| 329 | G.default_timeout = 5; \ |
| 330 | } while (0) |
| 331 | |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 332 | enum { |
Denys Vlasenko | 6cdc319 | 2018-04-15 12:49:11 +0200 | [diff] [blame] | 333 | OPT_debug = (1 << 0), |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 334 | }; |
| 335 | |
| 336 | static int parse_reply(const unsigned char *msg, size_t len) |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 337 | { |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 338 | HEADER *header; |
| 339 | |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 340 | ns_msg handle; |
| 341 | ns_rr rr; |
| 342 | int i, n, rdlen; |
| 343 | const char *format = NULL; |
| 344 | char astr[INET6_ADDRSTRLEN], dname[MAXDNAME]; |
| 345 | const unsigned char *cp; |
| 346 | |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 347 | header = (HEADER *)msg; |
| 348 | if (!header->aa) |
| 349 | printf("Non-authoritative answer:\n"); |
| 350 | |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 351 | if (ns_initparse(msg, len, &handle) != 0) { |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 352 | //printf("Unable to parse reply: %s\n", strerror(errno)); |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 353 | return -1; |
| 354 | } |
| 355 | |
| 356 | for (i = 0; i < ns_msg_count(handle, ns_s_an); i++) { |
| 357 | if (ns_parserr(&handle, ns_s_an, i, &rr) != 0) { |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 358 | //printf("Unable to parse resource record: %s\n", strerror(errno)); |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 359 | return -1; |
| 360 | } |
| 361 | |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 362 | rdlen = ns_rr_rdlen(rr); |
| 363 | |
| 364 | switch (ns_rr_type(rr)) |
| 365 | { |
| 366 | case ns_t_a: |
| 367 | if (rdlen != 4) { |
Denys Vlasenko | cf950cd | 2018-04-14 23:08:23 +0200 | [diff] [blame] | 368 | dbg("unexpected A record length %d\n", rdlen); |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 369 | return -1; |
| 370 | } |
| 371 | inet_ntop(AF_INET, ns_rr_rdata(rr), astr, sizeof(astr)); |
Denys Vlasenko | cf950cd | 2018-04-14 23:08:23 +0200 | [diff] [blame] | 372 | printf("Name:\t%s\nAddress: %s\n", ns_rr_name(rr), astr); |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 373 | break; |
| 374 | |
| 375 | #if ENABLE_FEATURE_IPV6 |
| 376 | case ns_t_aaaa: |
| 377 | if (rdlen != 16) { |
Denys Vlasenko | cf950cd | 2018-04-14 23:08:23 +0200 | [diff] [blame] | 378 | dbg("unexpected AAAA record length %d\n", rdlen); |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 379 | return -1; |
| 380 | } |
| 381 | inet_ntop(AF_INET6, ns_rr_rdata(rr), astr, sizeof(astr)); |
Denys Vlasenko | cf950cd | 2018-04-14 23:08:23 +0200 | [diff] [blame] | 382 | /* bind-utils-9.11.3 uses the same format for A and AAAA answers */ |
| 383 | printf("Name:\t%s\nAddress: %s\n", ns_rr_name(rr), astr); |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 384 | break; |
| 385 | #endif |
| 386 | |
| 387 | case ns_t_ns: |
| 388 | if (!format) |
| 389 | format = "%s\tnameserver = %s\n"; |
| 390 | /* fall through */ |
| 391 | |
| 392 | case ns_t_cname: |
| 393 | if (!format) |
| 394 | format = "%s\tcanonical name = %s\n"; |
| 395 | /* fall through */ |
| 396 | |
| 397 | case ns_t_ptr: |
| 398 | if (!format) |
| 399 | format = "%s\tname = %s\n"; |
| 400 | if (ns_name_uncompress(ns_msg_base(handle), ns_msg_end(handle), |
Denys Vlasenko | cf950cd | 2018-04-14 23:08:23 +0200 | [diff] [blame] | 401 | ns_rr_rdata(rr), dname, sizeof(dname)) < 0 |
| 402 | ) { |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 403 | //printf("Unable to uncompress domain: %s\n", strerror(errno)); |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 404 | return -1; |
| 405 | } |
| 406 | printf(format, ns_rr_name(rr), dname); |
| 407 | break; |
| 408 | |
| 409 | case ns_t_mx: |
| 410 | if (rdlen < 2) { |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 411 | printf("MX record too short\n"); |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 412 | return -1; |
| 413 | } |
| 414 | n = ns_get16(ns_rr_rdata(rr)); |
| 415 | if (ns_name_uncompress(ns_msg_base(handle), ns_msg_end(handle), |
Denys Vlasenko | cf950cd | 2018-04-14 23:08:23 +0200 | [diff] [blame] | 416 | ns_rr_rdata(rr) + 2, dname, sizeof(dname)) < 0 |
| 417 | ) { |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 418 | //printf("Cannot uncompress MX domain: %s\n", strerror(errno)); |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 419 | return -1; |
| 420 | } |
| 421 | printf("%s\tmail exchanger = %d %s\n", ns_rr_name(rr), n, dname); |
| 422 | break; |
| 423 | |
| 424 | case ns_t_txt: |
| 425 | if (rdlen < 1) { |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 426 | //printf("TXT record too short\n"); |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 427 | return -1; |
| 428 | } |
| 429 | n = *(unsigned char *)ns_rr_rdata(rr); |
| 430 | if (n > 0) { |
| 431 | memset(dname, 0, sizeof(dname)); |
| 432 | memcpy(dname, ns_rr_rdata(rr) + 1, n); |
| 433 | printf("%s\ttext = \"%s\"\n", ns_rr_name(rr), dname); |
| 434 | } |
| 435 | break; |
| 436 | |
| 437 | case ns_t_soa: |
| 438 | if (rdlen < 20) { |
Denys Vlasenko | cf950cd | 2018-04-14 23:08:23 +0200 | [diff] [blame] | 439 | dbg("SOA record too short:%d\n", rdlen); |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 440 | return -1; |
| 441 | } |
| 442 | |
| 443 | printf("%s\n", ns_rr_name(rr)); |
| 444 | |
| 445 | cp = ns_rr_rdata(rr); |
| 446 | n = ns_name_uncompress(ns_msg_base(handle), ns_msg_end(handle), |
| 447 | cp, dname, sizeof(dname)); |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 448 | if (n < 0) { |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 449 | //printf("Unable to uncompress domain: %s\n", strerror(errno)); |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 450 | return -1; |
| 451 | } |
| 452 | |
| 453 | printf("\torigin = %s\n", dname); |
| 454 | cp += n; |
| 455 | |
| 456 | n = ns_name_uncompress(ns_msg_base(handle), ns_msg_end(handle), |
| 457 | cp, dname, sizeof(dname)); |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 458 | if (n < 0) { |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 459 | //printf("Unable to uncompress domain: %s\n", strerror(errno)); |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 460 | return -1; |
| 461 | } |
| 462 | |
| 463 | printf("\tmail addr = %s\n", dname); |
| 464 | cp += n; |
| 465 | |
| 466 | printf("\tserial = %lu\n", ns_get32(cp)); |
| 467 | cp += 4; |
| 468 | |
| 469 | printf("\trefresh = %lu\n", ns_get32(cp)); |
| 470 | cp += 4; |
| 471 | |
| 472 | printf("\tretry = %lu\n", ns_get32(cp)); |
| 473 | cp += 4; |
| 474 | |
| 475 | printf("\texpire = %lu\n", ns_get32(cp)); |
| 476 | cp += 4; |
| 477 | |
| 478 | printf("\tminimum = %lu\n", ns_get32(cp)); |
| 479 | break; |
| 480 | |
| 481 | default: |
| 482 | break; |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | return i; |
| 487 | } |
| 488 | |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 489 | /* |
| 490 | * Function logic borrowed & modified from musl libc, res_msend.c |
Denys Vlasenko | 58e43a4 | 2018-04-15 14:10:45 +0200 | [diff] [blame] | 491 | * G.query_count is always > 0. |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 492 | */ |
Denys Vlasenko | 58e43a4 | 2018-04-15 14:10:45 +0200 | [diff] [blame] | 493 | static int send_queries(struct ns *ns) |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 494 | { |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 495 | unsigned char reply[512]; |
| 496 | uint8_t rcode; |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 497 | len_and_sockaddr *local_lsa; |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 498 | struct pollfd pfd; |
Denys Vlasenko | 55bc8e8 | 2018-04-14 17:54:24 +0200 | [diff] [blame] | 499 | int servfail_retry = 0; |
| 500 | int n_replies = 0; |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 501 | // int save_idx = 0; |
Denys Vlasenko | 55bc8e8 | 2018-04-14 17:54:24 +0200 | [diff] [blame] | 502 | unsigned retry_interval; |
| 503 | unsigned timeout = G.default_timeout * 1000; |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 504 | unsigned tstart, tsent, tcur; |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 505 | |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 506 | pfd.events = POLLIN; |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 507 | pfd.fd = xsocket_type(&local_lsa, ns->lsa->u.sa.sa_family, SOCK_DGRAM); |
| 508 | /* |
| 509 | * local_lsa has "null" address and port 0 now. |
| 510 | * bind() ensures we have a *particular port* selected by kernel |
| 511 | * and remembered in fd, thus later recv(fd) |
| 512 | * receives only packets sent to this port. |
| 513 | */ |
| 514 | xbind(pfd.fd, &local_lsa->u.sa, local_lsa->len); |
| 515 | free(local_lsa); |
| 516 | /* Make read/writes know the destination */ |
| 517 | xconnect(pfd.fd, &ns->lsa->u.sa, ns->lsa->len); |
| 518 | ndelay_on(pfd.fd); |
Denys Vlasenko | 55bc8e8 | 2018-04-14 17:54:24 +0200 | [diff] [blame] | 519 | |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 520 | retry_interval = timeout / G.default_retry; |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 521 | tstart = tcur = monotonic_ms(); |
| 522 | goto send; |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 523 | |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 524 | while (tcur - tstart < timeout) { |
| 525 | int qn; |
| 526 | int recvlen; |
| 527 | |
| 528 | if (tcur - tsent >= retry_interval) { |
| 529 | send: |
Denys Vlasenko | 58e43a4 | 2018-04-15 14:10:45 +0200 | [diff] [blame] | 530 | for (qn = 0; qn < G.query_count; qn++) { |
Denys Vlasenko | 71e4b3f | 2018-04-15 19:51:42 +0200 | [diff] [blame] | 531 | if (G.query[qn].qlen == 0) |
| 532 | continue; /* this one was replied already */ |
| 533 | |
Denys Vlasenko | 58e43a4 | 2018-04-15 14:10:45 +0200 | [diff] [blame] | 534 | if (write(pfd.fd, G.query[qn].query, G.query[qn].qlen) < 0) { |
Denys Vlasenko | db93b21 | 2018-04-14 18:11:35 +0200 | [diff] [blame] | 535 | bb_perror_msg("write to '%s'", ns->name); |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 536 | n_replies = -1; /* "no go, try next server" */ |
| 537 | goto ret; |
Denys Vlasenko | db93b21 | 2018-04-14 18:11:35 +0200 | [diff] [blame] | 538 | } |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 539 | dbg("query %u sent\n", qn); |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 540 | } |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 541 | tsent = tcur; |
Denys Vlasenko | 58e43a4 | 2018-04-15 14:10:45 +0200 | [diff] [blame] | 542 | servfail_retry = 2 * G.query_count; |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 543 | } |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 544 | |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 545 | /* Wait for a response, or until time to retry */ |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 546 | if (poll(&pfd, 1, retry_interval - (tcur - tsent)) <= 0) |
| 547 | goto next; |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 548 | |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 549 | recvlen = read(pfd.fd, reply, sizeof(reply)); |
| 550 | if (recvlen < 0) { |
| 551 | bb_perror_msg("read"); |
| 552 | next: |
| 553 | tcur = monotonic_ms(); |
| 554 | continue; |
| 555 | } |
Denys Vlasenko | 55bc8e8 | 2018-04-14 17:54:24 +0200 | [diff] [blame] | 556 | |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 557 | if (ns->replies++ == 0) { |
| 558 | printf("Server:\t\t%s\n", ns->name); |
| 559 | printf("Address:\t%s\n\n", |
| 560 | auto_string(xmalloc_sockaddr2dotted(&ns->lsa->u.sa)) |
| 561 | ); |
| 562 | /* In "Address", bind-utils-9.11.3 show port after a hash: "1.2.3.4#53" */ |
| 563 | /* Should we do the same? */ |
| 564 | } |
| 565 | |
| 566 | /* Non-identifiable packet */ |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 567 | if (recvlen < 4) { |
| 568 | dbg("read is too short:%d\n", recvlen); |
| 569 | goto next; |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 570 | } |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 571 | |
| 572 | /* Find which query this answer goes with, if any */ |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 573 | // qn = save_idx; |
| 574 | qn = 0; |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 575 | for (;;) { |
Denys Vlasenko | 58e43a4 | 2018-04-15 14:10:45 +0200 | [diff] [blame] | 576 | if (memcmp(reply, G.query[qn].query, 2) == 0) { |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 577 | dbg("response matches query %u\n", qn); |
| 578 | break; |
| 579 | } |
Denys Vlasenko | 58e43a4 | 2018-04-15 14:10:45 +0200 | [diff] [blame] | 580 | if (++qn >= G.query_count) { |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 581 | dbg("response does not match any query\n"); |
| 582 | goto next; |
| 583 | } |
| 584 | } |
| 585 | |
Denys Vlasenko | 71e4b3f | 2018-04-15 19:51:42 +0200 | [diff] [blame] | 586 | if (G.query[qn].qlen == 0) { |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 587 | dbg("dropped duplicate response to query %u\n", qn); |
| 588 | goto next; |
| 589 | } |
| 590 | |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 591 | rcode = reply[3] & 0x0f; |
| 592 | dbg("query %u rcode:%s\n", qn, rcodes[rcode]); |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 593 | |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 594 | /* Retry immediately on SERVFAIL */ |
| 595 | if (rcode == 2) { |
| 596 | ns->failures++; |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 597 | if (servfail_retry) { |
| 598 | servfail_retry--; |
Denys Vlasenko | 58e43a4 | 2018-04-15 14:10:45 +0200 | [diff] [blame] | 599 | write(pfd.fd, G.query[qn].query, G.query[qn].qlen); |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 600 | dbg("query %u resent\n", qn); |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 601 | goto next; |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 602 | } |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 603 | } |
| 604 | |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 605 | /* Process reply */ |
Denys Vlasenko | 71e4b3f | 2018-04-15 19:51:42 +0200 | [diff] [blame] | 606 | G.query[qn].qlen = 0; /* flag: "reply received" */ |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 607 | tcur = monotonic_ms(); |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 608 | #if 1 |
Denys Vlasenko | 6cdc319 | 2018-04-15 12:49:11 +0200 | [diff] [blame] | 609 | if (option_mask32 & OPT_debug) { |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 610 | printf("Query #%d completed in %ums:\n", qn, tcur - tstart); |
| 611 | } |
| 612 | if (rcode != 0) { |
| 613 | printf("** server can't find %s: %s\n", |
Denys Vlasenko | 58e43a4 | 2018-04-15 14:10:45 +0200 | [diff] [blame] | 614 | G.query[qn].name, rcodes[rcode]); |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 615 | } else { |
| 616 | if (parse_reply(reply, recvlen) < 0) |
Denys Vlasenko | 58e43a4 | 2018-04-15 14:10:45 +0200 | [diff] [blame] | 617 | printf("*** Can't find %s: Parse error\n", G.query[qn].name); |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 618 | } |
| 619 | bb_putchar('\n'); |
| 620 | n_replies++; |
Denys Vlasenko | 58e43a4 | 2018-04-15 14:10:45 +0200 | [diff] [blame] | 621 | if (n_replies >= G.query_count) |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 622 | goto ret; |
| 623 | #else |
| 624 | //used to store replies and process them later |
Denys Vlasenko | 58e43a4 | 2018-04-15 14:10:45 +0200 | [diff] [blame] | 625 | G.query[qn].latency = tcur - tstart; |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 626 | n_replies++; |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 627 | if (qn != save_idx) { |
| 628 | /* "wrong" receive buffer, move to correct one */ |
Denys Vlasenko | 58e43a4 | 2018-04-15 14:10:45 +0200 | [diff] [blame] | 629 | memcpy(G.query[qn].reply, G.query[save_idx].reply, recvlen); |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 630 | continue; |
| 631 | } |
Denys Vlasenko | 58e43a4 | 2018-04-15 14:10:45 +0200 | [diff] [blame] | 632 | /* G.query[0..save_idx] have replies, move to next one, if exists */ |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 633 | for (;;) { |
| 634 | save_idx++; |
Denys Vlasenko | 58e43a4 | 2018-04-15 14:10:45 +0200 | [diff] [blame] | 635 | if (save_idx >= G.query_count) |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 636 | goto ret; /* all are full: we have all results */ |
Denys Vlasenko | 58e43a4 | 2018-04-15 14:10:45 +0200 | [diff] [blame] | 637 | if (!G.query[save_idx].rlen) |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 638 | break; /* this one is empty */ |
| 639 | } |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 640 | #endif |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 641 | } /* while() */ |
| 642 | |
Denys Vlasenko | 55bc8e8 | 2018-04-14 17:54:24 +0200 | [diff] [blame] | 643 | ret: |
Denys Vlasenko | 71e016d | 2018-04-14 22:53:39 +0200 | [diff] [blame] | 644 | close(pfd.fd); |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 645 | |
| 646 | return n_replies; |
| 647 | } |
| 648 | |
Denys Vlasenko | 55bc8e8 | 2018-04-14 17:54:24 +0200 | [diff] [blame] | 649 | static void add_ns(const char *addr) |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 650 | { |
Denys Vlasenko | 55bc8e8 | 2018-04-14 17:54:24 +0200 | [diff] [blame] | 651 | struct ns *ns; |
| 652 | unsigned count; |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 653 | |
| 654 | dbg("%s: addr:'%s'\n", __func__, addr); |
| 655 | |
Denys Vlasenko | 55bc8e8 | 2018-04-14 17:54:24 +0200 | [diff] [blame] | 656 | count = G.serv_count++; |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 657 | |
Denys Vlasenko | 55bc8e8 | 2018-04-14 17:54:24 +0200 | [diff] [blame] | 658 | G.server = xrealloc_vector(G.server, /*8=2^3:*/ 3, count); |
| 659 | ns = &G.server[count]; |
| 660 | ns->name = addr; |
Denys Vlasenko | d4461ef | 2018-04-15 12:01:46 +0200 | [diff] [blame] | 661 | ns->lsa = xhost2sockaddr(addr, G.default_port); |
Denys Vlasenko | 55bc8e8 | 2018-04-14 17:54:24 +0200 | [diff] [blame] | 662 | /*ns->replies = 0; - already is */ |
| 663 | /*ns->failures = 0; - already is */ |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 664 | } |
| 665 | |
Denys Vlasenko | 55bc8e8 | 2018-04-14 17:54:24 +0200 | [diff] [blame] | 666 | static void parse_resolvconf(void) |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 667 | { |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 668 | FILE *resolv; |
| 669 | |
| 670 | resolv = fopen("/etc/resolv.conf", "r"); |
| 671 | if (resolv) { |
Denys Vlasenko | 9408978 | 2018-08-01 19:42:46 +0200 | [diff] [blame] | 672 | char line[512]; /* "search" is defined to be up to 256 chars */ |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 673 | |
| 674 | while (fgets(line, sizeof(line), resolv)) { |
Denys Vlasenko | 9408978 | 2018-08-01 19:42:46 +0200 | [diff] [blame] | 675 | char *p, *arg; |
| 676 | |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 677 | p = strtok(line, " \t\n"); |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 678 | if (!p) |
| 679 | continue; |
Denys Vlasenko | 9408978 | 2018-08-01 19:42:46 +0200 | [diff] [blame] | 680 | dbg("resolv_key:'%s'\n", p); |
| 681 | arg = strtok(NULL, "\n"); |
| 682 | dbg("resolv_arg:'%s'\n", arg); |
| 683 | if (!arg) |
| 684 | continue; |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 685 | |
Denys Vlasenko | 9408978 | 2018-08-01 19:42:46 +0200 | [diff] [blame] | 686 | if (strcmp(p, "domain") == 0) { |
| 687 | /* domain DOM */ |
| 688 | if (!G.have_search_directive) |
| 689 | goto set_search; |
| 690 | continue; |
| 691 | } |
| 692 | if (strcmp(p, "search") == 0) { |
| 693 | /* search DOM1 DOM2... */ |
| 694 | G.have_search_directive = 1; |
| 695 | set_search: |
| 696 | free(G.search); |
| 697 | G.search = xstrdup(arg); |
| 698 | dbg("search='%s'\n", G.search); |
| 699 | continue; |
| 700 | } |
| 701 | |
| 702 | if (strcmp(p, "nameserver") != 0) |
| 703 | continue; |
| 704 | |
| 705 | /* nameserver DNS */ |
| 706 | add_ns(xstrdup(arg)); |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | fclose(resolv); |
| 710 | } |
Denys Vlasenko | 9408978 | 2018-08-01 19:42:46 +0200 | [diff] [blame] | 711 | |
| 712 | if (!G.search) { |
| 713 | /* default search domain is domain part of hostname */ |
| 714 | char *h = safe_gethostname(); |
| 715 | char *d = strchr(h, '.'); |
| 716 | if (d) { |
| 717 | G.search = d + 1; |
| 718 | dbg("search='%s' (from hostname)\n", G.search); |
| 719 | } |
| 720 | /* else free(h); */ |
| 721 | } |
| 722 | |
| 723 | /* Cater for case of "domain ." in resolv.conf */ |
| 724 | if (G.search && LONE_CHAR(G.search, '.')) |
| 725 | G.search = NULL; |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 726 | } |
| 727 | |
Denys Vlasenko | 58e43a4 | 2018-04-15 14:10:45 +0200 | [diff] [blame] | 728 | static void add_query(int type, const char *dname) |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 729 | { |
| 730 | struct query *new_q; |
Denys Vlasenko | 58e43a4 | 2018-04-15 14:10:45 +0200 | [diff] [blame] | 731 | unsigned count; |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 732 | ssize_t qlen; |
| 733 | |
Denys Vlasenko | 58e43a4 | 2018-04-15 14:10:45 +0200 | [diff] [blame] | 734 | count = G.query_count++; |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 735 | |
Denys Vlasenko | 9408978 | 2018-08-01 19:42:46 +0200 | [diff] [blame] | 736 | G.query = xrealloc_vector(G.query, /*4=2^2:*/ 2, count); |
Denys Vlasenko | 58e43a4 | 2018-04-15 14:10:45 +0200 | [diff] [blame] | 737 | new_q = &G.query[count]; |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 738 | |
Denys Vlasenko | 58e43a4 | 2018-04-15 14:10:45 +0200 | [diff] [blame] | 739 | dbg("new query#%u type %u for '%s'\n", count, type, dname); |
| 740 | new_q->name = dname; |
Denys Vlasenko | 71e4b3f | 2018-04-15 19:51:42 +0200 | [diff] [blame] | 741 | |
| 742 | qlen = res_mkquery(QUERY, dname, C_IN, type, |
| 743 | /*data:*/ NULL, /*datalen:*/ 0, |
| 744 | /*newrr:*/ NULL, |
| 745 | new_q->query, sizeof(new_q->query) |
| 746 | ); |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 747 | new_q->qlen = qlen; |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 748 | } |
| 749 | |
Denys Vlasenko | 9408978 | 2018-08-01 19:42:46 +0200 | [diff] [blame] | 750 | static void add_query_with_search(int type, const char *dname) |
| 751 | { |
| 752 | char *s; |
| 753 | |
| 754 | if (type == T_PTR || !G.search || strchr(dname, '.')) { |
| 755 | add_query(type, dname); |
| 756 | return; |
| 757 | } |
| 758 | |
| 759 | s = G.search; |
| 760 | for (;;) { |
| 761 | char *fullname, *e; |
| 762 | |
| 763 | e = skip_non_whitespace(s); |
| 764 | fullname = xasprintf("%s.%.*s", dname, (int)(e - s), s); |
| 765 | add_query(type, fullname); |
| 766 | s = skip_whitespace(e); |
| 767 | if (!*s) |
| 768 | break; |
| 769 | } |
| 770 | } |
| 771 | |
Denys Vlasenko | c724995 | 2018-04-15 20:04:57 +0200 | [diff] [blame] | 772 | static char *make_ptr(const char *addrstr) |
| 773 | { |
| 774 | unsigned char addr[16]; |
Denys Vlasenko | c724995 | 2018-04-15 20:04:57 +0200 | [diff] [blame] | 775 | |
| 776 | #if ENABLE_FEATURE_IPV6 |
| 777 | if (inet_pton(AF_INET6, addrstr, addr)) { |
| 778 | if (memcmp(addr, v4_mapped, 12) != 0) { |
Denys Vlasenko | 2f7738e | 2018-06-27 14:44:35 +0200 | [diff] [blame] | 779 | int i; |
Denys Vlasenko | c724995 | 2018-04-15 20:04:57 +0200 | [diff] [blame] | 780 | char resbuf[80]; |
| 781 | char *ptr = resbuf; |
| 782 | for (i = 0; i < 16; i++) { |
| 783 | *ptr++ = 0x20 | bb_hexdigits_upcase[(unsigned char)addr[15 - i] & 0xf]; |
| 784 | *ptr++ = '.'; |
| 785 | *ptr++ = 0x20 | bb_hexdigits_upcase[(unsigned char)addr[15 - i] >> 4]; |
| 786 | *ptr++ = '.'; |
| 787 | } |
| 788 | strcpy(ptr, "ip6.arpa"); |
| 789 | return xstrdup(resbuf); |
| 790 | } |
| 791 | return xasprintf("%u.%u.%u.%u.in-addr.arpa", |
| 792 | addr[15], addr[14], addr[13], addr[12]); |
| 793 | } |
| 794 | #endif |
| 795 | |
| 796 | if (inet_pton(AF_INET, addrstr, addr)) { |
| 797 | return xasprintf("%u.%u.%u.%u.in-addr.arpa", |
| 798 | addr[3], addr[2], addr[1], addr[0]); |
| 799 | } |
| 800 | |
| 801 | return NULL; |
| 802 | } |
| 803 | |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 804 | int nslookup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 805 | int nslookup_main(int argc UNUSED_PARAM, char **argv) |
| 806 | { |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 807 | unsigned types; |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 808 | int rc; |
| 809 | int err; |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 810 | |
| 811 | INIT_G(); |
| 812 | |
Denys Vlasenko | d4461ef | 2018-04-15 12:01:46 +0200 | [diff] [blame] | 813 | /* manpage: "Options can also be specified on the command line |
| 814 | * if they precede the arguments and are prefixed with a hyphen." |
| 815 | */ |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 816 | types = 0; |
Denys Vlasenko | d4461ef | 2018-04-15 12:01:46 +0200 | [diff] [blame] | 817 | argv++; |
| 818 | for (;;) { |
| 819 | const char *options = |
| 820 | // bind-utils-9.11.3 accept these: |
| 821 | // class= cl= |
| 822 | // type= ty= querytype= query= qu= q= |
| 823 | // domain= do= |
| 824 | // port= po= |
| 825 | // timeout= t= |
| 826 | // retry= ret= |
| 827 | // ndots= |
| 828 | // recurse |
| 829 | // norecurse |
| 830 | // defname |
| 831 | // nodefname |
| 832 | // vc |
| 833 | // novc |
| 834 | // debug |
| 835 | // nodebug |
| 836 | // d2 |
| 837 | // nod2 |
| 838 | // search |
| 839 | // nosearch |
| 840 | // sil |
| 841 | // fail |
| 842 | // nofail |
| 843 | // ver (prints version and exits) |
| 844 | "type\0" /* 0 */ |
| 845 | "querytype\0" /* 1 */ |
| 846 | "port\0" /* 2 */ |
| 847 | "retry\0" /* 3 */ |
Denys Vlasenko | 6cdc319 | 2018-04-15 12:49:11 +0200 | [diff] [blame] | 848 | "debug\0" /* 4 */ |
Denys Vlasenko | d4461ef | 2018-04-15 12:01:46 +0200 | [diff] [blame] | 849 | "t\0" /* disambiguate with "type": else -t=2 fails */ |
| 850 | "timeout\0" /* 6 */ |
| 851 | ""; |
| 852 | int i; |
| 853 | char *arg; |
| 854 | char *val; |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 855 | |
Denys Vlasenko | d4461ef | 2018-04-15 12:01:46 +0200 | [diff] [blame] | 856 | if (!*argv) |
| 857 | bb_show_usage(); |
| 858 | if (argv[0][0] != '-') |
| 859 | break; |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 860 | |
Denys Vlasenko | d4461ef | 2018-04-15 12:01:46 +0200 | [diff] [blame] | 861 | /* Separate out "=val" part */ |
| 862 | arg = (*argv++) + 1; |
| 863 | val = strchrnul(arg, '='); |
| 864 | if (*val) |
| 865 | *val++ = '\0'; |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 866 | |
Denys Vlasenko | d4461ef | 2018-04-15 12:01:46 +0200 | [diff] [blame] | 867 | i = index_in_substrings(options, arg); |
| 868 | //bb_error_msg("i:%d arg:'%s' val:'%s'", i, arg, val); |
| 869 | if (i < 0) |
| 870 | bb_show_usage(); |
| 871 | |
| 872 | if (i <= 1) { |
| 873 | for (i = 0;; i++) { |
| 874 | if (i == ARRAY_SIZE(qtypes)) |
| 875 | bb_error_msg_and_die("invalid query type \"%s\"", val); |
Denys Vlasenko | 4b6091f | 2018-04-15 12:57:32 +0200 | [diff] [blame] | 876 | if (strcasecmp(qtypes[i].name, val) == 0) |
Denys Vlasenko | d4461ef | 2018-04-15 12:01:46 +0200 | [diff] [blame] | 877 | break; |
| 878 | } |
| 879 | types |= (1 << i); |
| 880 | continue; |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 881 | } |
Denys Vlasenko | d4461ef | 2018-04-15 12:01:46 +0200 | [diff] [blame] | 882 | if (i == 2) { |
| 883 | G.default_port = xatou_range(val, 1, 0xffff); |
| 884 | } |
| 885 | if (i == 3) { |
| 886 | G.default_retry = xatou_range(val, 1, INT_MAX); |
| 887 | } |
| 888 | if (i == 4) { |
Denys Vlasenko | 6cdc319 | 2018-04-15 12:49:11 +0200 | [diff] [blame] | 889 | option_mask32 |= OPT_debug; |
Denys Vlasenko | d4461ef | 2018-04-15 12:01:46 +0200 | [diff] [blame] | 890 | } |
| 891 | if (i > 4) { |
| 892 | G.default_timeout = xatou_range(val, 1, INT_MAX / 1000); |
| 893 | } |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 894 | } |
| 895 | |
Denys Vlasenko | 9408978 | 2018-08-01 19:42:46 +0200 | [diff] [blame] | 896 | /* Use given DNS server if present */ |
| 897 | if (argv[1]) { |
| 898 | if (argv[2]) |
| 899 | bb_show_usage(); |
| 900 | add_ns(argv[1]); |
| 901 | } else { |
| 902 | parse_resolvconf(); |
| 903 | /* Fall back to localhost if we could not find NS in resolv.conf */ |
| 904 | if (G.serv_count == 0) |
| 905 | add_ns("127.0.0.1"); |
| 906 | } |
| 907 | |
Denys Vlasenko | d4461ef | 2018-04-15 12:01:46 +0200 | [diff] [blame] | 908 | if (types == 0) { |
| 909 | /* No explicit type given, guess query type. |
| 910 | * If we can convert the domain argument into a ptr (means that |
| 911 | * inet_pton() could read it) we assume a PTR request, else |
| 912 | * we issue A+AAAA queries and switch to an output format |
| 913 | * mimicking the one of the traditional nslookup applet. |
| 914 | */ |
| 915 | char *ptr; |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 916 | |
Denys Vlasenko | c724995 | 2018-04-15 20:04:57 +0200 | [diff] [blame] | 917 | ptr = make_ptr(argv[0]); |
Denys Vlasenko | d4461ef | 2018-04-15 12:01:46 +0200 | [diff] [blame] | 918 | if (ptr) { |
Denys Vlasenko | c724995 | 2018-04-15 20:04:57 +0200 | [diff] [blame] | 919 | add_query(T_PTR, ptr); |
Denys Vlasenko | a980109 | 2018-04-15 10:52:11 +0200 | [diff] [blame] | 920 | } else { |
Denys Vlasenko | 9408978 | 2018-08-01 19:42:46 +0200 | [diff] [blame] | 921 | add_query_with_search(T_A, argv[0]); |
Denys Vlasenko | d4461ef | 2018-04-15 12:01:46 +0200 | [diff] [blame] | 922 | #if ENABLE_FEATURE_IPV6 |
Denys Vlasenko | 9408978 | 2018-08-01 19:42:46 +0200 | [diff] [blame] | 923 | add_query_with_search(T_AAAA, argv[0]); |
Denys Vlasenko | d4461ef | 2018-04-15 12:01:46 +0200 | [diff] [blame] | 924 | #endif |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 925 | } |
Denys Vlasenko | d4461ef | 2018-04-15 12:01:46 +0200 | [diff] [blame] | 926 | } else { |
| 927 | int c; |
| 928 | for (c = 0; c < ARRAY_SIZE(qtypes); c++) { |
| 929 | if (types & (1 << c)) |
Denys Vlasenko | 9408978 | 2018-08-01 19:42:46 +0200 | [diff] [blame] | 930 | add_query_with_search(qtypes[c].type, argv[0]); |
Denys Vlasenko | d4461ef | 2018-04-15 12:01:46 +0200 | [diff] [blame] | 931 | } |
| 932 | } |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 933 | |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 934 | for (rc = 0; rc < G.serv_count;) { |
| 935 | int c; |
| 936 | |
Denys Vlasenko | 58e43a4 | 2018-04-15 14:10:45 +0200 | [diff] [blame] | 937 | c = send_queries(&G.server[rc]); |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 938 | if (c > 0) { |
| 939 | /* more than zero replies received */ |
Denys Vlasenko | 6cdc319 | 2018-04-15 12:49:11 +0200 | [diff] [blame] | 940 | #if 0 /* which version does this? */ |
| 941 | if (option_mask32 & OPT_debug) { |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 942 | printf("Replies:\t%d\n", G.server[rc].replies); |
| 943 | printf("Failures:\t%d\n\n", G.server[rc].failures); |
| 944 | } |
Denys Vlasenko | 6cdc319 | 2018-04-15 12:49:11 +0200 | [diff] [blame] | 945 | #endif |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 946 | break; |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 947 | //FIXME: we "break" even though some queries may still be not answered, and other servers may know them? |
| 948 | } |
Denys Vlasenko | db93b21 | 2018-04-14 18:11:35 +0200 | [diff] [blame] | 949 | /* c = 0: timed out waiting for replies */ |
| 950 | /* c < 0: error (message already printed) */ |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 951 | rc++; |
Denys Vlasenko | 55bc8e8 | 2018-04-14 17:54:24 +0200 | [diff] [blame] | 952 | if (rc >= G.serv_count) { |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 953 | // |
| 954 | // NB: bind-utils-9.11.3 behavior (all to stdout, not stderr): |
| 955 | // |
| 956 | // $ nslookup gmail.com 8.8.8.8 |
| 957 | // ;; connection timed out; no servers could be reached |
| 958 | // |
Denys Vlasenko | 6cdc319 | 2018-04-15 12:49:11 +0200 | [diff] [blame] | 959 | // Using TCP mode: |
| 960 | // $ nslookup -vc gmail.com 8.8.8.8; echo EXITCODE:$? |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 961 | // <~10 sec> |
| 962 | // ;; Connection to 8.8.8.8#53(8.8.8.8) for gmail.com failed: timed out. |
| 963 | // <~10 sec> |
| 964 | // ;; Connection to 8.8.8.8#53(8.8.8.8) for gmail.com failed: timed out. |
| 965 | // <~10 sec> |
| 966 | // ;; connection timed out; no servers could be reached |
| 967 | // ;; Connection to 8.8.8.8#53(8.8.8.8) for gmail.com failed: timed out. |
| 968 | // <empty line> |
| 969 | // EXITCODE:1 |
| 970 | // $ _ |
| 971 | printf(";; connection timed out; no servers could be reached\n\n"); |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 972 | return EXIT_FAILURE; |
| 973 | } |
| 974 | } |
| 975 | |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 976 | err = 0; |
Denys Vlasenko | 58e43a4 | 2018-04-15 14:10:45 +0200 | [diff] [blame] | 977 | for (rc = 0; rc < G.query_count; rc++) { |
Denys Vlasenko | 71e4b3f | 2018-04-15 19:51:42 +0200 | [diff] [blame] | 978 | if (G.query[rc].qlen) { |
Denys Vlasenko | 58e43a4 | 2018-04-15 14:10:45 +0200 | [diff] [blame] | 979 | printf("*** Can't find %s: No answer\n", G.query[rc].name); |
Denys Vlasenko | 2cf75b3 | 2018-04-15 10:46:44 +0200 | [diff] [blame] | 980 | err = 1; |
| 981 | } |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 982 | } |
Denys Vlasenko | 71e4b3f | 2018-04-15 19:51:42 +0200 | [diff] [blame] | 983 | if (err) /* should this affect exicode too? */ |
| 984 | bb_putchar('\n'); |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 985 | |
| 986 | if (ENABLE_FEATURE_CLEAN_UP) { |
Denys Vlasenko | 58e43a4 | 2018-04-15 14:10:45 +0200 | [diff] [blame] | 987 | free(G.server); |
| 988 | free(G.query); |
Denys Vlasenko | 0dd3be8 | 2018-04-14 14:05:45 +0200 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | return EXIT_SUCCESS; |
| 992 | } |
| 993 | |
| 994 | #endif |