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