jdk/src/java.base/unix/native/libnet/NetworkInterface.c
changeset 41834 99c4f72c0a91
parent 41771 18c9669e76ca
child 42945 1a9931e1c0f5
equal deleted inserted replaced
41833:9a8760507dcf 41834:99c4f72c0a91
   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);
   360                     if (i >= 16) {
   352                     if (i >= 16) {
   361                         match = JNI_TRUE;
   353                         match = JNI_TRUE;
   362                         break;
   354                         break;
   363                     }
   355                     }
   364                 }
   356                 }
   365 #endif
       
   366             }
   357             }
   367 
   358 
   368             if (match) {
   359             if (match) {
   369                 break;
   360                 break;
   370             }
   361             }
   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,
   733                 (*env)->SetObjectArrayElement(env, bindArr, bind_index++, ibObj);
   723                 (*env)->SetObjectArrayElement(env, bindArr, bind_index++, ibObj);
   734             } else {
   724             } else {
   735                 return NULL;
   725                 return NULL;
   736             }
   726             }
   737         }
   727         }
   738 #endif
       
   739 
   728 
   740         (*env)->SetObjectArrayElement(env, addrArr, addr_index++, iaObj);
   729         (*env)->SetObjectArrayElement(env, addrArr, addr_index++, iaObj);
   741         addrP = addrP->next;
   730         addrP = addrP->next;
   742     }
   731     }
   743 
   732 
   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) {
  1176     // free buffer
  1153     // free buffer
  1177     free(buf);
  1154     free(buf);
  1178     return ifs;
  1155     return ifs;
  1179 }
  1156 }
  1180 
  1157 
  1181 #if defined(AF_INET6)
       
  1182 
       
  1183 /*
  1158 /*
  1184  * Enumerates and returns all IPv6 interfaces on Linux.
  1159  * Enumerates and returns all IPv6 interfaces on Linux.
  1185  */
  1160  */
  1186 static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs) {
  1161 static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs) {
  1187     FILE *f;
  1162     FILE *f;
  1219        fclose(f);
  1194        fclose(f);
  1220     }
  1195     }
  1221     return ifs;
  1196     return ifs;
  1222 }
  1197 }
  1223 
  1198 
  1224 #endif /* AF_INET6 */
       
  1225 
       
  1226 /*
  1199 /*
  1227  * Try to get the interface index.
  1200  * Try to get the interface index.
  1228  */
  1201  */
  1229 static int getIndex(int sock, const char *name) {
  1202 static int getIndex(int sock, const char *name) {
  1230     struct ifreq if2;
  1203     struct ifreq if2;
  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) {
  1333         }
  1305         }
  1334     }
  1306     }
  1335 
  1307 
  1336     return sock;
  1308     return sock;
  1337 }
  1309 }
  1338 #else
       
  1339 static int openSocketWithFallback(JNIEnv *env, const char *ifname) {
       
  1340     return openSocket(env, AF_INET);
       
  1341 }
       
  1342 #endif
       
  1343 
  1310 
  1344 /*
  1311 /*
  1345  * Enumerates and returns all IPv4 interfaces on AIX.
  1312  * Enumerates and returns all IPv4 interfaces on AIX.
  1346  */
  1313  */
  1347 static netif *enumIPv4Interfaces(JNIEnv *env, int sock, netif *ifs) {
  1314 static netif *enumIPv4Interfaces(JNIEnv *env, int sock, netif *ifs) {
  1421     // free buffer
  1388     // free buffer
  1422     free(buf);
  1389     free(buf);
  1423     return ifs;
  1390     return ifs;
  1424 }
  1391 }
  1425 
  1392 
  1426 #if defined(AF_INET6)
       
  1427 
       
  1428 /*
  1393 /*
  1429  * Enumerates and returns all IPv6 interfaces on AIX.
  1394  * Enumerates and returns all IPv6 interfaces on AIX.
  1430  */
  1395  */
  1431 static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs) {
  1396 static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs) {
  1432     struct ifconf ifc;
  1397     struct ifconf ifc;
  1497     // free buffer
  1462     // free buffer
  1498     free(buf);
  1463     free(buf);
  1499     return ifs;
  1464     return ifs;
  1500 }
  1465 }
  1501 
  1466 
  1502 #endif /* AF_INET6 */
       
  1503 
       
  1504 /*
  1467 /*
  1505  * Try to get the interface index.
  1468  * Try to get the interface index.
  1506  */
  1469  */
  1507 static int getIndex(int sock, const char *name) {
  1470 static int getIndex(int sock, const char *name) {
  1508     int index = if_nametoindex(name);
  1471     int index = if_nametoindex(name);
  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) {
  1638         }
  1600         }
  1639     }
  1601     }
  1640 
  1602 
  1641     return sock;
  1603     return sock;
  1642 }
  1604 }
  1643 #else
       
  1644 static int openSocketWithFallback(JNIEnv *env, const char *ifname) {
       
  1645     return openSocket(env, AF_INET);
       
  1646 }
       
  1647 #endif
       
  1648 
  1605 
  1649 /*
  1606 /*
  1650  * Enumerates and returns all IPv4 interfaces on Solaris.
  1607  * Enumerates and returns all IPv4 interfaces on Solaris.
  1651  */
  1608  */
  1652 static netif *enumIPv4Interfaces(JNIEnv *env, int sock, netif *ifs) {
  1609 static netif *enumIPv4Interfaces(JNIEnv *env, int sock, netif *ifs) {
  1718     // free buffer
  1675     // free buffer
  1719     free(buf);
  1676     free(buf);
  1720     return ifs;
  1677     return ifs;
  1721 }
  1678 }
  1722 
  1679 
  1723 #if defined(AF_INET6)
       
  1724 
       
  1725 /*
  1680 /*
  1726  * Enumerates and returns all IPv6 interfaces on Solaris.
  1681  * Enumerates and returns all IPv6 interfaces on Solaris.
  1727  */
  1682  */
  1728 static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs) {
  1683 static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs) {
  1729     struct lifconf ifc;
  1684     struct lifconf ifc;
  1782     // free buffer
  1737     // free buffer
  1783     free(buf);
  1738     free(buf);
  1784     return ifs;
  1739     return ifs;
  1785 }
  1740 }
  1786 
  1741 
  1787 #endif /* AF_INET6 */
       
  1788 
       
  1789 /*
  1742 /*
  1790  * Try to get the interface index.
  1743  * Try to get the interface index.
  1791  * (Not supported on Solaris 2.6 or 7)
  1744  * (Not supported on Solaris 2.6 or 7)
  1792  */
  1745  */
  1793 static int getIndex(int sock, const char *name) {
  1746 static int getIndex(int sock, const char *name) {
  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) {
  1979         }
  1931         }
  1980     }
  1932     }
  1981 
  1933 
  1982     return sock;
  1934     return sock;
  1983 }
  1935 }
  1984 #else
       
  1985 static int openSocketWithFallback(JNIEnv *env, const char *ifname) {
       
  1986     return openSocket(env, AF_INET);
       
  1987 }
       
  1988 #endif
       
  1989 
  1936 
  1990 /*
  1937 /*
  1991  * Enumerates and returns all IPv4 interfaces on BSD.
  1938  * Enumerates and returns all IPv4 interfaces on BSD.
  1992  */
  1939  */
  1993 static netif *enumIPv4Interfaces(JNIEnv *env, int sock, netif *ifs) {
  1940 static netif *enumIPv4Interfaces(JNIEnv *env, int sock, netif *ifs) {
  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;
  2070 
  2015 
  2071     // free ifaddrs buffer
  2016     // free ifaddrs buffer
  2072     freeifaddrs(origifa);
  2017     freeifaddrs(origifa);
  2073     return ifs;
  2018     return ifs;
  2074 }
  2019 }
  2075 
       
  2076 #endif /* AF_INET6 */
       
  2077 
  2020 
  2078 /*
  2021 /*
  2079  * Try to get the interface index.
  2022  * Try to get the interface index.
  2080  */
  2023  */
  2081 static int getIndex(int sock, const char *name) {
  2024 static int getIndex(int sock, const char *name) {