< prev index next >

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

Print this page




1227     struct ifreq if2;
1228     memset((char *)&if2, 0, sizeof(if2));
1229     strncpy(if2.ifr_name, name, sizeof(if2.ifr_name) - 1);
1230 
1231     if (ioctl(sock, SIOCGIFINDEX, (char *)&if2) < 0) {
1232         return -1;
1233     }
1234 
1235     return if2.ifr_ifindex;
1236 }
1237 
1238 /*
1239  * Gets the Hardware address (usually MAC address) for the named interface.
1240  * On return puts the data in buf, and returns the length, in byte, of the
1241  * MAC address. Returns -1 if there is no hardware address on that interface.
1242  */
1243 static int getMacAddress
1244   (JNIEnv *env, const char *ifname, const struct in_addr *addr,
1245    unsigned char *buf)
1246 {
1247     static struct ifreq ifr;
1248     int i, sock;
1249 
1250     if ((sock = openSocketWithFallback(env, ifname)) < 0) {
1251         return -1;
1252     }
1253 
1254     memset((char *)&ifr, 0, sizeof(ifr));
1255     strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
1256     if (ioctl(sock, SIOCGIFHWADDR, &ifr) < 0) {
1257         JNU_ThrowByNameWithMessageAndLastError
1258             (env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGIFHWADDR) failed");
1259         close(sock);
1260         return -1;
1261     }
1262 
1263     close(sock);
1264     memcpy(buf, &ifr.ifr_hwaddr.sa_data, IFHWADDRLEN);
1265 
1266     // all bytes to 0 means no hardware address
1267     for (i = 0; i < IFHWADDRLEN; i++) {




1227     struct ifreq if2;
1228     memset((char *)&if2, 0, sizeof(if2));
1229     strncpy(if2.ifr_name, name, sizeof(if2.ifr_name) - 1);
1230 
1231     if (ioctl(sock, SIOCGIFINDEX, (char *)&if2) < 0) {
1232         return -1;
1233     }
1234 
1235     return if2.ifr_ifindex;
1236 }
1237 
1238 /*
1239  * Gets the Hardware address (usually MAC address) for the named interface.
1240  * On return puts the data in buf, and returns the length, in byte, of the
1241  * MAC address. Returns -1 if there is no hardware address on that interface.
1242  */
1243 static int getMacAddress
1244   (JNIEnv *env, const char *ifname, const struct in_addr *addr,
1245    unsigned char *buf)
1246 {
1247     struct ifreq ifr;
1248     int i, sock;
1249 
1250     if ((sock = openSocketWithFallback(env, ifname)) < 0) {
1251         return -1;
1252     }
1253 
1254     memset((char *)&ifr, 0, sizeof(ifr));
1255     strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
1256     if (ioctl(sock, SIOCGIFHWADDR, &ifr) < 0) {
1257         JNU_ThrowByNameWithMessageAndLastError
1258             (env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGIFHWADDR) failed");
1259         close(sock);
1260         return -1;
1261     }
1262 
1263     close(sock);
1264     memcpy(buf, &ifr.ifr_hwaddr.sa_data, IFHWADDRLEN);
1265 
1266     // all bytes to 0 means no hardware address
1267     for (i = 0; i < IFHWADDRLEN; i++) {


< prev index next >