1 /*
2 * Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <arpa/inet.h>
38 #include <net/if.h>
39 #include <net/if_arp.h>
40
41 #ifdef __solaris__
42 #include <sys/dlpi.h>
43 #include <fcntl.h>
44 #include <stropts.h>
45 #include <sys/sockio.h>
46 #endif
47
48 #ifdef __linux__
49 #include <sys/ioctl.h>
50 #include <bits/ioctls.h>
51 #include <sys/utsname.h>
52 #include <stdio.h>
53 #endif
54
55 #ifdef __linux__
56 #define _PATH_PROCNET_IFINET6 "/proc/net/if_inet6"
57 #endif
58
59 #if defined(_ALLBSD_SOURCE)
60 #include <sys/param.h>
61 #include <sys/ioctl.h>
62 #include <sys/sockio.h>
63 #if defined(__APPLE__)
64 #include <net/ethernet.h>
65 #include <net/if_var.h>
66 #include <net/if_dl.h>
67 #include <netinet/in_var.h>
68 #include <ifaddrs.h>
69 #endif
70 #endif
71
72 #include "jvm.h"
73 #include "jni_util.h"
74 #include "net_util.h"
1024 * proto is AF_INET/AF_INET6
1025 */
1026 static int openSocket(JNIEnv *env, int proto){
1027 int sock;
1028
1029 if ((sock = JVM_Socket(proto, SOCK_DGRAM, 0)) < 0) {
1030 /*
1031 * If EPROTONOSUPPORT is returned it means we don't have
1032 * support for this proto so don't throw an exception.
1033 */
1034 if (errno != EPROTONOSUPPORT) {
1035 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "Socket creation failed");
1036 }
1037 return -1;
1038 }
1039
1040 return sock;
1041 }
1042
1043
1044 /** Linux **/
1045 #ifdef __linux__
1046 /* Open socket for further ioct calls, try v4 socket first and
1047 * if it falls return v6 socket
1048 */
1049
1050 #ifdef AF_INET6
1051 static int openSocketWithFallback(JNIEnv *env, const char *ifname){
1052 int sock;
1053 struct ifreq if2;
1054
1055 if ((sock = JVM_Socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1056 if (errno == EPROTONOSUPPORT){
1057 if ( (sock = JVM_Socket(AF_INET6, SOCK_DGRAM, 0)) < 0 ){
1058 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "IPV6 Socket creation failed");
1059 return -1;
1060 }
1061 }
1062 else{ // errno is not NOSUPPORT
1063 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "IPV4 Socket creation failed");
1064 return -1;
1065 }
1066 }
1067
1068 /* Linux starting from 2.6.? kernel allows ioctl call with either IPv4 or IPv6 socket regardless of type
1069 of address of an interface */
1070
1071 return sock;
1072 }
1073
1074 #else
1075 static int openSocketWithFallback(JNIEnv *env, const char *ifname){
1076 return openSocket(env,AF_INET);
1077 }
1078 #endif
1079
1080 static netif *enumIPv4Interfaces(JNIEnv *env, int sock, netif *ifs) {
1081 struct ifconf ifc;
1082 struct ifreq *ifreqP;
1083 char *buf;
1084 int numifs;
1085 unsigned i;
1086
1087
1088 /* need to do a dummy SIOCGIFCONF to determine the buffer size.
1089 * SIOCGIFCOUNT doesn't work
1090 */
1091 ifc.ifc_buf = NULL;
1092 if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
1093 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "ioctl SIOCGIFCONF failed");
1094 return ifs;
1095 }
1096
1097 CHECKED_MALLOC3(buf,char *, ifc.ifc_len);
1098
1099 ifc.ifc_buf = buf;
1100 if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
1101 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "ioctl SIOCGIFCONF failed");
1102 (void) free(buf);
1103 return ifs;
1104 }
1105
1106 /*
1107 * Iterate through each interface
1108 */
1109 ifreqP = ifc.ifc_req;
1110 for (i=0; i<ifc.ifc_len/sizeof (struct ifreq); i++, ifreqP++) {
1111 /*
1112 * Add to the list
1113 */
1114 ifs = addif(env, sock, ifreqP->ifr_name, ifs, (struct sockaddr *) & (ifreqP->ifr_addr), AF_INET, 0);
1115
1116 /*
1117 * If an exception occurred then free the list
1118 */
1119 if ((*env)->ExceptionOccurred(env)) {
1120 free(buf);
1121 freeif(ifs);
1122 return NULL;
1123 }
1124 }
1125
1126 /*
1127 * Free socket and buffer
1128 */
1129 free(buf);
1130 return ifs;
1131 }
1132
1133
1134 /*
1135 * Enumerates and returns all IPv6 interfaces on Linux
1136 */
1137
1138 #ifdef AF_INET6
1139 static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs) {
1140 FILE *f;
1141 char addr6[40], devname[21];
1142 char addr6p[8][5];
1143 int plen, scope, dad_status, if_idx;
1144 uint8_t ipv6addr[16];
1145
1146 if ((f = fopen(_PATH_PROCNET_IFINET6, "r")) != NULL) {
1147 while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %08x %02x %02x %02x %20s\n",
1148 addr6p[0], addr6p[1], addr6p[2], addr6p[3], addr6p[4], addr6p[5], addr6p[6], addr6p[7],
1149 &if_idx, &plen, &scope, &dad_status, devname) != EOF) {
1150
1151 struct netif *ifs_ptr = NULL;
1152 struct netif *last_ptr = NULL;
1153 struct sockaddr_in6 addr;
1154
1155 sprintf(addr6, "%s:%s:%s:%s:%s:%s:%s:%s",
1156 addr6p[0], addr6p[1], addr6p[2], addr6p[3], addr6p[4], addr6p[5], addr6p[6], addr6p[7]);
1157 inet_pton(AF_INET6, addr6, ipv6addr);
1158
1162 addr.sin6_scope_id = if_idx;
1163
1164 ifs = addif(env, sock, devname, ifs, (struct sockaddr *)&addr, AF_INET6, plen);
1165
1166
1167 /*
1168 * If an exception occurred then return the list as is.
1169 */
1170 if ((*env)->ExceptionOccurred(env)) {
1171 fclose(f);
1172 return ifs;
1173 }
1174 }
1175 fclose(f);
1176 }
1177 return ifs;
1178 }
1179 #endif
1180
1181
1182 static int getIndex(int sock, const char *name){
1183 /*
1184 * Try to get the interface index
1185 * (Not supported on Solaris 2.6 or 7)
1186 */
1187 struct ifreq if2;
1188 strcpy(if2.ifr_name, name);
1189
1190 if (ioctl(sock, SIOCGIFINDEX, (char *)&if2) < 0) {
1191 return -1;
1192 }
1193
1194 return if2.ifr_ifindex;
1195 }
1196
1197 /**
1198 * Returns the IPv4 broadcast address of a named interface, if it exists.
1199 * Returns 0 if it doesn't have one.
1200 */
1201 static struct sockaddr *getBroadcast(JNIEnv *env, int sock, const char *ifname, struct sockaddr *brdcast_store) {
1202 struct sockaddr *ret = NULL;
1203 struct ifreq if2;
1204
1205 memset((char *) &if2, 0, sizeof(if2));
1206 strcpy(if2.ifr_name, ifname);
1207
1208 /* Let's make sure the interface does have a broadcast address */
1209 if (ioctl(sock, SIOCGIFFLAGS, (char *)&if2) < 0) {
1210 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGIFFLAGS failed");
1211 return ret;
1212 }
1213
1214 if (if2.ifr_flags & IFF_BROADCAST) {
1241 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGIFNETMASK failed");
1242 return -1;
1243 }
1244
1245 mask = ntohl(((struct sockaddr_in*)&(if2.ifr_addr))->sin_addr.s_addr);
1246 ret = 0;
1247 while (mask) {
1248 mask <<= 1;
1249 ret++;
1250 }
1251
1252 return ret;
1253 }
1254
1255 /**
1256 * Get the Hardware address (usually MAC address) for the named interface.
1257 * return puts the data in buf, and returns the length, in byte, of the
1258 * MAC address. Returns -1 if there is no hardware address on that interface.
1259 */
1260 static int getMacAddress(JNIEnv *env, int sock, const char* ifname, const struct in_addr* addr, unsigned char *buf) {
1261 static struct ifreq ifr;
1262 int i;
1263
1264 strcpy(ifr.ifr_name, ifname);
1265 if (ioctl(sock, SIOCGIFHWADDR, &ifr) < 0) {
1266 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGIFHWADDR failed");
1267 return -1;
1268 }
1269
1270 memcpy(buf, &ifr.ifr_hwaddr.sa_data, IFHWADDRLEN);
1271
1272 /*
1273 * All bytes to 0 means no hardware address.
1274 */
1275
1276 for (i = 0; i < IFHWADDRLEN; i++) {
1277 if (buf[i] != 0)
1278 return IFHWADDRLEN;
1279 }
1280
1281 return -1;
1282 }
1283
1284 static int getMTU(JNIEnv *env, int sock, const char *ifname) {
1285 struct ifreq if2;
1286
1287 memset((char *) &if2, 0, sizeof(if2));
1288 strcpy(if2.ifr_name, ifname);
1289
1290 if (ioctl(sock, SIOCGIFMTU, (char *)&if2) < 0) {
1291 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGIFMTU failed");
1292 return -1;
1293 }
1294
1295 return if2.ifr_mtu;
1296 }
1297
1298 static int getFlags(int sock, const char *ifname, int *flags) {
1299 struct ifreq if2;
1300
1301 memset((char *) &if2, 0, sizeof(if2));
|
1 /*
2 * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <arpa/inet.h>
38 #include <net/if.h>
39 #include <net/if_arp.h>
40
41 #ifdef __solaris__
42 #include <sys/dlpi.h>
43 #include <fcntl.h>
44 #include <stropts.h>
45 #include <sys/sockio.h>
46 #endif
47
48 #ifdef __linux__
49 #include <sys/ioctl.h>
50 #include <bits/ioctls.h>
51 #include <sys/utsname.h>
52 #include <stdio.h>
53 #endif
54
55 #if defined(_AIX)
56 #include <sys/ioctl.h>
57 #include <netinet/in6_var.h>
58 #include <sys/ndd_var.h>
59 #include <sys/kinfo.h>
60 #endif
61
62 #ifdef __linux__
63 #define _PATH_PROCNET_IFINET6 "/proc/net/if_inet6"
64 #endif
65
66 #if defined(_ALLBSD_SOURCE)
67 #include <sys/param.h>
68 #include <sys/ioctl.h>
69 #include <sys/sockio.h>
70 #if defined(__APPLE__)
71 #include <net/ethernet.h>
72 #include <net/if_var.h>
73 #include <net/if_dl.h>
74 #include <netinet/in_var.h>
75 #include <ifaddrs.h>
76 #endif
77 #endif
78
79 #include "jvm.h"
80 #include "jni_util.h"
81 #include "net_util.h"
1031 * proto is AF_INET/AF_INET6
1032 */
1033 static int openSocket(JNIEnv *env, int proto){
1034 int sock;
1035
1036 if ((sock = JVM_Socket(proto, SOCK_DGRAM, 0)) < 0) {
1037 /*
1038 * If EPROTONOSUPPORT is returned it means we don't have
1039 * support for this proto so don't throw an exception.
1040 */
1041 if (errno != EPROTONOSUPPORT) {
1042 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "Socket creation failed");
1043 }
1044 return -1;
1045 }
1046
1047 return sock;
1048 }
1049
1050
1051 /** Linux, AIX **/
1052 #if defined(__linux__) || defined(_AIX)
1053 /* Open socket for further ioct calls, try v4 socket first and
1054 * if it falls return v6 socket
1055 */
1056
1057 #ifdef AF_INET6
1058 static int openSocketWithFallback(JNIEnv *env, const char *ifname){
1059 int sock;
1060 struct ifreq if2;
1061
1062 if ((sock = JVM_Socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1063 if (errno == EPROTONOSUPPORT){
1064 if ( (sock = JVM_Socket(AF_INET6, SOCK_DGRAM, 0)) < 0 ){
1065 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "IPV6 Socket creation failed");
1066 return -1;
1067 }
1068 }
1069 else{ // errno is not NOSUPPORT
1070 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "IPV4 Socket creation failed");
1071 return -1;
1072 }
1073 }
1074
1075 /* Linux starting from 2.6.? kernel allows ioctl call with either IPv4 or IPv6 socket regardless of type
1076 of address of an interface */
1077
1078 return sock;
1079 }
1080
1081 #else
1082 static int openSocketWithFallback(JNIEnv *env, const char *ifname){
1083 return openSocket(env,AF_INET);
1084 }
1085 #endif
1086
1087 static netif *enumIPv4Interfaces(JNIEnv *env, int sock, netif *ifs) {
1088 struct ifconf ifc;
1089 struct ifreq *ifreqP;
1090 char *buf = NULL;
1091 int numifs;
1092 unsigned i;
1093 int siocgifconfRequest = SIOCGIFCONF;
1094
1095
1096 #if defined(__linux__)
1097 /* need to do a dummy SIOCGIFCONF to determine the buffer size.
1098 * SIOCGIFCOUNT doesn't work
1099 */
1100 ifc.ifc_buf = NULL;
1101 if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
1102 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "ioctl SIOCGIFCONF failed");
1103 return ifs;
1104 }
1105 #elif defined(_AIX)
1106 ifc.ifc_buf = NULL;
1107 if (ioctl(sock, SIOCGSIZIFCONF, &(ifc.ifc_len)) < 0) {
1108 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "ioctl SIOCGSIZIFCONF failed");
1109 return ifs;
1110 }
1111 #endif /* __linux__ */
1112
1113 CHECKED_MALLOC3(buf,char *, ifc.ifc_len);
1114
1115 ifc.ifc_buf = buf;
1116 #if defined(_AIX)
1117 siocgifconfRequest = CSIOCGIFCONF;
1118 #endif
1119 if (ioctl(sock, siocgifconfRequest, (char *)&ifc) < 0) {
1120 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "ioctl SIOCGIFCONF failed");
1121 (void) free(buf);
1122 return ifs;
1123 }
1124
1125 /*
1126 * Iterate through each interface
1127 */
1128 ifreqP = ifc.ifc_req;
1129 for (i=0; i<ifc.ifc_len/sizeof (struct ifreq); i++, ifreqP++) {
1130 #if defined(_AIX)
1131 if (ifreqP->ifr_addr.sa_family != AF_INET) continue;
1132 #endif
1133 /*
1134 * Add to the list
1135 */
1136 ifs = addif(env, sock, ifreqP->ifr_name, ifs, (struct sockaddr *) & (ifreqP->ifr_addr), AF_INET, 0);
1137
1138 /*
1139 * If an exception occurred then free the list
1140 */
1141 if ((*env)->ExceptionOccurred(env)) {
1142 free(buf);
1143 freeif(ifs);
1144 return NULL;
1145 }
1146 }
1147
1148 /*
1149 * Free socket and buffer
1150 */
1151 free(buf);
1152 return ifs;
1153 }
1154
1155
1156 /*
1157 * Enumerates and returns all IPv6 interfaces on Linux
1158 */
1159
1160 #if defined(AF_INET6) && defined(__linux__)
1161 static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs) {
1162 FILE *f;
1163 char addr6[40], devname[21];
1164 char addr6p[8][5];
1165 int plen, scope, dad_status, if_idx;
1166 uint8_t ipv6addr[16];
1167
1168 if ((f = fopen(_PATH_PROCNET_IFINET6, "r")) != NULL) {
1169 while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %08x %02x %02x %02x %20s\n",
1170 addr6p[0], addr6p[1], addr6p[2], addr6p[3], addr6p[4], addr6p[5], addr6p[6], addr6p[7],
1171 &if_idx, &plen, &scope, &dad_status, devname) != EOF) {
1172
1173 struct netif *ifs_ptr = NULL;
1174 struct netif *last_ptr = NULL;
1175 struct sockaddr_in6 addr;
1176
1177 sprintf(addr6, "%s:%s:%s:%s:%s:%s:%s:%s",
1178 addr6p[0], addr6p[1], addr6p[2], addr6p[3], addr6p[4], addr6p[5], addr6p[6], addr6p[7]);
1179 inet_pton(AF_INET6, addr6, ipv6addr);
1180
1184 addr.sin6_scope_id = if_idx;
1185
1186 ifs = addif(env, sock, devname, ifs, (struct sockaddr *)&addr, AF_INET6, plen);
1187
1188
1189 /*
1190 * If an exception occurred then return the list as is.
1191 */
1192 if ((*env)->ExceptionOccurred(env)) {
1193 fclose(f);
1194 return ifs;
1195 }
1196 }
1197 fclose(f);
1198 }
1199 return ifs;
1200 }
1201 #endif
1202
1203
1204 /*
1205 * Enumerates and returns all IPv6 interfaces on AIX
1206 */
1207
1208 #if defined(AF_INET6) && defined(_AIX)
1209 static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs) {
1210 struct ifconf ifc;
1211 struct ifreq *ifreqP;
1212 char *buf;
1213 int numifs;
1214 unsigned i;
1215 unsigned bufsize;
1216 char *cp, *cplimit;
1217
1218 /* use SIOCGSIZIFCONF to get size for SIOCGIFCONF */
1219
1220 ifc.ifc_buf = NULL;
1221 if (ioctl(sock, SIOCGSIZIFCONF, &(ifc.ifc_len)) < 0) {
1222 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException",
1223 "ioctl SIOCGSIZIFCONF failed");
1224 return ifs;
1225 }
1226 bufsize = ifc.ifc_len;
1227
1228 buf = (char *)malloc(bufsize);
1229 if (!buf) {
1230 JNU_ThrowOutOfMemoryError(env, "Network interface native buffer allocation failed");
1231 return ifs;
1232 }
1233 ifc.ifc_len = bufsize;
1234 ifc.ifc_buf = buf;
1235 if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
1236 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException",
1237 "ioctl CSIOCGIFCONF failed");
1238 free(buf);
1239 return ifs;
1240 }
1241
1242 /*
1243 * Iterate through each interface
1244 */
1245 ifreqP = ifc.ifc_req;
1246 cp = (char *)ifc.ifc_req;
1247 cplimit = cp + ifc.ifc_len;
1248
1249 for ( ; cp < cplimit; cp += (sizeof(ifreqP->ifr_name) + MAX((ifreqP->ifr_addr).sa_len, sizeof(ifreqP->ifr_addr)))) {
1250 ifreqP = (struct ifreq *)cp;
1251 struct ifreq if2;
1252
1253 memset((char *)&if2, 0, sizeof(if2));
1254 strcpy(if2.ifr_name, ifreqP->ifr_name);
1255
1256 /*
1257 * Skip interface that aren't UP
1258 */
1259 if (ioctl(sock, SIOCGIFFLAGS, (char *)&if2) >= 0) {
1260 if (!(if2.ifr_flags & IFF_UP)) {
1261 continue;
1262 }
1263 }
1264
1265 if (ifreqP->ifr_addr.sa_family != AF_INET6)
1266 continue;
1267
1268 /*
1269 * Add to the list
1270 */
1271 ifs = addif(env, sock, ifreqP->ifr_name, ifs,
1272 (struct sockaddr *)&(ifreqP->ifr_addr),
1273 AF_INET6, 0);
1274
1275 /*
1276 * If an exception occurred then free the list
1277 */
1278 if ((*env)->ExceptionOccurred(env)) {
1279 free(buf);
1280 freeif(ifs);
1281 return NULL;
1282 }
1283 }
1284
1285 /*
1286 * Free socket and buffer
1287 */
1288 free(buf);
1289 return ifs;
1290 }
1291 #endif
1292
1293
1294 static int getIndex(int sock, const char *name){
1295 /*
1296 * Try to get the interface index
1297 */
1298 #if defined(_AIX)
1299 return if_nametoindex(name);
1300 #else
1301 struct ifreq if2;
1302 strcpy(if2.ifr_name, name);
1303
1304 if (ioctl(sock, SIOCGIFINDEX, (char *)&if2) < 0) {
1305 return -1;
1306 }
1307
1308 return if2.ifr_ifindex;
1309 #endif
1310 }
1311
1312 /**
1313 * Returns the IPv4 broadcast address of a named interface, if it exists.
1314 * Returns 0 if it doesn't have one.
1315 */
1316 static struct sockaddr *getBroadcast(JNIEnv *env, int sock, const char *ifname, struct sockaddr *brdcast_store) {
1317 struct sockaddr *ret = NULL;
1318 struct ifreq if2;
1319
1320 memset((char *) &if2, 0, sizeof(if2));
1321 strcpy(if2.ifr_name, ifname);
1322
1323 /* Let's make sure the interface does have a broadcast address */
1324 if (ioctl(sock, SIOCGIFFLAGS, (char *)&if2) < 0) {
1325 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGIFFLAGS failed");
1326 return ret;
1327 }
1328
1329 if (if2.ifr_flags & IFF_BROADCAST) {
1356 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGIFNETMASK failed");
1357 return -1;
1358 }
1359
1360 mask = ntohl(((struct sockaddr_in*)&(if2.ifr_addr))->sin_addr.s_addr);
1361 ret = 0;
1362 while (mask) {
1363 mask <<= 1;
1364 ret++;
1365 }
1366
1367 return ret;
1368 }
1369
1370 /**
1371 * Get the Hardware address (usually MAC address) for the named interface.
1372 * return puts the data in buf, and returns the length, in byte, of the
1373 * MAC address. Returns -1 if there is no hardware address on that interface.
1374 */
1375 static int getMacAddress(JNIEnv *env, int sock, const char* ifname, const struct in_addr* addr, unsigned char *buf) {
1376 #if defined (_AIX)
1377 int size;
1378 struct kinfo_ndd *nddp;
1379 void *end;
1380
1381 size = getkerninfo(KINFO_NDD, 0, 0, 0);
1382 if (size == 0) {
1383 return -1;
1384 }
1385
1386 if (size < 0) {
1387 perror("getkerninfo 1");
1388 return -1;
1389 }
1390
1391 nddp = (struct kinfo_ndd *)malloc(size);
1392
1393 if (!nddp) {
1394 return -1;
1395 }
1396
1397 if (getkerninfo(KINFO_NDD, nddp, &size, 0) < 0) {
1398 perror("getkerninfo 2");
1399 return -1;
1400 }
1401
1402 end = (void *)nddp + size;
1403 while ((void *)nddp < end) {
1404 if (!strcmp(nddp->ndd_alias, ifname) ||
1405 !strcmp(nddp->ndd_name, ifname)) {
1406 bcopy(nddp->ndd_addr, buf, 6);
1407 return 6;
1408 } else {
1409 nddp++;
1410 }
1411 }
1412
1413 return -1;
1414
1415 #elif defined(__linux__)
1416 static struct ifreq ifr;
1417 int i;
1418
1419 strcpy(ifr.ifr_name, ifname);
1420 if (ioctl(sock, SIOCGIFHWADDR, &ifr) < 0) {
1421 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGIFHWADDR failed");
1422 return -1;
1423 }
1424
1425 memcpy(buf, &ifr.ifr_hwaddr.sa_data, IFHWADDRLEN);
1426
1427 /*
1428 * All bytes to 0 means no hardware address.
1429 */
1430
1431 for (i = 0; i < IFHWADDRLEN; i++) {
1432 if (buf[i] != 0)
1433 return IFHWADDRLEN;
1434 }
1435
1436 return -1;
1437 #endif
1438 }
1439
1440 static int getMTU(JNIEnv *env, int sock, const char *ifname) {
1441 struct ifreq if2;
1442
1443 memset((char *) &if2, 0, sizeof(if2));
1444 strcpy(if2.ifr_name, ifname);
1445
1446 if (ioctl(sock, SIOCGIFMTU, (char *)&if2) < 0) {
1447 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGIFMTU failed");
1448 return -1;
1449 }
1450
1451 return if2.ifr_mtu;
1452 }
1453
1454 static int getFlags(int sock, const char *ifname, int *flags) {
1455 struct ifreq if2;
1456
1457 memset((char *) &if2, 0, sizeof(if2));
|