blob: b8fc8c86cc4725c122c03ee152a94738c5ee7c4d [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/* 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
32/*
33 * I'm only implementing non-interactive mode;
34 * I totally forgot nslookup even had an interactive mode.
35 *
36 * This applet is the only user of res_init(). Without it,
37 * you may avoid pulling in _res global from libc.
38 */
39
40/* Examples of 'standard' nslookup output
41 * $ nslookup yahoo.com
42 * Server: 128.193.0.10
43 * Address: 128.193.0.10#53
44 *
45 * Non-authoritative answer:
46 * Name: yahoo.com
47 * Address: 216.109.112.135
48 * Name: yahoo.com
49 * Address: 66.94.234.13
50 *
51 * $ nslookup 204.152.191.37
52 * Server: 128.193.4.20
53 * Address: 128.193.4.20#53
54 *
55 * Non-authoritative answer:
56 * 37.191.152.204.in-addr.arpa canonical name = 37.32-27.191.152.204.in-addr.arpa.
57 * 37.32-27.191.152.204.in-addr.arpa name = zeus-pub2.kernel.org.
58 *
59 * Authoritative answers can be found from:
60 * 32-27.191.152.204.in-addr.arpa nameserver = ns1.kernel.org.
61 * 32-27.191.152.204.in-addr.arpa nameserver = ns2.kernel.org.
62 * 32-27.191.152.204.in-addr.arpa nameserver = ns3.kernel.org.
63 * ns1.kernel.org internet address = 140.211.167.34
64 * ns2.kernel.org internet address = 204.152.191.4
65 * ns3.kernel.org internet address = 204.152.191.36
66 */
67
68static int print_host(const char *hostname, const char *header)
69{
70 /* We can't use xhost2sockaddr() - we want to get ALL addresses,
71 * not just one */
72 struct addrinfo *result = NULL;
73 int rc;
74 struct addrinfo hint;
75
76 memset(&hint, 0 , sizeof(hint));
77 /* hint.ai_family = AF_UNSPEC; - zero anyway */
78 /* Needed. Or else we will get each address thrice (or more)
79 * for each possible socket type (tcp,udp,raw...): */
80 hint.ai_socktype = SOCK_STREAM;
81 // hint.ai_flags = AI_CANONNAME;
82 rc = getaddrinfo(hostname, NULL /*service*/, &hint, &result);
83
84 if (rc == 0) {
85 struct addrinfo *cur = result;
86 unsigned cnt = 0;
87
88 printf("%-10s %s\n", header, hostname);
89 // puts(cur->ai_canonname); ?
90 while (cur) {
91 char *dotted, *revhost;
92 dotted = xmalloc_sockaddr2dotted_noport(cur->ai_addr);
93 revhost = xmalloc_sockaddr2hostonly_noport(cur->ai_addr);
94
95 printf("Address %u: %s%c", ++cnt, dotted, revhost ? ' ' : '\n');
96 if (revhost) {
97 puts(revhost);
98 if (ENABLE_FEATURE_CLEAN_UP)
99 free(revhost);
100 }
101 if (ENABLE_FEATURE_CLEAN_UP)
102 free(dotted);
103 cur = cur->ai_next;
104 }
105 } else {
106#if ENABLE_VERBOSE_RESOLUTION_ERRORS
107 bb_error_msg("can't resolve '%s': %s", hostname, gai_strerror(rc));
108#else
109 bb_error_msg("can't resolve '%s'", hostname);
110#endif
111 }
112 if (ENABLE_FEATURE_CLEAN_UP && result)
113 freeaddrinfo(result);
114 return (rc != 0);
115}
116
117/* lookup the default nameserver and display it */
118static void server_print(void)
119{
120#ifndef __UC_LIBC__
121 char *server;
122 struct sockaddr *sa;
123
124#if ENABLE_FEATURE_IPV6
125 sa = (struct sockaddr*)_res._u._ext.nsaddrs[0];
126 if (!sa)
127#endif
128 sa = (struct sockaddr*)&_res.nsaddr_list[0];
129 server = xmalloc_sockaddr2dotted_noport(sa);
130
131 print_host(server, "Server:");
132 if (ENABLE_FEATURE_CLEAN_UP)
133 free(server);
134 bb_putchar('\n');
135#endif
136}
137
138/* alter the global _res nameserver structure to use
139 an explicit dns server instead of what is in /etc/resolv.conf */
140static void set_default_dns(const char *server)
141{
142 len_and_sockaddr *lsa;
143
144 /* NB: this works even with, say, "[::1]:5353"! :) */
145 lsa = xhost2sockaddr(server, 53);
146
147 if (lsa->u.sa.sa_family == AF_INET) {
148 _res.nscount = 1;
149 /* struct copy */
150 _res.nsaddr_list[0] = lsa->u.sin;
151 }
152#if ENABLE_FEATURE_IPV6
153 /* Hoped libc can cope with IPv4 address there too.
154 * No such luck, glibc 2.4 segfaults even with IPv6,
155 * maybe I misunderstand how to make glibc use IPv6 addr?
156 * (uclibc 0.9.31+ should work) */
157 if (lsa->u.sa.sa_family == AF_INET6) {
158 // glibc neither SEGVs nor sends any dgrams with this
159 // (strace shows no socket ops):
160 //_res.nscount = 0;
161 _res._u._ext.nscount = 1;
162 /* store a pointer to part of malloc'ed lsa */
163 _res._u._ext.nsaddrs[0] = &lsa->u.sin6;
164 /* must not free(lsa)! */
165 }
166#endif
167}
168
169int nslookup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
170int nslookup_main(int argc, char **argv)
171{
172 /* We allow 1 or 2 arguments.
173 * The first is the name to be looked up and the second is an
174 * optional DNS server with which to do the lookup.
175 * More than 3 arguments is an error to follow the pattern of the
176 * standard nslookup */
177 if (!argv[1] || argv[1][0] == '-' || argc > 3)
178 bb_show_usage();
179
180#ifndef __UC_LIBC__
181 /* initialize DNS structure _res used in printing the default
182 * name server and in the explicit name server option feature. */
183 res_init();
184#endif
185
186 /* rfc2133 says this enables IPv6 lookups */
187 /* (but it also says "may be enabled in /etc/resolv.conf") */
188 /*_res.options |= RES_USE_INET6;*/
189
190 if (argv[2])
191 set_default_dns(argv[2]);
192
193 server_print();
194 return print_host(argv[1], "Name:");
195}