提交 d4681b69 编写于 作者: C chegar

6905552: libnet/nio portability issues

Reviewed-by: alanb
上级 d9f971bf
......@@ -162,10 +162,11 @@ NET_SockaddrToInetAddress(JNIEnv *env, struct sockaddr *him, int *port) {
JNIEXPORT jint JNICALL
NET_SockaddrEqualsInetAddress(JNIEnv *env, struct sockaddr *him, jobject iaObj)
{
jint family = (*env)->GetIntField(env, iaObj, ia_familyID) == IPv4?
AF_INET : AF_INET6;
jint family = AF_INET;
#ifdef AF_INET6
family = (*env)->GetIntField(env, iaObj, ia_familyID) == IPv4?
AF_INET : AF_INET6;
if (him->sa_family == AF_INET6) {
#ifdef WIN32
struct SOCKADDR_IN6 *him6 = (struct SOCKADDR_IN6 *)him;
......
......@@ -46,11 +46,6 @@
#define HENT_BUF_SIZE 1024
#define BIG_HENT_BUF_SIZE 10240 /* a jumbo-sized one */
#ifndef __GLIBC__
/* gethostname() is in libc.so but I can't find a header file for it */
extern int gethostname(char *buf, int buf_len);
#endif
/************************************************************************
* Inet4AddressImpl
*/
......
......@@ -49,10 +49,6 @@
#define NI_MAXHOST 1025
#endif
#ifndef __GLIBC__
/* gethostname() is in libc.so but I can't find a header file for it */
extern int gethostname(char *buf, int buf_len);
#endif
/************************************************************************
* Inet6AddressImpl
......@@ -360,8 +356,6 @@ Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
}
}
#endif /* AF_INET6 */
cleanupAndReturn:
{
struct addrinfo *iterator, *tmp;
......@@ -374,7 +368,6 @@ cleanupAndReturn:
JNU_ReleaseStringPlatformChars(env, host, hostname);
}
#ifdef AF_INET6
if (NET_addrtransAvailable())
(*freeaddrinfo_ptr)(res);
#endif /* AF_INET6 */
......
......@@ -253,8 +253,12 @@ JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByInetAddress0
(JNIEnv *env, jclass cls, jobject iaObj) {
netif *ifs, *curr;
#ifdef AF_INET6
int family = (*env)->GetIntField(env, iaObj, ni_iafamilyID) == IPv4?
AF_INET : AF_INET6;
#else
int family = AF_INET;
#endif
jobject obj = NULL;
jboolean match = JNI_FALSE;
......@@ -1528,6 +1532,7 @@ JNIEXPORT jint JNICALL Java_java_net_NetworkInterface_getMTU0(JNIEnv *env, jclas
strcpy((caddr_t)&(lifr.lifr_name), name_utf);
if (ioctl(sock, SIOCGLIFMTU, (caddr_t)&lifr) >= 0) {
ret = lifr.lifr_mtu;
#ifdef AF_INET6
} else {
/* Try wIth an IPv6 socket in case the interface has only IPv6 addresses assigned to it */
close(sock);
......@@ -1547,6 +1552,12 @@ JNIEXPORT jint JNICALL Java_java_net_NetworkInterface_getMTU0(JNIEnv *env, jclas
"IOCTL failed");
}
}
#else
} else {
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
"IOCTL failed");
}
#endif
#endif
close(sock);
}
......
......@@ -605,8 +605,12 @@ Java_java_net_PlainDatagramSocketImpl_peek(JNIEnv *env, jobject this,
}
iaObj = NET_SockaddrToInetAddress(env, (struct sockaddr *)&remote_addr, &port);
#ifdef AF_INET6
family = (*env)->GetIntField(env, iaObj, ia_familyID) == IPv4?
AF_INET : AF_INET6;
#else
family = AF_INET;
#endif
if (family == AF_INET) { /* this api can't handle IPV6 addresses */
int address = (*env)->GetIntField(env, iaObj, ia_addressID);
(*env)->SetIntField(env, addressObj, ia_addressID, address);
......@@ -812,9 +816,9 @@ Java_java_net_PlainDatagramSocketImpl_receive0(JNIEnv *env, jobject this,
jboolean retry;
#ifdef __linux__
jboolean connected = JNI_FALSE;
jobject connectedAddress;
jint connectedPort;
jlong prevTime;
jobject connectedAddress = NULL;
jint connectedPort = 0;
jlong prevTime = 0;
#endif
if (IS_NULL(fdObj)) {
......@@ -1186,6 +1190,7 @@ static void mcast_set_if_by_if_v4(JNIEnv *env, jobject this, int fd, jobject val
* Set outgoing multicast interface designated by a NetworkInterface.
* Throw exception if failed.
*/
#ifdef AF_INET6
static void mcast_set_if_by_if_v6(JNIEnv *env, jobject this, int fd, jobject value) {
static jfieldID ni_indexID;
int index;
......@@ -1222,6 +1227,7 @@ static void mcast_set_if_by_if_v6(JNIEnv *env, jobject this, int fd, jobject val
}
#endif
}
#endif /* AF_INET6 */
/*
* Set outgoing multicast interface designated by an InetAddress.
......@@ -1251,6 +1257,7 @@ static void mcast_set_if_by_addr_v4(JNIEnv *env, jobject this, int fd, jobject v
* Set outgoing multicast interface designated by an InetAddress.
* Throw exception if failed.
*/
#ifdef AF_INET6
static void mcast_set_if_by_addr_v6(JNIEnv *env, jobject this, int fd, jobject value) {
static jclass ni_class;
if (ni_class == NULL) {
......@@ -1272,6 +1279,7 @@ static void mcast_set_if_by_addr_v6(JNIEnv *env, jobject this, int fd, jobject v
mcast_set_if_by_if_v6(env, this, fd, value);
}
#endif
/*
* Sets the multicast interface.
......@@ -1307,6 +1315,7 @@ static void setMulticastInterface(JNIEnv *env, jobject this, int fd,
/*
* value is an InetAddress.
*/
#ifdef AF_INET6
#ifdef __solaris__
if (ipv6_available()) {
mcast_set_if_by_addr_v6(env, this, fd, value);
......@@ -1320,12 +1329,16 @@ static void setMulticastInterface(JNIEnv *env, jobject this, int fd,
mcast_set_if_by_addr_v6(env, this, fd, value);
}
#endif
#else
mcast_set_if_by_addr_v4(env, this, fd, value);
#endif /* AF_INET6 */
}
if (opt == java_net_SocketOptions_IP_MULTICAST_IF2) {
/*
* value is a NetworkInterface.
*/
#ifdef AF_INET6
#ifdef __solaris__
if (ipv6_available()) {
mcast_set_if_by_if_v6(env, this, fd, value);
......@@ -1339,6 +1352,9 @@ static void setMulticastInterface(JNIEnv *env, jobject this, int fd,
mcast_set_if_by_if_v6(env, this, fd, value);
}
#endif
#else
mcast_set_if_by_if_v4(env, this, fd, value);
#endif /* AF_INET6 */
}
}
......@@ -1368,6 +1384,7 @@ static void mcast_set_loop_v4(JNIEnv *env, jobject this, int fd, jobject value)
/*
* Enable/disable local loopback of multicast datagrams.
*/
#ifdef AF_INET6
static void mcast_set_loop_v6(JNIEnv *env, jobject this, int fd, jobject value) {
jclass cls;
jfieldID fid;
......@@ -1397,12 +1414,14 @@ static void mcast_set_loop_v6(JNIEnv *env, jobject this, int fd, jobject value)
}
#endif
}
#endif /* AF_INET6 */
/*
* Sets the multicast loopback mode.
*/
static void setMulticastLoopbackMode(JNIEnv *env, jobject this, int fd,
jint opt, jobject value) {
#ifdef AF_INET6
#ifdef __solaris__
if (ipv6_available()) {
mcast_set_loop_v6(env, this, fd, value);
......@@ -1416,6 +1435,9 @@ static void setMulticastLoopbackMode(JNIEnv *env, jobject this, int fd,
mcast_set_loop_v6(env, this, fd, value);
}
#endif
#else
mcast_set_loop_v4(env, this, fd, value);
#endif /* AF_INET6 */
}
/*
......@@ -1838,7 +1860,7 @@ Java_java_net_PlainDatagramSocketImpl_socketGetOption(JNIEnv *env, jobject this,
if (opt == java_net_SocketOptions_SO_BINDADDR) {
/* find out local IP address */
SOCKADDR him;
int len = 0;
socklen_t len = 0;
int port;
jobject iaObj;
......@@ -1941,6 +1963,7 @@ static void setTTL(JNIEnv *env, int fd, jint ttl) {
/*
* Set hops limit for a socket. Throw exception if failed.
*/
#ifdef AF_INET6
static void setHopLimit(JNIEnv *env, int fd, jint ttl) {
int ittl = (int)ttl;
if (JVM_SetSockOpt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
......@@ -1949,6 +1972,7 @@ static void setHopLimit(JNIEnv *env, int fd, jint ttl) {
"Error setting socket option");
}
}
#endif
/*
* Class: java_net_PlainDatagramSocketImpl
......@@ -1971,6 +1995,7 @@ Java_java_net_PlainDatagramSocketImpl_setTimeToLive(JNIEnv *env, jobject this,
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
}
/* setsockopt to be correct ttl */
#ifdef AF_INET6
#ifdef __solaris__
if (ipv6_available()) {
setHopLimit(env, fd, ttl);
......@@ -1986,7 +2011,10 @@ Java_java_net_PlainDatagramSocketImpl_setTimeToLive(JNIEnv *env, jobject this,
(*env)->SetIntField(env, this, pdsi_ttlID, ttl);
}
}
#endif
#endif // __linux__
#else
setTTL(env, fd, ttl);
#endif /* AF_INET6 */
}
/*
......
......@@ -319,8 +319,6 @@ jint IPv6_supported()
#endif /* __solaris */
#endif /* AF_INET6 */
/*
* OK we may have the stack available in the kernel,
* we should also check if the APIs are available.
......@@ -354,6 +352,7 @@ jint IPv6_supported()
close(fd);
return JNI_TRUE;
#endif /* AF_INET6 */
}
void
......
......@@ -133,7 +133,7 @@ extern jint NET_Wait(JNIEnv *env, jint fd, jint flags, jint timeout);
#else
#define SOCKADDR union { struct sockaddr_in him4 }
#define SOCKADDR union { struct sockaddr_in him4; }
#define SOCKADDR_LEN sizeof(SOCKADDR)
#endif
......
......@@ -44,7 +44,11 @@ JNIEXPORT void JNICALL
Java_sun_net_spi_SdpProvider_convert(JNIEnv *env, jclass cls, jint fd)
{
#ifdef PROTO_SDP
#ifdef AF_INET6
int domain = ipv6_available() ? AF_INET6 : AF_INET;
#else
int domain = AF_INET;
#endif
int s = socket(domain, SOCK_STREAM, PROTO_SDP);
if (s < 0) {
JNU_ThrowIOExceptionWithLastError(env, "socket");
......
......@@ -124,6 +124,7 @@ struct my_group_source_req {
* Copy IPv6 group, interface index, and IPv6 source address
* into group_source_req structure.
*/
#ifdef AF_INET6
static void initGroupSourceReq(JNIEnv* env, jbyteArray group, jint index,
jbyteArray source, struct my_group_source_req* req)
{
......@@ -139,7 +140,7 @@ static void initGroupSourceReq(JNIEnv* env, jbyteArray group, jint index,
sin6->sin6_family = AF_INET6;
COPY_INET6_ADDRESS(env, source, (jbyte*)&(sin6->sin6_addr));
}
#endif
JNIEXPORT void JNICALL
Java_sun_nio_ch_Net_initIDs(JNIEnv *env, jclass clazz)
......@@ -159,7 +160,11 @@ Java_sun_nio_ch_Net_socket0(JNIEnv *env, jclass cl, jboolean preferIPv6,
{
int fd;
int type = (stream ? SOCK_STREAM : SOCK_DGRAM);
#ifdef AF_INET6
int domain = (ipv6_available() && preferIPv6) ? AF_INET6 : AF_INET;
#else
int domain = AF_INET;
#endif
fd = socket(domain, type, 0);
if (fd < 0) {
......@@ -176,7 +181,7 @@ Java_sun_nio_ch_Net_socket0(JNIEnv *env, jclass cl, jboolean preferIPv6,
return -1;
}
}
#ifdef __linux__
#if defined(__linux__) && defined(AF_INET6)
/* By default, Linux uses the route default */
if (domain == AF_INET6 && type == SOCK_DGRAM) {
int arg = 1;
......@@ -424,6 +429,7 @@ JNIEXPORT jint JNICALL
Java_sun_nio_ch_Net_joinOrDrop6(JNIEnv *env, jobject this, jboolean join, jobject fdo,
jbyteArray group, jint index, jbyteArray source)
{
#ifdef AF_INET6
struct ipv6_mreq mreq6;
struct my_group_source_req req;
int opt, n, optlen;
......@@ -454,12 +460,17 @@ Java_sun_nio_ch_Net_joinOrDrop6(JNIEnv *env, jobject this, jboolean join, jobjec
handleSocketError(env, errno);
}
return 0;
#else
JNU_ThrowInternalError(env, "Should not get here");
return IOS_THROWN;
#endif /* AF_INET6 */
}
JNIEXPORT jint JNICALL
Java_sun_nio_ch_Net_blockOrUnblock6(JNIEnv *env, jobject this, jboolean block, jobject fdo,
jbyteArray group, jint index, jbyteArray source)
{
#ifdef AF_INET6
struct my_group_source_req req;
int n;
int opt = (block) ? MCAST_BLOCK_SOURCE : MCAST_UNBLOCK_SOURCE;
......@@ -474,6 +485,10 @@ Java_sun_nio_ch_Net_blockOrUnblock6(JNIEnv *env, jobject this, jboolean block, j
handleSocketError(env, errno);
}
return 0;
#else
JNU_ThrowInternalError(env, "Should not get here");
return IOS_THROWN;
#endif
}
JNIEXPORT void JNICALL
......
......@@ -168,14 +168,18 @@ JNIEXPORT jint JNICALL Java_sun_nio_ch_SctpNet_socket0
(JNIEnv *env, jclass klass, jboolean oneToOne) {
int fd;
struct sctp_event_subscribe event;
#ifdef AF_INET6
int domain = ipv6_available() ? AF_INET6 : AF_INET;
#else
int domain = AF_INET;
#endif
/* Try to load the socket API extension functions */
if (!funcsLoaded && !loadSocketExtensionFuncs(env)) {
return 0;
}
fd = socket(ipv6_available() ? AF_INET6 : AF_INET,
(oneToOne ? SOCK_STREAM : SOCK_SEQPACKET), IPPROTO_SCTP);
fd = socket(domain, (oneToOne ? SOCK_STREAM : SOCK_SEQPACKET), IPPROTO_SCTP);
if (fd < 0) {
return handleSocketError(env, errno);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册