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"
25 const char bb_INET_default[] = "default";
27 int INET_resolve(const char *name, struct sockaddr_in *s_in, int hostfirst)
33 s_in->sin_family = AF_INET;
36 /* Default is special, meaning 0.0.0.0. */
37 if (!strcmp(name, bb_INET_default)) {
38 s_in->sin_addr.s_addr = INADDR_ANY;
41 /* Look to see if it's a dotted quad. */
42 if (inet_aton(name, &s_in->sin_addr)) {
45 /* If we expect this to be a hostname, try hostname database first */
48 bb_error_msg("gethostbyname (%s)", name);
51 if (hostfirst && (hp = gethostbyname(name)) != (struct hostent *) NULL) {
52 memcpy((char *) &s_in->sin_addr, (char *) hp->h_addr_list[0],
53 sizeof(struct in_addr));
56 /* Try the NETWORKS database to see if this is a known network. */
58 bb_error_msg("getnetbyname (%s)", name);
60 if ((np = getnetbyname(name)) != (struct netent *) NULL) {
61 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) {
79 memcpy((char *) &s_in->sin_addr, (char *) hp->h_addr_list[0],
80 sizeof(struct in_addr));
87 struct sockaddr_in addr;
93 static struct addr *INET_nn = NULL; /* addr-to-name cache */
95 /* numeric: & 0x8000: default instead of *,
96 * & 0x4000: host instead of net,
97 * & 0x0fff: don't resolve
99 int INET_rresolve(char *name, size_t len, struct sockaddr_in *s_in,
100 int numeric, unsigned int netmask)
105 unsigned long ad, host_ad;
109 if (s_in->sin_family != AF_INET) {
111 bb_error_msg("rresolve: unsupport address family %d !",
114 errno = EAFNOSUPPORT;
117 ad = (unsigned long) s_in->sin_addr.s_addr;
119 bb_error_msg("rresolve: %08lx, mask %08x, num %08x", ad, netmask, numeric);
121 if (ad == INADDR_ANY) {
122 if ((numeric & 0x0FFF) == 0) {
123 if (numeric & 0x8000)
124 safe_strncpy(name, bb_INET_default, len);
126 safe_strncpy(name, "*", len);
130 if (numeric & 0x0FFF) {
131 safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
135 if ((ad & (~netmask)) != 0 || (numeric & 0x4000))
139 if (pn->addr.sin_addr.s_addr == ad && pn->host == host) {
140 safe_strncpy(name, pn->name, len);
142 bb_error_msg("rresolve: found %s %08lx in cache",
143 (host ? "host" : "net"), ad);
155 bb_error_msg("gethostbyaddr (%08lx)", ad);
157 ent = gethostbyaddr((char *) &ad, 4, AF_INET);
159 safe_strncpy(name, ent->h_name, len);
163 bb_error_msg("getnetbyaddr (%08lx)", host_ad);
165 np = getnetbyaddr(host_ad, AF_INET);
167 safe_strncpy(name, np->n_name, len);
170 if ((ent == NULL) && (np == NULL)) {
171 safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
173 pn = (struct addr *) xmalloc(sizeof(struct addr));
177 pn->name = bb_xstrdup(name);
183 #ifdef CONFIG_FEATURE_IPV6
185 int INET6_resolve(const char *name, struct sockaddr_in6 *sin6)
187 struct addrinfo req, *ai;
190 memset(&req, '\0', sizeof req);
191 req.ai_family = AF_INET6;
192 if ((s = getaddrinfo(name, NULL, &req, &ai))) {
193 bb_error_msg("getaddrinfo: %s: %d", name, s);
196 memcpy(sin6, ai->ai_addr, sizeof(struct sockaddr_in6));
203 #ifndef IN6_IS_ADDR_UNSPECIFIED
204 # define IN6_IS_ADDR_UNSPECIFIED(a) \
205 (((uint32_t *) (a))[0] == 0 && ((uint32_t *) (a))[1] == 0 && \
206 ((uint32_t *) (a))[2] == 0 && ((uint32_t *) (a))[3] == 0)
210 int INET6_rresolve(char *name, size_t len, struct sockaddr_in6 *sin6,
216 if (sin6->sin6_family != AF_INET6) {
218 bb_error_msg(_("rresolve: unsupport address family %d !\n"),
221 errno = EAFNOSUPPORT;
224 if (numeric & 0x7FFF) {
225 inet_ntop(AF_INET6, &sin6->sin6_addr, name, len);
228 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
229 if (numeric & 0x8000) {
230 strcpy(name, "default");
237 s = getnameinfo((struct sockaddr *) sin6, sizeof(struct sockaddr_in6), name, len, NULL, 0, 0);
239 bb_error_msg("getnameinfo failed");
245 #endif /* CONFIG_FEATURE_IPV6 */