8167457: Fixes for InetAddressImpl native coding on Windows

Reviewed-by: michaelm
This commit is contained in:
Christoph Langer 2017-02-06 10:47:14 +01:00
parent c8776e9797
commit 7a676b2b53
2 changed files with 305 additions and 524 deletions

View File

@ -29,60 +29,6 @@
#include "java_net_InetAddress.h" #include "java_net_InetAddress.h"
#include "java_net_Inet4AddressImpl.h" #include "java_net_Inet4AddressImpl.h"
/*
* Returns true if hostname is in dotted IP address format. Note that this
* function performs a syntax check only. For each octet it just checks that
* the octet is at most 3 digits.
*/
jboolean isDottedIPAddress(const char *hostname, unsigned int *addrp) {
char *c = (char *)hostname;
int octets = 0;
unsigned int cur = 0;
int digit_cnt = 0;
while (*c) {
if (*c == '.') {
if (digit_cnt == 0) {
return JNI_FALSE;
} else {
if (octets < 4) {
addrp[octets++] = cur;
cur = 0;
digit_cnt = 0;
} else {
return JNI_FALSE;
}
}
c++;
continue;
}
if ((*c < '0') || (*c > '9')) {
return JNI_FALSE;
}
digit_cnt++;
if (digit_cnt > 3) {
return JNI_FALSE;
}
/* don't check if current octet > 255 */
cur = cur*10 + (*c - '0');
/* Move onto next character and check for EOF */
c++;
if (*c == '\0') {
if (octets < 4) {
addrp[octets++] = cur;
} else {
return JNI_FALSE;
}
}
}
return (jboolean)(octets == 4);
}
/* /*
* Inet4AddressImpl * Inet4AddressImpl
*/ */
@ -93,17 +39,17 @@ jboolean isDottedIPAddress(const char *hostname, unsigned int *addrp) {
* Signature: ()Ljava/lang/String; * Signature: ()Ljava/lang/String;
*/ */
JNIEXPORT jstring JNICALL JNIEXPORT jstring JNICALL
Java_java_net_Inet4AddressImpl_getLocalHostName (JNIEnv *env, jobject this) { Java_java_net_Inet4AddressImpl_getLocalHostName(JNIEnv *env, jobject this) {
char hostname[256]; char hostname[256];
if (gethostname(hostname, sizeof hostname) == -1) { if (gethostname(hostname, sizeof(hostname)) == -1) {
strcpy(hostname, "localhost"); strcpy(hostname, "localhost");
} }
return JNU_NewStringPlatform(env, hostname); return JNU_NewStringPlatform(env, hostname);
} }
/* /*
* Find an internet address for a given hostname. Not this this * Find an internet address for a given hostname. Note that this
* code only works for addresses of type INET. The translation * code only works for addresses of type INET. The translation
* of %d.%d.%d.%d to an address (int) occurs in java now, so the * of %d.%d.%d.%d to an address (int) occurs in java now, so the
* String "host" shouldn't be a %d.%d.%d.%d string. The only * String "host" shouldn't be a %d.%d.%d.%d string. The only
@ -120,7 +66,6 @@ Java_java_net_Inet4AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
jobjectArray ret = NULL; jobjectArray ret = NULL;
const char *hostname; const char *hostname;
int error = 0; int error = 0;
unsigned int addr[4];
struct addrinfo hints, *res = NULL, *resNew = NULL, *last = NULL, struct addrinfo hints, *res = NULL, *resNew = NULL, *last = NULL,
*iterator; *iterator;
@ -134,57 +79,6 @@ Java_java_net_Inet4AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
hostname = JNU_GetStringPlatformChars(env, host, JNI_FALSE); hostname = JNU_GetStringPlatformChars(env, host, JNI_FALSE);
CHECK_NULL_RETURN(hostname, NULL); CHECK_NULL_RETURN(hostname, NULL);
/*
* The NT/2000 resolver tolerates a space in front of localhost. This
* is not consistent with other implementations of gethostbyname.
* In addition we must do a white space check on Solaris to avoid a
* bug whereby 0.0.0.0 is returned if any host name has a white space.
*/
if (isspace(hostname[0])) {
JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", hostname);
goto cleanupAndReturn;
}
/*
* If the format is x.x.x.x then don't use gethostbyname as Windows
* is unable to handle octets which are out of range.
*/
if (isDottedIPAddress(hostname, &addr[0])) {
unsigned int address;
jobject iaObj;
/*
* Are any of the octets out of range?
*/
if (addr[0] > 255 || addr[1] > 255 || addr[2] > 255 || addr[3] > 255) {
JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", hostname);
goto cleanupAndReturn;
}
/*
* Return an byte array with the populated address.
*/
address = (addr[3] << 24) & 0xff000000;
address |= (addr[2] << 16) & 0xff0000;
address |= (addr[1] << 8) & 0xff00;
address |= addr[0];
ret = (*env)->NewObjectArray(env, 1, ia_class, NULL);
if (IS_NULL(ret)) {
goto cleanupAndReturn;
}
iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
if (IS_NULL(iaObj)) {
ret = NULL;
goto cleanupAndReturn;
}
setInetAddress_addr(env, iaObj, ntohl(address));
(*env)->SetObjectArrayElement(env, ret, 0, iaObj);
goto cleanupAndReturn;
}
// try once, with our static buffer // try once, with our static buffer
memset(&hints, 0, sizeof(hints)); memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_CANONNAME; hints.ai_flags = AI_CANONNAME;
@ -193,6 +87,7 @@ Java_java_net_Inet4AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
error = getaddrinfo(hostname, NULL, &hints, &res); error = getaddrinfo(hostname, NULL, &hints, &res);
if (error) { if (error) {
// report error
NET_ThrowByNameWithLastError(env, "java/net/UnknownHostException", NET_ThrowByNameWithLastError(env, "java/net/UnknownHostException",
hostname); hostname);
goto cleanupAndReturn; goto cleanupAndReturn;
@ -311,145 +206,86 @@ Java_java_net_Inet4AddressImpl_getHostByAddr(JNIEnv *env, jobject this,
return ret; return ret;
} }
/**
* ping implementation using tcp port 7 (echo)
*/
static jboolean static jboolean
tcp_ping4(JNIEnv *env, tcp_ping4(JNIEnv *env, SOCKETADDRESS *sa, SOCKETADDRESS *netif, jint timeout,
jbyteArray addrArray,
jint timeout,
jbyteArray ifArray,
jint ttl) jint ttl)
{ {
jint addr;
jbyte caddr[4];
jint fd; jint fd;
struct sockaddr_in him;
struct sockaddr_in* netif = NULL;
struct sockaddr_in inf;
int len = 0;
WSAEVENT hEvent;
int connect_rv = -1; int connect_rv = -1;
int sz; WSAEVENT hEvent;
/** // open a TCP socket
* Convert IP address from byte array to integer
*/
sz = (*env)->GetArrayLength(env, addrArray);
if (sz != 4) {
return JNI_FALSE;
}
memset((char *) &him, 0, sizeof(him));
memset((char *) caddr, 0, sizeof(caddr));
(*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr);
addr = ((caddr[0]<<24) & 0xff000000);
addr |= ((caddr[1] <<16) & 0xff0000);
addr |= ((caddr[2] <<8) & 0xff00);
addr |= (caddr[3] & 0xff);
addr = htonl(addr);
/**
* Socket address
*/
him.sin_addr.s_addr = addr;
him.sin_family = AF_INET;
len = sizeof(him);
/**
* If a network interface was specified, let's convert its address
* as well.
*/
if (!(IS_NULL(ifArray))) {
memset((char *) caddr, 0, sizeof(caddr));
(*env)->GetByteArrayRegion(env, ifArray, 0, 4, caddr);
addr = ((caddr[0]<<24) & 0xff000000);
addr |= ((caddr[1] <<16) & 0xff0000);
addr |= ((caddr[2] <<8) & 0xff00);
addr |= (caddr[3] & 0xff);
addr = htonl(addr);
inf.sin_addr.s_addr = addr;
inf.sin_family = AF_INET;
inf.sin_port = 0;
netif = &inf;
}
/*
* Can't create a raw socket, so let's try a TCP socket
*/
fd = NET_Socket(AF_INET, SOCK_STREAM, 0); fd = NET_Socket(AF_INET, SOCK_STREAM, 0);
if (fd == -1) { if (fd == SOCKET_ERROR) {
/* note: if you run out of fds, you may not be able to load // note: if you run out of fds, you may not be able to load
* the exception class, and get a NoClassDefFoundError // the exception class, and get a NoClassDefFoundError instead.
* instead.
*/
NET_ThrowNew(env, WSAGetLastError(), "Can't create socket"); NET_ThrowNew(env, WSAGetLastError(), "Can't create socket");
return JNI_FALSE; return JNI_FALSE;
} }
// set TTL
if (ttl > 0) { if (ttl > 0) {
setsockopt(fd, IPPROTO_IP, IP_TTL, (const char *)&ttl, sizeof(ttl)); setsockopt(fd, IPPROTO_IP, IP_TTL, (const char *)&ttl, sizeof(ttl));
} }
/*
* A network interface was specified, so let's bind to it. // A network interface was specified, so let's bind to it.
*/
if (netif != NULL) { if (netif != NULL) {
if (bind(fd, (struct sockaddr*)netif, sizeof(struct sockaddr_in)) < 0) { if (bind(fd, &netif->sa, sizeof(struct sockaddr_in)) < 0) {
NET_ThrowNew(env, WSAGetLastError(), "Can't bind socket"); NET_ThrowNew(env, WSAGetLastError(), "Can't bind socket");
closesocket(fd); closesocket(fd);
return JNI_FALSE; return JNI_FALSE;
} }
} }
/* // Make the socket non blocking so we can use select/poll.
* Make the socket non blocking so we can use select/poll.
*/
hEvent = WSACreateEvent(); hEvent = WSACreateEvent();
WSAEventSelect(fd, hEvent, FD_READ|FD_CONNECT|FD_CLOSE); WSAEventSelect(fd, hEvent, FD_READ|FD_CONNECT|FD_CLOSE);
/* no need to use NET_Connect as non-blocking */ sa->sa4.sin_port = htons(7); // echo port
him.sin_port = htons(7); /* Echo */ connect_rv = connect(fd, &sa->sa, sizeof(struct sockaddr_in));
connect_rv = connect(fd, (struct sockaddr *)&him, len);
/** // connection established or refused immediately, either way it means
* connection established or refused immediately, either way it means // we were able to reach the host!
* we were able to reach the host!
*/
if (connect_rv == 0 || WSAGetLastError() == WSAECONNREFUSED) { if (connect_rv == 0 || WSAGetLastError() == WSAECONNREFUSED) {
WSACloseEvent(hEvent); WSACloseEvent(hEvent);
closesocket(fd); closesocket(fd);
return JNI_TRUE; return JNI_TRUE;
} else { }
int optlen;
switch (WSAGetLastError()) { switch (WSAGetLastError()) {
case WSAEHOSTUNREACH: /* Host Unreachable */ case WSAEHOSTUNREACH: // Host Unreachable
case WSAENETUNREACH: /* Network Unreachable */ case WSAENETUNREACH: // Network Unreachable
case WSAENETDOWN: /* Network is down */ case WSAENETDOWN: // Network is down
case WSAEPFNOSUPPORT: /* Protocol Family unsupported */ case WSAEPFNOSUPPORT: // Protocol Family unsupported
WSACloseEvent(hEvent);
closesocket(fd);
return JNI_FALSE;
case WSAEWOULDBLOCK: // this is expected as we'll probably have to wait
break;
default:
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ConnectException",
"connect failed");
WSACloseEvent(hEvent);
closesocket(fd);
return JNI_FALSE;
}
timeout = NET_Wait(env, fd, NET_WAIT_CONNECT, timeout);
if (timeout >= 0) {
// connection has been established, check for error condition
int optlen = sizeof(connect_rv);
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void *)&connect_rv,
&optlen) < 0)
{
connect_rv = WSAGetLastError();
}
if (connect_rv == 0 || connect_rv == WSAECONNREFUSED) {
WSACloseEvent(hEvent); WSACloseEvent(hEvent);
closesocket(fd); closesocket(fd);
return JNI_FALSE; return JNI_TRUE;
}
if (WSAGetLastError() != WSAEWOULDBLOCK) {
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ConnectException",
"connect failed");
WSACloseEvent(hEvent);
closesocket(fd);
return JNI_FALSE;
}
timeout = NET_Wait(env, fd, NET_WAIT_CONNECT, timeout);
/* has connection been established */
if (timeout >= 0) {
optlen = sizeof(connect_rv);
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&connect_rv,
&optlen) <0) {
connect_rv = WSAGetLastError();
}
if (connect_rv == 0 || connect_rv == WSAECONNREFUSED) {
WSACloseEvent(hEvent);
closesocket(fd);
return JNI_TRUE;
}
} }
} }
WSACloseEvent(hEvent); WSACloseEvent(hEvent);
@ -464,21 +300,17 @@ tcp_ping4(JNIEnv *env,
* Returns true is an ECHO_REPLY is received, otherwise, false. * Returns true is an ECHO_REPLY is received, otherwise, false.
*/ */
static jboolean static jboolean
ping4(JNIEnv *env, ping4(JNIEnv *env, HANDLE hIcmpFile, SOCKETADDRESS *sa,
unsigned long src_addr, SOCKETADDRESS *netif, jint timeout)
unsigned long dest_addr,
jint timeout,
HANDLE hIcmpFile)
{ {
// See https://msdn.microsoft.com/en-us/library/aa366050%28VS.85%29.aspx
DWORD dwRetVal = 0; DWORD dwRetVal = 0;
char SendData[32] = {0}; char SendData[32] = {0};
LPVOID ReplyBuffer = NULL; LPVOID ReplyBuffer = NULL;
DWORD ReplySize = 0; DWORD ReplySize = 0;
jboolean ret = JNI_FALSE; jboolean ret = JNI_FALSE;
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa366051%28v=vs.85%29.aspx // See https://msdn.microsoft.com/en-us/library/aa366050%28VS.85%29.aspx
// or https://msdn.microsoft.com/en-us/library/windows/desktop/aa366051%28v=vs.85%29.aspx
ReplySize = sizeof(ICMP_ECHO_REPLY) // The buffer should be large enough ReplySize = sizeof(ICMP_ECHO_REPLY) // The buffer should be large enough
// to hold at least one ICMP_ECHO_REPLY // to hold at least one ICMP_ECHO_REPLY
// structure // structure
@ -487,16 +319,16 @@ ping4(JNIEnv *env,
// to also hold 8 more bytes of data // to also hold 8 more bytes of data
// (the size of an ICMP error message) // (the size of an ICMP error message)
ReplyBuffer = (VOID*) malloc(ReplySize); ReplyBuffer = (VOID *)malloc(ReplySize);
if (ReplyBuffer == NULL) { if (ReplyBuffer == NULL) {
IcmpCloseHandle(hIcmpFile); IcmpCloseHandle(hIcmpFile);
NET_ThrowNew(env, WSAGetLastError(), "Unable to allocate memory"); NET_ThrowNew(env, WSAGetLastError(), "Unable to allocate memory");
return JNI_FALSE; return JNI_FALSE;
} }
if (src_addr == 0) { if (netif == NULL) {
dwRetVal = IcmpSendEcho(hIcmpFile, // HANDLE IcmpHandle, dwRetVal = IcmpSendEcho(hIcmpFile, // HANDLE IcmpHandle,
dest_addr, // IPAddr DestinationAddress, sa->sa4.sin_addr.s_addr, // IPAddr DestinationAddress,
SendData, // LPVOID RequestData, SendData, // LPVOID RequestData,
sizeof(SendData), // WORD RequestSize, sizeof(SendData), // WORD RequestSize,
NULL, // PIP_OPTION_INFORMATION RequestOptions, NULL, // PIP_OPTION_INFORMATION RequestOptions,
@ -506,20 +338,20 @@ ping4(JNIEnv *env,
// seem to have an undocumented minimum // seem to have an undocumented minimum
// timeout of 1000ms below which the // timeout of 1000ms below which the
// api behaves inconsistently. // api behaves inconsistently.
(timeout < 1000) ? 1000 : timeout); // DWORD Timeout (timeout < 1000) ? 1000 : timeout); // DWORD Timeout
} else { } else {
dwRetVal = IcmpSendEcho2Ex(hIcmpFile, // HANDLE IcmpHandle, dwRetVal = IcmpSendEcho2Ex(hIcmpFile, // HANDLE IcmpHandle,
NULL, // HANDLE Event NULL, // HANDLE Event
NULL, // PIO_APC_ROUTINE ApcRoutine NULL, // PIO_APC_ROUTINE ApcRoutine
NULL, // ApcContext NULL, // ApcContext
src_addr, // IPAddr SourceAddress, netif->sa4.sin_addr.s_addr, // IPAddr SourceAddress,
dest_addr, // IPAddr DestinationAddress, sa->sa4.sin_addr.s_addr, // IPAddr DestinationAddress,
SendData, // LPVOID RequestData, SendData, // LPVOID RequestData,
sizeof(SendData), // WORD RequestSize, sizeof(SendData), // WORD RequestSize,
NULL, // PIP_OPTION_INFORMATION RequestOptions, NULL, // PIP_OPTION_INFORMATION RequestOptions,
ReplyBuffer,// LPVOID ReplyBuffer, ReplyBuffer,// LPVOID ReplyBuffer,
ReplySize, // DWORD ReplySize, ReplySize, // DWORD ReplySize,
(timeout < 1000) ? 1000 : timeout); // DWORD Timeout (timeout < 1000) ? 1000 : timeout); // DWORD Timeout
} }
if (dwRetVal == 0) { // if the call failed if (dwRetVal == 0) { // if the call failed
@ -544,8 +376,8 @@ ping4(JNIEnv *env,
break; break;
default: default:
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&buf, 0, NULL); (LPTSTR)&buf, 0, NULL);
NET_ThrowNew(env, err, buf); NET_ThrowNew(env, err, buf);
LocalFree(buf); LocalFree(buf);
break; break;
@ -558,8 +390,8 @@ ping4(JNIEnv *env,
// We perform an extra check to make sure that our // We perform an extra check to make sure that our
// roundtrip time was less than our desired timeout // roundtrip time was less than our desired timeout
// for cases where that timeout is < 1000ms. // for cases where that timeout is < 1000ms.
if (pEchoReply->Status == IP_SUCCESS if (pEchoReply->Status == IP_SUCCESS &&
&& (int)pEchoReply->RoundTripTime <= timeout) (int)pEchoReply->RoundTripTime <= timeout)
{ {
ret = JNI_TRUE; ret = JNI_TRUE;
} }
@ -578,57 +410,58 @@ ping4(JNIEnv *env,
*/ */
JNIEXPORT jboolean JNICALL JNIEXPORT jboolean JNICALL
Java_java_net_Inet4AddressImpl_isReachable0(JNIEnv *env, jobject this, Java_java_net_Inet4AddressImpl_isReachable0(JNIEnv *env, jobject this,
jbyteArray addrArray, jbyteArray addrArray, jint timeout,
jint timeout, jbyteArray ifArray, jint ttl)
jbyteArray ifArray, {
jint ttl) {
jint src_addr = 0;
jint dest_addr = 0;
jbyte caddr[4]; jbyte caddr[4];
int sz; jint addr = 0, sz;
SOCKETADDRESS sa, inf, *netif = NULL;
HANDLE hIcmpFile; HANDLE hIcmpFile;
/** // check if address array size is 4 (IPv4 address)
* Convert IP address from byte array to integer
*/
sz = (*env)->GetArrayLength(env, addrArray); sz = (*env)->GetArrayLength(env, addrArray);
if (sz != 4) { if (sz != 4) {
return JNI_FALSE; return JNI_FALSE;
} }
memset((char *) caddr, 0, sizeof(caddr));
(*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr);
dest_addr = ((caddr[0]<<24) & 0xff000000);
dest_addr |= ((caddr[1] <<16) & 0xff0000);
dest_addr |= ((caddr[2] <<8) & 0xff00);
dest_addr |= (caddr[3] & 0xff);
dest_addr = htonl(dest_addr);
/** // convert IP address from byte array to integer
* If a network interface was specified, let's convert its address memset((char *)caddr, 0, sizeof(caddr));
* as well. (*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr);
*/ addr = ((caddr[0] << 24) & 0xff000000);
addr |= ((caddr[1] << 16) & 0xff0000);
addr |= ((caddr[2] << 8) & 0xff00);
addr |= (caddr[3] & 0xff);
memset((char *)&sa, 0, sizeof(SOCKETADDRESS));
sa.sa4.sin_addr.s_addr = htonl(addr);
sa.sa4.sin_family = AF_INET;
// If a network interface was specified, let's convert its address as well.
if (!(IS_NULL(ifArray))) { if (!(IS_NULL(ifArray))) {
memset((char *) caddr, 0, sizeof(caddr)); memset((char *)caddr, 0, sizeof(caddr));
(*env)->GetByteArrayRegion(env, ifArray, 0, 4, caddr); (*env)->GetByteArrayRegion(env, ifArray, 0, 4, caddr);
src_addr = ((caddr[0]<<24) & 0xff000000); addr = ((caddr[0] << 24) & 0xff000000);
src_addr |= ((caddr[1] <<16) & 0xff0000); addr |= ((caddr[1] << 16) & 0xff0000);
src_addr |= ((caddr[2] <<8) & 0xff00); addr |= ((caddr[2] << 8) & 0xff00);
src_addr |= (caddr[3] & 0xff); addr |= (caddr[3] & 0xff);
src_addr = htonl(src_addr); memset((char *)&inf, 0, sizeof(SOCKETADDRESS));
inf.sa4.sin_addr.s_addr = htonl(addr);
inf.sa4.sin_family = AF_INET;
netif = &inf;
} }
// Let's try to create an ICMP handle.
hIcmpFile = IcmpCreateFile(); hIcmpFile = IcmpCreateFile();
if (hIcmpFile == INVALID_HANDLE_VALUE) { if (hIcmpFile == INVALID_HANDLE_VALUE) {
int err = WSAGetLastError(); int err = WSAGetLastError();
if (err == ERROR_ACCESS_DENIED) { if (err == ERROR_ACCESS_DENIED) {
// fall back to TCP echo if access is denied to ICMP // fall back to TCP echo if access is denied to ICMP
return tcp_ping4(env, addrArray, timeout, ifArray, ttl); return tcp_ping4(env, &sa, netif, timeout, ttl);
} else { } else {
NET_ThrowNew(env, err, "Unable to create ICMP file handle"); NET_ThrowNew(env, err, "Unable to create ICMP file handle");
return JNI_FALSE; return JNI_FALSE;
} }
} else { } else {
return ping4(env, src_addr, dest_addr, timeout, hIcmpFile); // It didn't fail, so we can use ICMP.
return ping4(env, hIcmpFile, &sa, netif, timeout);
} }
} }

View File

@ -40,41 +40,40 @@
* Signature: ()Ljava/lang/String; * Signature: ()Ljava/lang/String;
*/ */
JNIEXPORT jstring JNICALL JNIEXPORT jstring JNICALL
Java_java_net_Inet6AddressImpl_getLocalHostName (JNIEnv *env, jobject this) { Java_java_net_Inet6AddressImpl_getLocalHostName(JNIEnv *env, jobject this) {
char hostname [256]; char hostname[256];
if (gethostname (hostname, sizeof (hostname)) == -1) { if (gethostname(hostname, sizeof(hostname)) == -1) {
strcpy (hostname, "localhost"); strcpy(hostname, "localhost");
} }
return JNU_NewStringPlatform (env, hostname); return JNU_NewStringPlatform(env, hostname);
} }
/*
* Class: java_net_Inet6AddressImpl
* Method: lookupAllHostAddr
* Signature: (Ljava/lang/String;)[[B
*/
JNIEXPORT jobjectArray JNICALL JNIEXPORT jobjectArray JNICALL
Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this, Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
jstring host) { jstring host) {
jobjectArray ret = NULL;
const char *hostname; const char *hostname;
jobjectArray ret = 0; int error = 0;
int retLen = 0; struct addrinfo hints, *res = NULL, *resNew = NULL, *last = NULL,
jboolean preferIPv6Address; *iterator;
int error=0;
struct addrinfo hints, *res = NULL, *resNew = NULL;
initInetAddressIDs(env); initInetAddressIDs(env);
JNU_CHECK_EXCEPTION_RETURN(env, NULL); JNU_CHECK_EXCEPTION_RETURN(env, NULL);
if (IS_NULL(host)) { if (IS_NULL(host)) {
JNU_ThrowNullPointerException(env, "host is null"); JNU_ThrowNullPointerException(env, "host argument is null");
return 0; return NULL;
} }
hostname = JNU_GetStringPlatformChars(env, host, JNI_FALSE); hostname = JNU_GetStringPlatformChars(env, host, JNI_FALSE);
CHECK_NULL_RETURN(hostname, NULL); CHECK_NULL_RETURN(hostname, NULL);
/* get the address preference */ // try once, with our static buffer
preferIPv6Address
= (*env)->GetStaticIntField(env, ia_class, ia_preferIPv6AddressID);
/* Try once, with our static buffer. */
memset(&hints, 0, sizeof(hints)); memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_CANONNAME; hints.ai_flags = AI_CANONNAME;
hints.ai_family = AF_UNSPEC; hints.ai_family = AF_UNSPEC;
@ -82,35 +81,28 @@ Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
error = getaddrinfo(hostname, NULL, &hints, &res); error = getaddrinfo(hostname, NULL, &hints, &res);
if (error) { if (error) {
if (WSAGetLastError() == WSATRY_AGAIN) { // report error
NET_ThrowByNameWithLastError(env, NET_ThrowByNameWithLastError(env, "java/net/UnknownHostException",
JNU_JAVANETPKG "UnknownHostException", hostname);
hostname); goto cleanupAndReturn;
JNU_ReleaseStringPlatformChars(env, host, hostname);
return NULL;
} else {
/* report error */
JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException",
(char *)hostname);
JNU_ReleaseStringPlatformChars(env, host, hostname);
return NULL;
}
} else { } else {
int i = 0; int i = 0, inetCount = 0, inet6Count = 0, inetIndex = 0,
int inetCount = 0, inet6Count = 0, inetIndex = 0, inet6Index = 0, originalIndex = 0; inet6Index = 0, originalIndex = 0;
struct addrinfo *itr, *last, *iterator = res; int addressPreference =
(*env)->GetStaticIntField(env, ia_class, ia_preferIPv6AddressID);
iterator = res;
while (iterator != NULL) { while (iterator != NULL) {
// skip duplicates
int skip = 0; int skip = 0;
itr = resNew; struct addrinfo *iteratorNew = resNew;
while (itr != NULL) { while (iteratorNew != NULL) {
if (iterator->ai_family == itr->ai_family && if (iterator->ai_family == iteratorNew->ai_family &&
iterator->ai_addrlen == itr->ai_addrlen) { iterator->ai_addrlen == iteratorNew->ai_addrlen) {
if (itr->ai_family == AF_INET) { /* AF_INET */ if (iteratorNew->ai_family == AF_INET) { /* AF_INET */
struct sockaddr_in *addr1, *addr2; struct sockaddr_in *addr1, *addr2;
addr1 = (struct sockaddr_in *)iterator->ai_addr; addr1 = (struct sockaddr_in *)iterator->ai_addr;
addr2 = (struct sockaddr_in *)itr->ai_addr; addr2 = (struct sockaddr_in *)iteratorNew->ai_addr;
if (addr1->sin_addr.s_addr == if (addr1->sin_addr.s_addr == addr2->sin_addr.s_addr) {
addr2->sin_addr.s_addr) {
skip = 1; skip = 1;
break; break;
} }
@ -118,7 +110,7 @@ Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
int t; int t;
struct sockaddr_in6 *addr1, *addr2; struct sockaddr_in6 *addr1, *addr2;
addr1 = (struct sockaddr_in6 *)iterator->ai_addr; addr1 = (struct sockaddr_in6 *)iterator->ai_addr;
addr2 = (struct sockaddr_in6 *)itr->ai_addr; addr2 = (struct sockaddr_in6 *)iteratorNew->ai_addr;
for (t = 0; t < 16; t++) { for (t = 0; t < 16; t++) {
if (addr1->sin6_addr.s6_addr[t] != if (addr1->sin6_addr.s6_addr[t] !=
@ -127,7 +119,7 @@ Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
} }
} }
if (t < 16) { if (t < 16) {
itr = itr->ai_next; iteratorNew = iteratorNew->ai_next;
continue; continue;
} else { } else {
skip = 1; skip = 1;
@ -136,16 +128,16 @@ Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
} }
} else if (iterator->ai_family != AF_INET && } else if (iterator->ai_family != AF_INET &&
iterator->ai_family != AF_INET6) { iterator->ai_family != AF_INET6) {
/* we can't handle other family types */ // we can't handle other family types
skip = 1; skip = 1;
break; break;
} }
itr = itr->ai_next; iteratorNew = iteratorNew->ai_next;
} }
if (!skip) { if (!skip) {
struct addrinfo *next struct addrinfo *next
= (struct addrinfo*) malloc(sizeof(struct addrinfo)); = (struct addrinfo *)malloc(sizeof(struct addrinfo));
if (!next) { if (!next) {
JNU_ThrowOutOfMemoryError(env, "Native heap allocation failed"); JNU_ThrowOutOfMemoryError(env, "Native heap allocation failed");
ret = NULL; ret = NULL;
@ -161,87 +153,81 @@ Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
last = next; last = next;
i++; i++;
if (iterator->ai_family == AF_INET) { if (iterator->ai_family == AF_INET) {
inetCount ++; inetCount++;
} else if (iterator->ai_family == AF_INET6) { } else if (iterator->ai_family == AF_INET6) {
inet6Count ++; inet6Count++;
} }
} }
iterator = iterator->ai_next; iterator = iterator->ai_next;
} }
retLen = i;
iterator = resNew;
i = 0;
ret = (*env)->NewObjectArray(env, retLen, ia_class, NULL);
// allocate array - at this point i contains the number of addresses
ret = (*env)->NewObjectArray(env, i, ia_class, NULL);
if (IS_NULL(ret)) { if (IS_NULL(ret)) {
/* we may have memory to free at the end of this */ /* we may have memory to free at the end of this */
goto cleanupAndReturn; goto cleanupAndReturn;
} }
if (preferIPv6Address == java_net_InetAddress_PREFER_IPV6_VALUE) { if (addressPreference == java_net_InetAddress_PREFER_IPV6_VALUE) {
inetIndex = inet6Count; inetIndex = inet6Count;
inet6Index = 0; inet6Index = 0;
} else if (preferIPv6Address == java_net_InetAddress_PREFER_IPV4_VALUE) { } else if (addressPreference == java_net_InetAddress_PREFER_IPV4_VALUE) {
inetIndex = 0; inetIndex = 0;
inet6Index = inetCount; inet6Index = inetCount;
} else if (preferIPv6Address == java_net_InetAddress_PREFER_SYSTEM_VALUE) { } else if (addressPreference == java_net_InetAddress_PREFER_SYSTEM_VALUE) {
inetIndex = inet6Index = originalIndex = 0; inetIndex = inet6Index = originalIndex = 0;
} }
iterator = resNew;
while (iterator != NULL) { while (iterator != NULL) {
if (iterator->ai_family == AF_INET) { if (iterator->ai_family == AF_INET) {
jobject iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID); jobject iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
if (IS_NULL(iaObj)) { if (IS_NULL(iaObj)) {
ret = NULL; ret = NULL;
goto cleanupAndReturn; goto cleanupAndReturn;
} }
setInetAddress_addr(env, iaObj, ntohl(((struct sockaddr_in*)iterator->ai_addr)->sin_addr.s_addr)); setInetAddress_addr(env, iaObj, ntohl(((struct sockaddr_in*)iterator->ai_addr)->sin_addr.s_addr));
setInetAddress_hostName(env, iaObj, host); setInetAddress_hostName(env, iaObj, host);
(*env)->SetObjectArrayElement(env, ret, (inetIndex | originalIndex), iaObj); (*env)->SetObjectArrayElement(env, ret, (inetIndex | originalIndex), iaObj);
inetIndex ++; inetIndex++;
} else if (iterator->ai_family == AF_INET6) { } else if (iterator->ai_family == AF_INET6) {
jint scope = 0; jint scope = 0;
jboolean ret1; jboolean ret1;
jobject iaObj = (*env)->NewObject(env, ia6_class, ia6_ctrID); jobject iaObj = (*env)->NewObject(env, ia6_class, ia6_ctrID);
if (IS_NULL(iaObj)) { if (IS_NULL(iaObj)) {
ret = NULL; ret = NULL;
goto cleanupAndReturn; goto cleanupAndReturn;
} }
ret1 = setInet6Address_ipaddress(env, iaObj, (jbyte *)&(((struct sockaddr_in6*)iterator->ai_addr)->sin6_addr)); ret1 = setInet6Address_ipaddress(env, iaObj, (char *)&(((struct sockaddr_in6*)iterator->ai_addr)->sin6_addr));
if (ret1 == JNI_FALSE) { if (ret1 == JNI_FALSE) {
ret = NULL; ret = NULL;
goto cleanupAndReturn; goto cleanupAndReturn;
} }
scope = ((struct sockaddr_in6*)iterator->ai_addr)->sin6_scope_id; scope = ((struct sockaddr_in6 *)iterator->ai_addr)->sin6_scope_id;
if (scope != 0) { /* zero is default value, no need to set */ if (scope != 0) { // zero is default value, no need to set
setInet6Address_scopeid(env, iaObj, scope); setInet6Address_scopeid(env, iaObj, scope);
} }
setInetAddress_hostName(env, iaObj, host); setInetAddress_hostName(env, iaObj, host);
(*env)->SetObjectArrayElement(env, ret, (inet6Index | originalIndex), iaObj); (*env)->SetObjectArrayElement(env, ret, (inet6Index | originalIndex), iaObj);
inet6Index ++; inet6Index++;
} }
if (preferIPv6Address == java_net_InetAddress_PREFER_SYSTEM_VALUE) { if (addressPreference == java_net_InetAddress_PREFER_SYSTEM_VALUE) {
originalIndex++; originalIndex++;
inetIndex = inet6Index = 0; inetIndex = inet6Index = 0;
} }
iterator = iterator->ai_next; iterator = iterator->ai_next;
} }
} }
cleanupAndReturn: cleanupAndReturn:
{ JNU_ReleaseStringPlatformChars(env, host, hostname);
struct addrinfo *iterator, *tmp; while (resNew != NULL) {
iterator = resNew; last = resNew;
while (iterator != NULL) { resNew = resNew->ai_next;
tmp = iterator; free(last);
iterator = iterator->ai_next; }
free(tmp); if (res != NULL) {
} freeaddrinfo(res);
JNU_ReleaseStringPlatformChars(env, host, hostname);
} }
freeaddrinfo(res);
return ret; return ret;
} }
@ -249,57 +235,48 @@ cleanupAndReturn:
* Class: java_net_Inet6AddressImpl * Class: java_net_Inet6AddressImpl
* Method: getHostByAddr * Method: getHostByAddr
* Signature: (I)Ljava/lang/String; * Signature: (I)Ljava/lang/String;
*
* Theoretically the UnknownHostException could be enriched with gai error
* information. But as it is silently ignored anyway, there's no need for this.
* It's only important that either a valid hostname is returned or an
* UnknownHostException is thrown.
*/ */
JNIEXPORT jstring JNICALL JNIEXPORT jstring JNICALL
Java_java_net_Inet6AddressImpl_getHostByAddr(JNIEnv *env, jobject this, Java_java_net_Inet6AddressImpl_getHostByAddr(JNIEnv *env, jobject this,
jbyteArray addrArray) { jbyteArray addrArray) {
jstring ret = NULL; jstring ret = NULL;
char host[NI_MAXHOST + 1];
char host[NI_MAXHOST+1];
int error = 0;
int len = 0; int len = 0;
jbyte caddr[16]; jbyte caddr[16];
SOCKETADDRESS sa;
struct sockaddr_in him4; memset((void *)&sa, 0, sizeof(SOCKETADDRESS));
struct sockaddr_in6 him6;
struct sockaddr *sa;
/* // construct a sockaddr_in structure (AF_INET or AF_INET6)
* For IPv4 addresses construct a sockaddr_in structure.
*/
if ((*env)->GetArrayLength(env, addrArray) == 4) { if ((*env)->GetArrayLength(env, addrArray) == 4) {
jint addr; jint addr;
(*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr); (*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr);
addr = ((caddr[0]<<24) & 0xff000000); addr = ((caddr[0] << 24) & 0xff000000);
addr |= ((caddr[1] <<16) & 0xff0000); addr |= ((caddr[1] << 16) & 0xff0000);
addr |= ((caddr[2] <<8) & 0xff00); addr |= ((caddr[2] << 8) & 0xff00);
addr |= (caddr[3] & 0xff); addr |= (caddr[3] & 0xff);
memset((char *) &him4, 0, sizeof(him4)); sa.sa4.sin_addr.s_addr = htonl(addr);
him4.sin_addr.s_addr = htonl(addr); sa.sa4.sin_family = AF_INET;
him4.sin_family = AF_INET; len = sizeof(struct sockaddr_in);
sa = (struct sockaddr *) &him4;
len = sizeof(him4);
} else { } else {
/*
* For IPv6 address construct a sockaddr_in6 structure.
*/
(*env)->GetByteArrayRegion(env, addrArray, 0, 16, caddr); (*env)->GetByteArrayRegion(env, addrArray, 0, 16, caddr);
memset((char *) &him6, 0, sizeof(him6)); memcpy((void *)&sa.sa6.sin6_addr, caddr, sizeof(struct in6_addr));
memcpy((void *)&(him6.sin6_addr), caddr, sizeof(struct in6_addr) ); sa.sa6.sin6_family = AF_INET6;
him6.sin6_family = AF_INET6; len = sizeof(struct sockaddr_in6);
sa = (struct sockaddr *) &him6 ;
len = sizeof(him6) ;
} }
error = getnameinfo(sa, len, host, NI_MAXHOST, NULL, 0, NI_NAMEREQD); if (getnameinfo(&sa.sa, len, host, NI_MAXHOST, NULL, 0, NI_NAMEREQD)) {
JNU_ThrowByName(env, "java/net/UnknownHostException", NULL);
if (!error) { } else {
ret = (*env)->NewStringUTF(env, host); ret = (*env)->NewStringUTF(env, host);
CHECK_NULL_RETURN(ret, NULL); if (ret == NULL) {
} JNU_ThrowByName(env, "java/net/UnknownHostException", NULL);
}
if (ret == NULL) {
JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", NULL);
} }
return ret; return ret;
@ -309,99 +286,82 @@ Java_java_net_Inet6AddressImpl_getHostByAddr(JNIEnv *env, jobject this,
* ping implementation using tcp port 7 (echo) * ping implementation using tcp port 7 (echo)
*/ */
static jboolean static jboolean
tcp_ping6(JNIEnv *env, tcp_ping6(JNIEnv *env, SOCKETADDRESS *sa, SOCKETADDRESS *netif, jint timeout,
jint timeout, jint ttl)
jint ttl,
struct sockaddr_in6 him6,
struct sockaddr_in6* netif,
int len)
{ {
jint fd; jint fd;
WSAEVENT hEvent;
int connect_rv = -1; int connect_rv = -1;
WSAEVENT hEvent;
// open a TCP socket
fd = NET_Socket(AF_INET6, SOCK_STREAM, 0); fd = NET_Socket(AF_INET6, SOCK_STREAM, 0);
if (fd == SOCKET_ERROR) { if (fd == SOCKET_ERROR) {
/* note: if you run out of fds, you may not be able to load // note: if you run out of fds, you may not be able to load
* the exception class, and get a NoClassDefFoundError // the exception class, and get a NoClassDefFoundError instead.
* instead. NET_ThrowNew(env, WSAGetLastError(), "Can't create socket");
*/
NET_ThrowNew(env, errno, "Can't create socket");
return JNI_FALSE; return JNI_FALSE;
} }
/** // set TTL
* A TTL was specified, let's set the socket option.
*/
if (ttl > 0) { if (ttl > 0) {
setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, (const char *)&ttl, sizeof(ttl)); setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, (const char *)&ttl, sizeof(ttl));
} }
/** // A network interface was specified, so let's bind to it.
* A network interface was specified, let's bind to it.
*/
if (netif != NULL) { if (netif != NULL) {
if (NET_Bind(fd, (struct sockaddr*)netif, sizeof(struct sockaddr_in6)) < 0) { if (bind(fd, &netif->sa, sizeof(struct sockaddr_in6)) < 0) {
NET_ThrowNew(env, WSAGetLastError(), "Can't bind socket to interface"); NET_ThrowNew(env, WSAGetLastError(), "Can't bind socket to interface");
closesocket(fd); closesocket(fd);
return JNI_FALSE; return JNI_FALSE;
} }
} }
/** // Make the socket non blocking so we can use select/poll.
* Make the socket non blocking.
*/
hEvent = WSACreateEvent(); hEvent = WSACreateEvent();
WSAEventSelect(fd, hEvent, FD_READ|FD_CONNECT|FD_CLOSE); WSAEventSelect(fd, hEvent, FD_READ|FD_CONNECT|FD_CLOSE);
/* no need to use NET_Connect as non-blocking */ sa->sa6.sin6_port = htons(7); // echo port
him6.sin6_port = htons((short) 7); /* Echo port */ connect_rv = connect(fd, &sa->sa, sizeof(struct sockaddr_in6));
connect_rv = connect(fd, (struct sockaddr *)&him6, len);
/** // connection established or refused immediately, either way it means
* connection established or refused immediately, either way it means // we were able to reach the host!
* we were able to reach the host!
*/
if (connect_rv == 0 || WSAGetLastError() == WSAECONNREFUSED) { if (connect_rv == 0 || WSAGetLastError() == WSAECONNREFUSED) {
WSACloseEvent(hEvent); WSACloseEvent(hEvent);
closesocket(fd); closesocket(fd);
return JNI_TRUE; return JNI_TRUE;
} else { }
int optlen;
switch (WSAGetLastError()) { switch (WSAGetLastError()) {
case WSAEHOSTUNREACH: /* Host Unreachable */ case WSAEHOSTUNREACH: // Host Unreachable
case WSAENETUNREACH: /* Network Unreachable */ case WSAENETUNREACH: // Network Unreachable
case WSAENETDOWN: /* Network is down */ case WSAENETDOWN: // Network is down
case WSAEPFNOSUPPORT: /* Protocol Family unsupported */ case WSAEPFNOSUPPORT: // Protocol Family unsupported
WSACloseEvent(hEvent); WSACloseEvent(hEvent);
closesocket(fd); closesocket(fd);
return JNI_FALSE; return JNI_FALSE;
} case WSAEWOULDBLOCK: // this is expected as we'll probably have to wait
break;
default:
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ConnectException",
"connect failed");
WSACloseEvent(hEvent);
closesocket(fd);
return JNI_FALSE;
}
if (WSAGetLastError() != WSAEWOULDBLOCK) { timeout = NET_Wait(env, fd, NET_WAIT_CONNECT, timeout);
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ConnectException", if (timeout >= 0) {
"connect failed"); // connection has been established, check for error condition
WSACloseEvent(hEvent); int optlen = sizeof(connect_rv);
closesocket(fd); if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void *)&connect_rv,
return JNI_FALSE; &optlen) < 0)
} {
timeout = NET_Wait(env, fd, NET_WAIT_CONNECT, timeout);
if (timeout >= 0) {
/* has connection been established? */
optlen = sizeof(connect_rv);
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&connect_rv,
&optlen) <0) {
connect_rv = WSAGetLastError(); connect_rv = WSAGetLastError();
} }
if (connect_rv == 0 || connect_rv == WSAECONNREFUSED) {
if (connect_rv == 0 || connect_rv == WSAECONNREFUSED) {
WSACloseEvent(hEvent); WSACloseEvent(hEvent);
closesocket(fd); closesocket(fd);
return JNI_TRUE; return JNI_TRUE;
}
} }
} }
WSACloseEvent(hEvent); WSACloseEvent(hEvent);
@ -416,21 +376,18 @@ tcp_ping6(JNIEnv *env,
* Returns true is an ECHO_REPLY is received, otherwise, false. * Returns true is an ECHO_REPLY is received, otherwise, false.
*/ */
static jboolean static jboolean
ping6(JNIEnv *env, ping6(JNIEnv *env, HANDLE hIcmpFile, SOCKETADDRESS *sa,
struct sockaddr_in6* src, SOCKETADDRESS *netif, jint timeout)
struct sockaddr_in6* dest,
jint timeout,
HANDLE hIcmpFile)
{ {
DWORD dwRetVal = 0; DWORD dwRetVal = 0;
char SendData[32] = {0}; char SendData[32] = {0};
LPVOID ReplyBuffer = NULL; LPVOID ReplyBuffer = NULL;
DWORD ReplySize = 0; DWORD ReplySize = 0;
IP_OPTION_INFORMATION ipInfo = {255, 0, 0, 0, NULL}; IP_OPTION_INFORMATION ipInfo = {255, 0, 0, 0, NULL};
struct sockaddr_in6 sa6Source; SOCKETADDRESS dftNetif;
ReplySize = sizeof(ICMPV6_ECHO_REPLY) + sizeof(SendData); ReplySize = sizeof(ICMPV6_ECHO_REPLY) + sizeof(SendData);
ReplyBuffer = (VOID*) malloc(ReplySize); ReplyBuffer = (VOID *)malloc(ReplySize);
if (ReplyBuffer == NULL) { if (ReplyBuffer == NULL) {
IcmpCloseHandle(hIcmpFile); IcmpCloseHandle(hIcmpFile);
NET_ThrowNew(env, WSAGetLastError(), "Unable to allocate memory"); NET_ThrowNew(env, WSAGetLastError(), "Unable to allocate memory");
@ -438,17 +395,20 @@ ping6(JNIEnv *env,
} }
//define local source information //define local source information
sa6Source.sin6_addr = in6addr_any; if (netif == NULL) {
sa6Source.sin6_family = AF_INET6; dftNetif.sa6.sin6_addr = in6addr_any;
sa6Source.sin6_flowinfo = 0; dftNetif.sa6.sin6_family = AF_INET6;
sa6Source.sin6_port = 0; dftNetif.sa6.sin6_flowinfo = 0;
dftNetif.sa6.sin6_port = 0;
netif = &dftNetif;
}
dwRetVal = Icmp6SendEcho2(hIcmpFile, // HANDLE IcmpHandle, dwRetVal = Icmp6SendEcho2(hIcmpFile, // HANDLE IcmpHandle,
NULL, // HANDLE Event, NULL, // HANDLE Event,
NULL, // PIO_APC_ROUTINE ApcRoutine, NULL, // PIO_APC_ROUTINE ApcRoutine,
NULL, // PVOID ApcContext, NULL, // PVOID ApcContext,
&sa6Source, // struct sockaddr_in6 *SourceAddress, &netif->sa6, // struct sockaddr_in6 *SourceAddress,
dest, // struct sockaddr_in6 *DestinationAddress, &sa->sa6, // struct sockaddr_in6 *DestinationAddress,
SendData, // LPVOID RequestData, SendData, // LPVOID RequestData,
sizeof(SendData), // WORD RequestSize, sizeof(SendData), // WORD RequestSize,
&ipInfo, // PIP_OPTION_INFORMATION RequestOptions, &ipInfo, // PIP_OPTION_INFORMATION RequestOptions,
@ -459,11 +419,10 @@ ping6(JNIEnv *env,
free(ReplyBuffer); free(ReplyBuffer);
IcmpCloseHandle(hIcmpFile); IcmpCloseHandle(hIcmpFile);
if (dwRetVal == 0) { // if the call failed
if (dwRetVal != 0) {
return JNI_TRUE;
} else {
return JNI_FALSE; return JNI_FALSE;
} else {
return JNI_TRUE;
} }
} }
@ -474,75 +433,64 @@ ping6(JNIEnv *env,
*/ */
JNIEXPORT jboolean JNICALL JNIEXPORT jboolean JNICALL
Java_java_net_Inet6AddressImpl_isReachable0(JNIEnv *env, jobject this, Java_java_net_Inet6AddressImpl_isReachable0(JNIEnv *env, jobject this,
jbyteArray addrArray, jbyteArray addrArray, jint scope,
jint scope, jint timeout, jbyteArray ifArray,
jint timeout, jint ttl, jint if_scope)
jbyteArray ifArray, {
jint ttl, jint if_scope) {
jbyte caddr[16]; jbyte caddr[16];
jint sz; jint sz;
struct sockaddr_in6 him6; SOCKETADDRESS sa, inf, *netif = NULL;
struct sockaddr_in6* netif = NULL;
struct sockaddr_in6 inf6;
int len = 0;
HANDLE hIcmpFile; HANDLE hIcmpFile;
/* // If IPv6 is not enabled, then we can't reach an IPv6 address, can we?
* If IPv6 is not enable, then we can't reach an IPv6 address, can we? // Actually, we probably shouldn't even get here.
* Actually, we probably shouldn't even get here.
*/
if (!ipv6_available()) { if (!ipv6_available()) {
return JNI_FALSE; return JNI_FALSE;
} }
/*
* If it's an IPv4 address, ICMP won't work with IPv4 mapped address, // If it's an IPv4 address, ICMP won't work with IPv4 mapped address,
* therefore, let's delegate to the Inet4Address method. // therefore, let's delegate to the Inet4Address method.
*/
sz = (*env)->GetArrayLength(env, addrArray); sz = (*env)->GetArrayLength(env, addrArray);
if (sz == 4) { if (sz == 4) {
return Java_java_net_Inet4AddressImpl_isReachable0(env, this, return Java_java_net_Inet4AddressImpl_isReachable0(env, this,
addrArray, addrArray, timeout,
timeout, ifArray, ttl);
ifArray, ttl);
} }
memset((char *) caddr, 0, 16); // load address to SOCKETADDRESS
memset((char *) &him6, 0, sizeof(him6)); memset((char *)caddr, 0, 16);
(*env)->GetByteArrayRegion(env, addrArray, 0, 16, caddr); (*env)->GetByteArrayRegion(env, addrArray, 0, 16, caddr);
memcpy((void *)&(him6.sin6_addr), caddr, sizeof(struct in6_addr) ); memset((char *)&sa, 0, sizeof(SOCKETADDRESS));
him6.sin6_family = AF_INET6; memcpy((void *)&sa.sa6.sin6_addr, caddr, sizeof(struct in6_addr));
sa.sa6.sin6_family = AF_INET6;
if (scope > 0) { if (scope > 0) {
him6.sin6_scope_id = scope; sa.sa6.sin6_scope_id = scope;
} }
len = sizeof(struct sockaddr_in6);
/** // load network interface address to SOCKETADDRESS, if specified
* A network interface was specified, let's convert the address
*/
if (!(IS_NULL(ifArray))) { if (!(IS_NULL(ifArray))) {
memset((char *) caddr, 0, 16); memset((char *)caddr, 0, 16);
memset((char *) &inf6, 0, sizeof(inf6)); (*env)->GetByteArrayRegion(env, ifArray, 0, 16, caddr);
(*env)->GetByteArrayRegion(env, ifArray, 0, 16, caddr); memset((char *)&inf, 0, sizeof(SOCKETADDRESS));
memcpy((void *)&(inf6.sin6_addr), caddr, sizeof(struct in6_addr) ); memcpy((void *)&inf.sa6.sin6_addr, caddr, sizeof(struct in6_addr));
inf6.sin6_family = AF_INET6; inf.sa6.sin6_family = AF_INET6;
inf6.sin6_port = 0; inf.sa6.sin6_scope_id = if_scope;
inf6.sin6_scope_id = if_scope; netif = &inf;
netif = &inf6;
} }
// Let's try to create an ICMP handle.
hIcmpFile = Icmp6CreateFile(); hIcmpFile = Icmp6CreateFile();
if (hIcmpFile == INVALID_HANDLE_VALUE) { if (hIcmpFile == INVALID_HANDLE_VALUE) {
int err = WSAGetLastError(); int err = WSAGetLastError();
if (err == ERROR_ACCESS_DENIED) { if (err == ERROR_ACCESS_DENIED) {
// fall back to TCP echo if access is denied to ICMP // fall back to TCP echo if access is denied to ICMP
return tcp_ping6(env, timeout, ttl, him6, netif, len); return tcp_ping6(env, &sa, netif, timeout, ttl);
} else { } else {
NET_ThrowNew(env, err, "Unable to create ICMP file handle"); NET_ThrowNew(env, err, "Unable to create ICMP file handle");
return JNI_FALSE; return JNI_FALSE;
} }
} else { } else {
return ping6(env, netif, &him6, timeout, hIcmpFile); // It didn't fail, so we can use ICMP.
return ping6(env, hIcmpFile, &sa, netif, timeout);
} }
return JNI_FALSE;
} }