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