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 * Version: $Id: inet_common.c,v 1.8 2004/03/10 07:42:38 mjn3 Exp $
13 #include "inet_common.h"
15 int INET_resolve(const char *name, struct sockaddr_in *s_in, int hostfirst)
21 s_in->sin_family = AF_INET;
24 /* Default is special, meaning 0.0.0.0. */
25 if (!strcmp(name, bb_str_default)) {
26 s_in->sin_addr.s_addr = INADDR_ANY;
29 /* Look to see if it's a dotted quad. */
30 if (inet_aton(name, &s_in->sin_addr)) {
33 /* If we expect this to be a hostname, try hostname database first */
36 bb_error_msg("gethostbyname (%s)", name);
39 if (hostfirst && (hp = gethostbyname(name)) != (struct hostent *) NULL) {
40 memcpy((char *) &s_in->sin_addr, (char *) hp->h_addr_list[0],
41 sizeof(struct in_addr));
44 /* Try the NETWORKS database to see if this is a known network. */
46 bb_error_msg("getnetbyname (%s)", name);
48 if ((np = getnetbyname(name)) != (struct netent *) NULL) {
49 s_in->sin_addr.s_addr = htonl(np->n_net);
58 _res.options |= RES_DEBUG;
62 bb_error_msg("gethostbyname (%s)", name);
64 if ((hp = gethostbyname(name)) == (struct hostent *) NULL) {
67 memcpy((char *) &s_in->sin_addr, (char *) hp->h_addr_list[0],
68 sizeof(struct in_addr));
75 struct sockaddr_in addr;
81 static struct addr *INET_nn = NULL; /* addr-to-name cache */
83 /* numeric: & 0x8000: default instead of *,
84 * & 0x4000: host instead of net,
85 * & 0x0fff: don't resolve
87 int INET_rresolve(char *name, size_t len, struct sockaddr_in *s_in,
88 int numeric, unsigned int netmask)
93 unsigned long ad, host_ad;
97 if (s_in->sin_family != AF_INET) {
99 bb_error_msg("rresolve: unsupport address family %d !",
102 errno = EAFNOSUPPORT;
105 ad = (unsigned long) s_in->sin_addr.s_addr;
107 bb_error_msg("rresolve: %08lx, mask %08x, num %08x", ad, netmask, numeric);
109 if (ad == INADDR_ANY) {
110 if ((numeric & 0x0FFF) == 0) {
111 if (numeric & 0x8000)
112 safe_strncpy(name, bb_str_default, len);
114 safe_strncpy(name, "*", len);
118 if (numeric & 0x0FFF) {
119 safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
123 if ((ad & (~netmask)) != 0 || (numeric & 0x4000))
127 if (pn->addr.sin_addr.s_addr == ad && pn->host == host) {
128 safe_strncpy(name, pn->name, len);
130 bb_error_msg("rresolve: found %s %08lx in cache",
131 (host ? "host" : "net"), ad);
143 bb_error_msg("gethostbyaddr (%08lx)", ad);
145 ent = gethostbyaddr((char *) &ad, 4, AF_INET);
147 safe_strncpy(name, ent->h_name, len);
151 bb_error_msg("getnetbyaddr (%08lx)", host_ad);
153 np = getnetbyaddr(host_ad, AF_INET);
155 safe_strncpy(name, np->n_name, len);
158 if ((ent == NULL) && (np == NULL)) {
159 safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
161 pn = xmalloc(sizeof(struct addr));
165 pn->name = xstrdup(name);
171 #ifdef CONFIG_FEATURE_IPV6
173 int INET6_resolve(const char *name, struct sockaddr_in6 *sin6)
175 struct addrinfo req, *ai;
178 memset(&req, '\0', sizeof req);
179 req.ai_family = AF_INET6;
180 s = getaddrinfo(name, NULL, &req, &ai);
182 bb_error_msg("getaddrinfo: %s: %d", name, s);
185 memcpy(sin6, ai->ai_addr, sizeof(struct sockaddr_in6));
192 #ifndef IN6_IS_ADDR_UNSPECIFIED
193 # define IN6_IS_ADDR_UNSPECIFIED(a) \
194 (((uint32_t *) (a))[0] == 0 && ((uint32_t *) (a))[1] == 0 && \
195 ((uint32_t *) (a))[2] == 0 && ((uint32_t *) (a))[3] == 0)
199 int INET6_rresolve(char *name, size_t len, struct sockaddr_in6 *sin6,
205 if (sin6->sin6_family != AF_INET6) {
207 bb_error_msg("rresolve: unsupport address family %d!",
210 errno = EAFNOSUPPORT;
213 if (numeric & 0x7FFF) {
214 inet_ntop(AF_INET6, &sin6->sin6_addr, name, len);
217 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
218 if (numeric & 0x8000) {
219 strcpy(name, bb_str_default);
227 s = getnameinfo((struct sockaddr *) sin6, sizeof(struct sockaddr_in6), name, len, NULL, 0, 0);
229 bb_error_msg("getnameinfo failed");
235 #endif /* CONFIG_FEATURE_IPV6 */