8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms

Reviewed-by: michaelm
This commit is contained in:
Christoph Langer 2017-02-06 09:58:17 +01:00
parent ef94596e36
commit c8776e9797
2 changed files with 611 additions and 729 deletions

View file

@ -46,7 +46,13 @@ extern jobjectArray lookupIfLocalhost(JNIEnv *env, const char *hostname, jboolea
#define NI_MAXHOST 1025 #define NI_MAXHOST 1025
#endif #endif
/************************************************************************ #define SET_NONBLOCKING(fd) { \
int flags = fcntl(fd, F_GETFL); \
flags |= O_NONBLOCK; \
fcntl(fd, F_SETFL, flags); \
}
/*
* Inet4AddressImpl * Inet4AddressImpl
*/ */
@ -60,32 +66,23 @@ Java_java_net_Inet4AddressImpl_getLocalHostName(JNIEnv *env, jobject this) {
char hostname[NI_MAXHOST + 1]; char hostname[NI_MAXHOST + 1];
hostname[0] = '\0'; hostname[0] = '\0';
if (gethostname(hostname, NI_MAXHOST)) { if (gethostname(hostname, NI_MAXHOST) != 0) {
/* Something went wrong, maybe networking is not setup? */
strcpy(hostname, "localhost"); strcpy(hostname, "localhost");
} else { } else {
// try to resolve hostname via nameservice
// if it is known but getnameinfo fails, hostname will still be the
// value from gethostname
struct addrinfo hints, *res; struct addrinfo hints, *res;
int error;
// make sure string is null-terminated
hostname[NI_MAXHOST] = '\0'; hostname[NI_MAXHOST] = '\0';
memset(&hints, 0, sizeof(hints)); memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_CANONNAME; hints.ai_flags = AI_CANONNAME;
hints.ai_family = AF_INET; hints.ai_family = AF_INET;
error = getaddrinfo(hostname, NULL, &hints, &res); if (getaddrinfo(hostname, NULL, &hints, &res) == 0) {
getnameinfo(res->ai_addr, res->ai_addrlen, hostname, NI_MAXHOST,
if (error == 0) {/* host is known to name service */ NULL, 0, NI_NAMEREQD);
getnameinfo(res->ai_addr,
res->ai_addrlen,
hostname,
NI_MAXHOST,
NULL,
0,
NI_NAMEREQD);
/* if getnameinfo fails hostname is still the value
from gethostname */
freeaddrinfo(res); freeaddrinfo(res);
} }
} }
@ -96,86 +93,67 @@ Java_java_net_Inet4AddressImpl_getLocalHostName(JNIEnv *env, jobject this) {
* Find an internet address for a given hostname. Note that 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 *ever* be a %d.%d.%d.%d string * String "host" shouldn't be a %d.%d.%d.%d string. The only
* exception should be when any of the %d are out of range and
* we fallback to a lookup.
* *
* Class: java_net_Inet4AddressImpl * Class: java_net_Inet4AddressImpl
* Method: lookupAllHostAddr * Method: lookupAllHostAddr
* Signature: (Ljava/lang/String;)[[B * Signature: (Ljava/lang/String;)[[B
*/ */
JNIEXPORT jobjectArray JNICALL JNIEXPORT jobjectArray JNICALL
Java_java_net_Inet4AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this, Java_java_net_Inet4AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
jstring host) { jstring host) {
jobjectArray ret = NULL;
const char *hostname; const char *hostname;
jobjectArray ret = 0;
int retLen = 0;
int error = 0; int error = 0;
struct addrinfo hints, *res, *resNew = NULL; struct addrinfo hints, *res = NULL, *resNew = NULL, *last = NULL,
*iterator;
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);
/* 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;
hints.ai_family = AF_INET; hints.ai_family = AF_INET;
#ifdef __solaris__
/*
* Workaround for Solaris bug 4160367 - if a hostname contains a
* white space then 0.0.0.0 is returned
*/
if (isspace((unsigned char)hostname[0])) {
JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException",
(char *)hostname);
JNU_ReleaseStringPlatformChars(env, host, hostname);
return NULL;
}
#endif
error = getaddrinfo(hostname, NULL, &hints, &res); error = getaddrinfo(hostname, NULL, &hints, &res);
#ifdef MACOSX
if (error) { if (error) {
#if defined(MACOSX)
// If getaddrinfo fails try getifaddrs, see bug 8170910. // If getaddrinfo fails try getifaddrs, see bug 8170910.
ret = lookupIfLocalhost(env, hostname, JNI_FALSE); ret = lookupIfLocalhost(env, hostname, JNI_FALSE);
if (ret != NULL || (*env)->ExceptionCheck(env)) { if (ret != NULL || (*env)->ExceptionCheck(env)) {
JNU_ReleaseStringPlatformChars(env, host, hostname); goto cleanupAndReturn;
return ret;
}
} }
#endif #endif
// report error
if (error) {
/* report error */
NET_ThrowUnknownHostExceptionWithGaiError(env, hostname, error); NET_ThrowUnknownHostExceptionWithGaiError(env, hostname, error);
JNU_ReleaseStringPlatformChars(env, host, hostname); goto cleanupAndReturn;
return NULL;
} else { } else {
int i = 0; int i = 0;
struct addrinfo *itr, *last = NULL, *iterator = res; iterator = res;
while (iterator != NULL) { while (iterator != NULL) {
// remove the duplicate one // skip duplicates
int skip = 0; int skip = 0;
itr = resNew; struct addrinfo *iteratorNew = resNew;
while (itr != NULL) { while (iteratorNew != NULL) {
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;
} }
itr = itr->ai_next; iteratorNew = iteratorNew->ai_next;
} }
if (!skip) { if (!skip) {
@ -199,44 +177,37 @@ Java_java_net_Inet4AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
iterator = iterator->ai_next; iterator = iterator->ai_next;
} }
retLen = i; // allocate array - at this point i contains the number of addresses
iterator = resNew; ret = (*env)->NewObjectArray(env, i, ia_class, NULL);
ret = (*env)->NewObjectArray(env, retLen, ia_class, NULL);
if (IS_NULL(ret)) { if (IS_NULL(ret)) {
/* we may have memory to free at the end of this */
goto cleanupAndReturn; goto cleanupAndReturn;
} }
i = 0; i = 0;
iterator = resNew;
while (iterator != NULL) { while (iterator != NULL) {
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, i++, iaObj); (*env)->SetObjectArrayElement(env, ret, i++, iaObj);
iterator = iterator->ai_next; iterator = iterator->ai_next;
} }
} }
cleanupAndReturn: cleanupAndReturn:
{
struct addrinfo *iterator, *tmp;
iterator = resNew;
while (iterator != NULL) {
tmp = iterator;
iterator = iterator->ai_next;
free(tmp);
}
JNU_ReleaseStringPlatformChars(env, host, hostname); JNU_ReleaseStringPlatformChars(env, host, hostname);
while (resNew != NULL) {
last = resNew;
resNew = resNew->ai_next;
free(last);
} }
if (res != NULL) {
freeaddrinfo(res); freeaddrinfo(res);
}
return ret; return ret;
} }
@ -244,129 +215,203 @@ cleanupAndReturn:
* Class: java_net_Inet4AddressImpl * Class: java_net_Inet4AddressImpl
* 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_Inet4AddressImpl_getHostByAddr(JNIEnv *env, jobject this, Java_java_net_Inet4AddressImpl_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;
jbyte caddr[4]; jbyte caddr[4];
struct sockaddr_in him4;
struct sockaddr *sa;
jint addr; jint addr;
struct sockaddr_in sa;
// construct a sockaddr_in structure
memset((char *)&sa, 0, sizeof(struct sockaddr_in));
(*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((void *) &him4, 0, sizeof(him4)); sa.sin_addr.s_addr = htonl(addr);
him4.sin_addr.s_addr = htonl(addr); sa.sin_family = AF_INET;
him4.sin_family = AF_INET;
sa = (struct sockaddr *) &him4;
len = sizeof(him4);
error = getnameinfo(sa, len, host, NI_MAXHOST, NULL, 0, NI_NAMEREQD); if (getnameinfo((struct sockaddr *)&sa, sizeof(struct sockaddr_in),
host, NI_MAXHOST, NULL, 0, NI_NAMEREQD)) {
if (!error) { JNU_ThrowByName(env, "java/net/UnknownHostException", NULL);
} else {
ret = (*env)->NewStringUTF(env, host); ret = (*env)->NewStringUTF(env, host);
}
if (ret == NULL) { if (ret == NULL) {
JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", NULL); JNU_ThrowByName(env, "java/net/UnknownHostException", NULL);
}
} }
return ret; return ret;
} }
#define SET_NONBLOCKING(fd) { \
int flags = fcntl(fd, F_GETFL); \
flags |= O_NONBLOCK; \
fcntl(fd, F_SETFL, flags); \
}
/** /**
* ping implementation. * ping implementation using tcp port 7 (echo)
* Send a ICMP_ECHO_REQUEST packet every second until either the timeout
* expires or a answer is received.
* Returns true is an ECHO_REPLY is received, otherwise, false.
*/ */
static jboolean static jboolean
ping4(JNIEnv *env, jint fd, struct sockaddr_in* him, jint timeout, tcp_ping4(JNIEnv *env, SOCKETADDRESS *sa, SOCKETADDRESS *netif, jint timeout,
struct sockaddr_in* netif, jint ttl) { jint ttl)
jint size; {
jint n, hlen1, icmplen; jint fd;
socklen_t len; int connect_rv = -1;
char sendbuf[1500];
char recvbuf[1500];
struct icmp *icmp;
struct ip *ip;
struct sockaddr_in sa_recv;
jchar pid;
jint tmout2, seq = 1;
struct timeval tv;
size_t plen;
/* icmp_id is a 16 bit data type, therefore down cast the pid */ // open a TCP socket
pid = (jchar)getpid(); fd = socket(AF_INET, SOCK_STREAM, 0);
size = 60*1024; if (fd == -1) {
setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size)); // note: if you run out of fds, you may not be able to load
/* // the exception class, and get a NoClassDefFoundError instead.
* sets the ttl (max number of hops) NET_ThrowNew(env, errno, "Can't create socket");
*/ return JNI_FALSE;
}
// set TTL
if (ttl > 0) { if (ttl > 0) {
setsockopt(fd, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl)); setsockopt(fd, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl));
} }
/*
* a specific interface was specified, so let's bind the socket // A network interface was specified, so let's bind to it.
* to that interface to ensure the requests are sent only through 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, errno, "Can't bind socket"); NET_ThrowNew(env, errno, "Can't bind socket");
close(fd); close(fd);
return JNI_FALSE; return JNI_FALSE;
} }
} }
/*
* Make the socket non blocking so we can use select // Make the socket non blocking so we can use select/poll.
SET_NONBLOCKING(fd);
sa->sa4.sin_port = htons(7); // echo port
connect_rv = NET_Connect(fd, &sa->sa, sizeof(struct sockaddr_in));
// connection established or refused immediately, either way it means
// we were able to reach the host!
if (connect_rv == 0 || errno == ECONNREFUSED) {
close(fd);
return JNI_TRUE;
}
switch (errno) {
case ENETUNREACH: // Network Unreachable
case EAFNOSUPPORT: // Address Family not supported
case EADDRNOTAVAIL: // address is not available on the remote machine
#if defined(__linux__) || defined(_AIX)
// On some Linux versions, when a socket is bound to the loopback
// interface, connect will fail and errno will be set to EINVAL
// or EHOSTUNREACH. When that happens, don't throw an exception,
// just return false.
case EINVAL:
case EHOSTUNREACH: // No route to host
#endif
close(fd);
return JNI_FALSE;
case EINPROGRESS: // this is expected as we'll probably have to wait
break;
default:
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ConnectException",
"connect failed");
close(fd);
return JNI_FALSE;
}
timeout = NET_Wait(env, fd, NET_WAIT_CONNECT, timeout);
if (timeout >= 0) {
// connection has been established, check for error condition
socklen_t optlen = (socklen_t)sizeof(connect_rv);
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&connect_rv,
&optlen) <0)
{
connect_rv = errno;
}
if (connect_rv == 0 || connect_rv == ECONNREFUSED) {
close(fd);
return JNI_TRUE;
}
}
close(fd);
return JNI_FALSE;
}
/**
* ping implementation.
* Send an ICMP_ECHO_REQUEST packet every second until either the timeout
* expires or an answer is received.
* Returns true if an ECHO_REPLY is received, false otherwise.
*/ */
static jboolean
ping4(JNIEnv *env, jint fd, SOCKETADDRESS *sa, SOCKETADDRESS *netif,
jint timeout, jint ttl)
{
jint n, size = 60 * 1024, hlen, tmout2, seq = 1;
socklen_t len;
unsigned char sendbuf[1500], recvbuf[1500];
struct icmp *icmp;
struct ip *ip;
struct sockaddr_in sa_recv;
jchar pid;
struct timeval tv;
size_t plen = ICMP_ADVLENMIN + sizeof(tv);
setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
// sets the ttl (max number of hops)
if (ttl > 0) {
setsockopt(fd, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl));
}
// a specific interface was specified, so let's bind the socket
// to that interface to ensure the requests are sent only through it.
if (netif != NULL) {
if (bind(fd, &netif->sa, sizeof(struct sockaddr_in)) < 0) {
NET_ThrowNew(env, errno, "Can't bind socket");
close(fd);
return JNI_FALSE;
}
}
// icmp_id is a 16 bit data type, therefore down cast the pid
pid = (jchar)getpid();
// Make the socket non blocking so we can use select
SET_NONBLOCKING(fd); SET_NONBLOCKING(fd);
do { do {
/* // create the ICMP request
* create the ICMP request
*/
icmp = (struct icmp *)sendbuf; icmp = (struct icmp *)sendbuf;
icmp->icmp_type = ICMP_ECHO; icmp->icmp_type = ICMP_ECHO;
icmp->icmp_code = 0; icmp->icmp_code = 0;
// let's tag the ECHO packet with our pid so we can identify it
icmp->icmp_id = htons(pid); icmp->icmp_id = htons(pid);
icmp->icmp_seq = htons(seq); icmp->icmp_seq = htons(seq);
seq++; seq++;
gettimeofday(&tv, NULL); gettimeofday(&tv, NULL);
memcpy(icmp->icmp_data, &tv, sizeof(tv)); memcpy(icmp->icmp_data, &tv, sizeof(tv));
plen = ICMP_ADVLENMIN + sizeof(tv);
icmp->icmp_cksum = 0; icmp->icmp_cksum = 0;
// manually calculate checksum
icmp->icmp_cksum = in_cksum((u_short *)icmp, plen); icmp->icmp_cksum = in_cksum((u_short *)icmp, plen);
/* // send it
* send it n = sendto(fd, sendbuf, plen, 0, &sa->sa, sizeof(struct sockaddr_in));
*/
n = sendto(fd, sendbuf, plen, 0, (struct sockaddr *)him,
sizeof(struct sockaddr));
if (n < 0 && errno != EINPROGRESS) { if (n < 0 && errno != EINPROGRESS) {
#ifdef __linux__ #if defined(__linux__)
if (errno != EINVAL && errno != EHOSTUNREACH)
/* /*
* On some Linux versions, when a socket is bound to the loopback * On some Linux versions, when a socket is bound to the loopback
* interface, sendto will fail and errno will be set to * interface, sendto will fail and errno will be set to
* EINVAL or EHOSTUNREACH. When that happens, don't throw an * EINVAL or EHOSTUNREACH. When that happens, don't throw an
* exception, just return false. * exception, just return false.
*/ */
#endif /*__linux__ */ if (errno != EINVAL && errno != EHOSTUNREACH) {
NET_ThrowNew(env, errno, "Can't send ICMP packet"); NET_ThrowNew(env, errno, "Can't send ICMP packet");
}
#else
NET_ThrowNew(env, errno, "Can't send ICMP packet");
#endif
close(fd); close(fd);
return JNI_FALSE; return JNI_FALSE;
} }
@ -376,28 +421,33 @@ ping4(JNIEnv *env, jint fd, struct sockaddr_in* him, jint timeout,
tmout2 = NET_Wait(env, fd, NET_WAIT_READ, tmout2); tmout2 = NET_Wait(env, fd, NET_WAIT_READ, tmout2);
if (tmout2 >= 0) { if (tmout2 >= 0) {
len = sizeof(sa_recv); len = sizeof(sa_recv);
n = recvfrom(fd, recvbuf, sizeof(recvbuf), 0, (struct sockaddr *)&sa_recv, &len); n = recvfrom(fd, recvbuf, sizeof(recvbuf), 0,
(struct sockaddr *)&sa_recv, &len);
// check if we received enough data
if (n < (jint)sizeof(struct ip)) {
continue;
}
ip = (struct ip *)recvbuf; ip = (struct ip *)recvbuf;
hlen1 = (ip->ip_hl) << 2; hlen = ((jint)(unsigned int)(ip->ip_hl)) << 2;
icmp = (struct icmp *) (recvbuf + hlen1); // check if we received enough data
icmplen = n - hlen1; if (n < (jint)(hlen + sizeof(struct icmp))) {
/* continue;
* We did receive something, but is it what we were expecting? }
* I.E.: A ICMP_ECHOREPLY packet with the proper PID. icmp = (struct icmp *)(recvbuf + hlen);
*/ // We did receive something, but is it what we were expecting?
if (icmplen >= 8 && icmp->icmp_type == ICMP_ECHOREPLY // I.E.: An ICMP_ECHO_REPLY packet with the proper PID and
&& (ntohs(icmp->icmp_id) == pid)) { // from the host that we are trying to determine is reachable.
if ((him->sin_addr.s_addr == sa_recv.sin_addr.s_addr)) { if (icmp->icmp_type == ICMP_ECHOREPLY &&
(ntohs(icmp->icmp_id) == pid))
{
if (sa->sa4.sin_addr.s_addr == sa_recv.sin_addr.s_addr) {
close(fd); close(fd);
return JNI_TRUE; return JNI_TRUE;
} } else if (sa->sa4.sin_addr.s_addr == 0) {
if (him->sin_addr.s_addr == 0) {
close(fd); close(fd);
return JNI_TRUE; return JNI_TRUE;
} }
} }
} }
} while (tmout2 > 0); } while (tmout2 > 0);
timeout -= 1000; timeout -= 1000;
@ -413,40 +463,31 @@ ping4(JNIEnv *env, jint fd, struct sockaddr_in* him, jint timeout,
*/ */
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 addr;
jbyte caddr[4]; jbyte caddr[4];
jint fd; jint addr = 0, sz, fd;
struct sockaddr_in him; SOCKETADDRESS sa, inf, *netif = NULL;
struct sockaddr_in* netif = NULL;
struct sockaddr_in inf;
int len = 0;
int connect_rv = -1;
int sz;
memset((char *) caddr, 0, sizeof(caddr)); // check if address array size is 4 (IPv4 address)
memset((char *) &him, 0, sizeof(him));
memset((char *) &inf, 0, sizeof(inf));
sz = (*env)->GetArrayLength(env, addrArray); sz = (*env)->GetArrayLength(env, addrArray);
if (sz != 4) { if (sz != 4) {
return JNI_FALSE; return JNI_FALSE;
} }
// convert IP address from byte array to integer
memset((char *)caddr, 0, sizeof(caddr));
(*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);
addr = htonl(addr); memset((char *)&sa, 0, sizeof(SOCKETADDRESS));
him.sin_addr.s_addr = addr; sa.sa4.sin_addr.s_addr = htonl(addr);
him.sin_family = AF_INET; sa.sa4.sin_family = AF_INET;
len = sizeof(him);
/* // If a network interface was specified, let's convert its address as well.
* If a network interface was specified, let's create the address
* for it.
*/
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);
@ -454,108 +495,19 @@ Java_java_net_Inet4AddressImpl_isReachable0(JNIEnv *env, jobject this,
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);
addr = htonl(addr); memset((char *)&inf, 0, sizeof(SOCKETADDRESS));
inf.sin_addr.s_addr = addr; inf.sa4.sin_addr.s_addr = htonl(addr);
inf.sin_family = AF_INET; inf.sa4.sin_family = AF_INET;
inf.sin_port = 0;
netif = &inf; netif = &inf;
} }
/* // Let's try to create a RAW socket to send ICMP packets.
* Let's try to create a RAW socket to send ICMP packets // This usually requires "root" privileges, so it's likely to fail.
* This usually requires "root" privileges, so it's likely to fail.
*/
fd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); fd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if (fd != -1) {
/*
* It didn't fail, so we can use ICMP_ECHO requests.
*/
return ping4(env, fd, &him, timeout, netif, ttl);
}
/*
* Can't create a raw socket, so let's try a TCP socket
*/
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd == -1) { if (fd == -1) {
/* note: if you run out of fds, you may not be able to load return tcp_ping4(env, &sa, netif, timeout, ttl);
* the exception class, and get a NoClassDefFoundError
* instead.
*/
NET_ThrowNew(env, errno, "Can't create socket");
return JNI_FALSE;
}
if (ttl > 0) {
setsockopt(fd, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl));
}
/*
* A network interface was specified, so let's bind to it.
*/
if (netif != NULL) {
if (bind(fd, (struct sockaddr*)netif, sizeof(struct sockaddr_in)) < 0) {
NET_ThrowNew(env, errno, "Can't bind socket");
close(fd);
return JNI_FALSE;
}
}
/*
* Make the socket non blocking so we can use select/poll.
*/
SET_NONBLOCKING(fd);
him.sin_port = htons(7); /* Echo */
connect_rv = NET_Connect(fd, (struct sockaddr *)&him, len);
/**
* connection established or refused immediately, either way it means
* we were able to reach the host!
*/
if (connect_rv == 0 || errno == ECONNREFUSED) {
close(fd);
return JNI_TRUE;
} else { } else {
socklen_t optlen = (socklen_t)sizeof(connect_rv); // It didn't fail, so we can use ICMP_ECHO requests.
return ping4(env, fd, &sa, netif, timeout, ttl);
switch (errno) {
case ENETUNREACH: /* Network Unreachable */
case EAFNOSUPPORT: /* Address Family not supported */
case EADDRNOTAVAIL: /* address is not available on the remote machine */
#if defined(__linux__) || defined(_AIX)
case EINVAL:
case EHOSTUNREACH: /* No route to host */
/*
* On some Linux versions, when a socket is bound to the loopback
* interface, connect will fail and errno will be set to EINVAL
* or EHOSTUNREACH. When that happens, don't throw an exception,
* just return false.
*/
#endif /* __linux__ */
close(fd);
return JNI_FALSE;
}
if (errno != EINPROGRESS) {
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ConnectException",
"connect failed");
close(fd);
return JNI_FALSE;
}
timeout = NET_Wait(env, fd, NET_WAIT_CONNECT, timeout);
if (timeout >= 0) {
/* has connection been established? */
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&connect_rv,
&optlen) <0) {
connect_rv = errno;
}
if (connect_rv == 0 || connect_rv == ECONNREFUSED) {
close(fd);
return JNI_TRUE;
}
}
close(fd);
return JNI_FALSE;
} }
} }

