1 /* vi: set sw=4 ts=4: */
3 * stolen from net-tools-1.59 and stripped down for busybox by
4 * Erik Andersen <andersen@codepoet.org>
6 * Heavily modified by Manuel Novoa III Mar 12, 2001
8 * Licensed under GPLv2, see file LICENSE in this source tree.
11 #include "inet_common.h"
14 # define dbg(...) bb_error_msg(__VA_ARGS__)
16 # define dbg(...) ((void)0)
19 int FAST_FUNC INET_resolve(const char *name, struct sockaddr_in *s_in, int hostfirst)
22 #if ENABLE_FEATURE_ETC_NETWORKS
27 s_in->sin_family = AF_INET;
30 /* Default is special, meaning 0.0.0.0. */
31 if (strcmp(name, "default") == 0) {
32 s_in->sin_addr.s_addr = INADDR_ANY;
35 /* Look to see if it's a dotted quad. */
36 if (inet_aton(name, &s_in->sin_addr)) {
39 /* If we expect this to be a hostname, try hostname database first */
41 dbg("gethostbyname(%s)", name);
42 hp = gethostbyname(name);
44 memcpy(&s_in->sin_addr, hp->h_addr_list[0],
45 sizeof(struct in_addr));
49 #if ENABLE_FEATURE_ETC_NETWORKS
50 /* Try the NETWORKS database to see if this is a known network. */
51 dbg("getnetbyname(%s)", name);
52 np = getnetbyname(name);
54 s_in->sin_addr.s_addr = htonl(np->n_net);
64 _res.options |= RES_DEBUG;
66 dbg("gethostbyname(%s)", name);
67 hp = gethostbyname(name);
71 memcpy(&s_in->sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
76 /* numeric: & 0x8000: "default" instead of "*",
77 * & 0x4000: host instead of net,
78 * & 0x0fff: don't resolve
80 char* FAST_FUNC INET_rresolve(struct sockaddr_in *s_in, int numeric, uint32_t netmask)
82 /* addr-to-name cache */
89 static struct addr *cache = NULL;
96 if (s_in->sin_family != AF_INET) {
97 dbg("rresolve: unsupported address family %d!", s_in->sin_family);
101 nip = s_in->sin_addr.s_addr;
102 dbg("rresolve: %08x mask:%08x num:%08x", (unsigned)nip, netmask, numeric);
103 if (numeric & 0x0FFF)
104 return xmalloc_sockaddr2dotted_noport((void*)s_in);
105 if (nip == INADDR_ANY) {
106 if (numeric & 0x8000)
107 return xstrdup("default");
111 is_host = ((nip & (~netmask)) != 0 || (numeric & 0x4000));
115 if (pn->nip == nip && pn->is_host == is_host) {
116 dbg("rresolve: found %s %08x in cache",
117 (is_host ? "host" : "net"), (unsigned)nip);
118 return xstrdup(pn->name);
125 dbg("sockaddr2host_noport(%08x)", (unsigned)nip);
126 name = xmalloc_sockaddr2host_noport((void*)s_in);
128 #if ENABLE_FEATURE_ETC_NETWORKS
131 dbg("getnetbyaddr(%08x)", (unsigned)ntohl(nip));
132 np = getnetbyaddr(ntohl(nip), AF_INET);
134 name = xstrdup(np->n_name);
138 name = xmalloc_sockaddr2dotted_noport((void*)s_in);
140 pn = xmalloc(sizeof(*pn) + strlen(name)); /* no '+ 1', it's already accounted for */
143 pn->is_host = is_host;
144 strcpy(pn->name, name);
150 #if ENABLE_FEATURE_IPV6
152 int FAST_FUNC INET6_resolve(const char *name, struct sockaddr_in6 *sin6)
154 struct addrinfo req, *ai = NULL;
157 memset(&req, 0, sizeof(req));
158 req.ai_family = AF_INET6;
159 s = getaddrinfo(name, NULL, &req, &ai);
161 bb_error_msg("getaddrinfo: %s: %d", name, s);
164 memcpy(sin6, ai->ai_addr, sizeof(*sin6));
169 #ifndef IN6_IS_ADDR_UNSPECIFIED
170 # define IN6_IS_ADDR_UNSPECIFIED(a) \
171 (((uint32_t *) (a))[0] == 0 && ((uint32_t *) (a))[1] == 0 && \
172 ((uint32_t *) (a))[2] == 0 && ((uint32_t *) (a))[3] == 0)
176 char* FAST_FUNC INET6_rresolve(struct sockaddr_in6 *sin6, int numeric)
178 if (sin6->sin6_family != AF_INET6) {
179 dbg("rresolve: unsupported address family %d!",
181 errno = EAFNOSUPPORT;
184 if (numeric & 0x7FFF) {
185 return xmalloc_sockaddr2dotted_noport((void*)sin6);
187 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
188 if (numeric & 0x8000)
189 return xstrdup("default");
193 return xmalloc_sockaddr2host_noport((void*)sin6);
196 #endif /* CONFIG_FEATURE_IPV6 */