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