2 * stolen from net-tools-1.59 and stripped down for busybox by
3 * Erik Andersen <andersee@debian.org>
5 * Heavily modified by Manuel Novoa III Mar 12, 2001
7 * Version: $Id: inet_common.c,v 1.3 2002/07/03 11:46:36 andersen Exp $
11 #include "inet_common.h"
24 const char bb_INET_default[]="default";
26 int INET_resolve(const char *name, struct sockaddr_in *s_in, int hostfirst)
32 s_in->sin_family = AF_INET;
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;
40 /* Look to see if it's a dotted quad. */
41 if (inet_aton(name, &s_in->sin_addr)) {
44 /* If we expect this to be a hostname, try hostname database first */
46 if (hostfirst) fprintf (stderr, "gethostbyname (%s)\n", name);
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));
54 /* Try the NETWORKS database to see if this is a known network. */
56 fprintf (stderr, "getnetbyname (%s)\n", name);
58 if ((np = getnetbyname(name)) != (struct netent *) NULL) {
59 s_in->sin_addr.s_addr = htonl(np->n_net);
69 _res.options |= RES_DEBUG;
73 fprintf (stderr, "gethostbyname (%s)\n", name);
75 if ((hp = gethostbyname(name)) == (struct hostent *) NULL) {
79 memcpy((char *) &s_in->sin_addr, (char *) hp->h_addr_list[0],
80 sizeof(struct in_addr));
87 struct sockaddr_in addr;
93 static struct addr *INET_nn = NULL; /* addr-to-name cache */
95 /* numeric: & 0x8000: default instead of *,
96 * & 0x4000: host instead of net,
97 * & 0x0fff: don't resolve
99 int INET_rresolve(char *name, size_t len, struct sockaddr_in *s_in,
100 int numeric, unsigned int netmask)
105 unsigned long ad, host_ad;
109 if (s_in->sin_family != AF_INET) {
111 fprintf(stderr, "rresolve: unsupport address family %d !\n", s_in->sin_family);
113 errno = EAFNOSUPPORT;
116 ad = (unsigned long) s_in->sin_addr.s_addr;
118 fprintf (stderr, "rresolve: %08lx, mask %08x, num %08x \n", ad, netmask, numeric);
120 if (ad == INADDR_ANY) {
121 if ((numeric & 0x0FFF) == 0) {
122 if (numeric & 0x8000)
123 safe_strncpy(name, bb_INET_default, len);
125 safe_strncpy(name, "*", len);
129 if (numeric & 0x0FFF) {
130 safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
134 if ((ad & (~netmask)) != 0 || (numeric & 0x4000))
141 if (pn->addr.sin_addr.s_addr == ad && pn->host == host) {
142 safe_strncpy(name, pn->name, len);
144 fprintf (stderr, "rresolve: found %s %08lx in cache\n", (host? "host": "net"), ad);
156 fprintf (stderr, "gethostbyaddr (%08lx)\n", ad);
158 ent = gethostbyaddr((char *) &ad, 4, AF_INET);
160 safe_strncpy(name, ent->h_name, len);
163 fprintf (stderr, "getnetbyaddr (%08lx)\n", host_ad);
165 np = getnetbyaddr(host_ad, AF_INET);
167 safe_strncpy(name, np->n_name, len);
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));
175 pn->name = xstrdup(name);
181 #if CONFIG_FEATURE_IPV6
183 int INET6_resolve(char *name, struct sockaddr_in6 *sin6)
185 struct addrinfo req, *ai;
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);
194 memcpy(sin6, ai->ai_addr, sizeof(struct sockaddr_in6));
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)
208 int INET6_rresolve(char *name, size_t len, struct sockaddr_in6 *sin6, int numeric)
213 if (sin6->sin6_family != AF_INET6) {
215 fprintf(stderr, _("rresolve: unsupport address family %d !\n"),
218 errno = EAFNOSUPPORT;
221 if (numeric & 0x7FFF) {
222 inet_ntop(AF_INET6, &sin6->sin6_addr, name, len);
225 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
226 if (numeric & 0x8000)
227 strcpy(name, "default");
233 if ((s = getnameinfo((struct sockaddr *) sin6, sizeof(struct sockaddr_in6),
234 name, len , NULL, 0, 0))) {
235 fputs("getnameinfo failed\n", stderr);
241 #endif /* CONFIG_FEATURE_IPV6 */