Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * _ _ ____ _ |
| 3 | * Project ___| | | | _ \| | |
| 4 | * / __| | | | |_) | | |
| 5 | * | (__| |_| | _ <| |___ |
| 6 | * \___|\___/|_| \_\_____| |
| 7 | * |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 8 | * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al. |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 9 | * |
| 10 | * This software is licensed as described in the file COPYING, which |
| 11 | * you should have received as part of this distribution. The terms |
Alex Deymo | 8f1a214 | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 12 | * are also available at https://curl.haxx.se/docs/copyright.html. |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 13 | * |
| 14 | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
| 15 | * copies of the Software, and permit persons to whom the Software is |
| 16 | * furnished to do so, under the terms of the COPYING file. |
| 17 | * |
| 18 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 19 | * KIND, either express or implied. |
| 20 | * |
| 21 | ***************************************************************************/ |
| 22 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 23 | #include "curl_setup.h" |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 24 | |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 25 | #ifdef HAVE_NETINET_IN_H |
| 26 | # include <netinet/in.h> |
| 27 | #endif |
| 28 | #ifdef HAVE_ARPA_INET_H |
| 29 | # include <arpa/inet.h> |
| 30 | #endif |
| 31 | #ifdef HAVE_NET_IF_H |
| 32 | # include <net/if.h> |
| 33 | #endif |
| 34 | #ifdef HAVE_SYS_IOCTL_H |
| 35 | # include <sys/ioctl.h> |
| 36 | #endif |
| 37 | #ifdef HAVE_NETDB_H |
| 38 | # include <netdb.h> |
| 39 | #endif |
| 40 | #ifdef HAVE_SYS_SOCKIO_H |
| 41 | # include <sys/sockio.h> |
| 42 | #endif |
| 43 | #ifdef HAVE_IFADDRS_H |
| 44 | # include <ifaddrs.h> |
| 45 | #endif |
| 46 | #ifdef HAVE_STROPTS_H |
| 47 | # include <stropts.h> |
| 48 | #endif |
| 49 | #ifdef __VMS |
| 50 | # include <inet.h> |
| 51 | #endif |
| 52 | |
| 53 | #include "inet_ntop.h" |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 54 | #include "strcase.h" |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 55 | #include "if2ip.h" |
Alex Deymo | 8f1a214 | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 56 | /* The last 3 #include files should be in this order */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 57 | #include "curl_printf.h" |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 58 | #include "curl_memory.h" |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 59 | #include "memdebug.h" |
| 60 | |
| 61 | /* ------------------------------------------------------------------ */ |
| 62 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 63 | /* Return the scope of the given address. */ |
| 64 | unsigned int Curl_ipv6_scope(const struct sockaddr *sa) |
| 65 | { |
| 66 | #ifndef ENABLE_IPV6 |
| 67 | (void) sa; |
| 68 | #else |
| 69 | if(sa->sa_family == AF_INET6) { |
Alex Deymo | 8f1a214 | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 70 | const struct sockaddr_in6 * sa6 = (const struct sockaddr_in6 *)(void *) sa; |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 71 | const unsigned char *b = sa6->sin6_addr.s6_addr; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 72 | unsigned short w = (unsigned short) ((b[0] << 8) | b[1]); |
| 73 | |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 74 | if((b[0] & 0xFE) == 0xFC) /* Handle ULAs */ |
| 75 | return IPV6_SCOPE_UNIQUELOCAL; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 76 | switch(w & 0xFFC0) { |
| 77 | case 0xFE80: |
| 78 | return IPV6_SCOPE_LINKLOCAL; |
| 79 | case 0xFEC0: |
| 80 | return IPV6_SCOPE_SITELOCAL; |
| 81 | case 0x0000: |
| 82 | w = b[1] | b[2] | b[3] | b[4] | b[5] | b[6] | b[7] | b[8] | b[9] | |
| 83 | b[10] | b[11] | b[12] | b[13] | b[14]; |
| 84 | if(w || b[15] != 0x01) |
| 85 | break; |
| 86 | return IPV6_SCOPE_NODELOCAL; |
| 87 | default: |
| 88 | break; |
| 89 | } |
| 90 | } |
| 91 | #endif |
| 92 | |
| 93 | return IPV6_SCOPE_GLOBAL; |
| 94 | } |
| 95 | |
| 96 | |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 97 | #if defined(HAVE_GETIFADDRS) |
| 98 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 99 | bool Curl_if_is_interface_name(const char *interf) |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 100 | { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 101 | bool result = FALSE; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 102 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 103 | struct ifaddrs *iface, *head; |
| 104 | |
| 105 | if(getifaddrs(&head) >= 0) { |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 106 | for(iface = head; iface != NULL; iface = iface->ifa_next) { |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 107 | if(strcasecompare(iface->ifa_name, interf)) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 108 | result = TRUE; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 109 | break; |
| 110 | } |
| 111 | } |
| 112 | freeifaddrs(head); |
| 113 | } |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 114 | return result; |
| 115 | } |
| 116 | |
| 117 | if2ip_result_t Curl_if2ip(int af, unsigned int remote_scope, |
| 118 | unsigned int remote_scope_id, const char *interf, |
| 119 | char *buf, int buf_size) |
| 120 | { |
| 121 | struct ifaddrs *iface, *head; |
| 122 | if2ip_result_t res = IF2IP_NOT_FOUND; |
| 123 | |
| 124 | #ifndef ENABLE_IPV6 |
| 125 | (void) remote_scope; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 126 | #endif |
| 127 | |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 128 | #if !defined(HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID) || \ |
| 129 | !defined(ENABLE_IPV6) |
| 130 | (void) remote_scope_id; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 131 | #endif |
| 132 | |
| 133 | if(getifaddrs(&head) >= 0) { |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 134 | for(iface = head; iface != NULL; iface = iface->ifa_next) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 135 | if(iface->ifa_addr != NULL) { |
| 136 | if(iface->ifa_addr->sa_family == af) { |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 137 | if(strcasecompare(iface->ifa_name, interf)) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 138 | void *addr; |
| 139 | char *ip; |
| 140 | char scope[12] = ""; |
| 141 | char ipstr[64]; |
| 142 | #ifdef ENABLE_IPV6 |
| 143 | if(af == AF_INET6) { |
| 144 | unsigned int scopeid = 0; |
| 145 | unsigned int ifscope = Curl_ipv6_scope(iface->ifa_addr); |
| 146 | |
| 147 | if(ifscope != remote_scope) { |
| 148 | /* We are interested only in interface addresses whose |
| 149 | scope matches the remote address we want to |
| 150 | connect to: global for global, link-local for |
| 151 | link-local, etc... */ |
| 152 | if(res == IF2IP_NOT_FOUND) res = IF2IP_AF_NOT_SUPPORTED; |
| 153 | continue; |
| 154 | } |
| 155 | |
Alex Deymo | 8f1a214 | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 156 | addr = |
| 157 | &((struct sockaddr_in6 *)(void *)iface->ifa_addr)->sin6_addr; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 158 | #ifdef HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID |
| 159 | /* Include the scope of this interface as part of the address */ |
Alex Deymo | 8f1a214 | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 160 | scopeid = ((struct sockaddr_in6 *)(void *)iface->ifa_addr) |
| 161 | ->sin6_scope_id; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 162 | |
| 163 | /* If given, scope id should match. */ |
| 164 | if(remote_scope_id && scopeid != remote_scope_id) { |
| 165 | if(res == IF2IP_NOT_FOUND) |
| 166 | res = IF2IP_AF_NOT_SUPPORTED; |
| 167 | |
| 168 | continue; |
| 169 | } |
| 170 | #endif |
| 171 | if(scopeid) |
| 172 | snprintf(scope, sizeof(scope), "%%%u", scopeid); |
| 173 | } |
| 174 | else |
| 175 | #endif |
Alex Deymo | 8f1a214 | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 176 | addr = |
| 177 | &((struct sockaddr_in *)(void *)iface->ifa_addr)->sin_addr; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 178 | res = IF2IP_FOUND; |
| 179 | ip = (char *) Curl_inet_ntop(af, addr, ipstr, sizeof(ipstr)); |
| 180 | snprintf(buf, buf_size, "%s%s", ip, scope); |
| 181 | break; |
| 182 | } |
| 183 | } |
| 184 | else if((res == IF2IP_NOT_FOUND) && |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 185 | strcasecompare(iface->ifa_name, interf)) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 186 | res = IF2IP_AF_NOT_SUPPORTED; |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | freeifaddrs(head); |
| 192 | } |
| 193 | |
| 194 | return res; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | #elif defined(HAVE_IOCTL_SIOCGIFADDR) |
| 198 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 199 | bool Curl_if_is_interface_name(const char *interf) |
| 200 | { |
| 201 | /* This is here just to support the old interfaces */ |
| 202 | char buf[256]; |
| 203 | |
| 204 | return (Curl_if2ip(AF_INET, 0 /* unused */, 0, interf, buf, sizeof(buf)) == |
| 205 | IF2IP_NOT_FOUND) ? FALSE : TRUE; |
| 206 | } |
| 207 | |
| 208 | if2ip_result_t Curl_if2ip(int af, unsigned int remote_scope, |
| 209 | unsigned int remote_scope_id, const char *interf, |
| 210 | char *buf, int buf_size) |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 211 | { |
| 212 | struct ifreq req; |
| 213 | struct in_addr in; |
| 214 | struct sockaddr_in *s; |
| 215 | curl_socket_t dummy; |
| 216 | size_t len; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 217 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 218 | (void)remote_scope; |
| 219 | (void)remote_scope_id; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 220 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 221 | if(!interf || (af != AF_INET)) |
| 222 | return IF2IP_NOT_FOUND; |
| 223 | |
| 224 | len = strlen(interf); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 225 | if(len >= sizeof(req.ifr_name)) |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 226 | return IF2IP_NOT_FOUND; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 227 | |
| 228 | dummy = socket(AF_INET, SOCK_STREAM, 0); |
| 229 | if(CURL_SOCKET_BAD == dummy) |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 230 | return IF2IP_NOT_FOUND; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 231 | |
| 232 | memset(&req, 0, sizeof(req)); |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 233 | memcpy(req.ifr_name, interf, len + 1); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 234 | req.ifr_addr.sa_family = AF_INET; |
| 235 | |
| 236 | if(ioctl(dummy, SIOCGIFADDR, &req) < 0) { |
| 237 | sclose(dummy); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 238 | /* With SIOCGIFADDR, we cannot tell the difference between an interface |
| 239 | that does not exist and an interface that has no address of the |
| 240 | correct family. Assume the interface does not exist */ |
| 241 | return IF2IP_NOT_FOUND; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 242 | } |
| 243 | |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 244 | s = (struct sockaddr_in *)(void *)&req.ifr_addr; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 245 | memcpy(&in, &s->sin_addr, sizeof(in)); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 246 | Curl_inet_ntop(s->sin_family, &in, buf, buf_size); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 247 | |
| 248 | sclose(dummy); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 249 | return IF2IP_FOUND; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | #else |
| 253 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 254 | bool Curl_if_is_interface_name(const char *interf) |
| 255 | { |
| 256 | (void) interf; |
| 257 | |
| 258 | return FALSE; |
| 259 | } |
| 260 | |
| 261 | if2ip_result_t Curl_if2ip(int af, unsigned int remote_scope, |
| 262 | unsigned int remote_scope_id, const char *interf, |
| 263 | char *buf, int buf_size) |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 264 | { |
| 265 | (void) af; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 266 | (void) remote_scope; |
| 267 | (void) remote_scope_id; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 268 | (void) interf; |
| 269 | (void) buf; |
| 270 | (void) buf_size; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 271 | return IF2IP_NOT_FOUND; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | #endif |