1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2022, Daniel Stenberg, <[email protected]>, 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.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 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24
25#include "curl_setup.h"
26
27#ifdef HAVE_NETINET_IN_H
28# include <netinet/in.h>
29#endif
30#ifdef HAVE_ARPA_INET_H
31# include <arpa/inet.h>
32#endif
33#ifdef HAVE_NET_IF_H
34# include <net/if.h>
35#endif
36#ifdef HAVE_SYS_IOCTL_H
37# include <sys/ioctl.h>
38#endif
39#ifdef HAVE_NETDB_H
40# include <netdb.h>
41#endif
42#ifdef HAVE_SYS_SOCKIO_H
43# include <sys/sockio.h>
44#endif
45#ifdef HAVE_IFADDRS_H
46# include <ifaddrs.h>
47#endif
48#ifdef HAVE_STROPTS_H
49# include <stropts.h>
50#endif
51#ifdef __VMS
52# include <inet.h>
53#endif
54
55#include "inet_ntop.h"
56#include "strcase.h"
57#include "if2ip.h"
58/* The last 3 #include files should be in this order */
59#include "curl_printf.h"
60#include "curl_memory.h"
61#include "memdebug.h"
62
63/* ------------------------------------------------------------------ */
64
65#ifdef ENABLE_IPV6
66/* Return the scope of the given address. */
67unsigned int Curl_ipv6_scope(const struct sockaddr *sa)
68{
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 return IPV6_SCOPE_GLOBAL;
92}
93#endif
94
95#if defined(HAVE_GETIFADDRS)
96
97if2ip_result_t Curl_if2ip(int af,
98#ifdef ENABLE_IPV6
99 unsigned int remote_scope,
100 unsigned int local_scope_id,
101#endif
102 const char *interf,
103 char *buf, int buf_size)
104{
105 struct ifaddrs *iface, *head;
106 if2ip_result_t res = IF2IP_NOT_FOUND;
107
108#if defined(ENABLE_IPV6) && \
109 !defined(HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID)
110 (void) local_scope_id;
111#endif
112
113 if(getifaddrs(&head) >= 0) {
114 for(iface = head; iface != NULL; iface = iface->ifa_next) {
115 if(iface->ifa_addr) {
116 if(iface->ifa_addr->sa_family == af) {
117 if(strcasecompare(iface->ifa_name, interf)) {
118 void *addr;
119 const char *ip;
120 char scope[12] = "";
121 char ipstr[64];
122#ifdef ENABLE_IPV6
123 if(af == AF_INET6) {
124#ifdef HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID
125 unsigned int scopeid = 0;
126#endif
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 scope
131 matches the remote address we want to connect to: global
132 for global, link-local for link-local, etc... */
133 if(res == IF2IP_NOT_FOUND)
134 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(local_scope_id && scopeid != local_scope_id) {
147 if(res == IF2IP_NOT_FOUND)
148 res = IF2IP_AF_NOT_SUPPORTED;
149
150 continue;
151 }
152
153 if(scopeid)
154 msnprintf(scope, sizeof(scope), "%%%u", scopeid);
155#endif
156 }
157 else
158#endif
159 addr =
160 &((struct sockaddr_in *)(void *)iface->ifa_addr)->sin_addr;
161 res = IF2IP_FOUND;
162 ip = Curl_inet_ntop(af, addr, ipstr, sizeof(ipstr));
163 msnprintf(buf, buf_size, "%s%s", ip, scope);
164 break;
165 }
166 }
167 else if((res == IF2IP_NOT_FOUND) &&
168 strcasecompare(iface->ifa_name, interf)) {
169 res = IF2IP_AF_NOT_SUPPORTED;
170 }
171 }
172 }
173
174 freeifaddrs(head);
175 }
176
177 return res;
178}
179
180#elif defined(HAVE_IOCTL_SIOCGIFADDR)
181
182if2ip_result_t Curl_if2ip(int af,
183#ifdef ENABLE_IPV6
184 unsigned int remote_scope,
185 unsigned int local_scope_id,
186#endif
187 const char *interf,
188 char *buf, int buf_size)
189{
190 struct ifreq req;
191 struct in_addr in;
192 struct sockaddr_in *s;
193 curl_socket_t dummy;
194 size_t len;
195 const char *r;
196
197#ifdef ENABLE_IPV6
198 (void)remote_scope;
199 (void)local_scope_id;
200#endif
201
202 if(!interf || (af != AF_INET))
203 return IF2IP_NOT_FOUND;
204
205 len = strlen(interf);
206 if(len >= sizeof(req.ifr_name))
207 return IF2IP_NOT_FOUND;
208
209 dummy = socket(AF_INET, SOCK_STREAM, 0);
210 if(CURL_SOCKET_BAD == dummy)
211 return IF2IP_NOT_FOUND;
212
213 memset(&req, 0, sizeof(req));
214 memcpy(req.ifr_name, interf, len + 1);
215 req.ifr_addr.sa_family = AF_INET;
216
217 if(ioctl(dummy, SIOCGIFADDR, &req) < 0) {
218 sclose(dummy);
219 /* With SIOCGIFADDR, we cannot tell the difference between an interface
220 that does not exist and an interface that has no address of the
221 correct family. Assume the interface does not exist */
222 return IF2IP_NOT_FOUND;
223 }
224
225 s = (struct sockaddr_in *)(void *)&req.ifr_addr;
226 memcpy(&in, &s->sin_addr, sizeof(in));
227 r = Curl_inet_ntop(s->sin_family, &in, buf, buf_size);
228
229 sclose(dummy);
230 if(!r)
231 return IF2IP_NOT_FOUND;
232 return IF2IP_FOUND;
233}
234
235#else
236
237if2ip_result_t Curl_if2ip(int af,
238#ifdef ENABLE_IPV6
239 unsigned int remote_scope,
240 unsigned int local_scope_id,
241#endif
242 const char *interf,
243 char *buf, int buf_size)
244{
245 (void) af;
246#ifdef ENABLE_IPV6
247 (void) remote_scope;
248 (void) local_scope_id;
249#endif
250 (void) interf;
251 (void) buf;
252 (void) buf_size;
253 return IF2IP_NOT_FOUND;
254}
255
256#endif
257