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