author | chegar |
Tue, 29 Jul 2008 09:53:35 -0700 | |
changeset 911 | 9edd1b9607e2 |
parent 910 | 1f53246fb014 |
child 1152 | 29d6145d1097 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
#include <errno.h> |
|
27 |
#include <string.h> |
|
28 |
#include <sys/types.h> |
|
29 |
#include <sys/socket.h> |
|
30 |
#include <netinet/tcp.h> /* Defines TCP_NODELAY, needed for 2.6 */ |
|
31 |
#include <netinet/in.h> |
|
32 |
#include <net/if.h> |
|
33 |
#include <netdb.h> |
|
34 |
#include <stdlib.h> |
|
35 |
#include <dlfcn.h> |
|
36 |
||
37 |
#ifdef __solaris__ |
|
38 |
#include <sys/sockio.h> |
|
39 |
#include <stropts.h> |
|
40 |
#include <inet/nd.h> |
|
41 |
#endif |
|
42 |
||
43 |
#ifdef __linux__ |
|
44 |
#include <arpa/inet.h> |
|
45 |
#include <net/route.h> |
|
46 |
#include <sys/utsname.h> |
|
47 |
||
48 |
#ifndef IPV6_FLOWINFO_SEND |
|
49 |
#define IPV6_FLOWINFO_SEND 33 |
|
50 |
#endif |
|
51 |
||
52 |
#endif |
|
53 |
||
54 |
#include "jni_util.h" |
|
55 |
#include "jvm.h" |
|
56 |
#include "net_util.h" |
|
57 |
||
58 |
#include "java_net_SocketOptions.h" |
|
59 |
||
60 |
/* needed from libsocket on Solaris 8 */ |
|
61 |
||
62 |
getaddrinfo_f getaddrinfo_ptr = NULL; |
|
63 |
freeaddrinfo_f freeaddrinfo_ptr = NULL; |
|
64 |
getnameinfo_f getnameinfo_ptr = NULL; |
|
65 |
||
66 |
/* |
|
67 |
* EXCLBIND socket options only on Solaris 8 & 9. |
|
68 |
*/ |
|
69 |
#if defined(__solaris__) && !defined(TCP_EXCLBIND) |
|
70 |
#define TCP_EXCLBIND 0x21 |
|
71 |
#endif |
|
72 |
#if defined(__solaris__) && !defined(UDP_EXCLBIND) |
|
73 |
#define UDP_EXCLBIND 0x0101 |
|
74 |
#endif |
|
75 |
||
76 |
#ifdef __solaris__ |
|
77 |
static int init_max_buf; |
|
78 |
static int tcp_max_buf; |
|
79 |
static int udp_max_buf; |
|
80 |
||
81 |
/* |
|
82 |
* Get the specified parameter from the specified driver. The value |
|
83 |
* of the parameter is assumed to be an 'int'. If the parameter |
|
84 |
* cannot be obtained return the specified default value. |
|
85 |
*/ |
|
86 |
static int |
|
87 |
getParam(char *driver, char *param, int dflt) |
|
88 |
{ |
|
89 |
struct strioctl stri; |
|
90 |
char buf [64]; |
|
91 |
int s; |
|
92 |
int value; |
|
93 |
||
94 |
s = open (driver, O_RDWR); |
|
95 |
if (s < 0) { |
|
96 |
return dflt; |
|
97 |
} |
|
98 |
strncpy (buf, param, sizeof(buf)); |
|
99 |
stri.ic_cmd = ND_GET; |
|
100 |
stri.ic_timout = 0; |
|
101 |
stri.ic_dp = buf; |
|
102 |
stri.ic_len = sizeof(buf); |
|
103 |
if (ioctl (s, I_STR, &stri) < 0) { |
|
104 |
value = dflt; |
|
105 |
} else { |
|
106 |
value = atoi(buf); |
|
107 |
} |
|
108 |
close (s); |
|
109 |
return value; |
|
110 |
} |
|
111 |
#endif |
|
112 |
||
113 |
#ifdef __linux__ |
|
114 |
static int kernelV22 = 0; |
|
115 |
static int vinit = 0; |
|
116 |
||
117 |
int kernelIsV22 () { |
|
118 |
if (!vinit) { |
|
119 |
struct utsname sysinfo; |
|
120 |
if (uname(&sysinfo) == 0) { |
|
121 |
sysinfo.release[3] = '\0'; |
|
122 |
if (strcmp(sysinfo.release, "2.2") == 0) { |
|
123 |
kernelV22 = JNI_TRUE; |
|
124 |
} |
|
125 |
} |
|
126 |
vinit = 1; |
|
127 |
} |
|
128 |
return kernelV22; |
|
129 |
} |
|
130 |
||
131 |
static int kernelV24 = 0; |
|
132 |
static int vinit24 = 0; |
|
133 |
||
134 |
int kernelIsV24 () { |
|
135 |
if (!vinit24) { |
|
136 |
struct utsname sysinfo; |
|
137 |
if (uname(&sysinfo) == 0) { |
|
138 |
sysinfo.release[3] = '\0'; |
|
139 |
if (strcmp(sysinfo.release, "2.4") == 0) { |
|
140 |
kernelV24 = JNI_TRUE; |
|
141 |
} |
|
142 |
} |
|
143 |
vinit24 = 1; |
|
144 |
} |
|
145 |
return kernelV24; |
|
146 |
} |
|
147 |
||
148 |
int getScopeID (struct sockaddr *him) { |
|
149 |
struct sockaddr_in6 *hext = (struct sockaddr_in6 *)him; |
|
150 |
if (kernelIsV22()) { |
|
151 |
return 0; |
|
152 |
} |
|
153 |
return hext->sin6_scope_id; |
|
154 |
} |
|
155 |
||
156 |
int cmpScopeID (unsigned int scope, struct sockaddr *him) { |
|
157 |
struct sockaddr_in6 *hext = (struct sockaddr_in6 *)him; |
|
158 |
if (kernelIsV22()) { |
|
159 |
return 1; /* scope is ignored for comparison in 2.2 kernel */ |
|
160 |
} |
|
161 |
return hext->sin6_scope_id == scope; |
|
162 |
} |
|
163 |
||
164 |
#else |
|
165 |
||
166 |
int getScopeID (struct sockaddr *him) { |
|
167 |
struct sockaddr_in6 *him6 = (struct sockaddr_in6 *)him; |
|
168 |
return him6->sin6_scope_id; |
|
169 |
} |
|
170 |
||
171 |
int cmpScopeID (unsigned int scope, struct sockaddr *him) { |
|
172 |
struct sockaddr_in6 *him6 = (struct sockaddr_in6 *)him; |
|
173 |
return him6->sin6_scope_id == scope; |
|
174 |
} |
|
175 |
||
176 |
#endif |
|
177 |
||
178 |
||
179 |
void |
|
180 |
NET_ThrowByNameWithLastError(JNIEnv *env, const char *name, |
|
181 |
const char *defaultDetail) { |
|
182 |
char errmsg[255]; |
|
183 |
sprintf(errmsg, "errno: %d, error: %s\n", errno, defaultDetail); |
|
184 |
JNU_ThrowByNameWithLastError(env, name, errmsg); |
|
185 |
} |
|
186 |
||
187 |
void |
|
188 |
NET_ThrowCurrent(JNIEnv *env, char *msg) { |
|
189 |
NET_ThrowNew(env, errno, msg); |
|
190 |
} |
|
191 |
||
192 |
void |
|
193 |
NET_ThrowNew(JNIEnv *env, int errorNumber, char *msg) { |
|
194 |
char fullMsg[512]; |
|
195 |
if (!msg) { |
|
196 |
msg = "no further information"; |
|
197 |
} |
|
198 |
switch(errorNumber) { |
|
199 |
case EBADF: |
|
200 |
jio_snprintf(fullMsg, sizeof(fullMsg), "socket closed: %s", msg); |
|
201 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", fullMsg); |
|
202 |
break; |
|
203 |
case EINTR: |
|
204 |
JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException", msg); |
|
205 |
break; |
|
206 |
default: |
|
207 |
errno = errorNumber; |
|
208 |
JNU_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", msg); |
|
209 |
break; |
|
210 |
} |
|
211 |
} |
|
212 |
||
213 |
||
214 |
jfieldID |
|
215 |
NET_GetFileDescriptorID(JNIEnv *env) |
|
216 |
{ |
|
217 |
jclass cls = (*env)->FindClass(env, "java/io/FileDescriptor"); |
|
218 |
CHECK_NULL_RETURN(cls, NULL); |
|
219 |
return (*env)->GetFieldID(env, cls, "fd", "I"); |
|
220 |
} |
|
221 |
||
222 |
jint IPv6_supported() |
|
223 |
{ |
|
224 |
#ifndef AF_INET6 |
|
225 |
return JNI_FALSE; |
|
226 |
#endif |
|
227 |
||
228 |
#ifdef AF_INET6 |
|
229 |
int fd; |
|
230 |
void *ipv6_fn; |
|
231 |
SOCKADDR sa; |
|
910 | 232 |
socklen_t sa_len = sizeof(sa); |
2 | 233 |
|
234 |
fd = JVM_Socket(AF_INET6, SOCK_STREAM, 0) ; |
|
235 |
if (fd < 0) { |
|
236 |
/* |
|
237 |
* TODO: We really cant tell since it may be an unrelated error |
|
238 |
* for now we will assume that AF_INET6 is not available |
|
239 |
*/ |
|
240 |
return JNI_FALSE; |
|
241 |
} |
|
242 |
||
243 |
/* |
|
244 |
* If fd 0 is a socket it means we've been launched from inetd or |
|
245 |
* xinetd. If it's a socket then check the family - if it's an |
|
246 |
* IPv4 socket then we need to disable IPv6. |
|
247 |
*/ |
|
248 |
if (getsockname(0, (struct sockaddr *)&sa, &sa_len) == 0) { |
|
249 |
struct sockaddr *saP = (struct sockaddr *)&sa; |
|
250 |
if (saP->sa_family != AF_INET6) { |
|
251 |
return JNI_FALSE; |
|
252 |
} |
|
253 |
} |
|
254 |
||
255 |
/** |
|
256 |
* Linux - check if any interface has an IPv6 address. |
|
257 |
* Don't need to parse the line - we just need an indication. |
|
258 |
*/ |
|
259 |
#ifdef __linux__ |
|
260 |
{ |
|
261 |
FILE *fP = fopen("/proc/net/if_inet6", "r"); |
|
262 |
char buf[255]; |
|
263 |
char *bufP; |
|
264 |
||
265 |
if (fP == NULL) { |
|
266 |
close(fd); |
|
267 |
return JNI_FALSE; |
|
268 |
} |
|
269 |
bufP = fgets(buf, sizeof(buf), fP); |
|
270 |
fclose(fP); |
|
271 |
if (bufP == NULL) { |
|
272 |
close(fd); |
|
273 |
return JNI_FALSE; |
|
274 |
} |
|
275 |
} |
|
276 |
#endif |
|
277 |
||
278 |
/** |
|
279 |
* On Solaris 8 it's possible to create INET6 sockets even |
|
280 |
* though IPv6 is not enabled on all interfaces. Thus we |
|
281 |
* query the number of IPv6 addresses to verify that IPv6 |
|
282 |
* has been configured on at least one interface. |
|
283 |
* |
|
284 |
* On Linux it doesn't matter - if IPv6 is built-in the |
|
285 |
* kernel then IPv6 addresses will be bound automatically |
|
286 |
* to all interfaces. |
|
287 |
*/ |
|
288 |
#ifdef __solaris__ |
|
289 |
||
290 |
#ifdef SIOCGLIFNUM |
|
291 |
{ |
|
292 |
struct lifnum numifs; |
|
293 |
||
294 |
numifs.lifn_family = AF_INET6; |
|
295 |
numifs.lifn_flags = 0; |
|
296 |
if (ioctl(fd, SIOCGLIFNUM, (char *)&numifs) < 0) { |
|
297 |
/** |
|
298 |
* SIOCGLIFNUM failed - assume IPv6 not configured |
|
299 |
*/ |
|
300 |
close(fd); |
|
301 |
return JNI_FALSE; |
|
302 |
} |
|
303 |
/** |
|
304 |
* If no IPv6 addresses then return false. If count > 0 |
|
305 |
* it's possible that all IPv6 addresses are "down" but |
|
306 |
* that's okay as they may be brought "up" while the |
|
307 |
* VM is running. |
|
308 |
*/ |
|
309 |
if (numifs.lifn_count == 0) { |
|
310 |
close(fd); |
|
311 |
return JNI_FALSE; |
|
312 |
} |
|
313 |
} |
|
314 |
#else |
|
315 |
/* SIOCGLIFNUM not defined in build environment ??? */ |
|
316 |
close(fd); |
|
317 |
return JNI_FALSE; |
|
318 |
#endif |
|
319 |
||
320 |
#endif /* __solaris */ |
|
321 |
||
322 |
#endif /* AF_INET6 */ |
|
323 |
||
324 |
/* |
|
325 |
* OK we may have the stack available in the kernel, |
|
326 |
* we should also check if the APIs are available. |
|
327 |
*/ |
|
328 |
ipv6_fn = JVM_FindLibraryEntry(RTLD_DEFAULT, "inet_pton"); |
|
329 |
if (ipv6_fn == NULL ) { |
|
330 |
close(fd); |
|
331 |
return JNI_FALSE; |
|
332 |
} |
|
333 |
||
334 |
/* |
|
335 |
* We've got the library, let's get the pointers to some |
|
336 |
* IPV6 specific functions. We have to do that because, at least |
|
337 |
* on Solaris we may build on a system without IPV6 networking |
|
338 |
* libraries, therefore we can't have a hard link to these |
|
339 |
* functions. |
|
340 |
*/ |
|
341 |
getaddrinfo_ptr = (getaddrinfo_f) |
|
342 |
JVM_FindLibraryEntry(RTLD_DEFAULT, "getaddrinfo"); |
|
343 |
||
344 |
freeaddrinfo_ptr = (freeaddrinfo_f) |
|
345 |
JVM_FindLibraryEntry(RTLD_DEFAULT, "freeaddrinfo"); |
|
346 |
||
347 |
getnameinfo_ptr = (getnameinfo_f) |
|
348 |
JVM_FindLibraryEntry(RTLD_DEFAULT, "getnameinfo"); |
|
349 |
||
350 |
if (freeaddrinfo_ptr == NULL || getnameinfo_ptr == NULL) { |
|
351 |
/* Wee need all 3 of them */ |
|
352 |
getaddrinfo_ptr = NULL; |
|
353 |
} |
|
354 |
||
355 |
close(fd); |
|
356 |
return JNI_TRUE; |
|
357 |
} |
|
358 |
||
359 |
void |
|
360 |
NET_AllocSockaddr(struct sockaddr **him, int *len) { |
|
361 |
#ifdef AF_INET6 |
|
362 |
if (ipv6_available()) { |
|
363 |
struct sockaddr_in6 *him6 = (struct sockaddr_in6*)malloc(sizeof(struct sockaddr_in6)); |
|
364 |
*him = (struct sockaddr*)him6; |
|
365 |
*len = sizeof(struct sockaddr_in6); |
|
366 |
} else |
|
367 |
#endif /* AF_INET6 */ |
|
368 |
{ |
|
369 |
struct sockaddr_in *him4 = (struct sockaddr_in*)malloc(sizeof(struct sockaddr_in)); |
|
370 |
*him = (struct sockaddr*)him4; |
|
371 |
*len = sizeof(struct sockaddr_in); |
|
372 |
} |
|
373 |
} |
|
374 |
||
375 |
#if defined(__linux__) && defined(AF_INET6) |
|
376 |
||
377 |
||
378 |
/* following code creates a list of addresses from the kernel |
|
379 |
* routing table that are routed via the loopback address. |
|
380 |
* We check all destination addresses against this table |
|
381 |
* and override the scope_id field to use the relevant value for "lo" |
|
382 |
* in order to work-around the Linux bug that prevents packets destined |
|
383 |
* for certain local addresses from being sent via a physical interface. |
|
384 |
*/ |
|
385 |
||
386 |
struct loopback_route { |
|
387 |
struct in6_addr addr; /* destination address */ |
|
388 |
int plen; /* prefix length */ |
|
389 |
}; |
|
390 |
||
391 |
static struct loopback_route *loRoutes = 0; |
|
392 |
static int nRoutes = 0; /* number of routes */ |
|
393 |
static int loRoutes_size = 16; /* initial size */ |
|
394 |
static int lo_scope_id = 0; |
|
395 |
||
396 |
static void initLoopbackRoutes(); |
|
397 |
||
398 |
void printAddr (struct in6_addr *addr) { |
|
399 |
int i; |
|
400 |
for (i=0; i<16; i++) { |
|
401 |
printf ("%02x", addr->s6_addr[i]); |
|
402 |
} |
|
403 |
printf ("\n"); |
|
404 |
} |
|
405 |
||
406 |
static jboolean needsLoopbackRoute (struct in6_addr* dest_addr) { |
|
407 |
int byte_count; |
|
408 |
int extra_bits, i; |
|
409 |
struct loopback_route *ptr; |
|
410 |
||
411 |
if (loRoutes == 0) { |
|
412 |
initLoopbackRoutes(); |
|
413 |
} |
|
414 |
||
415 |
for (ptr = loRoutes, i=0; i<nRoutes; i++, ptr++) { |
|
416 |
struct in6_addr *target_addr=&ptr->addr; |
|
417 |
int dest_plen = ptr->plen; |
|
418 |
byte_count = dest_plen >> 3; |
|
419 |
extra_bits = dest_plen & 0x3; |
|
420 |
||
421 |
if (byte_count > 0) { |
|
422 |
if (memcmp(target_addr, dest_addr, byte_count)) { |
|
423 |
continue; /* no match */ |
|
424 |
} |
|
425 |
} |
|
426 |
||
427 |
if (extra_bits > 0) { |
|
428 |
unsigned char c1 = ((unsigned char *)target_addr)[byte_count]; |
|
429 |
unsigned char c2 = ((unsigned char *)&dest_addr)[byte_count]; |
|
430 |
unsigned char mask = 0xff << (8 - extra_bits); |
|
431 |
if ((c1 & mask) != (c2 & mask)) { |
|
432 |
continue; |
|
433 |
} |
|
434 |
} |
|
435 |
return JNI_TRUE; |
|
436 |
} |
|
437 |
return JNI_FALSE; |
|
438 |
} |
|
439 |
||
440 |
||
441 |
static void initLoopbackRoutes() { |
|
442 |
FILE *f; |
|
443 |
char srcp[8][5]; |
|
444 |
char hopp[8][5]; |
|
445 |
int dest_plen, src_plen, use, refcnt, metric; |
|
446 |
unsigned long flags; |
|
447 |
char dest_str[40]; |
|
448 |
struct in6_addr dest_addr; |
|
449 |
char device[16]; |
|
450 |
||
451 |
if (loRoutes != 0) { |
|
452 |
free (loRoutes); |
|
453 |
} |
|
454 |
loRoutes = calloc (loRoutes_size, sizeof(struct loopback_route)); |
|
455 |
if (loRoutes == 0) { |
|
456 |
return; |
|
457 |
} |
|
458 |
/* |
|
459 |
* Scan /proc/net/ipv6_route looking for a matching |
|
460 |
* route. |
|
461 |
*/ |
|
462 |
if ((f = fopen("/proc/net/ipv6_route", "r")) == NULL) { |
|
463 |
return ; |
|
464 |
} |
|
465 |
while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x " |
|
466 |
"%4s%4s%4s%4s%4s%4s%4s%4s %02x " |
|
467 |
"%4s%4s%4s%4s%4s%4s%4s%4s " |
|
468 |
"%08x %08x %08x %08lx %8s", |
|
469 |
dest_str, &dest_str[5], &dest_str[10], &dest_str[15], |
|
470 |
&dest_str[20], &dest_str[25], &dest_str[30], &dest_str[35], |
|
471 |
&dest_plen, |
|
472 |
srcp[0], srcp[1], srcp[2], srcp[3], |
|
473 |
srcp[4], srcp[5], srcp[6], srcp[7], |
|
474 |
&src_plen, |
|
475 |
hopp[0], hopp[1], hopp[2], hopp[3], |
|
476 |
hopp[4], hopp[5], hopp[6], hopp[7], |
|
477 |
&metric, &use, &refcnt, &flags, device) == 31) { |
|
478 |
||
479 |
/* |
|
480 |
* Some routes should be ignored |
|
481 |
*/ |
|
482 |
if ( (dest_plen < 0 || dest_plen > 128) || |
|
483 |
(src_plen != 0) || |
|
484 |
(flags & (RTF_POLICY | RTF_FLOW)) || |
|
485 |
((flags & RTF_REJECT) && dest_plen == 0) ) { |
|
486 |
continue; |
|
487 |
} |
|
488 |
||
489 |
/* |
|
490 |
* Convert the destination address |
|
491 |
*/ |
|
492 |
dest_str[4] = ':'; |
|
493 |
dest_str[9] = ':'; |
|
494 |
dest_str[14] = ':'; |
|
495 |
dest_str[19] = ':'; |
|
496 |
dest_str[24] = ':'; |
|
497 |
dest_str[29] = ':'; |
|
498 |
dest_str[34] = ':'; |
|
499 |
dest_str[39] = '\0'; |
|
500 |
||
501 |
if (inet_pton(AF_INET6, dest_str, &dest_addr) < 0) { |
|
502 |
/* not an Ipv6 address */ |
|
503 |
continue; |
|
504 |
} |
|
505 |
if (strcmp(device, "lo") != 0) { |
|
506 |
/* Not a loopback route */ |
|
507 |
continue; |
|
508 |
} else { |
|
509 |
if (nRoutes == loRoutes_size) { |
|
510 |
loRoutes = realloc (loRoutes, loRoutes_size * |
|
511 |
sizeof (struct loopback_route) * 2); |
|
512 |
if (loRoutes == 0) { |
|
513 |
return ; |
|
514 |
} |
|
515 |
loRoutes_size *= 2; |
|
516 |
} |
|
517 |
memcpy (&loRoutes[nRoutes].addr,&dest_addr,sizeof(struct in6_addr)); |
|
518 |
loRoutes[nRoutes].plen = dest_plen; |
|
519 |
nRoutes ++; |
|
520 |
} |
|
521 |
} |
|
522 |
||
523 |
fclose (f); |
|
524 |
{ |
|
525 |
/* now find the scope_id for "lo" */ |
|
526 |
||
910 | 527 |
char devname[20]; |
2 | 528 |
char addr6p[8][5]; |
529 |
int plen, scope, dad_status, if_idx; |
|
530 |
||
531 |
if ((f = fopen("/proc/net/if_inet6", "r")) != NULL) { |
|
532 |
while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %20s\n", |
|
533 |
addr6p[0], addr6p[1], addr6p[2], addr6p[3], |
|
534 |
addr6p[4], addr6p[5], addr6p[6], addr6p[7], |
|
535 |
&if_idx, &plen, &scope, &dad_status, devname) == 13) { |
|
536 |
||
537 |
if (strcmp(devname, "lo") == 0) { |
|
538 |
/* |
|
539 |
* Found - so just return the index |
|
540 |
*/ |
|
541 |
fclose(f); |
|
542 |
lo_scope_id = if_idx; |
|
543 |
return; |
|
544 |
} |
|
545 |
} |
|
546 |
fclose(f); |
|
547 |
} |
|
548 |
} |
|
549 |
} |
|
550 |
||
551 |
/* |
|
552 |
* Following is used for binding to local addresses. Equivalent |
|
553 |
* to code above, for bind(). |
|
554 |
*/ |
|
555 |
||
556 |
struct localinterface { |
|
557 |
int index; |
|
558 |
char localaddr [16]; |
|
559 |
}; |
|
560 |
||
561 |
static struct localinterface *localifs = 0; |
|
562 |
static int localifsSize = 0; /* size of array */ |
|
563 |
static int nifs = 0; /* number of entries used in array */ |
|
564 |
||
565 |
/* not thread safe: make sure called once from one thread */ |
|
566 |
||
567 |
static void initLocalIfs () { |
|
568 |
FILE *f; |
|
569 |
unsigned char staddr [16]; |
|
570 |
char ifname [32]; |
|
571 |
struct localinterface *lif=0; |
|
572 |
int index, x1, x2, x3; |
|
573 |
unsigned int u0,u1,u2,u3,u4,u5,u6,u7,u8,u9,ua,ub,uc,ud,ue,uf; |
|
574 |
||
575 |
if ((f = fopen("/proc/net/if_inet6", "r")) == NULL) { |
|
576 |
return ; |
|
577 |
} |
|
578 |
while (fscanf (f, "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x " |
|
579 |
"%d %x %x %x %s",&u0,&u1,&u2,&u3,&u4,&u5,&u6,&u7, |
|
580 |
&u8,&u9,&ua,&ub,&uc,&ud,&ue,&uf, |
|
581 |
&index, &x1, &x2, &x3, ifname) == 21) { |
|
582 |
staddr[0] = (unsigned char)u0; |
|
583 |
staddr[1] = (unsigned char)u1; |
|
584 |
staddr[2] = (unsigned char)u2; |
|
585 |
staddr[3] = (unsigned char)u3; |
|
586 |
staddr[4] = (unsigned char)u4; |
|
587 |
staddr[5] = (unsigned char)u5; |
|
588 |
staddr[6] = (unsigned char)u6; |
|
589 |
staddr[7] = (unsigned char)u7; |
|
590 |
staddr[8] = (unsigned char)u8; |
|
591 |
staddr[9] = (unsigned char)u9; |
|
592 |
staddr[10] = (unsigned char)ua; |
|
593 |
staddr[11] = (unsigned char)ub; |
|
594 |
staddr[12] = (unsigned char)uc; |
|
595 |
staddr[13] = (unsigned char)ud; |
|
596 |
staddr[14] = (unsigned char)ue; |
|
597 |
staddr[15] = (unsigned char)uf; |
|
598 |
nifs ++; |
|
599 |
if (nifs > localifsSize) { |
|
600 |
localifs = (struct localinterface *) realloc ( |
|
601 |
localifs, sizeof (struct localinterface)* (localifsSize+5)); |
|
602 |
if (localifs == 0) { |
|
603 |
nifs = 0; |
|
604 |
fclose (f); |
|
605 |
return; |
|
606 |
} |
|
607 |
lif = localifs + localifsSize; |
|
608 |
localifsSize += 5; |
|
609 |
} else { |
|
610 |
lif ++; |
|
611 |
} |
|
612 |
memcpy (lif->localaddr, staddr, 16); |
|
613 |
lif->index = index; |
|
614 |
} |
|
615 |
fclose (f); |
|
616 |
} |
|
617 |
||
618 |
/* return the scope_id (interface index) of the |
|
619 |
* interface corresponding to the given address |
|
620 |
* returns 0 if no match found |
|
621 |
*/ |
|
622 |
||
623 |
static int getLocalScopeID (char *addr) { |
|
624 |
struct localinterface *lif; |
|
625 |
int i; |
|
626 |
if (localifs == 0) { |
|
627 |
initLocalIfs(); |
|
628 |
} |
|
629 |
for (i=0, lif=localifs; i<nifs; i++, lif++) { |
|
630 |
if (memcmp (addr, lif->localaddr, 16) == 0) { |
|
631 |
return lif->index; |
|
632 |
} |
|
633 |
} |
|
634 |
return 0; |
|
635 |
} |
|
636 |
||
637 |
void initLocalAddrTable () { |
|
638 |
initLoopbackRoutes(); |
|
639 |
initLocalIfs(); |
|
640 |
} |
|
641 |
||
642 |
#else |
|
643 |
||
644 |
void initLocalAddrTable () {} |
|
645 |
||
646 |
#endif |
|
647 |
||
648 |
/* In the case of an IPv4 Inetaddress this method will return an |
|
649 |
* IPv4 mapped address where IPv6 is available and v4MappedAddress is TRUE. |
|
650 |
* Otherwise it will return a sockaddr_in structure for an IPv4 InetAddress. |
|
651 |
*/ |
|
652 |
JNIEXPORT int JNICALL |
|
653 |
NET_InetAddressToSockaddr(JNIEnv *env, jobject iaObj, int port, struct sockaddr *him, |
|
654 |
int *len, jboolean v4MappedAddress) { |
|
655 |
jint family; |
|
656 |
family = (*env)->GetIntField(env, iaObj, ia_familyID); |
|
657 |
#ifdef AF_INET6 |
|
658 |
/* needs work. 1. family 2. clean up him6 etc deallocate memory */ |
|
659 |
if (ipv6_available() && !(family == IPv4 && v4MappedAddress == JNI_FALSE)) { |
|
660 |
struct sockaddr_in6 *him6 = (struct sockaddr_in6 *)him; |
|
661 |
jbyteArray ipaddress; |
|
662 |
jbyte caddr[16]; |
|
663 |
jint address; |
|
664 |
||
665 |
||
666 |
if (family == IPv4) { /* will convert to IPv4-mapped address */ |
|
667 |
memset((char *) caddr, 0, 16); |
|
668 |
address = (*env)->GetIntField(env, iaObj, ia_addressID); |
|
669 |
if (address == INADDR_ANY) { |
|
670 |
/* we would always prefer IPv6 wildcard address |
|
671 |
caddr[10] = 0xff; |
|
672 |
caddr[11] = 0xff; */ |
|
673 |
} else { |
|
674 |
caddr[10] = 0xff; |
|
675 |
caddr[11] = 0xff; |
|
676 |
caddr[12] = ((address >> 24) & 0xff); |
|
677 |
caddr[13] = ((address >> 16) & 0xff); |
|
678 |
caddr[14] = ((address >> 8) & 0xff); |
|
679 |
caddr[15] = (address & 0xff); |
|
680 |
} |
|
681 |
} else { |
|
682 |
ipaddress = (*env)->GetObjectField(env, iaObj, ia6_ipaddressID); |
|
683 |
(*env)->GetByteArrayRegion(env, ipaddress, 0, 16, caddr); |
|
684 |
} |
|
685 |
memset((char *)him6, 0, sizeof(struct sockaddr_in6)); |
|
686 |
him6->sin6_port = htons(port); |
|
687 |
memcpy((void *)&(him6->sin6_addr), caddr, sizeof(struct in6_addr) ); |
|
688 |
him6->sin6_family = AF_INET6; |
|
689 |
*len = sizeof(struct sockaddr_in6) ; |
|
690 |
||
691 |
/* |
|
692 |
* On Linux if we are connecting to a link-local address |
|
693 |
* we need to specify the interface in the scope_id (2.4 kernel only) |
|
694 |
* |
|
695 |
* If the scope was cached the we use the cached value. If not cached but |
|
696 |
* specified in the Inet6Address we use that, but we first check if the |
|
697 |
* address needs to be routed via the loopback interface. In this case, |
|
698 |
* we override the specified value with that of the loopback interface. |
|
699 |
* If no cached value exists and no value was specified by user, then |
|
700 |
* we try to determine a value ffrom the routing table. In all these |
|
701 |
* cases the used value is cached for further use. |
|
702 |
*/ |
|
703 |
#ifdef __linux__ |
|
704 |
if (IN6_IS_ADDR_LINKLOCAL(&(him6->sin6_addr))) { |
|
705 |
int cached_scope_id = 0, scope_id = 0; |
|
706 |
int old_kernel = kernelIsV22(); |
|
707 |
||
708 |
if (ia6_cachedscopeidID && !old_kernel) { |
|
709 |
cached_scope_id = (int)(*env)->GetIntField(env, iaObj, ia6_cachedscopeidID); |
|
710 |
/* if cached value exists then use it. Otherwise, check |
|
711 |
* if scope is set in the address. |
|
712 |
*/ |
|
713 |
if (!cached_scope_id) { |
|
714 |
if (ia6_scopeidID) { |
|
715 |
scope_id = (int)(*env)->GetIntField(env,iaObj,ia6_scopeidID); |
|
716 |
} |
|
717 |
if (scope_id != 0) { |
|
718 |
/* check user-specified value for loopback case |
|
719 |
* that needs to be overridden |
|
720 |
*/ |
|
721 |
if (kernelIsV24() && needsLoopbackRoute (&him6->sin6_addr)) { |
|
722 |
cached_scope_id = lo_scope_id; |
|
723 |
(*env)->SetIntField(env, iaObj, ia6_cachedscopeidID, cached_scope_id); |
|
724 |
} |
|
725 |
} else { |
|
726 |
/* |
|
727 |
* Otherwise consult the IPv6 routing tables to |
|
728 |
* try determine the appropriate interface. |
|
729 |
*/ |
|
730 |
if (kernelIsV24()) { |
|
731 |
cached_scope_id = getDefaultIPv6Interface( &(him6->sin6_addr) ); |
|
732 |
} else { |
|
733 |
cached_scope_id = getLocalScopeID( (char *)&(him6->sin6_addr) ); |
|
734 |
if (cached_scope_id == 0) { |
|
735 |
cached_scope_id = getDefaultIPv6Interface( &(him6->sin6_addr) ); |
|
736 |
} |
|
737 |
} |
|
738 |
(*env)->SetIntField(env, iaObj, ia6_cachedscopeidID, cached_scope_id); |
|
739 |
} |
|
740 |
} |
|
741 |
} |
|
742 |
||
743 |
/* |
|
744 |
* If we have a scope_id use the extended form |
|
745 |
* of sockaddr_in6. |
|
746 |
*/ |
|
747 |
||
748 |
if (!old_kernel) { |
|
749 |
struct sockaddr_in6 *him6 = |
|
750 |
(struct sockaddr_in6 *)him; |
|
751 |
him6->sin6_scope_id = cached_scope_id != 0 ? |
|
752 |
cached_scope_id : scope_id; |
|
753 |
*len = sizeof(struct sockaddr_in6); |
|
754 |
} |
|
755 |
} |
|
756 |
#else |
|
757 |
/* handle scope_id for solaris */ |
|
758 |
||
759 |
if (family != IPv4) { |
|
760 |
if (ia6_scopeidID) { |
|
761 |
him6->sin6_scope_id = (int)(*env)->GetIntField(env, iaObj, ia6_scopeidID); |
|
762 |
} |
|
763 |
} |
|
764 |
#endif |
|
765 |
} else |
|
766 |
#endif /* AF_INET6 */ |
|
767 |
{ |
|
768 |
struct sockaddr_in *him4 = (struct sockaddr_in*)him; |
|
769 |
jint address; |
|
770 |
if (family == IPv6) { |
|
771 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Protocol family unavailable"); |
|
772 |
return -1; |
|
773 |
} |
|
774 |
memset((char *) him4, 0, sizeof(struct sockaddr_in)); |
|
775 |
address = (*env)->GetIntField(env, iaObj, ia_addressID); |
|
776 |
him4->sin_port = htons((short) port); |
|
777 |
him4->sin_addr.s_addr = (uint32_t) htonl(address); |
|
778 |
him4->sin_family = AF_INET; |
|
779 |
*len = sizeof(struct sockaddr_in); |
|
780 |
} |
|
781 |
return 0; |
|
782 |
} |
|
783 |
||
784 |
void |
|
785 |
NET_SetTrafficClass(struct sockaddr *him, int trafficClass) { |
|
786 |
#ifdef AF_INET6 |
|
787 |
if (him->sa_family == AF_INET6) { |
|
788 |
struct sockaddr_in6 *him6 = (struct sockaddr_in6 *)him; |
|
789 |
him6->sin6_flowinfo = htonl((trafficClass & 0xff) << 20); |
|
790 |
} |
|
791 |
#endif /* AF_INET6 */ |
|
792 |
} |
|
793 |
||
794 |
jint |
|
795 |
NET_GetPortFromSockaddr(struct sockaddr *him) { |
|
796 |
#ifdef AF_INET6 |
|
797 |
if (him->sa_family == AF_INET6) { |
|
798 |
return ntohs(((struct sockaddr_in6*)him)->sin6_port); |
|
799 |
||
800 |
} else |
|
801 |
#endif /* AF_INET6 */ |
|
802 |
{ |
|
803 |
return ntohs(((struct sockaddr_in*)him)->sin_port); |
|
804 |
} |
|
805 |
} |
|
806 |
||
807 |
int |
|
808 |
NET_IsIPv4Mapped(jbyte* caddr) { |
|
809 |
int i; |
|
810 |
for (i = 0; i < 10; i++) { |
|
811 |
if (caddr[i] != 0x00) { |
|
812 |
return 0; /* false */ |
|
813 |
} |
|
814 |
} |
|
815 |
||
816 |
if (((caddr[10] & 0xff) == 0xff) && ((caddr[11] & 0xff) == 0xff)) { |
|
817 |
return 1; /* true */ |
|
818 |
} |
|
819 |
return 0; /* false */ |
|
820 |
} |
|
821 |
||
822 |
int |
|
823 |
NET_IPv4MappedToIPv4(jbyte* caddr) { |
|
824 |
return ((caddr[12] & 0xff) << 24) | ((caddr[13] & 0xff) << 16) | ((caddr[14] & 0xff) << 8) |
|
825 |
| (caddr[15] & 0xff); |
|
826 |
} |
|
827 |
||
828 |
int |
|
829 |
NET_IsEqual(jbyte* caddr1, jbyte* caddr2) { |
|
830 |
int i; |
|
831 |
for (i = 0; i < 16; i++) { |
|
832 |
if (caddr1[i] != caddr2[i]) { |
|
833 |
return 0; /* false */ |
|
834 |
} |
|
835 |
} |
|
836 |
return 1; |
|
837 |
} |
|
838 |
||
839 |
jboolean NET_addrtransAvailable() { |
|
840 |
return (jboolean)(getaddrinfo_ptr != NULL); |
|
841 |
} |
|
842 |
||
843 |
/* |
|
844 |
* Map the Java level socket option to the platform specific |
|
845 |
* level and option name. |
|
846 |
*/ |
|
847 |
int |
|
848 |
NET_MapSocketOption(jint cmd, int *level, int *optname) { |
|
849 |
static struct { |
|
850 |
jint cmd; |
|
851 |
int level; |
|
852 |
int optname; |
|
853 |
} const opts[] = { |
|
854 |
{ java_net_SocketOptions_TCP_NODELAY, IPPROTO_TCP, TCP_NODELAY }, |
|
855 |
{ java_net_SocketOptions_SO_OOBINLINE, SOL_SOCKET, SO_OOBINLINE }, |
|
856 |
{ java_net_SocketOptions_SO_LINGER, SOL_SOCKET, SO_LINGER }, |
|
857 |
{ java_net_SocketOptions_SO_SNDBUF, SOL_SOCKET, SO_SNDBUF }, |
|
858 |
{ java_net_SocketOptions_SO_RCVBUF, SOL_SOCKET, SO_RCVBUF }, |
|
859 |
{ java_net_SocketOptions_SO_KEEPALIVE, SOL_SOCKET, SO_KEEPALIVE }, |
|
860 |
{ java_net_SocketOptions_SO_REUSEADDR, SOL_SOCKET, SO_REUSEADDR }, |
|
861 |
{ java_net_SocketOptions_SO_BROADCAST, SOL_SOCKET, SO_BROADCAST }, |
|
862 |
{ java_net_SocketOptions_IP_TOS, IPPROTO_IP, IP_TOS }, |
|
863 |
{ java_net_SocketOptions_IP_MULTICAST_IF, IPPROTO_IP, IP_MULTICAST_IF }, |
|
864 |
{ java_net_SocketOptions_IP_MULTICAST_IF2, IPPROTO_IP, IP_MULTICAST_IF }, |
|
865 |
{ java_net_SocketOptions_IP_MULTICAST_LOOP, IPPROTO_IP, IP_MULTICAST_LOOP }, |
|
866 |
}; |
|
867 |
||
868 |
int i; |
|
869 |
||
870 |
/* |
|
871 |
* Different multicast options if IPv6 is enabled |
|
872 |
*/ |
|
873 |
#ifdef AF_INET6 |
|
874 |
if (ipv6_available()) { |
|
875 |
switch (cmd) { |
|
876 |
case java_net_SocketOptions_IP_MULTICAST_IF: |
|
877 |
case java_net_SocketOptions_IP_MULTICAST_IF2: |
|
878 |
*level = IPPROTO_IPV6; |
|
879 |
*optname = IPV6_MULTICAST_IF; |
|
880 |
return 0; |
|
881 |
||
882 |
case java_net_SocketOptions_IP_MULTICAST_LOOP: |
|
883 |
*level = IPPROTO_IPV6; |
|
884 |
*optname = IPV6_MULTICAST_LOOP; |
|
885 |
return 0; |
|
886 |
} |
|
887 |
} |
|
888 |
#endif |
|
889 |
||
890 |
/* |
|
891 |
* Map the Java level option to the native level |
|
892 |
*/ |
|
893 |
for (i=0; i<(int)(sizeof(opts) / sizeof(opts[0])); i++) { |
|
894 |
if (cmd == opts[i].cmd) { |
|
895 |
*level = opts[i].level; |
|
896 |
*optname = opts[i].optname; |
|
897 |
return 0; |
|
898 |
} |
|
899 |
} |
|
900 |
||
901 |
/* not found */ |
|
902 |
return -1; |
|
903 |
} |
|
904 |
||
905 |
/* |
|
906 |
* Determine the default interface for an IPv6 address. |
|
907 |
* |
|
908 |
* 1. Scans /proc/net/ipv6_route for a matching route |
|
909 |
* (eg: fe80::/10 or a route for the specific address). |
|
910 |
* This will tell us the interface to use (eg: "eth0"). |
|
911 |
* |
|
912 |
* 2. Lookup /proc/net/if_inet6 to map the interface |
|
913 |
* name to an interface index. |
|
914 |
* |
|
915 |
* Returns :- |
|
916 |
* -1 if error |
|
917 |
* 0 if no matching interface |
|
918 |
* >1 interface index to use for the link-local address. |
|
919 |
*/ |
|
920 |
#if defined(__linux__) && defined(AF_INET6) |
|
921 |
int getDefaultIPv6Interface(struct in6_addr *target_addr) { |
|
922 |
FILE *f; |
|
923 |
char srcp[8][5]; |
|
924 |
char hopp[8][5]; |
|
925 |
int dest_plen, src_plen, use, refcnt, metric; |
|
926 |
unsigned long flags; |
|
927 |
char dest_str[40]; |
|
928 |
struct in6_addr dest_addr; |
|
929 |
char device[16]; |
|
930 |
jboolean match = JNI_FALSE; |
|
931 |
||
932 |
/* |
|
933 |
* Scan /proc/net/ipv6_route looking for a matching |
|
934 |
* route. |
|
935 |
*/ |
|
936 |
if ((f = fopen("/proc/net/ipv6_route", "r")) == NULL) { |
|
937 |
return -1; |
|
938 |
} |
|
939 |
while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x " |
|
940 |
"%4s%4s%4s%4s%4s%4s%4s%4s %02x " |
|
941 |
"%4s%4s%4s%4s%4s%4s%4s%4s " |
|
942 |
"%08x %08x %08x %08lx %8s", |
|
943 |
dest_str, &dest_str[5], &dest_str[10], &dest_str[15], |
|
944 |
&dest_str[20], &dest_str[25], &dest_str[30], &dest_str[35], |
|
945 |
&dest_plen, |
|
946 |
srcp[0], srcp[1], srcp[2], srcp[3], |
|
947 |
srcp[4], srcp[5], srcp[6], srcp[7], |
|
948 |
&src_plen, |
|
949 |
hopp[0], hopp[1], hopp[2], hopp[3], |
|
950 |
hopp[4], hopp[5], hopp[6], hopp[7], |
|
951 |
&metric, &use, &refcnt, &flags, device) == 31) { |
|
952 |
||
953 |
/* |
|
954 |
* Some routes should be ignored |
|
955 |
*/ |
|
956 |
if ( (dest_plen < 0 || dest_plen > 128) || |
|
957 |
(src_plen != 0) || |
|
958 |
(flags & (RTF_POLICY | RTF_FLOW)) || |
|
959 |
((flags & RTF_REJECT) && dest_plen == 0) ) { |
|
960 |
continue; |
|
961 |
} |
|
962 |
||
963 |
/* |
|
964 |
* Convert the destination address |
|
965 |
*/ |
|
966 |
dest_str[4] = ':'; |
|
967 |
dest_str[9] = ':'; |
|
968 |
dest_str[14] = ':'; |
|
969 |
dest_str[19] = ':'; |
|
970 |
dest_str[24] = ':'; |
|
971 |
dest_str[29] = ':'; |
|
972 |
dest_str[34] = ':'; |
|
973 |
dest_str[39] = '\0'; |
|
974 |
||
975 |
if (inet_pton(AF_INET6, dest_str, &dest_addr) < 0) { |
|
976 |
/* not an Ipv6 address */ |
|
977 |
continue; |
|
978 |
} else { |
|
979 |
/* |
|
980 |
* The prefix len (dest_plen) indicates the number of bits we |
|
981 |
* need to match on. |
|
982 |
* |
|
983 |
* dest_plen / 8 => number of bytes to match |
|
984 |
* dest_plen % 8 => number of additional bits to match |
|
985 |
* |
|
986 |
* eg: fe80::/10 => match 1 byte + 2 additional bits in the |
|
987 |
* the next byte. |
|
988 |
*/ |
|
989 |
int byte_count = dest_plen >> 3; |
|
990 |
int extra_bits = dest_plen & 0x3; |
|
991 |
||
992 |
if (byte_count > 0) { |
|
993 |
if (memcmp(target_addr, &dest_addr, byte_count)) { |
|
994 |
continue; /* no match */ |
|
995 |
} |
|
996 |
} |
|
997 |
||
998 |
if (extra_bits > 0) { |
|
999 |
unsigned char c1 = ((unsigned char *)target_addr)[byte_count]; |
|
1000 |
unsigned char c2 = ((unsigned char *)&dest_addr)[byte_count]; |
|
1001 |
unsigned char mask = 0xff << (8 - extra_bits); |
|
1002 |
if ((c1 & mask) != (c2 & mask)) { |
|
1003 |
continue; |
|
1004 |
} |
|
1005 |
} |
|
1006 |
||
1007 |
/* |
|
1008 |
* We have a match |
|
1009 |
*/ |
|
1010 |
match = JNI_TRUE; |
|
1011 |
break; |
|
1012 |
} |
|
1013 |
} |
|
1014 |
fclose(f); |
|
1015 |
||
1016 |
/* |
|
1017 |
* If there's a match then we lookup the interface |
|
1018 |
* index. |
|
1019 |
*/ |
|
1020 |
if (match) { |
|
910 | 1021 |
char devname[20]; |
2 | 1022 |
char addr6p[8][5]; |
1023 |
int plen, scope, dad_status, if_idx; |
|
1024 |
||
1025 |
if ((f = fopen("/proc/net/if_inet6", "r")) != NULL) { |
|
1026 |
while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %20s\n", |
|
1027 |
addr6p[0], addr6p[1], addr6p[2], addr6p[3], |
|
1028 |
addr6p[4], addr6p[5], addr6p[6], addr6p[7], |
|
1029 |
&if_idx, &plen, &scope, &dad_status, devname) == 13) { |
|
1030 |
||
1031 |
if (strcmp(devname, device) == 0) { |
|
1032 |
/* |
|
1033 |
* Found - so just return the index |
|
1034 |
*/ |
|
1035 |
fclose(f); |
|
1036 |
return if_idx; |
|
1037 |
} |
|
1038 |
} |
|
1039 |
fclose(f); |
|
1040 |
} else { |
|
1041 |
/* |
|
1042 |
* Couldn't open /proc/net/if_inet6 |
|
1043 |
*/ |
|
1044 |
return -1; |
|
1045 |
} |
|
1046 |
} |
|
1047 |
||
1048 |
/* |
|
1049 |
* If we get here it means we didn't there wasn't any |
|
1050 |
* route or we couldn't get the index of the interface. |
|
1051 |
*/ |
|
1052 |
return 0; |
|
1053 |
} |
|
1054 |
#endif |
|
1055 |
||
1056 |
||
1057 |
/* |
|
1058 |
* Wrapper for getsockopt system routine - does any necessary |
|
1059 |
* pre/post processing to deal with OS specific oddies :- |
|
1060 |
* |
|
1061 |
* IP_TOS is a no-op with IPv6 sockets as it's setup when |
|
1062 |
* the connection is established. |
|
1063 |
* |
|
1064 |
* On Linux the SO_SNDBUF/SO_RCVBUF values must be post-processed |
|
1065 |
* to compensate for an incorrect value returned by the kernel. |
|
1066 |
*/ |
|
1067 |
int |
|
1068 |
NET_GetSockOpt(int fd, int level, int opt, void *result, |
|
1069 |
int *len) |
|
1070 |
{ |
|
1071 |
int rv; |
|
1072 |
||
1073 |
#ifdef AF_INET6 |
|
1074 |
if ((level == IPPROTO_IP) && (opt == IP_TOS)) { |
|
1075 |
if (ipv6_available()) { |
|
1076 |
||
1077 |
/* |
|
1078 |
* For IPv6 socket option implemented at Java-level |
|
1079 |
* so return -1. |
|
1080 |
*/ |
|
1081 |
int *tc = (int *)result; |
|
1082 |
*tc = -1; |
|
1083 |
return 0; |
|
1084 |
} |
|
1085 |
} |
|
1086 |
#endif |
|
1087 |
||
911
9edd1b9607e2
6730740: Fix for 6729881 has apparently broken several 64 bit tests: "Bad address"
chegar
parents:
910
diff
changeset
|
1088 |
#ifdef __solaris__ |
9edd1b9607e2
6730740: Fix for 6729881 has apparently broken several 64 bit tests: "Bad address"
chegar
parents:
910
diff
changeset
|
1089 |
rv = getsockopt(fd, level, opt, result, len); |
9edd1b9607e2
6730740: Fix for 6729881 has apparently broken several 64 bit tests: "Bad address"
chegar
parents:
910
diff
changeset
|
1090 |
#else |
9edd1b9607e2
6730740: Fix for 6729881 has apparently broken several 64 bit tests: "Bad address"
chegar
parents:
910
diff
changeset
|
1091 |
{ |
9edd1b9607e2
6730740: Fix for 6729881 has apparently broken several 64 bit tests: "Bad address"
chegar
parents:
910
diff
changeset
|
1092 |
socklen_t socklen = *len; |
9edd1b9607e2
6730740: Fix for 6729881 has apparently broken several 64 bit tests: "Bad address"
chegar
parents:
910
diff
changeset
|
1093 |
rv = getsockopt(fd, level, opt, result, &socklen); |
9edd1b9607e2
6730740: Fix for 6729881 has apparently broken several 64 bit tests: "Bad address"
chegar
parents:
910
diff
changeset
|
1094 |
*len = socklen; |
9edd1b9607e2
6730740: Fix for 6729881 has apparently broken several 64 bit tests: "Bad address"
chegar
parents:
910
diff
changeset
|
1095 |
} |
9edd1b9607e2
6730740: Fix for 6729881 has apparently broken several 64 bit tests: "Bad address"
chegar
parents:
910
diff
changeset
|
1096 |
#endif |
9edd1b9607e2
6730740: Fix for 6729881 has apparently broken several 64 bit tests: "Bad address"
chegar
parents:
910
diff
changeset
|
1097 |
|
2 | 1098 |
if (rv < 0) { |
1099 |
return rv; |
|
1100 |
} |
|
1101 |
||
1102 |
#ifdef __linux__ |
|
1103 |
/* |
|
1104 |
* On Linux SO_SNDBUF/SO_RCVBUF aren't symmetric. This |
|
1105 |
* stems from additional socket structures in the send |
|
1106 |
* and receive buffers. |
|
1107 |
*/ |
|
1108 |
if ((level == SOL_SOCKET) && ((opt == SO_SNDBUF) |
|
1109 |
|| (opt == SO_RCVBUF))) { |
|
1110 |
int n = *((int *)result); |
|
1111 |
n /= 2; |
|
1112 |
*((int *)result) = n; |
|
1113 |
} |
|
1114 |
#endif |
|
1115 |
||
1116 |
return rv; |
|
1117 |
} |
|
1118 |
||
1119 |
||
1120 |
/* |
|
1121 |
* Wrapper for setsockopt system routine - performs any |
|
1122 |
* necessary pre/post processing to deal with OS specific |
|
1123 |
* issue :- |
|
1124 |
* |
|
1125 |
* On Solaris need to limit the suggested value for SO_SNDBUF |
|
1126 |
* and SO_RCVBUF to the kernel configured limit |
|
1127 |
* |
|
1128 |
* For IP_TOS socket option need to mask off bits as this |
|
1129 |
* aren't automatically masked by the kernel and results in |
|
1130 |
* an error. In addition IP_TOS is a noop with IPv6 as it |
|
1131 |
* should be setup as connection time. |
|
1132 |
*/ |
|
1133 |
int |
|
1134 |
NET_SetSockOpt(int fd, int level, int opt, const void *arg, |
|
1135 |
int len) |
|
1136 |
{ |
|
1137 |
#ifndef IPTOS_TOS_MASK |
|
1138 |
#define IPTOS_TOS_MASK 0x1e |
|
1139 |
#endif |
|
1140 |
#ifndef IPTOS_PREC_MASK |
|
1141 |
#define IPTOS_PREC_MASK 0xe0 |
|
1142 |
#endif |
|
1143 |
||
1144 |
/* |
|
1145 |
* IPPROTO/IP_TOS :- |
|
1146 |
* 1. IPv6 on Solaris: no-op and will be set in flowinfo |
|
1147 |
* field when connecting TCP socket, or sending |
|
1148 |
* UDP packet. |
|
1149 |
* 2. IPv6 on Linux: By default Linux ignores flowinfo |
|
1150 |
* field so enable IPV6_FLOWINFO_SEND so that flowinfo |
|
1151 |
* will be examined. |
|
1152 |
* 3. IPv4: set socket option based on ToS and Precedence |
|
1153 |
* fields (otherwise get invalid argument) |
|
1154 |
*/ |
|
1155 |
if (level == IPPROTO_IP && opt == IP_TOS) { |
|
1156 |
int *iptos; |
|
1157 |
||
1158 |
#if defined(AF_INET6) && defined(__solaris__) |
|
1159 |
if (ipv6_available()) { |
|
1160 |
return 0; |
|
1161 |
} |
|
1162 |
#endif |
|
1163 |
||
1164 |
#if defined(AF_INET6) && defined(__linux__) |
|
1165 |
if (ipv6_available()) { |
|
1166 |
int optval = 1; |
|
1167 |
return setsockopt(fd, IPPROTO_IPV6, IPV6_FLOWINFO_SEND, |
|
1168 |
(void *)&optval, sizeof(optval)); |
|
1169 |
} |
|
1170 |
#endif |
|
1171 |
||
1172 |
iptos = (int *)arg; |
|
1173 |
*iptos &= (IPTOS_TOS_MASK | IPTOS_PREC_MASK); |
|
1174 |
} |
|
1175 |
||
1176 |
/* |
|
1177 |
* SOL_SOCKET/{SO_SNDBUF,SO_RCVBUF} - On Solaris need to |
|
1178 |
* ensure that value is <= max_buf as otherwise we get |
|
1179 |
* an invalid argument. |
|
1180 |
*/ |
|
1181 |
#ifdef __solaris__ |
|
1182 |
if (level == SOL_SOCKET) { |
|
1183 |
if (opt == SO_SNDBUF || opt == SO_RCVBUF) { |
|
1184 |
int sotype, arglen; |
|
1185 |
int *bufsize, maxbuf; |
|
1186 |
||
1187 |
if (!init_max_buf) { |
|
1188 |
tcp_max_buf = getParam("/dev/tcp", "tcp_max_buf", 64*1024); |
|
1189 |
udp_max_buf = getParam("/dev/udp", "udp_max_buf", 64*1024); |
|
1190 |
init_max_buf = 1; |
|
1191 |
} |
|
1192 |
||
1193 |
arglen = sizeof(sotype); |
|
1194 |
if (getsockopt(fd, SOL_SOCKET, SO_TYPE, (void *)&sotype, |
|
1195 |
&arglen) < 0) { |
|
1196 |
return -1; |
|
1197 |
} |
|
1198 |
||
1199 |
maxbuf = (sotype == SOCK_STREAM) ? tcp_max_buf : udp_max_buf; |
|
1200 |
bufsize = (int *)arg; |
|
1201 |
if (*bufsize > maxbuf) { |
|
1202 |
*bufsize = maxbuf; |
|
1203 |
} |
|
1204 |
} |
|
1205 |
} |
|
1206 |
#endif |
|
1207 |
||
1208 |
/* |
|
1209 |
* On Linux the receive buffer is used for both socket |
|
1210 |
* structures and the the packet payload. The implication |
|
1211 |
* is that if SO_RCVBUF is too small then small packets |
|
1212 |
* must be discard. |
|
1213 |
*/ |
|
1214 |
#ifdef __linux__ |
|
1215 |
if (level == SOL_SOCKET && opt == SO_RCVBUF) { |
|
1216 |
int *bufsize = (int *)arg; |
|
1217 |
if (*bufsize < 1024) { |
|
1218 |
*bufsize = 1024; |
|
1219 |
} |
|
1220 |
} |
|
1221 |
#endif |
|
1222 |
||
1223 |
return setsockopt(fd, level, opt, arg, len); |
|
1224 |
} |
|
1225 |
||
1226 |
/* |
|
1227 |
* Wrapper for bind system call - performs any necessary pre/post |
|
1228 |
* processing to deal with OS specific issues :- |
|
1229 |
* |
|
1230 |
* Linux allows a socket to bind to 127.0.0.255 which must be |
|
1231 |
* caught. |
|
1232 |
* |
|
1233 |
* On Solaris 8/9 with IPv6 enabled we must use an exclusive |
|
1234 |
* bind to guaranteed a unique port number across the IPv4 and |
|
1235 |
* IPv6 port spaces. |
|
1236 |
* |
|
1237 |
*/ |
|
1238 |
int |
|
1239 |
NET_Bind(int fd, struct sockaddr *him, int len) |
|
1240 |
{ |
|
1241 |
#if defined(__solaris__) && defined(AF_INET6) |
|
1242 |
int level = -1; |
|
1243 |
int exclbind = -1; |
|
1244 |
#endif |
|
1245 |
int rv; |
|
1246 |
||
1247 |
#ifdef __linux__ |
|
1248 |
/* |
|
1249 |
* ## get bugId for this issue - goes back to 1.2.2 port ## |
|
1250 |
* ## When IPv6 is enabled this will be an IPv4-mapped |
|
1251 |
* ## with family set to AF_INET6 |
|
1252 |
*/ |
|
1253 |
if (him->sa_family == AF_INET) { |
|
1254 |
struct sockaddr_in *sa = (struct sockaddr_in *)him; |
|
1255 |
if ((ntohl(sa->sin_addr.s_addr) & 0x7f0000ff) == 0x7f0000ff) { |
|
1256 |
errno = EADDRNOTAVAIL; |
|
1257 |
return -1; |
|
1258 |
} |
|
1259 |
} |
|
1260 |
#endif |
|
1261 |
||
1262 |
#if defined(__solaris__) && defined(AF_INET6) |
|
1263 |
/* |
|
1264 |
* Solaris 8/9 have seperate IPv4 and IPv6 port spaces so we |
|
1265 |
* use an exclusive bind when SO_REUSEADDR is not used to |
|
1266 |
* give the illusion of a unified port space. |
|
1267 |
* This also avoid problems with IPv6 sockets connecting |
|
1268 |
* to IPv4 mapped addresses whereby the socket conversion |
|
1269 |
* results in a late bind that fails because the |
|
1270 |
* corresponding IPv4 port is in use. |
|
1271 |
*/ |
|
1272 |
if (ipv6_available()) { |
|
1273 |
int arg, len; |
|
1274 |
||
1275 |
len = sizeof(arg); |
|
1276 |
if (getsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&arg, |
|
1277 |
&len) == 0) { |
|
1278 |
if (arg == 0) { |
|
1279 |
/* |
|
1280 |
* SO_REUSEADDR is disabled so enable TCP_EXCLBIND or |
|
1281 |
* UDP_EXCLBIND |
|
1282 |
*/ |
|
1283 |
len = sizeof(arg); |
|
1284 |
if (getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&arg, |
|
1285 |
&len) == 0) { |
|
1286 |
if (arg == SOCK_STREAM) { |
|
1287 |
level = IPPROTO_TCP; |
|
1288 |
exclbind = TCP_EXCLBIND; |
|
1289 |
} else { |
|
1290 |
level = IPPROTO_UDP; |
|
1291 |
exclbind = UDP_EXCLBIND; |
|
1292 |
} |
|
1293 |
} |
|
1294 |
||
1295 |
arg = 1; |
|
1296 |
setsockopt(fd, level, exclbind, (char *)&arg, |
|
1297 |
sizeof(arg)); |
|
1298 |
} |
|
1299 |
} |
|
1300 |
} |
|
1301 |
||
1302 |
#endif |
|
1303 |
||
1304 |
rv = bind(fd, him, len); |
|
1305 |
||
1306 |
#if defined(__solaris__) && defined(AF_INET6) |
|
1307 |
if (rv < 0) { |
|
1308 |
int en = errno; |
|
1309 |
/* Restore *_EXCLBIND if the bind fails */ |
|
1310 |
if (exclbind != -1) { |
|
1311 |
int arg = 0; |
|
1312 |
setsockopt(fd, level, exclbind, (char *)&arg, |
|
1313 |
sizeof(arg)); |
|
1314 |
} |
|
1315 |
errno = en; |
|
1316 |
} |
|
1317 |
#endif |
|
1318 |
||
1319 |
return rv; |
|
1320 |
} |
|
1321 |
||
1322 |
/** |
|
1323 |
* Wrapper for select/poll with timeout on a single file descriptor. |
|
1324 |
* |
|
1325 |
* flags (defined in net_util_md.h can be any combination of |
|
1326 |
* NET_WAIT_READ, NET_WAIT_WRITE & NET_WAIT_CONNECT. |
|
1327 |
* |
|
1328 |
* The function will return when either the socket is ready for one |
|
1329 |
* of the specified operation or the timeout expired. |
|
1330 |
* |
|
1331 |
* It returns the time left from the timeout (possibly 0), or -1 if it expired. |
|
1332 |
*/ |
|
1333 |
||
1334 |
jint |
|
1335 |
NET_Wait(JNIEnv *env, jint fd, jint flags, jint timeout) |
|
1336 |
{ |
|
1337 |
jlong prevTime = JVM_CurrentTimeMillis(env, 0); |
|
1338 |
jint read_rv; |
|
1339 |
||
1340 |
while (1) { |
|
1341 |
jlong newTime; |
|
1342 |
#ifndef USE_SELECT |
|
1343 |
{ |
|
1344 |
struct pollfd pfd; |
|
1345 |
pfd.fd = fd; |
|
1346 |
pfd.events = 0; |
|
1347 |
if (flags & NET_WAIT_READ) |
|
1348 |
pfd.events |= POLLIN; |
|
1349 |
if (flags & NET_WAIT_WRITE) |
|
1350 |
pfd.events |= POLLOUT; |
|
1351 |
if (flags & NET_WAIT_CONNECT) |
|
1352 |
pfd.events |= POLLOUT; |
|
1353 |
||
1354 |
errno = 0; |
|
1355 |
read_rv = NET_Poll(&pfd, 1, timeout); |
|
1356 |
} |
|
1357 |
#else |
|
1358 |
{ |
|
1359 |
fd_set rd, wr, ex; |
|
1360 |
struct timeval t; |
|
1361 |
||
1362 |
t.tv_sec = timeout / 1000; |
|
1363 |
t.tv_usec = (timeout % 1000) * 1000; |
|
1364 |
||
1365 |
FD_ZERO(&rd); |
|
1366 |
FD_ZERO(&wr); |
|
1367 |
FD_ZERO(&ex); |
|
1368 |
if (flags & NET_WAIT_READ) { |
|
1369 |
FD_SET(fd, &rd); |
|
1370 |
} |
|
1371 |
if (flags & NET_WAIT_WRITE) { |
|
1372 |
FD_SET(fd, &wr); |
|
1373 |
} |
|
1374 |
if (flags & NET_WAIT_CONNECT) { |
|
1375 |
FD_SET(fd, &wr); |
|
1376 |
FD_SET(fd, &ex); |
|
1377 |
} |
|
1378 |
||
1379 |
errno = 0; |
|
1380 |
read_rv = NET_Select(fd+1, &rd, &wr, &ex, &t); |
|
1381 |
} |
|
1382 |
#endif |
|
1383 |
||
1384 |
newTime = JVM_CurrentTimeMillis(env, 0); |
|
1385 |
timeout -= (newTime - prevTime); |
|
1386 |
if (timeout <= 0) { |
|
1387 |
return read_rv > 0 ? 0 : -1; |
|
1388 |
} |
|
1389 |
newTime = prevTime; |
|
1390 |
||
1391 |
if (read_rv > 0) { |
|
1392 |
break; |
|
1393 |
} |
|
1394 |
||
1395 |
||
1396 |
} /* while */ |
|
1397 |
||
1398 |
return timeout; |
|
1399 |
} |