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.
12 #include "inet_common.h"
14 int FAST_FUNC INET_resolve(const char *name, struct sockaddr_in *s_in, int hostfirst)
17 #if ENABLE_FEATURE_ETC_NETWORKS
22 s_in->sin_family = AF_INET;
25 /* Default is special, meaning 0.0.0.0. */
26 if (strcmp(name, "default") == 0) {
27 s_in->sin_addr.s_addr = INADDR_ANY;
30 /* Look to see if it's a dotted quad. */
31 if (inet_aton(name, &s_in->sin_addr)) {
34 /* If we expect this to be a hostname, try hostname database first */
37 bb_error_msg("gethostbyname(%s)", name);
39 hp = gethostbyname(name);
41 memcpy(&s_in->sin_addr, hp->h_addr_list[0],
42 sizeof(struct in_addr));
46 #if ENABLE_FEATURE_ETC_NETWORKS
47 /* Try the NETWORKS database to see if this is a known network. */
49 bb_error_msg("getnetbyname(%s)", name);
51 np = getnetbyname(name);
53 s_in->sin_addr.s_addr = htonl(np->n_net);
63 _res.options |= RES_DEBUG;
64 bb_error_msg("gethostbyname(%s)", name);
66 hp = gethostbyname(name);
70 memcpy(&s_in->sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
75 /* numeric: & 0x8000: "default" instead of "*",
76 * & 0x4000: host instead of net,
77 * & 0x0fff: don't resolve
79 char* FAST_FUNC INET_rresolve(struct sockaddr_in *s_in, int numeric, uint32_t netmask)
81 /* addr-to-name cache */
88 static struct addr *cache = NULL;
95 if (s_in->sin_family != AF_INET) {
97 bb_error_msg("rresolve: unsupported address family %d!",
100 errno = EAFNOSUPPORT;
103 nip = s_in->sin_addr.s_addr;
105 bb_error_msg("rresolve: %08x mask:%08x num:%08x", (unsigned)nip, netmask, numeric);
107 if (numeric & 0x0FFF)
108 return xmalloc_sockaddr2dotted_noport((void*)s_in);
109 if (nip == INADDR_ANY) {
110 if (numeric & 0x8000)
111 return xstrdup("default");
115 is_host = ((nip & (~netmask)) != 0 || (numeric & 0x4000));
119 if (pn->nip == nip && pn->is_host == is_host) {
121 bb_error_msg("rresolve: found %s %08x in cache",
122 (is_host ? "host" : "net"), (unsigned)nip);
124 return xstrdup(pn->name);
132 bb_error_msg("sockaddr2host_noport(%08x)", (unsigned)nip);
134 name = xmalloc_sockaddr2host_noport((void*)s_in);
135 } else if (ENABLE_FEATURE_ETC_NETWORKS) {
138 bb_error_msg("getnetbyaddr(%08x)", (unsigned)ntohl(nip));
140 np = getnetbyaddr(ntohl(nip), AF_INET);
142 name = xstrdup(np->n_name);
145 name = xmalloc_sockaddr2dotted_noport((void*)s_in);
147 pn = xmalloc(sizeof(*pn) + strlen(name)); /* no '+ 1', it's already accounted for */
150 pn->is_host = is_host;
151 strcpy(pn->name, name);
157 #if ENABLE_FEATURE_IPV6
159 int FAST_FUNC INET6_resolve(const char *name, struct sockaddr_in6 *sin6)
161 struct addrinfo req, *ai = NULL;
164 memset(&req, 0, sizeof(req));
165 req.ai_family = AF_INET6;
166 s = getaddrinfo(name, NULL, &req, &ai);
168 bb_error_msg("getaddrinfo: %s: %d", name, s);
171 memcpy(sin6, ai->ai_addr, sizeof(*sin6));
176 #ifndef IN6_IS_ADDR_UNSPECIFIED
177 # define IN6_IS_ADDR_UNSPECIFIED(a) \
178 (((uint32_t *) (a))[0] == 0 && ((uint32_t *) (a))[1] == 0 && \
179 ((uint32_t *) (a))[2] == 0 && ((uint32_t *) (a))[3] == 0)
183 char* FAST_FUNC INET6_rresolve(struct sockaddr_in6 *sin6, int numeric)
185 if (sin6->sin6_family != AF_INET6) {
187 bb_error_msg("rresolve: unsupported address family %d!",
190 errno = EAFNOSUPPORT;
193 if (numeric & 0x7FFF) {
194 return xmalloc_sockaddr2dotted_noport((void*)sin6);
196 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
197 if (numeric & 0x8000)
198 return xstrdup("default");
202 return xmalloc_sockaddr2host_noport((void*)sin6);
205 #endif /* CONFIG_FEATURE_IPV6 */