1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@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 http://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 "strequal.h"
55 #include "if2ip.h"
56 #include "curl_printf.h"
57
58 #include "curl_memory.h"
59 /* The last #include file should be: */
60 #include "memdebug.h"
61
62 /* ------------------------------------------------------------------ */
63
64 /* Return the scope of the given address. */
Curl_ipv6_scope(const struct sockaddr * sa)65 unsigned int Curl_ipv6_scope(const struct sockaddr *sa)
66 {
67 #ifndef ENABLE_IPV6
68 (void) sa;
69 #else
70 if(sa->sa_family == AF_INET6) {
71 const struct sockaddr_in6 * sa6 = (const struct sockaddr_in6 *) sa;
72 const unsigned char * b = sa6->sin6_addr.s6_addr;
73 unsigned short w = (unsigned short) ((b[0] << 8) | b[1]);
74
75 switch(w & 0xFFC0) {
76 case 0xFE80:
77 return IPV6_SCOPE_LINKLOCAL;
78 case 0xFEC0:
79 return IPV6_SCOPE_SITELOCAL;
80 case 0x0000:
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)
84 break;
85 return IPV6_SCOPE_NODELOCAL;
86 default:
87 break;
88 }
89 }
90 #endif
91
92 return IPV6_SCOPE_GLOBAL;
93 }
94
95
96 #if defined(HAVE_GETIFADDRS)
97
Curl_if_is_interface_name(const char * interf)98 bool Curl_if_is_interface_name(const char *interf)
99 {
100 bool result = FALSE;
101
102 struct ifaddrs *iface, *head;
103
104 if(getifaddrs(&head) >= 0) {
105 for(iface=head; iface != NULL; iface=iface->ifa_next) {
106 if(curl_strequal(iface->ifa_name, interf)) {
107 result = TRUE;
108 break;
109 }
110 }
111 freeifaddrs(head);
112 }
113 return result;
114 }
115
Curl_if2ip(int af,unsigned int remote_scope,unsigned int remote_scope_id,const char * interf,char * buf,int buf_size)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)
119 {
120 struct ifaddrs *iface, *head;
121 if2ip_result_t res = IF2IP_NOT_FOUND;
122
123 #ifndef ENABLE_IPV6
124 (void) remote_scope;
125
126 #ifndef HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID
127 (void) remote_scope_id;
128 #endif
129
130 #endif
131
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)) {
137 void *addr;
138 char *ip;
139 char scope[12] = "";
140 char ipstr[64];
141 #ifdef ENABLE_IPV6
142 if(af == AF_INET6) {
143 unsigned int scopeid = 0;
144 unsigned int ifscope = Curl_ipv6_scope(iface->ifa_addr);
145
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;
152 continue;
153 }
154
155 addr = &((struct sockaddr_in6 *)iface->ifa_addr)->sin6_addr;
156 #ifdef HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID
157 /* Include the scope of this interface as part of the address */
158 scopeid =
159 ((struct sockaddr_in6 *)iface->ifa_addr)->sin6_scope_id;
160
161 /* If given, scope id should match. */
162 if(remote_scope_id && scopeid != remote_scope_id) {
163 if(res == IF2IP_NOT_FOUND)
164 res = IF2IP_AF_NOT_SUPPORTED;
165
166 continue;
167 }
168 #endif
169 if(scopeid)
170 snprintf(scope, sizeof(scope), "%%%u", scopeid);
171 }
172 else
173 #endif
174 addr = &((struct sockaddr_in *)iface->ifa_addr)->sin_addr;
175 res = IF2IP_FOUND;
176 ip = (char *) Curl_inet_ntop(af, addr, ipstr, sizeof(ipstr));
177 snprintf(buf, buf_size, "%s%s", ip, scope);
178 break;
179 }
180 }
181 else if((res == IF2IP_NOT_FOUND) &&
182 curl_strequal(iface->ifa_name, interf)) {
183 res = IF2IP_AF_NOT_SUPPORTED;
184 }
185 }
186 }
187
188 freeifaddrs(head);
189 }
190
191 return res;
192 }
193
194 #elif defined(HAVE_IOCTL_SIOCGIFADDR)
195
Curl_if_is_interface_name(const char * interf)196 bool Curl_if_is_interface_name(const char *interf)
197 {
198 /* This is here just to support the old interfaces */
199 char buf[256];
200
201 return (Curl_if2ip(AF_INET, 0 /* unused */, 0, interf, buf, sizeof(buf)) ==
202 IF2IP_NOT_FOUND) ? FALSE : TRUE;
203 }
204
Curl_if2ip(int af,unsigned int remote_scope,unsigned int remote_scope_id,const char * interf,char * buf,int buf_size)205 if2ip_result_t Curl_if2ip(int af, unsigned int remote_scope,
206 unsigned int remote_scope_id, const char *interf,
207 char *buf, int buf_size)
208 {
209 struct ifreq req;
210 struct in_addr in;
211 struct sockaddr_in *s;
212 curl_socket_t dummy;
213 size_t len;
214
215 (void)remote_scope;
216 (void)remote_scope_id;
217
218 if(!interf || (af != AF_INET))
219 return IF2IP_NOT_FOUND;
220
221 len = strlen(interf);
222 if(len >= sizeof(req.ifr_name))
223 return IF2IP_NOT_FOUND;
224
225 dummy = socket(AF_INET, SOCK_STREAM, 0);
226 if(CURL_SOCKET_BAD == dummy)
227 return IF2IP_NOT_FOUND;
228
229 memset(&req, 0, sizeof(req));
230 memcpy(req.ifr_name, interf, len+1);
231 req.ifr_addr.sa_family = AF_INET;
232
233 if(ioctl(dummy, SIOCGIFADDR, &req) < 0) {
234 sclose(dummy);
235 /* With SIOCGIFADDR, we cannot tell the difference between an interface
236 that does not exist and an interface that has no address of the
237 correct family. Assume the interface does not exist */
238 return IF2IP_NOT_FOUND;
239 }
240
241 s = (struct sockaddr_in *)&req.ifr_addr;
242 memcpy(&in, &s->sin_addr, sizeof(in));
243 Curl_inet_ntop(s->sin_family, &in, buf, buf_size);
244
245 sclose(dummy);
246 return IF2IP_FOUND;
247 }
248
249 #else
250
Curl_if_is_interface_name(const char * interf)251 bool Curl_if_is_interface_name(const char *interf)
252 {
253 (void) interf;
254
255 return FALSE;
256 }
257
Curl_if2ip(int af,unsigned int remote_scope,unsigned int remote_scope_id,const char * interf,char * buf,int buf_size)258 if2ip_result_t Curl_if2ip(int af, unsigned int remote_scope,
259 unsigned int remote_scope_id, const char *interf,
260 char *buf, int buf_size)
261 {
262 (void) af;
263 (void) remote_scope;
264 (void) remote_scope_id;
265 (void) interf;
266 (void) buf;
267 (void) buf_size;
268 return IF2IP_NOT_FOUND;
269 }
270
271 #endif
272