1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 #include "curl_setup.h"
25 #ifdef HAVE_NETINET_IN_H
26 # include <netinet/in.h>
28 #ifdef HAVE_ARPA_INET_H
29 # include <arpa/inet.h>
34 #ifdef HAVE_SYS_IOCTL_H
35 # include <sys/ioctl.h>
40 #ifdef HAVE_SYS_SOCKIO_H
41 # include <sys/sockio.h>
53 #include "inet_ntop.h"
56 #include "curl_printf.h"
58 #include "curl_memory.h"
59 /* The last #include file should be: */
62 /* ------------------------------------------------------------------ */
64 /* Return the scope of the given address. */
65 unsigned int Curl_ipv6_scope(const struct sockaddr *sa)
70 if(sa->sa_family == AF_INET6) {
71 const struct sockaddr_in6 * sa6 = (const struct sockaddr_in6 *)(void *) sa;
72 const unsigned char * b = sa6->sin6_addr.s6_addr;
73 unsigned short w = (unsigned short) ((b[0] << 8) | b[1]);
77 return IPV6_SCOPE_LINKLOCAL;
79 return IPV6_SCOPE_SITELOCAL;
81 w = b[1] | b[2] | b[3] | b[4] | b[5] | b[6] | b[7] | b[8] | b[9] |
82 b[10] | b[11] | b[12] | b[13] | b[14];
83 if(w || b[15] != 0x01)
85 return IPV6_SCOPE_NODELOCAL;
92 return IPV6_SCOPE_GLOBAL;
96 #if defined(HAVE_GETIFADDRS)
98 bool Curl_if_is_interface_name(const char *interf)
102 struct ifaddrs *iface, *head;
104 if(getifaddrs(&head) >= 0) {
105 for(iface=head; iface != NULL; iface=iface->ifa_next) {
106 if(curl_strequal(iface->ifa_name, interf)) {
116 if2ip_result_t Curl_if2ip(int af, unsigned int remote_scope,
117 unsigned int remote_scope_id, const char *interf,
118 char *buf, int buf_size)
120 struct ifaddrs *iface, *head;
121 if2ip_result_t res = IF2IP_NOT_FOUND;
126 #ifndef HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID
127 (void) remote_scope_id;
132 if(getifaddrs(&head) >= 0) {
133 for(iface = head; iface != NULL; iface=iface->ifa_next) {
134 if(iface->ifa_addr != NULL) {
135 if(iface->ifa_addr->sa_family == af) {
136 if(curl_strequal(iface->ifa_name, interf)) {
143 unsigned int scopeid = 0;
144 unsigned int ifscope = Curl_ipv6_scope(iface->ifa_addr);
146 if(ifscope != remote_scope) {
147 /* We are interested only in interface addresses whose
148 scope matches the remote address we want to
149 connect to: global for global, link-local for
150 link-local, etc... */
151 if(res == IF2IP_NOT_FOUND) res = IF2IP_AF_NOT_SUPPORTED;
156 &((struct sockaddr_in6 *)(void *)iface->ifa_addr)->sin6_addr;
157 #ifdef HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID
158 /* Include the scope of this interface as part of the address */
159 scopeid = ((struct sockaddr_in6 *)(void *)iface->ifa_addr)
162 /* If given, scope id should match. */
163 if(remote_scope_id && scopeid != remote_scope_id) {
164 if(res == IF2IP_NOT_FOUND)
165 res = IF2IP_AF_NOT_SUPPORTED;
171 snprintf(scope, sizeof(scope), "%%%u", scopeid);
176 &((struct sockaddr_in *)(void *)iface->ifa_addr)->sin_addr;
178 ip = (char *) Curl_inet_ntop(af, addr, ipstr, sizeof(ipstr));
179 snprintf(buf, buf_size, "%s%s", ip, scope);
183 else if((res == IF2IP_NOT_FOUND) &&
184 curl_strequal(iface->ifa_name, interf)) {
185 res = IF2IP_AF_NOT_SUPPORTED;
196 #elif defined(HAVE_IOCTL_SIOCGIFADDR)
198 bool Curl_if_is_interface_name(const char *interf)
200 /* This is here just to support the old interfaces */
203 return (Curl_if2ip(AF_INET, 0 /* unused */, 0, interf, buf, sizeof(buf)) ==
204 IF2IP_NOT_FOUND) ? FALSE : TRUE;
207 if2ip_result_t Curl_if2ip(int af, unsigned int remote_scope,
208 unsigned int remote_scope_id, const char *interf,
209 char *buf, int buf_size)
213 struct sockaddr_in *s;
218 (void)remote_scope_id;
220 if(!interf || (af != AF_INET))
221 return IF2IP_NOT_FOUND;
223 len = strlen(interf);
224 if(len >= sizeof(req.ifr_name))
225 return IF2IP_NOT_FOUND;
227 dummy = socket(AF_INET, SOCK_STREAM, 0);
228 if(CURL_SOCKET_BAD == dummy)
229 return IF2IP_NOT_FOUND;
231 memset(&req, 0, sizeof(req));
232 memcpy(req.ifr_name, interf, len+1);
233 req.ifr_addr.sa_family = AF_INET;
235 if(ioctl(dummy, SIOCGIFADDR, &req) < 0) {
237 /* With SIOCGIFADDR, we cannot tell the difference between an interface
238 that does not exist and an interface that has no address of the
239 correct family. Assume the interface does not exist */
240 return IF2IP_NOT_FOUND;
243 s = (struct sockaddr_in *)&req.ifr_addr;
244 memcpy(&in, &s->sin_addr, sizeof(in));
245 Curl_inet_ntop(s->sin_family, &in, buf, buf_size);
253 bool Curl_if_is_interface_name(const char *interf)
260 if2ip_result_t Curl_if2ip(int af, unsigned int remote_scope,
261 unsigned int remote_scope_id, const char *interf,
262 char *buf, int buf_size)
266 (void) remote_scope_id;
270 return IF2IP_NOT_FOUND;