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