550 return 0; |
550 return 0; |
551 } |
551 } |
552 |
552 |
553 iaObj = NET_SockaddrToInetAddress(env, (struct sockaddr *)&remote_addr, &port); |
553 iaObj = NET_SockaddrToInetAddress(env, (struct sockaddr *)&remote_addr, &port); |
554 #ifdef AF_INET6 |
554 #ifdef AF_INET6 |
555 family = (*env)->GetIntField(env, iaObj, ia_familyID) == IPv4? |
555 family = getInetAddress_family(env, iaObj) == IPv4? AF_INET : AF_INET6; |
556 AF_INET : AF_INET6; |
|
557 #else |
556 #else |
558 family = AF_INET; |
557 family = AF_INET; |
559 #endif |
558 #endif |
560 if (family == AF_INET) { /* this API can't handle IPV6 addresses */ |
559 if (family == AF_INET) { /* this API can't handle IPV6 addresses */ |
561 int address = (*env)->GetIntField(env, iaObj, ia_addressID); |
560 int address = getInetAddress_addr(env, iaObj); |
562 (*env)->SetIntField(env, addressObj, ia_addressID, address); |
561 setInetAddress_addr(env, addressObj, address); |
563 } |
562 } |
564 return port; |
563 return port; |
565 } |
564 } |
566 |
565 |
567 JNIEXPORT jint JNICALL |
566 JNIEXPORT jint JNICALL |
1026 * Set outgoing multicast interface designated by a NetworkInterface. |
1025 * Set outgoing multicast interface designated by a NetworkInterface. |
1027 * Throw exception if failed. |
1026 * Throw exception if failed. |
1028 */ |
1027 */ |
1029 static void mcast_set_if_by_if_v4(JNIEnv *env, jobject this, int fd, jobject value) { |
1028 static void mcast_set_if_by_if_v4(JNIEnv *env, jobject this, int fd, jobject value) { |
1030 static jfieldID ni_addrsID; |
1029 static jfieldID ni_addrsID; |
1031 static jfieldID ia_addressID; |
|
1032 struct in_addr in; |
1030 struct in_addr in; |
1033 jobjectArray addrArray; |
1031 jobjectArray addrArray; |
1034 jsize len; |
1032 jsize len; |
1035 jobject addr; |
1033 jobject addr; |
1036 int i; |
1034 int i; |
1037 |
1035 |
1038 if (ni_addrsID == NULL || ia_addressID == NULL) { |
1036 if (ni_addrsID == NULL ) { |
1039 jclass c = (*env)->FindClass(env, "java/net/NetworkInterface"); |
1037 jclass c = (*env)->FindClass(env, "java/net/NetworkInterface"); |
1040 CHECK_NULL(c); |
1038 CHECK_NULL(c); |
1041 ni_addrsID = (*env)->GetFieldID(env, c, "addrs", |
1039 ni_addrsID = (*env)->GetFieldID(env, c, "addrs", |
1042 "[Ljava/net/InetAddress;"); |
1040 "[Ljava/net/InetAddress;"); |
1043 CHECK_NULL(ni_addrsID); |
1041 CHECK_NULL(ni_addrsID); |
1044 c = (*env)->FindClass(env,"java/net/InetAddress"); |
|
1045 CHECK_NULL(c); |
|
1046 ia_addressID = (*env)->GetFieldID(env, c, "address", "I"); |
|
1047 CHECK_NULL(ia_addressID); |
|
1048 } |
1042 } |
1049 |
1043 |
1050 addrArray = (*env)->GetObjectField(env, value, ni_addrsID); |
1044 addrArray = (*env)->GetObjectField(env, value, ni_addrsID); |
1051 len = (*env)->GetArrayLength(env, addrArray); |
1045 len = (*env)->GetArrayLength(env, addrArray); |
1052 |
1046 |
1063 /* |
1057 /* |
1064 * We need an ipv4 address here |
1058 * We need an ipv4 address here |
1065 */ |
1059 */ |
1066 for (i = 0; i < len; i++) { |
1060 for (i = 0; i < len; i++) { |
1067 addr = (*env)->GetObjectArrayElement(env, addrArray, i); |
1061 addr = (*env)->GetObjectArrayElement(env, addrArray, i); |
1068 if ((*env)->GetIntField(env, addr, ia_familyID) == IPv4) { |
1062 if (getInetAddress_family(env, addr) == IPv4) { |
1069 in.s_addr = htonl((*env)->GetIntField(env, addr, ia_addressID)); |
1063 in.s_addr = htonl(getInetAddress_addr(env, addr)); |
1070 break; |
1064 break; |
1071 } |
1065 } |
1072 } |
1066 } |
1073 |
1067 |
1074 if (JVM_SetSockOpt(fd, IPPROTO_IP, IP_MULTICAST_IF, |
1068 if (JVM_SetSockOpt(fd, IPPROTO_IP, IP_MULTICAST_IF, |
1114 /* |
1108 /* |
1115 * Set outgoing multicast interface designated by an InetAddress. |
1109 * Set outgoing multicast interface designated by an InetAddress. |
1116 * Throw exception if failed. |
1110 * Throw exception if failed. |
1117 */ |
1111 */ |
1118 static void mcast_set_if_by_addr_v4(JNIEnv *env, jobject this, int fd, jobject value) { |
1112 static void mcast_set_if_by_addr_v4(JNIEnv *env, jobject this, int fd, jobject value) { |
1119 static jfieldID ia_addressID; |
|
1120 struct in_addr in; |
1113 struct in_addr in; |
1121 |
1114 |
1122 if (ia_addressID == NULL) { |
1115 in.s_addr = htonl( getInetAddress_addr(env, value) ); |
1123 jclass c = (*env)->FindClass(env,"java/net/InetAddress"); |
|
1124 CHECK_NULL(c); |
|
1125 ia_addressID = (*env)->GetFieldID(env, c, "address", "I"); |
|
1126 CHECK_NULL(ia_addressID); |
|
1127 } |
|
1128 |
|
1129 in.s_addr = htonl( (*env)->GetIntField(env, value, ia_addressID) ); |
|
1130 |
1116 |
1131 if (JVM_SetSockOpt(fd, IPPROTO_IP, IP_MULTICAST_IF, |
1117 if (JVM_SetSockOpt(fd, IPPROTO_IP, IP_MULTICAST_IF, |
1132 (const char*)&in, sizeof(in)) < 0) { |
1118 (const char*)&in, sizeof(in)) < 0) { |
1133 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", |
1119 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", |
1134 "Error setting socket option"); |
1120 "Error setting socket option"); |
1454 * IPv4 implementation |
1440 * IPv4 implementation |
1455 */ |
1441 */ |
1456 if (isIPV4) { |
1442 if (isIPV4) { |
1457 static jclass inet4_class; |
1443 static jclass inet4_class; |
1458 static jmethodID inet4_ctrID; |
1444 static jmethodID inet4_ctrID; |
1459 static jfieldID inet4_addrID; |
|
1460 |
1445 |
1461 static jclass ni_class; |
1446 static jclass ni_class; |
1462 static jmethodID ni_ctrID; |
1447 static jmethodID ni_ctrID; |
1463 static jfieldID ni_indexID; |
1448 static jfieldID ni_indexID; |
1464 static jfieldID ni_addrsID; |
1449 static jfieldID ni_addrsID; |
1484 if (inet4_class == NULL) { |
1469 if (inet4_class == NULL) { |
1485 jclass c = (*env)->FindClass(env, "java/net/Inet4Address"); |
1470 jclass c = (*env)->FindClass(env, "java/net/Inet4Address"); |
1486 CHECK_NULL_RETURN(c, NULL); |
1471 CHECK_NULL_RETURN(c, NULL); |
1487 inet4_ctrID = (*env)->GetMethodID(env, c, "<init>", "()V"); |
1472 inet4_ctrID = (*env)->GetMethodID(env, c, "<init>", "()V"); |
1488 CHECK_NULL_RETURN(inet4_ctrID, NULL); |
1473 CHECK_NULL_RETURN(inet4_ctrID, NULL); |
1489 inet4_addrID = (*env)->GetFieldID(env, c, "address", "I"); |
|
1490 CHECK_NULL_RETURN(inet4_addrID, NULL); |
|
1491 inet4_class = (*env)->NewGlobalRef(env, c); |
1474 inet4_class = (*env)->NewGlobalRef(env, c); |
1492 CHECK_NULL_RETURN(inet4_class, NULL); |
1475 CHECK_NULL_RETURN(inet4_class, NULL); |
1493 } |
1476 } |
1494 addr = (*env)->NewObject(env, inet4_class, inet4_ctrID, 0); |
1477 addr = (*env)->NewObject(env, inet4_class, inet4_ctrID, 0); |
1495 CHECK_NULL_RETURN(addr, NULL); |
1478 CHECK_NULL_RETURN(addr, NULL); |
1496 |
1479 |
1497 (*env)->SetIntField(env, addr, inet4_addrID, ntohl(in.s_addr)); |
1480 setInetAddress_addr(env, addr, ntohl(in.s_addr)); |
1498 |
1481 |
1499 /* |
1482 /* |
1500 * For IP_MULTICAST_IF return InetAddress |
1483 * For IP_MULTICAST_IF return InetAddress |
1501 */ |
1484 */ |
1502 if (opt == java_net_SocketOptions_IP_MULTICAST_IF) { |
1485 if (opt == java_net_SocketOptions_IP_MULTICAST_IF) { |
1987 CHECK_NULL(c); |
1970 CHECK_NULL(c); |
1988 ni_indexID = (*env)->GetFieldID(env, c, "index", "I"); |
1971 ni_indexID = (*env)->GetFieldID(env, c, "index", "I"); |
1989 CHECK_NULL(ni_indexID); |
1972 CHECK_NULL(ni_indexID); |
1990 } |
1973 } |
1991 |
1974 |
1992 mname.imr_multiaddr.s_addr = htonl((*env)->GetIntField(env, iaObj, ia_addressID)); |
1975 mname.imr_multiaddr.s_addr = htonl(getInetAddress_addr(env, iaObj)); |
1993 mname.imr_address.s_addr = 0; |
1976 mname.imr_address.s_addr = 0; |
1994 mname.imr_ifindex = (*env)->GetIntField(env, niObj, ni_indexID); |
1977 mname.imr_ifindex = (*env)->GetIntField(env, niObj, ni_indexID); |
1995 mname_len = sizeof(struct ip_mreqn); |
1978 mname_len = sizeof(struct ip_mreqn); |
1996 } else |
1979 } else |
1997 #endif |
1980 #endif |
2005 "No IP addresses bound to interface"); |
1988 "No IP addresses bound to interface"); |
2006 return; |
1989 return; |
2007 } |
1990 } |
2008 addr = (*env)->GetObjectArrayElement(env, addrArray, 0); |
1991 addr = (*env)->GetObjectArrayElement(env, addrArray, 0); |
2009 |
1992 |
2010 mname.imr_multiaddr.s_addr = htonl((*env)->GetIntField(env, iaObj, ia_addressID)); |
1993 mname.imr_multiaddr.s_addr = htonl(getInetAddress_addr(env, iaObj)); |
2011 #ifdef __linux__ |
1994 #ifdef __linux__ |
2012 mname.imr_address.s_addr = htonl((*env)->GetIntField(env, addr, ia_addressID)); |
1995 mname.imr_address.s_addr = htonl(getInetAddress_addr(env, addr)); |
2013 #else |
1996 #else |
2014 mname.imr_interface.s_addr = htonl((*env)->GetIntField(env, addr, ia_addressID)); |
1997 mname.imr_interface.s_addr = htonl(getInetAddress_addr(env, addr)); |
2015 #endif |
1998 #endif |
2016 mname_len = sizeof(struct ip_mreq); |
1999 mname_len = sizeof(struct ip_mreq); |
2017 } |
2000 } |
2018 } |
2001 } |
2019 |
2002 |
2044 (char*)&index, &len) < 0) { |
2027 (char*)&index, &len) < 0) { |
2045 NET_ThrowCurrent(env, "getsockopt IPV6_MULTICAST_IF failed"); |
2028 NET_ThrowCurrent(env, "getsockopt IPV6_MULTICAST_IF failed"); |
2046 return; |
2029 return; |
2047 } |
2030 } |
2048 |
2031 |
2049 mname.imr_multiaddr.s_addr = htonl((*env)->GetIntField(env, iaObj, ia_addressID)); |
2032 mname.imr_multiaddr.s_addr = htonl(getInetAddress_addr(env, iaObj)); |
2050 mname.imr_address.s_addr = 0 ; |
2033 mname.imr_address.s_addr = 0 ; |
2051 mname.imr_ifindex = index; |
2034 mname.imr_ifindex = index; |
2052 mname_len = sizeof(struct ip_mreqn); |
2035 mname_len = sizeof(struct ip_mreqn); |
2053 } else |
2036 } else |
2054 #endif |
2037 #endif |
2131 struct ipv6_mreq mname6; |
2114 struct ipv6_mreq mname6; |
2132 jbyteArray ipaddress; |
2115 jbyteArray ipaddress; |
2133 jbyte caddr[16]; |
2116 jbyte caddr[16]; |
2134 jint family; |
2117 jint family; |
2135 jint address; |
2118 jint address; |
2136 family = (*env)->GetIntField(env, iaObj, ia_familyID) == IPv4? AF_INET : AF_INET6; |
2119 family = getInetAddress_family(env, iaObj) == IPv4? AF_INET : AF_INET6; |
2137 if (family == AF_INET) { /* will convert to IPv4-mapped address */ |
2120 if (family == AF_INET) { /* will convert to IPv4-mapped address */ |
2138 memset((char *) caddr, 0, 16); |
2121 memset((char *) caddr, 0, 16); |
2139 address = (*env)->GetIntField(env, iaObj, ia_addressID); |
2122 address = getInetAddress_addr(env, iaObj); |
2140 |
2123 |
2141 caddr[10] = 0xff; |
2124 caddr[10] = 0xff; |
2142 caddr[11] = 0xff; |
2125 caddr[11] = 0xff; |
2143 |
2126 |
2144 caddr[12] = ((address >> 24) & 0xff); |
2127 caddr[12] = ((address >> 24) & 0xff); |