blob: 0ef91dc4b53e06894afe33f62c1af77c22387533 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Eric Andersenc223ced2001-11-10 12:18:42 +00002/*
3 * stolen from net-tools-1.59 and stripped down for busybox by
Eric Andersencb81e642003-07-14 21:21:08 +00004 * Erik Andersen <andersen@codepoet.org>
Eric Andersenc223ced2001-11-10 12:18:42 +00005 *
6 * Heavily modified by Manuel Novoa III Mar 12, 2001
7 *
Eric Andersenc223ced2001-11-10 12:18:42 +00008 *
9 */
10
Rob Landleyea224be2006-06-18 20:20:07 +000011#include "libbb.h"
Eric Andersenc223ced2001-11-10 12:18:42 +000012#include "inet_common.h"
Eric Andersen77d22c42002-06-06 12:11:55 +000013
Eric Andersenc223ced2001-11-10 12:18:42 +000014int INET_resolve(const char *name, struct sockaddr_in *s_in, int hostfirst)
15{
Glenn L McGrath30db4232002-11-28 09:52:23 +000016 struct hostent *hp;
17 struct netent *np;
Eric Andersenc223ced2001-11-10 12:18:42 +000018
Glenn L McGrath30db4232002-11-28 09:52:23 +000019 /* Grmpf. -FvK */
20 s_in->sin_family = AF_INET;
21 s_in->sin_port = 0;
Eric Andersenc223ced2001-11-10 12:18:42 +000022
Glenn L McGrath30db4232002-11-28 09:52:23 +000023 /* Default is special, meaning 0.0.0.0. */
Denis Vlasenko7ca39212006-11-21 20:34:21 +000024 if (!strcmp(name, bb_str_default)) {
Glenn L McGrath30db4232002-11-28 09:52:23 +000025 s_in->sin_addr.s_addr = INADDR_ANY;
Denis Vlasenko7ca39212006-11-21 20:34:21 +000026 return 1;
Glenn L McGrath30db4232002-11-28 09:52:23 +000027 }
28 /* Look to see if it's a dotted quad. */
29 if (inet_aton(name, &s_in->sin_addr)) {
30 return 0;
31 }
32 /* If we expect this to be a hostname, try hostname database first */
Eric Andersenc223ced2001-11-10 12:18:42 +000033#ifdef DEBUG
Glenn L McGrath30db4232002-11-28 09:52:23 +000034 if (hostfirst) {
Denis Vlasenko703e2022007-01-22 14:12:08 +000035 bb_error_msg("gethostbyname(%s)", name);
Glenn L McGrath30db4232002-11-28 09:52:23 +000036 }
Eric Andersenc223ced2001-11-10 12:18:42 +000037#endif
Denis Vlasenko703e2022007-01-22 14:12:08 +000038 if (hostfirst) {
39 hp = gethostbyname(name);
40 if (hp != NULL) {
41 memcpy(&s_in->sin_addr, hp->h_addr_list[0],
42 sizeof(struct in_addr));
43 return 0;
44 }
Glenn L McGrath30db4232002-11-28 09:52:23 +000045 }
46 /* Try the NETWORKS database to see if this is a known network. */
47#ifdef DEBUG
Denis Vlasenko703e2022007-01-22 14:12:08 +000048 bb_error_msg("getnetbyname(%s)", name);
Glenn L McGrath30db4232002-11-28 09:52:23 +000049#endif
Denis Vlasenko703e2022007-01-22 14:12:08 +000050 np = getnetbyname(name);
51 if (np != NULL) {
Glenn L McGrath30db4232002-11-28 09:52:23 +000052 s_in->sin_addr.s_addr = htonl(np->n_net);
53 return 1;
54 }
55 if (hostfirst) {
56 /* Don't try again */
Glenn L McGrath30db4232002-11-28 09:52:23 +000057 return -1;
58 }
59#ifdef DEBUG
60 res_init();
61 _res.options |= RES_DEBUG;
62#endif
63
64#ifdef DEBUG
Denis Vlasenko703e2022007-01-22 14:12:08 +000065 bb_error_msg("gethostbyname(%s)", name);
Glenn L McGrath30db4232002-11-28 09:52:23 +000066#endif
Denis Vlasenko703e2022007-01-22 14:12:08 +000067 hp = gethostbyname(name);
68 if (hp == NULL) {
Glenn L McGrath30db4232002-11-28 09:52:23 +000069 return -1;
70 }
Denis Vlasenko703e2022007-01-22 14:12:08 +000071 memcpy(&s_in->sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
Eric Andersenc223ced2001-11-10 12:18:42 +000072 return 0;
Eric Andersenc223ced2001-11-10 12:18:42 +000073}
74
75/* cache */
76struct addr {
Glenn L McGrath30db4232002-11-28 09:52:23 +000077 struct sockaddr_in addr;
78 char *name;
79 int host;
80 struct addr *next;
Eric Andersenc223ced2001-11-10 12:18:42 +000081};
82
Denis Vlasenko703e2022007-01-22 14:12:08 +000083static struct addr *INET_nn = NULL; /* addr-to-name cache */
Eric Andersenc223ced2001-11-10 12:18:42 +000084
85/* numeric: & 0x8000: default instead of *,
86 * & 0x4000: host instead of net,
87 * & 0x0fff: don't resolve
88 */
89int INET_rresolve(char *name, size_t len, struct sockaddr_in *s_in,
Glenn L McGrath30db4232002-11-28 09:52:23 +000090 int numeric, unsigned int netmask)
Eric Andersenc223ced2001-11-10 12:18:42 +000091{
Glenn L McGrath30db4232002-11-28 09:52:23 +000092 struct hostent *ent;
93 struct netent *np;
94 struct addr *pn;
Denis Vlasenko703e2022007-01-22 14:12:08 +000095 uint32_t ad, host_ad;
Glenn L McGrath30db4232002-11-28 09:52:23 +000096 int host = 0;
Eric Andersenc223ced2001-11-10 12:18:42 +000097
Glenn L McGrath30db4232002-11-28 09:52:23 +000098 /* Grmpf. -FvK */
99 if (s_in->sin_family != AF_INET) {
Eric Andersenc223ced2001-11-10 12:18:42 +0000100#ifdef DEBUG
Manuel Novoa III cad53642003-03-19 09:13:01 +0000101 bb_error_msg("rresolve: unsupport address family %d !",
Glenn L McGrath30db4232002-11-28 09:52:23 +0000102 s_in->sin_family);
Eric Andersenc223ced2001-11-10 12:18:42 +0000103#endif
Glenn L McGrath30db4232002-11-28 09:52:23 +0000104 errno = EAFNOSUPPORT;
Denis Vlasenko7ca39212006-11-21 20:34:21 +0000105 return -1;
Eric Andersenc223ced2001-11-10 12:18:42 +0000106 }
Denis Vlasenko703e2022007-01-22 14:12:08 +0000107 ad = s_in->sin_addr.s_addr;
Glenn L McGrath30db4232002-11-28 09:52:23 +0000108#ifdef DEBUG
Denis Vlasenko703e2022007-01-22 14:12:08 +0000109 bb_error_msg("rresolve: %08x, mask %08x, num %08x", (unsigned)ad, netmask, numeric);
Glenn L McGrath30db4232002-11-28 09:52:23 +0000110#endif
111 if (ad == INADDR_ANY) {
112 if ((numeric & 0x0FFF) == 0) {
113 if (numeric & 0x8000)
Denis Vlasenko7ca39212006-11-21 20:34:21 +0000114 safe_strncpy(name, bb_str_default, len);
Glenn L McGrath30db4232002-11-28 09:52:23 +0000115 else
116 safe_strncpy(name, "*", len);
Denis Vlasenko7ca39212006-11-21 20:34:21 +0000117 return 0;
Glenn L McGrath30db4232002-11-28 09:52:23 +0000118 }
119 }
120 if (numeric & 0x0FFF) {
121 safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
Denis Vlasenko7ca39212006-11-21 20:34:21 +0000122 return 0;
Glenn L McGrath30db4232002-11-28 09:52:23 +0000123 }
Eric Andersenc223ced2001-11-10 12:18:42 +0000124
Glenn L McGrath30db4232002-11-28 09:52:23 +0000125 if ((ad & (~netmask)) != 0 || (numeric & 0x4000))
126 host = 1;
Glenn L McGrath30db4232002-11-28 09:52:23 +0000127 pn = INET_nn;
128 while (pn != NULL) {
129 if (pn->addr.sin_addr.s_addr == ad && pn->host == host) {
130 safe_strncpy(name, pn->name, len);
Eric Andersenc223ced2001-11-10 12:18:42 +0000131#ifdef DEBUG
Denis Vlasenko703e2022007-01-22 14:12:08 +0000132 bb_error_msg("rresolve: found %s %08x in cache",
133 (host ? "host" : "net"), (unsigned)ad);
Eric Andersenc223ced2001-11-10 12:18:42 +0000134#endif
Denis Vlasenko7ca39212006-11-21 20:34:21 +0000135 return 0;
Glenn L McGrath30db4232002-11-28 09:52:23 +0000136 }
137 pn = pn->next;
Eric Andersenc223ced2001-11-10 12:18:42 +0000138 }
Eric Andersenc223ced2001-11-10 12:18:42 +0000139
Glenn L McGrath30db4232002-11-28 09:52:23 +0000140 host_ad = ntohl(ad);
141 np = NULL;
142 ent = NULL;
143 if (host) {
Eric Andersenc223ced2001-11-10 12:18:42 +0000144#ifdef DEBUG
Denis Vlasenko703e2022007-01-22 14:12:08 +0000145 bb_error_msg("gethostbyaddr (%08x)", (unsigned)ad);
Eric Andersenc223ced2001-11-10 12:18:42 +0000146#endif
Glenn L McGrath30db4232002-11-28 09:52:23 +0000147 ent = gethostbyaddr((char *) &ad, 4, AF_INET);
148 if (ent != NULL) {
149 safe_strncpy(name, ent->h_name, len);
150 }
151 } else {
Eric Andersenc223ced2001-11-10 12:18:42 +0000152#ifdef DEBUG
Denis Vlasenko703e2022007-01-22 14:12:08 +0000153 bb_error_msg("getnetbyaddr (%08x)", (unsigned)host_ad);
Eric Andersenc223ced2001-11-10 12:18:42 +0000154#endif
Glenn L McGrath30db4232002-11-28 09:52:23 +0000155 np = getnetbyaddr(host_ad, AF_INET);
156 if (np != NULL) {
157 safe_strncpy(name, np->n_name, len);
158 }
159 }
Denis Vlasenko703e2022007-01-22 14:12:08 +0000160 if (!ent && !np) {
Glenn L McGrath30db4232002-11-28 09:52:23 +0000161 safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
162 }
Denis Vlasenkob95636c2006-12-19 23:36:04 +0000163 pn = xmalloc(sizeof(struct addr));
Glenn L McGrath30db4232002-11-28 09:52:23 +0000164 pn->addr = *s_in;
165 pn->next = INET_nn;
166 pn->host = host;
Rob Landleyd921b2e2006-08-03 15:41:12 +0000167 pn->name = xstrdup(name);
Glenn L McGrath30db4232002-11-28 09:52:23 +0000168 INET_nn = pn;
Denis Vlasenko7ca39212006-11-21 20:34:21 +0000169 return 0;
Eric Andersenc223ced2001-11-10 12:18:42 +0000170}
Eric Andersen51b8bd62002-07-03 11:46:38 +0000171
Glenn L McGrathd7fb1b32002-11-26 02:40:56 +0000172#ifdef CONFIG_FEATURE_IPV6
Eric Andersen51b8bd62002-07-03 11:46:38 +0000173
Manuel Novoa III 7d0c5192004-03-10 07:42:38 +0000174int INET6_resolve(const char *name, struct sockaddr_in6 *sin6)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000175{
Glenn L McGrath30db4232002-11-28 09:52:23 +0000176 struct addrinfo req, *ai;
177 int s;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000178
Glenn L McGrath30db4232002-11-28 09:52:23 +0000179 memset(&req, '\0', sizeof req);
180 req.ai_family = AF_INET6;
Denis Vlasenko7ca39212006-11-21 20:34:21 +0000181 s = getaddrinfo(name, NULL, &req, &ai);
182 if (s) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000183 bb_error_msg("getaddrinfo: %s: %d", name, s);
Glenn L McGrath30db4232002-11-28 09:52:23 +0000184 return -1;
185 }
186 memcpy(sin6, ai->ai_addr, sizeof(struct sockaddr_in6));
Glenn L McGrath30db4232002-11-28 09:52:23 +0000187 freeaddrinfo(ai);
Denis Vlasenko7ca39212006-11-21 20:34:21 +0000188 return 0;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000189}
190
191#ifndef IN6_IS_ADDR_UNSPECIFIED
Glenn L McGrathd7fb1b32002-11-26 02:40:56 +0000192# define IN6_IS_ADDR_UNSPECIFIED(a) \
Bernhard Reutner-Fischer38583fd2006-05-19 12:44:16 +0000193 (((uint32_t *) (a))[0] == 0 && ((uint32_t *) (a))[1] == 0 && \
194 ((uint32_t *) (a))[2] == 0 && ((uint32_t *) (a))[3] == 0)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000195#endif
196
197
Glenn L McGrath30db4232002-11-28 09:52:23 +0000198int INET6_rresolve(char *name, size_t len, struct sockaddr_in6 *sin6,
199 int numeric)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000200{
Glenn L McGrath30db4232002-11-28 09:52:23 +0000201 int s;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000202
Glenn L McGrath30db4232002-11-28 09:52:23 +0000203 /* Grmpf. -FvK */
204 if (sin6->sin6_family != AF_INET6) {
Eric Andersen51b8bd62002-07-03 11:46:38 +0000205#ifdef DEBUG
Denis Vlasenko7ca39212006-11-21 20:34:21 +0000206 bb_error_msg("rresolve: unsupport address family %d!",
Glenn L McGrath30db4232002-11-28 09:52:23 +0000207 sin6->sin6_family);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000208#endif
Glenn L McGrath30db4232002-11-28 09:52:23 +0000209 errno = EAFNOSUPPORT;
Denis Vlasenko7ca39212006-11-21 20:34:21 +0000210 return -1;
Glenn L McGrath30db4232002-11-28 09:52:23 +0000211 }
212 if (numeric & 0x7FFF) {
213 inet_ntop(AF_INET6, &sin6->sin6_addr, name, len);
Denis Vlasenko7ca39212006-11-21 20:34:21 +0000214 return 0;
Glenn L McGrath30db4232002-11-28 09:52:23 +0000215 }
216 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
217 if (numeric & 0x8000) {
Denis Vlasenko7ca39212006-11-21 20:34:21 +0000218 strcpy(name, bb_str_default);
Glenn L McGrath30db4232002-11-28 09:52:23 +0000219 } else {
Denis Vlasenko7ca39212006-11-21 20:34:21 +0000220 name[0] = '*';
221 name[1] = '\0';
Glenn L McGrath30db4232002-11-28 09:52:23 +0000222 }
Denis Vlasenko7ca39212006-11-21 20:34:21 +0000223 return 0;
Glenn L McGrath30db4232002-11-28 09:52:23 +0000224 }
Eric Andersen51b8bd62002-07-03 11:46:38 +0000225
Denis Vlasenko703e2022007-01-22 14:12:08 +0000226 s = getnameinfo((struct sockaddr *) sin6, sizeof(struct sockaddr_in6),
227 name, len, NULL, 0, 0);
Glenn L McGrath30db4232002-11-28 09:52:23 +0000228 if (s) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000229 bb_error_msg("getnameinfo failed");
Glenn L McGrath30db4232002-11-28 09:52:23 +0000230 return -1;
231 }
Denis Vlasenko7ca39212006-11-21 20:34:21 +0000232 return 0;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000233}
234
Glenn L McGrath30db4232002-11-28 09:52:23 +0000235#endif /* CONFIG_FEATURE_IPV6 */