VirtualBox

source: vbox/trunk/src/libs/curl-7.83.1/lib/if2ip.c@ 98341

最後變更 在這個檔案從98341是 95312,由 vboxsync 提交於 3 年 前

libs/{curl,libxml2}: OSE export fixes, bugref:8515

  • 屬性 svn:eol-style 設為 native
檔案大小: 7.0 KB
 
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 ***************************************************************************/
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#ifdef ENABLE_IPV6
64/* Return the scope of the given address. */
65unsigned int Curl_ipv6_scope(const struct sockaddr *sa)
66{
67 if(sa->sa_family == AF_INET6) {
68 const struct sockaddr_in6 * sa6 = (const struct sockaddr_in6 *)(void *) sa;
69 const unsigned char *b = sa6->sin6_addr.s6_addr;
70 unsigned short w = (unsigned short) ((b[0] << 8) | b[1]);
71
72 if((b[0] & 0xFE) == 0xFC) /* Handle ULAs */
73 return IPV6_SCOPE_UNIQUELOCAL;
74 switch(w & 0xFFC0) {
75 case 0xFE80:
76 return IPV6_SCOPE_LINKLOCAL;
77 case 0xFEC0:
78 return IPV6_SCOPE_SITELOCAL;
79 case 0x0000:
80 w = b[1] | b[2] | b[3] | b[4] | b[5] | b[6] | b[7] | b[8] | b[9] |
81 b[10] | b[11] | b[12] | b[13] | b[14];
82 if(w || b[15] != 0x01)
83 break;
84 return IPV6_SCOPE_NODELOCAL;
85 default:
86 break;
87 }
88 }
89 return IPV6_SCOPE_GLOBAL;
90}
91#endif
92
93#if defined(HAVE_GETIFADDRS)
94
95if2ip_result_t Curl_if2ip(int af,
96#ifdef ENABLE_IPV6
97 unsigned int remote_scope,
98 unsigned int local_scope_id,
99#endif
100 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#if defined(ENABLE_IPV6) && \
107 !defined(HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID)
108 (void) local_scope_id;
109#endif
110
111 if(getifaddrs(&head) >= 0) {
112 for(iface = head; iface != NULL; iface = iface->ifa_next) {
113 if(iface->ifa_addr) {
114 if(iface->ifa_addr->sa_family == af) {
115 if(strcasecompare(iface->ifa_name, interf)) {
116 void *addr;
117 const char *ip;
118 char scope[12] = "";
119 char ipstr[64];
120#ifdef ENABLE_IPV6
121 if(af == AF_INET6) {
122#ifdef HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID
123 unsigned int scopeid = 0;
124#endif
125 unsigned int ifscope = Curl_ipv6_scope(iface->ifa_addr);
126
127 if(ifscope != remote_scope) {
128 /* We are interested only in interface addresses whose scope
129 matches the remote address we want to connect to: global
130 for global, link-local for link-local, etc... */
131 if(res == IF2IP_NOT_FOUND)
132 res = IF2IP_AF_NOT_SUPPORTED;
133 continue;
134 }
135
136 addr =
137 &((struct sockaddr_in6 *)(void *)iface->ifa_addr)->sin6_addr;
138#ifdef HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID
139 /* Include the scope of this interface as part of the address */
140 scopeid = ((struct sockaddr_in6 *)(void *)iface->ifa_addr)
141 ->sin6_scope_id;
142
143 /* If given, scope id should match. */
144 if(local_scope_id && scopeid != local_scope_id) {
145 if(res == IF2IP_NOT_FOUND)
146 res = IF2IP_AF_NOT_SUPPORTED;
147
148 continue;
149 }
150
151 if(scopeid)
152 msnprintf(scope, sizeof(scope), "%%%u", scopeid);
153#endif
154 }
155 else
156#endif
157 addr =
158 &((struct sockaddr_in *)(void *)iface->ifa_addr)->sin_addr;
159 res = IF2IP_FOUND;
160 ip = Curl_inet_ntop(af, addr, ipstr, sizeof(ipstr));
161 msnprintf(buf, buf_size, "%s%s", ip, scope);
162 break;
163 }
164 }
165 else if((res == IF2IP_NOT_FOUND) &&
166 strcasecompare(iface->ifa_name, interf)) {
167 res = IF2IP_AF_NOT_SUPPORTED;
168 }
169 }
170 }
171
172 freeifaddrs(head);
173 }
174
175 return res;
176}
177
178#elif defined(HAVE_IOCTL_SIOCGIFADDR)
179
180if2ip_result_t Curl_if2ip(int af,
181#ifdef ENABLE_IPV6
182 unsigned int remote_scope,
183 unsigned int local_scope_id,
184#endif
185 const char *interf,
186 char *buf, int buf_size)
187{
188 struct ifreq req;
189 struct in_addr in;
190 struct sockaddr_in *s;
191 curl_socket_t dummy;
192 size_t len;
193 const char *r;
194
195#ifdef ENABLE_IPV6
196 (void)remote_scope;
197 (void)local_scope_id;
198#endif
199
200 if(!interf || (af != AF_INET))
201 return IF2IP_NOT_FOUND;
202
203 len = strlen(interf);
204 if(len >= sizeof(req.ifr_name))
205 return IF2IP_NOT_FOUND;
206
207 dummy = socket(AF_INET, SOCK_STREAM, 0);
208 if(CURL_SOCKET_BAD == dummy)
209 return IF2IP_NOT_FOUND;
210
211 memset(&req, 0, sizeof(req));
212 memcpy(req.ifr_name, interf, len + 1);
213 req.ifr_addr.sa_family = AF_INET;
214
215 if(ioctl(dummy, SIOCGIFADDR, &req) < 0) {
216 sclose(dummy);
217 /* With SIOCGIFADDR, we cannot tell the difference between an interface
218 that does not exist and an interface that has no address of the
219 correct family. Assume the interface does not exist */
220 return IF2IP_NOT_FOUND;
221 }
222
223 s = (struct sockaddr_in *)(void *)&req.ifr_addr;
224 memcpy(&in, &s->sin_addr, sizeof(in));
225 r = Curl_inet_ntop(s->sin_family, &in, buf, buf_size);
226
227 sclose(dummy);
228 if(!r)
229 return IF2IP_NOT_FOUND;
230 return IF2IP_FOUND;
231}
232
233#else
234
235if2ip_result_t Curl_if2ip(int af,
236#ifdef ENABLE_IPV6
237 unsigned int remote_scope,
238 unsigned int local_scope_id,
239#endif
240 const char *interf,
241 char *buf, int buf_size)
242{
243 (void) af;
244#ifdef ENABLE_IPV6
245 (void) remote_scope;
246 (void) local_scope_id;
247#endif
248 (void) interf;
249 (void) buf;
250 (void) buf_size;
251 return IF2IP_NOT_FOUND;
252}
253
254#endif
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette