"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | c223ced | 2001-11-10 12:18:42 +0000 | [diff] [blame] | 2 | /* |
| 3 | * stolen from net-tools-1.59 and stripped down for busybox by |
Eric Andersen | cb81e64 | 2003-07-14 21:21:08 +0000 | [diff] [blame] | 4 | * Erik Andersen <andersen@codepoet.org> |
Eric Andersen | c223ced | 2001-11-10 12:18:42 +0000 | [diff] [blame] | 5 | * |
| 6 | * Heavily modified by Manuel Novoa III Mar 12, 2001 |
| 7 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 8 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Eric Andersen | c223ced | 2001-11-10 12:18:42 +0000 | [diff] [blame] | 9 | */ |
Rob Landley | ea224be | 2006-06-18 20:20:07 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
Eric Andersen | c223ced | 2001-11-10 12:18:42 +0000 | [diff] [blame] | 11 | #include "inet_common.h" |
Eric Andersen | 77d22c4 | 2002-06-06 12:11:55 +0000 | [diff] [blame] | 12 | |
Denys Vlasenko | 7d7c7bb | 2017-07-14 20:17:17 +0200 | [diff] [blame] | 13 | #if 0 |
| 14 | # define dbg(...) bb_error_msg(__VA_ARGS__) |
| 15 | #else |
| 16 | # define dbg(...) ((void)0) |
| 17 | #endif |
| 18 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 19 | int FAST_FUNC INET_resolve(const char *name, struct sockaddr_in *s_in, int hostfirst) |
Eric Andersen | c223ced | 2001-11-10 12:18:42 +0000 | [diff] [blame] | 20 | { |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 21 | struct hostent *hp; |
Denis Vlasenko | a61cb92 | 2007-06-19 11:24:47 +0000 | [diff] [blame] | 22 | #if ENABLE_FEATURE_ETC_NETWORKS |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 23 | struct netent *np; |
Denis Vlasenko | a61cb92 | 2007-06-19 11:24:47 +0000 | [diff] [blame] | 24 | #endif |
Eric Andersen | c223ced | 2001-11-10 12:18:42 +0000 | [diff] [blame] | 25 | |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 26 | /* Grmpf. -FvK */ |
| 27 | s_in->sin_family = AF_INET; |
| 28 | s_in->sin_port = 0; |
Eric Andersen | c223ced | 2001-11-10 12:18:42 +0000 | [diff] [blame] | 29 | |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 30 | /* Default is special, meaning 0.0.0.0. */ |
Denys Vlasenko | 0004e99 | 2010-09-01 12:01:17 +0200 | [diff] [blame] | 31 | if (strcmp(name, "default") == 0) { |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 32 | s_in->sin_addr.s_addr = INADDR_ANY; |
Denis Vlasenko | 7ca3921 | 2006-11-21 20:34:21 +0000 | [diff] [blame] | 33 | return 1; |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 34 | } |
| 35 | /* Look to see if it's a dotted quad. */ |
| 36 | if (inet_aton(name, &s_in->sin_addr)) { |
| 37 | return 0; |
| 38 | } |
| 39 | /* If we expect this to be a hostname, try hostname database first */ |
Denys Vlasenko | 8e74ada | 2015-02-03 12:07:40 +0100 | [diff] [blame] | 40 | if (hostfirst) { |
Denys Vlasenko | 7d7c7bb | 2017-07-14 20:17:17 +0200 | [diff] [blame] | 41 | dbg("gethostbyname(%s)", name); |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 42 | hp = gethostbyname(name); |
Denys Vlasenko | 8e74ada | 2015-02-03 12:07:40 +0100 | [diff] [blame] | 43 | if (hp) { |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 44 | memcpy(&s_in->sin_addr, hp->h_addr_list[0], |
| 45 | sizeof(struct in_addr)); |
| 46 | return 0; |
| 47 | } |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 48 | } |
Denis Vlasenko | a61cb92 | 2007-06-19 11:24:47 +0000 | [diff] [blame] | 49 | #if ENABLE_FEATURE_ETC_NETWORKS |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 50 | /* Try the NETWORKS database to see if this is a known network. */ |
Denys Vlasenko | 7d7c7bb | 2017-07-14 20:17:17 +0200 | [diff] [blame] | 51 | dbg("getnetbyname(%s)", name); |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 52 | np = getnetbyname(name); |
Denys Vlasenko | 8e74ada | 2015-02-03 12:07:40 +0100 | [diff] [blame] | 53 | if (np) { |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 54 | s_in->sin_addr.s_addr = htonl(np->n_net); |
| 55 | return 1; |
| 56 | } |
Denis Vlasenko | a61cb92 | 2007-06-19 11:24:47 +0000 | [diff] [blame] | 57 | #endif |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 58 | if (hostfirst) { |
| 59 | /* Don't try again */ |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 60 | return -1; |
| 61 | } |
| 62 | #ifdef DEBUG |
| 63 | res_init(); |
| 64 | _res.options |= RES_DEBUG; |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 65 | #endif |
Denys Vlasenko | 7d7c7bb | 2017-07-14 20:17:17 +0200 | [diff] [blame] | 66 | dbg("gethostbyname(%s)", name); |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 67 | hp = gethostbyname(name); |
Denys Vlasenko | 8e74ada | 2015-02-03 12:07:40 +0100 | [diff] [blame] | 68 | if (!hp) { |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 69 | return -1; |
| 70 | } |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 71 | memcpy(&s_in->sin_addr, hp->h_addr_list[0], sizeof(struct in_addr)); |
Eric Andersen | c223ced | 2001-11-10 12:18:42 +0000 | [diff] [blame] | 72 | return 0; |
Eric Andersen | c223ced | 2001-11-10 12:18:42 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Eric Andersen | c223ced | 2001-11-10 12:18:42 +0000 | [diff] [blame] | 75 | |
Denys Vlasenko | 8e74ada | 2015-02-03 12:07:40 +0100 | [diff] [blame] | 76 | /* numeric: & 0x8000: "default" instead of "*", |
Eric Andersen | c223ced | 2001-11-10 12:18:42 +0000 | [diff] [blame] | 77 | * & 0x4000: host instead of net, |
| 78 | * & 0x0fff: don't resolve |
| 79 | */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 80 | char* FAST_FUNC INET_rresolve(struct sockaddr_in *s_in, int numeric, uint32_t netmask) |
Eric Andersen | c223ced | 2001-11-10 12:18:42 +0000 | [diff] [blame] | 81 | { |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame] | 82 | /* addr-to-name cache */ |
| 83 | struct addr { |
| 84 | struct addr *next; |
Denys Vlasenko | 8e74ada | 2015-02-03 12:07:40 +0100 | [diff] [blame] | 85 | uint32_t nip; |
| 86 | smallint is_host; |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame] | 87 | char name[1]; |
| 88 | }; |
| 89 | static struct addr *cache = NULL; |
| 90 | |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 91 | struct addr *pn; |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame] | 92 | char *name; |
Denys Vlasenko | 8e74ada | 2015-02-03 12:07:40 +0100 | [diff] [blame] | 93 | uint32_t nip; |
| 94 | smallint is_host; |
Eric Andersen | c223ced | 2001-11-10 12:18:42 +0000 | [diff] [blame] | 95 | |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 96 | if (s_in->sin_family != AF_INET) { |
Denys Vlasenko | 7d7c7bb | 2017-07-14 20:17:17 +0200 | [diff] [blame] | 97 | dbg("rresolve: unsupported address family %d!", s_in->sin_family); |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 98 | errno = EAFNOSUPPORT; |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame] | 99 | return NULL; |
Eric Andersen | c223ced | 2001-11-10 12:18:42 +0000 | [diff] [blame] | 100 | } |
Denys Vlasenko | 8e74ada | 2015-02-03 12:07:40 +0100 | [diff] [blame] | 101 | nip = s_in->sin_addr.s_addr; |
Denys Vlasenko | 7d7c7bb | 2017-07-14 20:17:17 +0200 | [diff] [blame] | 102 | dbg("rresolve: %08x mask:%08x num:%08x", (unsigned)nip, netmask, numeric); |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame] | 103 | if (numeric & 0x0FFF) |
Denys Vlasenko | 8e74ada | 2015-02-03 12:07:40 +0100 | [diff] [blame] | 104 | return xmalloc_sockaddr2dotted_noport((void*)s_in); |
| 105 | if (nip == INADDR_ANY) { |
| 106 | if (numeric & 0x8000) |
| 107 | return xstrdup("default"); |
| 108 | return xstrdup("*"); |
| 109 | } |
Eric Andersen | c223ced | 2001-11-10 12:18:42 +0000 | [diff] [blame] | 110 | |
Denys Vlasenko | 8e74ada | 2015-02-03 12:07:40 +0100 | [diff] [blame] | 111 | is_host = ((nip & (~netmask)) != 0 || (numeric & 0x4000)); |
| 112 | |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame] | 113 | pn = cache; |
| 114 | while (pn) { |
Denys Vlasenko | 8e74ada | 2015-02-03 12:07:40 +0100 | [diff] [blame] | 115 | if (pn->nip == nip && pn->is_host == is_host) { |
Denys Vlasenko | 7d7c7bb | 2017-07-14 20:17:17 +0200 | [diff] [blame] | 116 | dbg("rresolve: found %s %08x in cache", |
Denys Vlasenko | 8e74ada | 2015-02-03 12:07:40 +0100 | [diff] [blame] | 117 | (is_host ? "host" : "net"), (unsigned)nip); |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame] | 118 | return xstrdup(pn->name); |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 119 | } |
| 120 | pn = pn->next; |
Eric Andersen | c223ced | 2001-11-10 12:18:42 +0000 | [diff] [blame] | 121 | } |
Eric Andersen | c223ced | 2001-11-10 12:18:42 +0000 | [diff] [blame] | 122 | |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame] | 123 | name = NULL; |
Denys Vlasenko | 8e74ada | 2015-02-03 12:07:40 +0100 | [diff] [blame] | 124 | if (is_host) { |
Denys Vlasenko | 7d7c7bb | 2017-07-14 20:17:17 +0200 | [diff] [blame] | 125 | dbg("sockaddr2host_noport(%08x)", (unsigned)nip); |
Denys Vlasenko | 8e74ada | 2015-02-03 12:07:40 +0100 | [diff] [blame] | 126 | name = xmalloc_sockaddr2host_noport((void*)s_in); |
Denys Vlasenko | 7d7c7bb | 2017-07-14 20:17:17 +0200 | [diff] [blame] | 127 | } |
| 128 | #if ENABLE_FEATURE_ETC_NETWORKS |
| 129 | else { |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame] | 130 | struct netent *np; |
Denys Vlasenko | 7d7c7bb | 2017-07-14 20:17:17 +0200 | [diff] [blame] | 131 | dbg("getnetbyaddr(%08x)", (unsigned)ntohl(nip)); |
Denys Vlasenko | 8e74ada | 2015-02-03 12:07:40 +0100 | [diff] [blame] | 132 | np = getnetbyaddr(ntohl(nip), AF_INET); |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame] | 133 | if (np) |
| 134 | name = xstrdup(np->n_name); |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 135 | } |
Denys Vlasenko | 7d7c7bb | 2017-07-14 20:17:17 +0200 | [diff] [blame] | 136 | #endif |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame] | 137 | if (!name) |
Denys Vlasenko | 8e74ada | 2015-02-03 12:07:40 +0100 | [diff] [blame] | 138 | name = xmalloc_sockaddr2dotted_noport((void*)s_in); |
| 139 | |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame] | 140 | pn = xmalloc(sizeof(*pn) + strlen(name)); /* no '+ 1', it's already accounted for */ |
| 141 | pn->next = cache; |
Denys Vlasenko | 8e74ada | 2015-02-03 12:07:40 +0100 | [diff] [blame] | 142 | pn->nip = nip; |
| 143 | pn->is_host = is_host; |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame] | 144 | strcpy(pn->name, name); |
| 145 | cache = pn; |
Denys Vlasenko | 8e74ada | 2015-02-03 12:07:40 +0100 | [diff] [blame] | 146 | |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame] | 147 | return name; |
Eric Andersen | c223ced | 2001-11-10 12:18:42 +0000 | [diff] [blame] | 148 | } |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 149 | |
Denis Vlasenko | e324184 | 2007-08-13 10:36:25 +0000 | [diff] [blame] | 150 | #if ENABLE_FEATURE_IPV6 |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 151 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 152 | int FAST_FUNC INET6_resolve(const char *name, struct sockaddr_in6 *sin6) |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 153 | { |
Vitaly Magerya | 7f4b769 | 2011-03-22 20:14:26 +0100 | [diff] [blame] | 154 | struct addrinfo req, *ai = NULL; |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 155 | int s; |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 156 | |
Vitaly Magerya | 7f4b769 | 2011-03-22 20:14:26 +0100 | [diff] [blame] | 157 | memset(&req, 0, sizeof(req)); |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 158 | req.ai_family = AF_INET6; |
Denis Vlasenko | 7ca3921 | 2006-11-21 20:34:21 +0000 | [diff] [blame] | 159 | s = getaddrinfo(name, NULL, &req, &ai); |
Vitaly Magerya | 7f4b769 | 2011-03-22 20:14:26 +0100 | [diff] [blame] | 160 | if (s != 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 161 | bb_error_msg("getaddrinfo: %s: %d", name, s); |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 162 | return -1; |
| 163 | } |
Vitaly Magerya | 7f4b769 | 2011-03-22 20:14:26 +0100 | [diff] [blame] | 164 | memcpy(sin6, ai->ai_addr, sizeof(*sin6)); |
Denys Vlasenko | 1a7256a | 2013-11-29 16:39:28 +0100 | [diff] [blame] | 165 | freeaddrinfo(ai); |
Denis Vlasenko | 7ca3921 | 2006-11-21 20:34:21 +0000 | [diff] [blame] | 166 | return 0; |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | #ifndef IN6_IS_ADDR_UNSPECIFIED |
Glenn L McGrath | d7fb1b3 | 2002-11-26 02:40:56 +0000 | [diff] [blame] | 170 | # define IN6_IS_ADDR_UNSPECIFIED(a) \ |
Bernhard Reutner-Fischer | 38583fd | 2006-05-19 12:44:16 +0000 | [diff] [blame] | 171 | (((uint32_t *) (a))[0] == 0 && ((uint32_t *) (a))[1] == 0 && \ |
| 172 | ((uint32_t *) (a))[2] == 0 && ((uint32_t *) (a))[3] == 0) |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 173 | #endif |
| 174 | |
| 175 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 176 | char* FAST_FUNC INET6_rresolve(struct sockaddr_in6 *sin6, int numeric) |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 177 | { |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 178 | if (sin6->sin6_family != AF_INET6) { |
Denys Vlasenko | 7d7c7bb | 2017-07-14 20:17:17 +0200 | [diff] [blame] | 179 | dbg("rresolve: unsupported address family %d!", |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 180 | sin6->sin6_family); |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 181 | errno = EAFNOSUPPORT; |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame] | 182 | return NULL; |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 183 | } |
| 184 | if (numeric & 0x7FFF) { |
Denys Vlasenko | 8e74ada | 2015-02-03 12:07:40 +0100 | [diff] [blame] | 185 | return xmalloc_sockaddr2dotted_noport((void*)sin6); |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 186 | } |
| 187 | if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame] | 188 | if (numeric & 0x8000) |
Denys Vlasenko | 0004e99 | 2010-09-01 12:01:17 +0200 | [diff] [blame] | 189 | return xstrdup("default"); |
Denis Vlasenko | 6d9ea24 | 2007-06-19 11:12:46 +0000 | [diff] [blame] | 190 | return xstrdup("*"); |
Glenn L McGrath | 30db423 | 2002-11-28 09:52:23 +0000 | [diff] [blame] | 191 | } |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 192 | |
Denys Vlasenko | 8e74ada | 2015-02-03 12:07:40 +0100 | [diff] [blame] | 193 | return xmalloc_sockaddr2host_noport((void*)sin6); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 196 | #endif /* CONFIG_FEATURE_IPV6 */ |