Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini nslookup implementation for busybox |
| 4 | * |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999,2000 by Lineo, inc. and John Beppu |
| 6 | * Copyright (C) 1999,2000,2001 by John Beppu <beppu@codepoet.org> |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 7 | * |
Eric Andersen | 39cdf4e | 2004-01-30 22:40:05 +0000 | [diff] [blame] | 8 | * Correct default name server display and explicit name server option |
Robert Griebl | 31a2e20 | 2002-07-24 00:56:56 +0000 | [diff] [blame] | 9 | * added by Ben Zeckel <bzeckel@hmc.edu> June 2001 |
| 10 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 11 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 12 | */ |
| 13 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 14 | //usage:#define nslookup_trivial_usage |
| 15 | //usage: "[HOST] [SERVER]" |
| 16 | //usage:#define nslookup_full_usage "\n\n" |
| 17 | //usage: "Query the nameserver for the IP address of the given HOST\n" |
| 18 | //usage: "optionally using a specified DNS server" |
| 19 | //usage: |
| 20 | //usage:#define nslookup_example_usage |
| 21 | //usage: "$ nslookup localhost\n" |
| 22 | //usage: "Server: default\n" |
| 23 | //usage: "Address: default\n" |
| 24 | //usage: "\n" |
| 25 | //usage: "Name: debian\n" |
| 26 | //usage: "Address: 127.0.0.1\n" |
| 27 | |
Eric Andersen | dab3d46 | 2001-06-12 22:21:24 +0000 | [diff] [blame] | 28 | #include <resolv.h> |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 29 | #include "libbb.h" |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 30 | |
John Beppu | 50bc101 | 2000-01-30 09:47:16 +0000 | [diff] [blame] | 31 | /* |
Denis Vlasenko | 391ffa1 | 2008-11-04 21:44:28 +0000 | [diff] [blame] | 32 | * I'm only implementing non-interactive mode; |
| 33 | * I totally forgot nslookup even had an interactive mode. |
| 34 | * |
| 35 | * This applet is the only user of res_init(). Without it, |
| 36 | * you may avoid pulling in _res global from libc. |
John Beppu | 50bc101 | 2000-01-30 09:47:16 +0000 | [diff] [blame] | 37 | */ |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 38 | |
Denis Vlasenko | ebe578a | 2006-10-26 17:17:59 +0000 | [diff] [blame] | 39 | /* Examples of 'standard' nslookup output |
| 40 | * $ nslookup yahoo.com |
| 41 | * Server: 128.193.0.10 |
| 42 | * Address: 128.193.0.10#53 |
Denis Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 43 | * |
Denis Vlasenko | ebe578a | 2006-10-26 17:17:59 +0000 | [diff] [blame] | 44 | * Non-authoritative answer: |
| 45 | * Name: yahoo.com |
| 46 | * Address: 216.109.112.135 |
| 47 | * Name: yahoo.com |
| 48 | * Address: 66.94.234.13 |
| 49 | * |
| 50 | * $ nslookup 204.152.191.37 |
| 51 | * Server: 128.193.4.20 |
| 52 | * Address: 128.193.4.20#53 |
Denis Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 53 | * |
Denis Vlasenko | ebe578a | 2006-10-26 17:17:59 +0000 | [diff] [blame] | 54 | * Non-authoritative answer: |
| 55 | * 37.191.152.204.in-addr.arpa canonical name = 37.32-27.191.152.204.in-addr.arpa. |
| 56 | * 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] | 57 | * |
Denis Vlasenko | ebe578a | 2006-10-26 17:17:59 +0000 | [diff] [blame] | 58 | * Authoritative answers can be found from: |
| 59 | * 32-27.191.152.204.in-addr.arpa nameserver = ns1.kernel.org. |
| 60 | * 32-27.191.152.204.in-addr.arpa nameserver = ns2.kernel.org. |
| 61 | * 32-27.191.152.204.in-addr.arpa nameserver = ns3.kernel.org. |
| 62 | * ns1.kernel.org internet address = 140.211.167.34 |
| 63 | * ns2.kernel.org internet address = 204.152.191.4 |
| 64 | * ns3.kernel.org internet address = 204.152.191.36 |
| 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 | static int print_host(const char *hostname, const char *header) |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 68 | { |
Denis Vlasenko | 42823d5 | 2007-02-04 02:39:08 +0000 | [diff] [blame] | 69 | /* We can't use xhost2sockaddr() - we want to get ALL addresses, |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 70 | * not just one */ |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 71 | struct addrinfo *result = NULL; |
| 72 | int rc; |
| 73 | struct addrinfo hint; |
| 74 | |
| 75 | memset(&hint, 0 , sizeof(hint)); |
| 76 | /* hint.ai_family = AF_UNSPEC; - zero anyway */ |
| 77 | /* Needed. Or else we will get each address thrice (or more) |
| 78 | * for each possible socket type (tcp,udp,raw...): */ |
| 79 | hint.ai_socktype = SOCK_STREAM; |
| 80 | // hint.ai_flags = AI_CANONNAME; |
| 81 | rc = getaddrinfo(hostname, NULL /*service*/, &hint, &result); |
| 82 | |
Vitaly Magerya | 7f4b769 | 2011-03-22 20:14:26 +0100 | [diff] [blame] | 83 | if (rc == 0) { |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 84 | struct addrinfo *cur = result; |
| 85 | unsigned cnt = 0; |
| 86 | |
| 87 | printf("%-10s %s\n", header, hostname); |
Denis Vlasenko | feb7ae7 | 2007-10-01 12:05:12 +0000 | [diff] [blame] | 88 | // puts(cur->ai_canonname); ? |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 89 | while (cur) { |
| 90 | char *dotted, *revhost; |
Denis Vlasenko | a27a11b | 2007-08-18 14:16:39 +0000 | [diff] [blame] | 91 | dotted = xmalloc_sockaddr2dotted_noport(cur->ai_addr); |
| 92 | revhost = xmalloc_sockaddr2hostonly_noport(cur->ai_addr); |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 93 | |
| 94 | printf("Address %u: %s%c", ++cnt, dotted, revhost ? ' ' : '\n'); |
| 95 | if (revhost) { |
| 96 | puts(revhost); |
| 97 | if (ENABLE_FEATURE_CLEAN_UP) |
| 98 | free(revhost); |
| 99 | } |
| 100 | if (ENABLE_FEATURE_CLEAN_UP) |
| 101 | free(dotted); |
| 102 | cur = cur->ai_next; |
| 103 | } |
| 104 | } else { |
| 105 | #if ENABLE_VERBOSE_RESOLUTION_ERRORS |
Denis Vlasenko | 5de9e9c | 2007-01-22 22:46:04 +0000 | [diff] [blame] | 106 | bb_error_msg("can't resolve '%s': %s", hostname, gai_strerror(rc)); |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 107 | #else |
| 108 | bb_error_msg("can't resolve '%s'", hostname); |
| 109 | #endif |
| 110 | } |
Vitaly Magerya | 7f4b769 | 2011-03-22 20:14:26 +0100 | [diff] [blame] | 111 | if (ENABLE_FEATURE_CLEAN_UP && result) |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 112 | freeaddrinfo(result); |
| 113 | return (rc != 0); |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 116 | /* lookup the default nameserver and display it */ |
| 117 | static void server_print(void) |
| 118 | { |
| 119 | char *server; |
Denis Vlasenko | 3f5f246 | 2008-11-16 19:02:26 +0000 | [diff] [blame] | 120 | struct sockaddr *sa; |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 121 | |
Denis Vlasenko | 3f5f246 | 2008-11-16 19:02:26 +0000 | [diff] [blame] | 122 | #if ENABLE_FEATURE_IPV6 |
| 123 | sa = (struct sockaddr*)_res._u._ext.nsaddrs[0]; |
| 124 | if (!sa) |
| 125 | #endif |
| 126 | sa = (struct sockaddr*)&_res.nsaddr_list[0]; |
| 127 | server = xmalloc_sockaddr2dotted_noport(sa); |
| 128 | |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 129 | print_host(server, "Server:"); |
| 130 | if (ENABLE_FEATURE_CLEAN_UP) |
| 131 | free(server); |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 132 | bb_putchar('\n'); |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Robert Griebl | 31a2e20 | 2002-07-24 00:56:56 +0000 | [diff] [blame] | 135 | /* alter the global _res nameserver structure to use |
Denis Vlasenko | 391ffa1 | 2008-11-04 21:44:28 +0000 | [diff] [blame] | 136 | an explicit dns server instead of what is in /etc/resolv.conf */ |
Denis Vlasenko | 3f5f246 | 2008-11-16 19:02:26 +0000 | [diff] [blame] | 137 | static void set_default_dns(const char *server) |
Robert Griebl | 31a2e20 | 2002-07-24 00:56:56 +0000 | [diff] [blame] | 138 | { |
Denis Vlasenko | 3f5f246 | 2008-11-16 19:02:26 +0000 | [diff] [blame] | 139 | len_and_sockaddr *lsa; |
Robert Griebl | 31a2e20 | 2002-07-24 00:56:56 +0000 | [diff] [blame] | 140 | |
Denis Vlasenko | 3f5f246 | 2008-11-16 19:02:26 +0000 | [diff] [blame] | 141 | /* NB: this works even with, say, "[::1]:5353"! :) */ |
| 142 | lsa = xhost2sockaddr(server, 53); |
| 143 | |
| 144 | if (lsa->u.sa.sa_family == AF_INET) { |
Robert Griebl | 31a2e20 | 2002-07-24 00:56:56 +0000 | [diff] [blame] | 145 | _res.nscount = 1; |
Denis Vlasenko | 3f5f246 | 2008-11-16 19:02:26 +0000 | [diff] [blame] | 146 | /* struct copy */ |
| 147 | _res.nsaddr_list[0] = lsa->u.sin; |
Robert Griebl | 31a2e20 | 2002-07-24 00:56:56 +0000 | [diff] [blame] | 148 | } |
Denis Vlasenko | 3f5f246 | 2008-11-16 19:02:26 +0000 | [diff] [blame] | 149 | #if ENABLE_FEATURE_IPV6 |
Denis Vlasenko | 249d948 | 2008-11-17 15:36:36 +0000 | [diff] [blame] | 150 | /* Hoped libc can cope with IPv4 address there too. |
| 151 | * No such luck, glibc 2.4 segfaults even with IPv6, |
| 152 | * maybe I misunderstand how to make glibc use IPv6 addr? |
| 153 | * (uclibc 0.9.31+ should work) */ |
| 154 | if (lsa->u.sa.sa_family == AF_INET6) { |
| 155 | // glibc neither SEGVs nor sends any dgrams with this |
| 156 | // (strace shows no socket ops): |
| 157 | //_res.nscount = 0; |
Denis Vlasenko | 3f5f246 | 2008-11-16 19:02:26 +0000 | [diff] [blame] | 158 | _res._u._ext.nscount = 1; |
| 159 | /* store a pointer to part of malloc'ed lsa */ |
| 160 | _res._u._ext.nsaddrs[0] = &lsa->u.sin6; |
| 161 | /* must not free(lsa)! */ |
Denis Vlasenko | 249d948 | 2008-11-17 15:36:36 +0000 | [diff] [blame] | 162 | } |
Denis Vlasenko | 3f5f246 | 2008-11-16 19:02:26 +0000 | [diff] [blame] | 163 | #endif |
Eric Andersen | 39cdf4e | 2004-01-30 22:40:05 +0000 | [diff] [blame] | 164 | } |
Robert Griebl | 31a2e20 | 2002-07-24 00:56:56 +0000 | [diff] [blame] | 165 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 166 | int nslookup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 167 | int nslookup_main(int argc, char **argv) |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 168 | { |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 169 | /* We allow 1 or 2 arguments. |
| 170 | * The first is the name to be looked up and the second is an |
| 171 | * optional DNS server with which to do the lookup. |
| 172 | * More than 3 arguments is an error to follow the pattern of the |
| 173 | * standard nslookup */ |
Denis Vlasenko | 391ffa1 | 2008-11-04 21:44:28 +0000 | [diff] [blame] | 174 | if (!argv[1] || argv[1][0] == '-' || argc > 3) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 175 | bb_show_usage(); |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 176 | |
| 177 | /* initialize DNS structure _res used in printing the default |
| 178 | * name server and in the explicit name server option feature. */ |
| 179 | res_init(); |
| 180 | /* rfc2133 says this enables IPv6 lookups */ |
Denis Vlasenko | 391ffa1 | 2008-11-04 21:44:28 +0000 | [diff] [blame] | 181 | /* (but it also says "may be enabled in /etc/resolv.conf") */ |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 182 | /*_res.options |= RES_USE_INET6;*/ |
| 183 | |
Denis Vlasenko | 391ffa1 | 2008-11-04 21:44:28 +0000 | [diff] [blame] | 184 | if (argv[2]) |
Robert Griebl | 31a2e20 | 2002-07-24 00:56:56 +0000 | [diff] [blame] | 185 | set_default_dns(argv[2]); |
| 186 | |
Eric Andersen | fe9888a | 2001-01-20 21:51:21 +0000 | [diff] [blame] | 187 | server_print(); |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 188 | return print_host(argv[1], "Name:"); |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 189 | } |