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
12 #include "inet_common.h"
14 int 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, bb_str_default)) {
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);
41 hp = gethostbyname(name);
43 memcpy(&s_in->sin_addr, hp->h_addr_list[0],
44 sizeof(struct in_addr));
48 #if ENABLE_FEATURE_ETC_NETWORKS
49 /* Try the NETWORKS database to see if this is a known network. */
51 bb_error_msg("getnetbyname(%s)", name);
53 np = getnetbyname(name);
55 s_in->sin_addr.s_addr = htonl(np->n_net);
65 _res.options |= RES_DEBUG;
69 bb_error_msg("gethostbyname(%s)", name);
71 hp = gethostbyname(name);
75 memcpy(&s_in->sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
80 /* numeric: & 0x8000: default instead of *,
81 * & 0x4000: host instead of net,
82 * & 0x0fff: don't resolve
84 char *INET_rresolve(struct sockaddr_in *s_in, int numeric, uint32_t netmask)
86 /* addr-to-name cache */
89 struct sockaddr_in addr;
93 static struct addr *cache = NULL;
101 if (s_in->sin_family != AF_INET) {
103 bb_error_msg("rresolve: unsupported address family %d!",
106 errno = EAFNOSUPPORT;
109 ad = s_in->sin_addr.s_addr;
111 bb_error_msg("rresolve: %08x, mask %08x, num %08x", (unsigned)ad, netmask, numeric);
113 if (ad == INADDR_ANY) {
114 if ((numeric & 0x0FFF) == 0) {
115 if (numeric & 0x8000)
116 return xstrdup(bb_str_default);
120 if (numeric & 0x0FFF)
121 return xstrdup(inet_ntoa(s_in->sin_addr));
123 if ((ad & (~netmask)) != 0 || (numeric & 0x4000))
127 if (pn->addr.sin_addr.s_addr == ad && pn->host == host) {
129 bb_error_msg("rresolve: found %s %08x in cache",
130 (host ? "host" : "net"), (unsigned)ad);
132 return xstrdup(pn->name);
142 bb_error_msg("gethostbyaddr (%08x)", (unsigned)ad);
144 ent = gethostbyaddr((char *) &ad, 4, AF_INET);
146 name = xstrdup(ent->h_name);
147 } else if (ENABLE_FEATURE_ETC_NETWORKS) {
150 bb_error_msg("getnetbyaddr (%08x)", (unsigned)host_ad);
152 np = getnetbyaddr(host_ad, AF_INET);
154 name = xstrdup(np->n_name);
157 name = xstrdup(inet_ntoa(s_in->sin_addr));
158 pn = xmalloc(sizeof(*pn) + strlen(name)); /* no '+ 1', it's already accounted for */
162 strcpy(pn->name, name);
167 #ifdef CONFIG_FEATURE_IPV6
169 int INET6_resolve(const char *name, struct sockaddr_in6 *sin6)
171 struct addrinfo req, *ai;
174 memset(&req, '\0', sizeof req);
175 req.ai_family = AF_INET6;
176 s = getaddrinfo(name, NULL, &req, &ai);
178 bb_error_msg("getaddrinfo: %s: %d", name, s);
181 memcpy(sin6, ai->ai_addr, sizeof(struct sockaddr_in6));
186 #ifndef IN6_IS_ADDR_UNSPECIFIED
187 # define IN6_IS_ADDR_UNSPECIFIED(a) \
188 (((uint32_t *) (a))[0] == 0 && ((uint32_t *) (a))[1] == 0 && \
189 ((uint32_t *) (a))[2] == 0 && ((uint32_t *) (a))[3] == 0)
193 char *INET6_rresolve(struct sockaddr_in6 *sin6, int numeric)
199 if (sin6->sin6_family != AF_INET6) {
201 bb_error_msg("rresolve: unsupport address family %d!",
204 errno = EAFNOSUPPORT;
207 if (numeric & 0x7FFF) {
208 inet_ntop(AF_INET6, &sin6->sin6_addr, name, sizeof(name));
209 return xstrdup(name);
211 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
212 if (numeric & 0x8000)
213 return xstrdup(bb_str_default);
217 s = getnameinfo((struct sockaddr *) sin6, sizeof(struct sockaddr_in6),
218 name, sizeof(name), NULL, 0, 0);
220 bb_error_msg("getnameinfo failed");
223 return xstrdup(name);
226 #endif /* CONFIG_FEATURE_IPV6 */