Revert "Imported Upstream version 7.44.0"
[platform/upstream/curl.git] / lib / if2ip.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2014, 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
57 #define _MPRINTF_REPLACE /* use our functions only */
58 #include <curl/mprintf.h>
59
60 #include "curl_memory.h"
61 /* The last #include file should be: */
62 #include "memdebug.h"
63
64 /* ------------------------------------------------------------------ */
65
66 /* Return the scope of the given address. */
67 unsigned int Curl_ipv6_scope(const struct sockaddr *sa)
68 {
69 #ifndef ENABLE_IPV6
70   (void) sa;
71 #else
72   if(sa->sa_family == AF_INET6) {
73     const struct sockaddr_in6 * sa6 = (const struct sockaddr_in6 *) sa;
74     const unsigned char * b = sa6->sin6_addr.s6_addr;
75     unsigned short w = (unsigned short) ((b[0] << 8) | b[1]);
76
77     switch(w & 0xFFC0) {
78     case 0xFE80:
79       return IPV6_SCOPE_LINKLOCAL;
80     case 0xFEC0:
81       return IPV6_SCOPE_SITELOCAL;
82     case 0x0000:
83       w = b[1] | b[2] | b[3] | b[4] | b[5] | b[6] | b[7] | b[8] | b[9] |
84           b[10] | b[11] | b[12] | b[13] | b[14];
85       if(w || b[15] != 0x01)
86         break;
87       return IPV6_SCOPE_NODELOCAL;
88     default:
89       break;
90     }
91   }
92 #endif
93
94   return IPV6_SCOPE_GLOBAL;
95 }
96
97
98 #if defined(HAVE_GETIFADDRS)
99
100 bool Curl_if_is_interface_name(const char *interf)
101 {
102   bool result = FALSE;
103
104   struct ifaddrs *iface, *head;
105
106   if(getifaddrs(&head) >= 0) {
107     for(iface=head; iface != NULL; iface=iface->ifa_next) {
108       if(curl_strequal(iface->ifa_name, interf)) {
109         result = TRUE;
110         break;
111       }
112     }
113     freeifaddrs(head);
114   }
115   return result;
116 }
117
118 if2ip_result_t Curl_if2ip(int af, unsigned int remote_scope,
119                           unsigned int remote_scope_id, const char *interf,
120                           char *buf, int buf_size)
121 {
122   struct ifaddrs *iface, *head;
123   if2ip_result_t res = IF2IP_NOT_FOUND;
124
125 #ifndef ENABLE_IPV6
126   (void) remote_scope;
127
128 #ifndef HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID
129   (void) remote_scope_id;
130 #endif
131
132 #endif
133
134   if(getifaddrs(&head) >= 0) {
135     for(iface = head; iface != NULL; iface=iface->ifa_next) {
136       if(iface->ifa_addr != NULL) {
137         if(iface->ifa_addr->sa_family == af) {
138           if(curl_strequal(iface->ifa_name, interf)) {
139             void *addr;
140             char *ip;
141             char scope[12] = "";
142             char ipstr[64];
143 #ifdef ENABLE_IPV6
144             if(af == AF_INET6) {
145               unsigned int scopeid = 0;
146               unsigned int ifscope = Curl_ipv6_scope(iface->ifa_addr);
147
148               if(ifscope != remote_scope) {
149                 /* We are interested only in interface addresses whose
150                    scope matches the remote address we want to
151                    connect to: global for global, link-local for
152                    link-local, etc... */
153                 if(res == IF2IP_NOT_FOUND) res = IF2IP_AF_NOT_SUPPORTED;
154                 continue;
155               }
156
157               addr = &((struct sockaddr_in6 *)iface->ifa_addr)->sin6_addr;
158 #ifdef HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID
159               /* Include the scope of this interface as part of the address */
160               scopeid =
161                 ((struct sockaddr_in6 *)iface->ifa_addr)->sin6_scope_id;
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
176               addr = &((struct sockaddr_in *)iface->ifa_addr)->sin_addr;
177             res = IF2IP_FOUND;
178             ip = (char *) Curl_inet_ntop(af, addr, ipstr, sizeof(ipstr));
179             snprintf(buf, buf_size, "%s%s", ip, scope);
180             break;
181           }
182         }
183         else if((res == IF2IP_NOT_FOUND) &&
184                 curl_strequal(iface->ifa_name, interf)) {
185           res = IF2IP_AF_NOT_SUPPORTED;
186         }
187       }
188     }
189
190     freeifaddrs(head);
191   }
192
193   return res;
194 }
195
196 #elif defined(HAVE_IOCTL_SIOCGIFADDR)
197
198 bool Curl_if_is_interface_name(const char *interf)
199 {
200   /* This is here just to support the old interfaces */
201   char buf[256];
202
203   return (Curl_if2ip(AF_INET, 0 /* unused */, 0, interf, buf, sizeof(buf)) ==
204           IF2IP_NOT_FOUND) ? FALSE : TRUE;
205 }
206
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)
210 {
211   struct ifreq req;
212   struct in_addr in;
213   struct sockaddr_in *s;
214   curl_socket_t dummy;
215   size_t len;
216
217   (void)remote_scope;
218   (void)remote_scope_id;
219
220   if(!interf || (af != AF_INET))
221     return IF2IP_NOT_FOUND;
222
223   len = strlen(interf);
224   if(len >= sizeof(req.ifr_name))
225     return IF2IP_NOT_FOUND;
226
227   dummy = socket(AF_INET, SOCK_STREAM, 0);
228   if(CURL_SOCKET_BAD == dummy)
229     return IF2IP_NOT_FOUND;
230
231   memset(&req, 0, sizeof(req));
232   memcpy(req.ifr_name, interf, len+1);
233   req.ifr_addr.sa_family = AF_INET;
234
235   if(ioctl(dummy, SIOCGIFADDR, &req) < 0) {
236     sclose(dummy);
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;
241   }
242
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);
246
247   sclose(dummy);
248   return IF2IP_FOUND;
249 }
250
251 #else
252
253 bool Curl_if_is_interface_name(const char *interf)
254 {
255   (void) interf;
256
257   return FALSE;
258 }
259
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)
263 {
264     (void) af;
265     (void) remote_scope;
266     (void) remote_scope_id;
267     (void) interf;
268     (void) buf;
269     (void) buf_size;
270     return IF2IP_NOT_FOUND;
271 }
272
273 #endif