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)
20 s_in->sin_family = AF_INET;
23 /* Default is special, meaning 0.0.0.0. */
24 if (!strcmp(name, bb_str_default)) {
25 s_in->sin_addr.s_addr = INADDR_ANY;
28 /* Look to see if it's a dotted quad. */
29 if (inet_aton(name, &s_in->sin_addr)) {
32 /* If we expect this to be a hostname, try hostname database first */
35 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 /* Try the NETWORKS database to see if this is a known network. */
48 bb_error_msg("getnetbyname(%s)", name);
50 np = getnetbyname(name);
52 s_in->sin_addr.s_addr = htonl(np->n_net);
61 _res.options |= RES_DEBUG;
65 bb_error_msg("gethostbyname(%s)", name);
67 hp = gethostbyname(name);
71 memcpy(&s_in->sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
77 struct sockaddr_in addr;
83 static struct addr *INET_nn = NULL; /* addr-to-name cache */
85 /* numeric: & 0x8000: default instead of *,
86 * & 0x4000: host instead of net,
87 * & 0x0fff: don't resolve
89 int INET_rresolve(char *name, size_t len, struct sockaddr_in *s_in,
90 int numeric, unsigned int netmask)
99 if (s_in->sin_family != AF_INET) {
101 bb_error_msg("rresolve: unsupport address family %d !",
104 errno = EAFNOSUPPORT;
107 ad = s_in->sin_addr.s_addr;
109 bb_error_msg("rresolve: %08x, mask %08x, num %08x", (unsigned)ad, netmask, numeric);
111 if (ad == INADDR_ANY) {
112 if ((numeric & 0x0FFF) == 0) {
113 if (numeric & 0x8000)
114 safe_strncpy(name, bb_str_default, len);
116 safe_strncpy(name, "*", len);
120 if (numeric & 0x0FFF) {
121 safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
125 if ((ad & (~netmask)) != 0 || (numeric & 0x4000))
129 if (pn->addr.sin_addr.s_addr == ad && pn->host == host) {
130 safe_strncpy(name, pn->name, len);
132 bb_error_msg("rresolve: found %s %08x in cache",
133 (host ? "host" : "net"), (unsigned)ad);
145 bb_error_msg("gethostbyaddr (%08x)", (unsigned)ad);
147 ent = gethostbyaddr((char *) &ad, 4, AF_INET);
149 safe_strncpy(name, ent->h_name, len);
153 bb_error_msg("getnetbyaddr (%08x)", (unsigned)host_ad);
155 np = getnetbyaddr(host_ad, AF_INET);
157 safe_strncpy(name, np->n_name, len);
161 safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
163 pn = xmalloc(sizeof(struct addr));
167 pn->name = xstrdup(name);
172 #ifdef CONFIG_FEATURE_IPV6
174 int INET6_resolve(const char *name, struct sockaddr_in6 *sin6)
176 struct addrinfo req, *ai;
179 memset(&req, '\0', sizeof req);
180 req.ai_family = AF_INET6;
181 s = getaddrinfo(name, NULL, &req, &ai);
183 bb_error_msg("getaddrinfo: %s: %d", name, s);
186 memcpy(sin6, ai->ai_addr, sizeof(struct sockaddr_in6));
191 #ifndef IN6_IS_ADDR_UNSPECIFIED
192 # define IN6_IS_ADDR_UNSPECIFIED(a) \
193 (((uint32_t *) (a))[0] == 0 && ((uint32_t *) (a))[1] == 0 && \
194 ((uint32_t *) (a))[2] == 0 && ((uint32_t *) (a))[3] == 0)
198 int INET6_rresolve(char *name, size_t len, struct sockaddr_in6 *sin6,
204 if (sin6->sin6_family != AF_INET6) {
206 bb_error_msg("rresolve: unsupport address family %d!",
209 errno = EAFNOSUPPORT;
212 if (numeric & 0x7FFF) {
213 inet_ntop(AF_INET6, &sin6->sin6_addr, name, len);
216 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
217 if (numeric & 0x8000) {
218 strcpy(name, bb_str_default);
226 s = getnameinfo((struct sockaddr *) sin6, sizeof(struct sockaddr_in6),
227 name, len, NULL, 0, 0);
229 bb_error_msg("getnameinfo failed");
235 #endif /* CONFIG_FEATURE_IPV6 */