blob: 9cdcb11deae95d14e2a26387b8afbfd1fbc0f7e1 [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 *
Manuel Novoa III 7d0c5192004-03-10 07:42:38 +00008 * Version: $Id: inet_common.c,v 1.8 2004/03/10 07:42:38 mjn3 Exp $
Eric Andersenc223ced2001-11-10 12:18:42 +00009 *
10 */
11
Rob Landleyea224be2006-06-18 20:20:07 +000012#include "libbb.h"
Eric Andersenc223ced2001-11-10 12:18:42 +000013#include "inet_common.h"
Eric Andersen77d22c42002-06-06 12:11:55 +000014
Eric Andersenc223ced2001-11-10 12:18:42 +000015int INET_resolve(const char *name, struct sockaddr_in *s_in, int hostfirst)
16{
Glenn L McGrath30db4232002-11-28 09:52:23 +000017 struct hostent *hp;
18 struct netent *np;
Eric Andersenc223ced2001-11-10 12:18:42 +000019
Glenn L McGrath30db4232002-11-28 09:52:23 +000020 /* Grmpf. -FvK */
21 s_in->sin_family = AF_INET;
22 s_in->sin_port = 0;
Eric Andersenc223ced2001-11-10 12:18:42 +000023
Glenn L McGrath30db4232002-11-28 09:52:23 +000024 /* Default is special, meaning 0.0.0.0. */
Denis Vlasenko7ca39212006-11-21 20:34:21 +000025 if (!strcmp(name, bb_str_default)) {
Glenn L McGrath30db4232002-11-28 09:52:23 +000026 s_in->sin_addr.s_addr = INADDR_ANY;
Denis Vlasenko7ca39212006-11-21 20:34:21 +000027 return 1;
Glenn L McGrath30db4232002-11-28 09:52:23 +000028 }
29 /* Look to see if it's a dotted quad. */
30 if (inet_aton(name, &s_in->sin_addr)) {
31 return 0;
32 }
33 /* If we expect this to be a hostname, try hostname database first */
Eric Andersenc223ced2001-11-10 12:18:42 +000034#ifdef DEBUG
Glenn L McGrath30db4232002-11-28 09:52:23 +000035 if (hostfirst) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000036 bb_error_msg("gethostbyname (%s)", name);
Glenn L McGrath30db4232002-11-28 09:52:23 +000037 }
Eric Andersenc223ced2001-11-10 12:18:42 +000038#endif
Glenn L McGrath30db4232002-11-28 09:52:23 +000039 if (hostfirst && (hp = gethostbyname(name)) != (struct hostent *) NULL) {
40 memcpy((char *) &s_in->sin_addr, (char *) hp->h_addr_list[0],
41 sizeof(struct in_addr));
42 return 0;
43 }
44 /* Try the NETWORKS database to see if this is a known network. */
45#ifdef DEBUG
Manuel Novoa III cad53642003-03-19 09:13:01 +000046 bb_error_msg("getnetbyname (%s)", name);
Glenn L McGrath30db4232002-11-28 09:52:23 +000047#endif
48 if ((np = getnetbyname(name)) != (struct netent *) NULL) {
49 s_in->sin_addr.s_addr = htonl(np->n_net);
50 return 1;
51 }
52 if (hostfirst) {
53 /* Don't try again */
Glenn L McGrath30db4232002-11-28 09:52:23 +000054 return -1;
55 }
56#ifdef DEBUG
57 res_init();
58 _res.options |= RES_DEBUG;
59#endif
60
61#ifdef DEBUG
Manuel Novoa III cad53642003-03-19 09:13:01 +000062 bb_error_msg("gethostbyname (%s)", name);
Glenn L McGrath30db4232002-11-28 09:52:23 +000063#endif
64 if ((hp = gethostbyname(name)) == (struct hostent *) NULL) {
Glenn L McGrath30db4232002-11-28 09:52:23 +000065 return -1;
66 }
Eric Andersenc223ced2001-11-10 12:18:42 +000067 memcpy((char *) &s_in->sin_addr, (char *) hp->h_addr_list[0],
Glenn L McGrath30db4232002-11-28 09:52:23 +000068 sizeof(struct in_addr));
69
Eric Andersenc223ced2001-11-10 12:18:42 +000070 return 0;
Eric Andersenc223ced2001-11-10 12:18:42 +000071}
72
73/* cache */
74struct addr {
Glenn L McGrath30db4232002-11-28 09:52:23 +000075 struct sockaddr_in addr;
76 char *name;
77 int host;
78 struct addr *next;
Eric Andersenc223ced2001-11-10 12:18:42 +000079};
80
Glenn L McGrath30db4232002-11-28 09:52:23 +000081static struct addr *INET_nn = NULL; /* addr-to-name cache */
Eric Andersenc223ced2001-11-10 12:18:42 +000082
83/* numeric: & 0x8000: default instead of *,
84 * & 0x4000: host instead of net,
85 * & 0x0fff: don't resolve
86 */
87int INET_rresolve(char *name, size_t len, struct sockaddr_in *s_in,
Glenn L McGrath30db4232002-11-28 09:52:23 +000088 int numeric, unsigned int netmask)
Eric Andersenc223ced2001-11-10 12:18:42 +000089{
Glenn L McGrath30db4232002-11-28 09:52:23 +000090 struct hostent *ent;
91 struct netent *np;
92 struct addr *pn;
93 unsigned long ad, host_ad;
94 int host = 0;
Eric Andersenc223ced2001-11-10 12:18:42 +000095
Glenn L McGrath30db4232002-11-28 09:52:23 +000096 /* Grmpf. -FvK */
97 if (s_in->sin_family != AF_INET) {
Eric Andersenc223ced2001-11-10 12:18:42 +000098#ifdef DEBUG
Manuel Novoa III cad53642003-03-19 09:13:01 +000099 bb_error_msg("rresolve: unsupport address family %d !",
Glenn L McGrath30db4232002-11-28 09:52:23 +0000100 s_in->sin_family);
Eric Andersenc223ced2001-11-10 12:18:42 +0000101#endif
Glenn L McGrath30db4232002-11-28 09:52:23 +0000102 errno = EAFNOSUPPORT;
Denis Vlasenko7ca39212006-11-21 20:34:21 +0000103 return -1;
Eric Andersenc223ced2001-11-10 12:18:42 +0000104 }
Glenn L McGrath30db4232002-11-28 09:52:23 +0000105 ad = (unsigned long) s_in->sin_addr.s_addr;
106#ifdef DEBUG
Manuel Novoa III cad53642003-03-19 09:13:01 +0000107 bb_error_msg("rresolve: %08lx, mask %08x, num %08x", ad, netmask, numeric);
Glenn L McGrath30db4232002-11-28 09:52:23 +0000108#endif
109 if (ad == INADDR_ANY) {
110 if ((numeric & 0x0FFF) == 0) {
111 if (numeric & 0x8000)
Denis Vlasenko7ca39212006-11-21 20:34:21 +0000112 safe_strncpy(name, bb_str_default, len);
Glenn L McGrath30db4232002-11-28 09:52:23 +0000113 else
114 safe_strncpy(name, "*", len);
Denis Vlasenko7ca39212006-11-21 20:34:21 +0000115 return 0;
Glenn L McGrath30db4232002-11-28 09:52:23 +0000116 }
117 }
118 if (numeric & 0x0FFF) {
119 safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
Denis Vlasenko7ca39212006-11-21 20:34:21 +0000120 return 0;
Glenn L McGrath30db4232002-11-28 09:52:23 +0000121 }
Eric Andersenc223ced2001-11-10 12:18:42 +0000122
Glenn L McGrath30db4232002-11-28 09:52:23 +0000123 if ((ad & (~netmask)) != 0 || (numeric & 0x4000))
124 host = 1;
Glenn L McGrath30db4232002-11-28 09:52:23 +0000125 pn = INET_nn;
126 while (pn != NULL) {
127 if (pn->addr.sin_addr.s_addr == ad && pn->host == host) {
128 safe_strncpy(name, pn->name, len);
Eric Andersenc223ced2001-11-10 12:18:42 +0000129#ifdef DEBUG
Manuel Novoa III cad53642003-03-19 09:13:01 +0000130 bb_error_msg("rresolve: found %s %08lx in cache",
Glenn L McGrath30db4232002-11-28 09:52:23 +0000131 (host ? "host" : "net"), ad);
Eric Andersenc223ced2001-11-10 12:18:42 +0000132#endif
Denis Vlasenko7ca39212006-11-21 20:34:21 +0000133 return 0;
Glenn L McGrath30db4232002-11-28 09:52:23 +0000134 }
135 pn = pn->next;
Eric Andersenc223ced2001-11-10 12:18:42 +0000136 }
Eric Andersenc223ced2001-11-10 12:18:42 +0000137
Glenn L McGrath30db4232002-11-28 09:52:23 +0000138 host_ad = ntohl(ad);
139 np = NULL;
140 ent = NULL;
141 if (host) {
Eric Andersenc223ced2001-11-10 12:18:42 +0000142#ifdef DEBUG
Manuel Novoa III cad53642003-03-19 09:13:01 +0000143 bb_error_msg("gethostbyaddr (%08lx)", ad);
Eric Andersenc223ced2001-11-10 12:18:42 +0000144#endif
Glenn L McGrath30db4232002-11-28 09:52:23 +0000145 ent = gethostbyaddr((char *) &ad, 4, AF_INET);
146 if (ent != NULL) {
147 safe_strncpy(name, ent->h_name, len);
148 }
149 } else {
Eric Andersenc223ced2001-11-10 12:18:42 +0000150#ifdef DEBUG
Manuel Novoa III cad53642003-03-19 09:13:01 +0000151 bb_error_msg("getnetbyaddr (%08lx)", host_ad);
Eric Andersenc223ced2001-11-10 12:18:42 +0000152#endif
Glenn L McGrath30db4232002-11-28 09:52:23 +0000153 np = getnetbyaddr(host_ad, AF_INET);
154 if (np != NULL) {
155 safe_strncpy(name, np->n_name, len);
156 }
157 }
158 if ((ent == NULL) && (np == NULL)) {
159 safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
160 }
Denis Vlasenkob95636c2006-12-19 23:36:04 +0000161 pn = xmalloc(sizeof(struct addr));
Glenn L McGrath30db4232002-11-28 09:52:23 +0000162 pn->addr = *s_in;
163 pn->next = INET_nn;
164 pn->host = host;
Rob Landleyd921b2e2006-08-03 15:41:12 +0000165 pn->name = xstrdup(name);
Glenn L McGrath30db4232002-11-28 09:52:23 +0000166 INET_nn = pn;
Eric Andersenc223ced2001-11-10 12:18:42 +0000167
Denis Vlasenko7ca39212006-11-21 20:34:21 +0000168 return 0;
Eric Andersenc223ced2001-11-10 12:18:42 +0000169}
Eric Andersen51b8bd62002-07-03 11:46:38 +0000170
Glenn L McGrathd7fb1b32002-11-26 02:40:56 +0000171#ifdef CONFIG_FEATURE_IPV6
Eric Andersen51b8bd62002-07-03 11:46:38 +0000172
Manuel Novoa III 7d0c5192004-03-10 07:42:38 +0000173int INET6_resolve(const char *name, struct sockaddr_in6 *sin6)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000174{
Glenn L McGrath30db4232002-11-28 09:52:23 +0000175 struct addrinfo req, *ai;
176 int s;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000177
Glenn L McGrath30db4232002-11-28 09:52:23 +0000178 memset(&req, '\0', sizeof req);
179 req.ai_family = AF_INET6;
Denis Vlasenko7ca39212006-11-21 20:34:21 +0000180 s = getaddrinfo(name, NULL, &req, &ai);
181 if (s) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000182 bb_error_msg("getaddrinfo: %s: %d", name, s);
Glenn L McGrath30db4232002-11-28 09:52:23 +0000183 return -1;
184 }
185 memcpy(sin6, ai->ai_addr, sizeof(struct sockaddr_in6));
Eric Andersen51b8bd62002-07-03 11:46:38 +0000186
Glenn L McGrath30db4232002-11-28 09:52:23 +0000187 freeaddrinfo(ai);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000188
Denis Vlasenko7ca39212006-11-21 20:34:21 +0000189 return 0;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000190}
191
192#ifndef IN6_IS_ADDR_UNSPECIFIED
Glenn L McGrathd7fb1b32002-11-26 02:40:56 +0000193# define IN6_IS_ADDR_UNSPECIFIED(a) \
Bernhard Reutner-Fischer38583fd2006-05-19 12:44:16 +0000194 (((uint32_t *) (a))[0] == 0 && ((uint32_t *) (a))[1] == 0 && \
195 ((uint32_t *) (a))[2] == 0 && ((uint32_t *) (a))[3] == 0)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000196#endif
197
198
Glenn L McGrath30db4232002-11-28 09:52:23 +0000199int INET6_rresolve(char *name, size_t len, struct sockaddr_in6 *sin6,
200 int numeric)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000201{
Glenn L McGrath30db4232002-11-28 09:52:23 +0000202 int s;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000203
Glenn L McGrath30db4232002-11-28 09:52:23 +0000204 /* Grmpf. -FvK */
205 if (sin6->sin6_family != AF_INET6) {
Eric Andersen51b8bd62002-07-03 11:46:38 +0000206#ifdef DEBUG
Denis Vlasenko7ca39212006-11-21 20:34:21 +0000207 bb_error_msg("rresolve: unsupport address family %d!",
Glenn L McGrath30db4232002-11-28 09:52:23 +0000208 sin6->sin6_family);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000209#endif
Glenn L McGrath30db4232002-11-28 09:52:23 +0000210 errno = EAFNOSUPPORT;
Denis Vlasenko7ca39212006-11-21 20:34:21 +0000211 return -1;
Glenn L McGrath30db4232002-11-28 09:52:23 +0000212 }
213 if (numeric & 0x7FFF) {
214 inet_ntop(AF_INET6, &sin6->sin6_addr, name, len);
Denis Vlasenko7ca39212006-11-21 20:34:21 +0000215 return 0;
Glenn L McGrath30db4232002-11-28 09:52:23 +0000216 }
217 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
218 if (numeric & 0x8000) {
Denis Vlasenko7ca39212006-11-21 20:34:21 +0000219 strcpy(name, bb_str_default);
Glenn L McGrath30db4232002-11-28 09:52:23 +0000220 } else {
Denis Vlasenko7ca39212006-11-21 20:34:21 +0000221 name[0] = '*';
222 name[1] = '\0';
Glenn L McGrath30db4232002-11-28 09:52:23 +0000223 }
Denis Vlasenko7ca39212006-11-21 20:34:21 +0000224 return 0;
Glenn L McGrath30db4232002-11-28 09:52:23 +0000225 }
Eric Andersen51b8bd62002-07-03 11:46:38 +0000226
Glenn L McGrath30db4232002-11-28 09:52:23 +0000227 s = getnameinfo((struct sockaddr *) sin6, sizeof(struct sockaddr_in6), name, len, NULL, 0, 0);
228 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 */