0N/A/*
3909N/A * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A#include <windows.h>
0N/A#include <winsock2.h>
0N/A#include <ctype.h>
0N/A#include <stdio.h>
0N/A#include <stdlib.h>
0N/A#include <malloc.h>
0N/A#include <sys/types.h>
453N/A#include <process.h>
0N/A
0N/A#include "java_net_InetAddress.h"
0N/A#include "java_net_Inet4AddressImpl.h"
0N/A#include "java_net_Inet6AddressImpl.h"
0N/A#include "net_util.h"
0N/A#include "icmp.h"
0N/A
0N/A#ifdef WIN32
0N/A#ifndef _WIN64
0N/A
0N/A/* Retain this code a little longer to support building in
0N/A * old environments. _MSC_VER is defined as:
0N/A * 1200 for MSVC++ 6.0
0N/A * 1310 for Vc7
0N/A */
0N/A#if defined(_MSC_VER) && _MSC_VER < 1310
0N/A#define sockaddr_in6 SOCKADDR_IN6
0N/A#endif
0N/A#endif
0N/A#define uint32_t UINT32
0N/A#endif
0N/A
0N/A/*
0N/A * Inet6AddressImpl
0N/A */
0N/A
0N/A/*
0N/A * Class: java_net_Inet6AddressImpl
0N/A * Method: getLocalHostName
0N/A * Signature: ()Ljava/lang/String;
0N/A */
0N/AJNIEXPORT jstring JNICALL
0N/AJava_java_net_Inet6AddressImpl_getLocalHostName (JNIEnv *env, jobject this) {
0N/A char hostname [256];
0N/A
0N/A if (gethostname (hostname, sizeof (hostname)) == -1) {
0N/A strcpy (hostname, "localhost");
0N/A }
0N/A return JNU_NewStringPlatform (env, hostname);
0N/A}
0N/A
0N/Astatic jclass ni_iacls;
0N/Astatic jclass ni_ia4cls;
0N/Astatic jclass ni_ia6cls;
0N/Astatic jmethodID ni_ia4ctrID;
0N/Astatic jmethodID ni_ia6ctrID;
0N/Astatic jfieldID ni_ia6ipaddressID;
0N/Astatic int initialized = 0;
0N/A
0N/AJNIEXPORT jobjectArray JNICALL
0N/AJava_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
0N/A jstring host) {
0N/A const char *hostname;
0N/A jobjectArray ret = 0;
0N/A int retLen = 0;
0N/A jboolean preferIPv6Address;
3877N/A static jfieldID ia_preferIPv6AddressID;
0N/A
0N/A int error=0;
0N/A struct addrinfo hints, *res, *resNew = NULL;
0N/A
0N/A if (!initialized) {
0N/A ni_iacls = (*env)->FindClass(env, "java/net/InetAddress");
0N/A ni_iacls = (*env)->NewGlobalRef(env, ni_iacls);
0N/A ni_ia4cls = (*env)->FindClass(env, "java/net/Inet4Address");
0N/A ni_ia4cls = (*env)->NewGlobalRef(env, ni_ia4cls);
0N/A ni_ia6cls = (*env)->FindClass(env, "java/net/Inet6Address");
0N/A ni_ia6cls = (*env)->NewGlobalRef(env, ni_ia6cls);
0N/A ni_ia4ctrID = (*env)->GetMethodID(env, ni_ia4cls, "<init>", "()V");
0N/A ni_ia6ctrID = (*env)->GetMethodID(env, ni_ia6cls, "<init>", "()V");
0N/A ni_ia6ipaddressID = (*env)->GetFieldID(env, ni_ia6cls, "ipaddress", "[B");
0N/A initialized = 1;
0N/A }
0N/A if (IS_NULL(host)) {
0N/A JNU_ThrowNullPointerException(env, "host is null");
0N/A return 0;
0N/A }
0N/A hostname = JNU_GetStringPlatformChars(env, host, JNI_FALSE);
0N/A CHECK_NULL_RETURN(hostname, NULL);
0N/A
3877N/A if (ia_preferIPv6AddressID == NULL) {
3877N/A jclass c = (*env)->FindClass(env,"java/net/InetAddress");
3877N/A if (c) {
3877N/A ia_preferIPv6AddressID =
3877N/A (*env)->GetStaticFieldID(env, c, "preferIPv6Address", "Z");
0N/A }
3877N/A if (ia_preferIPv6AddressID == NULL) {
0N/A JNU_ReleaseStringPlatformChars(env, host, hostname);
0N/A return NULL;
3877N/A }
3877N/A }
3877N/A /* get the address preference */
3877N/A preferIPv6Address
3877N/A = (*env)->GetStaticBooleanField(env, ia_class, ia_preferIPv6AddressID);
3877N/A
3877N/A /* Try once, with our static buffer. */
3877N/A memset(&hints, 0, sizeof(hints));
3877N/A hints.ai_flags = AI_CANONNAME;
3877N/A hints.ai_family = AF_UNSPEC;
3877N/A
3877N/A error = getaddrinfo(hostname, NULL, &hints, &res);
0N/A
3877N/A if (error) {
3877N/A /* report error */
3877N/A JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException",
3877N/A (char *)hostname);
3877N/A JNU_ReleaseStringPlatformChars(env, host, hostname);
3877N/A return NULL;
3877N/A } else {
3877N/A int i = 0;
3877N/A int inetCount = 0, inet6Count = 0, inetIndex, inet6Index;
3877N/A struct addrinfo *itr, *last, *iterator = res;
3877N/A while (iterator != NULL) {
3877N/A int skip = 0;
3877N/A itr = resNew;
3877N/A while (itr != NULL) {
3877N/A if (iterator->ai_family == itr->ai_family &&
3877N/A iterator->ai_addrlen == itr->ai_addrlen) {
3877N/A if (itr->ai_family == AF_INET) { /* AF_INET */
3877N/A struct sockaddr_in *addr1, *addr2;
3877N/A addr1 = (struct sockaddr_in *)iterator->ai_addr;
3877N/A addr2 = (struct sockaddr_in *)itr->ai_addr;
3877N/A if (addr1->sin_addr.s_addr ==
3877N/A addr2->sin_addr.s_addr) {
3877N/A skip = 1;
3877N/A break;
3877N/A }
3877N/A } else {
3877N/A int t;
3877N/A struct sockaddr_in6 *addr1, *addr2;
3877N/A addr1 = (struct sockaddr_in6 *)iterator->ai_addr;
3877N/A addr2 = (struct sockaddr_in6 *)itr->ai_addr;
3877N/A
3877N/A for (t = 0; t < 16; t++) {
3877N/A if (addr1->sin6_addr.s6_addr[t] !=
3877N/A addr2->sin6_addr.s6_addr[t]) {
0N/A break;
0N/A }
0N/A }
3877N/A if (t < 16) {
3877N/A itr = itr->ai_next;
3877N/A continue;
3877N/A } else {
3877N/A skip = 1;
3877N/A break;
3877N/A }
0N/A }
3877N/A } else if (iterator->ai_family != AF_INET &&
3877N/A iterator->ai_family != AF_INET6) {
3877N/A /* we can't handle other family types */
3877N/A skip = 1;
3877N/A break;
0N/A }
3877N/A itr = itr->ai_next;
0N/A }
0N/A
3877N/A if (!skip) {
3877N/A struct addrinfo *next
3877N/A = (struct addrinfo*) malloc(sizeof(struct addrinfo));
3877N/A if (!next) {
5476N/A JNU_ThrowOutOfMemoryError(env, "Native heap allocation failed");
0N/A ret = NULL;
0N/A goto cleanupAndReturn;
3877N/A }
3877N/A memcpy(next, iterator, sizeof(struct addrinfo));
3877N/A next->ai_next = NULL;
3877N/A if (resNew == NULL) {
3877N/A resNew = next;
3877N/A } else {
3877N/A last->ai_next = next;
3877N/A }
3877N/A last = next;
3877N/A i++;
3877N/A if (iterator->ai_family == AF_INET) {
3877N/A inetCount ++;
0N/A } else if (iterator->ai_family == AF_INET6) {
3877N/A inet6Count ++;
0N/A }
0N/A }
3877N/A iterator = iterator->ai_next;
3877N/A }
3877N/A retLen = i;
3877N/A iterator = resNew;
3877N/A i = 0;
3877N/A ret = (*env)->NewObjectArray(env, retLen, ni_iacls, NULL);
3877N/A
3877N/A if (IS_NULL(ret)) {
3877N/A /* we may have memory to free at the end of this */
3877N/A goto cleanupAndReturn;
3877N/A }
3877N/A
3877N/A if (preferIPv6Address) {
3877N/A inetIndex = inet6Count;
3877N/A inet6Index = 0;
3877N/A } else {
3877N/A inetIndex = 0;
3877N/A inet6Index = inetCount;
3877N/A }
3877N/A
3877N/A while (iterator != NULL) {
3877N/A if (iterator->ai_family == AF_INET) {
3877N/A jobject iaObj = (*env)->NewObject(env, ni_ia4cls, ni_ia4ctrID);
3877N/A if (IS_NULL(iaObj)) {
3877N/A ret = NULL;
3877N/A goto cleanupAndReturn;
3877N/A }
5888N/A setInetAddress_addr(env, iaObj, ntohl(((struct sockaddr_in*)iterator->ai_addr)->sin_addr.s_addr));
5888N/A setInetAddress_hostName(env, iaObj, host);
3877N/A (*env)->SetObjectArrayElement(env, ret, inetIndex, iaObj);
3877N/A inetIndex ++;
3877N/A } else if (iterator->ai_family == AF_INET6) {
3877N/A jint scope = 0;
3877N/A jbyteArray ipaddress;
3877N/A jobject iaObj = (*env)->NewObject(env, ni_ia6cls, ni_ia6ctrID);
3877N/A if (IS_NULL(iaObj)) {
3877N/A ret = NULL;
3877N/A goto cleanupAndReturn;
3877N/A }
3877N/A ipaddress = (*env)->NewByteArray(env, 16);
3877N/A if (IS_NULL(ipaddress)) {
3877N/A ret = NULL;
3877N/A goto cleanupAndReturn;
3877N/A }
3877N/A (*env)->SetByteArrayRegion(env, ipaddress, 0, 16,
3877N/A (jbyte *)&(((struct sockaddr_in6*)iterator->ai_addr)->sin6_addr));
3877N/A scope = ((struct sockaddr_in6*)iterator->ai_addr)->sin6_scope_id;
3877N/A if (scope != 0) { /* zero is default value, no need to set */
3877N/A (*env)->SetIntField(env, iaObj, ia6_scopeidID, scope);
3877N/A (*env)->SetBooleanField(env, iaObj, ia6_scopeidsetID, JNI_TRUE);
3877N/A }
3877N/A (*env)->SetObjectField(env, iaObj, ni_ia6ipaddressID, ipaddress);
5888N/A setInetAddress_hostName(env, iaObj, host);
3877N/A (*env)->SetObjectArrayElement(env, ret, inet6Index, iaObj);
3877N/A inet6Index ++;
3877N/A }
3877N/A iterator = iterator->ai_next;
0N/A }
0N/A }
0N/A
0N/AcleanupAndReturn:
0N/A {
0N/A struct addrinfo *iterator, *tmp;
0N/A iterator = resNew;
0N/A while (iterator != NULL) {
0N/A tmp = iterator;
0N/A iterator = iterator->ai_next;
0N/A free(tmp);
0N/A }
0N/A JNU_ReleaseStringPlatformChars(env, host, hostname);
0N/A }
0N/A
3877N/A freeaddrinfo(res);
0N/A
0N/A return ret;
0N/A}
0N/A
0N/A/*
0N/A * Class: java_net_Inet6AddressImpl
0N/A * Method: getHostByAddr
0N/A * Signature: (I)Ljava/lang/String;
0N/A */
0N/AJNIEXPORT jstring JNICALL
0N/AJava_java_net_Inet6AddressImpl_getHostByAddr(JNIEnv *env, jobject this,
0N/A jbyteArray addrArray) {
0N/A jstring ret = NULL;
0N/A
0N/A char host[NI_MAXHOST+1];
0N/A int error = 0;
0N/A int len = 0;
0N/A jbyte caddr[16];
0N/A
3877N/A struct sockaddr_in him4;
3877N/A struct sockaddr_in6 him6;
3877N/A struct sockaddr *sa;
0N/A
3877N/A /*
3877N/A * For IPv4 addresses construct a sockaddr_in structure.
3877N/A */
3877N/A if ((*env)->GetArrayLength(env, addrArray) == 4) {
3877N/A jint addr;
3877N/A (*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr);
3877N/A addr = ((caddr[0]<<24) & 0xff000000);
3877N/A addr |= ((caddr[1] <<16) & 0xff0000);
3877N/A addr |= ((caddr[2] <<8) & 0xff00);
3877N/A addr |= (caddr[3] & 0xff);
3877N/A memset((char *) &him4, 0, sizeof(him4));
3877N/A him4.sin_addr.s_addr = (uint32_t) htonl(addr);
3877N/A him4.sin_family = AF_INET;
3877N/A sa = (struct sockaddr *) &him4;
3877N/A len = sizeof(him4);
3877N/A } else {
0N/A /*
3877N/A * For IPv6 address construct a sockaddr_in6 structure.
0N/A */
3877N/A (*env)->GetByteArrayRegion(env, addrArray, 0, 16, caddr);
3877N/A memset((char *) &him6, 0, sizeof(him6));
3877N/A memcpy((void *)&(him6.sin6_addr), caddr, sizeof(struct in6_addr) );
3877N/A him6.sin6_family = AF_INET6;
3877N/A sa = (struct sockaddr *) &him6 ;
3877N/A len = sizeof(him6) ;
3877N/A }
0N/A
3877N/A error = getnameinfo(sa, len, host, NI_MAXHOST, NULL, 0, NI_NAMEREQD);
0N/A
3877N/A if (!error) {
3877N/A ret = (*env)->NewStringUTF(env, host);
0N/A }
0N/A
0N/A if (ret == NULL) {
0N/A JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", NULL);
0N/A }
0N/A
0N/A return ret;
0N/A}
0N/A
0N/A#ifdef AF_INET6
0N/A
0N/A
0N/A/**
0N/A * ping implementation.
0N/A * Send a ICMP_ECHO_REQUEST packet every second until either the timeout
0N/A * expires or a answer is received.
0N/A * Returns true is an ECHO_REPLY is received, otherwise, false.
0N/A */
0N/Astatic jboolean
0N/Aping6(JNIEnv *env, jint fd, struct SOCKADDR_IN6* him, jint timeout,
0N/A struct SOCKADDR_IN6* netif, jint ttl) {
0N/A jint size;
453N/A jint n, len, i;
0N/A char sendbuf[1500];
0N/A char auxbuf[1500];
0N/A unsigned char recvbuf[1500];
0N/A struct icmp6_hdr *icmp6;
0N/A struct SOCKADDR_IN6 sa_recv;
0N/A unsigned short pid, seq;
0N/A int read_rv = 0;
0N/A WSAEVENT hEvent;
0N/A struct ip6_pseudo_hdr *pseudo_ip6;
0N/A int timestamp;
0N/A int tmout2;
0N/A
0N/A /* Initialize the sequence number to a suitable random number and
0N/A shift right one place to allow sufficient room for increamenting. */
0N/A seq = ((unsigned short)rand()) >> 1;
0N/A
0N/A /* icmp_id is a 16 bit data type, therefore down cast the pid */
453N/A pid = (unsigned short) _getpid();
0N/A
0N/A size = 60*1024;
0N/A setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (const char *)&size, sizeof(size));
0N/A /**
0N/A * A TTL was specified, let's set the socket option.
0N/A */
0N/A if (ttl > 0) {
0N/A setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, (const char *) &ttl, sizeof(ttl));
0N/A }
0N/A
0N/A /**
0N/A * A network interface was specified, let's bind to it.
0N/A */
0N/A if (netif != NULL) {
0N/A if (NET_Bind(fd, (struct sockaddr*)netif, sizeof(struct sockaddr_in6)) < 0){
0N/A NET_ThrowNew(env, WSAGetLastError(), "Can't bind socket to interface");
0N/A closesocket(fd);
0N/A return JNI_FALSE;
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Make the socket non blocking
0N/A */
0N/A hEvent = WSACreateEvent();
0N/A WSAEventSelect(fd, hEvent, FD_READ|FD_CONNECT|FD_CLOSE);
0N/A
0N/A /**
0N/A * send 1 ICMP REQUEST every second until either we get a valid reply
0N/A * or the timeout expired.
0N/A */
0N/A do {
0N/A /* let's tag the ECHO packet with our pid so we can identify it */
0N/A timestamp = GetCurrentTime();
0N/A memset(sendbuf, 0, 1500);
0N/A icmp6 = (struct icmp6_hdr *) sendbuf;
0N/A icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
0N/A icmp6->icmp6_code = 0;
0N/A icmp6->icmp6_id = htons(pid);
0N/A icmp6->icmp6_seq = htons(seq);
0N/A icmp6->icmp6_cksum = 0;
0N/A memcpy((icmp6 + 1), &timestamp, sizeof(int));
0N/A if (netif != NULL) {
0N/A memset(auxbuf, 0, 1500);
0N/A pseudo_ip6 = (struct ip6_pseudo_hdr*) auxbuf;
0N/A memcpy(&pseudo_ip6->ip6_src, &netif->sin6_addr, sizeof(struct in6_addr));
0N/A memcpy(&pseudo_ip6->ip6_dst, &him->sin6_addr, sizeof(struct in6_addr));
0N/A pseudo_ip6->ip6_plen= htonl( 64 );
0N/A pseudo_ip6->ip6_nxt = htonl( IPPROTO_ICMPV6 );
0N/A memcpy(auxbuf + sizeof(struct ip6_pseudo_hdr), icmp6, 64);
0N/A /**
0N/A * We shouldn't have to do that as computing the checksum is supposed
0N/A * to be done by the IPv6 stack. Unfortunately windows, here too, is
0N/A * uterly broken, or non compliant, so let's do it.
0N/A * Problem is to compute the checksum I need to know the source address
0N/A * which happens only if I know the interface to be used...
0N/A */
0N/A icmp6->icmp6_cksum = in_cksum((u_short *)pseudo_ip6, sizeof(struct ip6_pseudo_hdr) + 64);
0N/A }
0N/A
0N/A /**
0N/A * Ping!
0N/A */
0N/A n = sendto(fd, sendbuf, 64, 0, (struct sockaddr*) him, sizeof(struct sockaddr_in6));
0N/A if (n < 0 && (WSAGetLastError() == WSAEINTR || WSAGetLastError() == WSAEADDRNOTAVAIL)) {
0N/A // Happens when using a "tunnel interface" for instance.
0N/A // Or trying to send a packet on a different scope.
0N/A closesocket(fd);
0N/A WSACloseEvent(hEvent);
0N/A return JNI_FALSE;
0N/A }
0N/A if (n < 0 && WSAGetLastError() != WSAEWOULDBLOCK) {
0N/A NET_ThrowNew(env, WSAGetLastError(), "Can't send ICMP packet");
0N/A closesocket(fd);
0N/A WSACloseEvent(hEvent);
0N/A return JNI_FALSE;
0N/A }
0N/A
0N/A tmout2 = timeout > 1000 ? 1000 : timeout;
0N/A do {
0N/A tmout2 = NET_Wait(env, fd, NET_WAIT_READ, tmout2);
0N/A
0N/A if (tmout2 >= 0) {
0N/A len = sizeof(sa_recv);
0N/A memset(recvbuf, 0, 1500);
0N/A /**
0N/A * For some unknown reason, besides plain stupidity, windows
0N/A * truncates the first 4 bytes of the icmpv6 header some we can't
0N/A * check for the ICMP_ECHOREPLY value.
0N/A * we'll check the other values, though
0N/A */
0N/A n = recvfrom(fd, recvbuf + 4, sizeof(recvbuf) - 4, 0, (struct sockaddr*) &sa_recv, &len);
0N/A icmp6 = (struct icmp6_hdr *) (recvbuf);
0N/A memcpy(&i, (icmp6 + 1), sizeof(int));
0N/A /**
0N/A * Is that the reply we were expecting?
0N/A */
0N/A if (n >= 8 && ntohs(icmp6->icmp6_seq) == seq &&
0N/A ntohs(icmp6->icmp6_id) == pid && i == timestamp) {
0N/A closesocket(fd);
0N/A WSACloseEvent(hEvent);
0N/A return JNI_TRUE;
0N/A }
0N/A }
0N/A } while (tmout2 > 0);
0N/A timeout -= 1000;
0N/A seq++;
0N/A } while (timeout > 0);
0N/A closesocket(fd);
0N/A WSACloseEvent(hEvent);
0N/A return JNI_FALSE;
0N/A}
0N/A#endif /* AF_INET6 */
0N/A
0N/A/*
0N/A * Class: java_net_Inet6AddressImpl
0N/A * Method: isReachable0
0N/A * Signature: ([bII[bI)Z
0N/A */
0N/AJNIEXPORT jboolean JNICALL
0N/AJava_java_net_Inet6AddressImpl_isReachable0(JNIEnv *env, jobject this,
0N/A jbyteArray addrArray,
0N/A jint scope,
0N/A jint timeout,
0N/A jbyteArray ifArray,
0N/A jint ttl, jint if_scope) {
0N/A#ifdef AF_INET6
0N/A jbyte caddr[16];
0N/A jint fd, sz;
0N/A struct sockaddr_in6 him6;
0N/A struct sockaddr_in6* netif = NULL;
0N/A struct sockaddr_in6 inf6;
0N/A WSAEVENT hEvent;
0N/A int len = 0;
0N/A int connect_rv = -1;
0N/A
0N/A /*
0N/A * If IPv6 is not enable, then we can't reach an IPv6 address, can we?
0N/A * Actually, we probably shouldn't even get here.
0N/A */
0N/A if (!ipv6_available()) {
0N/A return JNI_FALSE;
0N/A }
0N/A /*
0N/A * If it's an IPv4 address, ICMP won't work with IPv4 mapped address,
0N/A * therefore, let's delegate to the Inet4Address method.
0N/A */
0N/A sz = (*env)->GetArrayLength(env, addrArray);
0N/A if (sz == 4) {
0N/A return Java_java_net_Inet4AddressImpl_isReachable0(env, this,
0N/A addrArray,
0N/A timeout,
0N/A ifArray, ttl);
0N/A }
0N/A
0N/A memset((char *) caddr, 0, 16);
0N/A memset((char *) &him6, 0, sizeof(him6));
0N/A (*env)->GetByteArrayRegion(env, addrArray, 0, 16, caddr);
0N/A memcpy((void *)&(him6.sin6_addr), caddr, sizeof(struct in6_addr) );
0N/A him6.sin6_family = AF_INET6;
0N/A if (scope > 0) {
0N/A him6.sin6_scope_id = scope;
0N/A }
0N/A len = sizeof(struct sockaddr_in6);
0N/A /**
0N/A * A network interface was specified, let's convert the address
0N/A */
0N/A if (!(IS_NULL(ifArray))) {
0N/A memset((char *) caddr, 0, 16);
0N/A memset((char *) &inf6, 0, sizeof(inf6));
0N/A (*env)->GetByteArrayRegion(env, ifArray, 0, 16, caddr);
0N/A memcpy((void *)&(inf6.sin6_addr), caddr, sizeof(struct in6_addr) );
0N/A inf6.sin6_family = AF_INET6;
0N/A inf6.sin6_port = 0;
0N/A inf6.sin6_scope_id = if_scope;
0N/A netif = &inf6;
0N/A }
0N/A
0N/A#if 0
0N/A /*
0N/A * Windows implementation of ICMP & RAW sockets is too unreliable for now.
0N/A * Therefore it's best not to try it at all and rely only on TCP
0N/A * We may revisit and enable this code in the future.
0N/A */
0N/A
0N/A /*
0N/A * Right now, windows doesn't generate the ICMP checksum automatically
0N/A * so we have to compute it, but we can do it only if we know which
0N/A * interface will be used. Therefore, don't try to use ICMP if no
0N/A * interface was specified.
0N/A * When ICMPv6 support improves in windows, we may change this.
0N/A */
0N/A if (!(IS_NULL(ifArray))) {
0N/A /*
0N/A * If we can create a RAW socket, then when can use the ICMP ECHO_REQUEST
0N/A * otherwise we'll try a tcp socket to the Echo port (7).
0N/A * Note that this is empiric, and not connecting could mean it's blocked
0N/A * or the echo servioe has been disabled.
0N/A */
0N/A fd = NET_Socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
0N/A
0N/A if (fd != -1) { /* Good to go, let's do a ping */
0N/A return ping6(env, fd, &him6, timeout, netif, ttl);
0N/A }
0N/A }
0N/A#endif
0N/A
0N/A /* No good, let's fall back on TCP */
0N/A fd = NET_Socket(AF_INET6, SOCK_STREAM, 0);
0N/A if (fd == JVM_IO_ERR) {
0N/A /* note: if you run out of fds, you may not be able to load
0N/A * the exception class, and get a NoClassDefFoundError
0N/A * instead.
0N/A */
0N/A NET_ThrowNew(env, errno, "Can't create socket");
0N/A return JNI_FALSE;
0N/A }
0N/A
0N/A /**
0N/A * A TTL was specified, let's set the socket option.
0N/A */
0N/A if (ttl > 0) {
0N/A setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, (const char *)&ttl, sizeof(ttl));
0N/A }
0N/A
0N/A /**
0N/A * A network interface was specified, let's bind to it.
0N/A */
0N/A if (netif != NULL) {
0N/A if (NET_Bind(fd, (struct sockaddr*)netif, sizeof(struct sockaddr_in6)) < 0) {
0N/A NET_ThrowNew(env, WSAGetLastError(), "Can't bind socket to interface");
0N/A closesocket(fd);
0N/A return JNI_FALSE;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Make the socket non blocking.
0N/A */
0N/A hEvent = WSACreateEvent();
0N/A WSAEventSelect(fd, hEvent, FD_READ|FD_CONNECT|FD_CLOSE);
0N/A
0N/A /* no need to use NET_Connect as non-blocking */
0N/A him6.sin6_port = htons((short) 7); /* Echo port */
0N/A connect_rv = connect(fd, (struct sockaddr *)&him6, len);
0N/A
0N/A /**
0N/A * connection established or refused immediately, either way it means
0N/A * we were able to reach the host!
0N/A */
0N/A if (connect_rv == 0 || WSAGetLastError() == WSAECONNREFUSED) {
0N/A WSACloseEvent(hEvent);
0N/A closesocket(fd);
0N/A return JNI_TRUE;
0N/A } else {
0N/A int optlen;
0N/A
0N/A switch (WSAGetLastError()) {
0N/A case WSAEHOSTUNREACH: /* Host Unreachable */
0N/A case WSAENETUNREACH: /* Network Unreachable */
0N/A case WSAENETDOWN: /* Network is down */
0N/A case WSAEPFNOSUPPORT: /* Protocol Family unsupported */
0N/A WSACloseEvent(hEvent);
0N/A closesocket(fd);
0N/A return JNI_FALSE;
0N/A }
0N/A
0N/A if (WSAGetLastError() != WSAEWOULDBLOCK) {
0N/A NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ConnectException",
0N/A "connect failed");
0N/A WSACloseEvent(hEvent);
0N/A closesocket(fd);
0N/A return JNI_FALSE;
0N/A }
0N/A
0N/A timeout = NET_Wait(env, fd, NET_WAIT_CONNECT, timeout);
0N/A
0N/A if (timeout >= 0) {
0N/A /* has connection been established? */
0N/A optlen = sizeof(connect_rv);
3334N/A if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&connect_rv,
3334N/A &optlen) <0) {
0N/A connect_rv = WSAGetLastError();
0N/A }
0N/A
0N/A if (connect_rv == 0 || connect_rv == WSAECONNREFUSED) {
0N/A WSACloseEvent(hEvent);
0N/A closesocket(fd);
0N/A return JNI_TRUE;
0N/A }
0N/A }
0N/A }
0N/A WSACloseEvent(hEvent);
0N/A closesocket(fd);
0N/A#endif /* AF_INET6 */
0N/A return JNI_FALSE;
0N/A}