1 /*
   2  * Copyright (c) 2000, 2016, 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
  23  * questions.
  24  */
  25 
  26 #include <errno.h>
  27 #include <strings.h>
  28 #if defined(_ALLBSD_SOURCE) && defined(__OpenBSD__)
  29 #include <sys/types.h>
  30 #endif
  31 #include <netinet/in.h>
  32 #include <stdlib.h>
  33 #include <string.h>
  34 #include <sys/types.h>
  35 #include <sys/socket.h>
  36 #include <arpa/inet.h>
  37 #include <net/if.h>
  38 #include <net/if_arp.h>
  39 
  40 #if defined(__solaris__)
  41 #include <sys/dlpi.h>
  42 #include <fcntl.h>
  43 #include <stropts.h>
  44 #include <sys/sockio.h>
  45 #endif
  46 
  47 #if defined(__linux__)
  48 #include <sys/ioctl.h>
  49 #include <bits/ioctls.h>
  50 #include <sys/utsname.h>
  51 #include <stdio.h>
  52 #endif
  53 
  54 #if defined(_AIX)
  55 #include <sys/ioctl.h>
  56 #include <netinet/in6_var.h>
  57 #include <sys/ndd_var.h>
  58 #include <sys/kinfo.h>
  59 #endif
  60 
  61 #if defined(_ALLBSD_SOURCE)
  62 #include <sys/param.h>
  63 #include <sys/ioctl.h>
  64 #include <sys/sockio.h>
  65 #if defined(__APPLE__)
  66 #include <net/ethernet.h>
  67 #include <net/if_var.h>
  68 #include <net/if_dl.h>
  69 #include <netinet/in_var.h>
  70 #include <ifaddrs.h>
  71 #endif
  72 #endif
  73 
  74 #include "jvm.h"
  75 #include "jni_util.h"
  76 #include "net_util.h"
  77 
  78 #if defined(__linux__)
  79 #define _PATH_PROCNET_IFINET6 "/proc/net/if_inet6"
  80 #endif
  81 
  82 typedef struct _netaddr  {
  83     struct sockaddr *addr;
  84     struct sockaddr *brdcast;
  85     short mask;
  86     int family; /* to make searches simple */
  87     struct _netaddr *next;
  88 } netaddr;
  89 
  90 typedef struct _netif {
  91     char *name;
  92     int index;
  93     char virtual;
  94     netaddr *addr;
  95     struct _netif *childs;
  96     struct _netif *next;
  97 } netif;
  98 
  99 /************************************************************************
 100  * NetworkInterface
 101  */
 102 
 103 #include "java_net_NetworkInterface.h"
 104 
 105 /************************************************************************
 106  * NetworkInterface
 107  */
 108 jclass ni_class;
 109 jfieldID ni_nameID;
 110 jfieldID ni_indexID;
 111 jfieldID ni_descID;
 112 jfieldID ni_addrsID;
 113 jfieldID ni_bindsID;
 114 jfieldID ni_virutalID;
 115 jfieldID ni_childsID;
 116 jfieldID ni_parentID;
 117 jfieldID ni_defaultIndexID;
 118 jmethodID ni_ctrID;
 119 
 120 static jclass ni_ibcls;
 121 static jmethodID ni_ibctrID;
 122 static jfieldID ni_ibaddressID;
 123 static jfieldID ni_ib4broadcastID;
 124 static jfieldID ni_ib4maskID;
 125 
 126 /** Private methods declarations **/
 127 static jobject createNetworkInterface(JNIEnv *env, netif *ifs);
 128 static int     getFlags0(JNIEnv *env, jstring  ifname);
 129 
 130 static netif  *enumInterfaces(JNIEnv *env);
 131 static netif  *enumIPv4Interfaces(JNIEnv *env, int sock, netif *ifs);
 132 
 133 #ifdef AF_INET6
 134 static netif  *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs);
 135 #endif
 136 
 137 static netif  *addif(JNIEnv *env, int sock, const char *if_name, netif *ifs,
 138                      struct sockaddr *ifr_addrP, int family, short prefix);
 139 static void    freeif(netif *ifs);
 140 
 141 static int     openSocket(JNIEnv *env, int proto);
 142 static int     openSocketWithFallback(JNIEnv *env, const char *ifname);
 143 
 144 
 145 static struct  sockaddr *getBroadcast(JNIEnv *env, int sock, const char *name,
 146                                       struct sockaddr *brdcast_store);
 147 static short   getSubnet(JNIEnv *env, int sock, const char *ifname);
 148 static int     getIndex(int sock, const char *ifname);
 149 
 150 static int     getFlags(int sock, const char *ifname, int *flags);
 151 static int     getMacAddress(JNIEnv *env, int sock,  const char *ifname,
 152                              const struct in_addr *addr, unsigned char *buf);
 153 static int     getMTU(JNIEnv *env, int sock, const char *ifname);
 154 
 155 
 156 #if defined(__solaris__)
 157 static netif  *enumIPvXInterfaces(JNIEnv *env, int sock, netif *ifs, int family);
 158 static int     getMacFromDevice(JNIEnv *env, const char *ifname,
 159                                 unsigned char *retbuf);
 160 
 161 #ifndef SIOCGLIFHWADDR
 162 #define SIOCGLIFHWADDR _IOWR('i', 192, struct lifreq)
 163 #endif
 164 
 165 #endif
 166 
 167 /******************* Java entry points *****************************/
 168 
 169 /*
 170  * Class:     java_net_NetworkInterface
 171  * Method:    init
 172  * Signature: ()V
 173  */
 174 JNIEXPORT void JNICALL Java_java_net_NetworkInterface_init
 175   (JNIEnv *env, jclass cls)
 176 {
 177     ni_class = (*env)->FindClass(env, "java/net/NetworkInterface");
 178     CHECK_NULL(ni_class);
 179     ni_class = (*env)->NewGlobalRef(env, ni_class);
 180     CHECK_NULL(ni_class);
 181     ni_nameID = (*env)->GetFieldID(env, ni_class, "name", "Ljava/lang/String;");
 182     CHECK_NULL(ni_nameID);
 183     ni_indexID = (*env)->GetFieldID(env, ni_class, "index", "I");
 184     CHECK_NULL(ni_indexID);
 185     ni_addrsID = (*env)->GetFieldID(env, ni_class, "addrs",
 186                                     "[Ljava/net/InetAddress;");
 187     CHECK_NULL(ni_addrsID);
 188     ni_bindsID = (*env)->GetFieldID(env, ni_class, "bindings",
 189                                     "[Ljava/net/InterfaceAddress;");
 190     CHECK_NULL(ni_bindsID);
 191     ni_descID = (*env)->GetFieldID(env, ni_class, "displayName",
 192                                    "Ljava/lang/String;");
 193     CHECK_NULL(ni_descID);
 194     ni_virutalID = (*env)->GetFieldID(env, ni_class, "virtual", "Z");
 195     CHECK_NULL(ni_virutalID);
 196     ni_childsID = (*env)->GetFieldID(env, ni_class, "childs",
 197                                      "[Ljava/net/NetworkInterface;");
 198     CHECK_NULL(ni_childsID);
 199     ni_parentID = (*env)->GetFieldID(env, ni_class, "parent",
 200                                      "Ljava/net/NetworkInterface;");
 201     CHECK_NULL(ni_parentID);
 202     ni_ctrID = (*env)->GetMethodID(env, ni_class, "<init>", "()V");
 203     CHECK_NULL(ni_ctrID);
 204     ni_ibcls = (*env)->FindClass(env, "java/net/InterfaceAddress");
 205     CHECK_NULL(ni_ibcls);
 206     ni_ibcls = (*env)->NewGlobalRef(env, ni_ibcls);
 207     CHECK_NULL(ni_ibcls);
 208     ni_ibctrID = (*env)->GetMethodID(env, ni_ibcls, "<init>", "()V");
 209     CHECK_NULL(ni_ibctrID);
 210     ni_ibaddressID = (*env)->GetFieldID(env, ni_ibcls, "address",
 211                                         "Ljava/net/InetAddress;");
 212     CHECK_NULL(ni_ibaddressID);
 213     ni_ib4broadcastID = (*env)->GetFieldID(env, ni_ibcls, "broadcast",
 214                                            "Ljava/net/Inet4Address;");
 215     CHECK_NULL(ni_ib4broadcastID);
 216     ni_ib4maskID = (*env)->GetFieldID(env, ni_ibcls, "maskLength", "S");
 217     CHECK_NULL(ni_ib4maskID);
 218     ni_defaultIndexID = (*env)->GetStaticFieldID(env, ni_class, "defaultIndex",
 219                                                  "I");
 220     CHECK_NULL(ni_defaultIndexID);
 221     initInetAddressIDs(env);
 222 }
 223 
 224 /*
 225  * Class:     java_net_NetworkInterface
 226  * Method:    getByName0
 227  * Signature: (Ljava/lang/String;)Ljava/net/NetworkInterface;
 228  */
 229 JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByName0
 230   (JNIEnv *env, jclass cls, jstring name)
 231 {
 232     netif *ifs, *curr;
 233     jboolean isCopy;
 234     const char* name_utf;
 235     jobject obj = NULL;
 236 
 237     ifs = enumInterfaces(env);
 238     if (ifs == NULL) {
 239         return NULL;
 240     }
 241 
 242     name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
 243     if (name_utf == NULL) {
 244        if (!(*env)->ExceptionCheck(env))
 245            JNU_ThrowOutOfMemoryError(env, NULL);
 246        return NULL;
 247     }
 248 
 249     // Search the list of interface based on name
 250     curr = ifs;
 251     while (curr != NULL) {
 252         if (strcmp(name_utf, curr->name) == 0) {
 253             break;
 254         }
 255         curr = curr->next;
 256     }
 257 
 258     // if found create a NetworkInterface
 259     if (curr != NULL) {;
 260         obj = createNetworkInterface(env, curr);
 261     }
 262 
 263     // release the UTF string and interface list
 264     (*env)->ReleaseStringUTFChars(env, name, name_utf);
 265     freeif(ifs);
 266 
 267     return obj;
 268 }
 269 
 270 /*
 271  * Class:     java_net_NetworkInterface
 272  * Method:    getByIndex0
 273  * Signature: (Ljava/lang/String;)Ljava/net/NetworkInterface;
 274  */
 275 JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByIndex0
 276   (JNIEnv *env, jclass cls, jint index)
 277 {
 278     netif *ifs, *curr;
 279     jobject obj = NULL;
 280 
 281     if (index <= 0) {
 282         return NULL;
 283     }
 284     ifs = enumInterfaces(env);
 285     if (ifs == NULL) {
 286         return NULL;
 287     }
 288 
 289     // Search the list of interface based on index
 290     curr = ifs;
 291     while (curr != NULL) {
 292         if (index == curr->index) {
 293             break;
 294         }
 295         curr = curr->next;
 296     }
 297 
 298     // if found create a NetworkInterface
 299     if (curr != NULL) {;
 300         obj = createNetworkInterface(env, curr);
 301     }
 302 
 303     freeif(ifs);
 304     return obj;
 305 }
 306 
 307 /*
 308  * Class:     java_net_NetworkInterface
 309  * Method:    getByInetAddress0
 310  * Signature: (Ljava/net/InetAddress;)Ljava/net/NetworkInterface;
 311  */
 312 JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByInetAddress0
 313   (JNIEnv *env, jclass cls, jobject iaObj)
 314 {
 315     netif *ifs, *curr;
 316 
 317 #ifdef AF_INET6
 318     int family = (getInetAddress_family(env, iaObj) == IPv4) ? AF_INET : AF_INET6;
 319 #else
 320     int family =  AF_INET;
 321 #endif
 322 
 323     jobject obj = NULL;
 324     jboolean match = JNI_FALSE;
 325 
 326     ifs = enumInterfaces(env);
 327     if (ifs == NULL) {
 328         return NULL;
 329     }
 330 
 331     curr = ifs;
 332     while (curr != NULL) {
 333         netaddr *addrP = curr->addr;
 334 
 335         // Iterate through each address on the interface
 336         while (addrP != NULL) {
 337 
 338             if (family == addrP->family) {
 339                 if (family == AF_INET) {
 340                     int address1 = htonl(
 341                         ((struct sockaddr_in*)addrP->addr)->sin_addr.s_addr);
 342                     int address2 = getInetAddress_addr(env, iaObj);
 343 
 344                     if (address1 == address2) {
 345                         match = JNI_TRUE;
 346                         break;
 347                     }
 348                 }
 349 
 350 #ifdef AF_INET6
 351                 if (family == AF_INET6) {
 352                     jbyte *bytes = (jbyte *)&(
 353                         ((struct sockaddr_in6*)addrP->addr)->sin6_addr);
 354                     jbyte caddr[16];
 355                     int i;
 356                     getInet6Address_ipaddress(env, iaObj, (char *)caddr);
 357                     i = 0;
 358                     while (i < 16) {
 359                         if (caddr[i] != bytes[i]) {
 360                             break;
 361                         }
 362                         i++;
 363                     }
 364                     if (i >= 16) {
 365                         match = JNI_TRUE;
 366                         break;
 367                     }
 368                 }
 369 #endif
 370             }
 371 
 372             if (match) {
 373                 break;
 374             }
 375             addrP = addrP->next;
 376         }
 377 
 378         if (match) {
 379             break;
 380         }
 381         curr = curr->next;
 382     }
 383 
 384     // if found create a NetworkInterface
 385     if (match) {;
 386         obj = createNetworkInterface(env, curr);
 387     }
 388 
 389     freeif(ifs);
 390     return obj;
 391 }
 392 
 393 /*
 394  * Class:     java_net_NetworkInterface
 395  * Method:    getAll
 396  * Signature: ()[Ljava/net/NetworkInterface;
 397  */
 398 JNIEXPORT jobjectArray JNICALL Java_java_net_NetworkInterface_getAll
 399   (JNIEnv *env, jclass cls)
 400 {
 401     netif *ifs, *curr;
 402     jobjectArray netIFArr;
 403     jint arr_index, ifCount;
 404 
 405     ifs = enumInterfaces(env);
 406     if (ifs == NULL) {
 407         return NULL;
 408     }
 409 
 410     // count the interface
 411     ifCount = 0;
 412     curr = ifs;
 413     while (curr != NULL) {
 414         ifCount++;
 415         curr = curr->next;
 416     }
 417 
 418     // allocate a NetworkInterface array
 419     netIFArr = (*env)->NewObjectArray(env, ifCount, cls, NULL);
 420     if (netIFArr == NULL) {
 421         freeif(ifs);
 422         return NULL;
 423     }
 424 
 425     // Iterate through the interfaces, create a NetworkInterface instance
 426     // for each array element and populate the object.
 427     curr = ifs;
 428     arr_index = 0;
 429     while (curr != NULL) {
 430         jobject netifObj;
 431 
 432         netifObj = createNetworkInterface(env, curr);
 433         if (netifObj == NULL) {
 434             freeif(ifs);
 435             return NULL;
 436         }
 437 
 438         // put the NetworkInterface into the array
 439         (*env)->SetObjectArrayElement(env, netIFArr, arr_index++, netifObj);
 440 
 441         curr = curr->next;
 442     }
 443 
 444     freeif(ifs);
 445     return netIFArr;
 446 }
 447 
 448 /*
 449  * Class:     java_net_NetworkInterface
 450  * Method:    isUp0
 451  * Signature: (Ljava/lang/String;I)Z
 452  */
 453 JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isUp0
 454   (JNIEnv *env, jclass cls, jstring name, jint index)
 455 {
 456     int ret = getFlags0(env, name);
 457     return ((ret & IFF_UP) && (ret & IFF_RUNNING)) ? JNI_TRUE :  JNI_FALSE;
 458 }
 459 
 460 /*
 461  * Class:     java_net_NetworkInterface
 462  * Method:    isP2P0
 463  * Signature: (Ljava/lang/String;I)Z
 464  */
 465 JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isP2P0
 466   (JNIEnv *env, jclass cls, jstring name, jint index)
 467 {
 468     int ret = getFlags0(env, name);
 469     return (ret & IFF_POINTOPOINT) ? JNI_TRUE :  JNI_FALSE;
 470 }
 471 
 472 /*
 473  * Class:     java_net_NetworkInterface
 474  * Method:    isLoopback0
 475  * Signature: (Ljava/lang/String;I)Z
 476  */
 477 JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isLoopback0
 478   (JNIEnv *env, jclass cls, jstring name, jint index)
 479 {
 480     int ret = getFlags0(env, name);
 481     return (ret & IFF_LOOPBACK) ? JNI_TRUE :  JNI_FALSE;
 482 }
 483 
 484 /*
 485  * Class:     java_net_NetworkInterface
 486  * Method:    supportsMulticast0
 487  * Signature: (Ljava/lang/String;I)Z
 488  */
 489 JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_supportsMulticast0
 490   (JNIEnv *env, jclass cls, jstring name, jint index)
 491 {
 492     int ret = getFlags0(env, name);
 493     return (ret & IFF_MULTICAST) ? JNI_TRUE :  JNI_FALSE;
 494 }
 495 
 496 /*
 497  * Class:     java_net_NetworkInterface
 498  * Method:    getMacAddr0
 499  * Signature: ([bLjava/lang/String;I)[b
 500  */
 501 JNIEXPORT jbyteArray JNICALL Java_java_net_NetworkInterface_getMacAddr0
 502   (JNIEnv *env, jclass cls, jbyteArray addrArray, jstring name, jint index)
 503 {
 504     jint addr;
 505     jbyte caddr[4];
 506     struct in_addr iaddr;
 507     jbyteArray ret = NULL;
 508     unsigned char mac[16];
 509     int len;
 510     int sock;
 511     jboolean isCopy;
 512     const char* name_utf;
 513 
 514     name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
 515     if (name_utf == NULL) {
 516        if (!(*env)->ExceptionCheck(env))
 517            JNU_ThrowOutOfMemoryError(env, NULL);
 518        return NULL;
 519     }
 520     if ((sock = openSocketWithFallback(env, name_utf)) < 0) {
 521        (*env)->ReleaseStringUTFChars(env, name, name_utf);
 522        return JNI_FALSE;
 523     }
 524 
 525     if (!IS_NULL(addrArray)) {
 526        (*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr);
 527        addr = ((caddr[0]<<24) & 0xff000000);
 528        addr |= ((caddr[1] <<16) & 0xff0000);
 529        addr |= ((caddr[2] <<8) & 0xff00);
 530        addr |= (caddr[3] & 0xff);
 531        iaddr.s_addr = htonl(addr);
 532        len = getMacAddress(env, sock, name_utf, &iaddr, mac);
 533     } else {
 534        len = getMacAddress(env, sock, name_utf, NULL, mac);
 535     }
 536     if (len > 0) {
 537        ret = (*env)->NewByteArray(env, len);
 538        if (IS_NULL(ret)) {
 539           /* we may have memory to free at the end of this */
 540           goto fexit;
 541        }
 542        (*env)->SetByteArrayRegion(env, ret, 0, len, (jbyte *)(mac));
 543     }
 544  fexit:
 545    // release the UTF string and interface list
 546    (*env)->ReleaseStringUTFChars(env, name, name_utf);
 547 
 548    close(sock);
 549    return ret;
 550 }
 551 
 552 /*
 553  * Class:       java_net_NetworkInterface
 554  * Method:      getMTU0
 555  * Signature:   ([bLjava/lang/String;I)I
 556  */
 557 JNIEXPORT jint JNICALL Java_java_net_NetworkInterface_getMTU0
 558   (JNIEnv *env, jclass cls, jstring name, jint index)
 559 {
 560     jboolean isCopy;
 561     int ret = -1;
 562     int sock;
 563     const char* name_utf = NULL;
 564 
 565     if (name != NULL) {
 566         name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
 567     } else {
 568         JNU_ThrowNullPointerException(env, "network interface name is NULL");
 569         return ret;
 570     }
 571     if (name_utf == NULL) {
 572        if (!(*env)->ExceptionCheck(env))
 573            JNU_ThrowOutOfMemoryError(env, NULL);
 574        return ret;
 575     }
 576 
 577     if ((sock = openSocketWithFallback(env, name_utf)) < 0) {
 578        (*env)->ReleaseStringUTFChars(env, name, name_utf);
 579        return JNI_FALSE;
 580     }
 581 
 582     ret = getMTU(env, sock, name_utf);
 583 
 584     (*env)->ReleaseStringUTFChars(env, name, name_utf);
 585 
 586     close(sock);
 587     return ret;
 588 }
 589 
 590 /*** Private methods definitions ****/
 591 
 592 static int getFlags0(JNIEnv *env, jstring name) {
 593     jboolean isCopy;
 594     int ret, sock;
 595     const char* name_utf;
 596     int flags = 0;
 597 
 598     if (name != NULL) {
 599         name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
 600     } else {
 601         JNU_ThrowNullPointerException(env, "network interface name is NULL");
 602         return -1;
 603     }
 604 
 605     if (name_utf == NULL) {
 606        if (!(*env)->ExceptionCheck(env))
 607            JNU_ThrowOutOfMemoryError(env, NULL);
 608        return -1;
 609     }
 610     if ((sock = openSocketWithFallback(env, name_utf)) < 0) {
 611         (*env)->ReleaseStringUTFChars(env, name, name_utf);
 612         return -1;
 613     }
 614 
 615     ret = getFlags(sock, name_utf, &flags);
 616 
 617     close(sock);
 618     (*env)->ReleaseStringUTFChars(env, name, name_utf);
 619 
 620     if (ret < 0) {
 621         NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
 622                                      "ioctl SIOCGLIFFLAGS failed");
 623         return -1;
 624     }
 625 
 626     return flags;
 627 }
 628 
 629 /*
 630  * Creates a NetworkInterface object, populates the name, the index, and
 631  * populates the InetAddress array based on the IP addresses for this
 632  * interface.
 633  */
 634 jobject createNetworkInterface(JNIEnv *env, netif *ifs) {
 635     jobject netifObj;
 636     jobject name;
 637     jobjectArray addrArr;
 638     jobjectArray bindArr;
 639     jobjectArray childArr;
 640     netaddr *addrs;
 641     jint addr_index, addr_count, bind_index;
 642     jint child_count, child_index;
 643     netaddr *addrP;
 644     netif *childP;
 645     jobject tmp;
 646 
 647     // Create a NetworkInterface object and populate it
 648     netifObj = (*env)->NewObject(env, ni_class, ni_ctrID);
 649     CHECK_NULL_RETURN(netifObj, NULL);
 650     name = (*env)->NewStringUTF(env, ifs->name);
 651     CHECK_NULL_RETURN(name, NULL);
 652     (*env)->SetObjectField(env, netifObj, ni_nameID, name);
 653     (*env)->SetObjectField(env, netifObj, ni_descID, name);
 654     (*env)->SetIntField(env, netifObj, ni_indexID, ifs->index);
 655     (*env)->SetBooleanField(env, netifObj, ni_virutalID,
 656                             ifs->virtual ? JNI_TRUE : JNI_FALSE);
 657 
 658     //Count the number of address on this interface
 659     addr_count = 0;
 660     addrP = ifs->addr;
 661     while (addrP != NULL) {
 662         addr_count++;
 663         addrP = addrP->next;
 664     }
 665 
 666     // Create the array of InetAddresses
 667     addrArr = (*env)->NewObjectArray(env, addr_count,  ia_class, NULL);
 668     if (addrArr == NULL) {
 669         return NULL;
 670     }
 671 
 672     bindArr = (*env)->NewObjectArray(env, addr_count, ni_ibcls, NULL);
 673     if (bindArr == NULL) {
 674        return NULL;
 675     }
 676     addrP = ifs->addr;
 677     addr_index = 0;
 678     bind_index = 0;
 679     while (addrP != NULL) {
 680         jobject iaObj = NULL;
 681         jobject ibObj = NULL;
 682 
 683         if (addrP->family == AF_INET) {
 684             iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
 685             if (iaObj) {
 686                  setInetAddress_addr(env, iaObj, htonl(
 687                      ((struct sockaddr_in*)addrP->addr)->sin_addr.s_addr));
 688             } else {
 689                 return NULL;
 690             }
 691             ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID);
 692             if (ibObj) {
 693                  (*env)->SetObjectField(env, ibObj, ni_ibaddressID, iaObj);
 694                  if (addrP->brdcast) {
 695                     jobject ia2Obj = NULL;
 696                     ia2Obj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
 697                     if (ia2Obj) {
 698                        setInetAddress_addr(env, ia2Obj, htonl(
 699                            ((struct sockaddr_in*)addrP->brdcast)->sin_addr.s_addr));
 700                        (*env)->SetObjectField(env, ibObj, ni_ib4broadcastID, ia2Obj);
 701                     } else {
 702                         return NULL;
 703                     }
 704                  }
 705                  (*env)->SetShortField(env, ibObj, ni_ib4maskID, addrP->mask);
 706                  (*env)->SetObjectArrayElement(env, bindArr, bind_index++, ibObj);
 707             } else {
 708                 return NULL;
 709             }
 710         }
 711 
 712 #ifdef AF_INET6
 713         if (addrP->family == AF_INET6) {
 714             int scope=0;
 715             iaObj = (*env)->NewObject(env, ia6_class, ia6_ctrID);
 716             if (iaObj) {
 717                 jboolean ret = setInet6Address_ipaddress(env, iaObj,
 718                     (char *)&(((struct sockaddr_in6*)addrP->addr)->sin6_addr));
 719                 if (ret == JNI_FALSE) {
 720                     return NULL;
 721                 }
 722 
 723                 scope = ((struct sockaddr_in6*)addrP->addr)->sin6_scope_id;
 724 
 725                 if (scope != 0) { /* zero is default value, no need to set */
 726                     setInet6Address_scopeid(env, iaObj, scope);
 727                     setInet6Address_scopeifname(env, iaObj, netifObj);
 728                 }
 729             } else {
 730                 return NULL;
 731             }
 732             ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID);
 733             if (ibObj) {
 734                 (*env)->SetObjectField(env, ibObj, ni_ibaddressID, iaObj);
 735                 (*env)->SetShortField(env, ibObj, ni_ib4maskID, addrP->mask);
 736                 (*env)->SetObjectArrayElement(env, bindArr, bind_index++, ibObj);
 737             } else {
 738                 return NULL;
 739             }
 740         }
 741 #endif
 742 
 743         (*env)->SetObjectArrayElement(env, addrArr, addr_index++, iaObj);
 744         addrP = addrP->next;
 745     }
 746 
 747     // See if there is any virtual interface attached to this one.
 748     child_count = 0;
 749     childP = ifs->childs;
 750     while (childP) {
 751         child_count++;
 752         childP = childP->next;
 753     }
 754 
 755     childArr = (*env)->NewObjectArray(env, child_count, ni_class, NULL);
 756     if (childArr == NULL) {
 757         return NULL;
 758     }
 759 
 760     // Create the NetworkInterface instances for the sub-interfaces as well.
 761     child_index = 0;
 762     childP = ifs->childs;
 763     while(childP) {
 764       tmp = createNetworkInterface(env, childP);
 765       if (tmp == NULL) {
 766          return NULL;
 767       }
 768       (*env)->SetObjectField(env, tmp, ni_parentID, netifObj);
 769       (*env)->SetObjectArrayElement(env, childArr, child_index++, tmp);
 770       childP = childP->next;
 771     }
 772     (*env)->SetObjectField(env, netifObj, ni_addrsID, addrArr);
 773     (*env)->SetObjectField(env, netifObj, ni_bindsID, bindArr);
 774     (*env)->SetObjectField(env, netifObj, ni_childsID, childArr);
 775 
 776     // return the NetworkInterface
 777     return netifObj;
 778 }
 779 
 780 /*
 781  * Enumerates all interfaces
 782  */
 783 static netif *enumInterfaces(JNIEnv *env) {
 784     netif *ifs;
 785     int sock;
 786 
 787     // Enumerate IPv4 addresses
 788     sock = openSocket(env, AF_INET);
 789     if (sock < 0 && (*env)->ExceptionOccurred(env)) {
 790         return NULL;
 791     }
 792 
 793     ifs = enumIPv4Interfaces(env, sock, NULL);
 794     close(sock);
 795 
 796     if (ifs == NULL && (*env)->ExceptionOccurred(env)) {
 797         return NULL;
 798     }
 799 
 800     // return partial list if an exception occurs in the middle of process ???
 801 
 802     // If IPv6 is available then enumerate IPv6 addresses.
 803 #ifdef AF_INET6
 804 
 805         // User can disable ipv6 explicitly by -Djava.net.preferIPv4Stack=true,
 806         // so we have to call ipv6_available()
 807         if (ipv6_available()) {
 808 
 809            sock = openSocket(env, AF_INET6);
 810            if (sock < 0 && (*env)->ExceptionOccurred(env)) {
 811                freeif(ifs);
 812                return NULL;
 813            }
 814 
 815            ifs = enumIPv6Interfaces(env, sock, ifs);
 816            close(sock);
 817 
 818            if ((*env)->ExceptionOccurred(env)) {
 819               freeif(ifs);
 820               return NULL;
 821            }
 822 
 823        }
 824 #endif
 825 
 826     return ifs;
 827 }
 828 
 829 #define CHECKED_MALLOC3(_pointer, _type, _size) \
 830     do { \
 831         _pointer = (_type)malloc(_size); \
 832         if (_pointer == NULL) { \
 833             JNU_ThrowOutOfMemoryError(env, "Native heap allocation failed"); \
 834             return ifs; /* return untouched list */ \
 835         } \
 836     } while(0)
 837 
 838 
 839 /*
 840  * Frees an interface list (including any attached addresses)
 841  */
 842 void freeif(netif *ifs) {
 843     netif *currif = ifs;
 844     netif *child = NULL;
 845 
 846     while (currif != NULL) {
 847         netaddr *addrP = currif->addr;
 848         while (addrP != NULL) {
 849             netaddr *next = addrP->next;
 850             free(addrP);
 851             addrP = next;
 852         }
 853 
 854         // Don't forget to free the sub-interfaces.
 855         if (currif->childs != NULL) {
 856             freeif(currif->childs);
 857         }
 858 
 859         ifs = currif->next;
 860         free(currif);
 861         currif = ifs;
 862     }
 863 }
 864 
 865 netif *addif(JNIEnv *env, int sock, const char *if_name, netif *ifs,
 866              struct sockaddr *ifr_addrP, int family, short prefix)
 867 {
 868     netif *currif = ifs, *parent;
 869     netaddr *addrP;
 870 
 871 #ifdef LIFNAMSIZ
 872     int ifnam_size = LIFNAMSIZ;
 873     char name[LIFNAMSIZ], vname[LIFNAMSIZ];
 874 #else
 875     int ifnam_size = IFNAMSIZ;
 876     char name[IFNAMSIZ], vname[IFNAMSIZ];
 877 #endif
 878 
 879     char *name_colonP;
 880     int mask;
 881     int isVirtual = 0;
 882     int addr_size;
 883     int flags = 0;
 884 
 885     // If the interface name is a logical interface then we remove the unit
 886     // number so that we have the physical interface (eg: hme0:1 -> hme0).
 887     // NetworkInterface currently doesn't have any concept of physical vs.
 888     // logical interfaces.
 889     strncpy(name, if_name, ifnam_size);
 890     name[ifnam_size - 1] = '\0';
 891     *vname = 0;
 892 
 893      // Create and populate the netaddr node. If allocation fails
 894      // return an un-updated list.
 895 
 896      // Allocate for addr and brdcast at once
 897 
 898 #ifdef AF_INET6
 899     addr_size = (family == AF_INET) ? sizeof(struct sockaddr_in)
 900                                     : sizeof(struct sockaddr_in6);
 901 #else
 902     addr_size = sizeof(struct sockaddr_in);
 903 #endif
 904 
 905     CHECKED_MALLOC3(addrP, netaddr *, sizeof(netaddr) + 2 * addr_size);
 906     addrP->addr = (struct sockaddr *)((char *)addrP + sizeof(netaddr));
 907     memcpy(addrP->addr, ifr_addrP, addr_size);
 908 
 909     addrP->family = family;
 910     addrP->brdcast = NULL;
 911     addrP->mask = prefix;
 912     addrP->next = 0;
 913     if (family == AF_INET) {
 914        // Deal with broadcast addr & subnet mask
 915        struct sockaddr *brdcast_to =
 916               (struct sockaddr *) ((char *)addrP + sizeof(netaddr) + addr_size);
 917        addrP->brdcast = getBroadcast(env, sock, name, brdcast_to);
 918        if ((*env)->ExceptionCheck(env) == JNI_TRUE) {
 919            return ifs;
 920        }
 921        if ((mask = getSubnet(env, sock, name)) != -1) {
 922            addrP->mask = mask;
 923        } else if((*env)->ExceptionCheck(env)) {
 924            return ifs;
 925        }
 926      }
 927 
 928     // Deal with virtual interface with colon notation e.g. eth0:1
 929     name_colonP = strchr(name, ':');
 930     if (name_colonP != NULL) {
 931         // This is a virtual interface. If we are able to access the parent
 932         // we need to create a new entry if it doesn't exist yet *and* update
 933         // the 'parent' interface with the new records.
 934         *name_colonP = 0;
 935         if (getFlags(sock, name, &flags) < 0 || flags < 0) {
 936              // failed to access parent interface do not create parent.
 937              // We are a virtual interface with no parent.
 938              isVirtual = 1;
 939              *name_colonP = ':';
 940         } else {
 941              // Got access to parent, so create it if necessary.
 942              // Save original name to vname and truncate name by ':'
 943              memcpy(vname, name, sizeof(vname) );
 944              vname[name_colonP - name] = ':';
 945         }
 946     }
 947 
 948     // Check if this is a "new" interface. Use the interface name for
 949     // matching because index isn't supported on Solaris 2.6 & 7.
 950     while (currif != NULL) {
 951         if (strcmp(name, currif->name) == 0) {
 952             break;
 953         }
 954         currif = currif->next;
 955     }
 956 
 957     // If "new" then create an netif structure and insert it into the list.
 958     if (currif == NULL) {
 959          CHECKED_MALLOC3(currif, netif *, sizeof(netif) + ifnam_size);
 960          currif->name = (char *)currif + sizeof(netif);
 961          strncpy(currif->name, name, ifnam_size);
 962          currif->name[ifnam_size - 1] = '\0';
 963          currif->index = getIndex(sock, name);
 964          currif->addr = NULL;
 965          currif->childs = NULL;
 966          currif->virtual = isVirtual;
 967          currif->next = ifs;
 968          ifs = currif;
 969     }
 970 
 971     // Finally insert the address on the interface
 972     addrP->next = currif->addr;
 973     currif->addr = addrP;
 974 
 975     parent = currif;
 976 
 977     // Deal with the virtual interface now.
 978     if (vname[0]) {
 979         netaddr *tmpaddr;
 980 
 981         currif = parent->childs;
 982 
 983         while (currif != NULL) {
 984             if (strcmp(vname, currif->name) == 0) {
 985                 break;
 986             }
 987             currif = currif->next;
 988         }
 989 
 990         if (currif == NULL) {
 991             CHECKED_MALLOC3(currif, netif *, sizeof(netif) + ifnam_size);
 992             currif->name = (char *)currif + sizeof(netif);
 993             strncpy(currif->name, vname, ifnam_size);
 994             currif->name[ifnam_size - 1] = '\0';
 995             currif->index = getIndex(sock, vname);
 996             currif->addr = NULL;
 997             // Need to duplicate the addr entry?
 998             currif->virtual = 1;
 999             currif->childs = NULL;
1000             currif->next = parent->childs;
1001             parent->childs = currif;
1002         }
1003 
1004         CHECKED_MALLOC3(tmpaddr, netaddr *, sizeof(netaddr) + 2 * addr_size);
1005         memcpy(tmpaddr, addrP, sizeof(netaddr));
1006         if (addrP->addr != NULL) {
1007             tmpaddr->addr = (struct sockaddr *)
1008                 ((char*)tmpaddr + sizeof(netaddr));
1009             memcpy(tmpaddr->addr, addrP->addr, addr_size);
1010         }
1011 
1012         if (addrP->brdcast != NULL) {
1013             tmpaddr->brdcast = (struct sockaddr *)
1014                 ((char *)tmpaddr + sizeof(netaddr) + addr_size);
1015             memcpy(tmpaddr->brdcast, addrP->brdcast, addr_size);
1016         }
1017 
1018         tmpaddr->next = currif->addr;
1019         currif->addr = tmpaddr;
1020     }
1021 
1022     return ifs;
1023 }
1024 
1025 /*
1026  * Opens a socket for further ioct calls. proto is one of AF_INET or AF_INET6.
1027  */
1028 static int openSocket(JNIEnv *env, int proto) {
1029     int sock;
1030 
1031     if ((sock = socket(proto, SOCK_DGRAM, 0)) < 0) {
1032         // If EPROTONOSUPPORT is returned it means we don't have
1033         // support for this proto so don't throw an exception.
1034         if (errno != EPROTONOSUPPORT) {
1035             NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
1036                                          "Socket creation failed");
1037         }
1038         return -1;
1039     }
1040 
1041     return sock;
1042 }
1043 
1044 
1045 /** Linux, AIX **/
1046 #if defined(__linux__) || defined(_AIX)
1047 
1048 #ifdef AF_INET6
1049 /*
1050  * Opens a socket for further ioct calls. Tries AF_INET socket first and
1051  * if it falls return AF_INET6 socket.
1052  */
1053 // unused arg ifname and struct if2
1054 static int openSocketWithFallback(JNIEnv *env, const char *ifname) {
1055     int sock;
1056     struct ifreq if2;
1057 
1058     if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1059         if (errno == EPROTONOSUPPORT) {
1060             if ((sock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1061                 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
1062                                              "IPV6 Socket creation failed");
1063                 return -1;
1064             }
1065         } else { // errno is not NOSUPPORT
1066             NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
1067                                          "IPV4 Socket creation failed");
1068             return -1;
1069         }
1070     }
1071 
1072     // Linux starting from 2.6.? kernel allows ioctl call with either IPv4 or
1073     // IPv6 socket regardless of type of address of an interface.
1074     return sock;
1075 }
1076 
1077 #else
1078 static int openSocketWithFallback(JNIEnv *env, const char *ifname) {
1079     return openSocket(env,AF_INET);
1080 }
1081 #endif
1082 
1083 static netif *enumIPv4Interfaces(JNIEnv *env, int sock, netif *ifs) {
1084     struct ifconf ifc;
1085     struct ifreq *ifreqP;
1086     char *buf = NULL;
1087     int numifs;
1088     unsigned i;
1089     int siocgifconfRequest = SIOCGIFCONF;
1090 
1091 #if defined(__linux__)
1092     // need to do a dummy SIOCGIFCONF to determine the buffer size.
1093     // SIOCGIFCOUNT doesn't work
1094     ifc.ifc_buf = NULL;
1095     if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
1096         NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
1097                                      "ioctl SIOCGIFCONF failed");
1098         return ifs;
1099     }
1100 #elif defined(_AIX)
1101     ifc.ifc_buf = NULL;
1102     if (ioctl(sock, SIOCGSIZIFCONF, &(ifc.ifc_len)) < 0) {
1103         NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
1104                                      "ioctl SIOCGSIZIFCONF failed");
1105         return ifs;
1106     }
1107 #endif /* __linux__ */
1108 
1109     CHECKED_MALLOC3(buf, char *, ifc.ifc_len);
1110 
1111     ifc.ifc_buf = buf;
1112 #if defined(_AIX)
1113     siocgifconfRequest = CSIOCGIFCONF;
1114 #endif
1115     if (ioctl(sock, siocgifconfRequest, (char *)&ifc) < 0) {
1116         NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
1117                                      "ioctl SIOCGIFCONF failed");
1118         free(buf);
1119         return ifs;
1120     }
1121 
1122     // Iterate through each interface
1123     ifreqP = ifc.ifc_req;
1124     for (i = 0; i < ifc.ifc_len / sizeof(struct ifreq); i++, ifreqP++) {
1125 #if defined(_AIX)
1126         if (ifreqP->ifr_addr.sa_family != AF_INET) continue;
1127 #endif
1128         // Add to the list
1129         ifs = addif(env, sock, ifreqP->ifr_name, ifs,
1130                     (struct sockaddr *)&(ifreqP->ifr_addr), AF_INET, 0);
1131 
1132         // If an exception occurred then free the list
1133         if ((*env)->ExceptionOccurred(env)) {
1134             free(buf);
1135             freeif(ifs);
1136             return NULL;
1137         }
1138     }
1139 
1140     // Free socket and buffer
1141     free(buf);
1142     return ifs;
1143 }
1144 
1145 
1146 #if defined(AF_INET6) && defined(__linux__)
1147 
1148 /*
1149  * Enumerates and returns all IPv6 interfaces on Linux.
1150  */
1151 static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs) {
1152     FILE *f;
1153     char addr6[40], devname[21];
1154     char addr6p[8][5];
1155     int prefix, scope, dad_status, if_idx;
1156     uint8_t ipv6addr[16];
1157 
1158     if ((f = fopen(_PATH_PROCNET_IFINET6, "r")) != NULL) {
1159         while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %08x %02x %02x %02x %20s\n",
1160                       addr6p[0], addr6p[1], addr6p[2], addr6p[3],
1161                       addr6p[4], addr6p[5], addr6p[6], addr6p[7],
1162                       &if_idx, &prefix, &scope, &dad_status, devname) != EOF) {
1163 
1164             struct netif *ifs_ptr = NULL;
1165             struct netif *last_ptr = NULL;
1166             struct sockaddr_in6 addr;
1167 
1168             sprintf(addr6, "%s:%s:%s:%s:%s:%s:%s:%s",
1169                     addr6p[0], addr6p[1], addr6p[2], addr6p[3],
1170                     addr6p[4], addr6p[5], addr6p[6], addr6p[7]);
1171             inet_pton(AF_INET6, addr6, ipv6addr);
1172 
1173             memset(&addr, 0, sizeof(struct sockaddr_in6));
1174             memcpy((void*)addr.sin6_addr.s6_addr, (const void*)ipv6addr, 16);
1175 
1176             addr.sin6_scope_id = if_idx;
1177 
1178             ifs = addif(env, sock, devname, ifs, (struct sockaddr *)&addr,
1179                         AF_INET6, (short)prefix);
1180 
1181             // If an exception occurred then return the list as is.
1182             if ((*env)->ExceptionOccurred(env)) {
1183                 fclose(f);
1184                 return ifs;
1185             }
1186        }
1187        fclose(f);
1188     }
1189     return ifs;
1190 }
1191 #endif
1192 
1193 
1194 #if defined(AF_INET6) && defined(_AIX)
1195 
1196 /*
1197  * Enumerates and returns all IPv6 interfaces on AIX.
1198  */
1199 static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs) {
1200     struct ifconf ifc;
1201     struct ifreq *ifreqP;
1202     char *buf;
1203     int numifs;
1204     unsigned i;
1205     unsigned bufsize;
1206     char *cp, *cplimit;
1207 
1208     // use SIOCGSIZIFCONF to get size for  SIOCGIFCONF
1209 
1210     ifc.ifc_buf = NULL;
1211     if (ioctl(sock, SIOCGSIZIFCONF, &(ifc.ifc_len)) < 0) {
1212         NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
1213                                     "ioctl SIOCGSIZIFCONF failed");
1214         return ifs;
1215     }
1216     bufsize = ifc.ifc_len;
1217 
1218     buf = (char *)malloc(bufsize);
1219     if (!buf) {
1220         JNU_ThrowOutOfMemoryError(env, "Network interface native buffer allocation failed");
1221         return ifs;
1222     }
1223     ifc.ifc_len = bufsize;
1224     ifc.ifc_buf = buf;
1225     if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
1226         NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
1227                                      "ioctl CSIOCGIFCONF failed");
1228         free(buf);
1229         return ifs;
1230     }
1231 
1232     // Iterate through each interface
1233     ifreqP = ifc.ifc_req;
1234     cp = (char *)ifc.ifc_req;
1235     cplimit = cp + ifc.ifc_len;
1236 
1237     for (; cp < cplimit;
1238         cp += (sizeof(ifreqP->ifr_name) +
1239                MAX((ifreqP->ifr_addr).sa_len, sizeof(ifreqP->ifr_addr))))
1240     {
1241         ifreqP = (struct ifreq *)cp;
1242         struct ifreq if2;
1243         memset((char *)&if2, 0, sizeof(if2));
1244         strncpy(if2.ifr_name, ifreqP->ifr_name, sizeof(if2.ifr_name) - 1);
1245 
1246         // Skip interface that aren't UP
1247         if (ioctl(sock, SIOCGIFFLAGS, (char *)&if2) >= 0) {
1248             if (!(if2.ifr_flags & IFF_UP)) {
1249                 continue;
1250             }
1251         }
1252 
1253         if (ifreqP->ifr_addr.sa_family != AF_INET6)
1254             continue;
1255 
1256         if (ioctl(sock, SIOCGIFSITE6, (char *)&if2) >= 0) {
1257             struct sockaddr_in6 *s6= (struct sockaddr_in6 *)&(ifreqP->ifr_addr);
1258             s6->sin6_scope_id = if2.ifr_site6;
1259         }
1260 
1261         // Add to the list
1262         ifs = addif(env, sock, ifreqP->ifr_name, ifs,
1263                     (struct sockaddr *)&(ifreqP->ifr_addr), AF_INET6, 0);
1264 
1265         // If an exception occurred then free the list
1266         if ((*env)->ExceptionOccurred(env)) {
1267             free(buf);
1268             freeif(ifs);
1269             return NULL;
1270         }
1271     }
1272 
1273     // Free socket and buffer
1274     free(buf);
1275     return ifs;
1276 }
1277 #endif
1278 
1279 
1280 static int getIndex(int sock, const char *name) {
1281      // Try to get the interface index
1282 #if defined(_AIX)
1283     return if_nametoindex(name);
1284 #else
1285     struct ifreq if2;
1286     memset((char *)&if2, 0, sizeof(if2));
1287     strncpy(if2.ifr_name, name, sizeof(if2.ifr_name) - 1);
1288 
1289     if (ioctl(sock, SIOCGIFINDEX, (char *)&if2) < 0) {
1290         return -1;
1291     }
1292 
1293     return if2.ifr_ifindex;
1294 #endif
1295 }
1296 
1297 /*
1298  * Returns the IPv4 broadcast address of a named interface, if it exists.
1299  * Returns 0 if it doesn't have one.
1300  */
1301 static struct sockaddr *getBroadcast
1302   (JNIEnv *env, int sock, const char *ifname, struct sockaddr *brdcast_store)
1303 {
1304     struct sockaddr *ret = NULL;
1305     struct ifreq if2;
1306     memset((char *)&if2, 0, sizeof(if2));
1307     strncpy(if2.ifr_name, ifname, sizeof(if2.ifr_name) - 1);
1308 
1309     // Let's make sure the interface does have a broadcast address.
1310     if (ioctl(sock, SIOCGIFFLAGS, (char *)&if2)  < 0) {
1311         NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
1312                                      "ioctl SIOCGIFFLAGS failed");
1313         return ret;
1314     }
1315 
1316     if (if2.ifr_flags & IFF_BROADCAST) {
1317         // It does, let's retrieve it
1318         if (ioctl(sock, SIOCGIFBRDADDR, (char *)&if2) < 0) {
1319             NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
1320                                          "ioctl SIOCGIFBRDADDR failed");
1321             return ret;
1322         }
1323 
1324         ret = brdcast_store;
1325         memcpy(ret, &if2.ifr_broadaddr, sizeof(struct sockaddr));
1326     }
1327 
1328     return ret;
1329 }
1330 
1331 /*
1332  * Returns the IPv4 subnet prefix length (aka subnet mask) for the named
1333  * interface, if it has one, otherwise return -1.
1334  */
1335 static short getSubnet(JNIEnv *env, int sock, const char *ifname) {
1336     unsigned int mask;
1337     short ret;
1338     struct ifreq if2;
1339     memset((char *)&if2, 0, sizeof(if2));
1340     strncpy(if2.ifr_name, ifname, sizeof(if2.ifr_name) - 1);
1341 
1342     if (ioctl(sock, SIOCGIFNETMASK, (char *)&if2) < 0) {
1343         NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
1344                                      "ioctl SIOCGIFNETMASK failed");
1345         return -1;
1346     }
1347 
1348     mask = ntohl(((struct sockaddr_in*)&(if2.ifr_addr))->sin_addr.s_addr);
1349     ret = 0;
1350     while (mask) {
1351        mask <<= 1;
1352        ret++;
1353     }
1354 
1355     return ret;
1356 }
1357 
1358 /*
1359  * Gets the Hardware address (usually MAC address) for the named interface.
1360  * On return puts the data in buf, and returns the length, in byte, of the
1361  * MAC address. Returns -1 if there is no hardware address on that interface.
1362  */
1363 static int getMacAddress
1364   (JNIEnv *env, int sock, const char* ifname, const struct in_addr* addr,
1365    unsigned char *buf)
1366 {
1367 #if defined (_AIX)
1368     int size;
1369     struct kinfo_ndd *nddp;
1370     void *end;
1371 
1372     size = getkerninfo(KINFO_NDD, 0, 0, 0);
1373     if (size == 0) {
1374         return -1;
1375     }
1376 
1377     if (size < 0) {
1378         perror("getkerninfo 1");
1379         return -1;
1380     }
1381 
1382     nddp = (struct kinfo_ndd *)malloc(size);
1383 
1384     if (!nddp) {
1385         JNU_ThrowOutOfMemoryError(env,
1386             "Network interface getMacAddress native buffer allocation failed");
1387         return -1;
1388     }
1389 
1390     if (getkerninfo(KINFO_NDD, nddp, &size, 0) < 0) {
1391         perror("getkerninfo 2");
1392         return -1;
1393     }
1394 
1395     end = (void *)nddp + size;
1396     while ((void *)nddp < end) {
1397         if (!strcmp(nddp->ndd_alias, ifname) ||
1398                 !strcmp(nddp->ndd_name, ifname)) {
1399             bcopy(nddp->ndd_addr, buf, 6);
1400             return 6;
1401         } else {
1402             nddp++;
1403         }
1404     }
1405 
1406     return -1;
1407 #elif defined(__linux__)
1408     static struct ifreq ifr;
1409     int i;
1410     memset((char *)&ifr, 0, sizeof(ifr));
1411     strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
1412     if (ioctl(sock, SIOCGIFHWADDR, &ifr) < 0) {
1413         NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
1414                                      "ioctl SIOCGIFHWADDR failed");
1415         return -1;
1416     }
1417 
1418     memcpy(buf, &ifr.ifr_hwaddr.sa_data, IFHWADDRLEN);
1419 
1420     // All bytes to 0 means no hardware address.
1421 
1422     for (i = 0; i < IFHWADDRLEN; i++) {
1423         if (buf[i] != 0)
1424             return IFHWADDRLEN;
1425     }
1426 
1427     return -1;
1428 #endif
1429 }
1430 
1431 static int getMTU(JNIEnv *env, int sock,  const char *ifname) {
1432     struct ifreq if2;
1433     memset((char *)&if2, 0, sizeof(if2));
1434 
1435     if (ifname != NULL) {
1436         strncpy(if2.ifr_name, ifname, sizeof(if2.ifr_name) - 1);
1437     } else {
1438         JNU_ThrowNullPointerException(env, "network interface name is NULL");
1439         return -1;
1440     }
1441 
1442     if (ioctl(sock, SIOCGIFMTU, (char *)&if2) < 0) {
1443         NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
1444                                      "ioctl SIOCGIFMTU failed");
1445         return -1;
1446     }
1447 
1448     return  if2.ifr_mtu;
1449 }
1450 
1451 static int getFlags(int sock, const char *ifname, int *flags) {
1452   struct ifreq if2;
1453   memset((char *)&if2, 0, sizeof(if2));
1454   strncpy(if2.ifr_name, ifname, sizeof(if2.ifr_name) - 1);
1455 
1456   if (ioctl(sock, SIOCGIFFLAGS, (char *)&if2) < 0) {
1457       return -1;
1458   }
1459 
1460   if (sizeof(if2.ifr_flags) == sizeof(short)) {
1461       *flags = (if2.ifr_flags & 0xffff);
1462   } else {
1463       *flags = if2.ifr_flags;
1464   }
1465   return 0;
1466 }
1467 
1468 #endif  /* defined(__linux__) || defined(_AIX) */
1469 
1470 /** Solaris **/
1471 #if defined(__solaris__)
1472 
1473 /*
1474  * Opens a socket for further ioct calls. Tries AF_INET socket first and
1475  * if it falls return AF_INET6 socket.
1476  */
1477 #ifdef AF_INET6
1478 static int openSocketWithFallback(JNIEnv *env, const char *ifname) {
1479     int sock, alreadyV6 = 0;
1480     struct lifreq if2;
1481 
1482     if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1483         if (errno == EPROTONOSUPPORT) {
1484             if ((sock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1485                 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
1486                                              "IPV6 Socket creation failed");
1487                 return -1;
1488             }
1489 
1490             alreadyV6=1;
1491         } else { // errno is not NOSUPPORT
1492             NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
1493                                          "IPV4 Socket creation failed");
1494             return -1;
1495         }
1496     }
1497 
1498 
1499     // Solaris requires that we have an IPv6 socket to query an  interface
1500     // without an IPv4 address - check it here. POSIX 1 require the kernel to
1501     // return ENOTTY if the call is inappropriate for a device e.g. the NETMASK
1502     // for a device having IPv6 only address but not all devices follow the
1503     // standard so fall back on any error. It's not an ecologically friendly
1504     // gesture but more reliable.
1505 
1506     if (!alreadyV6) {
1507         memset((char *)&if2, 0, sizeof(if2));
1508         strncpy(if2.lifr_name, ifname, sizeof(if2.lifr_name) - 1);
1509         if (ioctl(sock, SIOCGLIFNETMASK, (char *)&if2) < 0) {
1510             close(sock);
1511             if ((sock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1512                 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
1513                                              "IPV6 Socket creation failed");
1514                 return -1;
1515             }
1516         }
1517     }
1518 
1519     return sock;
1520 }
1521 
1522 #else
1523 static int openSocketWithFallback(JNIEnv *env, const char *ifname) {
1524     return openSocket(env,AF_INET);
1525 }
1526 #endif
1527 
1528 /*
1529  * Enumerates and returns all IPv4 interfaces.
1530  */
1531 static netif *enumIPv4Interfaces(JNIEnv *env, int sock, netif *ifs) {
1532     return enumIPvXInterfaces(env,sock, ifs, AF_INET);
1533 }
1534 
1535 #ifdef AF_INET6
1536 static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs) {
1537     return enumIPvXInterfaces(env,sock, ifs, AF_INET6);
1538 }
1539 #endif
1540 
1541 /*
1542  * Enumerates and returns all interfaces on Solaris.
1543  * Uses the same code for IPv4 and IPv6.
1544  */
1545 static netif *enumIPvXInterfaces(JNIEnv *env, int sock, netif *ifs, int family) {
1546     struct lifconf ifc;
1547     struct lifreq *ifr;
1548     int n;
1549     char *buf;
1550     struct lifnum numifs;
1551     unsigned bufsize;
1552 
1553     // Get the interface count
1554     numifs.lifn_family = family;
1555     numifs.lifn_flags = 0;
1556     if (ioctl(sock, SIOCGLIFNUM, (char *)&numifs) < 0) {
1557         NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
1558                                      "ioctl SIOCGLIFNUM failed");
1559         return ifs;
1560     }
1561 
1562     //  Enumerate the interface configurations
1563     bufsize = numifs.lifn_count * sizeof (struct lifreq);
1564     CHECKED_MALLOC3(buf, char *, bufsize);
1565 
1566     ifc.lifc_family = family;
1567     ifc.lifc_flags = 0;
1568     ifc.lifc_len = bufsize;
1569     ifc.lifc_buf = buf;
1570     if (ioctl(sock, SIOCGLIFCONF, (char *)&ifc) < 0) {
1571         NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
1572                                      "ioctl SIOCGLIFCONF failed");
1573         free(buf);
1574         return ifs;
1575     }
1576 
1577     // Iterate through each interface
1578     ifr = ifc.lifc_req;
1579     for (n=0; n<numifs.lifn_count; n++, ifr++) {
1580         int index = -1;
1581         struct lifreq if2;
1582 
1583         // Ignore either IPv4 or IPv6 addresses
1584         if (ifr->lifr_addr.ss_family != family) {
1585             continue;
1586         }
1587 
1588 #ifdef AF_INET6
1589         if (ifr->lifr_addr.ss_family == AF_INET6) {
1590             struct sockaddr_in6 *s6= (struct sockaddr_in6 *)&(ifr->lifr_addr);
1591             s6->sin6_scope_id = getIndex(sock, ifr->lifr_name);
1592         }
1593 #endif
1594 
1595         // add to the list
1596         ifs = addif(env, sock,ifr->lifr_name, ifs,
1597                     (struct sockaddr *)&(ifr->lifr_addr), family,
1598                     (short)ifr->lifr_addrlen);
1599 
1600         // If an exception occurred we return immediately
1601         if ((*env)->ExceptionOccurred(env)) {
1602             free(buf);
1603             return ifs;
1604         }
1605 
1606    }
1607 
1608     free(buf);
1609     return ifs;
1610 }
1611 
1612 static int getIndex(int sock, const char *name) {
1613     // Try to get the interface index.  (Not supported on Solaris 2.6 or 7)
1614     struct lifreq if2;
1615     memset((char *)&if2, 0, sizeof(if2));
1616     strncpy(if2.lifr_name, name, sizeof(if2.lifr_name) - 1);
1617 
1618     if (ioctl(sock, SIOCGLIFINDEX, (char *)&if2) < 0) {
1619         return -1;
1620     }
1621 
1622     return if2.lifr_index;
1623 }
1624 
1625 /*
1626  * Returns the IPv4 broadcast address of a named interface, if it exists.
1627  * Returns 0 if it doesn't have one.
1628  */
1629 static struct sockaddr *getBroadcast
1630   (JNIEnv *env, int sock, const char *ifname, struct sockaddr *brdcast_store)
1631 {
1632     struct sockaddr *ret = NULL;
1633     struct lifreq if2;
1634     memset((char *)&if2, 0, sizeof(if2));
1635     strncpy(if2.lifr_name, ifname, sizeof(if2.lifr_name) - 1);
1636 
1637     // Let's make sure the interface does have a broadcast address
1638     if (ioctl(sock, SIOCGLIFFLAGS, (char *)&if2)  < 0) {
1639         NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
1640                                      "ioctl SIOCGLIFFLAGS failed");
1641         return ret;
1642     }
1643 
1644     if (if2.lifr_flags & IFF_BROADCAST) {
1645         // It does, let's retrieve it
1646         if (ioctl(sock, SIOCGLIFBRDADDR, (char *)&if2) < 0) {
1647             NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
1648                                          "ioctl SIOCGLIFBRDADDR failed");
1649             return ret;
1650         }
1651 
1652         ret = brdcast_store;
1653         memcpy(ret, &if2.lifr_broadaddr, sizeof(struct sockaddr));
1654     }
1655 
1656     return ret;
1657 }
1658 
1659 /*
1660  * Returns the IPv4 subnet prefix length (aka subnet mask) for the named
1661  * interface, if it has one, otherwise return -1.
1662  */
1663 static short getSubnet(JNIEnv *env, int sock, const char *ifname) {
1664     unsigned int mask;
1665     short ret;
1666     struct lifreq if2;
1667     memset((char *)&if2, 0, sizeof(if2));
1668     strncpy(if2.lifr_name, ifname, sizeof(if2.lifr_name) - 1);
1669 
1670     if (ioctl(sock, SIOCGLIFNETMASK, (char *)&if2) < 0) {
1671         NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
1672                                      "ioctl SIOCGLIFNETMASK failed");
1673         return -1;
1674     }
1675 
1676     mask = ntohl(((struct sockaddr_in*)&(if2.lifr_addr))->sin_addr.s_addr);
1677     ret = 0;
1678 
1679     while (mask) {
1680        mask <<= 1;
1681        ret++;
1682     }
1683 
1684     return ret;
1685 }
1686 
1687 
1688 #define DEV_PREFIX  "/dev/"
1689 
1690 /*
1691  * Solaris specific DLPI code to get hardware address from a device.
1692  * Unfortunately, at least up to Solaris X, you have to have special
1693  * privileges (i.e. be root).
1694  */
1695 static int getMacFromDevice
1696   (JNIEnv *env, const char* ifname, unsigned char* retbuf)
1697 {
1698     char style1dev[MAXPATHLEN];
1699     int fd;
1700     dl_phys_addr_req_t dlpareq;
1701     dl_phys_addr_ack_t *dlpaack;
1702     struct strbuf msg;
1703     char buf[128];
1704     int flags = 0;
1705 
1706     // Device is in /dev.  e.g.: /dev/bge0
1707     strcpy(style1dev, DEV_PREFIX);
1708     strcat(style1dev, ifname);
1709     if ((fd = open(style1dev, O_RDWR)) < 0) {
1710          // Can't open it. We probably are missing the privilege.
1711          // We'll have to try something else
1712          return 0;
1713     }
1714 
1715     dlpareq.dl_primitive = DL_PHYS_ADDR_REQ;
1716     dlpareq.dl_addr_type = DL_CURR_PHYS_ADDR;
1717 
1718     msg.buf = (char *)&dlpareq;
1719     msg.len = DL_PHYS_ADDR_REQ_SIZE;
1720 
1721     if (putmsg(fd, &msg, NULL, 0) < 0) {
1722         NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
1723                                      "putmsg failed");
1724         return -1;
1725     }
1726 
1727     dlpaack = (dl_phys_addr_ack_t *)buf;
1728 
1729     msg.buf = (char *)buf;
1730     msg.len = 0;
1731     msg.maxlen = sizeof (buf);
1732     if (getmsg(fd, &msg, NULL, &flags) < 0) {
1733         NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
1734                                      "getmsg failed");
1735         return -1;
1736     }
1737 
1738     if (msg.len < DL_PHYS_ADDR_ACK_SIZE || dlpaack->dl_primitive != DL_PHYS_ADDR_ACK) {
1739         JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
1740                         "Couldn't obtain phys addr\n");
1741         return -1;
1742     }
1743 
1744     memcpy(retbuf, &buf[dlpaack->dl_addr_offset], dlpaack->dl_addr_length);
1745     return dlpaack->dl_addr_length;
1746 }
1747 
1748 /*
1749  * Gets the Hardware address (usually MAC address) for the named interface.
1750  * On return puts the data in buf, and returns the length, in byte, of the
1751  * MAC address. Returns -1 if there is no hardware address on that interface.
1752  */
1753 static int getMacAddress
1754   (JNIEnv *env, int sock, const char *ifname, const struct in_addr* addr,
1755    unsigned char *buf)
1756 {
1757     struct arpreq arpreq;
1758     struct sockaddr_in* sin;
1759     struct sockaddr_in ipAddr;
1760     int len, i;
1761     struct lifreq lif;
1762 
1763     // First, try the new (S11) SIOCGLIFHWADDR ioctl(). If that fails
1764     // try the old way.
1765     memset(&lif, 0, sizeof(lif));
1766     strlcpy(lif.lifr_name, ifname, sizeof(lif.lifr_name));
1767 
1768     if (ioctl(sock, SIOCGLIFHWADDR, &lif) != -1) {
1769         struct sockaddr_dl *sp;
1770         sp = (struct sockaddr_dl *)&lif.lifr_addr;
1771         memcpy(buf, &sp->sdl_data[0], sp->sdl_alen);
1772         return sp->sdl_alen;
1773     }
1774 
1775     // On Solaris we have to use DLPI, but it will only work if we have
1776     // privileged access (i.e. root). If that fails, we try a lookup
1777     // in the ARP table, which requires an IPv4 address.
1778     if ((len = getMacFromDevice(env, ifname, buf))  == 0) {
1779         // DLPI failed - trying to do arp lookup
1780 
1781         if (addr == NULL) {
1782              // No IPv4 address for that interface, so can't do an ARP lookup.
1783              return -1;
1784          }
1785 
1786          len = 6; //???
1787 
1788          sin = (struct sockaddr_in *) &arpreq.arp_pa;
1789          memset((char *) &arpreq, 0, sizeof(struct arpreq));
1790          ipAddr.sin_port = 0;
1791          ipAddr.sin_family = AF_INET;
1792          memcpy(&ipAddr.sin_addr, addr, sizeof(struct in_addr));
1793          memcpy(&arpreq.arp_pa, &ipAddr, sizeof(struct sockaddr_in));
1794          arpreq.arp_flags= ATF_PUBL;
1795 
1796          if (ioctl(sock, SIOCGARP, &arpreq) < 0) {
1797              return -1;
1798          }
1799 
1800          memcpy(buf, &arpreq.arp_ha.sa_data[0], len );
1801     }
1802 
1803     // All bytes to 0 means no hardware address.
1804     for (i = 0; i < len; i++) {
1805       if (buf[i] != 0)
1806          return len;
1807     }
1808 
1809     return -1;
1810 }
1811 
1812 static int getMTU(JNIEnv *env, int sock,  const char *ifname) {
1813     struct lifreq if2;
1814     memset((char *)&if2, 0, sizeof(if2));
1815     strncpy(if2.lifr_name, ifname, sizeof(if2.lifr_name) - 1);
1816 
1817     if (ioctl(sock, SIOCGLIFMTU, (char *)&if2) < 0) {
1818         NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
1819                                     "ioctl SIOCGLIFMTU failed");
1820         return -1;
1821     }
1822 
1823     return if2.lifr_mtu;
1824 }
1825 
1826 static int getFlags(int sock, const char *ifname, int *flags) {
1827     struct lifreq if2;
1828     memset((char *)&if2, 0, sizeof(if2));
1829     strncpy(if2.lifr_name, ifname, sizeof(if2.lifr_name) - 1);
1830 
1831     if (ioctl(sock, SIOCGLIFFLAGS, (char *)&if2) < 0) {
1832          return -1;
1833     }
1834 
1835     *flags = if2.lifr_flags;
1836     return 0;
1837 }
1838 
1839 
1840 #endif  /* __solaris__ */
1841 
1842 
1843 /** BSD **/
1844 #ifdef _ALLBSD_SOURCE
1845 
1846 /*
1847  * Opens a socket for further ioct calls. Tries AF_INET socket first and
1848  * if it falls return AF_INET6 socket.
1849  */
1850 #ifdef AF_INET6
1851 static int openSocketWithFallback(JNIEnv *env, const char *ifname) {
1852     int sock;
1853     struct ifreq if2;
1854 
1855      if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1856          if (errno == EPROTONOSUPPORT) {
1857               if ((sock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1858                  NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
1859                                               "IPV6 Socket creation failed");
1860                  return -1;
1861               }
1862          }
1863          else{ // errno is not NOSUPPORT
1864              NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
1865                                           "IPV4 Socket creation failed");
1866              return -1;
1867          }
1868    }
1869 
1870    return sock;
1871 }
1872 
1873 #else
1874 static int openSocketWithFallback(JNIEnv *env, const char *ifname) {
1875     return openSocket(env,AF_INET);
1876 }
1877 #endif
1878 
1879 /*
1880  * Enumerates and returns all IPv4 interfaces.
1881  */
1882 static netif *enumIPv4Interfaces(JNIEnv *env, int sock, netif *ifs) {
1883     struct ifaddrs *ifa, *origifa;
1884 
1885     if (getifaddrs(&origifa) != 0) {
1886         NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
1887                                      "getifaddrs() function failed");
1888         return ifs;
1889     }
1890 
1891     for (ifa = origifa; ifa != NULL; ifa = ifa->ifa_next) {
1892 
1893         // Skip non-AF_INET entries.
1894         if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family != AF_INET)
1895             continue;
1896 
1897         // Add to the list.
1898         ifs = addif(env, sock, ifa->ifa_name, ifs, ifa->ifa_addr, AF_INET, 0);
1899 
1900         // If an exception occurred then free the list.
1901         if ((*env)->ExceptionOccurred(env)) {
1902             freeifaddrs(origifa);
1903             freeif(ifs);
1904             return NULL;
1905         }
1906     }
1907 
1908     // Free socket and buffer
1909     freeifaddrs(origifa);
1910     return ifs;
1911 }
1912 
1913 #ifdef AF_INET6
1914 /*
1915  * Determines the prefix on BSD for IPv6 interfaces.
1916  */
1917 static int prefix(void *val, int size) {
1918     u_char *name = (u_char *)val;
1919     int byte, bit, prefix = 0;
1920 
1921     for (byte = 0; byte < size; byte++, prefix += 8)
1922         if (name[byte] != 0xff)
1923             break;
1924     if (byte == size)
1925         return prefix;
1926     for (bit = 7; bit != 0; bit--, prefix++)
1927         if (!(name[byte] & (1 << bit)))
1928             break;
1929     for (; bit != 0; bit--)
1930         if (name[byte] & (1 << bit))
1931             return (0);
1932     byte++;
1933     for (; byte < size; byte++)
1934         if (name[byte])
1935             return (0);
1936     return prefix;
1937 }
1938 
1939 /*
1940  * Enumerates and returns all IPv6 interfaces on BSD.
1941  */
1942 static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs) {
1943     struct ifaddrs *ifa, *origifa;
1944     struct sockaddr_in6 *sin6;
1945     struct in6_ifreq ifr6;
1946 
1947     if (getifaddrs(&origifa) != 0) {
1948         NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
1949                                      "getifaddrs() function failed");
1950         return ifs;
1951     }
1952 
1953     for (ifa = origifa; ifa != NULL; ifa = ifa->ifa_next) {
1954 
1955         // Skip non-AF_INET6 entries.
1956         if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family != AF_INET6)
1957             continue;
1958 
1959         memset(&ifr6, 0, sizeof(ifr6));
1960         strlcpy(ifr6.ifr_name, ifa->ifa_name, sizeof(ifr6.ifr_name));
1961         memcpy(&ifr6.ifr_addr, ifa->ifa_addr,
1962                MIN(sizeof(ifr6.ifr_addr), ifa->ifa_addr->sa_len));
1963 
1964         if (ioctl(sock, SIOCGIFNETMASK_IN6, (caddr_t)&ifr6) < 0) {
1965             NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
1966                                          "ioctl SIOCGIFNETMASK_IN6 failed");
1967             freeifaddrs(origifa);
1968             freeif(ifs);
1969             return NULL;
1970         }
1971 
1972         // Add to the list.
1973         sin6 = (struct sockaddr_in6 *)&ifr6.ifr_addr;
1974         ifs = addif(env, sock, ifa->ifa_name, ifs, ifa->ifa_addr, AF_INET6,
1975             (short)prefix(&sin6->sin6_addr, sizeof(struct in6_addr)));
1976 
1977         // If an exception occurred then free the list.
1978         if ((*env)->ExceptionOccurred(env)) {
1979             freeifaddrs(origifa);
1980             freeif(ifs);
1981             return NULL;
1982         }
1983     }
1984 
1985     // Free socket and ifaddrs buffer
1986     freeifaddrs(origifa);
1987     return ifs;
1988 }
1989 #endif
1990 
1991 static int getIndex(int sock, const char *name) {
1992 #ifdef __FreeBSD__
1993     // Try to get the interface index
1994     // (Not supported on Solaris 2.6 or 7)
1995     struct ifreq if2;
1996     memset((char *)&if2, 0, sizeof(if2));
1997     strncpy(if2.ifr_name, name, sizeof(if2.ifr_name) - 1);
1998 
1999     if (ioctl(sock, SIOCGIFINDEX, (char *)&if2) < 0) {
2000         return -1;
2001     }
2002 
2003     return if2.ifr_index;
2004 #else
2005     // Try to get the interface index using BSD specific if_nametoindex
2006     int index = if_nametoindex(name);
2007     return (index == 0) ? -1 : index;
2008 #endif
2009 }
2010 
2011 /*
2012  * Returns the IPv4 broadcast address of a named interface, if it exists.
2013  * Returns 0 if it doesn't have one.
2014  */
2015 static struct sockaddr *getBroadcast
2016   (JNIEnv *env, int sock, const char *ifname, struct sockaddr *brdcast_store)
2017 {
2018     struct sockaddr *ret = NULL;
2019     struct ifreq if2;
2020     memset((char *)&if2, 0, sizeof(if2));
2021     strncpy(if2.ifr_name, ifname, sizeof(if2.ifr_name) - 1);
2022 
2023     // Make sure the interface does have a broadcast address
2024     if (ioctl(sock, SIOCGIFFLAGS, (char *)&if2) < 0) {
2025         NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
2026                                      "ioctl SIOCGIFFLAGS failed");
2027         return ret;
2028     }
2029 
2030     if (if2.ifr_flags & IFF_BROADCAST) {
2031         // It does, let's retrieve it
2032         if (ioctl(sock, SIOCGIFBRDADDR, (char *)&if2) < 0) {
2033             NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
2034                                          "ioctl SIOCGIFBRDADDR failed");
2035             return ret;
2036         }
2037 
2038         ret = brdcast_store;
2039         memcpy(ret, &if2.ifr_broadaddr, sizeof(struct sockaddr));
2040     }
2041 
2042     return ret;
2043 }
2044 
2045 /*
2046  * Returns the IPv4 subnet prefix length (aka subnet mask) for the named
2047  * interface, if it has one, otherwise return -1.
2048  */
2049 static short getSubnet(JNIEnv *env, int sock, const char *ifname) {
2050     unsigned int mask;
2051     short ret;
2052     struct ifreq if2;
2053     memset((char *)&if2, 0, sizeof(if2));
2054     strncpy(if2.ifr_name, ifname, sizeof(if2.ifr_name) - 1);
2055 
2056     if (ioctl(sock, SIOCGIFNETMASK, (char *)&if2) < 0) {
2057         NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
2058                                      "ioctl SIOCGIFNETMASK failed");
2059         return -1;
2060     }
2061 
2062     mask = ntohl(((struct sockaddr_in*)&(if2.ifr_addr))->sin_addr.s_addr);
2063     ret = 0;
2064     while (mask) {
2065        mask <<= 1;
2066        ret++;
2067     }
2068 
2069     return ret;
2070 }
2071 
2072 /*
2073  * Gets the Hardware address (usually MAC address) for the named interface.
2074  * return puts the data in buf, and returns the length, in byte, of the
2075  * MAC address. Returns -1 if there is no hardware address on that interface.
2076  */
2077 static int getMacAddress
2078   (JNIEnv *env, int sock, const char* ifname, const struct in_addr* addr,
2079    unsigned char *buf)
2080 {
2081     struct ifaddrs *ifa0, *ifa;
2082     struct sockaddr *saddr;
2083     int i;
2084 
2085     // Grab the interface list
2086     if (!getifaddrs(&ifa0)) {
2087         // Cycle through the interfaces
2088         for (i = 0, ifa = ifa0; ifa != NULL; ifa = ifa->ifa_next, i++) {
2089             saddr = ifa->ifa_addr;
2090             // Link layer contains the MAC address
2091             if (saddr->sa_family == AF_LINK && !strcmp(ifname, ifa->ifa_name)) {
2092                 struct sockaddr_dl *sadl = (struct sockaddr_dl *) saddr;
2093                 // Check the address is the correct length
2094                 if (sadl->sdl_alen == ETHER_ADDR_LEN) {
2095                     memcpy(buf, (sadl->sdl_data + sadl->sdl_nlen), ETHER_ADDR_LEN);
2096                     freeifaddrs(ifa0);
2097                     return ETHER_ADDR_LEN;
2098                 }
2099             }
2100         }
2101         freeifaddrs(ifa0);
2102     }
2103 
2104     return -1;
2105 }
2106 
2107 static int getMTU(JNIEnv *env, int sock,  const char *ifname) {
2108     struct ifreq if2;
2109     memset((char *)&if2, 0, sizeof(if2));
2110     strncpy(if2.ifr_name, ifname, sizeof(if2.ifr_name) - 1);
2111 
2112     if (ioctl(sock, SIOCGIFMTU, (char *)&if2) < 0) {
2113         NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
2114                                      "ioctl SIOCGIFMTU failed");
2115         return -1;
2116     }
2117 
2118     return  if2.ifr_mtu;
2119 }
2120 
2121 static int getFlags(int sock, const char *ifname, int *flags) {
2122     struct ifreq if2;
2123     int ret = -1;
2124     memset((char *)&if2, 0, sizeof(if2));
2125     strncpy(if2.ifr_name, ifname, sizeof(if2.ifr_name) - 1);
2126 
2127     if (ioctl(sock, SIOCGIFFLAGS, (char *)&if2) < 0) {
2128         return -1;
2129     }
2130 
2131     if (sizeof(if2.ifr_flags) == sizeof(short)) {
2132         *flags = (if2.ifr_flags & 0xffff);
2133     } else {
2134         *flags = if2.ifr_flags;
2135     }
2136     return 0;
2137 }
2138 #endif /* __ALLBSD_SOURCE__ */