blob: 8b0ec8a0b687dfb6768955c33bb25e1f492d0e0d [file] [log] [blame]
Eric Andersenc223ced2001-11-10 12:18:42 +00001/*
2 * stolen from net-tools-1.59 and stripped down for busybox by
Eric Andersencb81e642003-07-14 21:21:08 +00003 * Erik Andersen <andersen@codepoet.org>
Eric Andersenc223ced2001-11-10 12:18:42 +00004 *
5 * Heavily modified by Manuel Novoa III Mar 12, 2001
6 *
Eric Andersencb81e642003-07-14 21:21:08 +00007 * Version: $Id: inet_common.c,v 1.7 2003/07/14 21:20:55 andersen Exp $
Eric Andersenc223ced2001-11-10 12:18:42 +00008 *
9 */
10
11#include "inet_common.h"
12#include <stdio.h>
13#include <errno.h>
14#include <stdlib.h>
15#include <string.h>
16#include <unistd.h>
17#include "libbb.h"
18
Eric Andersen77d22c42002-06-06 12:11:55 +000019#ifdef DEBUG
Glenn L McGrathd7fb1b32002-11-26 02:40:56 +000020# include <resolv.h>
Eric Andersen77d22c42002-06-06 12:11:55 +000021#endif
22
23
Glenn L McGrath30db4232002-11-28 09:52:23 +000024const char bb_INET_default[] = "default";
Eric Andersenc223ced2001-11-10 12:18:42 +000025
26int INET_resolve(const char *name, struct sockaddr_in *s_in, int hostfirst)
27{
Glenn L McGrath30db4232002-11-28 09:52:23 +000028 struct hostent *hp;
29 struct netent *np;
Eric Andersenc223ced2001-11-10 12:18:42 +000030
Glenn L McGrath30db4232002-11-28 09:52:23 +000031 /* Grmpf. -FvK */
32 s_in->sin_family = AF_INET;
33 s_in->sin_port = 0;
Eric Andersenc223ced2001-11-10 12:18:42 +000034
Glenn L McGrath30db4232002-11-28 09:52:23 +000035 /* Default is special, meaning 0.0.0.0. */
36 if (!strcmp(name, bb_INET_default)) {
37 s_in->sin_addr.s_addr = INADDR_ANY;
38 return (1);
39 }
40 /* Look to see if it's a dotted quad. */
41 if (inet_aton(name, &s_in->sin_addr)) {
42 return 0;
43 }
44 /* If we expect this to be a hostname, try hostname database first */
Eric Andersenc223ced2001-11-10 12:18:42 +000045#ifdef DEBUG
Glenn L McGrath30db4232002-11-28 09:52:23 +000046 if (hostfirst) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000047 bb_error_msg("gethostbyname (%s)", name);
Glenn L McGrath30db4232002-11-28 09:52:23 +000048 }
Eric Andersenc223ced2001-11-10 12:18:42 +000049#endif
Glenn L McGrath30db4232002-11-28 09:52:23 +000050 if (hostfirst && (hp = gethostbyname(name)) != (struct hostent *) NULL) {
51 memcpy((char *) &s_in->sin_addr, (char *) hp->h_addr_list[0],
52 sizeof(struct in_addr));
53 return 0;
54 }
55 /* Try the NETWORKS database to see if this is a known network. */
56#ifdef DEBUG
Manuel Novoa III cad53642003-03-19 09:13:01 +000057 bb_error_msg("getnetbyname (%s)", name);
Glenn L McGrath30db4232002-11-28 09:52:23 +000058#endif
59 if ((np = getnetbyname(name)) != (struct netent *) NULL) {
60 s_in->sin_addr.s_addr = htonl(np->n_net);
61 return 1;
62 }
63 if (hostfirst) {
64 /* Don't try again */
65 errno = h_errno;
66 return -1;
67 }
68#ifdef DEBUG
69 res_init();
70 _res.options |= RES_DEBUG;
71#endif
72
73#ifdef DEBUG
Manuel Novoa III cad53642003-03-19 09:13:01 +000074 bb_error_msg("gethostbyname (%s)", name);
Glenn L McGrath30db4232002-11-28 09:52:23 +000075#endif
76 if ((hp = gethostbyname(name)) == (struct hostent *) NULL) {
77 errno = h_errno;
78 return -1;
79 }
Eric Andersenc223ced2001-11-10 12:18:42 +000080 memcpy((char *) &s_in->sin_addr, (char *) hp->h_addr_list[0],
Glenn L McGrath30db4232002-11-28 09:52:23 +000081 sizeof(struct in_addr));
82
Eric Andersenc223ced2001-11-10 12:18:42 +000083 return 0;
Eric Andersenc223ced2001-11-10 12:18:42 +000084}
85
86/* cache */
87struct addr {
Glenn L McGrath30db4232002-11-28 09:52:23 +000088 struct sockaddr_in addr;
89 char *name;
90 int host;
91 struct addr *next;
Eric Andersenc223ced2001-11-10 12:18:42 +000092};
93
Glenn L McGrath30db4232002-11-28 09:52:23 +000094static struct addr *INET_nn = NULL; /* addr-to-name cache */
Eric Andersenc223ced2001-11-10 12:18:42 +000095
96/* numeric: & 0x8000: default instead of *,
97 * & 0x4000: host instead of net,
98 * & 0x0fff: don't resolve
99 */
100int INET_rresolve(char *name, size_t len, struct sockaddr_in *s_in,
Glenn L McGrath30db4232002-11-28 09:52:23 +0000101 int numeric, unsigned int netmask)
Eric Andersenc223ced2001-11-10 12:18:42 +0000102{
Glenn L McGrath30db4232002-11-28 09:52:23 +0000103 struct hostent *ent;
104 struct netent *np;
105 struct addr *pn;
106 unsigned long ad, host_ad;
107 int host = 0;
Eric Andersenc223ced2001-11-10 12:18:42 +0000108
Glenn L McGrath30db4232002-11-28 09:52:23 +0000109 /* Grmpf. -FvK */
110 if (s_in->sin_family != AF_INET) {
Eric Andersenc223ced2001-11-10 12:18:42 +0000111#ifdef DEBUG
Manuel Novoa III cad53642003-03-19 09:13:01 +0000112 bb_error_msg("rresolve: unsupport address family %d !",
Glenn L McGrath30db4232002-11-28 09:52:23 +0000113 s_in->sin_family);
Eric Andersenc223ced2001-11-10 12:18:42 +0000114#endif
Glenn L McGrath30db4232002-11-28 09:52:23 +0000115 errno = EAFNOSUPPORT;
116 return (-1);
Eric Andersenc223ced2001-11-10 12:18:42 +0000117 }
Glenn L McGrath30db4232002-11-28 09:52:23 +0000118 ad = (unsigned long) s_in->sin_addr.s_addr;
119#ifdef DEBUG
Manuel Novoa III cad53642003-03-19 09:13:01 +0000120 bb_error_msg("rresolve: %08lx, mask %08x, num %08x", ad, netmask, numeric);
Glenn L McGrath30db4232002-11-28 09:52:23 +0000121#endif
122 if (ad == INADDR_ANY) {
123 if ((numeric & 0x0FFF) == 0) {
124 if (numeric & 0x8000)
125 safe_strncpy(name, bb_INET_default, len);
126 else
127 safe_strncpy(name, "*", len);
128 return (0);
129 }
130 }
131 if (numeric & 0x0FFF) {
132 safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
133 return (0);
134 }
Eric Andersenc223ced2001-11-10 12:18:42 +0000135
Glenn L McGrath30db4232002-11-28 09:52:23 +0000136 if ((ad & (~netmask)) != 0 || (numeric & 0x4000))
137 host = 1;
Eric Andersenc223ced2001-11-10 12:18:42 +0000138#if 0
Glenn L McGrath30db4232002-11-28 09:52:23 +0000139 INET_nn = NULL;
Eric Andersenc223ced2001-11-10 12:18:42 +0000140#endif
Glenn L McGrath30db4232002-11-28 09:52:23 +0000141 pn = INET_nn;
142 while (pn != NULL) {
143 if (pn->addr.sin_addr.s_addr == ad && pn->host == host) {
144 safe_strncpy(name, pn->name, len);
Eric Andersenc223ced2001-11-10 12:18:42 +0000145#ifdef DEBUG
Manuel Novoa III cad53642003-03-19 09:13:01 +0000146 bb_error_msg("rresolve: found %s %08lx in cache",
Glenn L McGrath30db4232002-11-28 09:52:23 +0000147 (host ? "host" : "net"), ad);
Eric Andersenc223ced2001-11-10 12:18:42 +0000148#endif
Glenn L McGrath30db4232002-11-28 09:52:23 +0000149 return (0);
150 }
151 pn = pn->next;
Eric Andersenc223ced2001-11-10 12:18:42 +0000152 }
Eric Andersenc223ced2001-11-10 12:18:42 +0000153
Glenn L McGrath30db4232002-11-28 09:52:23 +0000154 host_ad = ntohl(ad);
155 np = NULL;
156 ent = NULL;
157 if (host) {
Eric Andersenc223ced2001-11-10 12:18:42 +0000158#ifdef DEBUG
Manuel Novoa III cad53642003-03-19 09:13:01 +0000159 bb_error_msg("gethostbyaddr (%08lx)", ad);
Eric Andersenc223ced2001-11-10 12:18:42 +0000160#endif
Glenn L McGrath30db4232002-11-28 09:52:23 +0000161 ent = gethostbyaddr((char *) &ad, 4, AF_INET);
162 if (ent != NULL) {
163 safe_strncpy(name, ent->h_name, len);
164 }
165 } else {
Eric Andersenc223ced2001-11-10 12:18:42 +0000166#ifdef DEBUG
Manuel Novoa III cad53642003-03-19 09:13:01 +0000167 bb_error_msg("getnetbyaddr (%08lx)", host_ad);
Eric Andersenc223ced2001-11-10 12:18:42 +0000168#endif
Glenn L McGrath30db4232002-11-28 09:52:23 +0000169 np = getnetbyaddr(host_ad, AF_INET);
170 if (np != NULL) {
171 safe_strncpy(name, np->n_name, len);
172 }
173 }
174 if ((ent == NULL) && (np == NULL)) {
175 safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
176 }
177 pn = (struct addr *) xmalloc(sizeof(struct addr));
178 pn->addr = *s_in;
179 pn->next = INET_nn;
180 pn->host = host;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000181 pn->name = bb_xstrdup(name);
Glenn L McGrath30db4232002-11-28 09:52:23 +0000182 INET_nn = pn;
Eric Andersenc223ced2001-11-10 12:18:42 +0000183
Glenn L McGrath30db4232002-11-28 09:52:23 +0000184 return (0);
Eric Andersenc223ced2001-11-10 12:18:42 +0000185}
Eric Andersen51b8bd62002-07-03 11:46:38 +0000186
Glenn L McGrathd7fb1b32002-11-26 02:40:56 +0000187#ifdef CONFIG_FEATURE_IPV6
Eric Andersen51b8bd62002-07-03 11:46:38 +0000188
189int INET6_resolve(char *name, struct sockaddr_in6 *sin6)
190{
Glenn L McGrath30db4232002-11-28 09:52:23 +0000191 struct addrinfo req, *ai;
192 int s;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000193
Glenn L McGrath30db4232002-11-28 09:52:23 +0000194 memset(&req, '\0', sizeof req);
195 req.ai_family = AF_INET6;
196 if ((s = getaddrinfo(name, NULL, &req, &ai))) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000197 bb_error_msg("getaddrinfo: %s: %d", name, s);
Glenn L McGrath30db4232002-11-28 09:52:23 +0000198 return -1;
199 }
200 memcpy(sin6, ai->ai_addr, sizeof(struct sockaddr_in6));
Eric Andersen51b8bd62002-07-03 11:46:38 +0000201
Glenn L McGrath30db4232002-11-28 09:52:23 +0000202 freeaddrinfo(ai);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000203
Glenn L McGrath30db4232002-11-28 09:52:23 +0000204 return (0);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000205}
206
207#ifndef IN6_IS_ADDR_UNSPECIFIED
Glenn L McGrathd7fb1b32002-11-26 02:40:56 +0000208# define IN6_IS_ADDR_UNSPECIFIED(a) \
Eric Andersen51b8bd62002-07-03 11:46:38 +0000209 (((__u32 *) (a))[0] == 0 && ((__u32 *) (a))[1] == 0 && \
210 ((__u32 *) (a))[2] == 0 && ((__u32 *) (a))[3] == 0)
211#endif
212
213
Glenn L McGrath30db4232002-11-28 09:52:23 +0000214int INET6_rresolve(char *name, size_t len, struct sockaddr_in6 *sin6,
215 int numeric)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000216{
Glenn L McGrath30db4232002-11-28 09:52:23 +0000217 int s;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000218
Glenn L McGrath30db4232002-11-28 09:52:23 +0000219 /* Grmpf. -FvK */
220 if (sin6->sin6_family != AF_INET6) {
Eric Andersen51b8bd62002-07-03 11:46:38 +0000221#ifdef DEBUG
Manuel Novoa III cad53642003-03-19 09:13:01 +0000222 bb_error_msg(_("rresolve: unsupport address family %d !\n"),
Glenn L McGrath30db4232002-11-28 09:52:23 +0000223 sin6->sin6_family);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000224#endif
Glenn L McGrath30db4232002-11-28 09:52:23 +0000225 errno = EAFNOSUPPORT;
226 return (-1);
227 }
228 if (numeric & 0x7FFF) {
229 inet_ntop(AF_INET6, &sin6->sin6_addr, name, len);
230 return (0);
231 }
232 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
233 if (numeric & 0x8000) {
234 strcpy(name, "default");
235 } else {
236 strcpy(name, "*");
237 }
238 return (0);
239 }
Eric Andersen51b8bd62002-07-03 11:46:38 +0000240
Glenn L McGrath30db4232002-11-28 09:52:23 +0000241 s = getnameinfo((struct sockaddr *) sin6, sizeof(struct sockaddr_in6), name, len, NULL, 0, 0);
242 if (s) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000243 bb_error_msg("getnameinfo failed");
Glenn L McGrath30db4232002-11-28 09:52:23 +0000244 return -1;
245 }
246 return (0);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000247}
248
Glenn L McGrath30db4232002-11-28 09:52:23 +0000249#endif /* CONFIG_FEATURE_IPV6 */