< prev index next >

src/java.base/unix/native/libnet/NetworkInterface.c

Print this page




1054         }
1055         for (; bit != 0; bit--) {
1056             if (addrBytes[byte] & (1 << bit)) {
1057                 prefix = 0;
1058                 break;
1059             }
1060         }
1061         if (prefix > 0) {
1062             byte++;
1063             for (; byte < sizeof(struct in6_addr); byte++) {
1064                 if (addrBytes[byte]) {
1065                     prefix = 0;
1066                 }
1067             }
1068         }
1069     }
1070 
1071     return prefix;
1072 }
1073 








1074 /*
1075  * Opens a socket for further ioct calls. proto is one of AF_INET or AF_INET6.
1076  */
1077 static int openSocket(JNIEnv *env, int proto) {
1078     int sock;
1079 
1080     if ((sock = socket(proto, SOCK_DGRAM, 0)) < 0) {
1081         // If we lack support for this address family or protocol,
1082         // don't throw an exception.
1083         if (errno != EPROTONOSUPPORT && errno != EAFNOSUPPORT) {
1084             JNU_ThrowByNameWithMessageAndLastError
1085                 (env, JNU_JAVANETPKG "SocketException", "Socket creation failed");
1086         }
1087         return -1;
1088     }
1089 
1090     return sock;
1091 }
1092 
1093 /** Linux **/
1094 #if defined(__linux__)
1095 
1096 /*
1097  * Opens a socket for further ioctl calls. Tries AF_INET socket first and
1098  * if it fails return AF_INET6 socket.
1099  */
1100 static int openSocketWithFallback(JNIEnv *env, const char *ifname) {
1101     int sock;
1102 
1103     if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1104         if (errno == EPROTONOSUPPORT || errno == EAFNOSUPPORT) {
1105             if ((sock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1106                 JNU_ThrowByNameWithMessageAndLastError
1107                     (env, JNU_JAVANETPKG "SocketException", "IPV6 Socket creation failed");
1108                 return -1;
1109             }
1110         } else { // errno is not NOSUPPORT
1111             JNU_ThrowByNameWithMessageAndLastError
1112                 (env, JNU_JAVANETPKG "SocketException", "IPV4 Socket creation failed");
1113             return -1;
1114         }
1115     }
1116 
1117     // Linux starting from 2.6.? kernel allows ioctl call with either IPv4 or
1118     // IPv6 socket regardless of type of address of an interface.
1119     return sock;
1120 }
1121 
1122 /*
1123  * Enumerates and returns all IPv4 interfaces on Linux.
1124  */
1125 static netif *enumIPv4Interfaces(JNIEnv *env, int sock, netif *ifs) {




1054         }
1055         for (; bit != 0; bit--) {
1056             if (addrBytes[byte] & (1 << bit)) {
1057                 prefix = 0;
1058                 break;
1059             }
1060         }
1061         if (prefix > 0) {
1062             byte++;
1063             for (; byte < sizeof(struct in6_addr); byte++) {
1064                 if (addrBytes[byte]) {
1065                     prefix = 0;
1066                 }
1067             }
1068         }
1069     }
1070 
1071     return prefix;
1072 }
1073 
1074 static int socket0(int domain, int type, int protocol) {
1075     if (domain == AF_INET) {
1076         errno = EAFNOSUPPORT;
1077         return -1;
1078     }
1079     return socket(domain, type, protocol);
1080 }
1081 
1082 /*
1083  * Opens a socket for further ioct calls. proto is one of AF_INET or AF_INET6.
1084  */
1085 static int openSocket(JNIEnv *env, int proto) {
1086     int sock;
1087 
1088     if ((sock = socket0(proto, SOCK_DGRAM, 0)) < 0) {
1089         // If we lack support for this address family or protocol,
1090         // don't throw an exception.
1091         if (errno != EPROTONOSUPPORT && errno != EAFNOSUPPORT) {
1092             JNU_ThrowByNameWithMessageAndLastError
1093                 (env, JNU_JAVANETPKG "SocketException", "Socket creation failed");
1094         }
1095         return -1;
1096     }
1097 
1098     return sock;
1099 }
1100 
1101 /** Linux **/
1102 #if defined(__linux__)
1103 
1104 /*
1105  * Opens a socket for further ioctl calls. Tries AF_INET socket first and
1106  * if it fails return AF_INET6 socket.
1107  */
1108 static int openSocketWithFallback(JNIEnv *env, const char *ifname) {
1109     int sock;
1110 
1111     if ((sock = socket0(AF_INET, SOCK_DGRAM, 0)) < 0) {
1112         if (errno == EPROTONOSUPPORT || errno == EAFNOSUPPORT) {
1113             if ((sock = socket0(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1114                 JNU_ThrowByNameWithMessageAndLastError
1115                     (env, JNU_JAVANETPKG "SocketException", "IPV6 Socket creation failed");
1116                 return -1;
1117             }
1118         } else { // errno is not NOSUPPORT
1119             JNU_ThrowByNameWithMessageAndLastError
1120                 (env, JNU_JAVANETPKG "SocketException", "IPV4 Socket creation failed");
1121             return -1;
1122         }
1123     }
1124 
1125     // Linux starting from 2.6.? kernel allows ioctl call with either IPv4 or
1126     // IPv6 socket regardless of type of address of an interface.
1127     return sock;
1128 }
1129 
1130 /*
1131  * Enumerates and returns all IPv4 interfaces on Linux.
1132  */
1133 static netif *enumIPv4Interfaces(JNIEnv *env, int sock, netif *ifs) {


< prev index next >