summaryrefslogtreecommitdiff
path: root/networking/nslookup.c (plain)
blob: 202356b81a53db01e5d934669b06395d6102bd61
1/* vi: set sw=4 ts=4: */
2/*
3 * Mini nslookup implementation for busybox
4 *
5 * Copyright (C) 1999,2000 by Lineo, inc. and John Beppu
6 * Copyright (C) 1999,2000,2001 by John Beppu <beppu@codepoet.org>
7 *
8 * Correct default name server display and explicit name server option
9 * added by Ben Zeckel <bzeckel@hmc.edu> June 2001
10 *
11 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
12 */
13
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
28#include <resolv.h>
29#include "libbb.h"
30
31#ifdef ANDROID
32# include <netinet/in.h>
33# if ENABLE_FEATURE_IPV6
34# include <netinet/in6.h>
35# endif
36# define ANDROID_CHANGES
37# ifdef BIONIC_L
38# include <arpa/nameser.h>
39# include <dns/include/resolv_private.h>
40# include <dns/resolv/res_private.h>
41# else
42# include <arpa_nameser.h>
43# include <private/resolv_private.h>
44# include <netbsd/resolv/res_private.h>
45# endif
46
47static struct __res_state res_st;
48struct __res_state * __res_state(void)
49{
50 return &res_st;
51}
52#endif
53
54#define EXT(res) ((&res)->_u._ext)
55
56/*
57 * I'm only implementing non-interactive mode;
58 * I totally forgot nslookup even had an interactive mode.
59 *
60 * This applet is the only user of res_init(). Without it,
61 * you may avoid pulling in _res global from libc.
62 */
63
64/* Examples of 'standard' nslookup output
65 * $ nslookup yahoo.com
66 * Server: 128.193.0.10
67 * Address: 128.193.0.10#53
68 *
69 * Non-authoritative answer:
70 * Name: yahoo.com
71 * Address: 216.109.112.135
72 * Name: yahoo.com
73 * Address: 66.94.234.13
74 *
75 * $ nslookup 204.152.191.37
76 * Server: 128.193.4.20
77 * Address: 128.193.4.20#53
78 *
79 * Non-authoritative answer:
80 * 37.191.152.204.in-addr.arpa canonical name = 37.32-27.191.152.204.in-addr.arpa.
81 * 37.32-27.191.152.204.in-addr.arpa name = zeus-pub2.kernel.org.
82 *
83 * Authoritative answers can be found from:
84 * 32-27.191.152.204.in-addr.arpa nameserver = ns1.kernel.org.
85 * 32-27.191.152.204.in-addr.arpa nameserver = ns2.kernel.org.
86 * 32-27.191.152.204.in-addr.arpa nameserver = ns3.kernel.org.
87 * ns1.kernel.org internet address = 140.211.167.34
88 * ns2.kernel.org internet address = 204.152.191.4
89 * ns3.kernel.org internet address = 204.152.191.36
90 */
91
92static int print_host(const char *hostname, const char *header)
93{
94 /* We can't use xhost2sockaddr() - we want to get ALL addresses,
95 * not just one */
96 struct addrinfo *result = NULL;
97 int rc;
98 struct addrinfo hint;
99
100 memset(&hint, 0 , sizeof(hint));
101 /* hint.ai_family = AF_UNSPEC; - zero anyway */
102 /* Needed. Or else we will get each address thrice (or more)
103 * for each possible socket type (tcp,udp,raw...): */
104 hint.ai_socktype = SOCK_STREAM;
105 // hint.ai_flags = AI_CANONNAME;
106 rc = getaddrinfo(hostname, NULL /*service*/, &hint, &result);
107
108 if (rc == 0) {
109 struct addrinfo *cur = result;
110 unsigned cnt = 0;
111
112 printf("%-10s %s\n", header, hostname);
113 // puts(cur->ai_canonname); ?
114 while (cur) {
115 char *dotted, *revhost;
116 dotted = xmalloc_sockaddr2dotted_noport(cur->ai_addr);
117 revhost = xmalloc_sockaddr2hostonly_noport(cur->ai_addr);
118
119 printf("Address %u: %s%c", ++cnt, dotted, revhost ? ' ' : '\n');
120 if (revhost) {
121 puts(revhost);
122 if (ENABLE_FEATURE_CLEAN_UP)
123 free(revhost);
124 }
125 if (ENABLE_FEATURE_CLEAN_UP)
126 free(dotted);
127 cur = cur->ai_next;
128 }
129 } else {
130#if ENABLE_VERBOSE_RESOLUTION_ERRORS
131 bb_error_msg("can't resolve '%s': %s", hostname, gai_strerror(rc));
132#else
133 bb_error_msg("can't resolve '%s'", hostname);
134#endif
135 }
136 if (ENABLE_FEATURE_CLEAN_UP && result)
137 freeaddrinfo(result);
138 return (rc != 0);
139}
140
141/* lookup the default nameserver and display it */
142static void server_print(void)
143{
144 char *server;
145 struct sockaddr *sa = NULL;
146
147#if ENABLE_FEATURE_IPV6
148# ifdef ANDROID
149 if (EXT(_res).ext)
150 sa = (struct sockaddr*) &EXT(_res).ext->nsaddrs[0];
151# else
152 sa = (struct sockaddr*)_res._u._ext.nsaddrs[0];
153# endif
154
155 if (!sa)
156#endif
157 sa = (struct sockaddr*) &_res.nsaddr_list[0];
158 server = xmalloc_sockaddr2dotted_noport(sa);
159
160 print_host(server, "Server:");
161 if (ENABLE_FEATURE_CLEAN_UP)
162 free(server);
163 bb_putchar('\n');
164}
165
166/* alter the global _res nameserver structure to use
167 an explicit dns server instead of what is in /etc/resolv.conf */
168static void set_default_dns(const char *server)
169{
170 len_and_sockaddr *lsa;
171
172 if (!server)
173 return;
174
175 /* NB: this works even with, say, "[::1]:53"! :) */
176 lsa = xhost2sockaddr(server, 53);
177
178 if (lsa->u.sa.sa_family == AF_INET) {
179 _res.nscount = 1;
180 /* struct copy */
181 _res.nsaddr_list[0] = lsa->u.sin;
182 }
183
184#if ENABLE_FEATURE_IPV6
185 /* Hoped libc can cope with IPv4 address there too.
186 * No such luck, glibc 2.4 segfaults even with IPv6,
187 * maybe I misunderstand how to make glibc use IPv6 addr?
188 * (uclibc 0.9.31+ should work) */
189 if (lsa->u.sa.sa_family == AF_INET6) {
190 // glibc neither SEGVs nor sends any dgrams with this
191 // (strace shows no socket ops):
192 //_res.nscount = 0;
193 #ifdef ANDROID
194 if (EXT(_res).ext) {
195 EXT(_res).nscount = 1;
196 memcpy(&EXT(_res).ext->nsaddrs[0].sin6, &lsa->u.sin6,
197 sizeof(struct sockaddr_in6));
198 }
199 #else
200 /* store a pointer to part of malloc'ed lsa */
201 _res._u._ext.nscount = 1;
202 _res._u._ext.nsaddrs[0] = &lsa->u.sin6;
203 /* must not free(lsa)! */
204 #endif
205 }
206#endif
207}
208
209int nslookup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
210int nslookup_main(int argc, char **argv)
211{
212 /* We allow 1 or 2 arguments.
213 * The first is the name to be looked up and the second is an
214 * optional DNS server with which to do the lookup.
215 * More than 3 arguments is an error to follow the pattern of the
216 * standard nslookup */
217 if (!argv[1] || argv[1][0] == '-' || argc > 3)
218 bb_show_usage();
219
220 /* initialize DNS structure _res used in printing the default
221 * name server and in the explicit name server option feature. */
222 res_init();
223
224#ifdef ANDROID
225 res_ninit(&_res);
226#endif
227
228 /* rfc2133 says this enables IPv6 lookups */
229 /* (but it also says "may be enabled in /etc/resolv.conf") */
230 /*_res.options |= RES_USE_INET6;*/
231
232 set_default_dns(argv[2]);
233
234 server_print();
235
236 /* getaddrinfo and friends are free to request a resolver
237 * reinitialization. Just in case, set_default_dns() again
238 * after getaddrinfo (in server_print). This reportedly helps
239 * with bug 675 "nslookup does not properly use second argument"
240 * at least on Debian Wheezy and Openwrt AA (eglibc based).
241 */
242 set_default_dns(argv[2]);
243
244 return print_host(argv[1], "Name:");
245}
246