blob: 4038504149b4db1f677508b4d94b4a4833c189ea [file] [log] [blame]
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +00001/*
2 * Copyright (c) 2000, 2012, Oracle and/or its affiliates. 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26#include <errno.h>
27#include <sys/time.h>
28#include <sys/types.h>
29#include <sys/socket.h>
30#include <netinet/in.h>
31#include <netdb.h>
32#include <string.h>
33#include <strings.h>
34#include <stdlib.h>
35#include <ctype.h>
36#ifdef _ALLBSD_SOURCE
37#include <unistd.h> /* gethostname */
38#endif
39
40#include "jvm.h"
41#include "jni_util.h"
42#include "net_util.h"
43#ifndef IPV6_DEFS_H
44#include <netinet/icmp6.h>
45#endif
46
47#include "java_net_Inet4AddressImpl.h"
48#include "java_net_Inet6AddressImpl.h"
49
50/* the initial size of our hostent buffers */
51#ifndef NI_MAXHOST
52#define NI_MAXHOST 1025
53#endif
54
55
56/************************************************************************
57 * Inet6AddressImpl
58 */
59
60/*
61 * Class: java_net_Inet6AddressImpl
62 * Method: getLocalHostName
63 * Signature: ()Ljava/lang/String;
64 */
65JNIEXPORT jstring JNICALL
66Java_java_net_Inet6AddressImpl_getLocalHostName(JNIEnv *env, jobject this) {
67 char hostname[NI_MAXHOST+1];
68
69 hostname[0] = '\0';
70 if (JVM_GetHostName(hostname, sizeof(hostname))) {
71 /* Something went wrong, maybe networking is not setup? */
72 strcpy(hostname, "localhost");
73 } else {
74 // ensure null-terminated
75 hostname[NI_MAXHOST] = '\0';
76#if defined(__linux__) || defined(_ALLBSD_SOURCE)
77 /* On Linux/FreeBSD gethostname() says "host.domain.sun.com". On
78 * Solaris gethostname() says "host", so extra work is needed.
79 */
80#else
81 /* Solaris doesn't want to give us a fully qualified domain name.
82 * We do a reverse lookup to try and get one. This works
83 * if DNS occurs before NIS in /etc/resolv.conf, but fails
84 * if NIS comes first (it still gets only a partial name).
85 * We use thread-safe system calls.
86 */
87#ifdef AF_INET6
88 if (NET_addrtransAvailable()) {
89 struct addrinfo hints, *res;
90 int error;
91
92 bzero(&hints, sizeof(hints));
93 hints.ai_flags = AI_CANONNAME;
94 hints.ai_family = AF_UNSPEC;
95
96 error = (*getaddrinfo_ptr)(hostname, NULL, &hints, &res);
97
98 if (error == 0) {
99 /* host is known to name service */
100 error = (*getnameinfo_ptr)(res->ai_addr,
101 res->ai_addrlen,
102 hostname,
103 NI_MAXHOST,
104 NULL,
105 0,
106 NI_NAMEREQD);
107
108 /* if getnameinfo fails hostname is still the value
109 from gethostname */
110
111 (*freeaddrinfo_ptr)(res);
112 }
113 }
114#endif /* AF_INET6 */
115#endif /* __linux__ || _ALLBSD_SOURCE */
116 }
117 return (*env)->NewStringUTF(env, hostname);
118}
119
120static jclass ni_iacls;
121static jclass ni_ia4cls;
122static jclass ni_ia6cls;
123static jmethodID ni_ia4ctrID;
124static jmethodID ni_ia6ctrID;
125static jfieldID ni_ia6ipaddressID;
126static int initialized = 0;
127
128/*
129 * Find an internet address for a given hostname. Note that this
130 * code only works for addresses of type INET. The translation
131 * of %d.%d.%d.%d to an address (int) occurs in java now, so the
132 * String "host" shouldn't *ever* be a %d.%d.%d.%d string
133 *
134 * Class: java_net_Inet6AddressImpl
135 * Method: lookupAllHostAddr
136 * Signature: (Ljava/lang/String;)[[B
137 */
138
139JNIEXPORT jobjectArray JNICALL
140Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
141 jstring host) {
142 const char *hostname;
143 jobjectArray ret = 0;
144 int retLen = 0;
145 jboolean preferIPv6Address;
146
147 int error=0;
148#ifdef AF_INET6
149 struct addrinfo hints, *res, *resNew = NULL;
150#endif /* AF_INET6 */
151
152 if (!initialized) {
153 ni_iacls = (*env)->FindClass(env, "java/net/InetAddress");
154 ni_iacls = (*env)->NewGlobalRef(env, ni_iacls);
155 ni_ia4cls = (*env)->FindClass(env, "java/net/Inet4Address");
156 ni_ia4cls = (*env)->NewGlobalRef(env, ni_ia4cls);
157 ni_ia6cls = (*env)->FindClass(env, "java/net/Inet6Address");
158 ni_ia6cls = (*env)->NewGlobalRef(env, ni_ia6cls);
159 ni_ia4ctrID = (*env)->GetMethodID(env, ni_ia4cls, "<init>", "()V");
160 ni_ia6ctrID = (*env)->GetMethodID(env, ni_ia6cls, "<init>", "()V");
161 ni_ia6ipaddressID = (*env)->GetFieldID(env, ni_ia6cls, "ipaddress", "[B");
162 initialized = 1;
163 }
164
165 if (IS_NULL(host)) {
166 JNU_ThrowNullPointerException(env, "host is null");
167 return 0;
168 }
169 hostname = JNU_GetStringPlatformChars(env, host, JNI_FALSE);
170 CHECK_NULL_RETURN(hostname, NULL);
171
172#ifdef AF_INET6
173 if (NET_addrtransAvailable()) {
174 static jfieldID ia_preferIPv6AddressID;
175 if (ia_preferIPv6AddressID == NULL) {
176 jclass c = (*env)->FindClass(env,"java/net/InetAddress");
177 if (c) {
178 ia_preferIPv6AddressID =
179 (*env)->GetStaticFieldID(env, c, "preferIPv6Address", "Z");
180 }
181 if (ia_preferIPv6AddressID == NULL) {
182 JNU_ReleaseStringPlatformChars(env, host, hostname);
183 return NULL;
184 }
185 }
186 /* get the address preference */
187 preferIPv6Address
188 = (*env)->GetStaticBooleanField(env, ia_class, ia_preferIPv6AddressID);
189
190 /* Try once, with our static buffer. */
191 bzero(&hints, sizeof(hints));
192 hints.ai_flags = AI_CANONNAME;
193 hints.ai_family = AF_UNSPEC;
194
195#ifdef __solaris__
196 /*
197 * Workaround for Solaris bug 4160367 - if a hostname contains a
198 * white space then 0.0.0.0 is returned
199 */
200 if (isspace((unsigned char)hostname[0])) {
201 JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException",
202 hostname);
203 JNU_ReleaseStringPlatformChars(env, host, hostname);
204 return NULL;
205 }
206#endif
207
208 error = (*getaddrinfo_ptr)(hostname, NULL, &hints, &res);
209
210 if (error) {
211 /* report error */
212 ThrowUnknownHostExceptionWithGaiError(env, hostname, error);
213 JNU_ReleaseStringPlatformChars(env, host, hostname);
214 return NULL;
215 } else {
216 int i = 0;
217 int inetCount = 0, inet6Count = 0, inetIndex, inet6Index;
218 struct addrinfo *itr, *last = NULL, *iterator = res;
219 while (iterator != NULL) {
220 int skip = 0;
221 itr = resNew;
222 while (itr != NULL) {
223 if (iterator->ai_family == itr->ai_family &&
224 iterator->ai_addrlen == itr->ai_addrlen) {
225 if (itr->ai_family == AF_INET) { /* AF_INET */
226 struct sockaddr_in *addr1, *addr2;
227 addr1 = (struct sockaddr_in *)iterator->ai_addr;
228 addr2 = (struct sockaddr_in *)itr->ai_addr;
229 if (addr1->sin_addr.s_addr ==
230 addr2->sin_addr.s_addr) {
231 skip = 1;
232 break;
233 }
234 } else {
235 int t;
236 struct sockaddr_in6 *addr1, *addr2;
237 addr1 = (struct sockaddr_in6 *)iterator->ai_addr;
238 addr2 = (struct sockaddr_in6 *)itr->ai_addr;
239
240 for (t = 0; t < 16; t++) {
241 if (addr1->sin6_addr.s6_addr[t] !=
242 addr2->sin6_addr.s6_addr[t]) {
243 break;
244 }
245 }
246 if (t < 16) {
247 itr = itr->ai_next;
248 continue;
249 } else {
250 skip = 1;
251 break;
252 }
253 }
254 } else if (iterator->ai_family != AF_INET &&
255 iterator->ai_family != AF_INET6) {
256 /* we can't handle other family types */
257 skip = 1;
258 break;
259 }
260 itr = itr->ai_next;
261 }
262
263 if (!skip) {
264 struct addrinfo *next
265 = (struct addrinfo*) malloc(sizeof(struct addrinfo));
266 if (!next) {
267 JNU_ThrowOutOfMemoryError(env, "Native heap allocation failed");
268 ret = NULL;
269 goto cleanupAndReturn;
270 }
271 memcpy(next, iterator, sizeof(struct addrinfo));
272 next->ai_next = NULL;
273 if (resNew == NULL) {
274 resNew = next;
275 } else {
276 last->ai_next = next;
277 }
278 last = next;
279 i++;
280 if (iterator->ai_family == AF_INET) {
281 inetCount ++;
282 } else if (iterator->ai_family == AF_INET6) {
283 inet6Count ++;
284 }
285 }
286 iterator = iterator->ai_next;
287 }
288 retLen = i;
289 iterator = resNew;
290
291 ret = (*env)->NewObjectArray(env, retLen, ni_iacls, NULL);
292
293 if (IS_NULL(ret)) {
294 /* we may have memory to free at the end of this */
295 goto cleanupAndReturn;
296 }
297
298 if (preferIPv6Address) {
299 /* AF_INET addresses will be offset by inet6Count */
300 inetIndex = inet6Count;
301 inet6Index = 0;
302 } else {
303 /* AF_INET6 addresses will be offset by inetCount */
304 inetIndex = 0;
305 inet6Index = inetCount;
306 }
307
308 while (iterator != NULL) {
309 if (iterator->ai_family == AF_INET) {
310 jobject iaObj = (*env)->NewObject(env, ni_ia4cls, ni_ia4ctrID);
311 if (IS_NULL(iaObj)) {
312 ret = NULL;
313 goto cleanupAndReturn;
314 }
315 setInetAddress_addr(env, iaObj, ntohl(((struct sockaddr_in*)iterator->ai_addr)->sin_addr.s_addr));
316 setInetAddress_hostName(env, iaObj, host);
317 (*env)->SetObjectArrayElement(env, ret, inetIndex, iaObj);
318 inetIndex++;
319 } else if (iterator->ai_family == AF_INET6) {
320 jint scope = 0;
321 jbyteArray ipaddress;
322
323 jobject iaObj = (*env)->NewObject(env, ni_ia6cls, ni_ia6ctrID);
324 if (IS_NULL(iaObj)) {
325 ret = NULL;
326 goto cleanupAndReturn;
327 }
328 ipaddress = (*env)->NewByteArray(env, 16);
329 if (IS_NULL(ipaddress)) {
330 ret = NULL;
331 goto cleanupAndReturn;
332 }
333 (*env)->SetByteArrayRegion(env, ipaddress, 0, 16,
334 (jbyte *)&(((struct sockaddr_in6*)iterator->ai_addr)->sin6_addr));
335#ifdef __linux__
336 if (!kernelIsV22()) {
337 scope = ((struct sockaddr_in6*)iterator->ai_addr)->sin6_scope_id;
338 }
339#else
340 scope = ((struct sockaddr_in6*)iterator->ai_addr)->sin6_scope_id;
341#endif
342 if (scope != 0) { /* zero is default value, no need to set */
343 (*env)->SetIntField(env, iaObj, ia6_scopeidID, scope);
344 (*env)->SetBooleanField(env, iaObj, ia6_scopeidsetID, JNI_TRUE);
345 }
346 (*env)->SetObjectField(env, iaObj, ni_ia6ipaddressID, ipaddress);
347 setInetAddress_hostName(env, iaObj, host);
348 (*env)->SetObjectArrayElement(env, ret, inet6Index, iaObj);
349 inet6Index++;
350 }
351 iterator = iterator->ai_next;
352 }
353 }
354 }
355
356cleanupAndReturn:
357 {
358 struct addrinfo *iterator, *tmp;
359 iterator = resNew;
360 while (iterator != NULL) {
361 tmp = iterator;
362 iterator = iterator->ai_next;
363 free(tmp);
364 }
365 JNU_ReleaseStringPlatformChars(env, host, hostname);
366 }
367
368 if (NET_addrtransAvailable())
369 (*freeaddrinfo_ptr)(res);
370#endif /* AF_INET6 */
371
372 return ret;
373}
374
375/*
376 * Class: java_net_Inet6AddressImpl
377 * Method: getHostByAddr
378 * Signature: (I)Ljava/lang/String;
379 */
380JNIEXPORT jstring JNICALL
381Java_java_net_Inet6AddressImpl_getHostByAddr(JNIEnv *env, jobject this,
382 jbyteArray addrArray) {
383
384 jstring ret = NULL;
385
386#ifdef AF_INET6
387 char host[NI_MAXHOST+1];
388 int error = 0;
389 int len = 0;
390 jbyte caddr[16];
391
392 if (NET_addrtransAvailable()) {
393 struct sockaddr_in him4;
394 struct sockaddr_in6 him6;
395 struct sockaddr *sa;
396
397 /*
398 * For IPv4 addresses construct a sockaddr_in structure.
399 */
400 if ((*env)->GetArrayLength(env, addrArray) == 4) {
401 jint addr;
402 (*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr);
403 addr = ((caddr[0]<<24) & 0xff000000);
404 addr |= ((caddr[1] <<16) & 0xff0000);
405 addr |= ((caddr[2] <<8) & 0xff00);
406 addr |= (caddr[3] & 0xff);
407 memset((void *) &him4, 0, sizeof(him4));
408 him4.sin_addr.s_addr = (uint32_t) htonl(addr);
409 him4.sin_family = AF_INET;
410 sa = (struct sockaddr *) &him4;
411 len = sizeof(him4);
412 } else {
413 /*
414 * For IPv6 address construct a sockaddr_in6 structure.
415 */
416 (*env)->GetByteArrayRegion(env, addrArray, 0, 16, caddr);
417 memset((void *) &him6, 0, sizeof(him6));
418 memcpy((void *)&(him6.sin6_addr), caddr, sizeof(struct in6_addr) );
419 him6.sin6_family = AF_INET6;
420 sa = (struct sockaddr *) &him6 ;
421 len = sizeof(him6) ;
422 }
423
424 error = (*getnameinfo_ptr)(sa, len, host, NI_MAXHOST, NULL, 0,
425 NI_NAMEREQD);
426
427 if (!error) {
428 ret = (*env)->NewStringUTF(env, host);
429 }
430 }
431#endif /* AF_INET6 */
432
433 if (ret == NULL) {
434 JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", NULL);
435 }
436
437 return ret;
438}
439
440#define SET_NONBLOCKING(fd) { \
441 int flags = fcntl(fd, F_GETFL); \
442 flags |= O_NONBLOCK; \
443 fcntl(fd, F_SETFL, flags); \
444}
445
446#ifdef AF_INET6
447static jboolean
448ping6(JNIEnv *env, jint fd, struct sockaddr_in6* him, jint timeout,
449 struct sockaddr_in6* netif, jint ttl) {
450 jint size;
451 jint n;
452 socklen_t len;
453 char sendbuf[1500];
454 unsigned char recvbuf[1500];
455 struct icmp6_hdr *icmp6;
456 struct sockaddr_in6 sa_recv;
457 jbyte *caddr, *recv_caddr;
458 jchar pid;
459 jint tmout2, seq = 1;
460 struct timeval tv;
461 size_t plen;
462
463#ifdef __linux__
464 {
465 int csum_offset;
466 /**
467 * For some strange reason, the linux kernel won't calculate the
468 * checksum of ICMPv6 packets unless you set this socket option
469 */
470 csum_offset = 2;
471 setsockopt(fd, SOL_RAW, IPV6_CHECKSUM, &csum_offset, sizeof(int));
472 }
473#endif
474
475 caddr = (jbyte *)&(him->sin6_addr);
476
477 /* icmp_id is a 16 bit data type, therefore down cast the pid */
478 pid = (jchar)getpid();
479 size = 60*1024;
480 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
481 if (ttl > 0) {
482 setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl));
483 }
484 if (netif != NULL) {
485 if (bind(fd, (struct sockaddr*)netif, sizeof(struct sockaddr_in6)) <0) {
486 NET_ThrowNew(env, errno, "Can't bind socket");
487 close(fd);
488 return JNI_FALSE;
489 }
490 }
491 SET_NONBLOCKING(fd);
492
493 do {
494 icmp6 = (struct icmp6_hdr *) sendbuf;
495 icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
496 icmp6->icmp6_code = 0;
497 /* let's tag the ECHO packet with our pid so we can identify it */
498 icmp6->icmp6_id = htons(pid);
499 icmp6->icmp6_seq = htons(seq);
500 seq++;
501 icmp6->icmp6_cksum = 0;
502 gettimeofday(&tv, NULL);
503 memcpy(sendbuf + sizeof(struct icmp6_hdr), &tv, sizeof(tv));
504 plen = sizeof(struct icmp6_hdr) + sizeof(tv);
505 n = sendto(fd, sendbuf, plen, 0, (struct sockaddr*) him, sizeof(struct sockaddr_in6));
506 if (n < 0 && errno != EINPROGRESS) {
507#ifdef __linux__
508 if (errno != EINVAL && errno != EHOSTUNREACH)
509 /*
510 * On some Linuxes, when bound to the loopback interface, sendto
511 * will fail and errno will be set to EINVAL or EHOSTUNREACH.
512 * When that happens, don't throw an exception, just return false.
513 */
514#endif /*__linux__ */
515 NET_ThrowNew(env, errno, "Can't send ICMP packet");
516 close(fd);
517 return JNI_FALSE;
518 }
519
520 tmout2 = timeout > 1000 ? 1000 : timeout;
521 do {
522 tmout2 = NET_Wait(env, fd, NET_WAIT_READ, tmout2);
523
524 if (tmout2 >= 0) {
525 len = sizeof(sa_recv);
526 n = recvfrom(fd, recvbuf, sizeof(recvbuf), 0, (struct sockaddr*) &sa_recv, &len);
527 icmp6 = (struct icmp6_hdr *) (recvbuf);
528 recv_caddr = (jbyte *)&(sa_recv.sin6_addr);
529 /*
530 * We did receive something, but is it what we were expecting?
531 * I.E.: An ICMP6_ECHO_REPLY packet with the proper PID and
532 * from the host that we are trying to determine is reachable.
533 */
534 if (n >= 8 && icmp6->icmp6_type == ICMP6_ECHO_REPLY &&
535 (ntohs(icmp6->icmp6_id) == pid)) {
536 if (NET_IsEqual(caddr, recv_caddr)) {
537 close(fd);
538 return JNI_TRUE;
539 }
540 if (NET_IsZeroAddr(caddr)) {
541 close(fd);
542 return JNI_TRUE;
543 }
544 }
545 }
546 } while (tmout2 > 0);
547 timeout -= 1000;
548 } while (timeout > 0);
549 close(fd);
550 return JNI_FALSE;
551}
552#endif /* AF_INET6 */
553
554/*
555 * Class: java_net_Inet6AddressImpl
556 * Method: isReachable0
557 * Signature: ([bII[bI)Z
558 */
559JNIEXPORT jboolean JNICALL
560Java_java_net_Inet6AddressImpl_isReachable0(JNIEnv *env, jobject this,
561 jbyteArray addrArray,
562 jint scope,
563 jint timeout,
564 jbyteArray ifArray,
565 jint ttl, jint if_scope) {
566#ifdef AF_INET6
567 jbyte caddr[16];
568 jint fd, sz;
569 struct sockaddr_in6 him6;
570 struct sockaddr_in6 inf6;
571 struct sockaddr_in6* netif = NULL;
572 int len = 0;
573 int connect_rv = -1;
574
575 /*
576 * If IPv6 is not enable, then we can't reach an IPv6 address, can we?
577 */
578 if (!ipv6_available()) {
579 return JNI_FALSE;
580 }
581 /*
582 * If it's an IPv4 address, ICMP won't work with IPv4 mapped address,
583 * therefore, let's delegate to the Inet4Address method.
584 */
585 sz = (*env)->GetArrayLength(env, addrArray);
586 if (sz == 4) {
587 return Java_java_net_Inet4AddressImpl_isReachable0(env, this,
588 addrArray,
589 timeout,
590 ifArray, ttl);
591 }
592
593 memset((void *) caddr, 0, 16);
594 memset((void *) &him6, 0, sizeof(him6));
595 (*env)->GetByteArrayRegion(env, addrArray, 0, 16, caddr);
596 memcpy((void *)&(him6.sin6_addr), caddr, sizeof(struct in6_addr) );
597 him6.sin6_family = AF_INET6;
598#ifdef __linux__
599 if (scope > 0)
600 him6.sin6_scope_id = scope;
601 else
602 him6.sin6_scope_id = getDefaultIPv6Interface( &(him6.sin6_addr));
603 len = sizeof(struct sockaddr_in6);
604#else
605 if (scope > 0)
606 him6.sin6_scope_id = scope;
607 len = sizeof(struct sockaddr_in6);
608#endif
609 /*
610 * If a network interface was specified, let's create the address
611 * for it.
612 */
613 if (!(IS_NULL(ifArray))) {
614 memset((void *) caddr, 0, 16);
615 memset((void *) &inf6, 0, sizeof(inf6));
616 (*env)->GetByteArrayRegion(env, ifArray, 0, 16, caddr);
617 memcpy((void *)&(inf6.sin6_addr), caddr, sizeof(struct in6_addr) );
618 inf6.sin6_family = AF_INET6;
619 inf6.sin6_scope_id = if_scope;
620 netif = &inf6;
621 }
622 /*
623 * If we can create a RAW socket, then when can use the ICMP ECHO_REQUEST
624 * otherwise we'll try a tcp socket to the Echo port (7).
625 * Note that this is empiric, and not connecting could mean it's blocked
626 * or the echo servioe has been disabled.
627 */
628
629 fd = JVM_Socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
630
631 if (fd != -1) { /* Good to go, let's do a ping */
632 return ping6(env, fd, &him6, timeout, netif, ttl);
633 }
634
635 /* No good, let's fall back on TCP */
636 fd = JVM_Socket(AF_INET6, SOCK_STREAM, 0);
637 if (fd == JVM_IO_ERR) {
638 /* note: if you run out of fds, you may not be able to load
639 * the exception class, and get a NoClassDefFoundError
640 * instead.
641 */
642 NET_ThrowNew(env, errno, "Can't create socket");
643 return JNI_FALSE;
644 }
645 if (ttl > 0) {
646 setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl));
647 }
648
649 /*
650 * A network interface was specified, so let's bind to it.
651 */
652 if (netif != NULL) {
653 if (bind(fd, (struct sockaddr*)netif, sizeof(struct sockaddr_in6)) <0) {
654 NET_ThrowNew(env, errno, "Can't bind socket");
655 close(fd);
656 return JNI_FALSE;
657 }
658 }
659 SET_NONBLOCKING(fd);
660
661 /* no need to use NET_Connect as non-blocking */
662 him6.sin6_port = htons((short) 7); /* Echo port */
663 connect_rv = JVM_Connect(fd, (struct sockaddr *)&him6, len);
664
665 /**
666 * connection established or refused immediately, either way it means
667 * we were able to reach the host!
668 */
669 if (connect_rv == 0 || errno == ECONNREFUSED) {
670 close(fd);
671 return JNI_TRUE;
672 } else {
673 int optlen;
674
675 switch (errno) {
676 case ENETUNREACH: /* Network Unreachable */
677 case EAFNOSUPPORT: /* Address Family not supported */
678 case EADDRNOTAVAIL: /* address is not available on the remote machine */
679#ifdef __linux__
680 case EINVAL:
681 case EHOSTUNREACH:
682 /*
683 * On some Linuxes, when bound to the loopback interface, connect
684 * will fail and errno will be set to EINVAL or EHOSTUNREACH.
685 * When that happens, don't throw an exception, just return false.
686 */
687#endif /* __linux__ */
688 close(fd);
689 return JNI_FALSE;
690 }
691
692 if (errno != EINPROGRESS) {
693 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ConnectException",
694 "connect failed");
695 close(fd);
696 return JNI_FALSE;
697 }
698
699 timeout = NET_Wait(env, fd, NET_WAIT_CONNECT, timeout);
700
701 if (timeout >= 0) {
702 /* has connection been established */
703 optlen = sizeof(connect_rv);
704 if (JVM_GetSockOpt(fd, SOL_SOCKET, SO_ERROR, (void*)&connect_rv,
705 &optlen) <0) {
706 connect_rv = errno;
707 }
708 if (connect_rv == 0 || ECONNREFUSED) {
709 close(fd);
710 return JNI_TRUE;
711 }
712 }
713 close(fd);
714 return JNI_FALSE;
715 }
716#else /* AF_INET6 */
717 return JNI_FALSE;
718#endif /* AF_INET6 */
719}