View file

@ -38,17 +38,22 @@
#include "net_util.h" #include "net_util.h"
#include "java_net_InetAddress.h"
#include "java_net_Inet4AddressImpl.h" #include "java_net_Inet4AddressImpl.h"
#include "java_net_Inet6AddressImpl.h" #include "java_net_Inet6AddressImpl.h"
#include "java_net_InetAddress.h"
/* the initial size of our hostent buffers */ /* the initial size of our hostent buffers */
#ifndef NI_MAXHOST #ifndef NI_MAXHOST
#define NI_MAXHOST 1025 #define NI_MAXHOST 1025
#endif #endif
#define SET_NONBLOCKING(fd) { \
int flags = fcntl(fd, F_GETFL); \
flags |= O_NONBLOCK; \
fcntl(fd, F_SETFL, flags); \
}
/************************************************************************ /*
* Inet6AddressImpl * Inet6AddressImpl
*/ */
@ -59,58 +64,40 @@
*/ */
JNIEXPORT jstring JNICALL JNIEXPORT jstring JNICALL
Java_java_net_Inet6AddressImpl_getLocalHostName(JNIEnv *env, jobject this) { Java_java_net_Inet6AddressImpl_getLocalHostName(JNIEnv *env, jobject this) {
int ret;
char hostname[NI_MAXHOST + 1]; char hostname[NI_MAXHOST + 1];
hostname[0] = '\0'; hostname[0] = '\0';
ret = gethostname(hostname, NI_MAXHOST); if (gethostname(hostname, NI_MAXHOST) != 0) {
if (ret == -1) {
/* Something went wrong, maybe networking is not setup? */
strcpy(hostname, "localhost"); strcpy(hostname, "localhost");
} else {
// ensure null-terminated
hostname[NI_MAXHOST] = '\0';
}
#if defined(__solaris__) #if defined(__solaris__)
if (ret == 0) { } else {
/* Solaris doesn't want to give us a fully qualified domain name. // try to resolve hostname via nameservice
* We do a reverse lookup to try and get one. This works // if it is known but getnameinfo fails, hostname will still be the
* if DNS occurs before NIS in /etc/resolv.conf, but fails // value from gethostname
* if NIS comes first (it still gets only a partial name).
* We use thread-safe system calls.
*/
struct addrinfo hints, *res; struct addrinfo hints, *res;
int error;
// make sure string is null-terminated
hostname[NI_MAXHOST] = '\0';
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;
error = getaddrinfo(hostname, NULL, &hints, &res); if (getaddrinfo(hostname, NULL, &hints, &res) == 0) {
getnameinfo(res->ai_addr, res->ai_addrlen, hostname, NI_MAXHOST,
if (error == 0) { NULL, 0, NI_NAMEREQD);
/* host is known to name service */
error = getnameinfo(res->ai_addr,
res->ai_addrlen,
hostname,
NI_MAXHOST,
NULL,
0,
NI_NAMEREQD);
/* if getnameinfo fails hostname is still the value
from gethostname */
freeaddrinfo(res); freeaddrinfo(res);
} }
} }
#else
} else {
// make sure string is null-terminated
hostname[NI_MAXHOST] = '\0';
}
#endif #endif
return (*env)->NewStringUTF(env, hostname); return (*env)->NewStringUTF(env, hostname);
} }
#ifdef MACOSX #if defined(MACOSX)
/* also called from Inet4AddressImpl.c */ /* also called from Inet4AddressImpl.c */
__private_extern__ jobjectArray __private_extern__ jobjectArray
lookupIfLocalhost(JNIEnv *env, const char *hostname, jboolean includeV6) lookupIfLocalhost(JNIEnv *env, const char *hostname, jboolean includeV6)
@ -172,9 +159,7 @@ lookupIfLocalhost(JNIEnv *env, const char *hostname, jboolean includeV6)
} else if (family == AF_INET6 && includeV6) { } else if (family == AF_INET6 && includeV6) {
addrs6++; addrs6++;
if (isLoopback) numV6Loopbacks++; if (isLoopback) numV6Loopbacks++;
} else { } // else we don't care, e.g. AF_LINK
/* We don't care e.g. AF_LINK */
}
} }
iter = iter->ifa_next; iter = iter->ifa_next;
} }
@ -205,9 +190,9 @@ lookupIfLocalhost(JNIEnv *env, const char *hostname, jboolean includeV6)
jboolean isLoopback = iter->ifa_flags & IFF_LOOPBACK; jboolean isLoopback = iter->ifa_flags & IFF_LOOPBACK;
int family = iter->ifa_addr->sa_family; int family = iter->ifa_addr->sa_family;
if (iter->ifa_name[0] != '\0' && iter->ifa_addr if (iter->ifa_name[0] != '\0' && iter->ifa_addr &&
&& (family == AF_INET || (family == AF_INET6 && includeV6)) (family == AF_INET || (family == AF_INET6 && includeV6)) &&
&& (!isLoopback || includeLoopback)) (!isLoopback || includeLoopback))
{ {
int port; int port;
int index = (family == AF_INET) ? i++ : j++; int index = (family == AF_INET) ? i++ : j++;
@ -234,93 +219,65 @@ lookupIfLocalhost(JNIEnv *env, const char *hostname, jboolean includeV6)
#endif #endif
/* /*
* Find an internet address for a given hostname. Note that this
* 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
* String "host" shouldn't *ever* be a %d.%d.%d.%d string
*
* Class: java_net_Inet6AddressImpl * Class: java_net_Inet6AddressImpl
* Method: lookupAllHostAddr * Method: lookupAllHostAddr
* Signature: (Ljava/lang/String;)[[B * 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,
*iterator;
int getaddrinfo_error=0;
struct addrinfo hints, *res, *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);
/* 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;
hints.ai_family = AF_UNSPEC; hints.ai_family = AF_UNSPEC;
#ifdef __solaris__ error = getaddrinfo(hostname, NULL, &hints, &res);
/*
* Workaround for Solaris bug 4160367 - if a hostname contains a
* white space then 0.0.0.0 is returned
*/
if (isspace((unsigned char)hostname[0])) {
JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException",
hostname);
JNU_ReleaseStringPlatformChars(env, host, hostname);
return NULL;
}
#endif
getaddrinfo_error = getaddrinfo(hostname, NULL, &hints, &res); if (error) {
#if defined(MACOSX)
#ifdef MACOSX // if getaddrinfo fails try getifaddrs
if (getaddrinfo_error) {
/*
* If getaddrinfo fails looking up the local machine, attempt to get the
* address from getifaddrs. This ensures we get an IPv6 address for the
* local machine.
*/
ret = lookupIfLocalhost(env, hostname, JNI_TRUE); ret = lookupIfLocalhost(env, hostname, JNI_TRUE);
if (ret != NULL || (*env)->ExceptionCheck(env)) { if (ret != NULL || (*env)->ExceptionCheck(env)) {
JNU_ReleaseStringPlatformChars(env, host, hostname); goto cleanupAndReturn;
return ret;
}
} }
#endif #endif
// report error
if (getaddrinfo_error) { NET_ThrowUnknownHostExceptionWithGaiError(env, hostname, error);
/* report error */ goto cleanupAndReturn;
NET_ThrowUnknownHostExceptionWithGaiError(
env, hostname, getaddrinfo_error);
JNU_ReleaseStringPlatformChars(env, host, hostname);
return NULL;
} else { } else {
int i = 0, addressPreference = -1; 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 = NULL, *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;
} }
@ -328,7 +285,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] !=
@ -337,7 +294,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;
@ -346,11 +303,11 @@ 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) {
@ -378,32 +335,26 @@ Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
} }
iterator = iterator->ai_next; iterator = iterator->ai_next;
} }
retLen = i;
iterator = resNew;
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;
} }
addressPreference = (*env)->GetStaticIntField(env, ia_class, ia_preferIPv6AddressID);
if (addressPreference == java_net_InetAddress_PREFER_IPV6_VALUE) { if (addressPreference == java_net_InetAddress_PREFER_IPV6_VALUE) {
/* AF_INET addresses will be offset by inet6Count */
inetIndex = inet6Count; inetIndex = inet6Count;
inet6Index = 0; inet6Index = 0;
} else if (addressPreference == java_net_InetAddress_PREFER_IPV4_VALUE) { } else if (addressPreference == java_net_InetAddress_PREFER_IPV4_VALUE) {
/* AF_INET6 addresses will be offset by inetCount */
inetIndex = 0; inetIndex = 0;
inet6Index = inetCount; inet6Index = inetCount;
} else if (addressPreference == 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) {
jboolean ret1;
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)) {
@ -416,7 +367,7 @@ Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
inetIndex++; inetIndex++;
} else if (iterator->ai_family == AF_INET6) { } else if (iterator->ai_family == AF_INET6) {
jint scope = 0; jint scope = 0;
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;
@ -427,9 +378,8 @@ Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
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);
@ -443,21 +393,16 @@ Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
iterator = iterator->ai_next; iterator = iterator->ai_next;
} }
} }
cleanupAndReturn: cleanupAndReturn:
{
struct addrinfo *iterator, *tmp;
iterator = resNew;
while (iterator != NULL) {
tmp = iterator;
iterator = iterator->ai_next;
free(tmp);
}
JNU_ReleaseStringPlatformChars(env, host, hostname); JNU_ReleaseStringPlatformChars(env, host, hostname);
while (resNew != NULL) {
last = resNew;
resNew = resNew->ai_next;
free(last);
} }
if (res != NULL) {
freeaddrinfo(res); freeaddrinfo(res);
}
return ret; return ret;
} }
@ -465,25 +410,24 @@ Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
* 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);
@ -491,113 +435,193 @@ Java_java_net_Inet6AddressImpl_getHostByAddr(JNIEnv *env, jobject this,
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((void *) &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((void *)&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) { if (ret == NULL) {
JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", NULL); JNU_ThrowByName(env, "java/net/UnknownHostException", NULL);
}
} }
return ret; return ret;
} }
#define SET_NONBLOCKING(fd) { \
int flags = fcntl(fd, F_GETFL); \
flags |= O_NONBLOCK; \
fcntl(fd, F_SETFL, flags); \
}
static jboolean
ping6(JNIEnv *env, jint fd, struct sockaddr_in6* him, jint timeout,
struct sockaddr_in6* netif, jint ttl) {
jint size;
jint n;
socklen_t len;
char sendbuf[1500];
unsigned char recvbuf[1500];
struct icmp6_hdr *icmp6;
struct sockaddr_in6 sa_recv;
jbyte *caddr, *recv_caddr;
jchar pid;
jint tmout2, seq = 1;
struct timeval tv;
size_t plen;
#ifdef __linux__
{
int csum_offset;
/** /**
* For some strange reason, the linux kernel won't calculate the * ping implementation using tcp port 7 (echo)
* checksum of ICMPv6 packets unless you set this socket option
*/ */
csum_offset = 2; static jboolean
setsockopt(fd, SOL_RAW, IPV6_CHECKSUM, &csum_offset, sizeof(int)); tcp_ping6(JNIEnv *env, SOCKETADDRESS *sa, SOCKETADDRESS *netif, jint timeout,
jint ttl)
{
jint fd;
int connect_rv = -1;
// open a TCP socket
fd = socket(AF_INET6, SOCK_STREAM, 0);
if (fd == -1) {
// note: if you run out of fds, you may not be able to load
// the exception class, and get a NoClassDefFoundError instead.
NET_ThrowNew(env, errno, "Can't create socket");
return JNI_FALSE;
} }
#endif
caddr = (jbyte *)&(him->sin6_addr); // set TTL
/* icmp_id is a 16 bit data type, therefore down cast the pid */
pid = (jchar)getpid();
size = 60*1024;
setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
if (ttl > 0) { if (ttl > 0) {
setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl)); setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl));
} }
// 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_in6)) <0) { if (bind(fd, &netif->sa, sizeof(struct sockaddr_in6)) <0) {
NET_ThrowNew(env, errno, "Can't bind socket"); NET_ThrowNew(env, errno, "Can't bind socket");
close(fd); close(fd);
return JNI_FALSE; return JNI_FALSE;
} }
} }
// Make the socket non blocking so we can use select/poll.
SET_NONBLOCKING(fd); SET_NONBLOCKING(fd);
sa->sa6.sin6_port = htons(7); // echo port
connect_rv = NET_Connect(fd, &sa->sa, sizeof(struct sockaddr_in6));
// connection established or refused immediately, either way it means
// we were able to reach the host!
if (connect_rv == 0 || errno == ECONNREFUSED) {
close(fd);
return JNI_TRUE;
}
switch (errno) {
case ENETUNREACH: // Network Unreachable
case EAFNOSUPPORT: // Address Family not supported
case EADDRNOTAVAIL: // address is not available on the remote machine
#if defined(__linux__) || defined(_AIX)
// On some Linux versions, when a socket is bound to the loopback
// interface, connect will fail and errno will be set to EINVAL
// or EHOSTUNREACH. When that happens, don't throw an exception,
// just return false.
case EINVAL:
case EHOSTUNREACH: // No route to host
#endif
close(fd);
return JNI_FALSE;
case EINPROGRESS: // this is expected as we'll probably have to wait
break;
default:
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ConnectException",
"connect failed");
close(fd);
return JNI_FALSE;
}
timeout = NET_Wait(env, fd, NET_WAIT_CONNECT, timeout);
if (timeout >= 0) {
// connection has been established, check for error condition
socklen_t optlen = (socklen_t)sizeof(connect_rv);
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&connect_rv,
&optlen) <0)
{
connect_rv = errno;
}
if (connect_rv == 0 || connect_rv == ECONNREFUSED) {
close(fd);
return JNI_TRUE;
}
}
close(fd);
return JNI_FALSE;
}
/**
* ping implementation.
* Send an ICMP_ECHO_REQUEST packet every second until either the timeout
* expires or an answer is received.
* Returns true if an ECHO_REPLY is received, false otherwise.
*/
static jboolean
ping6(JNIEnv *env, jint fd, SOCKETADDRESS *sa, SOCKETADDRESS *netif,
jint timeout, jint ttl)
{
jint n, size = 60 * 1024, tmout2, seq = 1;
socklen_t len;
unsigned char sendbuf[1500], recvbuf[1500];
struct icmp6_hdr *icmp6;
struct sockaddr_in6 sa_recv;
jchar pid;
struct timeval tv;
size_t plen = sizeof(struct icmp6_hdr) + sizeof(tv);
#if defined(__linux__)
/**
* For some strange reason, the linux kernel won't calculate the
* checksum of ICMPv6 packets unless you set this socket option
*/
int csum_offset = 2;
setsockopt(fd, SOL_RAW, IPV6_CHECKSUM, &csum_offset, sizeof(int));
#endif
setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
// sets the ttl (max number of hops)
if (ttl > 0) {
setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl));
}
// a specific interface was specified, so let's bind the socket
// to that interface to ensure the requests are sent only through it.
if (netif != NULL) {
if (bind(fd, &netif->sa, sizeof(struct sockaddr_in6)) <0) {
NET_ThrowNew(env, errno, "Can't bind socket");
close(fd);
return JNI_FALSE;
}
}
// icmp_id is a 16 bit data type, therefore down cast the pid
pid = (jchar)getpid();
// Make the socket non blocking so we can use select
SET_NONBLOCKING(fd);
do { do {
// create the ICMP request
icmp6 = (struct icmp6_hdr *)sendbuf; icmp6 = (struct icmp6_hdr *)sendbuf;
icmp6->icmp6_type = ICMP6_ECHO_REQUEST; icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
icmp6->icmp6_code = 0; icmp6->icmp6_code = 0;
/* let's tag the ECHO packet with our pid so we can identify it */ // let's tag the ECHO packet with our pid so we can identify it
icmp6->icmp6_id = htons(pid); icmp6->icmp6_id = htons(pid);
icmp6->icmp6_seq = htons(seq); icmp6->icmp6_seq = htons(seq);
seq++; seq++;
icmp6->icmp6_cksum = 0;
gettimeofday(&tv, NULL); gettimeofday(&tv, NULL);
memcpy(sendbuf + sizeof(struct icmp6_hdr), &tv, sizeof(tv)); memcpy(sendbuf + sizeof(struct icmp6_hdr), &tv, sizeof(tv));
plen = sizeof(struct icmp6_hdr) + sizeof(tv); icmp6->icmp6_cksum = 0;
n = sendto(fd, sendbuf, plen, 0, (struct sockaddr*) him, sizeof(struct sockaddr_in6)); // send it
n = sendto(fd, sendbuf, plen, 0, &sa->sa, sizeof(struct sockaddr_in6));
if (n < 0 && errno != EINPROGRESS) { if (n < 0 && errno != EINPROGRESS) {
#ifdef __linux__ #if defined(__linux__)
if (errno != EINVAL && errno != EHOSTUNREACH)
/* /*
* On some Linux versions, when a socket is bound to the * On some Linux versions, when a socket is bound to the loopback
* loopback interface, sendto will fail and errno will be * interface, sendto will fail and errno will be set to
* set to EINVAL or EHOSTUNREACH. * EINVAL or EHOSTUNREACH. When that happens, don't throw an
* When that happens, don't throw an exception, just return false. * exception, just return false.
*/ */
#endif /*__linux__ */ if (errno != EINVAL && errno != EHOSTUNREACH) {
NET_ThrowNew(env, errno, "Can't send ICMP packet"); NET_ThrowNew(env, errno, "Can't send ICMP packet");
}
#else
NET_ThrowNew(env, errno, "Can't send ICMP packet");
#endif
close(fd); close(fd);
return JNI_FALSE; return JNI_FALSE;
} }
@ -605,24 +629,26 @@ ping6(JNIEnv *env, jint fd, struct sockaddr_in6* him, jint timeout,
tmout2 = timeout > 1000 ? 1000 : timeout; tmout2 = timeout > 1000 ? 1000 : timeout;
do { do {
tmout2 = NET_Wait(env, fd, NET_WAIT_READ, tmout2); tmout2 = NET_Wait(env, fd, NET_WAIT_READ, tmout2);
if (tmout2 >= 0) { if (tmout2 >= 0) {
len = sizeof(sa_recv); len = sizeof(sa_recv);
n = recvfrom(fd, recvbuf, sizeof(recvbuf), 0, (struct sockaddr*) &sa_recv, &len); n = recvfrom(fd, recvbuf, sizeof(recvbuf), 0,
icmp6 = (struct icmp6_hdr *) (recvbuf); (struct sockaddr *)&sa_recv, &len);
recv_caddr = (jbyte *)&(sa_recv.sin6_addr); // check if we received enough data
/* if (n < (jint)sizeof(struct icmp6_hdr)) {
* We did receive something, but is it what we were expecting? continue;
* I.E.: An ICMP6_ECHO_REPLY packet with the proper PID and }
* from the host that we are trying to determine is reachable. icmp6 = (struct icmp6_hdr *)recvbuf;
*/ // We did receive something, but is it what we were expecting?
if (n >= 8 && icmp6->icmp6_type == ICMP6_ECHO_REPLY && // I.E.: An ICMP6_ECHO_REPLY packet with the proper PID and
(ntohs(icmp6->icmp6_id) == pid)) { // from the host that we are trying to determine is reachable.
if (NET_IsEqual(caddr, recv_caddr)) { if (icmp6->icmp6_type == ICMP6_ECHO_REPLY &&
(ntohs(icmp6->icmp6_id) == pid))
{
if (NET_IsEqual((jbyte *)&sa->sa6.sin6_addr,
(jbyte *)&sa_recv.sin6_addr)) {
close(fd); close(fd);
return JNI_TRUE; return JNI_TRUE;
} } else if (NET_IsZeroAddr((jbyte *)&sa->sa6.sin6_addr)) {
if (NET_IsZeroAddr(caddr)) {
close(fd); close(fd);
return JNI_TRUE; return JNI_TRUE;
} }
@ -642,157 +668,61 @@ ping6(JNIEnv *env, jint fd, struct sockaddr_in6* him, jint timeout,
*/ */
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 fd, sz; jint sz, fd;
struct sockaddr_in6 him6; SOCKETADDRESS sa, inf, *netif = NULL;
struct sockaddr_in6 inf6;
struct sockaddr_in6* netif = NULL;
int len = 0;
int connect_rv = -1;
/* // 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.
*/
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((void *) caddr, 0, 16); // load address to SOCKETADDRESS
memset((void *) &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));
#ifdef __linux__ sa.sa6.sin6_family = AF_INET6;
if (scope > 0) if (scope > 0) {
him6.sin6_scope_id = scope; sa.sa6.sin6_scope_id = scope;
else #if defined(__linux__)
him6.sin6_scope_id = getDefaultIPv6Interface( &(him6.sin6_addr));
len = sizeof(struct sockaddr_in6);
#else
if (scope > 0)
him6.sin6_scope_id = scope;
len = sizeof(struct sockaddr_in6);
#endif
/*
* If a network interface was specified, let's create the address
* for it.
*/
if (!(IS_NULL(ifArray))) {
memset((void *) caddr, 0, 16);
memset((void *) &inf6, 0, sizeof(inf6));
(*env)->GetByteArrayRegion(env, ifArray, 0, 16, caddr);
memcpy((void *)&(inf6.sin6_addr), caddr, sizeof(struct in6_addr) );
inf6.sin6_family = AF_INET6;
inf6.sin6_scope_id = if_scope;
netif = &inf6;
}
/*
* If we can create a RAW socket, then when can use the ICMP ECHO_REQUEST
* otherwise we'll try a tcp socket to the Echo port (7).
* Note that this is empiric, and not connecting could mean it's blocked
* or the echo service has been disabled.
*/
fd = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
if (fd != -1) { /* Good to go, let's do a ping */
return ping6(env, fd, &him6, timeout, netif, ttl);
}
/* No good, let's fall back on TCP */
fd = socket(AF_INET6, SOCK_STREAM, 0);
if (fd == -1) {
/* note: if you run out of fds, you may not be able to load
* the exception class, and get a NoClassDefFoundError
* instead.
*/
NET_ThrowNew(env, errno, "Can't create socket");
return JNI_FALSE;
}
if (ttl > 0) {
setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl));
}
/*
* A network interface was specified, so let's bind to it.
*/
if (netif != NULL) {
if (bind(fd, (struct sockaddr*)netif, sizeof(struct sockaddr_in6)) <0) {
NET_ThrowNew(env, errno, "Can't bind socket");
close(fd);
return JNI_FALSE;
}
}
SET_NONBLOCKING(fd);
him6.sin6_port = htons((short) 7); /* Echo port */
connect_rv = NET_Connect(fd, (struct sockaddr *)&him6, len);
/**
* connection established or refused immediately, either way it means
* we were able to reach the host!
*/
if (connect_rv == 0 || errno == ECONNREFUSED) {
close(fd);
return JNI_TRUE;
} else { } else {
socklen_t optlen = (socklen_t)sizeof(connect_rv); sa.sa6.sin6_scope_id = getDefaultIPv6Interface(&sa.sa6.sin6_addr);
#endif
switch (errno) {
case ENETUNREACH: /* Network Unreachable */
case EAFNOSUPPORT: /* Address Family not supported */
case EADDRNOTAVAIL: /* address is not available on the remote machine */
#if defined(__linux__) || defined(_AIX)
case EINVAL:
case EHOSTUNREACH: /* No route to host */
/*
* On some Linux versions, when a socket is bound to the
* loopback interface, connect will fail and errno will
* be set to EINVAL or EHOSTUNREACH. When that happens,
* don't throw an exception, just return false.
*/
#endif /* __linux__ */
close(fd);
return JNI_FALSE;
} }
if (errno != EINPROGRESS) { // load network interface address to SOCKETADDRESS, if specified
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ConnectException", if (!(IS_NULL(ifArray))) {
"connect failed"); memset((char *)caddr, 0, 16);
close(fd); (*env)->GetByteArrayRegion(env, ifArray, 0, 16, caddr);
return JNI_FALSE; memset((char *)&inf, 0, sizeof(SOCKETADDRESS));
memcpy((void *)&inf.sa6.sin6_addr, caddr, sizeof(struct in6_addr));
inf.sa6.sin6_family = AF_INET6;
inf.sa6.sin6_scope_id = if_scope;
netif = &inf;
} }
timeout = NET_Wait(env, fd, NET_WAIT_CONNECT, timeout); // Let's try to create a RAW socket to send ICMP packets.
// This usually requires "root" privileges, so it's likely to fail.
if (timeout >= 0) { fd = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
/* has connection been established */ if (fd == -1) {
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&connect_rv, return tcp_ping6(env, &sa, netif, timeout, ttl);
&optlen) <0) { } else {
connect_rv = errno; // It didn't fail, so we can use ICMP_ECHO requests.
} return ping6(env, fd, &sa, netif, timeout, ttl);
if (connect_rv == 0 || connect_rv == ECONNREFUSED) {
close(fd);
return JNI_TRUE;
}
}
close(fd);
return JNI_FALSE;
} }
} }