124 static jobject createNetworkInterface(JNIEnv *env, netif *ifs); |
124 static jobject createNetworkInterface(JNIEnv *env, netif *ifs); |
125 static int getFlags0(JNIEnv *env, jstring ifname); |
125 static int getFlags0(JNIEnv *env, jstring ifname); |
126 |
126 |
127 static netif *enumInterfaces(JNIEnv *env); |
127 static netif *enumInterfaces(JNIEnv *env); |
128 static netif *enumIPv4Interfaces(JNIEnv *env, int sock, netif *ifs); |
128 static netif *enumIPv4Interfaces(JNIEnv *env, int sock, netif *ifs); |
129 |
|
130 #if defined(AF_INET6) |
|
131 static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs); |
129 static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs); |
132 #endif |
|
133 |
130 |
134 static netif *addif(JNIEnv *env, int sock, const char *if_name, netif *ifs, |
131 static netif *addif(JNIEnv *env, int sock, const char *if_name, netif *ifs, |
135 struct sockaddr *ifr_addrP, |
132 struct sockaddr *ifr_addrP, |
136 struct sockaddr *ifr_broadaddrP, |
133 struct sockaddr *ifr_broadaddrP, |
137 int family, short prefix); |
134 int family, short prefix); |
310 */ |
307 */ |
311 JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByInetAddress0 |
308 JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByInetAddress0 |
312 (JNIEnv *env, jclass cls, jobject iaObj) |
309 (JNIEnv *env, jclass cls, jobject iaObj) |
313 { |
310 { |
314 netif *ifs, *curr; |
311 netif *ifs, *curr; |
315 #if defined(AF_INET6) |
312 int family = (getInetAddress_family(env, iaObj) == java_net_InetAddress_IPv4) ? |
316 int family = (getInetAddress_family(env, iaObj) == java_net_InetAddress_IPv4) ? AF_INET : AF_INET6; |
313 AF_INET : AF_INET6; |
317 #else |
|
318 int family = AF_INET; |
|
319 #endif |
|
320 jobject obj = NULL; |
314 jobject obj = NULL; |
321 jboolean match = JNI_FALSE; |
315 jboolean match = JNI_FALSE; |
322 |
316 |
323 ifs = enumInterfaces(env); |
317 ifs = enumInterfaces(env); |
324 if (ifs == NULL) { |
318 if (ifs == NULL) { |
340 |
334 |
341 if (address1 == address2) { |
335 if (address1 == address2) { |
342 match = JNI_TRUE; |
336 match = JNI_TRUE; |
343 break; |
337 break; |
344 } |
338 } |
345 } |
339 } else if (family == AF_INET6) { |
346 #if defined(AF_INET6) |
|
347 if (family == AF_INET6) { |
|
348 jbyte *bytes = (jbyte *)&( |
340 jbyte *bytes = (jbyte *)&( |
349 ((struct sockaddr_in6*)addrP->addr)->sin6_addr); |
341 ((struct sockaddr_in6*)addrP->addr)->sin6_addr); |
350 jbyte caddr[16]; |
342 jbyte caddr[16]; |
351 int i; |
343 int i; |
352 getInet6Address_ipaddress(env, iaObj, (char *)caddr); |
344 getInet6Address_ipaddress(env, iaObj, (char *)caddr); |
704 (*env)->SetObjectArrayElement(env, bindArr, bind_index++, ibObj); |
695 (*env)->SetObjectArrayElement(env, bindArr, bind_index++, ibObj); |
705 } else { |
696 } else { |
706 return NULL; |
697 return NULL; |
707 } |
698 } |
708 } |
699 } |
709 #if defined(AF_INET6) |
|
710 if (addrP->family == AF_INET6) { |
700 if (addrP->family == AF_INET6) { |
711 int scope=0; |
701 int scope=0; |
712 iaObj = (*env)->NewObject(env, ia6_class, ia6_ctrID); |
702 iaObj = (*env)->NewObject(env, ia6_class, ia6_ctrID); |
713 if (iaObj) { |
703 if (iaObj) { |
714 jboolean ret = setInet6Address_ipaddress(env, iaObj, |
704 jboolean ret = setInet6Address_ipaddress(env, iaObj, |
794 if (ifs == NULL && (*env)->ExceptionOccurred(env)) { |
783 if (ifs == NULL && (*env)->ExceptionOccurred(env)) { |
795 return NULL; |
784 return NULL; |
796 } |
785 } |
797 |
786 |
798 // If IPv6 is available then enumerate IPv6 addresses. |
787 // If IPv6 is available then enumerate IPv6 addresses. |
799 #if defined(AF_INET6) |
788 // User can disable ipv6 explicitly by -Djava.net.preferIPv4Stack=true, |
800 // User can disable ipv6 explicitly by -Djava.net.preferIPv4Stack=true, |
789 // so we have to call ipv6_available() |
801 // so we have to call ipv6_available() |
790 if (ipv6_available()) { |
802 if (ipv6_available()) { |
791 sock = openSocket(env, AF_INET6); |
803 sock = openSocket(env, AF_INET6); |
792 if (sock < 0 && (*env)->ExceptionOccurred(env)) { |
804 if (sock < 0 && (*env)->ExceptionOccurred(env)) { |
793 freeif(ifs); |
805 freeif(ifs); |
794 return NULL; |
806 return NULL; |
795 } |
807 } |
796 |
808 |
797 ifs = enumIPv6Interfaces(env, sock, ifs); |
809 ifs = enumIPv6Interfaces(env, sock, ifs); |
798 close(sock); |
810 close(sock); |
799 |
811 |
800 if ((*env)->ExceptionOccurred(env)) { |
812 if ((*env)->ExceptionOccurred(env)) { |
801 freeif(ifs); |
813 freeif(ifs); |
802 return NULL; |
814 return NULL; |
803 } |
815 } |
804 } |
816 } |
|
817 #endif |
|
818 |
805 |
819 return ifs; |
806 return ifs; |
820 } |
807 } |
821 |
808 |
822 /* |
809 /* |
868 // Create and populate the netaddr node. If allocation fails |
855 // Create and populate the netaddr node. If allocation fails |
869 // return an un-updated list. |
856 // return an un-updated list. |
870 |
857 |
871 // Allocate for addr and brdcast at once |
858 // Allocate for addr and brdcast at once |
872 |
859 |
873 #if defined(AF_INET6) |
|
874 addr_size = (family == AF_INET) ? sizeof(struct sockaddr_in) |
860 addr_size = (family == AF_INET) ? sizeof(struct sockaddr_in) |
875 : sizeof(struct sockaddr_in6); |
861 : sizeof(struct sockaddr_in6); |
876 #else |
|
877 addr_size = sizeof(struct sockaddr_in); |
|
878 #endif |
|
879 |
862 |
880 CHECKED_MALLOC3(addrP, netaddr *, sizeof(netaddr) + 2 * addr_size); |
863 CHECKED_MALLOC3(addrP, netaddr *, sizeof(netaddr) + 2 * addr_size); |
881 addrP->addr = (struct sockaddr *)((char *)addrP + sizeof(netaddr)); |
864 addrP->addr = (struct sockaddr *)((char *)addrP + sizeof(netaddr)); |
882 memcpy(addrP->addr, ifr_addrP, addr_size); |
865 memcpy(addrP->addr, ifr_addrP, addr_size); |
883 |
866 |
1062 } |
1045 } |
1063 |
1046 |
1064 /** Linux **/ |
1047 /** Linux **/ |
1065 #if defined(__linux__) |
1048 #if defined(__linux__) |
1066 |
1049 |
1067 #if defined(AF_INET6) |
|
1068 /* |
1050 /* |
1069 * Opens a socket for further ioctl calls. Tries AF_INET socket first and |
1051 * Opens a socket for further ioctl calls. Tries AF_INET socket first and |
1070 * if it fails return AF_INET6 socket. |
1052 * if it fails return AF_INET6 socket. |
1071 */ |
1053 */ |
1072 static int openSocketWithFallback(JNIEnv *env, const char *ifname) { |
1054 static int openSocketWithFallback(JNIEnv *env, const char *ifname) { |
1088 |
1070 |
1089 // Linux starting from 2.6.? kernel allows ioctl call with either IPv4 or |
1071 // Linux starting from 2.6.? kernel allows ioctl call with either IPv4 or |
1090 // IPv6 socket regardless of type of address of an interface. |
1072 // IPv6 socket regardless of type of address of an interface. |
1091 return sock; |
1073 return sock; |
1092 } |
1074 } |
1093 #else |
|
1094 static int openSocketWithFallback(JNIEnv *env, const char *ifname) { |
|
1095 return openSocket(env, AF_INET); |
|
1096 } |
|
1097 #endif |
|
1098 |
1075 |
1099 /* |
1076 /* |
1100 * Enumerates and returns all IPv4 interfaces on Linux. |
1077 * Enumerates and returns all IPv4 interfaces on Linux. |
1101 */ |
1078 */ |
1102 static netif *enumIPv4Interfaces(JNIEnv *env, int sock, netif *ifs) { |
1079 static netif *enumIPv4Interfaces(JNIEnv *env, int sock, netif *ifs) { |
1309 #endif /* __linux__ */ |
1282 #endif /* __linux__ */ |
1310 |
1283 |
1311 /** AIX **/ |
1284 /** AIX **/ |
1312 #if defined(_AIX) |
1285 #if defined(_AIX) |
1313 |
1286 |
1314 #if defined(AF_INET6) |
|
1315 /* |
1287 /* |
1316 * Opens a socket for further ioctl calls. Tries AF_INET socket first and |
1288 * Opens a socket for further ioctl calls. Tries AF_INET socket first and |
1317 * if it fails return AF_INET6 socket. |
1289 * if it fails return AF_INET6 socket. |
1318 */ |
1290 */ |
1319 static int openSocketWithFallback(JNIEnv *env, const char *ifname) { |
1291 static int openSocketWithFallback(JNIEnv *env, const char *ifname) { |
1593 #endif /* _AIX */ |
1556 #endif /* _AIX */ |
1594 |
1557 |
1595 /** Solaris **/ |
1558 /** Solaris **/ |
1596 #if defined(__solaris__) |
1559 #if defined(__solaris__) |
1597 |
1560 |
1598 #if defined(AF_INET6) |
|
1599 /* |
1561 /* |
1600 * Opens a socket for further ioctl calls. Tries AF_INET socket first and |
1562 * Opens a socket for further ioctl calls. Tries AF_INET socket first and |
1601 * if it fails return AF_INET6 socket. |
1563 * if it fails return AF_INET6 socket. |
1602 */ |
1564 */ |
1603 static int openSocketWithFallback(JNIEnv *env, const char *ifname) { |
1565 static int openSocketWithFallback(JNIEnv *env, const char *ifname) { |
1955 #endif /* __solaris__ */ |
1908 #endif /* __solaris__ */ |
1956 |
1909 |
1957 /** BSD **/ |
1910 /** BSD **/ |
1958 #if defined(_ALLBSD_SOURCE) |
1911 #if defined(_ALLBSD_SOURCE) |
1959 |
1912 |
1960 #if defined(AF_INET6) |
|
1961 /* |
1913 /* |
1962 * Opens a socket for further ioctl calls. Tries AF_INET socket first and |
1914 * Opens a socket for further ioctl calls. Tries AF_INET socket first and |
1963 * if it fails return AF_INET6 socket. |
1915 * if it fails return AF_INET6 socket. |
1964 */ |
1916 */ |
1965 static int openSocketWithFallback(JNIEnv *env, const char *ifname) { |
1917 static int openSocketWithFallback(JNIEnv *env, const char *ifname) { |
2029 // free ifaddrs buffer |
1976 // free ifaddrs buffer |
2030 freeifaddrs(origifa); |
1977 freeifaddrs(origifa); |
2031 return ifs; |
1978 return ifs; |
2032 } |
1979 } |
2033 |
1980 |
2034 #if defined(AF_INET6) |
|
2035 |
|
2036 /* |
1981 /* |
2037 * Enumerates and returns all IPv6 interfaces on BSD. |
1982 * Enumerates and returns all IPv6 interfaces on BSD. |
2038 */ |
1983 */ |
2039 static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs) { |
1984 static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs) { |
2040 struct ifaddrs *ifa, *origifa; |
1985 struct ifaddrs *ifa, *origifa; |