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.8 2004/03/10 07:42:38 mjn3 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);
69 _res.options |= RES_DEBUG;
73 bb_error_msg("gethostbyname (%s)", name);
75 if ((hp = gethostbyname(name)) == (struct hostent *) NULL) {
78 memcpy((char *) &s_in->sin_addr, (char *) hp->h_addr_list[0],
79 sizeof(struct in_addr));
86 struct sockaddr_in addr;
92 static struct addr *INET_nn = NULL; /* addr-to-name cache */
94 /* numeric: & 0x8000: default instead of *,
95 * & 0x4000: host instead of net,
96 * & 0x0fff: don't resolve
98 int INET_rresolve(char *name, size_t len, struct sockaddr_in *s_in,
99 int numeric, unsigned int netmask)
104 unsigned long ad, host_ad;
108 if (s_in->sin_family != AF_INET) {
110 bb_error_msg("rresolve: unsupport address family %d !",
113 errno = EAFNOSUPPORT;
116 ad = (unsigned long) s_in->sin_addr.s_addr;
118 bb_error_msg("rresolve: %08lx, mask %08x, num %08x", ad, netmask, numeric);
120 if (ad == INADDR_ANY) {
121 if ((numeric & 0x0FFF) == 0) {
122 if (numeric & 0x8000)
123 safe_strncpy(name, bb_INET_default, len);
125 safe_strncpy(name, "*", len);
129 if (numeric & 0x0FFF) {
130 safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
134 if ((ad & (~netmask)) != 0 || (numeric & 0x4000))
141 if (pn->addr.sin_addr.s_addr == ad && pn->host == host) {
142 safe_strncpy(name, pn->name, len);
144 bb_error_msg("rresolve: found %s %08lx in cache",
145 (host ? "host" : "net"), ad);
157 bb_error_msg("gethostbyaddr (%08lx)", ad);
159 ent = gethostbyaddr((char *) &ad, 4, AF_INET);
161 safe_strncpy(name, ent->h_name, len);
165 bb_error_msg("getnetbyaddr (%08lx)", host_ad);
167 np = getnetbyaddr(host_ad, AF_INET);
169 safe_strncpy(name, np->n_name, len);
172 if ((ent == NULL) && (np == NULL)) {
173 safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
175 pn = (struct addr *) xmalloc(sizeof(struct addr));
179 pn->name = bb_xstrdup(name);
185 #ifdef CONFIG_FEATURE_IPV6
187 int INET6_resolve(const char *name, struct sockaddr_in6 *sin6)
189 struct addrinfo req, *ai;
192 memset(&req, '\0', sizeof req);
193 req.ai_family = AF_INET6;
194 if ((s = getaddrinfo(name, NULL, &req, &ai))) {
195 bb_error_msg("getaddrinfo: %s: %d", name, s);
198 memcpy(sin6, ai->ai_addr, sizeof(struct sockaddr_in6));
205 #ifndef IN6_IS_ADDR_UNSPECIFIED
206 # define IN6_IS_ADDR_UNSPECIFIED(a) \
207 (((uint32_t *) (a))[0] == 0 && ((uint32_t *) (a))[1] == 0 && \
208 ((uint32_t *) (a))[2] == 0 && ((uint32_t *) (a))[3] == 0)
212 int INET6_rresolve(char *name, size_t len, struct sockaddr_in6 *sin6,
218 if (sin6->sin6_family != AF_INET6) {
220 bb_error_msg(_("rresolve: unsupport address family %d !\n"),
223 errno = EAFNOSUPPORT;
226 if (numeric & 0x7FFF) {
227 inet_ntop(AF_INET6, &sin6->sin6_addr, name, len);
230 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
231 if (numeric & 0x8000) {
232 strcpy(name, "default");
239 s = getnameinfo((struct sockaddr *) sin6, sizeof(struct sockaddr_in6), name, len, NULL, 0, 0);
241 bb_error_msg("getnameinfo failed");
247 #endif /* CONFIG_FEATURE_IPV6 */