1376 memset((char *)&if2, 0, sizeof(if2));
1377 strncpy(if2.ifr_name, ifname, sizeof(if2.ifr_name) - 1);
1378
1379 if (ioctl(sock, SIOCGIFFLAGS, (char *)&if2) < 0) {
1380 return -1;
1381 }
1382
1383 if (sizeof(if2.ifr_flags) == sizeof(short)) {
1384 *flags = (if2.ifr_flags & 0xffff);
1385 } else {
1386 *flags = if2.ifr_flags;
1387 }
1388 return 0;
1389 }
1390
1391 #endif /* __linux__ */
1392
1393 /** AIX **/
1394 #if defined(_AIX)
1395
1396 /*
1397 * Opens a socket for further ioctl calls. Tries AF_INET socket first and
1398 * if it fails return AF_INET6 socket.
1399 */
1400 static int openSocketWithFallback(JNIEnv *env, const char *ifname) {
1401 int sock;
1402
1403 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1404 if (errno == EPROTONOSUPPORT || errno == EAFNOSUPPORT) {
1405 if ((sock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1406 JNU_ThrowByNameWithMessageAndLastError
1407 (env, JNU_JAVANETPKG "SocketException", "IPV6 Socket creation failed");
1408 return -1;
1409 }
1410 } else { // errno is not NOSUPPORT
1411 JNU_ThrowByNameWithMessageAndLastError
1412 (env, JNU_JAVANETPKG "SocketException", "IPV4 Socket creation failed");
1413 return -1;
1414 }
1415 }
1595 void *end;
1596
1597 size = getkerninfo(KINFO_NDD, 0, 0, 0);
1598 if (size == 0) {
1599 return -1;
1600 }
1601
1602 if (size < 0) {
1603 perror("getkerninfo 1");
1604 return -1;
1605 }
1606
1607 nddp = (struct kinfo_ndd *)malloc(size);
1608
1609 if (!nddp) {
1610 JNU_ThrowOutOfMemoryError(env,
1611 "Network interface getMacAddress native buffer allocation failed");
1612 return -1;
1613 }
1614
1615 if (getkerninfo(KINFO_NDD, nddp, &size, 0) < 0) {
1616 perror("getkerninfo 2");
1617 free(nddp);
1618 return -1;
1619 }
1620
1621 end = (void *)nddp + size;
1622 while ((void *)nddp < end) {
1623 if (!strcmp(nddp->ndd_alias, ifname) ||
1624 !strcmp(nddp->ndd_name, ifname)) {
1625 bcopy(nddp->ndd_addr, buf, 6);
1626 free(nddp);
1627 return 6;
1628 } else {
1629 nddp++;
1630 }
1631 }
1632
1633 free(nddp);
1634 return -1;
1635 }
|
1376 memset((char *)&if2, 0, sizeof(if2));
1377 strncpy(if2.ifr_name, ifname, sizeof(if2.ifr_name) - 1);
1378
1379 if (ioctl(sock, SIOCGIFFLAGS, (char *)&if2) < 0) {
1380 return -1;
1381 }
1382
1383 if (sizeof(if2.ifr_flags) == sizeof(short)) {
1384 *flags = (if2.ifr_flags & 0xffff);
1385 } else {
1386 *flags = if2.ifr_flags;
1387 }
1388 return 0;
1389 }
1390
1391 #endif /* __linux__ */
1392
1393 /** AIX **/
1394 #if defined(_AIX)
1395
1396 /* seems getkerninfo is guarded by _KERNEL in the system headers */
1397 /* see net/proto_uipc.h */
1398 int getkerninfo(int, char *, int *, int32long64_t);
1399
1400 /*
1401 * Opens a socket for further ioctl calls. Tries AF_INET socket first and
1402 * if it fails return AF_INET6 socket.
1403 */
1404 static int openSocketWithFallback(JNIEnv *env, const char *ifname) {
1405 int sock;
1406
1407 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1408 if (errno == EPROTONOSUPPORT || errno == EAFNOSUPPORT) {
1409 if ((sock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1410 JNU_ThrowByNameWithMessageAndLastError
1411 (env, JNU_JAVANETPKG "SocketException", "IPV6 Socket creation failed");
1412 return -1;
1413 }
1414 } else { // errno is not NOSUPPORT
1415 JNU_ThrowByNameWithMessageAndLastError
1416 (env, JNU_JAVANETPKG "SocketException", "IPV4 Socket creation failed");
1417 return -1;
1418 }
1419 }
1599 void *end;
1600
1601 size = getkerninfo(KINFO_NDD, 0, 0, 0);
1602 if (size == 0) {
1603 return -1;
1604 }
1605
1606 if (size < 0) {
1607 perror("getkerninfo 1");
1608 return -1;
1609 }
1610
1611 nddp = (struct kinfo_ndd *)malloc(size);
1612
1613 if (!nddp) {
1614 JNU_ThrowOutOfMemoryError(env,
1615 "Network interface getMacAddress native buffer allocation failed");
1616 return -1;
1617 }
1618
1619 if (getkerninfo(KINFO_NDD, (char*) nddp, &size, 0) < 0) {
1620 perror("getkerninfo 2");
1621 free(nddp);
1622 return -1;
1623 }
1624
1625 end = (void *)nddp + size;
1626 while ((void *)nddp < end) {
1627 if (!strcmp(nddp->ndd_alias, ifname) ||
1628 !strcmp(nddp->ndd_name, ifname)) {
1629 bcopy(nddp->ndd_addr, buf, 6);
1630 free(nddp);
1631 return 6;
1632 } else {
1633 nddp++;
1634 }
1635 }
1636
1637 free(nddp);
1638 return -1;
1639 }
|