8167295: Further cleanup to the native parts of libnet/libnio

Reviewed-by: chegar
This commit is contained in:
Christoph Langer 2016-10-10 22:42:45 +02:00
parent 3d4d013f55
commit 0d046ab808
21 changed files with 381 additions and 464 deletions

View File

@ -98,7 +98,6 @@ SUNWprivate_1.1 {
Java_sun_net_sdp_SdpSupport_create0; Java_sun_net_sdp_SdpSupport_create0;
Java_sun_net_spi_DefaultProxySelector_init; Java_sun_net_spi_DefaultProxySelector_init;
Java_sun_net_spi_DefaultProxySelector_getSystemProxy; Java_sun_net_spi_DefaultProxySelector_getSystemProxy;
NET_AllocSockaddr;
NET_SockaddrToInetAddress; NET_SockaddrToInetAddress;
NET_SockaddrEqualsInetAddress; NET_SockaddrEqualsInetAddress;
NET_InetAddressToSockaddr; NET_InetAddressToSockaddr;

View File

@ -42,6 +42,10 @@
#define NET_ERROR(env, ex, msg) \ #define NET_ERROR(env, ex, msg) \
{ if (!(*env)->ExceptionOccurred(env)) JNU_ThrowByName(env, ex, msg); } { if (!(*env)->ExceptionOccurred(env)) JNU_ThrowByName(env, ex, msg); }
#define NET_WAIT_READ 0x01
#define NET_WAIT_WRITE 0x02
#define NET_WAIT_CONNECT 0x04
/************************************************************************ /************************************************************************
* Cached field IDs * Cached field IDs
* *
@ -133,9 +137,6 @@ JNIEXPORT jint JNICALL ipv6_available() ;
JNIEXPORT jint JNICALL reuseport_available() ; JNIEXPORT jint JNICALL reuseport_available() ;
void
NET_AllocSockaddr(struct sockaddr **him, int *len);
JNIEXPORT int JNICALL JNIEXPORT int JNICALL
NET_InetAddressToSockaddr(JNIEnv *env, jobject iaObj, int port, struct sockaddr *him, int *len, jboolean v4MappedAddress); NET_InetAddressToSockaddr(JNIEnv *env, jobject iaObj, int port, struct sockaddr *him, int *len, jboolean v4MappedAddress);
@ -196,4 +197,6 @@ int cmpScopeID (unsigned int, struct sockaddr *);
unsigned short in_cksum(unsigned short *addr, int len); unsigned short in_cksum(unsigned short *addr, int len);
jint NET_Wait(JNIEnv *env, jint fd, jint flags, jint timeout);
#endif /* NET_UTILS_H */ #endif /* NET_UTILS_H */

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -788,9 +788,9 @@ Java_java_net_Inet4AddressImpl_isReachable0(JNIEnv *env, jobject this,
case ENETUNREACH: /* Network Unreachable */ case ENETUNREACH: /* Network Unreachable */
case EAFNOSUPPORT: /* Address Family not supported */ case EAFNOSUPPORT: /* Address Family not supported */
case EADDRNOTAVAIL: /* address is not available on the remote machine */ case EADDRNOTAVAIL: /* address is not available on the remote machine */
#ifdef __linux__ #if defined(__linux__) || defined(_AIX)
case EINVAL: case EINVAL:
case EHOSTUNREACH: case EHOSTUNREACH: /* No route to host */
/* /*
* 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, connect will fail and errno will be set to EINVAL * interface, connect will fail and errno will be set to EINVAL

View File

@ -775,9 +775,9 @@ Java_java_net_Inet6AddressImpl_isReachable0(JNIEnv *env, jobject this,
case ENETUNREACH: /* Network Unreachable */ case ENETUNREACH: /* Network Unreachable */
case EAFNOSUPPORT: /* Address Family not supported */ case EAFNOSUPPORT: /* Address Family not supported */
case EADDRNOTAVAIL: /* address is not available on the remote machine */ case EADDRNOTAVAIL: /* address is not available on the remote machine */
#ifdef __linux__ #if defined(__linux__) || defined(_AIX)
case EINVAL: case EINVAL:
case EHOSTUNREACH: case EHOSTUNREACH: /* No route to host */
/* /*
* On some Linux versions, when a socket is bound to the * On some Linux versions, when a socket is bound to the
* loopback interface, connect will fail and errno will * loopback interface, connect will fail and errno will
@ -804,7 +804,7 @@ Java_java_net_Inet6AddressImpl_isReachable0(JNIEnv *env, jobject this,
&optlen) <0) { &optlen) <0) {
connect_rv = errno; connect_rv = errno;
} }
if (connect_rv == 0 || ECONNREFUSED) { if (connect_rv == 0 || connect_rv == ECONNREFUSED) {
close(fd); close(fd);
return JNI_TRUE; return JNI_TRUE;
} }

View File

@ -193,8 +193,8 @@ Java_java_net_PlainDatagramSocketImpl_bind0(JNIEnv *env, jobject this,
/* fd is an int field on fdObj */ /* fd is an int field on fdObj */
int fd; int fd;
int len = 0; int len = 0;
SOCKADDR him; SOCKETADDRESS him;
socklen_t slen = sizeof(him); socklen_t slen = sizeof(SOCKETADDRESS);
if (IS_NULL(fdObj)) { if (IS_NULL(fdObj)) {
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
@ -210,12 +210,12 @@ Java_java_net_PlainDatagramSocketImpl_bind0(JNIEnv *env, jobject this,
} }
/* bind */ /* bind */
if (NET_InetAddressToSockaddr(env, iaObj, localport, (struct sockaddr *)&him, &len, JNI_TRUE) != 0) { if (NET_InetAddressToSockaddr(env, iaObj, localport, &him.sa, &len, JNI_TRUE) != 0) {
return; return;
} }
setDefaultScopeID(env, (struct sockaddr *)&him); setDefaultScopeID(env, &him.sa);
if (NET_Bind(fd, (struct sockaddr *)&him, len) < 0) { if (NET_Bind(fd, &him.sa, len) < 0) {
if (errno == EADDRINUSE || errno == EADDRNOTAVAIL || if (errno == EADDRINUSE || errno == EADDRNOTAVAIL ||
errno == EPERM || errno == EACCES) { errno == EPERM || errno == EACCES) {
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "BindException", NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "BindException",
@ -232,13 +232,13 @@ Java_java_net_PlainDatagramSocketImpl_bind0(JNIEnv *env, jobject this,
/* Now that we're a connected socket, let's extract the port number /* Now that we're a connected socket, let's extract the port number
* that the system chose for us and store it in the Socket object. * that the system chose for us and store it in the Socket object.
*/ */
if (getsockname(fd, (struct sockaddr *)&him, &slen) == -1) { if (getsockname(fd, &him.sa, &slen) == -1) {
JNU_ThrowByNameWithMessageAndLastError JNU_ThrowByNameWithMessageAndLastError
(env, JNU_JAVANETPKG "SocketException", "Error getting socket name"); (env, JNU_JAVANETPKG "SocketException", "Error getting socket name");
return; return;
} }
localport = NET_GetPortFromSockaddr((struct sockaddr *)&him); localport = NET_GetPortFromSockaddr(&him.sa);
(*env)->SetIntField(env, this, pdsi_localPortID, localport); (*env)->SetIntField(env, this, pdsi_localPortID, localport);
} else { } else {
@ -259,7 +259,7 @@ Java_java_net_PlainDatagramSocketImpl_connect0(JNIEnv *env, jobject this,
/* The fdObj'fd */ /* The fdObj'fd */
jint fd; jint fd;
/* The packetAddress address, family and port */ /* The packetAddress address, family and port */
SOCKADDR rmtaddr; SOCKETADDRESS rmtaddr;
int len = 0; int len = 0;
if (IS_NULL(fdObj)) { if (IS_NULL(fdObj)) {
@ -274,18 +274,16 @@ Java_java_net_PlainDatagramSocketImpl_connect0(JNIEnv *env, jobject this,
return; return;
} }
if (NET_InetAddressToSockaddr(env, address, port, (struct sockaddr *)&rmtaddr, &len, JNI_TRUE) != 0) { if (NET_InetAddressToSockaddr(env, address, port, &rmtaddr.sa, &len, JNI_TRUE) != 0) {
return; return;
} }
setDefaultScopeID(env, (struct sockaddr *)&rmtaddr); setDefaultScopeID(env, &rmtaddr.sa);
if (NET_Connect(fd, (struct sockaddr *)&rmtaddr, len) == -1) { if (NET_Connect(fd, &rmtaddr.sa, len) == -1) {
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ConnectException", NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ConnectException",
"Connect failed"); "Connect failed");
return;
} }
} }
/* /*
@ -301,7 +299,7 @@ Java_java_net_PlainDatagramSocketImpl_disconnect0(JNIEnv *env, jobject this, jin
jint fd; jint fd;
#if defined(__linux__) || defined(_ALLBSD_SOURCE) #if defined(__linux__) || defined(_ALLBSD_SOURCE)
SOCKADDR addr; SOCKETADDRESS addr;
socklen_t len; socklen_t len;
#endif #endif
@ -314,36 +312,34 @@ Java_java_net_PlainDatagramSocketImpl_disconnect0(JNIEnv *env, jobject this, jin
memset(&addr, 0, sizeof(addr)); memset(&addr, 0, sizeof(addr));
#ifdef AF_INET6 #ifdef AF_INET6
if (ipv6_available()) { if (ipv6_available()) {
struct sockaddr_in6 *him6 = (struct sockaddr_in6 *)&addr; addr.sa6.sin6_family = AF_UNSPEC;
him6->sin6_family = AF_UNSPEC;
len = sizeof(struct sockaddr_in6); len = sizeof(struct sockaddr_in6);
} else } else
#endif #endif
{ {
struct sockaddr_in *him4 = (struct sockaddr_in*)&addr; addr.sa4.sin_family = AF_UNSPEC;
him4->sin_family = AF_UNSPEC;
len = sizeof(struct sockaddr_in); len = sizeof(struct sockaddr_in);
} }
NET_Connect(fd, (struct sockaddr *)&addr, len); NET_Connect(fd, &addr.sa, len);
#ifdef __linux__ #ifdef __linux__
int localPort = 0; int localPort = 0;
if (getsockname(fd, (struct sockaddr *)&addr, &len) == -1) if (getsockname(fd, &addr.sa, &len) == -1)
return; return;
localPort = NET_GetPortFromSockaddr((struct sockaddr *)&addr); localPort = NET_GetPortFromSockaddr(&addr.sa);
if (localPort == 0) { if (localPort == 0) {
localPort = (*env)->GetIntField(env, this, pdsi_localPortID); localPort = (*env)->GetIntField(env, this, pdsi_localPortID);
#ifdef AF_INET6 #ifdef AF_INET6
if (((struct sockaddr*)&addr)->sa_family == AF_INET6) { if (addr.sa.sa_family == AF_INET6) {
((struct sockaddr_in6*)&addr)->sin6_port = htons(localPort); addr.sa6.sin6_port = htons(localPort);
} else } else
#endif /* AF_INET6 */ #endif /* AF_INET6 */
{ {
((struct sockaddr_in*)&addr)->sin_port = htons(localPort); addr.sa4.sin_port = htons(localPort);
} }
NET_Bind(fd, (struct sockaddr *)&addr, len); NET_Bind(fd, &addr.sa, len);
} }
#endif #endif
@ -376,7 +372,7 @@ Java_java_net_PlainDatagramSocketImpl_send(JNIEnv *env, jobject this,
/* The fdObj'fd */ /* The fdObj'fd */
jint fd; jint fd;
SOCKADDR rmtaddr, *rmtaddrP=&rmtaddr; SOCKETADDRESS rmtaddr, *rmtaddrP = &rmtaddr;
int len; int len;
if (IS_NULL(fdObj)) { if (IS_NULL(fdObj)) {
@ -409,11 +405,11 @@ Java_java_net_PlainDatagramSocketImpl_send(JNIEnv *env, jobject this,
rmtaddrP = 0; rmtaddrP = 0;
} else { } else {
packetPort = (*env)->GetIntField(env, packet, dp_portID); packetPort = (*env)->GetIntField(env, packet, dp_portID);
if (NET_InetAddressToSockaddr(env, packetAddress, packetPort, (struct sockaddr *)&rmtaddr, &len, JNI_TRUE) != 0) { if (NET_InetAddressToSockaddr(env, packetAddress, packetPort, &rmtaddr.sa, &len, JNI_TRUE) != 0) {
return; return;
} }
} }
setDefaultScopeID(env, (struct sockaddr *)&rmtaddr); setDefaultScopeID(env, &rmtaddr.sa);
if (packetBufferLen > MAX_BUFFER_LEN) { if (packetBufferLen > MAX_BUFFER_LEN) {
/* When JNI-ifying the JDK's IO routines, we turned /* When JNI-ifying the JDK's IO routines, we turned
@ -449,7 +445,7 @@ Java_java_net_PlainDatagramSocketImpl_send(JNIEnv *env, jobject this,
(jbyte *)fullPacket); (jbyte *)fullPacket);
#ifdef AF_INET6 #ifdef AF_INET6
if (trafficClass != 0 && ipv6_available()) { if (trafficClass != 0 && ipv6_available()) {
NET_SetTrafficClass((struct sockaddr *)&rmtaddr, trafficClass); NET_SetTrafficClass(&rmtaddr.sa, trafficClass);
} }
#endif /* AF_INET6 */ #endif /* AF_INET6 */
@ -492,8 +488,8 @@ Java_java_net_PlainDatagramSocketImpl_peek(JNIEnv *env, jobject this,
jint timeout = (*env)->GetIntField(env, this, pdsi_timeoutID); jint timeout = (*env)->GetIntField(env, this, pdsi_timeoutID);
jint fd; jint fd;
ssize_t n; ssize_t n;
SOCKADDR remote_addr; SOCKETADDRESS rmtaddr;
socklen_t slen = SOCKADDR_LEN; socklen_t slen = sizeof(SOCKETADDRESS);
char buf[1]; char buf[1];
jint family; jint family;
jobject iaObj; jobject iaObj;
@ -527,7 +523,7 @@ Java_java_net_PlainDatagramSocketImpl_peek(JNIEnv *env, jobject this,
} }
} }
n = NET_RecvFrom(fd, buf, 1, MSG_PEEK, (struct sockaddr *)&remote_addr, &slen); n = NET_RecvFrom(fd, buf, 1, MSG_PEEK, &rmtaddr.sa, &slen);
if (n == -1) { if (n == -1) {
@ -552,7 +548,7 @@ Java_java_net_PlainDatagramSocketImpl_peek(JNIEnv *env, jobject this,
return 0; return 0;
} }
iaObj = NET_SockaddrToInetAddress(env, (struct sockaddr *)&remote_addr, &port); iaObj = NET_SockaddrToInetAddress(env, &rmtaddr.sa, &port);
#ifdef AF_INET6 #ifdef AF_INET6
family = getInetAddress_family(env, iaObj) == IPv4? AF_INET : AF_INET6; family = getInetAddress_family(env, iaObj) == IPv4? AF_INET : AF_INET6;
#else #else
@ -574,16 +570,13 @@ Java_java_net_PlainDatagramSocketImpl_peekData(JNIEnv *env, jobject this,
int mallocedPacket = JNI_FALSE; int mallocedPacket = JNI_FALSE;
jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID); jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
jint timeout = (*env)->GetIntField(env, this, pdsi_timeoutID); jint timeout = (*env)->GetIntField(env, this, pdsi_timeoutID);
jbyteArray packetBuffer; jbyteArray packetBuffer;
jint packetBufferOffset, packetBufferLen; jint packetBufferOffset, packetBufferLen;
int fd; int fd;
int n; int n;
SOCKADDR remote_addr; SOCKETADDRESS rmtaddr;
socklen_t slen = SOCKADDR_LEN; socklen_t slen = sizeof(SOCKETADDRESS);
int port; int port = -1;
if (IS_NULL(fdObj)) { if (IS_NULL(fdObj)) {
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
@ -661,7 +654,7 @@ Java_java_net_PlainDatagramSocketImpl_peekData(JNIEnv *env, jobject this,
} }
n = NET_RecvFrom(fd, fullPacket, packetBufferLen, MSG_PEEK, n = NET_RecvFrom(fd, fullPacket, packetBufferLen, MSG_PEEK,
(struct sockaddr *)&remote_addr, &slen); &rmtaddr.sa, &slen);
/* truncate the data if the packet's length is too small */ /* truncate the data if the packet's length is too small */
if (n > packetBufferLen) { if (n > packetBufferLen) {
n = packetBufferLen; n = packetBufferLen;
@ -706,18 +699,18 @@ Java_java_net_PlainDatagramSocketImpl_peekData(JNIEnv *env, jobject this,
*/ */
packetAddress = (*env)->GetObjectField(env, packet, dp_addressID); packetAddress = (*env)->GetObjectField(env, packet, dp_addressID);
if (packetAddress != NULL) { if (packetAddress != NULL) {
if (!NET_SockaddrEqualsInetAddress(env, (struct sockaddr *)&remote_addr, packetAddress)) { if (!NET_SockaddrEqualsInetAddress(env, &rmtaddr.sa, packetAddress)) {
/* force a new InetAddress to be created */ /* force a new InetAddress to be created */
packetAddress = NULL; packetAddress = NULL;
} }
} }
if (packetAddress == NULL) { if (packetAddress == NULL) {
packetAddress = NET_SockaddrToInetAddress(env, (struct sockaddr *)&remote_addr, &port); packetAddress = NET_SockaddrToInetAddress(env, &rmtaddr.sa, &port);
/* stuff the new Inetaddress in the packet */ /* stuff the new Inetaddress in the packet */
(*env)->SetObjectField(env, packet, dp_addressID, packetAddress); (*env)->SetObjectField(env, packet, dp_addressID, packetAddress);
} else { } else {
/* only get the new port number */ /* only get the new port number */
port = NET_GetPortFromSockaddr((struct sockaddr *)&remote_addr); port = NET_GetPortFromSockaddr(&rmtaddr.sa);
} }
/* and fill in the data, remote address/port and such */ /* and fill in the data, remote address/port and such */
(*env)->SetByteArrayRegion(env, packetBuffer, packetBufferOffset, n, (*env)->SetByteArrayRegion(env, packetBuffer, packetBufferOffset, n,
@ -753,8 +746,8 @@ Java_java_net_PlainDatagramSocketImpl_receive0(JNIEnv *env, jobject this,
int fd; int fd;
int n; int n;
SOCKADDR remote_addr; SOCKETADDRESS rmtaddr;
socklen_t slen = SOCKADDR_LEN; socklen_t slen = sizeof(SOCKETADDRESS);
jboolean retry; jboolean retry;
#ifdef __linux__ #ifdef __linux__
jboolean connected = JNI_FALSE; jboolean connected = JNI_FALSE;
@ -849,7 +842,7 @@ Java_java_net_PlainDatagramSocketImpl_receive0(JNIEnv *env, jobject this,
} }
n = NET_RecvFrom(fd, fullPacket, packetBufferLen, 0, n = NET_RecvFrom(fd, fullPacket, packetBufferLen, 0,
(struct sockaddr *)&remote_addr, &slen); &rmtaddr.sa, &slen);
/* truncate the data if the packet's length is too small */ /* truncate the data if the packet's length is too small */
if (n > packetBufferLen) { if (n > packetBufferLen) {
n = packetBufferLen; n = packetBufferLen;
@ -887,18 +880,18 @@ Java_java_net_PlainDatagramSocketImpl_receive0(JNIEnv *env, jobject this,
*/ */
packetAddress = (*env)->GetObjectField(env, packet, dp_addressID); packetAddress = (*env)->GetObjectField(env, packet, dp_addressID);
if (packetAddress != NULL) { if (packetAddress != NULL) {
if (!NET_SockaddrEqualsInetAddress(env, (struct sockaddr *)&remote_addr, packetAddress)) { if (!NET_SockaddrEqualsInetAddress(env, &rmtaddr.sa, packetAddress)) {
/* force a new InetAddress to be created */ /* force a new InetAddress to be created */
packetAddress = NULL; packetAddress = NULL;
} }
} }
if (packetAddress == NULL) { if (packetAddress == NULL) {
packetAddress = NET_SockaddrToInetAddress(env, (struct sockaddr *)&remote_addr, &port); packetAddress = NET_SockaddrToInetAddress(env, &rmtaddr.sa, &port);
/* stuff the new Inetaddress in the packet */ /* stuff the new Inetaddress in the packet */
(*env)->SetObjectField(env, packet, dp_addressID, packetAddress); (*env)->SetObjectField(env, packet, dp_addressID, packetAddress);
} else { } else {
/* only get the new port number */ /* only get the new port number */
port = NET_GetPortFromSockaddr((struct sockaddr *)&remote_addr); port = NET_GetPortFromSockaddr(&rmtaddr.sa);
} }
/* and fill in the data, remote address/port and such */ /* and fill in the data, remote address/port and such */
(*env)->SetByteArrayRegion(env, packetBuffer, packetBufferOffset, n, (*env)->SetByteArrayRegion(env, packetBuffer, packetBufferOffset, n,
@ -1729,19 +1722,17 @@ Java_java_net_PlainDatagramSocketImpl_socketGetOption
*/ */
if (opt == java_net_SocketOptions_SO_BINDADDR) { if (opt == java_net_SocketOptions_SO_BINDADDR) {
/* find out local IP address */ /* find out local IP address */
SOCKADDR him; SOCKETADDRESS him;
socklen_t len = 0; socklen_t len = sizeof(SOCKETADDRESS);
int port; int port;
jobject iaObj; jobject iaObj;
len = SOCKADDR_LEN; if (getsockname(fd, &him.sa, &len) == -1) {
if (getsockname(fd, (struct sockaddr *)&him, &len) == -1) {
JNU_ThrowByNameWithMessageAndLastError JNU_ThrowByNameWithMessageAndLastError
(env, JNU_JAVANETPKG "SocketException", "Error getting socket name"); (env, JNU_JAVANETPKG "SocketException", "Error getting socket name");
return NULL; return NULL;
} }
iaObj = NET_SockaddrToInetAddress(env, (struct sockaddr *)&him, &port); iaObj = NET_SockaddrToInetAddress(env, &him.sa, &port);
return iaObj; return iaObj;
} }

View File

@ -274,7 +274,7 @@ Java_java_net_PlainSocketImpl_socketConnect(JNIEnv *env, jobject this,
/* fd is an int field on iaObj */ /* fd is an int field on iaObj */
jint fd; jint fd;
SOCKADDR him; SOCKETADDRESS him;
/* The result of the connection */ /* The result of the connection */
int connect_rv = -1; int connect_rv = -1;
@ -290,18 +290,18 @@ Java_java_net_PlainSocketImpl_socketConnect(JNIEnv *env, jobject this,
} }
/* connect */ /* connect */
if (NET_InetAddressToSockaddr(env, iaObj, port, (struct sockaddr *)&him, &len, JNI_TRUE) != 0) { if (NET_InetAddressToSockaddr(env, iaObj, port, &him.sa, &len, JNI_TRUE) != 0) {
return; return;
} }
setDefaultScopeID(env, (struct sockaddr *)&him); setDefaultScopeID(env, &him.sa);
#ifdef AF_INET6 #ifdef AF_INET6
if (trafficClass != 0 && ipv6_available()) { if (trafficClass != 0 && ipv6_available()) {
NET_SetTrafficClass((struct sockaddr *)&him, trafficClass); NET_SetTrafficClass(&him.sa, trafficClass);
} }
#endif /* AF_INET6 */ #endif /* AF_INET6 */
if (timeout <= 0) { if (timeout <= 0) {
connect_rv = NET_Connect(fd, (struct sockaddr *)&him, len); connect_rv = NET_Connect(fd, &him.sa, len);
#ifdef __solaris__ #ifdef __solaris__
if (connect_rv == -1 && errno == EINPROGRESS ) { if (connect_rv == -1 && errno == EINPROGRESS ) {
@ -350,7 +350,7 @@ Java_java_net_PlainSocketImpl_socketConnect(JNIEnv *env, jobject this,
SET_NONBLOCKING(fd); SET_NONBLOCKING(fd);
/* no need to use NET_Connect as non-blocking */ /* no need to use NET_Connect as non-blocking */
connect_rv = connect(fd, (struct sockaddr *)&him, len); connect_rv = connect(fd, &him.sa, len);
/* connection not established immediately */ /* connection not established immediately */
if (connect_rv != 0) { if (connect_rv != 0) {
@ -497,12 +497,12 @@ Java_java_net_PlainSocketImpl_socketConnect(JNIEnv *env, jobject this,
/* Now that we're a connected socket, let's extract the port number /* Now that we're a connected socket, let's extract the port number
* that the system chose for us and store it in the Socket object. * that the system chose for us and store it in the Socket object.
*/ */
socklen_t slen = SOCKADDR_LEN; socklen_t slen = sizeof(SOCKETADDRESS);
if (getsockname(fd, (struct sockaddr *)&him, &slen) == -1) { if (getsockname(fd, &him.sa, &slen) == -1) {
JNU_ThrowByNameWithMessageAndLastError JNU_ThrowByNameWithMessageAndLastError
(env, JNU_JAVANETPKG "SocketException", "Error getting socket name"); (env, JNU_JAVANETPKG "SocketException", "Error getting socket name");
} else { } else {
localport = NET_GetPortFromSockaddr((struct sockaddr *)&him); localport = NET_GetPortFromSockaddr(&him.sa);
(*env)->SetIntField(env, this, psi_localportID, localport); (*env)->SetIntField(env, this, psi_localportID, localport);
} }
} }
@ -522,7 +522,7 @@ Java_java_net_PlainSocketImpl_socketBind(JNIEnv *env, jobject this,
/* fd is an int field on fdObj */ /* fd is an int field on fdObj */
int fd; int fd;
int len; int len;
SOCKADDR him; SOCKETADDRESS him;
if (IS_NULL(fdObj)) { if (IS_NULL(fdObj)) {
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
@ -537,13 +537,13 @@ Java_java_net_PlainSocketImpl_socketBind(JNIEnv *env, jobject this,
} }
/* bind */ /* bind */
if (NET_InetAddressToSockaddr(env, iaObj, localport, (struct sockaddr *)&him, if (NET_InetAddressToSockaddr(env, iaObj, localport, &him.sa,
&len, JNI_TRUE) != 0) { &len, JNI_TRUE) != 0) {
return; return;
} }
setDefaultScopeID(env, (struct sockaddr *)&him); setDefaultScopeID(env, &him.sa);
if (NET_Bind(fd, (struct sockaddr *)&him, len) < 0) { if (NET_Bind(fd, &him.sa, len) < 0) {
if (errno == EADDRINUSE || errno == EADDRNOTAVAIL || if (errno == EADDRINUSE || errno == EADDRNOTAVAIL ||
errno == EPERM || errno == EACCES) { errno == EPERM || errno == EACCES) {
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "BindException", NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "BindException",
@ -560,16 +560,16 @@ Java_java_net_PlainSocketImpl_socketBind(JNIEnv *env, jobject this,
/* initialize the local port */ /* initialize the local port */
if (localport == 0) { if (localport == 0) {
socklen_t slen = sizeof(him); socklen_t slen = sizeof(SOCKETADDRESS);
/* Now that we're a connected socket, let's extract the port number /* Now that we're a connected socket, let's extract the port number
* that the system chose for us and store it in the Socket object. * that the system chose for us and store it in the Socket object.
*/ */
if (getsockname(fd, (struct sockaddr *)&him, &slen) == -1) { if (getsockname(fd, &him.sa, &slen) == -1) {
JNU_ThrowByNameWithMessageAndLastError JNU_ThrowByNameWithMessageAndLastError
(env, JNU_JAVANETPKG "SocketException", "Error getting socket name"); (env, JNU_JAVANETPKG "SocketException", "Error getting socket name");
return; return;
} }
localport = NET_GetPortFromSockaddr((struct sockaddr *)&him); localport = NET_GetPortFromSockaddr(&him.sa);
(*env)->SetIntField(env, this, psi_localportID, localport); (*env)->SetIntField(env, this, psi_localportID, localport);
} else { } else {
(*env)->SetIntField(env, this, psi_localportID, localport); (*env)->SetIntField(env, this, psi_localportID, localport);
@ -637,8 +637,8 @@ Java_java_net_PlainSocketImpl_socketAccept(JNIEnv *env, jobject this,
/* accepted fd */ /* accepted fd */
jint newfd; jint newfd;
SOCKADDR him; SOCKETADDRESS him;
socklen_t slen = SOCKADDR_LEN; socklen_t slen = sizeof(SOCKETADDRESS);
if (IS_NULL(fdObj)) { if (IS_NULL(fdObj)) {
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
@ -692,7 +692,7 @@ Java_java_net_PlainSocketImpl_socketAccept(JNIEnv *env, jobject this,
return; return;
} }
newfd = NET_Accept(fd, (struct sockaddr *)&him, &slen); newfd = NET_Accept(fd, &him.sa, &slen);
/* connection accepted */ /* connection accepted */
if (newfd >= 0) { if (newfd >= 0) {
@ -740,7 +740,7 @@ Java_java_net_PlainSocketImpl_socketAccept(JNIEnv *env, jobject this,
/* /*
* fill up the remote peer port and address in the new socket structure. * fill up the remote peer port and address in the new socket structure.
*/ */
socketAddressObj = NET_SockaddrToInetAddress(env, (struct sockaddr *)&him, &port); socketAddressObj = NET_SockaddrToInetAddress(env, &him.sa, &port);
if (socketAddressObj == NULL) { if (socketAddressObj == NULL) {
/* should be pending exception */ /* should be pending exception */
close(newfd); close(newfd);
@ -975,21 +975,19 @@ Java_java_net_PlainSocketImpl_socketGetOption
* SO_BINDADDR isn't a socket option * SO_BINDADDR isn't a socket option
*/ */
if (cmd == java_net_SocketOptions_SO_BINDADDR) { if (cmd == java_net_SocketOptions_SO_BINDADDR) {
SOCKADDR him; SOCKETADDRESS him;
socklen_t len = 0; socklen_t len = sizeof(SOCKETADDRESS);
int port; int port;
jobject iaObj; jobject iaObj;
jclass iaCntrClass; jclass iaCntrClass;
jfieldID iaFieldID; jfieldID iaFieldID;
len = SOCKADDR_LEN; if (getsockname(fd, &him.sa, &len) < 0) {
if (getsockname(fd, (struct sockaddr *)&him, &len) < 0) {
JNU_ThrowByNameWithMessageAndLastError JNU_ThrowByNameWithMessageAndLastError
(env, JNU_JAVANETPKG "SocketException", "Error getting socket name"); (env, JNU_JAVANETPKG "SocketException", "Error getting socket name");
return -1; return -1;
} }
iaObj = NET_SockaddrToInetAddress(env, (struct sockaddr *)&him, &port); iaObj = NET_SockaddrToInetAddress(env, &him.sa, &port);
CHECK_NULL_RETURN(iaObj, -1); CHECK_NULL_RETURN(iaObj, -1);
iaCntrClass = (*env)->GetObjectClass(env, iaContainerObj); iaCntrClass = (*env)->GetObjectClass(env, iaContainerObj);

View File

@ -334,8 +334,8 @@ jint IPv6_supported()
#ifdef AF_INET6 #ifdef AF_INET6
int fd; int fd;
void *ipv6_fn; void *ipv6_fn;
SOCKADDR sa; SOCKETADDRESS sa;
socklen_t sa_len = sizeof(sa); socklen_t sa_len = sizeof(SOCKETADDRESS);
fd = socket(AF_INET6, SOCK_STREAM, 0) ; fd = socket(AF_INET6, SOCK_STREAM, 0) ;
if (fd < 0) { if (fd < 0) {
@ -351,9 +351,8 @@ jint IPv6_supported()
* xinetd. If it's a socket then check the family - if it's an * xinetd. If it's a socket then check the family - if it's an
* IPv4 socket then we need to disable IPv6. * IPv4 socket then we need to disable IPv6.
*/ */
if (getsockname(0, (struct sockaddr *)&sa, &sa_len) == 0) { if (getsockname(0, &sa.sa, &sa_len) == 0) {
struct sockaddr *saP = (struct sockaddr *)&sa; if (sa.sa.sa_family != AF_INET6) {
if (saP->sa_family != AF_INET6) {
close(fd); close(fd);
return JNI_FALSE; return JNI_FALSE;
} }
@ -488,25 +487,8 @@ void NET_ThrowUnknownHostExceptionWithGaiError(JNIEnv *env,
} }
} }
void
NET_AllocSockaddr(struct sockaddr **him, int *len) {
#ifdef AF_INET6
if (ipv6_available()) {
struct sockaddr_in6 *him6 = (struct sockaddr_in6*)malloc(sizeof(struct sockaddr_in6));
*him = (struct sockaddr*)him6;
*len = sizeof(struct sockaddr_in6);
} else
#endif /* AF_INET6 */
{
struct sockaddr_in *him4 = (struct sockaddr_in*)malloc(sizeof(struct sockaddr_in));
*him = (struct sockaddr*)him4;
*len = sizeof(struct sockaddr_in);
}
}
#if defined(__linux__) && defined(AF_INET6) #if defined(__linux__) && defined(AF_INET6)
/* following code creates a list of addresses from the kernel /* following code creates a list of addresses from the kernel
* routing table that are routed via the loopback address. * routing table that are routed via the loopback address.
* We check all destination addresses against this table * We check all destination addresses against this table

View File

@ -60,9 +60,9 @@ void NET_ThrowUnknownHostExceptionWithGaiError(JNIEnv *env,
void NET_ThrowByNameWithLastError(JNIEnv *env, const char *name, void NET_ThrowByNameWithLastError(JNIEnv *env, const char *name,
const char *defaultDetail); const char *defaultDetail);
#define NET_WAIT_READ 0x01 /************************************************************************
#define NET_WAIT_WRITE 0x02 * Macros and constants
#define NET_WAIT_CONNECT 0x04 */
/* Defines SO_REUSEPORT */ /* Defines SO_REUSEPORT */
#ifndef SO_REUSEPORT #ifndef SO_REUSEPORT
@ -77,12 +77,6 @@ void NET_ThrowByNameWithLastError(JNIEnv *env, const char *name,
#endif #endif
#endif #endif
jint NET_Wait(JNIEnv *env, jint fd, jint flags, jint timeout);
/************************************************************************
* Macros and constants
*/
/* /*
* On 64-bit JDKs we use a much larger stack and heap buffer. * On 64-bit JDKs we use a much larger stack and heap buffer.
*/ */
@ -95,20 +89,16 @@ jint NET_Wait(JNIEnv *env, jint fd, jint flags, jint timeout);
#endif #endif
#ifdef AF_INET6 #ifdef AF_INET6
typedef union {
#define SOCKADDR union { \ struct sockaddr sa;
struct sockaddr_in him4; \ struct sockaddr_in sa4;
struct sockaddr_in6 him6; \ struct sockaddr_in6 sa6;
} } SOCKETADDRESS;
#define SOCKADDR_LEN (ipv6_available() ? sizeof(SOCKADDR) : \
sizeof(struct sockaddr_in))
#else #else
typedef union {
#define SOCKADDR union { struct sockaddr_in him4; } struct sockaddr sa;
#define SOCKADDR_LEN sizeof(SOCKADDR) struct sockaddr_in sa4;
} SOCKETADDRESS;
#endif #endif
/************************************************************************ /************************************************************************

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -87,58 +87,50 @@ Java_sun_nio_ch_DatagramChannelImpl_disconnect0(JNIEnv *env, jobject this,
jint fd = fdval(env, fdo); jint fd = fdval(env, fdo);
int rv; int rv;
#ifdef __solaris__ #if defined(__solaris__)
rv = connect(fd, 0, 0); rv = connect(fd, 0, 0);
#endif #else
int len;
SOCKETADDRESS sa;
#if defined(__linux__) || defined(_ALLBSD_SOURCE) || defined(_AIX) memset(&sa, 0, sizeof(sa));
{
int len;
SOCKADDR sa;
memset(&sa, 0, sizeof(sa));
#ifdef AF_INET6 #ifdef AF_INET6
if (isIPv6) { if (isIPv6) {
struct sockaddr_in6 *him6 = (struct sockaddr_in6 *)&sa;
#if defined(_ALLBSD_SOURCE) #if defined(_ALLBSD_SOURCE)
him6->sin6_family = AF_INET6; sa.sa6.sin6_family = AF_INET6;
#else #else
him6->sin6_family = AF_UNSPEC; sa.sa6.sin6_family = AF_UNSPEC;
#endif #endif
len = sizeof(struct sockaddr_in6); len = sizeof(struct sockaddr_in6);
} else } else
#endif #endif
{ {
struct sockaddr_in *him4 = (struct sockaddr_in*)&sa;
#if defined(_ALLBSD_SOURCE) #if defined(_ALLBSD_SOURCE)
him4->sin_family = AF_INET; sa.sa4.sin_family = AF_INET;
#else #else
him4->sin_family = AF_UNSPEC; sa.sa4.sin_family = AF_UNSPEC;
#endif
len = sizeof(struct sockaddr_in);
}
rv = connect(fd, (struct sockaddr *)&sa, len);
#if defined(_ALLBSD_SOURCE)
if (rv < 0 && errno == EADDRNOTAVAIL)
rv = errno = 0;
#endif
#if defined(_AIX)
/* See W. Richard Stevens, "UNIX Network Programming, Volume 1", p. 254:
* 'Setting the address family to AF_UNSPEC might return EAFNOSUPPORT
* but that is acceptable.
*/
if (rv < 0 && errno == EAFNOSUPPORT)
rv = errno = 0;
#endif #endif
len = sizeof(struct sockaddr_in);
} }
rv = connect(fd, &sa.sa, len);
#if defined(_ALLBSD_SOURCE)
if (rv < 0 && errno == EADDRNOTAVAIL)
rv = errno = 0;
#elif defined(_AIX)
/* See W. Richard Stevens, "UNIX Network Programming, Volume 1", p. 254:
* 'Setting the address family to AF_UNSPEC might return EAFNOSUPPORT
* but that is acceptable.
*/
if (rv < 0 && errno == EAFNOSUPPORT)
rv = errno = 0;
#endif
#endif #endif
if (rv < 0) if (rv < 0)
handleSocketError(env, errno); handleSocketError(env, errno);
} }
JNIEXPORT jint JNICALL JNIEXPORT jint JNICALL
@ -148,8 +140,8 @@ Java_sun_nio_ch_DatagramChannelImpl_receive0(JNIEnv *env, jobject this,
{ {
jint fd = fdval(env, fdo); jint fd = fdval(env, fdo);
void *buf = (void *)jlong_to_ptr(address); void *buf = (void *)jlong_to_ptr(address);
SOCKADDR sa; SOCKETADDRESS sa;
socklen_t sa_len = SOCKADDR_LEN; socklen_t sa_len = sizeof(SOCKETADDRESS);
jboolean retry = JNI_FALSE; jboolean retry = JNI_FALSE;
jint n = 0; jint n = 0;
jobject senderAddr; jobject senderAddr;
@ -160,7 +152,7 @@ Java_sun_nio_ch_DatagramChannelImpl_receive0(JNIEnv *env, jobject this,
do { do {
retry = JNI_FALSE; retry = JNI_FALSE;
n = recvfrom(fd, buf, len, 0, (struct sockaddr *)&sa, &sa_len); n = recvfrom(fd, buf, len, 0, &sa.sa, &sa_len);
if (n < 0) { if (n < 0) {
if (errno == EWOULDBLOCK) { if (errno == EWOULDBLOCK) {
return IOS_UNAVAILABLE; return IOS_UNAVAILABLE;
@ -189,12 +181,11 @@ Java_sun_nio_ch_DatagramChannelImpl_receive0(JNIEnv *env, jobject this,
*/ */
senderAddr = (*env)->GetObjectField(env, this, dci_senderAddrID); senderAddr = (*env)->GetObjectField(env, this, dci_senderAddrID);
if (senderAddr != NULL) { if (senderAddr != NULL) {
if (!NET_SockaddrEqualsInetAddress(env, (struct sockaddr *)&sa, if (!NET_SockaddrEqualsInetAddress(env, &sa.sa, senderAddr)) {
senderAddr)) {
senderAddr = NULL; senderAddr = NULL;
} else { } else {
jint port = (*env)->GetIntField(env, this, dci_senderPortID); jint port = (*env)->GetIntField(env, this, dci_senderPortID);
if (port != NET_GetPortFromSockaddr((struct sockaddr *)&sa)) { if (port != NET_GetPortFromSockaddr(&sa.sa)) {
senderAddr = NULL; senderAddr = NULL;
} }
} }
@ -202,7 +193,7 @@ Java_sun_nio_ch_DatagramChannelImpl_receive0(JNIEnv *env, jobject this,
if (senderAddr == NULL) { if (senderAddr == NULL) {
jobject isa = NULL; jobject isa = NULL;
int port = 0; int port = 0;
jobject ia = NET_SockaddrToInetAddress(env, (struct sockaddr *)&sa, &port); jobject ia = NET_SockaddrToInetAddress(env, &sa.sa, &port);
if (ia != NULL) { if (ia != NULL) {
isa = (*env)->NewObject(env, isa_class, isa_ctorID, ia, port); isa = (*env)->NewObject(env, isa_class, isa_ctorID, ia, port);
} }
@ -210,7 +201,7 @@ Java_sun_nio_ch_DatagramChannelImpl_receive0(JNIEnv *env, jobject this,
(*env)->SetObjectField(env, this, dci_senderAddrID, ia); (*env)->SetObjectField(env, this, dci_senderAddrID, ia);
(*env)->SetIntField(env, this, dci_senderPortID, (*env)->SetIntField(env, this, dci_senderPortID,
NET_GetPortFromSockaddr((struct sockaddr *)&sa)); NET_GetPortFromSockaddr(&sa.sa));
(*env)->SetObjectField(env, this, dci_senderID, isa); (*env)->SetObjectField(env, this, dci_senderID, isa);
} }
return n; return n;
@ -223,21 +214,20 @@ Java_sun_nio_ch_DatagramChannelImpl_send0(JNIEnv *env, jobject this,
{ {
jint fd = fdval(env, fdo); jint fd = fdval(env, fdo);
void *buf = (void *)jlong_to_ptr(address); void *buf = (void *)jlong_to_ptr(address);
SOCKADDR sa; SOCKETADDRESS sa;
int sa_len = SOCKADDR_LEN; int sa_len = sizeof(SOCKETADDRESS);
jint n = 0; jint n = 0;
if (len > MAX_PACKET_LEN) { if (len > MAX_PACKET_LEN) {
len = MAX_PACKET_LEN; len = MAX_PACKET_LEN;
} }
if (NET_InetAddressToSockaddr(env, destAddress, destPort, if (NET_InetAddressToSockaddr(env, destAddress, destPort, &sa.sa,
(struct sockaddr *)&sa,
&sa_len, preferIPv6) != 0) { &sa_len, preferIPv6) != 0) {
return IOS_THROWN; return IOS_THROWN;
} }
n = sendto(fd, buf, len, 0, (struct sockaddr *)&sa, sa_len); n = sendto(fd, buf, len, 0, &sa.sa, sa_len);
if (n < 0) { if (n < 0) {
if (errno == EAGAIN) { if (errno == EAGAIN) {
return IOS_UNAVAILABLE; return IOS_UNAVAILABLE;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -57,18 +57,16 @@ Java_sun_nio_ch_InheritedChannel_initIDs(JNIEnv *env, jclass cla)
JNIEXPORT jobject JNICALL JNIEXPORT jobject JNICALL
Java_sun_nio_ch_InheritedChannel_peerAddress0(JNIEnv *env, jclass cla, jint fd) Java_sun_nio_ch_InheritedChannel_peerAddress0(JNIEnv *env, jclass cla, jint fd)
{ {
struct sockaddr *sa; SOCKETADDRESS sa;
socklen_t sa_len; socklen_t len = sizeof(SOCKETADDRESS);
jobject remote_ia = NULL; jobject remote_ia = NULL;
jint remote_port; jint remote_port;
NET_AllocSockaddr(&sa, (int *)&sa_len); if (getpeername(fd, &sa.sa, &len) == 0) {
if (getpeername(fd, sa, &sa_len) == 0) { if (matchFamily(&sa.sa)) {
if (matchFamily(sa)) { remote_ia = NET_SockaddrToInetAddress(env, &sa.sa, (int *)&remote_port);
remote_ia = NET_SockaddrToInetAddress(env, sa, (int *)&remote_port);
} }
} }
free((void *)sa);
return remote_ia; return remote_ia;
} }
@ -77,17 +75,15 @@ Java_sun_nio_ch_InheritedChannel_peerAddress0(JNIEnv *env, jclass cla, jint fd)
JNIEXPORT jint JNICALL JNIEXPORT jint JNICALL
Java_sun_nio_ch_InheritedChannel_peerPort0(JNIEnv *env, jclass cla, jint fd) Java_sun_nio_ch_InheritedChannel_peerPort0(JNIEnv *env, jclass cla, jint fd)
{ {
struct sockaddr *sa; SOCKETADDRESS sa;
socklen_t sa_len; socklen_t len = sizeof(SOCKETADDRESS);
jint remote_port = -1; jint remote_port = -1;
NET_AllocSockaddr(&sa, (int *)&sa_len); if (getpeername(fd, &sa.sa, &len) == 0) {
if (getpeername(fd, sa, &sa_len) == 0) { if (matchFamily(&sa.sa)) {
if (matchFamily(sa)) { NET_SockaddrToInetAddress(env, &sa.sa, (int *)&remote_port);
NET_SockaddrToInetAddress(env, sa, (int *)&remote_port);
} }
} }
free((void *)sa);
return remote_port; return remote_port;
} }

View File

@ -273,15 +273,16 @@ JNIEXPORT void JNICALL
Java_sun_nio_ch_Net_bind0(JNIEnv *env, jclass clazz, jobject fdo, jboolean preferIPv6, Java_sun_nio_ch_Net_bind0(JNIEnv *env, jclass clazz, jobject fdo, jboolean preferIPv6,
jboolean useExclBind, jobject iao, int port) jboolean useExclBind, jobject iao, int port)
{ {
SOCKADDR sa; SOCKETADDRESS sa;
int sa_len = SOCKADDR_LEN; int sa_len = sizeof(SOCKETADDRESS);
int rv = 0; int rv = 0;
if (NET_InetAddressToSockaddr(env, iao, port, (struct sockaddr *)&sa, &sa_len, preferIPv6) != 0) { if (NET_InetAddressToSockaddr(env, iao, port, &sa.sa, &sa_len,
return; preferIPv6) != 0) {
return;
} }
rv = NET_Bind(fdval(env, fdo), (struct sockaddr *)&sa, sa_len); rv = NET_Bind(fdval(env, fdo), &sa.sa, sa_len);
if (rv != 0) { if (rv != 0) {
handleSocketError(env, errno); handleSocketError(env, errno);
} }
@ -298,17 +299,16 @@ JNIEXPORT jint JNICALL
Java_sun_nio_ch_Net_connect0(JNIEnv *env, jclass clazz, jboolean preferIPv6, Java_sun_nio_ch_Net_connect0(JNIEnv *env, jclass clazz, jboolean preferIPv6,
jobject fdo, jobject iao, jint port) jobject fdo, jobject iao, jint port)
{ {
SOCKADDR sa; SOCKETADDRESS sa;
int sa_len = SOCKADDR_LEN; int sa_len = sizeof(SOCKETADDRESS);
int rv; int rv;
if (NET_InetAddressToSockaddr(env, iao, port, (struct sockaddr *) &sa, if (NET_InetAddressToSockaddr(env, iao, port, &sa.sa, &sa_len,
&sa_len, preferIPv6) != 0) preferIPv6) != 0) {
{ return IOS_THROWN;
return IOS_THROWN;
} }
rv = connect(fdval(env, fdo), (struct sockaddr *)&sa, sa_len); rv = connect(fdval(env, fdo), &sa.sa, sa_len);
if (rv != 0) { if (rv != 0) {
if (errno == EINPROGRESS) { if (errno == EINPROGRESS) {
return IOS_UNAVAILABLE; return IOS_UNAVAILABLE;
@ -323,9 +323,9 @@ Java_sun_nio_ch_Net_connect0(JNIEnv *env, jclass clazz, jboolean preferIPv6,
JNIEXPORT jint JNICALL JNIEXPORT jint JNICALL
Java_sun_nio_ch_Net_localPort(JNIEnv *env, jclass clazz, jobject fdo) Java_sun_nio_ch_Net_localPort(JNIEnv *env, jclass clazz, jobject fdo)
{ {
SOCKADDR sa; SOCKETADDRESS sa;
socklen_t sa_len = SOCKADDR_LEN; socklen_t sa_len = sizeof(SOCKETADDRESS);
if (getsockname(fdval(env, fdo), (struct sockaddr *)&sa, &sa_len) < 0) { if (getsockname(fdval(env, fdo), &sa.sa, &sa_len) < 0) {
#ifdef _ALLBSD_SOURCE #ifdef _ALLBSD_SOURCE
/* /*
* XXXBSD: * XXXBSD:
@ -335,13 +335,11 @@ Java_sun_nio_ch_Net_localPort(JNIEnv *env, jclass clazz, jobject fdo)
* it shouldn't fail. As such, we just fill in generic Linux-compatible values. * it shouldn't fail. As such, we just fill in generic Linux-compatible values.
*/ */
if (errno == ECONNRESET) { if (errno == ECONNRESET) {
struct sockaddr_in *sin; bzero(&sa.sa4, sizeof(sa));
sin = (struct sockaddr_in *) &sa; sa.sa4.sin_len = sizeof(struct sockaddr_in);
bzero(sin, sizeof(*sin)); sa.sa4.sin_family = AF_INET;
sin->sin_len = sizeof(struct sockaddr_in); sa.sa4.sin_port = htonl(0);
sin->sin_family = AF_INET; sa.sa4.sin_addr.s_addr = INADDR_ANY;
sin->sin_port = htonl(0);
sin->sin_addr.s_addr = INADDR_ANY;
} else { } else {
handleSocketError(env, errno); handleSocketError(env, errno);
return -1; return -1;
@ -351,16 +349,16 @@ Java_sun_nio_ch_Net_localPort(JNIEnv *env, jclass clazz, jobject fdo)
return -1; return -1;
#endif /* _ALLBSD_SOURCE */ #endif /* _ALLBSD_SOURCE */
} }
return NET_GetPortFromSockaddr((struct sockaddr *)&sa); return NET_GetPortFromSockaddr(&sa.sa);
} }
JNIEXPORT jobject JNICALL JNIEXPORT jobject JNICALL
Java_sun_nio_ch_Net_localInetAddress(JNIEnv *env, jclass clazz, jobject fdo) Java_sun_nio_ch_Net_localInetAddress(JNIEnv *env, jclass clazz, jobject fdo)
{ {
SOCKADDR sa; SOCKETADDRESS sa;
socklen_t sa_len = SOCKADDR_LEN; socklen_t sa_len = sizeof(SOCKETADDRESS);
int port; int port;
if (getsockname(fdval(env, fdo), (struct sockaddr *)&sa, &sa_len) < 0) { if (getsockname(fdval(env, fdo), &sa.sa, &sa_len) < 0) {
#ifdef _ALLBSD_SOURCE #ifdef _ALLBSD_SOURCE
/* /*
* XXXBSD: * XXXBSD:
@ -370,13 +368,11 @@ Java_sun_nio_ch_Net_localInetAddress(JNIEnv *env, jclass clazz, jobject fdo)
* it shouldn't fail. As such, we just fill in generic Linux-compatible values. * it shouldn't fail. As such, we just fill in generic Linux-compatible values.
*/ */
if (errno == ECONNRESET) { if (errno == ECONNRESET) {
struct sockaddr_in *sin; bzero(&sa.sa4, sizeof(sa));
sin = (struct sockaddr_in *) &sa; sa.sa4.sin_len = sizeof(struct sockaddr_in);
bzero(sin, sizeof(*sin)); sa.sa4.sin_family = AF_INET;
sin->sin_len = sizeof(struct sockaddr_in); sa.sa4.sin_port = htonl(0);
sin->sin_family = AF_INET; sa.sa4.sin_addr.s_addr = INADDR_ANY;
sin->sin_port = htonl(0);
sin->sin_addr.s_addr = INADDR_ANY;
} else { } else {
handleSocketError(env, errno); handleSocketError(env, errno);
return NULL; return NULL;
@ -386,7 +382,7 @@ Java_sun_nio_ch_Net_localInetAddress(JNIEnv *env, jclass clazz, jobject fdo)
return NULL; return NULL;
#endif /* _ALLBSD_SOURCE */ #endif /* _ALLBSD_SOURCE */
} }
return NET_SockaddrToInetAddress(env, (struct sockaddr *)&sa, &port); return NET_SockaddrToInetAddress(env, &sa.sa, &port);
} }
JNIEXPORT jint JNICALL JNIEXPORT jint JNICALL

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2000, 2008, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -80,26 +80,19 @@ Java_sun_nio_ch_ServerSocketChannelImpl_accept0(JNIEnv *env, jobject this,
{ {
jint ssfd = (*env)->GetIntField(env, ssfdo, fd_fdID); jint ssfd = (*env)->GetIntField(env, ssfdo, fd_fdID);
jint newfd; jint newfd;
struct sockaddr *sa; SOCKETADDRESS sa;
int alloc_len; socklen_t sa_len = sizeof(SOCKETADDRESS);
jobject remote_ia = 0; jobject remote_ia = 0;
jobject isa; jobject isa;
jint remote_port = 0; jint remote_port = 0;
NET_AllocSockaddr(&sa, &alloc_len);
if (sa == NULL) {
JNU_ThrowOutOfMemoryError(env, NULL);
return IOS_THROWN;
}
/* /*
* accept connection but ignore ECONNABORTED indicating that * accept connection but ignore ECONNABORTED indicating that
* a connection was eagerly accepted but was reset before * a connection was eagerly accepted but was reset before
* accept() was called. * accept() was called.
*/ */
for (;;) { for (;;) {
socklen_t sa_len = alloc_len; newfd = accept(ssfd, &sa.sa, &sa_len);
newfd = accept(ssfd, sa, &sa_len);
if (newfd >= 0) { if (newfd >= 0) {
break; break;
} }
@ -110,7 +103,6 @@ Java_sun_nio_ch_ServerSocketChannelImpl_accept0(JNIEnv *env, jobject this,
} }
if (newfd < 0) { if (newfd < 0) {
free((void *)sa);
if (errno == EAGAIN) if (errno == EAGAIN)
return IOS_UNAVAILABLE; return IOS_UNAVAILABLE;
if (errno == EINTR) if (errno == EINTR)
@ -120,8 +112,7 @@ Java_sun_nio_ch_ServerSocketChannelImpl_accept0(JNIEnv *env, jobject this,
} }
(*env)->SetIntField(env, newfdo, fd_fdID, newfd); (*env)->SetIntField(env, newfdo, fd_fdID, newfd);
remote_ia = NET_SockaddrToInetAddress(env, sa, (int *)&remote_port); remote_ia = NET_SockaddrToInetAddress(env, &sa.sa, (int *)&remote_port);
free((void *)sa);
CHECK_NULL_RETURN(remote_ia, IOS_THROWN); CHECK_NULL_RETURN(remote_ia, IOS_THROWN);
isa = (*env)->NewObject(env, isa_class, isa_ctorID, remote_ia, remote_port); isa = (*env)->NewObject(env, isa_class, isa_ctorID, remote_ia, remote_port);
CHECK_NULL_RETURN(isa, IOS_THROWN); CHECK_NULL_RETURN(isa, IOS_THROWN);

View File

@ -83,7 +83,7 @@ Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
jboolean preferIPv6Address; jboolean preferIPv6Address;
int error=0; int error=0;
struct addrinfo hints, *res, *resNew = NULL; struct addrinfo hints, *res = NULL, *resNew = NULL;
initInetAddressIDs(env); initInetAddressIDs(env);
JNU_CHECK_EXCEPTION_RETURN(env, NULL); JNU_CHECK_EXCEPTION_RETURN(env, NULL);
@ -223,7 +223,7 @@ Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
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;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -429,8 +429,8 @@ int enumAddresses_win(JNIEnv *env, netif *netifP, netaddr **netaddrPP)
return -1; return -1;
} }
curr->addr.him4.sin_family = AF_INET; curr->addr.sa4.sin_family = AF_INET;
curr->addr.him4.sin_addr.s_addr = tableP->table[i].dwAddr; curr->addr.sa4.sin_addr.s_addr = tableP->table[i].dwAddr;
/* /*
* Get netmask / broadcast address * Get netmask / broadcast address
*/ */
@ -447,11 +447,11 @@ int enumAddresses_win(JNIEnv *env, netif *netifP, netaddr **netaddrPP)
* to 0 or 1. * to 0 or 1.
* Yes, I know it's stupid, but what can I say, it's MSFTs API. * Yes, I know it's stupid, but what can I say, it's MSFTs API.
*/ */
curr->brdcast.him4.sin_family = AF_INET; curr->brdcast.sa4.sin_family = AF_INET;
if (tableP->table[i].dwBCastAddr == 1) if (tableP->table[i].dwBCastAddr == 1)
curr->brdcast.him4.sin_addr.s_addr = (tableP->table[i].dwAddr & tableP->table[i].dwMask) | (0xffffffff ^ tableP->table[i].dwMask); curr->brdcast.sa4.sin_addr.s_addr = (tableP->table[i].dwAddr & tableP->table[i].dwMask) | (0xffffffff ^ tableP->table[i].dwMask);
else else
curr->brdcast.him4.sin_addr.s_addr = (tableP->table[i].dwAddr & tableP->table[i].dwMask); curr->brdcast.sa4.sin_addr.s_addr = (tableP->table[i].dwAddr & tableP->table[i].dwMask);
mask = ntohl(tableP->table[i].dwMask); mask = ntohl(tableP->table[i].dwMask);
curr->mask = 0; curr->mask = 0;
while (mask) { while (mask) {
@ -584,7 +584,7 @@ jobject createNetworkInterface
while (addrs != NULL) { while (addrs != NULL) {
jobject iaObj, ia2Obj; jobject iaObj, ia2Obj;
jobject ibObj = NULL; jobject ibObj = NULL;
if (addrs->addr.him.sa_family == AF_INET) { if (addrs->addr.sa.sa_family == AF_INET) {
iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID); iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
if (iaObj == NULL) { if (iaObj == NULL) {
free_netaddr(netaddrP); free_netaddr(netaddrP);
@ -592,7 +592,7 @@ jobject createNetworkInterface
} }
/* default ctor will set family to AF_INET */ /* default ctor will set family to AF_INET */
setInetAddress_addr(env, iaObj, ntohl(addrs->addr.him4.sin_addr.s_addr)); setInetAddress_addr(env, iaObj, ntohl(addrs->addr.sa4.sin_addr.s_addr));
if (addrs->mask != -1) { if (addrs->mask != -1) {
ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID); ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID);
if (ibObj == NULL) { if (ibObj == NULL) {
@ -605,7 +605,7 @@ jobject createNetworkInterface
free_netaddr(netaddrP); free_netaddr(netaddrP);
return NULL; return NULL;
} }
setInetAddress_addr(env, ia2Obj, ntohl(addrs->brdcast.him4.sin_addr.s_addr)); setInetAddress_addr(env, ia2Obj, ntohl(addrs->brdcast.sa4.sin_addr.s_addr));
(*env)->SetObjectField(env, ibObj, ni_ibbroadcastID, ia2Obj); (*env)->SetObjectField(env, ibObj, ni_ibbroadcastID, ia2Obj);
(*env)->SetShortField(env, ibObj, ni_ibmaskID, addrs->mask); (*env)->SetShortField(env, ibObj, ni_ibmaskID, addrs->mask);
(*env)->SetObjectArrayElement(env, bindsArr, bind_index++, ibObj); (*env)->SetObjectArrayElement(env, bindsArr, bind_index++, ibObj);
@ -614,12 +614,12 @@ jobject createNetworkInterface
int scope; int scope;
iaObj = (*env)->NewObject(env, ia6_class, ia6_ctrID); iaObj = (*env)->NewObject(env, ia6_class, ia6_ctrID);
if (iaObj) { if (iaObj) {
jboolean ret = setInet6Address_ipaddress(env, iaObj, (jbyte *)&(addrs->addr.him6.sin6_addr.s6_addr)); jboolean ret = setInet6Address_ipaddress(env, iaObj, (jbyte *)&(addrs->addr.sa6.sin6_addr.s6_addr));
if (ret == JNI_FALSE) { if (ret == JNI_FALSE) {
return NULL; return NULL;
} }
scope = addrs->addr.him6.sin6_scope_id; scope = addrs->addr.sa6.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);
setInet6Address_scopeifname(env, iaObj, netifObj); setInet6Address_scopeifname(env, iaObj, netifObj);
@ -795,7 +795,7 @@ JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByInetAddress0
addrP = addrList; addrP = addrList;
while (addrP != NULL) { while (addrP != NULL) {
if ((unsigned long)addr == ntohl(addrP->addr.him4.sin_addr.s_addr)) { if ((unsigned long)addr == ntohl(addrP->addr.sa4.sin_addr.s_addr)) {
break; break;
} }
addrP = addrP->next; addrP = addrP->next;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -595,14 +595,14 @@ static jobject createNetworkInterfaceXP(JNIEnv *env, netif *ifs)
while (addrs != NULL) { while (addrs != NULL) {
jobject iaObj, ia2Obj; jobject iaObj, ia2Obj;
jobject ibObj = NULL; jobject ibObj = NULL;
if (addrs->addr.him.sa_family == AF_INET) { if (addrs->addr.sa.sa_family == AF_INET) {
iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID); iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
if (iaObj == NULL) { if (iaObj == NULL) {
return NULL; return NULL;
} }
/* default ctor will set family to AF_INET */ /* default ctor will set family to AF_INET */
setInetAddress_addr(env, iaObj, ntohl(addrs->addr.him4.sin_addr.s_addr)); setInetAddress_addr(env, iaObj, ntohl(addrs->addr.sa4.sin_addr.s_addr));
ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID); ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID);
if (ibObj == NULL) { if (ibObj == NULL) {
@ -615,7 +615,7 @@ static jobject createNetworkInterfaceXP(JNIEnv *env, netif *ifs)
free_netaddr(netaddrP); free_netaddr(netaddrP);
return NULL; return NULL;
} }
setInetAddress_addr(env, ia2Obj, ntohl(addrs->brdcast.him4.sin_addr.s_addr)); setInetAddress_addr(env, ia2Obj, ntohl(addrs->brdcast.sa4.sin_addr.s_addr));
(*env)->SetObjectField(env, ibObj, ni_ibbroadcastID, ia2Obj); (*env)->SetObjectField(env, ibObj, ni_ibbroadcastID, ia2Obj);
(*env)->SetShortField(env, ibObj, ni_ibmaskID, addrs->mask); (*env)->SetShortField(env, ibObj, ni_ibmaskID, addrs->mask);
(*env)->SetObjectArrayElement(env, bindsArr, bind_index++, ibObj); (*env)->SetObjectArrayElement(env, bindsArr, bind_index++, ibObj);
@ -623,11 +623,11 @@ static jobject createNetworkInterfaceXP(JNIEnv *env, netif *ifs)
int scope; int scope;
iaObj = (*env)->NewObject(env, ia6_class, ia6_ctrID); iaObj = (*env)->NewObject(env, ia6_class, ia6_ctrID);
if (iaObj) { if (iaObj) {
jboolean ret = setInet6Address_ipaddress(env, iaObj, (jbyte *)&(addrs->addr.him6.sin6_addr.s6_addr)); jboolean ret = setInet6Address_ipaddress(env, iaObj, (jbyte *)&(addrs->addr.sa6.sin6_addr.s6_addr));
if (ret == JNI_FALSE) { if (ret == JNI_FALSE) {
return NULL; return NULL;
} }
scope = addrs->addr.him6.sin6_scope_id; scope = addrs->addr.sa6.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);
setInet6Address_scopeifname(env, iaObj, netifObj); setInet6Address_scopeifname(env, iaObj, netifObj);

View File

@ -51,6 +51,8 @@
#define IN_CLASSD(i) (((long)(i) & 0xf0000000) == 0xe0000000) #define IN_CLASSD(i) (((long)(i) & 0xf0000000) == 0xe0000000)
#define IN_MULTICAST(i) IN_CLASSD(i) #define IN_MULTICAST(i) IN_CLASSD(i)
extern int getAllInterfacesAndAddresses(JNIEnv *env, netif **netifPP);
/************************************************************************ /************************************************************************
* TwoStacksPlainDatagramSocketImpl * TwoStacksPlainDatagramSocketImpl
*/ */
@ -88,7 +90,7 @@ static int w2k_or_later = 0;
* Returns a java.lang.Integer based on 'i' * Returns a java.lang.Integer based on 'i'
*/ */
jobject createInteger(JNIEnv *env, int i) { jobject createInteger(JNIEnv *env, int i) {
static jclass i_class; static jclass i_class = NULL;
static jmethodID i_ctrID; static jmethodID i_ctrID;
static jfieldID i_valueID; static jfieldID i_valueID;
@ -101,14 +103,14 @@ jobject createInteger(JNIEnv *env, int i) {
CHECK_NULL_RETURN(i_class, NULL); CHECK_NULL_RETURN(i_class, NULL);
} }
return ( (*env)->NewObject(env, i_class, i_ctrID, i) ); return (*env)->NewObject(env, i_class, i_ctrID, i);
} }
/* /*
* Returns a java.lang.Boolean based on 'b' * Returns a java.lang.Boolean based on 'b'
*/ */
jobject createBoolean(JNIEnv *env, int b) { jobject createBoolean(JNIEnv *env, int b) {
static jclass b_class; static jclass b_class = NULL;
static jmethodID b_ctrID; static jmethodID b_ctrID;
static jfieldID b_valueID; static jfieldID b_valueID;
@ -121,10 +123,9 @@ jobject createBoolean(JNIEnv *env, int b) {
CHECK_NULL_RETURN(b_class, NULL); CHECK_NULL_RETURN(b_class, NULL);
} }
return( (*env)->NewObject(env, b_class, b_ctrID, (jboolean)(b!=0)) ); return (*env)->NewObject(env, b_class, b_ctrID, (jboolean)(b!=0));
} }
static int getFD(JNIEnv *env, jobject this) { static int getFD(JNIEnv *env, jobject this) {
jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID); jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
@ -333,7 +334,7 @@ static jboolean purgeOutstandingICMP(JNIEnv *env, jobject this, jint fd)
fd_set tbl; fd_set tbl;
struct timeval t = { 0, 0 }; struct timeval t = { 0, 0 };
SOCKETADDRESS rmtaddr; SOCKETADDRESS rmtaddr;
int addrlen = sizeof(rmtaddr); int addrlen = sizeof(SOCKETADDRESS);
memset((char *)&rmtaddr, 0, sizeof(rmtaddr)); memset((char *)&rmtaddr, 0, sizeof(rmtaddr));
@ -354,8 +355,7 @@ static jboolean purgeOutstandingICMP(JNIEnv *env, jobject this, jint fd)
if (select(/*ignored*/fd+1, &tbl, 0, 0, &t) <= 0) { if (select(/*ignored*/fd+1, &tbl, 0, 0, &t) <= 0) {
break; break;
} }
if (recvfrom(fd, buf, 1, MSG_PEEK, if (recvfrom(fd, buf, 1, MSG_PEEK, &rmtaddr.sa, &addrlen) != SOCKET_ERROR) {
(struct sockaddr *)&rmtaddr, &addrlen) != SOCKET_ERROR) {
break; break;
} }
if (WSAGetLastError() != WSAECONNRESET) { if (WSAGetLastError() != WSAECONNRESET) {
@ -363,7 +363,7 @@ static jboolean purgeOutstandingICMP(JNIEnv *env, jobject this, jint fd)
break; break;
} }
recvfrom(fd, buf, 1, 0, (struct sockaddr *)&rmtaddr, &addrlen); recvfrom(fd, buf, 1, 0, &rmtaddr.sa, &addrlen);
got_icmp = JNI_TRUE; got_icmp = JNI_TRUE;
} }
@ -429,11 +429,11 @@ Java_java_net_TwoStacksPlainDatagramSocketImpl_bind0(JNIEnv *env, jobject this,
jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID); jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
jobject fd1Obj = (*env)->GetObjectField(env, this, pdsi_fd1ID); jobject fd1Obj = (*env)->GetObjectField(env, this, pdsi_fd1ID);
int fd, fd1, family; int fd, fd1 = -1, family;
int ipv6_supported = ipv6_available(); int ipv6_supported = ipv6_available();
SOCKETADDRESS lcladdr; SOCKETADDRESS lcladdr;
int lcladdrlen = sizeof(lcladdr); int lcladdrlen = sizeof(SOCKETADDRESS);
int address; int address;
memset((char *)&lcladdr, 0, sizeof(lcladdr)); memset((char *)&lcladdr, 0, sizeof(lcladdr));
@ -461,8 +461,9 @@ Java_java_net_TwoStacksPlainDatagramSocketImpl_bind0(JNIEnv *env, jobject this,
address = getInetAddress_addr(env, addressObj); address = getInetAddress_addr(env, addressObj);
} }
if (NET_InetAddressToSockaddr(env, addressObj, port, (struct sockaddr *)&lcladdr, &lcladdrlen, JNI_FALSE) != 0) { if (NET_InetAddressToSockaddr(env, addressObj, port, &lcladdr.sa,
return; &lcladdrlen, JNI_FALSE) != 0) {
return;
} }
if (ipv6_supported) { if (ipv6_supported) {
@ -500,7 +501,7 @@ Java_java_net_TwoStacksPlainDatagramSocketImpl_bind0(JNIEnv *env, jobject this,
return; return;
} }
} else { } else {
if (NET_WinBind(fd, (struct sockaddr *)&lcladdr, lcladdrlen, exclBind) == -1) { if (NET_WinBind(fd, &lcladdr.sa, lcladdrlen, exclBind) == -1) {
if (WSAGetLastError() == WSAEACCES) { if (WSAGetLastError() == WSAEACCES) {
WSASetLastError(WSAEADDRINUSE); WSASetLastError(WSAEADDRINUSE);
} }
@ -510,11 +511,7 @@ Java_java_net_TwoStacksPlainDatagramSocketImpl_bind0(JNIEnv *env, jobject this,
} }
if (port == 0) { if (port == 0) {
if (fd == -1) { if (getsockname(fd == -1 ? fd1 : fd, &lcladdr.sa, &lcladdrlen) == -1) {
/* must be an IPV6 only socket. */
fd = fd1;
}
if (getsockname(fd, (struct sockaddr *)&lcladdr, &lcladdrlen) == -1) {
NET_ThrowCurrent(env, "getsockname"); NET_ThrowCurrent(env, "getsockname");
return; return;
} }
@ -583,11 +580,12 @@ Java_java_net_TwoStacksPlainDatagramSocketImpl_connect0(JNIEnv *env, jobject thi
res = WSAIoctl(fdc,SIO_UDP_CONNRESET,&t,sizeof(t),&x1,sizeof(x1),&x2,0,0); res = WSAIoctl(fdc,SIO_UDP_CONNRESET,&t,sizeof(t),&x1,sizeof(x1),&x2,0,0);
} }
if (NET_InetAddressToSockaddr(env, address, port,(struct sockaddr *)&rmtaddr, &rmtaddrlen, JNI_FALSE) != 0) { if (NET_InetAddressToSockaddr(env, address, port, &rmtaddr.sa,
return; &rmtaddrlen, JNI_FALSE) != 0) {
return;
} }
if (connect(fdc, (struct sockaddr *)&rmtaddr, sizeof(rmtaddr)) == -1) { if (connect(fdc, &rmtaddr.sa, sizeof(rmtaddr)) == -1) {
NET_ThrowCurrent(env, "connect"); NET_ThrowCurrent(env, "connect");
return; return;
} }
@ -622,7 +620,7 @@ Java_java_net_TwoStacksPlainDatagramSocketImpl_disconnect0(JNIEnv *env, jobject
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
memset((char *)&addr, 0, len); memset((char *)&addr, 0, len);
connect(fd, (struct sockaddr *)&addr, len); connect(fd, &addr.sa, len);
/* /*
* use SIO_UDP_CONNRESET * use SIO_UDP_CONNRESET
@ -657,8 +655,7 @@ Java_java_net_TwoStacksPlainDatagramSocketImpl_send(JNIEnv *env, jobject this,
jbyteArray packetBuffer; jbyteArray packetBuffer;
jboolean connected; jboolean connected;
SOCKETADDRESS rmtaddr; SOCKETADDRESS rmtaddr, *addrp = &rmtaddr;
SOCKETADDRESS *addrp = &rmtaddr;
int addrlen = 0; int addrlen = 0;
memset((char *)&rmtaddr, 0, sizeof(rmtaddr)); memset((char *)&rmtaddr, 0, sizeof(rmtaddr));
@ -711,9 +708,10 @@ Java_java_net_TwoStacksPlainDatagramSocketImpl_send(JNIEnv *env, jobject this,
addrp = 0; /* arg to sendto () null in this case */ addrp = 0; /* arg to sendto () null in this case */
addrlen = 0; addrlen = 0;
} else { } else {
if (NET_InetAddressToSockaddr(env, iaObj, packetPort, (struct sockaddr *)&rmtaddr, &addrlen, JNI_FALSE) != 0) { if (NET_InetAddressToSockaddr(env, iaObj, packetPort, &rmtaddr.sa,
return; &addrlen, JNI_FALSE) != 0) {
} return;
}
} }
if (packetBufferLen > MAX_BUFFER_LEN) { if (packetBufferLen > MAX_BUFFER_LEN) {
@ -732,7 +730,7 @@ Java_java_net_TwoStacksPlainDatagramSocketImpl_send(JNIEnv *env, jobject this,
if (connected) { if (connected) {
address = getInetAddress_addr(env, iaObj); address = getInetAddress_addr(env, iaObj);
} else { } else {
address = ntohl(rmtaddr.him4.sin_addr.s_addr); address = ntohl(rmtaddr.sa4.sin_addr.s_addr);
} }
if (exceedSizeLimit(env, fd, address, packetBufferLen)) { if (exceedSizeLimit(env, fd, address, packetBufferLen)) {
@ -813,8 +811,8 @@ Java_java_net_TwoStacksPlainDatagramSocketImpl_peek(JNIEnv *env, jobject this,
jint address, family; jint address, family;
int n; int n;
struct sockaddr_in remote_addr; SOCKETADDRESS remote_addr;
jint remote_addrsize = sizeof (remote_addr); jint remote_addrsize = sizeof(SOCKETADDRESS);
char buf[1]; char buf[1];
BOOL retry; BOOL retry;
jlong prevTime = 0; jlong prevTime = 0;
@ -860,8 +858,7 @@ Java_java_net_TwoStacksPlainDatagramSocketImpl_peek(JNIEnv *env, jobject this,
} }
/* now try the peek */ /* now try the peek */
n = recvfrom(fd, buf, 1, MSG_PEEK, n = recvfrom(fd, buf, 1, MSG_PEEK, &remote_addr.sa, &remote_addrsize);
(struct sockaddr *)&remote_addr, &remote_addrsize);
if (n == SOCKET_ERROR) { if (n == SOCKET_ERROR) {
if (WSAGetLastError() == WSAECONNRESET) { if (WSAGetLastError() == WSAECONNRESET) {
@ -907,11 +904,11 @@ Java_java_net_TwoStacksPlainDatagramSocketImpl_peek(JNIEnv *env, jobject this,
NET_ThrowCurrent(env, "Datagram peek failed"); NET_ThrowCurrent(env, "Datagram peek failed");
return 0; return 0;
} }
setInetAddress_addr(env, addressObj, ntohl(remote_addr.sin_addr.s_addr)); setInetAddress_addr(env, addressObj, ntohl(remote_addr.sa4.sin_addr.s_addr));
setInetAddress_family(env, addressObj, IPv4); setInetAddress_family(env, addressObj, IPv4);
/* return port */ /* return port */
return ntohs(remote_addr.sin_port); return ntohs(remote_addr.sa4.sin_port);
} }
JNIEXPORT jint JNICALL JNIEXPORT jint JNICALL
@ -927,13 +924,13 @@ Java_java_net_TwoStacksPlainDatagramSocketImpl_peekData(JNIEnv *env, jobject thi
jbyteArray packetBuffer; jbyteArray packetBuffer;
jint packetBufferOffset, packetBufferLen; jint packetBufferOffset, packetBufferLen;
int fd, fd1, fduse, nsockets=0, errorCode; int fd = -1, fd1 = -1, fduse, nsockets = 0, errorCode;
int port; int port;
int checkBoth = 0; int checkBoth = 0;
int n; int n;
SOCKETADDRESS remote_addr; SOCKETADDRESS remote_addr;
jint remote_addrsize=sizeof(remote_addr); jint remote_addrsize = sizeof(SOCKETADDRESS);
BOOL retry; BOOL retry;
jlong prevTime = 0; jlong prevTime = 0;
@ -1063,7 +1060,7 @@ Java_java_net_TwoStacksPlainDatagramSocketImpl_peekData(JNIEnv *env, jobject thi
/* receive the packet */ /* receive the packet */
n = recvfrom(fduse, fullPacket, packetBufferLen, MSG_PEEK, n = recvfrom(fduse, fullPacket, packetBufferLen, MSG_PEEK,
(struct sockaddr *)&remote_addr, &remote_addrsize); &remote_addr.sa, &remote_addrsize);
port = (int) ntohs ((u_short) GET_PORT((SOCKETADDRESS *)&remote_addr)); port = (int) ntohs ((u_short) GET_PORT((SOCKETADDRESS *)&remote_addr));
if (n == SOCKET_ERROR) { if (n == SOCKET_ERROR) {
if (WSAGetLastError() == WSAECONNRESET) { if (WSAGetLastError() == WSAECONNRESET) {
@ -1145,15 +1142,15 @@ Java_java_net_TwoStacksPlainDatagramSocketImpl_peekData(JNIEnv *env, jobject thi
*/ */
packetAddress = (*env)->GetObjectField(env, packet, dp_addressID); packetAddress = (*env)->GetObjectField(env, packet, dp_addressID);
if (packetAddress != NULL) { if (packetAddress != NULL) {
if (!NET_SockaddrEqualsInetAddress(env, (struct sockaddr *) if (!NET_SockaddrEqualsInetAddress(env, &remote_addr.sa,
&remote_addr, packetAddress)) { packetAddress)) {
/* force a new InetAddress to be created */ /* force a new InetAddress to be created */
packetAddress = NULL; packetAddress = NULL;
} }
} }
if (packetAddress == NULL) { if (packetAddress == NULL) {
packetAddress = NET_SockaddrToInetAddress(env, (struct sockaddr *) packetAddress = NET_SockaddrToInetAddress(env, &remote_addr.sa,
&remote_addr, &port); &port);
/* stuff the new Inetaddress in the packet */ /* stuff the new Inetaddress in the packet */
(*env)->SetObjectField(env, packet, dp_addressID, packetAddress); (*env)->SetObjectField(env, packet, dp_addressID, packetAddress);
} }
@ -1195,11 +1192,11 @@ Java_java_net_TwoStacksPlainDatagramSocketImpl_receive0(JNIEnv *env, jobject thi
/* as a result of the changes for ipv6, peek() or peekData() /* as a result of the changes for ipv6, peek() or peekData()
* must be called prior to receive() so that fduse can be set. * must be called prior to receive() so that fduse can be set.
*/ */
int fd, fd1, fduse, errorCode; int fd = -1, fd1 = -1, fduse, errorCode;
int n, nsockets=0; int n, nsockets=0;
SOCKETADDRESS remote_addr; SOCKETADDRESS remote_addr;
jint remote_addrsize=sizeof(remote_addr); jint remote_addrsize = sizeof(SOCKETADDRESS);
BOOL retry; BOOL retry;
jlong prevTime = 0, selectTime=0; jlong prevTime = 0, selectTime=0;
jboolean connected; jboolean connected;
@ -1327,8 +1324,8 @@ Java_java_net_TwoStacksPlainDatagramSocketImpl_receive0(JNIEnv *env, jobject thi
retry = FALSE; retry = FALSE;
/* receive the packet */ /* receive the packet */
n = recvfrom(fduse, fullPacket, packetBufferLen, 0, n = recvfrom(fduse, fullPacket, packetBufferLen, 0, &remote_addr.sa,
(struct sockaddr *)&remote_addr, &remote_addrsize); &remote_addrsize);
if (n == SOCKET_ERROR) { if (n == SOCKET_ERROR) {
if (WSAGetLastError() == WSAECONNRESET) { if (WSAGetLastError() == WSAECONNRESET) {
@ -1431,18 +1428,18 @@ Java_java_net_TwoStacksPlainDatagramSocketImpl_receive0(JNIEnv *env, jobject thi
packetAddress = (*env)->GetObjectField(env, packet, dp_addressID); packetAddress = (*env)->GetObjectField(env, packet, dp_addressID);
if (packetAddress != NULL) { if (packetAddress != NULL) {
if (!NET_SockaddrEqualsInetAddress(env, (struct sockaddr *)&remote_addr, packetAddress)) { if (!NET_SockaddrEqualsInetAddress(env, &remote_addr.sa, packetAddress)) {
/* force a new InetAddress to be created */ /* force a new InetAddress to be created */
packetAddress = NULL; packetAddress = NULL;
} }
} }
if (packetAddress == NULL) { if (packetAddress == NULL) {
packetAddress = NET_SockaddrToInetAddress(env, (struct sockaddr *)&remote_addr, &port); packetAddress = NET_SockaddrToInetAddress(env, &remote_addr.sa, &port);
/* stuff the new Inetaddress in the packet */ /* stuff the new Inetaddress in the packet */
(*env)->SetObjectField(env, packet, dp_addressID, packetAddress); (*env)->SetObjectField(env, packet, dp_addressID, packetAddress);
} else { } else {
/* only get the new port number */ /* only get the new port number */
port = NET_GetPortFromSockaddr((struct sockaddr *)&remote_addr); port = NET_GetPortFromSockaddr(&remote_addr.sa);
} }
/* populate the packet */ /* populate the packet */
(*env)->SetByteArrayRegion(env, packetBuffer, packetBufferOffset, n, (*env)->SetByteArrayRegion(env, packetBuffer, packetBufferOffset, n,
@ -1612,7 +1609,7 @@ static int getInet4AddrFromIf (JNIEnv *env, jobject nif, struct in_addr *iaddr)
/* Get the multicasting index from the interface */ /* Get the multicasting index from the interface */
static int getIndexFromIf (JNIEnv *env, jobject nif) { static int getIndexFromIf (JNIEnv *env, jobject nif) {
static jfieldID ni_indexID; static jfieldID ni_indexID = NULL;
if (ni_indexID == NULL) { if (ni_indexID == NULL) {
jclass c = (*env)->FindClass(env, "java/net/NetworkInterface"); jclass c = (*env)->FindClass(env, "java/net/NetworkInterface");
@ -1625,10 +1622,9 @@ static int getIndexFromIf (JNIEnv *env, jobject nif) {
} }
static int isAdapterIpv6Enabled(JNIEnv *env, int index) { static int isAdapterIpv6Enabled(JNIEnv *env, int index) {
extern int getAllInterfacesAndAddresses (JNIEnv *env, netif **netifPP);
netif *ifList, *curr; netif *ifList, *curr;
int ipv6Enabled = 0; int ipv6Enabled = 0;
if (getAllInterfacesAndAddresses (env, &ifList) < 0) { if (getAllInterfacesAndAddresses(env, &ifList) < 0) {
return ipv6Enabled; return ipv6Enabled;
} }
@ -1689,7 +1685,7 @@ static void setMulticastInterface(JNIEnv *env, jobject this, int fd, int fd1,
* option instead of IP_MULTICAST_IF * option instead of IP_MULTICAST_IF
*/ */
if (ipv6_supported) { if (ipv6_supported) {
static jclass ni_class; static jclass ni_class = NULL;
if (ni_class == NULL) { if (ni_class == NULL) {
jclass c = (*env)->FindClass(env, "java/net/NetworkInterface"); jclass c = (*env)->FindClass(env, "java/net/NetworkInterface");
CHECK_NULL(c); CHECK_NULL(c);
@ -1729,7 +1725,7 @@ static void setMulticastInterface(JNIEnv *env, jobject this, int fd, int fd1,
* option. For IPv6 both must be done. * option. For IPv6 both must be done.
*/ */
if (ipv6_supported) { if (ipv6_supported) {
static jfieldID ni_indexID; static jfieldID ni_indexID = NULL;
struct in_addr in; struct in_addr in;
int index; int index;
@ -2250,10 +2246,10 @@ Java_java_net_TwoStacksPlainDatagramSocketImpl_socketGetOption
* Signature: (I)Ljava/lang/Object; * Signature: (I)Ljava/lang/Object;
*/ */
JNIEXPORT jobject JNICALL JNIEXPORT jobject JNICALL
Java_java_net_TwoStacksPlainDatagramSocketImpl_socketLocalAddress(JNIEnv *env, jobject this, Java_java_net_TwoStacksPlainDatagramSocketImpl_socketLocalAddress
jint family) { (JNIEnv *env, jobject this, jint family)
{
int fd=-1, fd1=-1; int fd = -1, fd1 = -1;
SOCKETADDRESS him; SOCKETADDRESS him;
int len = 0; int len = 0;
int port; int port;
@ -2273,7 +2269,7 @@ Java_java_net_TwoStacksPlainDatagramSocketImpl_socketLocalAddress(JNIEnv *env, j
/* find out local IP address */ /* find out local IP address */
len = sizeof (struct sockaddr_in); len = sizeof(struct sockaddr_in);
/* family==-1 when socket is not connected */ /* family==-1 when socket is not connected */
if ((family == IPv6) || (family == -1 && fd == -1)) { if ((family == IPv6) || (family == -1 && fd == -1)) {
@ -2287,12 +2283,12 @@ Java_java_net_TwoStacksPlainDatagramSocketImpl_socketLocalAddress(JNIEnv *env, j
return NULL; return NULL;
} }
if (getsockname(fd, (struct sockaddr *)&him, &len) == -1) { if (getsockname(fd, &him.sa, &len) == -1) {
JNU_ThrowByNameWithMessageAndLastError JNU_ThrowByNameWithMessageAndLastError
(env, JNU_JAVANETPKG "SocketException", "Error getting socket name"); (env, JNU_JAVANETPKG "SocketException", "Error getting socket name");
return NULL; return NULL;
} }
iaObj = NET_SockaddrToInetAddress(env, (struct sockaddr *)&him, &port); iaObj = NET_SockaddrToInetAddress(env, &him.sa, &port);
return iaObj; return iaObj;
} }
@ -2450,17 +2446,17 @@ static void mcast_join_leave(JNIEnv *env, jobject this,
return; return;
} }
if (NET_InetAddressToSockaddr(env, iaObj, 0, (struct sockaddr *)&name, &len, JNI_FALSE) != 0) { if (NET_InetAddressToSockaddr(env, iaObj, 0, &name.sa, &len, JNI_FALSE) != 0) {
return; return;
} }
/* Set the multicast group address in the ip_mreq field /* Set the multicast group address in the ip_mreq field
* eventually this check should be done by the security manager * eventually this check should be done by the security manager
*/ */
family = name.him.sa_family; family = name.sa.sa_family;
if (family == AF_INET) { if (family == AF_INET) {
int address = name.him4.sin_addr.s_addr; int address = name.sa4.sin_addr.s_addr;
if (!IN_MULTICAST(ntohl(address))) { if (!IN_MULTICAST(ntohl(address))) {
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "not in multicast"); JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "not in multicast");
return; return;
@ -2499,7 +2495,7 @@ static void mcast_join_leave(JNIEnv *env, jobject this,
} else /* AF_INET6 */ { } else /* AF_INET6 */ {
if (ipv6_supported) { if (ipv6_supported) {
struct in6_addr *address; struct in6_addr *address;
address = &name.him6.sin6_addr; address = &name.sa6.sin6_addr;
if (!IN6_IS_ADDR_MULTICAST(address)) { if (!IN6_IS_ADDR_MULTICAST(address)) {
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "not in6 multicast"); JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "not in6 multicast");
return; return;

View File

@ -192,7 +192,7 @@ Java_java_net_TwoStacksPlainSocketImpl_socketConnect(JNIEnv *env, jobject this,
/* family and localport are int fields of iaObj */ /* family and localport are int fields of iaObj */
int family; int family;
jint fd, fd1=-1; jint fd = -1, fd1 = -1;
jint len; jint len;
int ipv6_supported = ipv6_available(); int ipv6_supported = ipv6_available();
@ -222,11 +222,11 @@ Java_java_net_TwoStacksPlainSocketImpl_socketConnect(JNIEnv *env, jobject this,
return; return;
} }
if (NET_InetAddressToSockaddr(env, iaObj, port, (struct sockaddr *)&him, &len, JNI_FALSE) != 0) { if (NET_InetAddressToSockaddr(env, iaObj, port, &him.sa, &len, JNI_FALSE) != 0) {
return; return;
} }
family = him.him.sa_family; family = him.sa.sa_family;
if (family == AF_INET6) { if (family == AF_INET6) {
if (!ipv6_supported) { if (!ipv6_supported) {
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
@ -258,7 +258,7 @@ Java_java_net_TwoStacksPlainSocketImpl_socketConnect(JNIEnv *env, jobject this,
(*env)->SetObjectField(env, this, psi_fd1ID, NULL); (*env)->SetObjectField(env, this, psi_fd1ID, NULL);
if (timeout <= 0) { if (timeout <= 0) {
connect_res = connect(fd, (struct sockaddr *) &him, SOCKETADDRESS_LEN(&him)); connect_res = connect(fd, &him.sa, sizeof(SOCKETADDRESS));
if (connect_res == SOCKET_ERROR) { if (connect_res == SOCKET_ERROR) {
connect_res = WSAGetLastError(); connect_res = WSAGetLastError();
} }
@ -268,10 +268,10 @@ Java_java_net_TwoStacksPlainSocketImpl_socketConnect(JNIEnv *env, jobject this,
/* make socket non-blocking */ /* make socket non-blocking */
optval = 1; optval = 1;
ioctlsocket( fd, FIONBIO, &optval ); ioctlsocket(fd, FIONBIO, &optval);
/* initiate the connect */ /* initiate the connect */
connect_res = connect(fd, (struct sockaddr *) &him, SOCKETADDRESS_LEN(&him)); connect_res = connect(fd, &him.sa, sizeof(SOCKETADDRESS));
if (connect_res == SOCKET_ERROR) { if (connect_res == SOCKET_ERROR) {
if (WSAGetLastError() != WSAEWOULDBLOCK) { if (WSAGetLastError() != WSAEWOULDBLOCK) {
connect_res = WSAGetLastError(); connect_res = WSAGetLastError();
@ -371,18 +371,17 @@ Java_java_net_TwoStacksPlainSocketImpl_socketConnect(JNIEnv *env, jobject this,
* that the system chose for us and store it in the Socket object. * that the system chose for us and store it in the Socket object.
*/ */
u_short port; u_short port;
int len = SOCKETADDRESS_LEN(&him); int len = sizeof(SOCKETADDRESS);
if (getsockname(fd, (struct sockaddr *)&him, &len) == -1) { if (getsockname(fd, &him.sa, &len) == -1) {
if (WSAGetLastError() == WSAENOTSOCK) { if (WSAGetLastError() == WSAENOTSOCK) {
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
"Socket closed"); "Socket closed");
} else { } else {
NET_ThrowCurrent(env, "getsockname failed"); NET_ThrowCurrent(env, "getsockname failed");
} }
return; return;
} }
port = ntohs ((u_short)GET_PORT(&him)); port = ntohs((u_short)GET_PORT(&him));
(*env)->SetIntField(env, this, psi_localportID, (int) port); (*env)->SetIntField(env, this, psi_localportID, (int) port);
} }
} }
@ -400,7 +399,7 @@ Java_java_net_TwoStacksPlainSocketImpl_socketBind(JNIEnv *env, jobject this,
/* fdObj is the FileDescriptor field on this */ /* fdObj is the FileDescriptor field on this */
jobject fdObj, fd1Obj; jobject fdObj, fd1Obj;
/* fd is an int field on fdObj */ /* fd is an int field on fdObj */
int fd, fd1, len = 0; int fd, fd1 = -1, len = 0;
int ipv6_supported = ipv6_available(); int ipv6_supported = ipv6_available();
/* family is an int field of iaObj */ /* family is an int field of iaObj */
@ -435,9 +434,9 @@ Java_java_net_TwoStacksPlainSocketImpl_socketBind(JNIEnv *env, jobject this,
return; return;
} }
if (NET_InetAddressToSockaddr(env, iaObj, localport, if (NET_InetAddressToSockaddr(env, iaObj, localport, &him.sa, &len,
(struct sockaddr *)&him, &len, JNI_FALSE) != 0) { JNI_FALSE) != 0) {
return; return;
} }
if (ipv6_supported) { if (ipv6_supported) {
struct ipv6bind v6bind; struct ipv6bind v6bind;
@ -473,7 +472,7 @@ Java_java_net_TwoStacksPlainSocketImpl_socketBind(JNIEnv *env, jobject this,
(*env)->SetObjectField(env, this, psi_fd1ID, NULL); (*env)->SetObjectField(env, this, psi_fd1ID, NULL);
} }
} else { } else {
rv = NET_WinBind(fd, (struct sockaddr *)&him, len, exclBind); rv = NET_WinBind(fd, &him.sa, len, exclBind);
} }
if (rv == -1) { if (rv == -1) {
@ -489,17 +488,16 @@ Java_java_net_TwoStacksPlainSocketImpl_socketBind(JNIEnv *env, jobject this,
/* Now that we're a bound socket, let's extract the port number /* Now that we're a bound socket, let's extract the port number
* that the system chose for us and store it in the Socket object. * that the system chose for us and store it in the Socket object.
*/ */
int len = SOCKETADDRESS_LEN(&him); int len = sizeof(SOCKETADDRESS);
u_short port; u_short port;
fd = him.him.sa_family == AF_INET? fd: fd1;
if (getsockname(fd, (struct sockaddr *)&him, &len) == -1) { if (getsockname(him.sa.sa_family == AF_INET ? fd: fd1, &him.sa, &len) == -1) {
NET_ThrowCurrent(env, "getsockname in plain socketBind"); NET_ThrowCurrent(env, "getsockname in plain socketBind");
return; return;
} }
port = ntohs ((u_short) GET_PORT (&him)); port = ntohs((u_short) GET_PORT (&him));
(*env)->SetIntField(env, this, psi_localportID, (int) port); (*env)->SetIntField(env, this, psi_localportID, (int)port);
} else { } else {
(*env)->SetIntField(env, this, psi_localportID, localport); (*env)->SetIntField(env, this, psi_localportID, localport);
} }
@ -511,16 +509,17 @@ Java_java_net_TwoStacksPlainSocketImpl_socketBind(JNIEnv *env, jobject this,
* Signature: (I)V * Signature: (I)V
*/ */
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
Java_java_net_TwoStacksPlainSocketImpl_socketListen (JNIEnv *env, jobject this, Java_java_net_TwoStacksPlainSocketImpl_socketListen
jint count) (JNIEnv *env, jobject this, jint count)
{ {
/* this FileDescriptor fd field */ /* this FileDescriptor fd field */
jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID); jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
jobject fd1Obj = (*env)->GetObjectField(env, this, psi_fd1ID); jobject fd1Obj = (*env)->GetObjectField(env, this, psi_fd1ID);
jobject address; jobject address;
/* fdObj's int fd field */ /* fdObj's int fd field */
int fd, fd1; int fd = INVALID_SOCKET, fd1 = INVALID_SOCKET;
SOCKETADDRESS addr; int addrlen; SOCKETADDRESS addr;
int addrlen;
if (IS_NULL(fdObj) && IS_NULL(fd1Obj)) { if (IS_NULL(fdObj) && IS_NULL(fd1Obj)) {
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
@ -540,12 +539,12 @@ Java_java_net_TwoStacksPlainSocketImpl_socketListen (JNIEnv *env, jobject this,
JNU_ThrowNullPointerException(env, "socket address"); JNU_ThrowNullPointerException(env, "socket address");
return; return;
} }
if (NET_InetAddressToSockaddr(env, address, 0, (struct sockaddr *)&addr, if (NET_InetAddressToSockaddr(env, address, 0, &addr.sa, &addrlen,
&addrlen, JNI_FALSE) != 0) { JNI_FALSE) != 0) {
return; return;
} }
if (addr.him.sa_family == AF_INET || IN6ADDR_ISANY(&addr.him6)) { if (addr.sa.sa_family == AF_INET || IN6ADDR_ISANY(&addr.sa6)) {
/* listen on v4 */ /* listen on v4 */
if (listen(fd, count) == -1) { if (listen(fd, count) == -1) {
NET_ThrowCurrent(env, "listen failed"); NET_ThrowCurrent(env, "listen failed");
@ -556,7 +555,7 @@ Java_java_net_TwoStacksPlainSocketImpl_socketListen (JNIEnv *env, jobject this,
} }
if (ipv6_available() && !IS_NULL(fd1Obj)) { if (ipv6_available() && !IS_NULL(fd1Obj)) {
fd1 = (*env)->GetIntField(env, fd1Obj, IO_fd_fdID); fd1 = (*env)->GetIntField(env, fd1Obj, IO_fd_fdID);
if (addr.him.sa_family == AF_INET6 || addr.him4.sin_addr.s_addr == INADDR_ANY) { if (addr.sa.sa_family == AF_INET6 || addr.sa4.sin_addr.s_addr == INADDR_ANY) {
/* listen on v6 */ /* listen on v6 */
if (listen(fd1, count) == -1) { if (listen(fd1, count) == -1) {
NET_ThrowCurrent(env, "listen failed"); NET_ThrowCurrent(env, "listen failed");
@ -687,7 +686,7 @@ Java_java_net_TwoStacksPlainSocketImpl_socketAccept(JNIEnv *env, jobject this,
} }
} }
} }
fd = accept(fd, (struct sockaddr *)&him, &len); fd = accept(fd, &him.sa, &len);
if (fd < 0) { if (fd < 0) {
/* REMIND: SOCKET CLOSED PROBLEM */ /* REMIND: SOCKET CLOSED PROBLEM */
if (fd == -2) { if (fd == -2) {
@ -702,7 +701,7 @@ Java_java_net_TwoStacksPlainSocketImpl_socketAccept(JNIEnv *env, jobject this,
SetHandleInformation((HANDLE)(UINT_PTR)fd, HANDLE_FLAG_INHERIT, 0); SetHandleInformation((HANDLE)(UINT_PTR)fd, HANDLE_FLAG_INHERIT, 0);
(*env)->SetIntField(env, socketFdObj, IO_fd_fdID, fd); (*env)->SetIntField(env, socketFdObj, IO_fd_fdID, fd);
if (him.him.sa_family == AF_INET) { if (him.sa.sa_family == AF_INET) {
if (inet4Cls == NULL) { if (inet4Cls == NULL) {
jclass c = (*env)->FindClass(env, "java/net/Inet4Address"); jclass c = (*env)->FindClass(env, "java/net/Inet4Address");
if (c != NULL) { if (c != NULL) {
@ -728,7 +727,7 @@ Java_java_net_TwoStacksPlainSocketImpl_socketAccept(JNIEnv *env, jobject this,
return; return;
} }
setInetAddress_addr(env, socketAddressObj, ntohl(him.him4.sin_addr.s_addr)); setInetAddress_addr(env, socketAddressObj, ntohl(him.sa4.sin_addr.s_addr));
setInetAddress_family(env, socketAddressObj, IPv4); setInetAddress_family(env, socketAddressObj, IPv4);
(*env)->SetObjectField(env, socket, psi_addressID, socketAddressObj); (*env)->SetObjectField(env, socket, psi_addressID, socketAddressObj);
} else { } else {
@ -754,9 +753,9 @@ Java_java_net_TwoStacksPlainSocketImpl_socketAccept(JNIEnv *env, jobject this,
NET_SocketClose(fd); NET_SocketClose(fd);
return; return;
} }
setInet6Address_ipaddress(env, socketAddressObj, (char *)&him.him6.sin6_addr); setInet6Address_ipaddress(env, socketAddressObj, (char *)&him.sa6.sin6_addr);
setInetAddress_family(env, socketAddressObj, IPv6); setInetAddress_family(env, socketAddressObj, IPv6);
setInet6Address_scopeid(env, socketAddressObj, him.him6.sin6_scope_id); setInet6Address_scopeid(env, socketAddressObj, him.sa6.sin6_scope_id);
} }
/* fields common to AF_INET and AF_INET6 */ /* fields common to AF_INET and AF_INET6 */
@ -1036,13 +1035,12 @@ Java_java_net_TwoStacksPlainSocketImpl_socketGetOption
*/ */
if (opt == java_net_SocketOptions_SO_BINDADDR) { if (opt == java_net_SocketOptions_SO_BINDADDR) {
SOCKETADDRESS him; SOCKETADDRESS him;
int len; int len = sizeof(SOCKETADDRESS);
int port; int port;
jobject iaObj; jobject iaObj;
jclass iaCntrClass; jclass iaCntrClass;
jfieldID iaFieldID; jfieldID iaFieldID;
len = sizeof(him);
memset((char *)&him, 0, len); memset((char *)&him, 0, len);
if (fd == -1) { if (fd == -1) {
@ -1052,12 +1050,12 @@ Java_java_net_TwoStacksPlainSocketImpl_socketGetOption
fd = getFD1 (env, this); fd = getFD1 (env, this);
} }
if (getsockname(fd, (struct sockaddr *)&him, &len) < 0) { if (getsockname(fd, &him.sa, &len) < 0) {
JNU_ThrowByNameWithMessageAndLastError JNU_ThrowByNameWithMessageAndLastError
(env, JNU_JAVANETPKG "SocketException", "Error getting socket name"); (env, JNU_JAVANETPKG "SocketException", "Error getting socket name");
return -1; return -1;
} }
iaObj = NET_SockaddrToInetAddress(env, (struct sockaddr *)&him, &port); iaObj = NET_SockaddrToInetAddress(env, &him.sa, &port);
CHECK_NULL_RETURN(iaObj, -1); CHECK_NULL_RETURN(iaObj, -1);
iaCntrClass = (*env)->GetObjectClass(env, iaContainerObj); iaCntrClass = (*env)->GetObjectClass(env, iaContainerObj);

View File

@ -668,21 +668,21 @@ void dumpAddr (char *str, void *addr) {
*/ */
JNIEXPORT int JNICALL JNIEXPORT int JNICALL
NET_BindV6(struct ipv6bind* b, jboolean exclBind) { NET_BindV6(struct ipv6bind *b, jboolean exclBind) {
int fd=-1, ofd=-1, rv, len; int fd=-1, ofd=-1, rv, len;
/* need to defer close until new sockets created */ /* need to defer close until new sockets created */
int close_fd=-1, close_ofd=-1; int close_fd=-1, close_ofd=-1;
SOCKETADDRESS oaddr; /* other address to bind */ SOCKETADDRESS oaddr; /* other address to bind */
int family = b->addr->him.sa_family; int family = b->addr->sa.sa_family;
int ofamily; int ofamily;
u_short port; /* requested port parameter */ u_short port; /* requested port parameter */
u_short bound_port; u_short bound_port;
if (family == AF_INET && (b->addr->him4.sin_addr.s_addr != INADDR_ANY)) { if (family == AF_INET && (b->addr->sa4.sin_addr.s_addr != INADDR_ANY)) {
/* bind to v4 only */ /* bind to v4 only */
int ret; int ret;
ret = NET_WinBind ((int)b->ipv4_fd, (struct sockaddr *)b->addr, ret = NET_WinBind((int)b->ipv4_fd, (struct sockaddr *)b->addr,
sizeof (struct sockaddr_in), exclBind); sizeof(SOCKETADDRESS), exclBind);
if (ret == SOCKET_ERROR) { if (ret == SOCKET_ERROR) {
CLOSE_SOCKETS_AND_RETURN; CLOSE_SOCKETS_AND_RETURN;
} }
@ -690,11 +690,11 @@ NET_BindV6(struct ipv6bind* b, jboolean exclBind) {
b->ipv6_fd = -1; b->ipv6_fd = -1;
return 0; return 0;
} }
if (family == AF_INET6 && (!IN6_IS_ADDR_ANY(&b->addr->him6.sin6_addr))) { if (family == AF_INET6 && (!IN6_IS_ADDR_ANY(&b->addr->sa6.sin6_addr))) {
/* bind to v6 only */ /* bind to v6 only */
int ret; int ret;
ret = NET_WinBind ((int)b->ipv6_fd, (struct sockaddr *)b->addr, ret = NET_WinBind((int)b->ipv6_fd, (struct sockaddr *)b->addr,
sizeof (struct SOCKADDR_IN6), exclBind); sizeof(SOCKETADDRESS), exclBind);
if (ret == SOCKET_ERROR) { if (ret == SOCKET_ERROR) {
CLOSE_SOCKETS_AND_RETURN; CLOSE_SOCKETS_AND_RETURN;
} }
@ -711,32 +711,32 @@ NET_BindV6(struct ipv6bind* b, jboolean exclBind) {
fd = (int)b->ipv4_fd; fd = (int)b->ipv4_fd;
ofd = (int)b->ipv6_fd; ofd = (int)b->ipv6_fd;
port = (u_short)GET_PORT (b->addr); port = (u_short)GET_PORT (b->addr);
IN6ADDR_SETANY (&oaddr.him6); IN6ADDR_SETANY(&oaddr.sa6);
oaddr.him6.sin6_port = port; oaddr.sa6.sin6_port = port;
} else { } else {
ofamily = AF_INET; ofamily = AF_INET;
ofd = (int)b->ipv4_fd; ofd = (int)b->ipv4_fd;
fd = (int)b->ipv6_fd; fd = (int)b->ipv6_fd;
port = (u_short)GET_PORT (b->addr); port = (u_short)GET_PORT (b->addr);
oaddr.him4.sin_family = AF_INET; oaddr.sa4.sin_family = AF_INET;
oaddr.him4.sin_port = port; oaddr.sa4.sin_port = port;
oaddr.him4.sin_addr.s_addr = INADDR_ANY; oaddr.sa4.sin_addr.s_addr = INADDR_ANY;
} }
rv = NET_WinBind(fd, (struct sockaddr *)b->addr, SOCKETADDRESS_LEN(b->addr), exclBind); rv = NET_WinBind(fd, (struct sockaddr *)b->addr, sizeof(SOCKETADDRESS), exclBind);
if (rv == SOCKET_ERROR) { if (rv == SOCKET_ERROR) {
CLOSE_SOCKETS_AND_RETURN; CLOSE_SOCKETS_AND_RETURN;
} }
/* get the port and set it in the other address */ /* get the port and set it in the other address */
len = SOCKETADDRESS_LEN(b->addr); len = sizeof(SOCKETADDRESS);
if (getsockname(fd, (struct sockaddr *)b->addr, &len) == -1) { if (getsockname(fd, (struct sockaddr *)b->addr, &len) == -1) {
CLOSE_SOCKETS_AND_RETURN; CLOSE_SOCKETS_AND_RETURN;
} }
bound_port = GET_PORT (b->addr); bound_port = GET_PORT (b->addr);
SET_PORT (&oaddr, bound_port); SET_PORT (&oaddr, bound_port);
if ((rv=NET_WinBind (ofd, (struct sockaddr *) &oaddr, if ((rv = NET_WinBind(ofd, &oaddr.sa,
SOCKETADDRESS_LEN (&oaddr), exclBind)) == SOCKET_ERROR) { sizeof(SOCKETADDRESS), exclBind)) == SOCKET_ERROR) {
int retries; int retries;
int sotype, arglen=sizeof(sotype); int sotype, arglen=sizeof(sotype);
@ -772,8 +772,7 @@ NET_BindV6(struct ipv6bind* b, jboolean exclBind) {
/* bind random port on first socket */ /* bind random port on first socket */
SET_PORT (&oaddr, 0); SET_PORT (&oaddr, 0);
rv = NET_WinBind (ofd, (struct sockaddr *)&oaddr, SOCKETADDRESS_LEN(&oaddr), rv = NET_WinBind(ofd, &oaddr.sa, sizeof(SOCKETADDRESS), exclBind);
exclBind);
if (rv == SOCKET_ERROR) { if (rv == SOCKET_ERROR) {
CLOSE_SOCKETS_AND_RETURN; CLOSE_SOCKETS_AND_RETURN;
} }
@ -783,14 +782,14 @@ NET_BindV6(struct ipv6bind* b, jboolean exclBind) {
close_fd = close_ofd = -1; close_fd = close_ofd = -1;
/* bind new port on second socket */ /* bind new port on second socket */
len = SOCKETADDRESS_LEN(&oaddr); len = sizeof(SOCKETADDRESS);
if (getsockname(ofd, (struct sockaddr *)&oaddr, &len) == -1) { if (getsockname(ofd, &oaddr.sa, &len) == -1) {
CLOSE_SOCKETS_AND_RETURN; CLOSE_SOCKETS_AND_RETURN;
} }
bound_port = GET_PORT (&oaddr); bound_port = GET_PORT (&oaddr);
SET_PORT (b->addr, bound_port); SET_PORT (b->addr, bound_port);
rv = NET_WinBind (fd, (struct sockaddr *)b->addr, SOCKETADDRESS_LEN(b->addr), rv = NET_WinBind(fd, (struct sockaddr *)b->addr,
exclBind); sizeof(SOCKETADDRESS), exclBind);
if (rv != SOCKET_ERROR) { if (rv != SOCKET_ERROR) {
if (family == AF_INET) { if (family == AF_INET) {
@ -826,9 +825,9 @@ jint getDefaultIPv6Interface(JNIEnv *env, struct SOCKADDR_IN6 *target_addr)
} }
ret = WSAIoctl(fd, SIO_ROUTING_INTERFACE_QUERY, ret = WSAIoctl(fd, SIO_ROUTING_INTERFACE_QUERY,
(void *)target_addr, sizeof(struct sockaddr_in6), (void *)target_addr, sizeof(struct sockaddr_in6),
(void *)&route, sizeof(struct sockaddr_in6), (void *)&route, sizeof(struct sockaddr_in6),
&b, 0, 0); &b, 0, 0);
if (ret == SOCKET_ERROR) { if (ret == SOCKET_ERROR) {
// error // error
closesocket(fd); closesocket(fd);
@ -909,13 +908,13 @@ NET_InetAddressToSockaddr(JNIEnv *env, jobject iaObj, int port, struct sockaddr
him6->sin6_scope_id = scopeid != 0 ? scopeid : cached_scope_id; him6->sin6_scope_id = scopeid != 0 ? scopeid : cached_scope_id;
*len = sizeof(struct SOCKADDR_IN6) ; *len = sizeof(struct SOCKADDR_IN6) ;
} else { } else {
struct sockaddr_in *him4 = (struct sockaddr_in*)him; struct sockaddr_in *him4 = (struct sockaddr_in *)him;
jint address; jint address;
if (family != AF_INET) { if (family != AF_INET) {
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Protocol family unavailable"); JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Protocol family unavailable");
return -1; return -1;
} }
memset((char *) him4, 0, sizeof(struct sockaddr_in)); memset((char *)him4, 0, sizeof(struct sockaddr_in));
address = getInetAddress_addr(env, iaObj); address = getInetAddress_addr(env, iaObj);
him4->sin_port = htons((short) port); him4->sin_port = htons((short) port);
him4->sin_addr.s_addr = (u_long) htonl(address); him4->sin_addr.s_addr = (u_long) htonl(address);
@ -928,9 +927,9 @@ NET_InetAddressToSockaddr(JNIEnv *env, jobject iaObj, int port, struct sockaddr
JNIEXPORT jint JNICALL JNIEXPORT jint JNICALL
NET_GetPortFromSockaddr(struct sockaddr *him) { NET_GetPortFromSockaddr(struct sockaddr *him) {
if (him->sa_family == AF_INET6) { if (him->sa_family == AF_INET6) {
return ntohs(((struct sockaddr_in6*)him)->sin6_port); return ntohs(((struct sockaddr_in6 *)him)->sin6_port);
} else { } else {
return ntohs(((struct sockaddr_in*)him)->sin_port); return ntohs(((struct sockaddr_in *)him)->sin_port);
} }
} }
@ -966,12 +965,12 @@ NET_IsEqual(jbyte* caddr1, jbyte* caddr2) {
return 1; return 1;
} }
int getScopeID (struct sockaddr *him) { int getScopeID(struct sockaddr *him) {
struct SOCKADDR_IN6 *him6 = (struct SOCKADDR_IN6 *)him; struct SOCKADDR_IN6 *him6 = (struct SOCKADDR_IN6 *)him;
return him6->sin6_scope_id; return him6->sin6_scope_id;
} }
int cmpScopeID (unsigned int scope, struct sockaddr *him) { int cmpScopeID(unsigned int scope, struct sockaddr *him) {
struct SOCKADDR_IN6 *him6 = (struct SOCKADDR_IN6 *)him; struct SOCKADDR_IN6 *him6 = (struct SOCKADDR_IN6 *)him;
return him6->sin6_scope_id == scope; return him6->sin6_scope_id == scope;
} }

View File

@ -247,9 +247,9 @@ void NET_ThrowCurrent(JNIEnv *env, char *msg);
int NET_GetDefaultTOS(void); int NET_GetDefaultTOS(void);
typedef union { typedef union {
struct sockaddr him; struct sockaddr sa;
struct sockaddr_in him4; struct sockaddr_in sa4;
struct SOCKADDR_IN6 him6; struct SOCKADDR_IN6 sa6;
} SOCKETADDRESS; } SOCKETADDRESS;
/* /*
@ -257,15 +257,11 @@ typedef union {
* sockets. On return they may refer to different sockets. * sockets. On return they may refer to different sockets.
*/ */
struct ipv6bind { struct ipv6bind {
SOCKETADDRESS *addr; SOCKETADDRESS *addr;
SOCKET ipv4_fd; SOCKET ipv4_fd;
SOCKET ipv6_fd; SOCKET ipv6_fd;
}; };
#define SOCKETADDRESS_LEN(X) \
(((X)->him.sa_family==AF_INET6)? sizeof(struct SOCKADDR_IN6) : \
sizeof(struct sockaddr_in))
#define SOCKETADDRESS_COPY(DST,SRC) { \ #define SOCKETADDRESS_COPY(DST,SRC) { \
if ((SRC)->sa_family == AF_INET6) { \ if ((SRC)->sa_family == AF_INET6) { \
memcpy ((DST), (SRC), sizeof (struct SOCKADDR_IN6)); \ memcpy ((DST), (SRC), sizeof (struct SOCKADDR_IN6)); \
@ -274,20 +270,20 @@ struct ipv6bind {
} \ } \
} }
#define SET_PORT(X,Y) { \ #define SET_PORT(X,Y) { \
if ((X)->him.sa_family == AF_INET) { \ if ((X)->sa.sa_family == AF_INET) { \
(X)->him4.sin_port = (Y); \ (X)->sa4.sin_port = (Y); \
} else { \ } else { \
(X)->him6.sin6_port = (Y); \ (X)->sa6.sin6_port = (Y); \
} \ } \
} }
#define GET_PORT(X) ((X)->him.sa_family==AF_INET ?(X)->him4.sin_port: (X)->him6.sin6_port) #define GET_PORT(X) ((X)->sa.sa_family == AF_INET ? (X)->sa4.sin_port : (X)->sa6.sin6_port)
#define IS_LOOPBACK_ADDRESS(x) ( \ #define IS_LOOPBACK_ADDRESS(x) ( \
((x)->him.sa_family == AF_INET) ? \ ((x)->sa.sa_family == AF_INET) ? \
(ntohl((x)->him4.sin_addr.s_addr)==INADDR_LOOPBACK) : \ (ntohl((x)->sa4.sin_addr.s_addr) == INADDR_LOOPBACK) : \
(IN6ADDR_ISLOOPBACK (x)) \ (IN6ADDR_ISLOOPBACK(x)) \
) )
JNIEXPORT int JNICALL NET_SocketClose(int fd); JNIEXPORT int JNICALL NET_SocketClose(int fd);
@ -297,7 +293,7 @@ JNIEXPORT int JNICALL NET_Timeout(int fd, long timeout);
int NET_Socket(int domain, int type, int protocol); int NET_Socket(int domain, int type, int protocol);
void NET_ThrowByNameWithLastError(JNIEnv *env, const char *name, void NET_ThrowByNameWithLastError(JNIEnv *env, const char *name,
const char *defaultDetail); const char *defaultDetail);
/* /*
* differs from NET_Timeout() as follows: * differs from NET_Timeout() as follows:
@ -312,46 +308,39 @@ void NET_ThrowByNameWithLastError(JNIEnv *env, const char *name,
*/ */
JNIEXPORT int JNICALL NET_Timeout2(int fd, int fd1, long timeout, int *fdret); JNIEXPORT int JNICALL NET_Timeout2(int fd, int fd1, long timeout, int *fdret);
JNIEXPORT int JNICALL NET_BindV6(struct ipv6bind* b, jboolean exclBind); JNIEXPORT int JNICALL NET_BindV6(struct ipv6bind *b, jboolean exclBind);
#define NET_WAIT_READ 0x01
#define NET_WAIT_WRITE 0x02
#define NET_WAIT_CONNECT 0x04
extern jint NET_Wait(JNIEnv *env, jint fd, jint flags, jint timeout);
JNIEXPORT int JNICALL NET_WinBind(int s, struct sockaddr *him, int len, JNIEXPORT int JNICALL NET_WinBind(int s, struct sockaddr *him, int len,
jboolean exclBind); jboolean exclBind);
/* XP versions of the native routines */ /* XP versions of the native routines */
JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByName0_XP JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByName0_XP
(JNIEnv *env, jclass cls, jstring name); (JNIEnv *env, jclass cls, jstring name);
JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByIndex0_XP JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByIndex0_XP
(JNIEnv *env, jclass cls, jint index); (JNIEnv *env, jclass cls, jint index);
JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByInetAddress0_XP JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByInetAddress0_XP
(JNIEnv *env, jclass cls, jobject iaObj); (JNIEnv *env, jclass cls, jobject iaObj);
JNIEXPORT jobjectArray JNICALL Java_java_net_NetworkInterface_getAll_XP JNIEXPORT jobjectArray JNICALL Java_java_net_NetworkInterface_getAll_XP
(JNIEnv *env, jclass cls); (JNIEnv *env, jclass cls);
JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_supportsMulticast0_XP JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_supportsMulticast0_XP
(JNIEnv *env, jclass cls, jstring name, jint index); (JNIEnv *env, jclass cls, jstring name, jint index);
JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isUp0_XP JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isUp0_XP
(JNIEnv *env, jclass cls, jstring name, jint index); (JNIEnv *env, jclass cls, jstring name, jint index);
JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isP2P0_XP JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isP2P0_XP
(JNIEnv *env, jclass cls, jstring name, jint index); (JNIEnv *env, jclass cls, jstring name, jint index);
JNIEXPORT jbyteArray JNICALL Java_java_net_NetworkInterface_getMacAddr0_XP JNIEXPORT jbyteArray JNICALL Java_java_net_NetworkInterface_getMacAddr0_XP
(JNIEnv *env, jclass cls, jstring name, jint index); (JNIEnv *env, jclass cls, jstring name, jint index);
JNIEXPORT jint JNICALL Java_java_net_NetworkInterface_getMTU0_XP JNIEXPORT jint JNICALL Java_java_net_NetworkInterface_getMTU0_XP
(JNIEnv *env, jclass class, jstring name, jint index); (JNIEnv *env, jclass class, jstring name, jint index);
JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isLoopback0_XP JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isLoopback0_XP
(JNIEnv *env, jclass cls, jstring name, jint index); (JNIEnv *env, jclass cls, jstring name, jint index);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2009, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -417,8 +417,8 @@ void handleMessage
JNIEXPORT jint JNICALL Java_sun_nio_ch_sctp_SctpChannelImpl_receive0 JNIEXPORT jint JNICALL Java_sun_nio_ch_sctp_SctpChannelImpl_receive0
(JNIEnv *env, jclass klass, jint fd, jobject resultContainerObj, (JNIEnv *env, jclass klass, jint fd, jobject resultContainerObj,
jlong address, jint length, jboolean peek) { jlong address, jint length, jboolean peek) {
SOCKADDR sa; SOCKETADDRESS sa;
int sa_len = sizeof(sa); int sa_len = sizeof(SOCKETADDRESS);
ssize_t rv = 0; ssize_t rv = 0;
jlong *addr = jlong_to_ptr(address); jlong *addr = jlong_to_ptr(address);
struct iovec iov[1]; struct iovec iov[1];
@ -501,7 +501,7 @@ JNIEXPORT jint JNICALL Java_sun_nio_ch_sctp_SctpChannelImpl_receive0
snp = (union sctp_notification *) bufp; snp = (union sctp_notification *) bufp;
if (handleNotification(env, fd, resultContainerObj, snp, rv, if (handleNotification(env, fd, resultContainerObj, snp, rv,
(msg->msg_flags & MSG_EOR), (msg->msg_flags & MSG_EOR),
(struct sockaddr*)&sa ) == JNI_TRUE) { &sa.sa) == JNI_TRUE) {
/* We have received a notification that is of interest /* We have received a notification that is of interest
to the Java API. The appropriate notification will be to the Java API. The appropriate notification will be
set in the result container. */ set in the result container. */
@ -524,7 +524,7 @@ JNIEXPORT jint JNICALL Java_sun_nio_ch_sctp_SctpChannelImpl_receive0
} while (msg->msg_flags & MSG_NOTIFICATION); } while (msg->msg_flags & MSG_NOTIFICATION);
handleMessage(env, resultContainerObj, msg, rv, handleMessage(env, resultContainerObj, msg, rv,
(msg->msg_flags & MSG_EOR), (struct sockaddr*)&sa); (msg->msg_flags & MSG_EOR), &sa.sa);
return rv; return rv;
} }
@ -537,8 +537,8 @@ JNIEXPORT jint JNICALL Java_sun_nio_ch_sctp_SctpChannelImpl_send0
(JNIEnv *env, jclass klass, jint fd, jlong address, jint length, (JNIEnv *env, jclass klass, jint fd, jlong address, jint length,
jobject targetAddress, jint targetPort, jint assocId, jint streamNumber, jobject targetAddress, jint targetPort, jint assocId, jint streamNumber,
jboolean unordered, jint ppid) { jboolean unordered, jint ppid) {
SOCKADDR sa; SOCKETADDRESS sa;
int sa_len = sizeof(sa); int sa_len = sizeof(SOCKETADDRESS);
ssize_t rv = 0; ssize_t rv = 0;
jlong *addr = jlong_to_ptr(address); jlong *addr = jlong_to_ptr(address);
struct iovec iov[1]; struct iovec iov[1];
@ -555,8 +555,7 @@ JNIEXPORT jint JNICALL Java_sun_nio_ch_sctp_SctpChannelImpl_send0
* Association already existing, assocId != -1, targetAddress = preferred addr * Association already existing, assocId != -1, targetAddress = preferred addr
*/ */
if (targetAddress != NULL /*&& assocId <= 0*/) { if (targetAddress != NULL /*&& assocId <= 0*/) {
if (NET_InetAddressToSockaddr(env, targetAddress, targetPort, if (NET_InetAddressToSockaddr(env, targetAddress, targetPort, &sa.sa,
(struct sockaddr *)&sa,
&sa_len, JNI_TRUE) != 0) { &sa_len, JNI_TRUE) != 0) {
return IOS_THROWN; return IOS_THROWN;
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2009, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -210,20 +210,20 @@ JNIEXPORT jint JNICALL Java_sun_nio_ch_sctp_SctpNet_socket0
JNIEXPORT void JNICALL Java_sun_nio_ch_sctp_SctpNet_bindx JNIEXPORT void JNICALL Java_sun_nio_ch_sctp_SctpNet_bindx
(JNIEnv *env, jclass klass, jint fd, jobjectArray addrs, jint port, (JNIEnv *env, jclass klass, jint fd, jobjectArray addrs, jint port,
jint addrsLength, jboolean add, jboolean preferIPv6) { jint addrsLength, jboolean add, jboolean preferIPv6) {
SOCKADDR *sap, *tmpSap; SOCKETADDRESS *sap, *tmpSap;
int i, sa_len = sizeof(SOCKADDR); int i, sa_len = sizeof(SOCKETADDRESS);
jobject ia; jobject ia;
if (addrsLength < 1) if (addrsLength < 1)
return; return;
if ((sap = calloc(addrsLength, sa_len)) == NULL) { if ((sap = calloc(addrsLength, sa_len)) == NULL) {
JNU_ThrowOutOfMemoryError(env, "heap allocation failure"); JNU_ThrowOutOfMemoryError(env, "heap allocation failure");
return; return;
} }
tmpSap = sap; tmpSap = sap;
for (i=0; i<addrsLength; i++) { for (i = 0; i < addrsLength; i++) {
ia = (*env)->GetObjectArrayElement(env, addrs, i); ia = (*env)->GetObjectArrayElement(env, addrs, i);
if (NET_InetAddressToSockaddr(env, ia, port, (struct sockaddr*)tmpSap, if (NET_InetAddressToSockaddr(env, ia, port, (struct sockaddr*)tmpSap,
&sa_len, preferIPv6) != 0) { &sa_len, preferIPv6) != 0) {
@ -233,7 +233,7 @@ JNIEXPORT void JNICALL Java_sun_nio_ch_sctp_SctpNet_bindx
tmpSap++; tmpSap++;
} }
if (nio_sctp_bindx(fd, (void*)sap, addrsLength, add ? SCTP_BINDX_ADD_ADDR : if (nio_sctp_bindx(fd, (void *)sap, addrsLength, add ? SCTP_BINDX_ADD_ADDR :
SCTP_BINDX_REM_ADDR) != 0) { SCTP_BINDX_REM_ADDR) != 0) {
handleSocketError(env, errno); handleSocketError(env, errno);
} }
@ -261,16 +261,16 @@ Java_sun_nio_ch_sctp_SctpNet_listen0
JNIEXPORT jint JNICALL JNIEXPORT jint JNICALL
Java_sun_nio_ch_sctp_SctpNet_connect0 Java_sun_nio_ch_sctp_SctpNet_connect0
(JNIEnv *env, jclass clazz, int fd, jobject iao, jint port) { (JNIEnv *env, jclass clazz, int fd, jobject iao, jint port) {
SOCKADDR sa; SOCKETADDRESS sa;
int sa_len = SOCKADDR_LEN; int sa_len = sizeof(SOCKETADDRESS);
int rv; int rv;
if (NET_InetAddressToSockaddr(env, iao, port, (struct sockaddr *) &sa, if (NET_InetAddressToSockaddr(env, iao, port, &sa.sa,
&sa_len, JNI_TRUE) != 0) { &sa_len, JNI_TRUE) != 0) {
return IOS_THROWN; return IOS_THROWN;
} }
rv = connect(fd, (struct sockaddr *)&sa, sa_len); rv = connect(fd, &sa.sa, sa_len);
if (rv != 0) { if (rv != 0) {
if (errno == EINPROGRESS) { if (errno == EINPROGRESS) {
return IOS_UNAVAILABLE; return IOS_UNAVAILABLE;