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 const char bb_INET_default[] = "default";
17 int INET_resolve(const char *name, struct sockaddr_in *s_in, int hostfirst)
23 s_in->sin_family = AF_INET;
26 /* Default is special, meaning 0.0.0.0. */
27 if (!strcmp(name, bb_INET_default)) {
28 s_in->sin_addr.s_addr = INADDR_ANY;
31 /* Look to see if it's a dotted quad. */
32 if (inet_aton(name, &s_in->sin_addr)) {
35 /* If we expect this to be a hostname, try hostname database first */
38 bb_error_msg("gethostbyname (%s)", name);
41 if (hostfirst && (hp = gethostbyname(name)) != (struct hostent *) NULL) {
42 memcpy((char *) &s_in->sin_addr, (char *) hp->h_addr_list[0],
43 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 if ((np = getnetbyname(name)) != (struct netent *) NULL) {
51 s_in->sin_addr.s_addr = htonl(np->n_net);
60 _res.options |= RES_DEBUG;
64 bb_error_msg("gethostbyname (%s)", name);
66 if ((hp = gethostbyname(name)) == (struct hostent *) NULL) {
69 memcpy((char *) &s_in->sin_addr, (char *) hp->h_addr_list[0],
70 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)
95 unsigned long ad, host_ad;
99 if (s_in->sin_family != AF_INET) {
101 bb_error_msg("rresolve: unsupport address family %d !",
104 errno = EAFNOSUPPORT;
107 ad = (unsigned long) s_in->sin_addr.s_addr;
109 bb_error_msg("rresolve: %08lx, mask %08x, num %08x", ad, netmask, numeric);
111 if (ad == INADDR_ANY) {
112 if ((numeric & 0x0FFF) == 0) {
113 if (numeric & 0x8000)
114 safe_strncpy(name, bb_INET_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 %08lx in cache",
133 (host ? "host" : "net"), ad);
145 bb_error_msg("gethostbyaddr (%08lx)", ad);
147 ent = gethostbyaddr((char *) &ad, 4, AF_INET);
149 safe_strncpy(name, ent->h_name, len);
153 bb_error_msg("getnetbyaddr (%08lx)", host_ad);
155 np = getnetbyaddr(host_ad, AF_INET);
157 safe_strncpy(name, np->n_name, len);
160 if ((ent == NULL) && (np == NULL)) {
161 safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
163 pn = (struct addr *) xmalloc(sizeof(struct addr));
167 pn->name = xstrdup(name);
173 #ifdef CONFIG_FEATURE_IPV6
175 int INET6_resolve(const char *name, struct sockaddr_in6 *sin6)
177 struct addrinfo req, *ai;
180 memset(&req, '\0', sizeof req);
181 req.ai_family = AF_INET6;
182 if ((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));
193 #ifndef IN6_IS_ADDR_UNSPECIFIED
194 # define IN6_IS_ADDR_UNSPECIFIED(a) \
195 (((uint32_t *) (a))[0] == 0 && ((uint32_t *) (a))[1] == 0 && \
196 ((uint32_t *) (a))[2] == 0 && ((uint32_t *) (a))[3] == 0)
200 int INET6_rresolve(char *name, size_t len, struct sockaddr_in6 *sin6,
206 if (sin6->sin6_family != AF_INET6) {
208 bb_error_msg(_("rresolve: unsupport address family %d!"),
211 errno = EAFNOSUPPORT;
214 if (numeric & 0x7FFF) {
215 inet_ntop(AF_INET6, &sin6->sin6_addr, name, len);
218 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
219 if (numeric & 0x8000) {
220 strcpy(name, "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 */