aboutsummaryrefslogtreecommitdiff
path: root/busybox/libbb/inet_common.c
diff options
context:
space:
mode:
Diffstat (limited to 'busybox/libbb/inet_common.c')
-rw-r--r--busybox/libbb/inet_common.c249
1 files changed, 249 insertions, 0 deletions
diff --git a/busybox/libbb/inet_common.c b/busybox/libbb/inet_common.c
new file mode 100644
index 000000000..321322d1f
--- /dev/null
+++ b/busybox/libbb/inet_common.c
@@ -0,0 +1,249 @@
1/*
2 * stolen from net-tools-1.59 and stripped down for busybox by
3 * Erik Andersen <andersen@codepoet.org>
4 *
5 * Heavily modified by Manuel Novoa III Mar 12, 2001
6 *
7 * Version: $Id: inet_common.c,v 1.8 2004/03/10 07:42:38 mjn3 Exp $
8 *
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
19#ifdef DEBUG
20# include <resolv.h>
21#endif
22
23
24const char bb_INET_default[] = "default";
25
26int INET_resolve(const char *name, struct sockaddr_in *s_in, int hostfirst)
27{
28 struct hostent *hp;
29 struct netent *np;
30
31 /* Grmpf. -FvK */
32 s_in->sin_family = AF_INET;
33 s_in->sin_port = 0;
34
35 /* 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 */
45#ifdef DEBUG
46 if (hostfirst) {
47 bb_error_msg("gethostbyname (%s)", name);
48 }
49#endif
50 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
57 bb_error_msg("getnetbyname (%s)", name);
58#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
74 bb_error_msg("gethostbyname (%s)", name);
75#endif
76 if ((hp = gethostbyname(name)) == (struct hostent *) NULL) {
77 errno = h_errno;
78 return -1;
79 }
80 memcpy((char *) &s_in->sin_addr, (char *) hp->h_addr_list[0],
81 sizeof(struct in_addr));
82
83 return 0;
84}
85
86/* cache */
87struct addr {
88 struct sockaddr_in addr;
89 char *name;
90 int host;
91 struct addr *next;
92};
93
94static struct addr *INET_nn = NULL; /* addr-to-name cache */
95
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,
101 int numeric, unsigned int netmask)
102{
103 struct hostent *ent;
104 struct netent *np;
105 struct addr *pn;
106 unsigned long ad, host_ad;
107 int host = 0;
108
109 /* Grmpf. -FvK */
110 if (s_in->sin_family != AF_INET) {
111#ifdef DEBUG
112 bb_error_msg("rresolve: unsupport address family %d !",
113 s_in->sin_family);
114#endif
115 errno = EAFNOSUPPORT;
116 return (-1);
117 }
118 ad = (unsigned long) s_in->sin_addr.s_addr;
119#ifdef DEBUG
120 bb_error_msg("rresolve: %08lx, mask %08x, num %08x", ad, netmask, numeric);
121#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 }
135
136 if ((ad & (~netmask)) != 0 || (numeric & 0x4000))
137 host = 1;
138#if 0
139 INET_nn = NULL;
140#endif
141 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);
145#ifdef DEBUG
146 bb_error_msg("rresolve: found %s %08lx in cache",
147 (host ? "host" : "net"), ad);
148#endif
149 return (0);
150 }
151 pn = pn->next;
152 }
153
154 host_ad = ntohl(ad);
155 np = NULL;
156 ent = NULL;
157 if (host) {
158#ifdef DEBUG
159 bb_error_msg("gethostbyaddr (%08lx)", ad);
160#endif
161 ent = gethostbyaddr((char *) &ad, 4, AF_INET);
162 if (ent != NULL) {
163 safe_strncpy(name, ent->h_name, len);
164 }
165 } else {
166#ifdef DEBUG
167 bb_error_msg("getnetbyaddr (%08lx)", host_ad);
168#endif
169 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;
181 pn->name = bb_xstrdup(name);
182 INET_nn = pn;
183
184 return (0);
185}
186
187#ifdef CONFIG_FEATURE_IPV6
188
189int INET6_resolve(const char *name, struct sockaddr_in6 *sin6)
190{
191 struct addrinfo req, *ai;
192 int s;
193
194 memset(&req, '\0', sizeof req);
195 req.ai_family = AF_INET6;
196 if ((s = getaddrinfo(name, NULL, &req, &ai))) {
197 bb_error_msg("getaddrinfo: %s: %d", name, s);
198 return -1;
199 }
200 memcpy(sin6, ai->ai_addr, sizeof(struct sockaddr_in6));
201
202 freeaddrinfo(ai);
203
204 return (0);
205}
206
207#ifndef IN6_IS_ADDR_UNSPECIFIED
208# define IN6_IS_ADDR_UNSPECIFIED(a) \
209 (((__u32 *) (a))[0] == 0 && ((__u32 *) (a))[1] == 0 && \
210 ((__u32 *) (a))[2] == 0 && ((__u32 *) (a))[3] == 0)
211#endif
212
213
214int INET6_rresolve(char *name, size_t len, struct sockaddr_in6 *sin6,
215 int numeric)
216{
217 int s;
218
219 /* Grmpf. -FvK */
220 if (sin6->sin6_family != AF_INET6) {
221#ifdef DEBUG
222 bb_error_msg(_("rresolve: unsupport address family %d !\n"),
223 sin6->sin6_family);
224#endif
225 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 }
240
241 s = getnameinfo((struct sockaddr *) sin6, sizeof(struct sockaddr_in6), name, len, NULL, 0, 0);
242 if (s) {
243 bb_error_msg("getnameinfo failed");
244 return -1;
245 }
246 return (0);
247}
248
249#endif /* CONFIG_FEATURE_IPV6 */