2 * stolen from net-tools-1.59 and stripped down for busybox by
3 * Erik Andersen <andersen@codepoet.org>
5 * Heavily modified by Manuel Novoa III Mar 12, 2001
7 * Version: $Id: inet_common.c,v 1.7 2003/07/14 21:20:55 andersen Exp $
11 #include "inet_common.h"
24 const char bb_INET_default[] = "default";
26 int INET_resolve(const char *name, struct sockaddr_in *s_in, int hostfirst)
32 s_in->sin_family = AF_INET;
35 /* Default is special, meaning 0.0.0.0. */
36 if (!strcmp(name, bb_INET_default)) {
37 s_in->sin_addr.s_addr = INADDR_ANY;
40 /* Look to see if it's a dotted quad. */
41 if (inet_aton(name, &s_in->sin_addr)) {
44 /* If we expect this to be a hostname, try hostname database first */
47 bb_error_msg("gethostbyname (%s)", name);
50 if (hostfirst && (hp = gethostbyname(name)) != (struct hostent *) NULL) {
51 memcpy((char *) &s_in->sin_addr, (char *) hp->h_addr_list[0],
52 sizeof(struct in_addr));
55 /* Try the NETWORKS database to see if this is a known network. */
57 bb_error_msg("getnetbyname (%s)", name);
59 if ((np = getnetbyname(name)) != (struct netent *) NULL) {
60 s_in->sin_addr.s_addr = htonl(np->n_net);
70 _res.options |= RES_DEBUG;
74 bb_error_msg("gethostbyname (%s)", name);
76 if ((hp = gethostbyname(name)) == (struct hostent *) NULL) {
80 memcpy((char *) &s_in->sin_addr, (char *) hp->h_addr_list[0],
81 sizeof(struct in_addr));
88 struct sockaddr_in addr;
94 static struct addr *INET_nn = NULL; /* addr-to-name cache */
96 /* numeric: & 0x8000: default instead of *,
97 * & 0x4000: host instead of net,
98 * & 0x0fff: don't resolve
100 int INET_rresolve(char *name, size_t len, struct sockaddr_in *s_in,
101 int numeric, unsigned int netmask)
106 unsigned long ad, host_ad;
110 if (s_in->sin_family != AF_INET) {
112 bb_error_msg("rresolve: unsupport address family %d !",
115 errno = EAFNOSUPPORT;
118 ad = (unsigned long) s_in->sin_addr.s_addr;
120 bb_error_msg("rresolve: %08lx, mask %08x, num %08x", ad, netmask, numeric);
122 if (ad == INADDR_ANY) {
123 if ((numeric & 0x0FFF) == 0) {
124 if (numeric & 0x8000)
125 safe_strncpy(name, bb_INET_default, len);
127 safe_strncpy(name, "*", len);
131 if (numeric & 0x0FFF) {
132 safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
136 if ((ad & (~netmask)) != 0 || (numeric & 0x4000))
143 if (pn->addr.sin_addr.s_addr == ad && pn->host == host) {
144 safe_strncpy(name, pn->name, len);
146 bb_error_msg("rresolve: found %s %08lx in cache",
147 (host ? "host" : "net"), ad);
159 bb_error_msg("gethostbyaddr (%08lx)", ad);
161 ent = gethostbyaddr((char *) &ad, 4, AF_INET);
163 safe_strncpy(name, ent->h_name, len);
167 bb_error_msg("getnetbyaddr (%08lx)", host_ad);
169 np = getnetbyaddr(host_ad, AF_INET);
171 safe_strncpy(name, np->n_name, len);
174 if ((ent == NULL) && (np == NULL)) {
175 safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
177 pn = (struct addr *) xmalloc(sizeof(struct addr));
181 pn->name = bb_xstrdup(name);
187 #ifdef CONFIG_FEATURE_IPV6
189 int INET6_resolve(char *name, struct sockaddr_in6 *sin6)
191 struct addrinfo req, *ai;
194 memset(&req, '\0', sizeof req);
195 req.ai_family = AF_INET6;
196 if ((s = getaddrinfo(name, NULL, &req, &ai))) {
197 bb_error_msg("getaddrinfo: %s: %d", name, s);
200 memcpy(sin6, ai->ai_addr, sizeof(struct sockaddr_in6));
207 #ifndef IN6_IS_ADDR_UNSPECIFIED
208 # define IN6_IS_ADDR_UNSPECIFIED(a) \
209 (((__u32 *) (a))[0] == 0 && ((__u32 *) (a))[1] == 0 && \
210 ((__u32 *) (a))[2] == 0 && ((__u32 *) (a))[3] == 0)
214 int INET6_rresolve(char *name, size_t len, struct sockaddr_in6 *sin6,
220 if (sin6->sin6_family != AF_INET6) {
222 bb_error_msg(_("rresolve: unsupport address family %d !\n"),
225 errno = EAFNOSUPPORT;
228 if (numeric & 0x7FFF) {
229 inet_ntop(AF_INET6, &sin6->sin6_addr, name, len);
232 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
233 if (numeric & 0x8000) {
234 strcpy(name, "default");
241 s = getnameinfo((struct sockaddr *) sin6, sizeof(struct sockaddr_in6), name, len, NULL, 0, 0);
243 bb_error_msg("getnameinfo failed");
249 #endif /* CONFIG_FEATURE_IPV6 */