1 /* vi: set sw=4 ts=4: */
3 * Mini nslookup implementation for busybox
5 * Copyright (C) 1999,2000 by Lineo, inc. and John Beppu
6 * Copyright (C) 1999,2000,2001 by John Beppu <beppu@codepoet.org>
8 * Correct default name server display and explicit name server option
9 * added by Ben Zeckel <bzeckel@hmc.edu> June 2001
11 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
13 //config:config NSLOOKUP
14 //config: bool "nslookup (4.5 kb)"
17 //config: nslookup is a tool to query Internet name servers.
19 //applet:IF_NSLOOKUP(APPLET(nslookup, BB_DIR_USR_BIN, BB_SUID_DROP))
21 //kbuild:lib-$(CONFIG_NSLOOKUP) += nslookup.o
23 //usage:#define nslookup_trivial_usage
24 //usage: "[HOST] [SERVER]"
25 //usage:#define nslookup_full_usage "\n\n"
26 //usage: "Query the nameserver for the IP address of the given HOST\n"
27 //usage: "optionally using a specified DNS server"
29 //usage:#define nslookup_example_usage
30 //usage: "$ nslookup localhost\n"
31 //usage: "Server: default\n"
32 //usage: "Address: default\n"
34 //usage: "Name: debian\n"
35 //usage: "Address: 127.0.0.1\n"
41 * I'm only implementing non-interactive mode;
42 * I totally forgot nslookup even had an interactive mode.
44 * This applet is the only user of res_init(). Without it,
45 * you may avoid pulling in _res global from libc.
48 /* Examples of 'standard' nslookup output
49 * $ nslookup yahoo.com
50 * Server: 128.193.0.10
51 * Address: 128.193.0.10#53
53 * Non-authoritative answer:
55 * Address: 216.109.112.135
57 * Address: 66.94.234.13
59 * $ nslookup 204.152.191.37
60 * Server: 128.193.4.20
61 * Address: 128.193.4.20#53
63 * Non-authoritative answer:
64 * 37.191.152.204.in-addr.arpa canonical name = 37.32-27.191.152.204.in-addr.arpa.
65 * 37.32-27.191.152.204.in-addr.arpa name = zeus-pub2.kernel.org.
67 * Authoritative answers can be found from:
68 * 32-27.191.152.204.in-addr.arpa nameserver = ns1.kernel.org.
69 * 32-27.191.152.204.in-addr.arpa nameserver = ns2.kernel.org.
70 * 32-27.191.152.204.in-addr.arpa nameserver = ns3.kernel.org.
71 * ns1.kernel.org internet address = 140.211.167.34
72 * ns2.kernel.org internet address = 204.152.191.4
73 * ns3.kernel.org internet address = 204.152.191.36
76 static int print_host(const char *hostname, const char *header)
78 /* We can't use xhost2sockaddr() - we want to get ALL addresses,
80 struct addrinfo *result = NULL;
84 memset(&hint, 0 , sizeof(hint));
85 /* hint.ai_family = AF_UNSPEC; - zero anyway */
86 /* Needed. Or else we will get each address thrice (or more)
87 * for each possible socket type (tcp,udp,raw...): */
88 hint.ai_socktype = SOCK_STREAM;
89 // hint.ai_flags = AI_CANONNAME;
90 rc = getaddrinfo(hostname, NULL /*service*/, &hint, &result);
93 struct addrinfo *cur = result;
96 printf("%-10s %s\n", header, hostname);
97 // puts(cur->ai_canonname); ?
99 char *dotted, *revhost;
100 dotted = xmalloc_sockaddr2dotted_noport(cur->ai_addr);
101 revhost = xmalloc_sockaddr2hostonly_noport(cur->ai_addr);
103 printf("Address %u: %s%c", ++cnt, dotted, revhost ? ' ' : '\n');
106 if (ENABLE_FEATURE_CLEAN_UP)
109 if (ENABLE_FEATURE_CLEAN_UP)
114 #if ENABLE_VERBOSE_RESOLUTION_ERRORS
115 bb_error_msg("can't resolve '%s': %s", hostname, gai_strerror(rc));
117 bb_error_msg("can't resolve '%s'", hostname);
120 if (ENABLE_FEATURE_CLEAN_UP && result)
121 freeaddrinfo(result);
125 /* lookup the default nameserver and display it */
126 static void server_print(void)
131 #if ENABLE_FEATURE_IPV6
132 sa = (struct sockaddr*)_res._u._ext.nsaddrs[0];
135 sa = (struct sockaddr*)&_res.nsaddr_list[0];
136 server = xmalloc_sockaddr2dotted_noport(sa);
138 print_host(server, "Server:");
139 if (ENABLE_FEATURE_CLEAN_UP)
144 /* alter the global _res nameserver structure to use
145 an explicit dns server instead of what is in /etc/resolv.conf */
146 static void set_default_dns(const char *server)
148 len_and_sockaddr *lsa;
153 /* NB: this works even with, say, "[::1]:5353"! :) */
154 lsa = xhost2sockaddr(server, 53);
156 if (lsa->u.sa.sa_family == AF_INET) {
159 _res.nsaddr_list[0] = lsa->u.sin;
161 #if ENABLE_FEATURE_IPV6
162 /* Hoped libc can cope with IPv4 address there too.
163 * No such luck, glibc 2.4 segfaults even with IPv6,
164 * maybe I misunderstand how to make glibc use IPv6 addr?
165 * (uclibc 0.9.31+ should work) */
166 if (lsa->u.sa.sa_family == AF_INET6) {
167 // glibc neither SEGVs nor sends any dgrams with this
168 // (strace shows no socket ops):
170 _res._u._ext.nscount = 1;
171 /* store a pointer to part of malloc'ed lsa */
172 _res._u._ext.nsaddrs[0] = &lsa->u.sin6;
173 /* must not free(lsa)! */
178 int nslookup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
179 int nslookup_main(int argc, char **argv)
181 /* We allow 1 or 2 arguments.
182 * The first is the name to be looked up and the second is an
183 * optional DNS server with which to do the lookup.
184 * More than 3 arguments is an error to follow the pattern of the
185 * standard nslookup */
186 if (!argv[1] || argv[1][0] == '-' || argc > 3)
189 /* initialize DNS structure _res used in printing the default
190 * name server and in the explicit name server option feature. */
192 /* rfc2133 says this enables IPv6 lookups */
193 /* (but it also says "may be enabled in /etc/resolv.conf") */
194 /*_res.options |= RES_USE_INET6;*/
196 set_default_dns(argv[2]);
200 /* getaddrinfo and friends are free to request a resolver
201 * reinitialization. Just in case, set_default_dns() again
202 * after getaddrinfo (in server_print). This reportedly helps
203 * with bug 675 "nslookup does not properly use second argument"
204 * at least on Debian Wheezy and Openwrt AA (eglibc based).
206 set_default_dns(argv[2]);
208 return print_host(argv[1], "Name:");