1 /*************************************************************************** 2 * _ _ ____ _ 3 * Project ___| | | | _ \| | 4 * / __| | | | |_) | | 5 * | (__| |_| | _ <| |___ 6 * \___|\___/|_| \_\_____| 7 * 8 * Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel (at) haxx.se>, et al. 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 12 * are also available at https://curl.haxx.se/docs/copyright.html. 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 23 #include "curl_setup.h" 24 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" 54 #include "strcase.h" 55 #include "if2ip.h" 56 /* The last 3 #include files should be in this order */ 57 #include "curl_printf.h" 58 #include "curl_memory.h" 59 #include "memdebug.h" 60 61 /* ------------------------------------------------------------------ */ 62 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) { 70 const struct sockaddr_in6 * sa6 = (const struct sockaddr_in6 *)(void *) sa; 71 const unsigned char *b = sa6->sin6_addr.s6_addr; 72 unsigned short w = (unsigned short) ((b[0] << 8) | b[1]); 73 74 if((b[0] & 0xFE) == 0xFC) /* Handle ULAs */ 75 return IPV6_SCOPE_UNIQUELOCAL; 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 97 #if defined(HAVE_GETIFADDRS) 98 99 if2ip_result_t Curl_if2ip(int af, unsigned int remote_scope, 100 unsigned int remote_scope_id, const char *interf, 101 char *buf, int buf_size) 102 { 103 struct ifaddrs *iface, *head; 104 if2ip_result_t res = IF2IP_NOT_FOUND; 105 106 #ifndef ENABLE_IPV6 107 (void) remote_scope; 108 #endif 109 110 #if !defined(HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID) || \ 111 !defined(ENABLE_IPV6) 112 (void) remote_scope_id; 113 #endif 114 115 if(getifaddrs(&head) >= 0) { 116 for(iface = head; iface != NULL; iface = iface->ifa_next) { 117 if(iface->ifa_addr != NULL) { 118 if(iface->ifa_addr->sa_family == af) { 119 if(strcasecompare(iface->ifa_name, interf)) { 120 void *addr; 121 char *ip; 122 char scope[12] = ""; 123 char ipstr[64]; 124 #ifdef ENABLE_IPV6 125 if(af == AF_INET6) { 126 unsigned int scopeid = 0; 127 unsigned int ifscope = Curl_ipv6_scope(iface->ifa_addr); 128 129 if(ifscope != remote_scope) { 130 /* We are interested only in interface addresses whose 131 scope matches the remote address we want to 132 connect to: global for global, link-local for 133 link-local, etc... */ 134 if(res == IF2IP_NOT_FOUND) res = IF2IP_AF_NOT_SUPPORTED; 135 continue; 136 } 137 138 addr = 139 &((struct sockaddr_in6 *)(void *)iface->ifa_addr)->sin6_addr; 140 #ifdef HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID 141 /* Include the scope of this interface as part of the address */ 142 scopeid = ((struct sockaddr_in6 *)(void *)iface->ifa_addr) 143 ->sin6_scope_id; 144 145 /* If given, scope id should match. */ 146 if(remote_scope_id && scopeid != remote_scope_id) { 147 if(res == IF2IP_NOT_FOUND) 148 res = IF2IP_AF_NOT_SUPPORTED; 149 150 continue; 151 } 152 #endif 153 if(scopeid) 154 msnprintf(scope, sizeof(scope), "%%%u", scopeid); 155 } 156 else 157 #endif 158 addr = 159 &((struct sockaddr_in *)(void *)iface->ifa_addr)->sin_addr; 160 res = IF2IP_FOUND; 161 ip = (char *) Curl_inet_ntop(af, addr, ipstr, sizeof(ipstr)); 162 msnprintf(buf, buf_size, "%s%s", ip, scope); 163 break; 164 } 165 } 166 else if((res == IF2IP_NOT_FOUND) && 167 strcasecompare(iface->ifa_name, interf)) { 168 res = IF2IP_AF_NOT_SUPPORTED; 169 } 170 } 171 } 172 173 freeifaddrs(head); 174 } 175 176 return res; 177 } 178 179 #elif defined(HAVE_IOCTL_SIOCGIFADDR) 180 181 if2ip_result_t Curl_if2ip(int af, unsigned int remote_scope, 182 unsigned int remote_scope_id, const char *interf, 183 char *buf, int buf_size) 184 { 185 struct ifreq req; 186 struct in_addr in; 187 struct sockaddr_in *s; 188 curl_socket_t dummy; 189 size_t len; 190 191 (void)remote_scope; 192 (void)remote_scope_id; 193 194 if(!interf || (af != AF_INET)) 195 return IF2IP_NOT_FOUND; 196 197 len = strlen(interf); 198 if(len >= sizeof(req.ifr_name)) 199 return IF2IP_NOT_FOUND; 200 201 dummy = socket(AF_INET, SOCK_STREAM, 0); 202 if(CURL_SOCKET_BAD == dummy) 203 return IF2IP_NOT_FOUND; 204 205 memset(&req, 0, sizeof(req)); 206 memcpy(req.ifr_name, interf, len + 1); 207 req.ifr_addr.sa_family = AF_INET; 208 209 if(ioctl(dummy, SIOCGIFADDR, &req) < 0) { 210 sclose(dummy); 211 /* With SIOCGIFADDR, we cannot tell the difference between an interface 212 that does not exist and an interface that has no address of the 213 correct family. Assume the interface does not exist */ 214 return IF2IP_NOT_FOUND; 215 } 216 217 s = (struct sockaddr_in *)(void *)&req.ifr_addr; 218 memcpy(&in, &s->sin_addr, sizeof(in)); 219 Curl_inet_ntop(s->sin_family, &in, buf, buf_size); 220 221 sclose(dummy); 222 return IF2IP_FOUND; 223 } 224 225 #else 226 227 if2ip_result_t Curl_if2ip(int af, unsigned int remote_scope, 228 unsigned int remote_scope_id, const char *interf, 229 char *buf, int buf_size) 230 { 231 (void) af; 232 (void) remote_scope; 233 (void) remote_scope_id; 234 (void) interf; 235 (void) buf; 236 (void) buf_size; 237 return IF2IP_NOT_FOUND; 238 } 239 240 #endif 241