blob: c7bf409c4a9fa16d4cfb17e43c7e9c3bb7f1c060 [file] [log] [blame]
Eric Andersenc223ced2001-11-10 12:18:42 +00001/*
2 * stolen from net-tools-1.59 and stripped down for busybox by
3 * Erik Andersen <andersee@debian.org>
4 *
5 * Heavily modified by Manuel Novoa III Mar 12, 2001
6 *
Eric Andersen51b8bd62002-07-03 11:46:38 +00007 * Version: $Id: inet_common.c,v 1.3 2002/07/03 11:46:36 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
20#include <resolv.h>
21#endif
22
23
Eric Andersenc223ced2001-11-10 12:18:42 +000024const 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) fprintf (stderr, "gethostbyname (%s)\n", name);
47#endif
48 if (hostfirst &&
49 (hp = gethostbyname(name)) != (struct hostent *) NULL) {
50 memcpy((char *) &s_in->sin_addr, (char *) hp->h_addr_list[0],
51 sizeof(struct in_addr));
52 return 0;
53 }
54 /* Try the NETWORKS database to see if this is a known network. */
55#ifdef DEBUG
56 fprintf (stderr, "getnetbyname (%s)\n", name);
57#endif
58 if ((np = getnetbyname(name)) != (struct netent *) NULL) {
59 s_in->sin_addr.s_addr = htonl(np->n_net);
60 return 1;
61 }
62 if (hostfirst) {
63 /* Don't try again */
64 errno = h_errno;
65 return -1;
66 }
67#ifdef DEBUG
68 res_init();
69 _res.options |= RES_DEBUG;
70#endif
71
72#ifdef DEBUG
73 fprintf (stderr, "gethostbyname (%s)\n", name);
74#endif
75 if ((hp = gethostbyname(name)) == (struct hostent *) NULL) {
76 errno = h_errno;
77 return -1;
78 }
79 memcpy((char *) &s_in->sin_addr, (char *) hp->h_addr_list[0],
80 sizeof(struct in_addr));
81
82 return 0;
83}
84
85/* cache */
86struct addr {
87 struct sockaddr_in addr;
88 char *name;
89 int host;
90 struct addr *next;
91};
92
93static struct addr *INET_nn = NULL; /* addr-to-name cache */
94
95/* numeric: & 0x8000: default instead of *,
96 * & 0x4000: host instead of net,
97 * & 0x0fff: don't resolve
98 */
99int INET_rresolve(char *name, size_t len, struct sockaddr_in *s_in,
100 int numeric, unsigned int netmask)
101{
102 struct hostent *ent;
103 struct netent *np;
104 struct addr *pn;
105 unsigned long ad, host_ad;
106 int host = 0;
107
108 /* Grmpf. -FvK */
109 if (s_in->sin_family != AF_INET) {
110#ifdef DEBUG
Eric Andersen77d22c42002-06-06 12:11:55 +0000111 fprintf(stderr, "rresolve: unsupport address family %d !\n", s_in->sin_family);
Eric Andersenc223ced2001-11-10 12:18:42 +0000112#endif
113 errno = EAFNOSUPPORT;
114 return (-1);
115 }
116 ad = (unsigned long) s_in->sin_addr.s_addr;
117#ifdef DEBUG
118 fprintf (stderr, "rresolve: %08lx, mask %08x, num %08x \n", ad, netmask, numeric);
119#endif
120 if (ad == INADDR_ANY) {
121 if ((numeric & 0x0FFF) == 0) {
122 if (numeric & 0x8000)
123 safe_strncpy(name, bb_INET_default, len);
124 else
125 safe_strncpy(name, "*", len);
126 return (0);
127 }
128 }
129 if (numeric & 0x0FFF) {
130 safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
131 return (0);
132 }
133
134 if ((ad & (~netmask)) != 0 || (numeric & 0x4000))
135 host = 1;
136#if 0
137 INET_nn = NULL;
138#endif
139 pn = INET_nn;
140 while (pn != NULL) {
141 if (pn->addr.sin_addr.s_addr == ad && pn->host == host) {
142 safe_strncpy(name, pn->name, len);
143#ifdef DEBUG
144 fprintf (stderr, "rresolve: found %s %08lx in cache\n", (host? "host": "net"), ad);
145#endif
146 return (0);
147 }
148 pn = pn->next;
149 }
150
151 host_ad = ntohl(ad);
152 np = NULL;
153 ent = NULL;
154 if (host) {
155#ifdef DEBUG
156 fprintf (stderr, "gethostbyaddr (%08lx)\n", ad);
157#endif
158 ent = gethostbyaddr((char *) &ad, 4, AF_INET);
159 if (ent != NULL)
160 safe_strncpy(name, ent->h_name, len);
161 } else {
162#ifdef DEBUG
163 fprintf (stderr, "getnetbyaddr (%08lx)\n", host_ad);
164#endif
165 np = getnetbyaddr(host_ad, AF_INET);
166 if (np != NULL)
167 safe_strncpy(name, np->n_name, len);
168 }
169 if ((ent == NULL) && (np == NULL))
170 safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
171 pn = (struct addr *) xmalloc(sizeof(struct addr));
172 pn->addr = *s_in;
173 pn->next = INET_nn;
174 pn->host = host;
175 pn->name = xstrdup(name);
176 INET_nn = pn;
177
178 return (0);
179}
Eric Andersen51b8bd62002-07-03 11:46:38 +0000180
181#if CONFIG_FEATURE_IPV6
182
183int INET6_resolve(char *name, struct sockaddr_in6 *sin6)
184{
185 struct addrinfo req, *ai;
186 int s;
187
188 memset (&req, '\0', sizeof req);
189 req.ai_family = AF_INET6;
190 if ((s = getaddrinfo(name, NULL, &req, &ai))) {
191 fprintf(stderr, "getaddrinfo: %s: %d\n", name, s);
192 return -1;
193 }
194 memcpy(sin6, ai->ai_addr, sizeof(struct sockaddr_in6));
195
196 freeaddrinfo(ai);
197
198 return (0);
199}
200
201#ifndef IN6_IS_ADDR_UNSPECIFIED
202#define IN6_IS_ADDR_UNSPECIFIED(a) \
203 (((__u32 *) (a))[0] == 0 && ((__u32 *) (a))[1] == 0 && \
204 ((__u32 *) (a))[2] == 0 && ((__u32 *) (a))[3] == 0)
205#endif
206
207
208int INET6_rresolve(char *name, size_t len, struct sockaddr_in6 *sin6, int numeric)
209{
210 int s;
211
212 /* Grmpf. -FvK */
213 if (sin6->sin6_family != AF_INET6) {
214#ifdef DEBUG
215 fprintf(stderr, _("rresolve: unsupport address family %d !\n"),
216 sin6->sin6_family);
217#endif
218 errno = EAFNOSUPPORT;
219 return (-1);
220 }
221 if (numeric & 0x7FFF) {
222 inet_ntop(AF_INET6, &sin6->sin6_addr, name, len);
223 return (0);
224 }
225 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
226 if (numeric & 0x8000)
227 strcpy(name, "default");
228 else
229 strcpy(name, "*");
230 return (0);
231 }
232
233 if ((s = getnameinfo((struct sockaddr *) sin6, sizeof(struct sockaddr_in6),
234 name, len , NULL, 0, 0))) {
235 fputs("getnameinfo failed\n", stderr);
236 return -1;
237 }
238 return (0);
239}
240
241#endif /* CONFIG_FEATURE_IPV6 */