blob: 202356b81a53db01e5d934669b06395d6102bd61 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
John Beppub332e772000-01-29 12:59:01 +00002/*
3 * Mini nslookup implementation for busybox
4 *
Eric Andersenbdfd0d72001-10-24 05:00:29 +00005 * Copyright (C) 1999,2000 by Lineo, inc. and John Beppu
6 * Copyright (C) 1999,2000,2001 by John Beppu <beppu@codepoet.org>
John Beppub332e772000-01-29 12:59:01 +00007 *
Eric Andersen39cdf4e2004-01-30 22:40:05 +00008 * Correct default name server display and explicit name server option
Robert Griebl31a2e202002-07-24 00:56:56 +00009 * added by Ben Zeckel <bzeckel@hmc.edu> June 2001
10 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020011 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
John Beppub332e772000-01-29 12:59:01 +000012 */
13
Pere Orga5bc8c002011-04-11 03:29:49 +020014//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 Andersendab3d462001-06-12 22:21:24 +000028#include <resolv.h>
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000029#include "libbb.h"
John Beppub332e772000-01-29 12:59:01 +000030
maxwen27116ba2015-08-14 21:41:28 +020031#ifdef ANDROID
Tanguy Pruvot8aeb3712011-06-30 08:59:26 +020032# include <netinet/in.h>
maxwen27116ba2015-08-14 21:41:28 +020033# if ENABLE_FEATURE_IPV6
Tanguy Pruvot8aeb3712011-06-30 08:59:26 +020034# include <netinet/in6.h>
35# endif
maxwen27116ba2015-08-14 21:41:28 +020036# 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}
Tanguy Pruvot8aeb3712011-06-30 08:59:26 +020052#endif
53
maxwen27116ba2015-08-14 21:41:28 +020054#define EXT(res) ((&res)->_u._ext)
55
John Beppu50bc1012000-01-30 09:47:16 +000056/*
Denis Vlasenko391ffa12008-11-04 21:44:28 +000057 * 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.
John Beppu50bc1012000-01-30 09:47:16 +000062 */
John Beppub332e772000-01-29 12:59:01 +000063
Denis Vlasenkoebe578a2006-10-26 17:17:59 +000064/* Examples of 'standard' nslookup output
65 * $ nslookup yahoo.com
66 * Server: 128.193.0.10
67 * Address: 128.193.0.10#53
Denis Vlasenkof7996f32007-01-11 17:20:00 +000068 *
Denis Vlasenkoebe578a2006-10-26 17:17:59 +000069 * 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
Denis Vlasenkof7996f32007-01-11 17:20:00 +000078 *
Denis Vlasenkoebe578a2006-10-26 17:17:59 +000079 * 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.
Denis Vlasenkof7996f32007-01-11 17:20:00 +000082 *
Denis Vlasenkoebe578a2006-10-26 17:17:59 +000083 * 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 */
John Beppub332e772000-01-29 12:59:01 +000091
Denis Vlasenkoebe578a2006-10-26 17:17:59 +000092static int print_host(const char *hostname, const char *header)
John Beppub332e772000-01-29 12:59:01 +000093{
Denis Vlasenko42823d52007-02-04 02:39:08 +000094 /* We can't use xhost2sockaddr() - we want to get ALL addresses,
Denis Vlasenko448f0242007-01-22 22:43:05 +000095 * not just one */
Denis Vlasenko448f0242007-01-22 22:43:05 +000096 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
Vitaly Magerya7f4b7692011-03-22 20:14:26 +0100108 if (rc == 0) {
Denis Vlasenko448f0242007-01-22 22:43:05 +0000109 struct addrinfo *cur = result;
110 unsigned cnt = 0;
111
112 printf("%-10s %s\n", header, hostname);
Denis Vlasenkofeb7ae72007-10-01 12:05:12 +0000113 // puts(cur->ai_canonname); ?
Denis Vlasenko448f0242007-01-22 22:43:05 +0000114 while (cur) {
115 char *dotted, *revhost;
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000116 dotted = xmalloc_sockaddr2dotted_noport(cur->ai_addr);
117 revhost = xmalloc_sockaddr2hostonly_noport(cur->ai_addr);
Denis Vlasenko448f0242007-01-22 22:43:05 +0000118
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
Denis Vlasenko5de9e9c2007-01-22 22:46:04 +0000131 bb_error_msg("can't resolve '%s': %s", hostname, gai_strerror(rc));
Denis Vlasenko448f0242007-01-22 22:43:05 +0000132#else
133 bb_error_msg("can't resolve '%s'", hostname);
134#endif
135 }
Vitaly Magerya7f4b7692011-03-22 20:14:26 +0100136 if (ENABLE_FEATURE_CLEAN_UP && result)
Denis Vlasenko448f0242007-01-22 22:43:05 +0000137 freeaddrinfo(result);
138 return (rc != 0);
Denis Vlasenko448f0242007-01-22 22:43:05 +0000139}
140
Denis Vlasenko448f0242007-01-22 22:43:05 +0000141/* lookup the default nameserver and display it */
142static void server_print(void)
143{
144 char *server;
maxwen27116ba2015-08-14 21:41:28 +0200145 struct sockaddr *sa = NULL;
Denis Vlasenko448f0242007-01-22 22:43:05 +0000146
maxwen27116ba2015-08-14 21:41:28 +0200147#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
Denis Vlasenko3f5f2462008-11-16 19:02:26 +0000155 if (!sa)
156#endif
maxwen27116ba2015-08-14 21:41:28 +0200157 sa = (struct sockaddr*) &_res.nsaddr_list[0];
Denis Vlasenko3f5f2462008-11-16 19:02:26 +0000158 server = xmalloc_sockaddr2dotted_noport(sa);
159
Denis Vlasenko448f0242007-01-22 22:43:05 +0000160 print_host(server, "Server:");
161 if (ENABLE_FEATURE_CLEAN_UP)
162 free(server);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000163 bb_putchar('\n');
John Beppub332e772000-01-29 12:59:01 +0000164}
165
Robert Griebl31a2e202002-07-24 00:56:56 +0000166/* alter the global _res nameserver structure to use
Denis Vlasenko391ffa12008-11-04 21:44:28 +0000167 an explicit dns server instead of what is in /etc/resolv.conf */
Denis Vlasenko3f5f2462008-11-16 19:02:26 +0000168static void set_default_dns(const char *server)
Robert Griebl31a2e202002-07-24 00:56:56 +0000169{
Denis Vlasenko3f5f2462008-11-16 19:02:26 +0000170 len_and_sockaddr *lsa;
Robert Griebl31a2e202002-07-24 00:56:56 +0000171
maxwen27116ba2015-08-14 21:41:28 +0200172 if (!server)
173 return;
174
175 /* NB: this works even with, say, "[::1]:53"! :) */
Denis Vlasenko3f5f2462008-11-16 19:02:26 +0000176 lsa = xhost2sockaddr(server, 53);
177
178 if (lsa->u.sa.sa_family == AF_INET) {
maxwen27116ba2015-08-14 21:41:28 +0200179 _res.nscount = 1;
Denis Vlasenko3f5f2462008-11-16 19:02:26 +0000180 /* struct copy */
maxwen27116ba2015-08-14 21:41:28 +0200181 _res.nsaddr_list[0] = lsa->u.sin;
Robert Griebl31a2e202002-07-24 00:56:56 +0000182 }
maxwen27116ba2015-08-14 21:41:28 +0200183
184#if ENABLE_FEATURE_IPV6
Denis Vlasenko249d9482008-11-17 15:36:36 +0000185 /* 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;
maxwen27116ba2015-08-14 21:41:28 +0200193 #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
Denis Vlasenko3f5f2462008-11-16 19:02:26 +0000200 /* store a pointer to part of malloc'ed lsa */
maxwen27116ba2015-08-14 21:41:28 +0200201 _res._u._ext.nscount = 1;
Denis Vlasenko3f5f2462008-11-16 19:02:26 +0000202 _res._u._ext.nsaddrs[0] = &lsa->u.sin6;
203 /* must not free(lsa)! */
maxwen27116ba2015-08-14 21:41:28 +0200204 #endif
Denis Vlasenko249d9482008-11-17 15:36:36 +0000205 }
Denis Vlasenko3f5f2462008-11-16 19:02:26 +0000206#endif
Eric Andersen39cdf4e2004-01-30 22:40:05 +0000207}
Robert Griebl31a2e202002-07-24 00:56:56 +0000208
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000209int nslookup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000210int nslookup_main(int argc, char **argv)
John Beppub332e772000-01-29 12:59:01 +0000211{
Denis Vlasenko448f0242007-01-22 22:43:05 +0000212 /* 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 */
Denis Vlasenko391ffa12008-11-04 21:44:28 +0000217 if (!argv[1] || argv[1][0] == '-' || argc > 3)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000218 bb_show_usage();
Denis Vlasenko448f0242007-01-22 22:43:05 +0000219
220 /* initialize DNS structure _res used in printing the default
221 * name server and in the explicit name server option feature. */
222 res_init();
maxwen27116ba2015-08-14 21:41:28 +0200223
224#ifdef ANDROID
225 res_ninit(&_res);
226#endif
227
Denis Vlasenko448f0242007-01-22 22:43:05 +0000228 /* rfc2133 says this enables IPv6 lookups */
Denis Vlasenko391ffa12008-11-04 21:44:28 +0000229 /* (but it also says "may be enabled in /etc/resolv.conf") */
Denis Vlasenko448f0242007-01-22 22:43:05 +0000230 /*_res.options |= RES_USE_INET6;*/
231
maxwen27116ba2015-08-14 21:41:28 +0200232 set_default_dns(argv[2]);
Robert Griebl31a2e202002-07-24 00:56:56 +0000233
Eric Andersenfe9888a2001-01-20 21:51:21 +0000234 server_print();
maxwen27116ba2015-08-14 21:41:28 +0200235
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
Denis Vlasenko448f0242007-01-22 22:43:05 +0000244 return print_host(argv[1], "Name:");
John Beppub332e772000-01-29 12:59:01 +0000245}