< prev index next >

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

Print this page
rev 15429 : Further networkinterface.c cleanups


  63 #include <net/if_var.h>
  64 #include <net/if_dl.h>
  65 #include <netinet/in_var.h>
  66 #include <ifaddrs.h>
  67 #endif
  68 #endif
  69 
  70 #include "jvm.h"
  71 #include "jni_util.h"
  72 #include "net_util.h"
  73 
  74 #if defined(__linux__)
  75     #define _PATH_PROCNET_IFINET6 "/proc/net/if_inet6"
  76 #elif defined(__solaris__)
  77     #ifndef SIOCGLIFHWADDR
  78         #define SIOCGLIFHWADDR _IOWR('i', 192, struct lifreq)
  79     #endif
  80     #define DEV_PREFIX "/dev/"
  81 #endif
  82 






  83 #define CHECKED_MALLOC3(_pointer, _type, _size) \
  84     do { \
  85         _pointer = (_type)malloc(_size); \
  86         if (_pointer == NULL) { \
  87             JNU_ThrowOutOfMemoryError(env, "Native heap allocation failed"); \
  88             return ifs; /* return untouched list */ \
  89         } \
  90     } while(0)
  91 
  92 typedef struct _netaddr  {
  93     struct sockaddr *addr;
  94     struct sockaddr *brdcast;
  95     short mask;
  96     int family; /* to make searches simple */
  97     struct _netaddr *next;
  98 } netaddr;
  99 
 100 typedef struct _netif {
 101     char *name;
 102     int index;


 141 static netif  *enumIPv4Interfaces(JNIEnv *env, int sock, netif *ifs);
 142 
 143 #if defined(AF_INET6)
 144 static netif  *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs);
 145 #endif
 146 
 147 static netif  *addif(JNIEnv *env, int sock, const char *if_name, netif *ifs,
 148                      struct sockaddr *ifr_addrP,
 149                      struct sockaddr *ifr_broadaddrP,
 150                      int family, short prefix);
 151 static void    freeif(netif *ifs);
 152 
 153 static int     openSocket(JNIEnv *env, int proto);
 154 static int     openSocketWithFallback(JNIEnv *env, const char *ifname);
 155 
 156 static short   translateIPv4AddressToPrefix(struct sockaddr_in *addr);
 157 static short   translateIPv6AddressToPrefix(struct sockaddr_in6 *addr);
 158 
 159 static int     getIndex(int sock, const char *ifname);
 160 static int     getFlags(int sock, const char *ifname, int *flags);
 161 static int     getMacAddress(JNIEnv *env, int sock, const char *ifname,
 162                              const struct in_addr *addr, unsigned char *buf);
 163 static int     getMTU(JNIEnv *env, int sock, const char *ifname);
 164 
 165 #if defined(__solaris__)
 166 static int     getMacFromDevice(JNIEnv *env, const char *ifname,
 167                                 unsigned char *retbuf);
 168 #endif
 169 
 170 /******************* Java entry points *****************************/
 171 
 172 /*
 173  * Class:     java_net_NetworkInterface
 174  * Method:    init
 175  * Signature: ()V
 176  */
 177 JNIEXPORT void JNICALL Java_java_net_NetworkInterface_init
 178   (JNIEnv *env, jclass cls)
 179 {
 180     ni_class = (*env)->FindClass(env, "java/net/NetworkInterface");
 181     CHECK_NULL(ni_class);


 220     CHECK_NULL(ni_ib4maskID);
 221     ni_defaultIndexID = (*env)->GetStaticFieldID(env, ni_class, "defaultIndex",
 222                                                  "I");
 223     CHECK_NULL(ni_defaultIndexID);
 224     initInetAddressIDs(env);
 225 }
 226 
 227 /*
 228  * Class:     java_net_NetworkInterface
 229  * Method:    getByName0
 230  * Signature: (Ljava/lang/String;)Ljava/net/NetworkInterface;
 231  */
 232 JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByName0
 233   (JNIEnv *env, jclass cls, jstring name)
 234 {
 235     netif *ifs, *curr;
 236     jboolean isCopy;
 237     const char* name_utf;
 238     jobject obj = NULL;
 239 
 240     ifs = enumInterfaces(env);
 241     if (ifs == NULL) {


 242         return NULL;
 243     }
 244 
 245     name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
 246     if (name_utf == NULL) {
 247        if (!(*env)->ExceptionCheck(env))
 248            JNU_ThrowOutOfMemoryError(env, NULL);
 249        freeif(ifs);
 250        return NULL;
 251     }
 252 
 253     // Search the list of interface based on name





 254     curr = ifs;
 255     while (curr != NULL) {
 256         if (strcmp(name_utf, curr->name) == 0) {
 257             break;
 258         }
 259         curr = curr->next;
 260     }
 261 
 262     // if found create a NetworkInterface
 263     if (curr != NULL) {
 264         obj = createNetworkInterface(env, curr);
 265     }
 266 
 267     // release the UTF string and interface list
 268     (*env)->ReleaseStringUTFChars(env, name, name_utf);
 269     freeif(ifs);
 270 
 271     return obj;
 272 }
 273 
 274 /*
 275  * Class:     java_net_NetworkInterface
 276  * Method:    getByIndex0
 277  * Signature: (Ljava/lang/String;)Ljava/net/NetworkInterface;
 278  */
 279 JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByIndex0
 280   (JNIEnv *env, jclass cls, jint index)
 281 {
 282     netif *ifs, *curr;
 283     jobject obj = NULL;
 284 
 285     if (index <= 0) {
 286         return NULL;
 287     }

 288     ifs = enumInterfaces(env);
 289     if (ifs == NULL) {
 290         return NULL;
 291     }
 292 
 293     // Search the list of interface based on index
 294     curr = ifs;
 295     while (curr != NULL) {
 296         if (index == curr->index) {
 297             break;
 298         }
 299         curr = curr->next;
 300     }
 301 
 302     // if found create a NetworkInterface
 303     if (curr != NULL) {
 304         obj = createNetworkInterface(env, curr);
 305     }
 306 

 307     freeif(ifs);

 308     return obj;
 309 }
 310 
 311 /*
 312  * Class:     java_net_NetworkInterface
 313  * Method:    getByInetAddress0
 314  * Signature: (Ljava/net/InetAddress;)Ljava/net/NetworkInterface;
 315  */
 316 JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByInetAddress0
 317   (JNIEnv *env, jclass cls, jobject iaObj)
 318 {
 319     netif *ifs, *curr;
 320 
 321 #if defined(AF_INET6)
 322     int family = (getInetAddress_family(env, iaObj) == IPv4) ? AF_INET : AF_INET6;
 323 #else
 324     int family =  AF_INET;
 325 #endif
 326 
 327     jobject obj = NULL;
 328     jboolean match = JNI_FALSE;
 329 
 330     ifs = enumInterfaces(env);
 331     if (ifs == NULL) {
 332         return NULL;
 333     }
 334 
 335     curr = ifs;
 336     while (curr != NULL) {
 337         netaddr *addrP = curr->addr;
 338 
 339         // iterate through each address on the interface
 340         while (addrP != NULL) {
 341 
 342             if (family == addrP->family) {
 343                 if (family == AF_INET) {
 344                     int address1 = htonl(
 345                         ((struct sockaddr_in*)addrP->addr)->sin_addr.s_addr);
 346                     int address2 = getInetAddress_addr(env, iaObj);
 347 
 348                     if (address1 == address2) {
 349                         match = JNI_TRUE;
 350                         break;
 351                     }
 352                 }
 353 
 354 #if defined(AF_INET6)
 355                 if (family == AF_INET6) {
 356                     jbyte *bytes = (jbyte *)&(
 357                         ((struct sockaddr_in6*)addrP->addr)->sin6_addr);
 358                     jbyte caddr[16];
 359                     int i;
 360                     getInet6Address_ipaddress(env, iaObj, (char *)caddr);
 361                     i = 0;
 362                     while (i < 16) {
 363                         if (caddr[i] != bytes[i]) {
 364                             break;
 365                         }
 366                         i++;
 367                     }
 368                     if (i >= 16) {
 369                         match = JNI_TRUE;
 370                         break;
 371                     }
 372                 }
 373 #endif
 374             }
 375 
 376             if (match) {
 377                 break;
 378             }
 379             addrP = addrP->next;
 380         }
 381 
 382         if (match) {
 383             break;
 384         }
 385         curr = curr->next;
 386     }
 387 
 388     // if found create a NetworkInterface
 389     if (match) {
 390         obj = createNetworkInterface(env, curr);
 391     }
 392 

 393     freeif(ifs);

 394     return obj;
 395 }
 396 
 397 /*
 398  * Class:     java_net_NetworkInterface
 399  * Method:    getAll
 400  * Signature: ()[Ljava/net/NetworkInterface;
 401  */
 402 JNIEXPORT jobjectArray JNICALL Java_java_net_NetworkInterface_getAll
 403   (JNIEnv *env, jclass cls)
 404 {
 405     netif *ifs, *curr;
 406     jobjectArray netIFArr;
 407     jint arr_index, ifCount;
 408 
 409     ifs = enumInterfaces(env);
 410     if (ifs == NULL) {
 411         return NULL;
 412     }
 413 
 414     // count the interface
 415     ifCount = 0;
 416     curr = ifs;
 417     while (curr != NULL) {
 418         ifCount++;
 419         curr = curr->next;
 420     }
 421 
 422     // allocate a NetworkInterface array
 423     netIFArr = (*env)->NewObjectArray(env, ifCount, cls, NULL);
 424     if (netIFArr == NULL) {
 425         freeif(ifs);
 426         return NULL;
 427     }
 428 
 429     // Iterate through the interfaces, create a NetworkInterface instance
 430     // for each array element and populate the object.
 431     curr = ifs;
 432     arr_index = 0;
 433     while (curr != NULL) {
 434         jobject netifObj;
 435 
 436         netifObj = createNetworkInterface(env, curr);
 437         if (netifObj == NULL) {
 438             freeif(ifs);
 439             return NULL;
 440         }
 441 
 442         // put the NetworkInterface into the array
 443         (*env)->SetObjectArrayElement(env, netIFArr, arr_index++, netifObj);
 444 
 445         curr = curr->next;
 446     }
 447 

 448     freeif(ifs);

 449     return netIFArr;
 450 }
 451 
 452 /*
 453  * Class:     java_net_NetworkInterface
 454  * Method:    isUp0
 455  * Signature: (Ljava/lang/String;I)Z
 456  */
 457 JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isUp0
 458   (JNIEnv *env, jclass cls, jstring name, jint index)
 459 {
 460     int ret = getFlags0(env, name);
 461     return ((ret & IFF_UP) && (ret & IFF_RUNNING)) ? JNI_TRUE :  JNI_FALSE;
 462 }
 463 
 464 /*
 465  * Class:     java_net_NetworkInterface
 466  * Method:    isP2P0
 467  * Signature: (Ljava/lang/String;I)Z
 468  */


 494   (JNIEnv *env, jclass cls, jstring name, jint index)
 495 {
 496     int ret = getFlags0(env, name);
 497     return (ret & IFF_MULTICAST) ? JNI_TRUE :  JNI_FALSE;
 498 }
 499 
 500 /*
 501  * Class:     java_net_NetworkInterface
 502  * Method:    getMacAddr0
 503  * Signature: ([bLjava/lang/String;I)[b
 504  */
 505 JNIEXPORT jbyteArray JNICALL Java_java_net_NetworkInterface_getMacAddr0
 506   (JNIEnv *env, jclass cls, jbyteArray addrArray, jstring name, jint index)
 507 {
 508     jint addr;
 509     jbyte caddr[4];
 510     struct in_addr iaddr;
 511     jbyteArray ret = NULL;
 512     unsigned char mac[16];
 513     int len;
 514     int sock;
 515     jboolean isCopy;
 516     const char* name_utf;
 517 
 518     name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
 519     if (name_utf == NULL) {
 520        if (!(*env)->ExceptionCheck(env))
 521            JNU_ThrowOutOfMemoryError(env, NULL);
 522        return NULL;
 523     }
 524     if ((sock = openSocketWithFallback(env, name_utf)) < 0) {
 525        (*env)->ReleaseStringUTFChars(env, name, name_utf);
 526        return NULL;


 527     }
 528 
 529     if (!IS_NULL(addrArray)) {
 530        (*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr);
 531        addr = ((caddr[0]<<24) & 0xff000000);
 532        addr |= ((caddr[1] <<16) & 0xff0000);
 533        addr |= ((caddr[2] <<8) & 0xff00);
 534        addr |= (caddr[3] & 0xff);
 535        iaddr.s_addr = htonl(addr);
 536        len = getMacAddress(env, sock, name_utf, &iaddr, mac);
 537     } else {
 538        len = getMacAddress(env, sock, name_utf, NULL, mac);
 539     }

 540     if (len > 0) {
 541        ret = (*env)->NewByteArray(env, len);
 542        if (IS_NULL(ret)) {
 543           /* we may have memory to free at the end of this */
 544           goto fexit;
 545        }
 546        (*env)->SetByteArrayRegion(env, ret, 0, len, (jbyte *)(mac));
 547     }
 548  fexit:
 549    // release the UTF string and interface list
 550    (*env)->ReleaseStringUTFChars(env, name, name_utf);
 551 
 552    close(sock);
 553    return ret;


 554 }
 555 
 556 /*
 557  * Class:       java_net_NetworkInterface
 558  * Method:      getMTU0
 559  * Signature:   ([bLjava/lang/String;I)I
 560  */
 561 JNIEXPORT jint JNICALL Java_java_net_NetworkInterface_getMTU0
 562   (JNIEnv *env, jclass cls, jstring name, jint index)
 563 {
 564     jboolean isCopy;
 565     int ret = -1;
 566     int sock;
 567     const char* name_utf = NULL;
 568 
 569     if (name != NULL) {
 570         name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
 571     } else {
 572         JNU_ThrowNullPointerException(env, "network interface name is NULL");
 573         return ret;
 574     }

 575     if (name_utf == NULL) {
 576        if (!(*env)->ExceptionCheck(env))
 577            JNU_ThrowOutOfMemoryError(env, NULL);
 578        return ret;
 579     }
 580 
 581     if ((sock = openSocketWithFallback(env, name_utf)) < 0) {
 582        (*env)->ReleaseStringUTFChars(env, name, name_utf);
 583        return JNI_FALSE;
 584     }
 585 
 586     ret = getMTU(env, sock, name_utf);
 587 
 588     (*env)->ReleaseStringUTFChars(env, name, name_utf);
 589 
 590     close(sock);
 591     return ret;
 592 }
 593 
 594 /*** Private methods definitions ****/
 595 
 596 static int getFlags0(JNIEnv *env, jstring name) {
 597     jboolean isCopy;
 598     int ret, sock;
 599     const char* name_utf;
 600     int flags = 0;
 601 
 602     if (name != NULL) {
 603         name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
 604     } else {
 605         JNU_ThrowNullPointerException(env, "network interface name is NULL");
 606         return -1;
 607     }
 608 
 609     if (name_utf == NULL) {
 610        if (!(*env)->ExceptionCheck(env))
 611            JNU_ThrowOutOfMemoryError(env, NULL);
 612        return -1;
 613     }
 614     if ((sock = openSocketWithFallback(env, name_utf)) < 0) {
 615         (*env)->ReleaseStringUTFChars(env, name, name_utf);
 616         return -1;
 617     }
 618 
 619     ret = getFlags(sock, name_utf, &flags);
 620 


 631 }
 632 
 633 /*
 634  * Creates a NetworkInterface object, populates the name, the index, and
 635  * populates the InetAddress array based on the IP addresses for this
 636  * interface.
 637  */
 638 static jobject createNetworkInterface(JNIEnv *env, netif *ifs) {
 639     jobject netifObj;
 640     jobject name;
 641     jobjectArray addrArr;
 642     jobjectArray bindArr;
 643     jobjectArray childArr;
 644     netaddr *addrs;
 645     jint addr_index, addr_count, bind_index;
 646     jint child_count, child_index;
 647     netaddr *addrP;
 648     netif *childP;
 649     jobject tmp;
 650 
 651     // Create a NetworkInterface object and populate it
 652     netifObj = (*env)->NewObject(env, ni_class, ni_ctrID);
 653     CHECK_NULL_RETURN(netifObj, NULL);
 654     name = (*env)->NewStringUTF(env, ifs->name);
 655     CHECK_NULL_RETURN(name, NULL);
 656     (*env)->SetObjectField(env, netifObj, ni_nameID, name);
 657     (*env)->SetObjectField(env, netifObj, ni_descID, name);
 658     (*env)->SetIntField(env, netifObj, ni_indexID, ifs->index);
 659     (*env)->SetBooleanField(env, netifObj, ni_virutalID,
 660                             ifs->virtual ? JNI_TRUE : JNI_FALSE);
 661 
 662     //Count the number of address on this interface
 663     addr_count = 0;
 664     addrP = ifs->addr;
 665     while (addrP != NULL) {
 666         addr_count++;
 667         addrP = addrP->next;
 668     }
 669 
 670     // Create the array of InetAddresses
 671     addrArr = (*env)->NewObjectArray(env, addr_count, ia_class, NULL);
 672     if (addrArr == NULL) {
 673         return NULL;
 674     }
 675 
 676     bindArr = (*env)->NewObjectArray(env, addr_count, ni_ibcls, NULL);
 677     if (bindArr == NULL) {
 678        return NULL;
 679     }
 680     addrP = ifs->addr;
 681     addr_index = 0;
 682     bind_index = 0;
 683     while (addrP != NULL) {
 684         jobject iaObj = NULL;
 685         jobject ibObj = NULL;
 686 
 687         if (addrP->family == AF_INET) {
 688             iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
 689             if (iaObj) {
 690                  setInetAddress_addr(env, iaObj, htonl(


 695             ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID);
 696             if (ibObj) {
 697                  (*env)->SetObjectField(env, ibObj, ni_ibaddressID, iaObj);
 698                  if (addrP->brdcast) {
 699                     jobject ia2Obj = NULL;
 700                     ia2Obj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
 701                     if (ia2Obj) {
 702                        setInetAddress_addr(env, ia2Obj, htonl(
 703                            ((struct sockaddr_in*)addrP->brdcast)->sin_addr.s_addr));
 704                        (*env)->SetObjectField(env, ibObj, ni_ib4broadcastID, ia2Obj);
 705                     } else {
 706                         return NULL;
 707                     }
 708                  }
 709                  (*env)->SetShortField(env, ibObj, ni_ib4maskID, addrP->mask);
 710                  (*env)->SetObjectArrayElement(env, bindArr, bind_index++, ibObj);
 711             } else {
 712                 return NULL;
 713             }
 714         }
 715 
 716 #if defined(AF_INET6)
 717         if (addrP->family == AF_INET6) {
 718             int scope=0;
 719             iaObj = (*env)->NewObject(env, ia6_class, ia6_ctrID);
 720             if (iaObj) {
 721                 jboolean ret = setInet6Address_ipaddress(env, iaObj,
 722                     (char *)&(((struct sockaddr_in6*)addrP->addr)->sin6_addr));
 723                 if (ret == JNI_FALSE) {
 724                     return NULL;
 725                 }
 726 
 727                 scope = ((struct sockaddr_in6*)addrP->addr)->sin6_scope_id;
 728 
 729                 if (scope != 0) { /* zero is default value, no need to set */
 730                     setInet6Address_scopeid(env, iaObj, scope);
 731                     setInet6Address_scopeifname(env, iaObj, netifObj);
 732                 }
 733             } else {
 734                 return NULL;
 735             }
 736             ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID);
 737             if (ibObj) {
 738                 (*env)->SetObjectField(env, ibObj, ni_ibaddressID, iaObj);
 739                 (*env)->SetShortField(env, ibObj, ni_ib4maskID, addrP->mask);
 740                 (*env)->SetObjectArrayElement(env, bindArr, bind_index++, ibObj);
 741             } else {
 742                 return NULL;
 743             }
 744         }
 745 #endif
 746 
 747         (*env)->SetObjectArrayElement(env, addrArr, addr_index++, iaObj);
 748         addrP = addrP->next;
 749     }
 750 
 751     // See if there is any virtual interface attached to this one.
 752     child_count = 0;
 753     childP = ifs->childs;
 754     while (childP) {
 755         child_count++;
 756         childP = childP->next;
 757     }
 758 
 759     childArr = (*env)->NewObjectArray(env, child_count, ni_class, NULL);
 760     if (childArr == NULL) {
 761         return NULL;
 762     }
 763 
 764     // Create the NetworkInterface instances for the sub-interfaces as well.
 765     child_index = 0;
 766     childP = ifs->childs;
 767     while(childP) {
 768       tmp = createNetworkInterface(env, childP);
 769       if (tmp == NULL) {
 770          return NULL;
 771       }
 772       (*env)->SetObjectField(env, tmp, ni_parentID, netifObj);
 773       (*env)->SetObjectArrayElement(env, childArr, child_index++, tmp);
 774       childP = childP->next;
 775     }
 776     (*env)->SetObjectField(env, netifObj, ni_addrsID, addrArr);
 777     (*env)->SetObjectField(env, netifObj, ni_bindsID, bindArr);
 778     (*env)->SetObjectField(env, netifObj, ni_childsID, childArr);
 779 
 780     // return the NetworkInterface
 781     return netifObj;
 782 }
 783 
 784 /*
 785  * Enumerates all interfaces
 786  */
 787 static netif *enumInterfaces(JNIEnv *env) {
 788     netif *ifs;
 789     int sock;
 790 
 791     // Enumerate IPv4 addresses
 792     sock = openSocket(env, AF_INET);
 793     if (sock < 0 && (*env)->ExceptionOccurred(env)) {
 794         return NULL;
 795     }
 796 

 797     ifs = enumIPv4Interfaces(env, sock, NULL);
 798     close(sock);
 799 

 800     if (ifs == NULL && (*env)->ExceptionOccurred(env)) {
 801         return NULL;
 802     }
 803 
 804     // return partial list if an exception occurs in the middle of process ???
 805 
 806     // If IPv6 is available then enumerate IPv6 addresses.
 807 #if defined(AF_INET6)
 808 
 809         // User can disable ipv6 explicitly by -Djava.net.preferIPv4Stack=true,
 810         // so we have to call ipv6_available()
 811         if (ipv6_available()) {





 812 
 813            sock = openSocket(env, AF_INET6);
 814            if (sock < 0 && (*env)->ExceptionOccurred(env)) {
 815                freeif(ifs);
 816                return NULL;
 817            }
 818 
 819            ifs = enumIPv6Interfaces(env, sock, ifs);
 820            close(sock);
 821 
 822            if ((*env)->ExceptionOccurred(env)) {
 823               freeif(ifs);
 824               return NULL;
 825            }
 826 
 827        }




 828 #endif
 829 
 830     return ifs;
 831 }
 832 
 833 /*
 834  * Frees an interface list (including any attached addresses).
 835  */
 836 static void freeif(netif *ifs) {
 837     netif *currif = ifs;
 838     netif *child = NULL;
 839 
 840     while (currif != NULL) {
 841         netaddr *addrP = currif->addr;
 842         while (addrP != NULL) {
 843             netaddr *next = addrP->next;
 844             free(addrP);
 845             addrP = next;
 846         }
 847 
 848         // Don't forget to free the sub-interfaces.
 849         if (currif->childs != NULL) {
 850             freeif(currif->childs);
 851         }
 852 
 853         ifs = currif->next;
 854         free(currif);
 855         currif = ifs;
 856     }
 857 }
 858 
 859 static netif *addif(JNIEnv *env, int sock, const char *if_name, netif *ifs,
 860                     struct sockaddr *ifr_addrP,
 861                     struct sockaddr *ifr_broadaddrP,
 862                     int family, short prefix)
 863 {
 864     netif *currif = ifs, *parent;
 865     netaddr *addrP;
 866 
 867 #ifdef LIFNAMSIZ
 868     int ifnam_size = LIFNAMSIZ;
 869     char name[LIFNAMSIZ], vname[LIFNAMSIZ];
 870 #else
 871     int ifnam_size = IFNAMSIZ;
 872     char name[IFNAMSIZ], vname[IFNAMSIZ];
 873 #endif
 874 
 875     char *name_colonP;
 876     int isVirtual = 0;
 877     int addr_size;
 878     int flags = 0;
 879 
 880     // If the interface name is a logical interface then we remove the unit
 881     // number so that we have the physical interface (eg: hme0:1 -> hme0).
 882     // NetworkInterface currently doesn't have any concept of physical vs.
 883     // logical interfaces.
 884     strncpy(name, if_name, ifnam_size);
 885     name[ifnam_size - 1] = '\0';
 886     *vname = 0;
 887 
 888     // Create and populate the netaddr node. If allocation fails
 889     // return an un-updated list.
 890 
 891     // Allocate for addr and brdcast at once
 892 
 893 #if defined(AF_INET6)
 894     addr_size = (family == AF_INET) ? sizeof(struct sockaddr_in)
 895                                     : sizeof(struct sockaddr_in6);
 896 #else
 897     addr_size = sizeof(struct sockaddr_in);
 898 #endif
 899 
 900     CHECKED_MALLOC3(addrP, netaddr *, sizeof(netaddr) + 2 * addr_size);
 901     addrP->addr = (struct sockaddr *)((char *)addrP + sizeof(netaddr));
 902     memcpy(addrP->addr, ifr_addrP, addr_size);
 903 
 904     addrP->family = family;
 905     addrP->mask = prefix;
 906     addrP->next = 0;
 907 
 908     // for IPv4 add broadcast address
 909     if (family == AF_INET && ifr_broadaddrP != NULL) {
 910         addrP->brdcast = (struct sockaddr *)
 911                              ((char *)addrP + sizeof(netaddr) + addr_size);
 912         memcpy(addrP->brdcast, ifr_broadaddrP, addr_size);
 913     } else {
 914         addrP->brdcast = NULL;
 915     }
 916 
 917     // Deal with virtual interface with colon notation e.g. eth0:1
 918     name_colonP = strchr(name, ':');
 919     if (name_colonP != NULL) {

 920         // This is a virtual interface. If we are able to access the parent
 921         // we need to create a new entry if it doesn't exist yet *and* update
 922         // the 'parent' interface with the new records.
 923         *name_colonP = 0;
 924         if (getFlags(sock, name, &flags) < 0 || flags < 0) {
 925              // failed to access parent interface do not create parent.
 926              // We are a virtual interface with no parent.
 927              isVirtual = 1;
 928              *name_colonP = ':';
 929         } else {
 930              // Got access to parent, so create it if necessary.
 931              // Save original name to vname and truncate name by ':'
 932              memcpy(vname, name, sizeof(vname) );
 933              vname[name_colonP - name] = ':';
 934         }
 935     }
 936 
 937     // Check if this is a "new" interface. Use the interface name for
 938     // matching because index isn't supported on Solaris 2.6 & 7.
 939     while (currif != NULL) {
 940         if (strcmp(name, currif->name) == 0) {
 941             break;
 942         }
 943         currif = currif->next;
 944     }
 945 
 946     // If "new" then create an netif structure and insert it into the list.
 947     if (currif == NULL) {
 948          CHECKED_MALLOC3(currif, netif *, sizeof(netif) + ifnam_size);
 949          currif->name = (char *)currif + sizeof(netif);
 950          strncpy(currif->name, name, ifnam_size);
 951          currif->name[ifnam_size - 1] = '\0';
 952          currif->index = getIndex(sock, name);
 953          currif->addr = NULL;
 954          currif->childs = NULL;
 955          currif->virtual = isVirtual;
 956          currif->next = ifs;
 957          ifs = currif;
 958     }
 959 
 960     // Finally insert the address on the interface
 961     addrP->next = currif->addr;
 962     currif->addr = addrP;
 963 
 964     parent = currif;
 965 
 966     // Deal with the virtual interface now.
 967     if (vname[0]) {
 968         netaddr *tmpaddr;
 969 
 970         currif = parent->childs;
 971 
 972         while (currif != NULL) {
 973             if (strcmp(vname, currif->name) == 0) {
 974                 break;
 975             }
 976             currif = currif->next;
 977         }
 978 
 979         if (currif == NULL) {
 980             CHECKED_MALLOC3(currif, netif *, sizeof(netif) + ifnam_size);
 981             currif->name = (char *)currif + sizeof(netif);
 982             strncpy(currif->name, vname, ifnam_size);
 983             currif->name[ifnam_size - 1] = '\0';
 984             currif->index = getIndex(sock, vname);
 985             currif->addr = NULL;
 986             // Need to duplicate the addr entry?
 987             currif->virtual = 1;
 988             currif->childs = NULL;
 989             currif->next = parent->childs;
 990             parent->childs = currif;
 991         }
 992 
 993         CHECKED_MALLOC3(tmpaddr, netaddr *, sizeof(netaddr) + 2 * addr_size);
 994         memcpy(tmpaddr, addrP, sizeof(netaddr));
 995         if (addrP->addr != NULL) {
 996             tmpaddr->addr = (struct sockaddr *)
 997                 ((char*)tmpaddr + sizeof(netaddr));
 998             memcpy(tmpaddr->addr, addrP->addr, addr_size);
 999         }
1000 
1001         if (addrP->brdcast != NULL) {
1002             tmpaddr->brdcast = (struct sockaddr *)
1003                 ((char *)tmpaddr + sizeof(netaddr) + addr_size);
1004             memcpy(tmpaddr->brdcast, addrP->brdcast, addr_size);
1005         }
1006 


1133             (env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGIFCONF) failed");
1134         return ifs;
1135     }
1136 
1137     // call SIOCGIFCONF to enumerate the interfaces
1138     CHECKED_MALLOC3(buf, char *, ifc.ifc_len);
1139     ifc.ifc_buf = buf;
1140     if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
1141         JNU_ThrowByNameWithMessageAndLastError
1142             (env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGIFCONF) failed");
1143         free(buf);
1144         return ifs;
1145     }
1146 
1147     // iterate through each interface
1148     ifreqP = ifc.ifc_req;
1149     for (i = 0; i < ifc.ifc_len / sizeof(struct ifreq); i++, ifreqP++) {
1150         struct sockaddr addr, broadaddr, *broadaddrP = NULL;
1151         short prefix = 0;
1152 
1153         // ignore non IPv4 interfaces
1154         if (ifreqP->ifr_addr.sa_family != AF_INET) {
1155             continue;
1156         }
1157 
1158         // save socket address
1159         memcpy(&addr, &(ifreqP->ifr_addr), sizeof(struct sockaddr));
1160 
1161         // determine broadcast address, if applicable
1162         if ((ioctl(sock, SIOCGIFFLAGS, ifreqP) == 0) &&
1163             ifreqP->ifr_flags & IFF_BROADCAST) {
1164 
1165             // restore socket address to ifreqP
1166             memcpy(&(ifreqP->ifr_addr), &addr, sizeof(struct sockaddr));
1167 
1168             if (ioctl(sock, SIOCGIFBRDADDR, ifreqP) == 0) {
1169                 memcpy(&broadaddr, &(ifreqP->ifr_broadaddr),
1170                        sizeof(struct sockaddr));
1171                 broadaddrP = &broadaddr;
1172             }
1173         }


1247  * Try to get the interface index.
1248  */
1249 static int getIndex(int sock, const char *name) {
1250     struct ifreq if2;
1251     memset((char *)&if2, 0, sizeof(if2));
1252     strncpy(if2.ifr_name, name, sizeof(if2.ifr_name) - 1);
1253 
1254     if (ioctl(sock, SIOCGIFINDEX, (char *)&if2) < 0) {
1255         return -1;
1256     }
1257 
1258     return if2.ifr_ifindex;
1259 }
1260 
1261 /*
1262  * Gets the Hardware address (usually MAC address) for the named interface.
1263  * On return puts the data in buf, and returns the length, in byte, of the
1264  * MAC address. Returns -1 if there is no hardware address on that interface.
1265  */
1266 static int getMacAddress
1267   (JNIEnv *env, int sock, const char *ifname, const struct in_addr *addr,
1268    unsigned char *buf)
1269 {
1270     static struct ifreq ifr;
1271     int i;





1272     memset((char *)&ifr, 0, sizeof(ifr));
1273     strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
1274     if (ioctl(sock, SIOCGIFHWADDR, &ifr) < 0) {
1275         JNU_ThrowByNameWithMessageAndLastError
1276             (env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGIFHWADDR) failed");

1277         return -1;
1278     }
1279 

1280     memcpy(buf, &ifr.ifr_hwaddr.sa_data, IFHWADDRLEN);
1281 
1282     // all bytes to 0 means no hardware address
1283     for (i = 0; i < IFHWADDRLEN; i++) {
1284         if (buf[i] != 0)
1285             return IFHWADDRLEN;
1286     }
1287 
1288     return -1;
1289 }
1290 
1291 static int getMTU(JNIEnv *env, int sock, const char *ifname) {
1292     struct ifreq if2;
1293     memset((char *)&if2, 0, sizeof(if2));
1294     strncpy(if2.ifr_name, ifname, sizeof(if2.ifr_name) - 1);
1295 
1296     if (ioctl(sock, SIOCGIFMTU, (char *)&if2) < 0) {
1297         JNU_ThrowByNameWithMessageAndLastError
1298             (env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGIFMTU) failed");
1299         return -1;


1371     }
1372 
1373     // call CSIOCGIFCONF instead of SIOCGIFCONF where interface
1374     // records will always have sizeof(struct ifreq) length.
1375     // Be aware that only IPv4 data is complete this way.
1376     CHECKED_MALLOC3(buf, char *, ifc.ifc_len);
1377     ifc.ifc_buf = buf;
1378     if (ioctl(sock, CSIOCGIFCONF, (char *)&ifc) < 0) {
1379         JNU_ThrowByNameWithMessageAndLastError
1380             (env, JNU_JAVANETPKG "SocketException", "ioctl(CSIOCGIFCONF) failed");
1381         free(buf);
1382         return ifs;
1383     }
1384 
1385     // iterate through each interface
1386     ifreqP = ifc.ifc_req;
1387     for (i = 0; i < ifc.ifc_len / sizeof(struct ifreq); i++, ifreqP++) {
1388         struct sockaddr addr, broadaddr, *broadaddrP = NULL;
1389         short prefix = 0;
1390 
1391         // ignore non IPv4 interfaces
1392         if (ifreqP->ifr_addr.sa_family != AF_INET) {
1393             continue;
1394         }
1395 
1396         // save socket address
1397         memcpy(&addr, &(ifreqP->ifr_addr), sizeof(struct sockaddr));
1398 
1399         // determine broadcast address, if applicable
1400         if ((ioctl(sock, SIOCGIFFLAGS, ifreqP) == 0) &&
1401             ifreqP->ifr_flags & IFF_BROADCAST) {
1402 
1403             // restore socket address to ifreqP
1404             memcpy(&(ifreqP->ifr_addr), &addr, sizeof(struct sockaddr));
1405 
1406             if (ioctl(sock, SIOCGIFBRDADDR, ifreqP) == 0) {
1407                 memcpy(&broadaddr, &(ifreqP->ifr_broadaddr),
1408                        sizeof(struct sockaddr));
1409                 broadaddrP = &broadaddr;
1410             }
1411         }


1427         if ((*env)->ExceptionOccurred(env)) {
1428             free(buf);
1429             freeif(ifs);
1430             return NULL;
1431         }
1432     }
1433 
1434     // free buffer
1435     free(buf);
1436     return ifs;
1437 }
1438 
1439 #if defined(AF_INET6)
1440 
1441 /*
1442  * Enumerates and returns all IPv6 interfaces on AIX.
1443  */
1444 static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs) {
1445     struct ifconf ifc;
1446     struct ifreq *ifreqP;
1447     char *buf;
1448 
1449     // call SIOCGSIZIFCONF to get size for SIOCGIFCONF buffer
1450     if (ioctl(sock, SIOCGSIZIFCONF, &(ifc.ifc_len)) < 0) {
1451         JNU_ThrowByNameWithMessageAndLastError
1452             (env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGSIZIFCONF) failed");
1453         return ifs;
1454     }
1455 
1456     // call SIOCGIFCONF to enumerate the interfaces
1457     CHECKED_MALLOC3(buf, char *, ifc.ifc_len);
1458     ifc.ifc_buf = buf;
1459     if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
1460         JNU_ThrowByNameWithMessageAndLastError
1461             (env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGIFCONF) failed");
1462         free(buf);
1463         return ifs;
1464     }
1465 
1466     // iterate through each interface
1467     char *cp = (char *)ifc.ifc_req;
1468     char *cplimit = cp + ifc.ifc_len;

1469 
1470     for (; cp < cplimit;
1471          cp += (sizeof(ifreqP->ifr_name) +
1472                 MAX((ifreqP->ifr_addr).sa_len, sizeof(ifreqP->ifr_addr))))
1473     {
1474         ifreqP = (struct ifreq *)cp;
1475         short prefix = 0;
1476 
1477         // ignore non IPv6 interfaces
1478         if (ifreqP->ifr_addr.sa_family != AF_INET6) {
1479             continue;
1480         }
1481 
1482         // determine netmask
1483         struct in6_ifreq if6;
1484         memset((char *)&if6, 0, sizeof(if6));
1485         strncpy(if6.ifr_name, ifreqP->ifr_name, sizeof(if6.ifr_name) - 1);
1486         memcpy(&(if6.ifr_Addr), &(ifreqP->ifr_addr),
1487                sizeof(struct sockaddr_in6));
1488         if (ioctl(sock, SIOCGIFNETMASK6, (char *)&if6) >= 0) {
1489             prefix = translateIPv6AddressToPrefix(&(if6.ifr_Addr));
1490         }
1491 
1492         // set scope ID to interface index
1493         ((struct sockaddr_in6 *)&(ifreqP->ifr_addr))->sin6_scope_id =
1494             getIndex(sock, ifreqP->ifr_name);
1495 
1496         // add interface to the list
1497         ifs = addif(env, sock, ifreqP->ifr_name, ifs,


1510     free(buf);
1511     return ifs;
1512 }
1513 
1514 #endif /* AF_INET6 */
1515 
1516 /*
1517  * Try to get the interface index.
1518  */
1519 static int getIndex(int sock, const char *name) {
1520     int index = if_nametoindex(name);
1521     return (index == 0) ? -1 : index;
1522 }
1523 
1524 /*
1525  * Gets the Hardware address (usually MAC address) for the named interface.
1526  * On return puts the data in buf, and returns the length, in byte, of the
1527  * MAC address. Returns -1 if there is no hardware address on that interface.
1528  */
1529 static int getMacAddress
1530   (JNIEnv *env, int sock, const char *ifname, const struct in_addr *addr,
1531    unsigned char *buf)
1532 {
1533     int size;
1534     struct kinfo_ndd *nddp;
1535     void *end;
1536 
1537     size = getkerninfo(KINFO_NDD, 0, 0, 0);
1538     if (size == 0) {
1539         return -1;
1540     }
1541 
1542     if (size < 0) {
1543         perror("getkerninfo 1");
1544         return -1;
1545     }
1546 
1547     nddp = (struct kinfo_ndd *)malloc(size);
1548 
1549     if (!nddp) {
1550         JNU_ThrowOutOfMemoryError(env,


1569     }
1570 
1571     return -1;
1572 }
1573 
1574 static int getMTU(JNIEnv *env, int sock, const char *ifname) {
1575     struct ifreq if2;
1576     memset((char *)&if2, 0, sizeof(if2));
1577     strncpy(if2.ifr_name, ifname, sizeof(if2.ifr_name) - 1);
1578 
1579     if (ioctl(sock, SIOCGIFMTU, (char *)&if2) < 0) {
1580         JNU_ThrowByNameWithMessageAndLastError
1581             (env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGIFMTU) failed");
1582         return -1;
1583     }
1584 
1585     return if2.ifr_mtu;
1586 }
1587 
1588 static int getFlags(int sock, const char *ifname, int *flags) {
1589   struct ifreq if2;
1590   memset((char *)&if2, 0, sizeof(if2));
1591   strncpy(if2.ifr_name, ifname, sizeof(if2.ifr_name) - 1);
1592 
1593   if (ioctl(sock, SIOCGIFFLAGS, (char *)&if2) < 0) {
1594       return -1;
1595   }
1596 
1597   if (sizeof(if2.ifr_flags) == sizeof(short)) {
1598       *flags = (if2.ifr_flags & 0xffff);
1599   } else {
1600       *flags = if2.ifr_flags;
1601   }
1602   return 0;
1603 }
1604 
1605 #endif /* _AIX */
1606 
1607 /** Solaris **/
1608 #if defined(__solaris__)
1609 
1610 #if defined(AF_INET6)
1611 /*
1612  * Opens a socket for further ioctl calls. Tries AF_INET socket first and
1613  * if it fails return AF_INET6 socket.
1614  */
1615 static int openSocketWithFallback(JNIEnv *env, const char *ifname) {
1616     int sock, alreadyV6 = 0;
1617     struct lifreq if2;
1618 
1619     if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1620         if (errno == EPROTONOSUPPORT) {
1621             if ((sock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1622                 JNU_ThrowByNameWithMessageAndLastError


1651     }
1652 
1653     return sock;
1654 }
1655 #else
1656 static int openSocketWithFallback(JNIEnv *env, const char *ifname) {
1657     return openSocket(env, AF_INET);
1658 }
1659 #endif
1660 
1661 /*
1662  * Enumerates and returns all IPv4 interfaces on Solaris.
1663  */
1664 static netif *enumIPv4Interfaces(JNIEnv *env, int sock, netif *ifs) {
1665     struct lifconf ifc;
1666     struct lifreq *ifreqP;
1667     struct lifnum numifs;
1668     char *buf = NULL;
1669     unsigned i;
1670 
1671     // call SIOCGLIFNUM to get the size of SIOCGIFCONF buffer
1672     numifs.lifn_family = AF_INET;
1673     numifs.lifn_flags = 0;
1674     if (ioctl(sock, SIOCGLIFNUM, (char *)&numifs) < 0) {
1675         JNU_ThrowByNameWithMessageAndLastError
1676             (env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGLIFNUM) failed");
1677         return ifs;
1678     }
1679 
1680     // call SIOCGLIFCONF to enumerate the interfaces
1681     ifc.lifc_len = numifs.lifn_count * sizeof(struct lifreq);
1682     CHECKED_MALLOC3(buf, char *, ifc.lifc_len);
1683     ifc.lifc_buf = buf;
1684     ifc.lifc_family = AF_INET;
1685     ifc.lifc_flags = 0;
1686     if (ioctl(sock, SIOCGLIFCONF, (char *)&ifc) < 0) {
1687         JNU_ThrowByNameWithMessageAndLastError
1688             (env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGLIFCONF) failed");
1689         free(buf);
1690         return ifs;
1691     }
1692 
1693     // iterate through each interface
1694     ifreqP = ifc.lifc_req;
1695     for (i = 0; i < numifs.lifn_count; i++, ifreqP++) {
1696         struct sockaddr addr, *broadaddrP = NULL;
1697 
1698         // ignore non IPv4 interfaces
1699         if (ifreqP->lifr_addr.ss_family != AF_INET) {
1700             continue;
1701         }
1702 
1703         // save socket address
1704         memcpy(&addr, &(ifreqP->lifr_addr), sizeof(struct sockaddr));
1705 
1706         // determine broadcast address, if applicable
1707         if ((ioctl(sock, SIOCGLIFFLAGS, ifreqP) == 0) &&
1708             ifreqP->lifr_flags & IFF_BROADCAST) {
1709 
1710             // restore socket address to ifreqP
1711             memcpy(&(ifreqP->lifr_addr), &addr, sizeof(struct sockaddr));
1712 
1713             // query broadcast address and set pointer to it
1714             if (ioctl(sock, SIOCGLIFBRDADDR, ifreqP) == 0) {
1715                 broadaddrP = (struct sockaddr *)&(ifreqP->lifr_broadaddr);
1716             }
1717         }
1718 


1727         }
1728    }
1729 
1730     // free buffer
1731     free(buf);
1732     return ifs;
1733 }
1734 
1735 #if defined(AF_INET6)
1736 
1737 /*
1738  * Enumerates and returns all IPv6 interfaces on Solaris.
1739  */
1740 static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs) {
1741     struct lifconf ifc;
1742     struct lifreq *ifreqP;
1743     struct lifnum numifs;
1744     char *buf = NULL;
1745     unsigned i;
1746 
1747     // call SIOCGLIFNUM to get the size of SIOCGLIFCONF buffer
1748     numifs.lifn_family = AF_INET6;
1749     numifs.lifn_flags = 0;
1750     if (ioctl(sock, SIOCGLIFNUM, (char *)&numifs) < 0) {
1751         JNU_ThrowByNameWithMessageAndLastError
1752             (env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGLIFNUM) failed");
1753         return ifs;
1754     }
1755 
1756     // call SIOCGLIFCONF to enumerate the interfaces
1757     ifc.lifc_len = numifs.lifn_count * sizeof(struct lifreq);
1758     CHECKED_MALLOC3(buf, char *, ifc.lifc_len);
1759     ifc.lifc_buf = buf;
1760     ifc.lifc_family = AF_INET6;
1761     ifc.lifc_flags = 0;
1762     if (ioctl(sock, SIOCGLIFCONF, (char *)&ifc) < 0) {
1763         JNU_ThrowByNameWithMessageAndLastError
1764             (env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGLIFCONF) failed");
1765         free(buf);
1766         return ifs;
1767     }
1768 
1769     // iterate through each interface
1770     ifreqP = ifc.lifc_req;
1771     for (i = 0; i < numifs.lifn_count; i++, ifreqP++) {
1772 
1773         // ignore non IPv6 interfaces
1774         if (ifreqP->lifr_addr.ss_family != AF_INET6) {
1775             continue;
1776         }
1777 
1778         // set scope ID to interface index
1779         ((struct sockaddr_in6 *)&(ifreqP->lifr_addr))->sin6_scope_id =
1780             getIndex(sock, ifreqP->lifr_name);
1781 
1782         // add to the list
1783         ifs = addif(env, sock, ifreqP->lifr_name, ifs,
1784                     (struct sockaddr *)&(ifreqP->lifr_addr),
1785                     NULL, AF_INET6, (short)ifreqP->lifr_addrlen);
1786 
1787         // if an exception occurred we return immediately
1788         if ((*env)->ExceptionOccurred(env)) {
1789             free(buf);
1790             return ifs;
1791         }
1792    }
1793 
1794     // free buffer
1795     free(buf);
1796     return ifs;
1797 }
1798 
1799 #endif /* AF_INET6 */
1800 
1801 /*
1802  * Try to get the interface index.
1803  * (Not supported on Solaris 2.6 or 7)
1804  */
1805 static int getIndex(int sock, const char *name) {
1806     struct lifreq if2;
1807     memset((char *)&if2, 0, sizeof(if2));
1808     strncpy(if2.lifr_name, name, sizeof(if2.lifr_name) - 1);
1809 
1810     if (ioctl(sock, SIOCGLIFINDEX, (char *)&if2) < 0) {
1811         return -1;
1812     }


1817 /*
1818  * Solaris specific DLPI code to get hardware address from a device.
1819  * Unfortunately, at least up to Solaris X, you have to have special
1820  * privileges (i.e. be root).
1821  */
1822 static int getMacFromDevice
1823   (JNIEnv *env, const char *ifname, unsigned char *retbuf)
1824 {
1825     char style1dev[MAXPATHLEN];
1826     int fd;
1827     dl_phys_addr_req_t dlpareq;
1828     dl_phys_addr_ack_t *dlpaack;
1829     struct strbuf msg;
1830     char buf[128];
1831     int flags = 0;
1832 
1833     // Device is in /dev.  e.g.: /dev/bge0
1834     strcpy(style1dev, DEV_PREFIX);
1835     strcat(style1dev, ifname);
1836     if ((fd = open(style1dev, O_RDWR)) < 0) {
1837          // Can't open it. We probably are missing the privilege.
1838          // We'll have to try something else
1839          return 0;
1840     }
1841 
1842     dlpareq.dl_primitive = DL_PHYS_ADDR_REQ;
1843     dlpareq.dl_addr_type = DL_CURR_PHYS_ADDR;
1844 
1845     msg.buf = (char *)&dlpareq;
1846     msg.len = DL_PHYS_ADDR_REQ_SIZE;
1847 
1848     if (putmsg(fd, &msg, NULL, 0) < 0) {
1849         JNU_ThrowByNameWithMessageAndLastError
1850             (env, JNU_JAVANETPKG "SocketException", "putmsg() failed");
1851         return -1;
1852     }
1853 
1854     dlpaack = (dl_phys_addr_ack_t *)buf;
1855 
1856     msg.buf = (char *)buf;
1857     msg.len = 0;
1858     msg.maxlen = sizeof (buf);
1859     if (getmsg(fd, &msg, NULL, &flags) < 0) {


1861             (env, JNU_JAVANETPKG "SocketException", "getmsg() failed");
1862         return -1;
1863     }
1864 
1865     if (msg.len < DL_PHYS_ADDR_ACK_SIZE || dlpaack->dl_primitive != DL_PHYS_ADDR_ACK) {
1866         JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
1867                         "Couldn't obtain phys addr\n");
1868         return -1;
1869     }
1870 
1871     memcpy(retbuf, &buf[dlpaack->dl_addr_offset], dlpaack->dl_addr_length);
1872     return dlpaack->dl_addr_length;
1873 }
1874 
1875 /*
1876  * Gets the Hardware address (usually MAC address) for the named interface.
1877  * On return puts the data in buf, and returns the length, in byte, of the
1878  * MAC address. Returns -1 if there is no hardware address on that interface.
1879  */
1880 static int getMacAddress
1881   (JNIEnv *env, int sock, const char *ifname, const struct in_addr *addr,
1882    unsigned char *buf)
1883 {
1884     struct lifreq if2;
1885     int len, i;




1886 
1887     // First, try the new (S11) SIOCGLIFHWADDR ioctl(). If that fails
1888     // try the old way.
1889     memset((char *)&if2, 0, sizeof(if2));
1890     strncpy(if2.lifr_name, ifname, sizeof(if2.lifr_name) - 1);
1891 
1892     if (ioctl(sock, SIOCGLIFHWADDR, &if2) != -1) {
1893         struct sockaddr_dl *sp;
1894         sp = (struct sockaddr_dl *)&if2.lifr_addr;
1895         memcpy(buf, &sp->sdl_data[0], sp->sdl_alen);

1896         return sp->sdl_alen;
1897     }
1898 
1899     // On Solaris we have to use DLPI, but it will only work if we have
1900     // privileged access (i.e. root). If that fails, we try a lookup
1901     // in the ARP table, which requires an IPv4 address.
1902     if ((len = getMacFromDevice(env, ifname, buf))  == 0) {
1903         // DLPI failed - trying to do arp lookup
1904 
1905         struct arpreq arpreq;
1906         struct sockaddr_in *sin;
1907         struct sockaddr_in ipAddr;
1908 
1909         if (addr == NULL) {
1910              // No IPv4 address for that interface, so can't do an ARP lookup.
1911              return -1;
1912          }
1913 
1914          len = 6; //???
1915 
1916          sin = (struct sockaddr_in *)&arpreq.arp_pa;
1917          memset((char *)&arpreq, 0, sizeof(struct arpreq));
1918          ipAddr.sin_port = 0;
1919          ipAddr.sin_family = AF_INET;
1920          memcpy(&ipAddr.sin_addr, addr, sizeof(struct in_addr));
1921          memcpy(&arpreq.arp_pa, &ipAddr, sizeof(struct sockaddr_in));
1922          arpreq.arp_flags= ATF_PUBL;
1923 
1924          if (ioctl(sock, SIOCGARP, &arpreq) < 0) {
1925              return -1;
1926          }
1927 
1928          memcpy(buf, &arpreq.arp_ha.sa_data[0], len);













1929     }

1930 
1931     // all bytes to 0 means no hardware address
1932     for (i = 0; i < len; i++) {
1933         if (buf[i] != 0)
1934             return len;
1935     }
1936 
1937     return -1;
1938 }
1939 
1940 static int getMTU(JNIEnv *env, int sock, const char *ifname) {
1941     struct lifreq if2;
1942     memset((char *)&if2, 0, sizeof(if2));
1943     strncpy(if2.lifr_name, ifname, sizeof(if2.lifr_name) - 1);
1944 
1945     if (ioctl(sock, SIOCGLIFMTU, (char *)&if2) < 0) {
1946         JNU_ThrowByNameWithMessageAndLastError
1947             (env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGLIFMTU) failed");
1948         return -1;
1949     }


1997 static int openSocketWithFallback(JNIEnv *env, const char *ifname) {
1998     return openSocket(env, AF_INET);
1999 }
2000 #endif
2001 
2002 /*
2003  * Enumerates and returns all IPv4 interfaces on BSD.
2004  */
2005 static netif *enumIPv4Interfaces(JNIEnv *env, int sock, netif *ifs) {
2006     struct ifaddrs *ifa, *origifa;
2007 
2008     if (getifaddrs(&origifa) != 0) {
2009         JNU_ThrowByNameWithMessageAndLastError
2010             (env, JNU_JAVANETPKG "SocketException", "getifaddrs() failed");
2011         return ifs;
2012     }
2013 
2014     for (ifa = origifa; ifa != NULL; ifa = ifa->ifa_next) {
2015         struct sockaddr *broadaddrP = NULL;
2016 
2017         // ignore non IPv4 interfaces
2018         if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family != AF_INET)
2019             continue;
2020 
2021         // set ifa_broadaddr, if there is one
2022         if ((ifa->ifa_flags & IFF_POINTOPOINT) == 0 &&
2023             ifa->ifa_flags & IFF_BROADCAST) {
2024             broadaddrP = ifa->ifa_dstaddr;
2025         }
2026 
2027         // add interface to the list
2028         ifs = addif(env, sock, ifa->ifa_name, ifs, ifa->ifa_addr,
2029                     broadaddrP, AF_INET,
2030                     translateIPv4AddressToPrefix((struct sockaddr_in *)
2031                                                  ifa->ifa_netmask));
2032 
2033         // if an exception occurred then free the list
2034         if ((*env)->ExceptionOccurred(env)) {
2035             freeifaddrs(origifa);
2036             freeif(ifs);
2037             return NULL;


2041     // free ifaddrs buffer
2042     freeifaddrs(origifa);
2043     return ifs;
2044 }
2045 
2046 #if defined(AF_INET6)
2047 
2048 /*
2049  * Enumerates and returns all IPv6 interfaces on BSD.
2050  */
2051 static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs) {
2052     struct ifaddrs *ifa, *origifa;
2053 
2054     if (getifaddrs(&origifa) != 0) {
2055         JNU_ThrowByNameWithMessageAndLastError
2056             (env, JNU_JAVANETPKG "SocketException", "getifaddrs() failed");
2057         return ifs;
2058     }
2059 
2060     for (ifa = origifa; ifa != NULL; ifa = ifa->ifa_next) {
2061         // ignore non IPv6 interfaces
2062         if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family != AF_INET6)
2063             continue;
2064 
2065         // set scope ID to interface index
2066         ((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_scope_id =
2067             getIndex(sock, ifa->ifa_name);
2068 
2069         // add interface to the list
2070         ifs = addif(env, sock, ifa->ifa_name, ifs, ifa->ifa_addr, NULL,
2071                     AF_INET6,
2072                     translateIPv6AddressToPrefix((struct sockaddr_in6 *)
2073                                                  ifa->ifa_netmask));
2074 
2075         // if an exception occurred then free the list
2076         if ((*env)->ExceptionOccurred(env)) {
2077             freeifaddrs(origifa);
2078             freeif(ifs);
2079             return NULL;
2080         }
2081     }


2096     return (index == 0) ? -1 : index;
2097 #else
2098     struct ifreq if2;
2099     memset((char *)&if2, 0, sizeof(if2));
2100     strncpy(if2.ifr_name, name, sizeof(if2.ifr_name) - 1);
2101 
2102     if (ioctl(sock, SIOCGIFINDEX, (char *)&if2) < 0) {
2103         return -1;
2104     }
2105 
2106     return if2.ifr_index;
2107 #endif
2108 }
2109 
2110 /*
2111  * Gets the Hardware address (usually MAC address) for the named interface.
2112  * On return puts the data in buf, and returns the length, in byte, of the
2113  * MAC address. Returns -1 if there is no hardware address on that interface.
2114  */
2115 static int getMacAddress
2116   (JNIEnv *env, int sock, const char *ifname, const struct in_addr *addr,
2117    unsigned char *buf)
2118 {
2119     struct ifaddrs *ifa0, *ifa;
2120     struct sockaddr *saddr;
2121     int i;
2122 
2123     // Grab the interface list
2124     if (!getifaddrs(&ifa0)) {
2125         // Cycle through the interfaces
2126         for (i = 0, ifa = ifa0; ifa != NULL; ifa = ifa->ifa_next, i++) {
2127             saddr = ifa->ifa_addr;
2128             // Link layer contains the MAC address
2129             if (saddr->sa_family == AF_LINK && !strcmp(ifname, ifa->ifa_name)) {
2130                 struct sockaddr_dl *sadl = (struct sockaddr_dl *) saddr;
2131                 // Check the address is the correct length
2132                 if (sadl->sdl_alen == ETHER_ADDR_LEN) {
2133                     memcpy(buf, (sadl->sdl_data + sadl->sdl_nlen), ETHER_ADDR_LEN);
2134                     freeifaddrs(ifa0);
2135                     return ETHER_ADDR_LEN;
2136                 }
2137             }
2138         }
2139         freeifaddrs(ifa0);
2140     }
2141 
2142     return -1;
2143 }
2144 
2145 static int getMTU(JNIEnv *env, int sock, const char *ifname) {
2146     struct ifreq if2;
2147     memset((char *)&if2, 0, sizeof(if2));
2148     strncpy(if2.ifr_name, ifname, sizeof(if2.ifr_name) - 1);
2149 
2150     if (ioctl(sock, SIOCGIFMTU, (char *)&if2) < 0) {
2151         JNU_ThrowByNameWithMessageAndLastError




  63 #include <net/if_var.h>
  64 #include <net/if_dl.h>
  65 #include <netinet/in_var.h>
  66 #include <ifaddrs.h>
  67 #endif
  68 #endif
  69 
  70 #include "jvm.h"
  71 #include "jni_util.h"
  72 #include "net_util.h"
  73 
  74 #if defined(__linux__)
  75     #define _PATH_PROCNET_IFINET6 "/proc/net/if_inet6"
  76 #elif defined(__solaris__)
  77     #ifndef SIOCGLIFHWADDR
  78         #define SIOCGLIFHWADDR _IOWR('i', 192, struct lifreq)
  79     #endif
  80     #define DEV_PREFIX "/dev/"
  81 #endif
  82 
  83 #ifdef LIFNAMSIZ
  84     #define IFNAMESIZE LIFNAMSIZ
  85 #else
  86     #define IFNAMESIZE IFNAMSIZ
  87 #endif
  88 
  89 #define CHECKED_MALLOC3(_pointer, _type, _size) \
  90     do { \
  91         _pointer = (_type)malloc(_size); \
  92         if (_pointer == NULL) { \
  93             JNU_ThrowOutOfMemoryError(env, "Native heap allocation failed"); \
  94             return ifs; /* return untouched list */ \
  95         } \
  96     } while(0)
  97 
  98 typedef struct _netaddr  {
  99     struct sockaddr *addr;
 100     struct sockaddr *brdcast;
 101     short mask;
 102     int family; /* to make searches simple */
 103     struct _netaddr *next;
 104 } netaddr;
 105 
 106 typedef struct _netif {
 107     char *name;
 108     int index;


 147 static netif  *enumIPv4Interfaces(JNIEnv *env, int sock, netif *ifs);
 148 
 149 #if defined(AF_INET6)
 150 static netif  *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs);
 151 #endif
 152 
 153 static netif  *addif(JNIEnv *env, int sock, const char *if_name, netif *ifs,
 154                      struct sockaddr *ifr_addrP,
 155                      struct sockaddr *ifr_broadaddrP,
 156                      int family, short prefix);
 157 static void    freeif(netif *ifs);
 158 
 159 static int     openSocket(JNIEnv *env, int proto);
 160 static int     openSocketWithFallback(JNIEnv *env, const char *ifname);
 161 
 162 static short   translateIPv4AddressToPrefix(struct sockaddr_in *addr);
 163 static short   translateIPv6AddressToPrefix(struct sockaddr_in6 *addr);
 164 
 165 static int     getIndex(int sock, const char *ifname);
 166 static int     getFlags(int sock, const char *ifname, int *flags);
 167 static int     getMacAddress(JNIEnv *env, const char *ifname,
 168                              const struct in_addr *addr, unsigned char *buf);
 169 static int     getMTU(JNIEnv *env, int sock, const char *ifname);
 170 
 171 #if defined(__solaris__)
 172 static int     getMacFromDevice(JNIEnv *env, const char *ifname,
 173                                 unsigned char *retbuf);
 174 #endif
 175 
 176 /******************* Java entry points *****************************/
 177 
 178 /*
 179  * Class:     java_net_NetworkInterface
 180  * Method:    init
 181  * Signature: ()V
 182  */
 183 JNIEXPORT void JNICALL Java_java_net_NetworkInterface_init
 184   (JNIEnv *env, jclass cls)
 185 {
 186     ni_class = (*env)->FindClass(env, "java/net/NetworkInterface");
 187     CHECK_NULL(ni_class);


 226     CHECK_NULL(ni_ib4maskID);
 227     ni_defaultIndexID = (*env)->GetStaticFieldID(env, ni_class, "defaultIndex",
 228                                                  "I");
 229     CHECK_NULL(ni_defaultIndexID);
 230     initInetAddressIDs(env);
 231 }
 232 
 233 /*
 234  * Class:     java_net_NetworkInterface
 235  * Method:    getByName0
 236  * Signature: (Ljava/lang/String;)Ljava/net/NetworkInterface;
 237  */
 238 JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByName0
 239   (JNIEnv *env, jclass cls, jstring name)
 240 {
 241     netif *ifs, *curr;
 242     jboolean isCopy;
 243     const char* name_utf;
 244     jobject obj = NULL;
 245 
 246     if (name != NULL) {
 247         name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
 248     } else {
 249         JNU_ThrowNullPointerException(env, "network interface name is NULL");
 250         return NULL;
 251     }
 252 

 253     if (name_utf == NULL) {
 254        if (!(*env)->ExceptionCheck(env))
 255            JNU_ThrowOutOfMemoryError(env, NULL);

 256        return NULL;
 257     }
 258 
 259     ifs = enumInterfaces(env);
 260     if (ifs == NULL) {
 261         return NULL;
 262     }
 263 
 264     // search the list of interfaces based on name
 265     curr = ifs;
 266     while (curr != NULL) {
 267         if (strcmp(name_utf, curr->name) == 0) {
 268             break;
 269         }
 270         curr = curr->next;
 271     }
 272 
 273     // if found create a NetworkInterface
 274     if (curr != NULL) {
 275         obj = createNetworkInterface(env, curr);
 276     }
 277 
 278     // release the UTF string and interface list
 279     (*env)->ReleaseStringUTFChars(env, name, name_utf);
 280     freeif(ifs);
 281 
 282     return obj;
 283 }
 284 
 285 /*
 286  * Class:     java_net_NetworkInterface
 287  * Method:    getByIndex0
 288  * Signature: (Ljava/lang/String;)Ljava/net/NetworkInterface;
 289  */
 290 JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByIndex0
 291   (JNIEnv *env, jclass cls, jint index)
 292 {
 293     netif *ifs, *curr;
 294     jobject obj = NULL;
 295 
 296     if (index <= 0) {
 297         return NULL;
 298     }
 299 
 300     ifs = enumInterfaces(env);
 301     if (ifs == NULL) {
 302         return NULL;
 303     }
 304 
 305     // search the list of interfaces based on index
 306     curr = ifs;
 307     while (curr != NULL) {
 308         if (index == curr->index) {
 309             break;
 310         }
 311         curr = curr->next;
 312     }
 313 
 314     // if found create a NetworkInterface
 315     if (curr != NULL) {
 316         obj = createNetworkInterface(env, curr);
 317     }
 318 
 319     // release the interface list
 320     freeif(ifs);
 321 
 322     return obj;
 323 }
 324 
 325 /*
 326  * Class:     java_net_NetworkInterface
 327  * Method:    getByInetAddress0
 328  * Signature: (Ljava/net/InetAddress;)Ljava/net/NetworkInterface;
 329  */
 330 JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByInetAddress0
 331   (JNIEnv *env, jclass cls, jobject iaObj)
 332 {
 333     netif *ifs, *curr;

 334 #if defined(AF_INET6)
 335     int family = (getInetAddress_family(env, iaObj) == IPv4) ? AF_INET : AF_INET6;
 336 #else
 337     int family =  AF_INET;
 338 #endif

 339     jobject obj = NULL;
 340     jboolean match = JNI_FALSE;
 341 
 342     ifs = enumInterfaces(env);
 343     if (ifs == NULL) {
 344         return NULL;
 345     }
 346 
 347     curr = ifs;
 348     while (curr != NULL) {
 349         netaddr *addrP = curr->addr;
 350 
 351         // iterate through each address on the interface
 352         while (addrP != NULL) {
 353 
 354             if (family == addrP->family) {
 355                 if (family == AF_INET) {
 356                     int address1 = htonl(
 357                         ((struct sockaddr_in *)addrP->addr)->sin_addr.s_addr);
 358                     int address2 = getInetAddress_addr(env, iaObj);
 359 
 360                     if (address1 == address2) {
 361                         match = JNI_TRUE;
 362                         break;
 363                     }
 364                 }

 365 #if defined(AF_INET6)
 366                 if (family == AF_INET6) {
 367                     jbyte *bytes = (jbyte *)&(
 368                         ((struct sockaddr_in6*)addrP->addr)->sin6_addr);
 369                     jbyte caddr[16];
 370                     int i;
 371                     getInet6Address_ipaddress(env, iaObj, (char *)caddr);
 372                     i = 0;
 373                     while (i < 16) {
 374                         if (caddr[i] != bytes[i]) {
 375                             break;
 376                         }
 377                         i++;
 378                     }
 379                     if (i >= 16) {
 380                         match = JNI_TRUE;
 381                         break;
 382                     }
 383                 }
 384 #endif
 385             }
 386 
 387             if (match) {
 388                 break;
 389             }
 390             addrP = addrP->next;
 391         }
 392 
 393         if (match) {
 394             break;
 395         }
 396         curr = curr->next;
 397     }
 398 
 399     // if found create a NetworkInterface
 400     if (match) {
 401         obj = createNetworkInterface(env, curr);
 402     }
 403 
 404     // release the interface list
 405     freeif(ifs);
 406 
 407     return obj;
 408 }
 409 
 410 /*
 411  * Class:     java_net_NetworkInterface
 412  * Method:    getAll
 413  * Signature: ()[Ljava/net/NetworkInterface;
 414  */
 415 JNIEXPORT jobjectArray JNICALL Java_java_net_NetworkInterface_getAll
 416   (JNIEnv *env, jclass cls)
 417 {
 418     netif *ifs, *curr;
 419     jobjectArray netIFArr;
 420     jint arr_index, ifCount;
 421 
 422     ifs = enumInterfaces(env);
 423     if (ifs == NULL) {
 424         return NULL;
 425     }
 426 
 427     // count the interfaces
 428     ifCount = 0;
 429     curr = ifs;
 430     while (curr != NULL) {
 431         ifCount++;
 432         curr = curr->next;
 433     }
 434 
 435     // allocate a NetworkInterface array
 436     netIFArr = (*env)->NewObjectArray(env, ifCount, cls, NULL);
 437     if (netIFArr == NULL) {
 438         freeif(ifs);
 439         return NULL;
 440     }
 441 
 442     // iterate through the interfaces, create a NetworkInterface instance
 443     // for each array element and populate the object
 444     curr = ifs;
 445     arr_index = 0;
 446     while (curr != NULL) {
 447         jobject netifObj;
 448 
 449         netifObj = createNetworkInterface(env, curr);
 450         if (netifObj == NULL) {
 451             freeif(ifs);
 452             return NULL;
 453         }
 454 
 455         // put the NetworkInterface into the array
 456         (*env)->SetObjectArrayElement(env, netIFArr, arr_index++, netifObj);
 457 
 458         curr = curr->next;
 459     }
 460 
 461     // release the interface list
 462     freeif(ifs);
 463 
 464     return netIFArr;
 465 }
 466 
 467 /*
 468  * Class:     java_net_NetworkInterface
 469  * Method:    isUp0
 470  * Signature: (Ljava/lang/String;I)Z
 471  */
 472 JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isUp0
 473   (JNIEnv *env, jclass cls, jstring name, jint index)
 474 {
 475     int ret = getFlags0(env, name);
 476     return ((ret & IFF_UP) && (ret & IFF_RUNNING)) ? JNI_TRUE :  JNI_FALSE;
 477 }
 478 
 479 /*
 480  * Class:     java_net_NetworkInterface
 481  * Method:    isP2P0
 482  * Signature: (Ljava/lang/String;I)Z
 483  */


 509   (JNIEnv *env, jclass cls, jstring name, jint index)
 510 {
 511     int ret = getFlags0(env, name);
 512     return (ret & IFF_MULTICAST) ? JNI_TRUE :  JNI_FALSE;
 513 }
 514 
 515 /*
 516  * Class:     java_net_NetworkInterface
 517  * Method:    getMacAddr0
 518  * Signature: ([bLjava/lang/String;I)[b
 519  */
 520 JNIEXPORT jbyteArray JNICALL Java_java_net_NetworkInterface_getMacAddr0
 521   (JNIEnv *env, jclass cls, jbyteArray addrArray, jstring name, jint index)
 522 {
 523     jint addr;
 524     jbyte caddr[4];
 525     struct in_addr iaddr;
 526     jbyteArray ret = NULL;
 527     unsigned char mac[16];
 528     int len;

 529     jboolean isCopy;
 530     const char *name_utf;
 531 
 532     if (name != NULL) {
 533         name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
 534     } else {
 535         JNU_ThrowNullPointerException(env, "network interface name is NULL");
 536         return NULL;
 537     }
 538 
 539     if (name_utf == NULL) {
 540         if (!(*env)->ExceptionCheck(env))
 541             JNU_ThrowOutOfMemoryError(env, NULL);
 542         return NULL;
 543     }
 544 
 545     if (!IS_NULL(addrArray)) {
 546         (*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr);
 547         addr = ((caddr[0]<<24) & 0xff000000);
 548         addr |= ((caddr[1] <<16) & 0xff0000);
 549         addr |= ((caddr[2] <<8) & 0xff00);
 550         addr |= (caddr[3] & 0xff);
 551         iaddr.s_addr = htonl(addr);
 552         len = getMacAddress(env, name_utf, &iaddr, mac);
 553     } else {
 554         len = getMacAddress(env, name_utf, NULL, mac);
 555     }
 556 
 557     if (len > 0) {
 558         ret = (*env)->NewByteArray(env, len);
 559         if (!IS_NULL(ret)) {
 560             (*env)->SetByteArrayRegion(env, ret, 0, len, (jbyte *)(mac));
 561         }


 562     }



 563 
 564     // release the UTF string and interface list
 565     (*env)->ReleaseStringUTFChars(env, name, name_utf);
 566 
 567     return ret;
 568 }
 569 
 570 /*
 571  * Class:       java_net_NetworkInterface
 572  * Method:      getMTU0
 573  * Signature:   ([bLjava/lang/String;I)I
 574  */
 575 JNIEXPORT jint JNICALL Java_java_net_NetworkInterface_getMTU0
 576   (JNIEnv *env, jclass cls, jstring name, jint index)
 577 {
 578     jboolean isCopy;
 579     int sock, ret = -1;

 580     const char* name_utf = NULL;
 581 
 582     if (name != NULL) {
 583         name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
 584     } else {
 585         JNU_ThrowNullPointerException(env, "network interface name is NULL");
 586         return ret;
 587     }
 588 
 589     if (name_utf == NULL) {
 590        if (!(*env)->ExceptionCheck(env))
 591            JNU_ThrowOutOfMemoryError(env, NULL);
 592        return ret;
 593     }
 594 
 595     if ((sock = openSocketWithFallback(env, name_utf)) < 0) {
 596        (*env)->ReleaseStringUTFChars(env, name, name_utf);
 597        return JNI_FALSE;
 598     }
 599 
 600     ret = getMTU(env, sock, name_utf);
 601 
 602     (*env)->ReleaseStringUTFChars(env, name, name_utf);
 603 
 604     close(sock);
 605     return ret;
 606 }
 607 
 608 /*** Private methods definitions ****/
 609 
 610 static int getFlags0(JNIEnv *env, jstring name) {
 611     jboolean isCopy;
 612     int ret, sock, flags = 0;
 613     const char *name_utf;

 614 
 615     if (name != NULL) {
 616         name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
 617     } else {
 618         JNU_ThrowNullPointerException(env, "network interface name is NULL");
 619         return -1;
 620     }
 621 
 622     if (name_utf == NULL) {
 623        if (!(*env)->ExceptionCheck(env))
 624            JNU_ThrowOutOfMemoryError(env, NULL);
 625        return -1;
 626     }
 627     if ((sock = openSocketWithFallback(env, name_utf)) < 0) {
 628         (*env)->ReleaseStringUTFChars(env, name, name_utf);
 629         return -1;
 630     }
 631 
 632     ret = getFlags(sock, name_utf, &flags);
 633 


 644 }
 645 
 646 /*
 647  * Creates a NetworkInterface object, populates the name, the index, and
 648  * populates the InetAddress array based on the IP addresses for this
 649  * interface.
 650  */
 651 static jobject createNetworkInterface(JNIEnv *env, netif *ifs) {
 652     jobject netifObj;
 653     jobject name;
 654     jobjectArray addrArr;
 655     jobjectArray bindArr;
 656     jobjectArray childArr;
 657     netaddr *addrs;
 658     jint addr_index, addr_count, bind_index;
 659     jint child_count, child_index;
 660     netaddr *addrP;
 661     netif *childP;
 662     jobject tmp;
 663 
 664     // create a NetworkInterface object and populate it
 665     netifObj = (*env)->NewObject(env, ni_class, ni_ctrID);
 666     CHECK_NULL_RETURN(netifObj, NULL);
 667     name = (*env)->NewStringUTF(env, ifs->name);
 668     CHECK_NULL_RETURN(name, NULL);
 669     (*env)->SetObjectField(env, netifObj, ni_nameID, name);
 670     (*env)->SetObjectField(env, netifObj, ni_descID, name);
 671     (*env)->SetIntField(env, netifObj, ni_indexID, ifs->index);
 672     (*env)->SetBooleanField(env, netifObj, ni_virutalID,
 673                             ifs->virtual ? JNI_TRUE : JNI_FALSE);
 674 
 675     // count the number of addresses on this interface
 676     addr_count = 0;
 677     addrP = ifs->addr;
 678     while (addrP != NULL) {
 679         addr_count++;
 680         addrP = addrP->next;
 681     }
 682 
 683     // create the array of InetAddresses
 684     addrArr = (*env)->NewObjectArray(env, addr_count, ia_class, NULL);
 685     if (addrArr == NULL) {
 686         return NULL;
 687     }
 688 
 689     bindArr = (*env)->NewObjectArray(env, addr_count, ni_ibcls, NULL);
 690     if (bindArr == NULL) {
 691        return NULL;
 692     }
 693     addrP = ifs->addr;
 694     addr_index = 0;
 695     bind_index = 0;
 696     while (addrP != NULL) {
 697         jobject iaObj = NULL;
 698         jobject ibObj = NULL;
 699 
 700         if (addrP->family == AF_INET) {
 701             iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
 702             if (iaObj) {
 703                  setInetAddress_addr(env, iaObj, htonl(


 708             ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID);
 709             if (ibObj) {
 710                  (*env)->SetObjectField(env, ibObj, ni_ibaddressID, iaObj);
 711                  if (addrP->brdcast) {
 712                     jobject ia2Obj = NULL;
 713                     ia2Obj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
 714                     if (ia2Obj) {
 715                        setInetAddress_addr(env, ia2Obj, htonl(
 716                            ((struct sockaddr_in*)addrP->brdcast)->sin_addr.s_addr));
 717                        (*env)->SetObjectField(env, ibObj, ni_ib4broadcastID, ia2Obj);
 718                     } else {
 719                         return NULL;
 720                     }
 721                  }
 722                  (*env)->SetShortField(env, ibObj, ni_ib4maskID, addrP->mask);
 723                  (*env)->SetObjectArrayElement(env, bindArr, bind_index++, ibObj);
 724             } else {
 725                 return NULL;
 726             }
 727         }

 728 #if defined(AF_INET6)
 729         if (addrP->family == AF_INET6) {
 730             int scope=0;
 731             iaObj = (*env)->NewObject(env, ia6_class, ia6_ctrID);
 732             if (iaObj) {
 733                 jboolean ret = setInet6Address_ipaddress(env, iaObj,
 734                     (char *)&(((struct sockaddr_in6*)addrP->addr)->sin6_addr));
 735                 if (ret == JNI_FALSE) {
 736                     return NULL;
 737                 }
 738 
 739                 scope = ((struct sockaddr_in6*)addrP->addr)->sin6_scope_id;
 740 
 741                 if (scope != 0) { /* zero is default value, no need to set */
 742                     setInet6Address_scopeid(env, iaObj, scope);
 743                     setInet6Address_scopeifname(env, iaObj, netifObj);
 744                 }
 745             } else {
 746                 return NULL;
 747             }
 748             ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID);
 749             if (ibObj) {
 750                 (*env)->SetObjectField(env, ibObj, ni_ibaddressID, iaObj);
 751                 (*env)->SetShortField(env, ibObj, ni_ib4maskID, addrP->mask);
 752                 (*env)->SetObjectArrayElement(env, bindArr, bind_index++, ibObj);
 753             } else {
 754                 return NULL;
 755             }
 756         }
 757 #endif
 758 
 759         (*env)->SetObjectArrayElement(env, addrArr, addr_index++, iaObj);
 760         addrP = addrP->next;
 761     }
 762 
 763     // see if there is any virtual interface attached to this one.
 764     child_count = 0;
 765     childP = ifs->childs;
 766     while (childP) {
 767         child_count++;
 768         childP = childP->next;
 769     }
 770 
 771     childArr = (*env)->NewObjectArray(env, child_count, ni_class, NULL);
 772     if (childArr == NULL) {
 773         return NULL;
 774     }
 775 
 776     // create the NetworkInterface instances for the sub-interfaces as well
 777     child_index = 0;
 778     childP = ifs->childs;
 779     while(childP) {
 780       tmp = createNetworkInterface(env, childP);
 781       if (tmp == NULL) {
 782          return NULL;
 783       }
 784       (*env)->SetObjectField(env, tmp, ni_parentID, netifObj);
 785       (*env)->SetObjectArrayElement(env, childArr, child_index++, tmp);
 786       childP = childP->next;
 787     }
 788     (*env)->SetObjectField(env, netifObj, ni_addrsID, addrArr);
 789     (*env)->SetObjectField(env, netifObj, ni_bindsID, bindArr);
 790     (*env)->SetObjectField(env, netifObj, ni_childsID, childArr);
 791 
 792     // return the NetworkInterface
 793     return netifObj;
 794 }
 795 
 796 /*
 797  * Enumerates all interfaces
 798  */
 799 static netif *enumInterfaces(JNIEnv *env) {
 800     netif *ifs = NULL;
 801     int sock;
 802 

 803     sock = openSocket(env, AF_INET);
 804     if (sock < 0 && (*env)->ExceptionOccurred(env)) {
 805         return NULL;
 806     }
 807 
 808     // enumerate IPv4 addresses
 809     ifs = enumIPv4Interfaces(env, sock, NULL);
 810     close(sock);
 811 
 812     // return partial list if an exception occurs in the middle of process ???
 813     if (ifs == NULL && (*env)->ExceptionOccurred(env)) {
 814         return NULL;
 815     }
 816 


 817     // If IPv6 is available then enumerate IPv6 addresses.
 818 #if defined(AF_INET6)

 819         // User can disable ipv6 explicitly by -Djava.net.preferIPv4Stack=true,
 820         // so we have to call ipv6_available()
 821         if (ipv6_available()) {
 822             sock = openSocket(env, AF_INET6);
 823             if (sock < 0 && (*env)->ExceptionOccurred(env)) {
 824                 freeif(ifs);
 825                 return NULL;
 826             }
 827 
 828             ifs = enumIPv6Interfaces(env, sock, ifs);
 829             close(sock);











 830 
 831             if ((*env)->ExceptionOccurred(env)) {
 832                 freeif(ifs);
 833                 return NULL;
 834             }
 835         }
 836 #endif
 837 
 838     return ifs;
 839 }
 840 
 841 /*
 842  * Frees an interface list (including any attached addresses).
 843  */
 844 static void freeif(netif *ifs) {
 845     netif *currif = ifs;
 846     netif *child = NULL;
 847 
 848     while (currif != NULL) {
 849         netaddr *addrP = currif->addr;
 850         while (addrP != NULL) {
 851             netaddr *next = addrP->next;
 852             free(addrP);
 853             addrP = next;
 854         }
 855 
 856         // don't forget to free the sub-interfaces
 857         if (currif->childs != NULL) {
 858             freeif(currif->childs);
 859         }
 860 
 861         ifs = currif->next;
 862         free(currif);
 863         currif = ifs;
 864     }
 865 }
 866 
 867 static netif *addif(JNIEnv *env, int sock, const char *if_name, netif *ifs,
 868                     struct sockaddr *ifr_addrP,
 869                     struct sockaddr *ifr_broadaddrP,
 870                     int family, short prefix)
 871 {
 872     netif *currif = ifs, *parent;
 873     netaddr *addrP;
 874     char name[IFNAMESIZE], vname[IFNAMESIZE];








 875     char *name_colonP;
 876     int isVirtual = 0;
 877     int addr_size;

 878 
 879     // If the interface name is a logical interface then we remove the unit
 880     // number so that we have the physical interface (eg: hme0:1 -> hme0).
 881     // NetworkInterface currently doesn't have any concept of physical vs.
 882     // logical interfaces.
 883     strncpy(name, if_name, IFNAMESIZE);
 884     name[IFNAMESIZE - 1] = '\0';
 885     *vname = 0;
 886 
 887     // Create and populate the netaddr node. If allocation fails
 888     // return an un-updated list.
 889 
 890     // Allocate for addr and brdcast at once
 891 
 892 #if defined(AF_INET6)
 893     addr_size = (family == AF_INET) ? sizeof(struct sockaddr_in)
 894                                     : sizeof(struct sockaddr_in6);
 895 #else
 896     addr_size = sizeof(struct sockaddr_in);
 897 #endif
 898 
 899     CHECKED_MALLOC3(addrP, netaddr *, sizeof(netaddr) + 2 * addr_size);
 900     addrP->addr = (struct sockaddr *)((char *)addrP + sizeof(netaddr));
 901     memcpy(addrP->addr, ifr_addrP, addr_size);
 902 
 903     addrP->family = family;
 904     addrP->mask = prefix;
 905     addrP->next = 0;
 906 
 907     // for IPv4 add broadcast address
 908     if (family == AF_INET && ifr_broadaddrP != NULL) {
 909         addrP->brdcast = (struct sockaddr *)
 910                              ((char *)addrP + sizeof(netaddr) + addr_size);
 911         memcpy(addrP->brdcast, ifr_broadaddrP, addr_size);
 912     } else {
 913         addrP->brdcast = NULL;
 914     }
 915 
 916     // Deal with virtual interface with colon notation e.g. eth0:1
 917     name_colonP = strchr(name, ':');
 918     if (name_colonP != NULL) {
 919         int flags = 0;
 920         // This is a virtual interface. If we are able to access the parent
 921         // we need to create a new entry if it doesn't exist yet *and* update
 922         // the 'parent' interface with the new records.
 923         *name_colonP = 0;
 924         if (getFlags(sock, name, &flags) < 0 || flags < 0) {
 925              // failed to access parent interface do not create parent.
 926              // We are a virtual interface with no parent.
 927              isVirtual = 1;
 928              *name_colonP = ':';
 929         } else {
 930              // Got access to parent, so create it if necessary.
 931              // Save original name to vname and truncate name by ':'
 932              memcpy(vname, name, sizeof(vname));
 933              vname[name_colonP - name] = ':';
 934         }
 935     }
 936 
 937     // Check if this is a "new" interface. Use the interface name for
 938     // matching because index isn't supported on Solaris 2.6 & 7.
 939     while (currif != NULL) {
 940         if (strcmp(name, currif->name) == 0) {
 941             break;
 942         }
 943         currif = currif->next;
 944     }
 945 
 946     // If "new" then create a netif structure and insert it into the list.
 947     if (currif == NULL) {
 948          CHECKED_MALLOC3(currif, netif *, sizeof(netif) + IFNAMESIZE);
 949          currif->name = (char *)currif + sizeof(netif);
 950          strncpy(currif->name, name, IFNAMESIZE);
 951          currif->name[IFNAMESIZE - 1] = '\0';
 952          currif->index = getIndex(sock, name);
 953          currif->addr = NULL;
 954          currif->childs = NULL;
 955          currif->virtual = isVirtual;
 956          currif->next = ifs;
 957          ifs = currif;
 958     }
 959 
 960     // Finally insert the address on the interface
 961     addrP->next = currif->addr;
 962     currif->addr = addrP;
 963 
 964     parent = currif;
 965 
 966     // Deal with the virtual interface now.
 967     if (vname[0]) {
 968         netaddr *tmpaddr;
 969 
 970         currif = parent->childs;
 971 
 972         while (currif != NULL) {
 973             if (strcmp(vname, currif->name) == 0) {
 974                 break;
 975             }
 976             currif = currif->next;
 977         }
 978 
 979         if (currif == NULL) {
 980             CHECKED_MALLOC3(currif, netif *, sizeof(netif) + IFNAMESIZE);
 981             currif->name = (char *)currif + sizeof(netif);
 982             strncpy(currif->name, vname, IFNAMESIZE);
 983             currif->name[IFNAMESIZE - 1] = '\0';
 984             currif->index = getIndex(sock, vname);
 985             currif->addr = NULL; // Need to duplicate the addr entry?

 986             currif->virtual = 1;
 987             currif->childs = NULL;
 988             currif->next = parent->childs;
 989             parent->childs = currif;
 990         }
 991 
 992         CHECKED_MALLOC3(tmpaddr, netaddr *, sizeof(netaddr) + 2 * addr_size);
 993         memcpy(tmpaddr, addrP, sizeof(netaddr));
 994         if (addrP->addr != NULL) {
 995             tmpaddr->addr = (struct sockaddr *)
 996                 ((char*)tmpaddr + sizeof(netaddr));
 997             memcpy(tmpaddr->addr, addrP->addr, addr_size);
 998         }
 999 
1000         if (addrP->brdcast != NULL) {
1001             tmpaddr->brdcast = (struct sockaddr *)
1002                 ((char *)tmpaddr + sizeof(netaddr) + addr_size);
1003             memcpy(tmpaddr->brdcast, addrP->brdcast, addr_size);
1004         }
1005 


1132             (env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGIFCONF) failed");
1133         return ifs;
1134     }
1135 
1136     // call SIOCGIFCONF to enumerate the interfaces
1137     CHECKED_MALLOC3(buf, char *, ifc.ifc_len);
1138     ifc.ifc_buf = buf;
1139     if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
1140         JNU_ThrowByNameWithMessageAndLastError
1141             (env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGIFCONF) failed");
1142         free(buf);
1143         return ifs;
1144     }
1145 
1146     // iterate through each interface
1147     ifreqP = ifc.ifc_req;
1148     for (i = 0; i < ifc.ifc_len / sizeof(struct ifreq); i++, ifreqP++) {
1149         struct sockaddr addr, broadaddr, *broadaddrP = NULL;
1150         short prefix = 0;
1151 
1152         // ignore non IPv4 addresses
1153         if (ifreqP->ifr_addr.sa_family != AF_INET) {
1154             continue;
1155         }
1156 
1157         // save socket address
1158         memcpy(&addr, &(ifreqP->ifr_addr), sizeof(struct sockaddr));
1159 
1160         // determine broadcast address, if applicable
1161         if ((ioctl(sock, SIOCGIFFLAGS, ifreqP) == 0) &&
1162             ifreqP->ifr_flags & IFF_BROADCAST) {
1163 
1164             // restore socket address to ifreqP
1165             memcpy(&(ifreqP->ifr_addr), &addr, sizeof(struct sockaddr));
1166 
1167             if (ioctl(sock, SIOCGIFBRDADDR, ifreqP) == 0) {
1168                 memcpy(&broadaddr, &(ifreqP->ifr_broadaddr),
1169                        sizeof(struct sockaddr));
1170                 broadaddrP = &broadaddr;
1171             }
1172         }


1246  * Try to get the interface index.
1247  */
1248 static int getIndex(int sock, const char *name) {
1249     struct ifreq if2;
1250     memset((char *)&if2, 0, sizeof(if2));
1251     strncpy(if2.ifr_name, name, sizeof(if2.ifr_name) - 1);
1252 
1253     if (ioctl(sock, SIOCGIFINDEX, (char *)&if2) < 0) {
1254         return -1;
1255     }
1256 
1257     return if2.ifr_ifindex;
1258 }
1259 
1260 /*
1261  * Gets the Hardware address (usually MAC address) for the named interface.
1262  * On return puts the data in buf, and returns the length, in byte, of the
1263  * MAC address. Returns -1 if there is no hardware address on that interface.
1264  */
1265 static int getMacAddress
1266   (JNIEnv *env, const char *ifname, const struct in_addr *addr,
1267    unsigned char *buf)
1268 {
1269     static struct ifreq ifr;
1270     int i, sock;
1271 
1272     if ((sock = openSocketWithFallback(env, ifname)) < 0) {
1273         return -1;
1274     }
1275 
1276     memset((char *)&ifr, 0, sizeof(ifr));
1277     strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
1278     if (ioctl(sock, SIOCGIFHWADDR, &ifr) < 0) {
1279         JNU_ThrowByNameWithMessageAndLastError
1280             (env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGIFHWADDR) failed");
1281         close(sock);
1282         return -1;
1283     }
1284 
1285     close(sock);
1286     memcpy(buf, &ifr.ifr_hwaddr.sa_data, IFHWADDRLEN);
1287 
1288     // all bytes to 0 means no hardware address
1289     for (i = 0; i < IFHWADDRLEN; i++) {
1290         if (buf[i] != 0)
1291             return IFHWADDRLEN;
1292     }
1293 
1294     return -1;
1295 }
1296 
1297 static int getMTU(JNIEnv *env, int sock, const char *ifname) {
1298     struct ifreq if2;
1299     memset((char *)&if2, 0, sizeof(if2));
1300     strncpy(if2.ifr_name, ifname, sizeof(if2.ifr_name) - 1);
1301 
1302     if (ioctl(sock, SIOCGIFMTU, (char *)&if2) < 0) {
1303         JNU_ThrowByNameWithMessageAndLastError
1304             (env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGIFMTU) failed");
1305         return -1;


1377     }
1378 
1379     // call CSIOCGIFCONF instead of SIOCGIFCONF where interface
1380     // records will always have sizeof(struct ifreq) length.
1381     // Be aware that only IPv4 data is complete this way.
1382     CHECKED_MALLOC3(buf, char *, ifc.ifc_len);
1383     ifc.ifc_buf = buf;
1384     if (ioctl(sock, CSIOCGIFCONF, (char *)&ifc) < 0) {
1385         JNU_ThrowByNameWithMessageAndLastError
1386             (env, JNU_JAVANETPKG "SocketException", "ioctl(CSIOCGIFCONF) failed");
1387         free(buf);
1388         return ifs;
1389     }
1390 
1391     // iterate through each interface
1392     ifreqP = ifc.ifc_req;
1393     for (i = 0; i < ifc.ifc_len / sizeof(struct ifreq); i++, ifreqP++) {
1394         struct sockaddr addr, broadaddr, *broadaddrP = NULL;
1395         short prefix = 0;
1396 
1397         // ignore non IPv4 addresses
1398         if (ifreqP->ifr_addr.sa_family != AF_INET) {
1399             continue;
1400         }
1401 
1402         // save socket address
1403         memcpy(&addr, &(ifreqP->ifr_addr), sizeof(struct sockaddr));
1404 
1405         // determine broadcast address, if applicable
1406         if ((ioctl(sock, SIOCGIFFLAGS, ifreqP) == 0) &&
1407             ifreqP->ifr_flags & IFF_BROADCAST) {
1408 
1409             // restore socket address to ifreqP
1410             memcpy(&(ifreqP->ifr_addr), &addr, sizeof(struct sockaddr));
1411 
1412             if (ioctl(sock, SIOCGIFBRDADDR, ifreqP) == 0) {
1413                 memcpy(&broadaddr, &(ifreqP->ifr_broadaddr),
1414                        sizeof(struct sockaddr));
1415                 broadaddrP = &broadaddr;
1416             }
1417         }


1433         if ((*env)->ExceptionOccurred(env)) {
1434             free(buf);
1435             freeif(ifs);
1436             return NULL;
1437         }
1438     }
1439 
1440     // free buffer
1441     free(buf);
1442     return ifs;
1443 }
1444 
1445 #if defined(AF_INET6)
1446 
1447 /*
1448  * Enumerates and returns all IPv6 interfaces on AIX.
1449  */
1450 static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs) {
1451     struct ifconf ifc;
1452     struct ifreq *ifreqP;
1453     char *buf, *cp, *cplimit;
1454 
1455     // call SIOCGSIZIFCONF to get size for SIOCGIFCONF buffer
1456     if (ioctl(sock, SIOCGSIZIFCONF, &(ifc.ifc_len)) < 0) {
1457         JNU_ThrowByNameWithMessageAndLastError
1458             (env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGSIZIFCONF) failed");
1459         return ifs;
1460     }
1461 
1462     // call SIOCGIFCONF to enumerate the interfaces
1463     CHECKED_MALLOC3(buf, char *, ifc.ifc_len);
1464     ifc.ifc_buf = buf;
1465     if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
1466         JNU_ThrowByNameWithMessageAndLastError
1467             (env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGIFCONF) failed");
1468         free(buf);
1469         return ifs;
1470     }
1471 
1472     // iterate through each interface
1473     ifreqP = ifc.ifc_req;
1474     cp = (char *)ifc.ifc_req;
1475     cplimit = cp + ifc.ifc_len;
1476 
1477     for (; cp < cplimit;
1478          cp += (sizeof(ifreqP->ifr_name) +
1479                 MAX((ifreqP->ifr_addr).sa_len, sizeof(ifreqP->ifr_addr))))
1480     {
1481         ifreqP = (struct ifreq *)cp;
1482         short prefix = 0;
1483 
1484         // ignore non IPv6 addresses
1485         if (ifreqP->ifr_addr.sa_family != AF_INET6) {
1486             continue;
1487         }
1488 
1489         // determine netmask
1490         struct in6_ifreq if6;
1491         memset((char *)&if6, 0, sizeof(if6));
1492         strncpy(if6.ifr_name, ifreqP->ifr_name, sizeof(if6.ifr_name) - 1);
1493         memcpy(&(if6.ifr_Addr), &(ifreqP->ifr_addr),
1494                sizeof(struct sockaddr_in6));
1495         if (ioctl(sock, SIOCGIFNETMASK6, (char *)&if6) >= 0) {
1496             prefix = translateIPv6AddressToPrefix(&(if6.ifr_Addr));
1497         }
1498 
1499         // set scope ID to interface index
1500         ((struct sockaddr_in6 *)&(ifreqP->ifr_addr))->sin6_scope_id =
1501             getIndex(sock, ifreqP->ifr_name);
1502 
1503         // add interface to the list
1504         ifs = addif(env, sock, ifreqP->ifr_name, ifs,


1517     free(buf);
1518     return ifs;
1519 }
1520 
1521 #endif /* AF_INET6 */
1522 
1523 /*
1524  * Try to get the interface index.
1525  */
1526 static int getIndex(int sock, const char *name) {
1527     int index = if_nametoindex(name);
1528     return (index == 0) ? -1 : index;
1529 }
1530 
1531 /*
1532  * Gets the Hardware address (usually MAC address) for the named interface.
1533  * On return puts the data in buf, and returns the length, in byte, of the
1534  * MAC address. Returns -1 if there is no hardware address on that interface.
1535  */
1536 static int getMacAddress
1537   (JNIEnv *env, const char *ifname, const struct in_addr *addr,
1538    unsigned char *buf)
1539 {
1540     int size;
1541     struct kinfo_ndd *nddp;
1542     void *end;
1543 
1544     size = getkerninfo(KINFO_NDD, 0, 0, 0);
1545     if (size == 0) {
1546         return -1;
1547     }
1548 
1549     if (size < 0) {
1550         perror("getkerninfo 1");
1551         return -1;
1552     }
1553 
1554     nddp = (struct kinfo_ndd *)malloc(size);
1555 
1556     if (!nddp) {
1557         JNU_ThrowOutOfMemoryError(env,


1576     }
1577 
1578     return -1;
1579 }
1580 
1581 static int getMTU(JNIEnv *env, int sock, const char *ifname) {
1582     struct ifreq if2;
1583     memset((char *)&if2, 0, sizeof(if2));
1584     strncpy(if2.ifr_name, ifname, sizeof(if2.ifr_name) - 1);
1585 
1586     if (ioctl(sock, SIOCGIFMTU, (char *)&if2) < 0) {
1587         JNU_ThrowByNameWithMessageAndLastError
1588             (env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGIFMTU) failed");
1589         return -1;
1590     }
1591 
1592     return if2.ifr_mtu;
1593 }
1594 
1595 static int getFlags(int sock, const char *ifname, int *flags) {
1596     struct ifreq if2;
1597     memset((char *)&if2, 0, sizeof(if2));
1598     strncpy(if2.ifr_name, ifname, sizeof(if2.ifr_name) - 1);
1599 
1600     if (ioctl(sock, SIOCGIFFLAGS, (char *)&if2) < 0) {
1601         return -1;
1602     }
1603 
1604     if (sizeof(if2.ifr_flags) == sizeof(short)) {
1605         *flags = (if2.ifr_flags & 0xffff);
1606     } else {
1607         *flags = if2.ifr_flags;
1608     }
1609     return 0;
1610 }
1611 
1612 #endif /* _AIX */
1613 
1614 /** Solaris **/
1615 #if defined(__solaris__)
1616 
1617 #if defined(AF_INET6)
1618 /*
1619  * Opens a socket for further ioctl calls. Tries AF_INET socket first and
1620  * if it fails return AF_INET6 socket.
1621  */
1622 static int openSocketWithFallback(JNIEnv *env, const char *ifname) {
1623     int sock, alreadyV6 = 0;
1624     struct lifreq if2;
1625 
1626     if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1627         if (errno == EPROTONOSUPPORT) {
1628             if ((sock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1629                 JNU_ThrowByNameWithMessageAndLastError


1658     }
1659 
1660     return sock;
1661 }
1662 #else
1663 static int openSocketWithFallback(JNIEnv *env, const char *ifname) {
1664     return openSocket(env, AF_INET);
1665 }
1666 #endif
1667 
1668 /*
1669  * Enumerates and returns all IPv4 interfaces on Solaris.
1670  */
1671 static netif *enumIPv4Interfaces(JNIEnv *env, int sock, netif *ifs) {
1672     struct lifconf ifc;
1673     struct lifreq *ifreqP;
1674     struct lifnum numifs;
1675     char *buf = NULL;
1676     unsigned i;
1677 
1678     // call SIOCGLIFNUM to get the interface count
1679     numifs.lifn_family = AF_INET;
1680     numifs.lifn_flags = 0;
1681     if (ioctl(sock, SIOCGLIFNUM, (char *)&numifs) < 0) {
1682         JNU_ThrowByNameWithMessageAndLastError
1683             (env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGLIFNUM) failed");
1684         return ifs;
1685     }
1686 
1687     // call SIOCGLIFCONF to enumerate the interfaces
1688     ifc.lifc_len = numifs.lifn_count * sizeof(struct lifreq);
1689     CHECKED_MALLOC3(buf, char *, ifc.lifc_len);
1690     ifc.lifc_buf = buf;
1691     ifc.lifc_family = AF_INET;
1692     ifc.lifc_flags = 0;
1693     if (ioctl(sock, SIOCGLIFCONF, (char *)&ifc) < 0) {
1694         JNU_ThrowByNameWithMessageAndLastError
1695             (env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGLIFCONF) failed");
1696         free(buf);
1697         return ifs;
1698     }
1699 
1700     // iterate through each interface
1701     ifreqP = ifc.lifc_req;
1702     for (i = 0; i < numifs.lifn_count; i++, ifreqP++) {
1703         struct sockaddr addr, *broadaddrP = NULL;
1704 
1705         // ignore non IPv4 addresses
1706         if (ifreqP->lifr_addr.ss_family != AF_INET) {
1707             continue;
1708         }
1709 
1710         // save socket address
1711         memcpy(&addr, &(ifreqP->lifr_addr), sizeof(struct sockaddr));
1712 
1713         // determine broadcast address, if applicable
1714         if ((ioctl(sock, SIOCGLIFFLAGS, ifreqP) == 0) &&
1715             ifreqP->lifr_flags & IFF_BROADCAST) {
1716 
1717             // restore socket address to ifreqP
1718             memcpy(&(ifreqP->lifr_addr), &addr, sizeof(struct sockaddr));
1719 
1720             // query broadcast address and set pointer to it
1721             if (ioctl(sock, SIOCGLIFBRDADDR, ifreqP) == 0) {
1722                 broadaddrP = (struct sockaddr *)&(ifreqP->lifr_broadaddr);
1723             }
1724         }
1725 


1734         }
1735    }
1736 
1737     // free buffer
1738     free(buf);
1739     return ifs;
1740 }
1741 
1742 #if defined(AF_INET6)
1743 
1744 /*
1745  * Enumerates and returns all IPv6 interfaces on Solaris.
1746  */
1747 static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs) {
1748     struct lifconf ifc;
1749     struct lifreq *ifreqP;
1750     struct lifnum numifs;
1751     char *buf = NULL;
1752     unsigned i;
1753 
1754     // call SIOCGLIFNUM to get the interface count
1755     numifs.lifn_family = AF_INET6;
1756     numifs.lifn_flags = 0;
1757     if (ioctl(sock, SIOCGLIFNUM, (char *)&numifs) < 0) {
1758         JNU_ThrowByNameWithMessageAndLastError
1759             (env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGLIFNUM) failed");
1760         return ifs;
1761     }
1762 
1763     // call SIOCGLIFCONF to enumerate the interfaces
1764     ifc.lifc_len = numifs.lifn_count * sizeof(struct lifreq);
1765     CHECKED_MALLOC3(buf, char *, ifc.lifc_len);
1766     ifc.lifc_buf = buf;
1767     ifc.lifc_family = AF_INET6;
1768     ifc.lifc_flags = 0;
1769     if (ioctl(sock, SIOCGLIFCONF, (char *)&ifc) < 0) {
1770         JNU_ThrowByNameWithMessageAndLastError
1771             (env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGLIFCONF) failed");
1772         free(buf);
1773         return ifs;
1774     }
1775 
1776     // iterate through each interface
1777     ifreqP = ifc.lifc_req;
1778     for (i = 0; i < numifs.lifn_count; i++, ifreqP++) {
1779 
1780         // ignore non IPv6 addresses
1781         if (ifreqP->lifr_addr.ss_family != AF_INET6) {
1782             continue;
1783         }
1784 
1785         // set scope ID to interface index
1786         ((struct sockaddr_in6 *)&(ifreqP->lifr_addr))->sin6_scope_id =
1787             getIndex(sock, ifreqP->lifr_name);
1788 
1789         // add to the list
1790         ifs = addif(env, sock, ifreqP->lifr_name, ifs,
1791                     (struct sockaddr *)&(ifreqP->lifr_addr),
1792                     NULL, AF_INET6, (short)ifreqP->lifr_addrlen);
1793 
1794         // if an exception occurred we return immediately
1795         if ((*env)->ExceptionOccurred(env)) {
1796             free(buf);
1797             return ifs;
1798         }
1799     }
1800 
1801     // free buffer
1802     free(buf);
1803     return ifs;
1804 }
1805 
1806 #endif /* AF_INET6 */
1807 
1808 /*
1809  * Try to get the interface index.
1810  * (Not supported on Solaris 2.6 or 7)
1811  */
1812 static int getIndex(int sock, const char *name) {
1813     struct lifreq if2;
1814     memset((char *)&if2, 0, sizeof(if2));
1815     strncpy(if2.lifr_name, name, sizeof(if2.lifr_name) - 1);
1816 
1817     if (ioctl(sock, SIOCGLIFINDEX, (char *)&if2) < 0) {
1818         return -1;
1819     }


1824 /*
1825  * Solaris specific DLPI code to get hardware address from a device.
1826  * Unfortunately, at least up to Solaris X, you have to have special
1827  * privileges (i.e. be root).
1828  */
1829 static int getMacFromDevice
1830   (JNIEnv *env, const char *ifname, unsigned char *retbuf)
1831 {
1832     char style1dev[MAXPATHLEN];
1833     int fd;
1834     dl_phys_addr_req_t dlpareq;
1835     dl_phys_addr_ack_t *dlpaack;
1836     struct strbuf msg;
1837     char buf[128];
1838     int flags = 0;
1839 
1840     // Device is in /dev.  e.g.: /dev/bge0
1841     strcpy(style1dev, DEV_PREFIX);
1842     strcat(style1dev, ifname);
1843     if ((fd = open(style1dev, O_RDWR)) < 0) {
1844         // Can't open it. We probably are missing the privilege.
1845         // We'll have to try something else
1846         return 0;
1847     }
1848 
1849     dlpareq.dl_primitive = DL_PHYS_ADDR_REQ;
1850     dlpareq.dl_addr_type = DL_CURR_PHYS_ADDR;
1851 
1852     msg.buf = (char *)&dlpareq;
1853     msg.len = DL_PHYS_ADDR_REQ_SIZE;
1854 
1855     if (putmsg(fd, &msg, NULL, 0) < 0) {
1856         JNU_ThrowByNameWithMessageAndLastError
1857             (env, JNU_JAVANETPKG "SocketException", "putmsg() failed");
1858         return -1;
1859     }
1860 
1861     dlpaack = (dl_phys_addr_ack_t *)buf;
1862 
1863     msg.buf = (char *)buf;
1864     msg.len = 0;
1865     msg.maxlen = sizeof (buf);
1866     if (getmsg(fd, &msg, NULL, &flags) < 0) {


1868             (env, JNU_JAVANETPKG "SocketException", "getmsg() failed");
1869         return -1;
1870     }
1871 
1872     if (msg.len < DL_PHYS_ADDR_ACK_SIZE || dlpaack->dl_primitive != DL_PHYS_ADDR_ACK) {
1873         JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
1874                         "Couldn't obtain phys addr\n");
1875         return -1;
1876     }
1877 
1878     memcpy(retbuf, &buf[dlpaack->dl_addr_offset], dlpaack->dl_addr_length);
1879     return dlpaack->dl_addr_length;
1880 }
1881 
1882 /*
1883  * Gets the Hardware address (usually MAC address) for the named interface.
1884  * On return puts the data in buf, and returns the length, in byte, of the
1885  * MAC address. Returns -1 if there is no hardware address on that interface.
1886  */
1887 static int getMacAddress
1888   (JNIEnv *env, const char *ifname, const struct in_addr *addr,
1889    unsigned char *buf)
1890 {
1891     struct lifreq if2;
1892     int len, i, sock;
1893 
1894     if ((sock = openSocketWithFallback(env, ifname)) < 0) {
1895         return -1;
1896     }
1897 
1898     // First, try the new (S11) SIOCGLIFHWADDR ioctl(). If that fails
1899     // try the old way.
1900     memset((char *)&if2, 0, sizeof(if2));
1901     strncpy(if2.lifr_name, ifname, sizeof(if2.lifr_name) - 1);
1902 
1903     if (ioctl(sock, SIOCGLIFHWADDR, &if2) != -1) {
1904         struct sockaddr_dl *sp;
1905         sp = (struct sockaddr_dl *)&if2.lifr_addr;
1906         memcpy(buf, &sp->sdl_data[0], sp->sdl_alen);
1907         close(sock);
1908         return sp->sdl_alen;
1909     }
1910 
1911     // On Solaris we have to use DLPI, but it will only work if we have
1912     // privileged access (i.e. root). If that fails, we try a lookup
1913     // in the ARP table, which requires an IPv4 address.
1914     if (((len = getMacFromDevice(env, ifname, buf)) == 0) && (addr != NULL)) {


1915         struct arpreq arpreq;
1916         struct sockaddr_in *sin;
1917         struct sockaddr_in ipAddr;
1918 
1919         len = 6; //???

















1920 
1921         sin = (struct sockaddr_in *)&arpreq.arp_pa;
1922         memset((char *)&arpreq, 0, sizeof(struct arpreq));
1923         ipAddr.sin_port = 0;
1924         ipAddr.sin_family = AF_INET;
1925         memcpy(&ipAddr.sin_addr, addr, sizeof(struct in_addr));
1926         memcpy(&arpreq.arp_pa, &ipAddr, sizeof(struct sockaddr_in));
1927         arpreq.arp_flags= ATF_PUBL;
1928 
1929         if (ioctl(sock, SIOCGARP, &arpreq) < 0) {
1930             close(sock);
1931             return -1;
1932         }
1933 
1934         memcpy(buf, &arpreq.arp_ha.sa_data[0], len);
1935     }
1936     close(sock);
1937 
1938     // all bytes to 0 means no hardware address
1939     for (i = 0; i < len; i++) {
1940         if (buf[i] != 0)
1941             return len;
1942     }
1943 
1944     return -1;
1945 }
1946 
1947 static int getMTU(JNIEnv *env, int sock, const char *ifname) {
1948     struct lifreq if2;
1949     memset((char *)&if2, 0, sizeof(if2));
1950     strncpy(if2.lifr_name, ifname, sizeof(if2.lifr_name) - 1);
1951 
1952     if (ioctl(sock, SIOCGLIFMTU, (char *)&if2) < 0) {
1953         JNU_ThrowByNameWithMessageAndLastError
1954             (env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGLIFMTU) failed");
1955         return -1;
1956     }


2004 static int openSocketWithFallback(JNIEnv *env, const char *ifname) {
2005     return openSocket(env, AF_INET);
2006 }
2007 #endif
2008 
2009 /*
2010  * Enumerates and returns all IPv4 interfaces on BSD.
2011  */
2012 static netif *enumIPv4Interfaces(JNIEnv *env, int sock, netif *ifs) {
2013     struct ifaddrs *ifa, *origifa;
2014 
2015     if (getifaddrs(&origifa) != 0) {
2016         JNU_ThrowByNameWithMessageAndLastError
2017             (env, JNU_JAVANETPKG "SocketException", "getifaddrs() failed");
2018         return ifs;
2019     }
2020 
2021     for (ifa = origifa; ifa != NULL; ifa = ifa->ifa_next) {
2022         struct sockaddr *broadaddrP = NULL;
2023 
2024         // ignore non IPv4 addresses
2025         if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family != AF_INET)
2026             continue;
2027 
2028         // set ifa_broadaddr, if there is one
2029         if ((ifa->ifa_flags & IFF_POINTOPOINT) == 0 &&
2030             ifa->ifa_flags & IFF_BROADCAST) {
2031             broadaddrP = ifa->ifa_dstaddr;
2032         }
2033 
2034         // add interface to the list
2035         ifs = addif(env, sock, ifa->ifa_name, ifs, ifa->ifa_addr,
2036                     broadaddrP, AF_INET,
2037                     translateIPv4AddressToPrefix((struct sockaddr_in *)
2038                                                  ifa->ifa_netmask));
2039 
2040         // if an exception occurred then free the list
2041         if ((*env)->ExceptionOccurred(env)) {
2042             freeifaddrs(origifa);
2043             freeif(ifs);
2044             return NULL;


2048     // free ifaddrs buffer
2049     freeifaddrs(origifa);
2050     return ifs;
2051 }
2052 
2053 #if defined(AF_INET6)
2054 
2055 /*
2056  * Enumerates and returns all IPv6 interfaces on BSD.
2057  */
2058 static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs) {
2059     struct ifaddrs *ifa, *origifa;
2060 
2061     if (getifaddrs(&origifa) != 0) {
2062         JNU_ThrowByNameWithMessageAndLastError
2063             (env, JNU_JAVANETPKG "SocketException", "getifaddrs() failed");
2064         return ifs;
2065     }
2066 
2067     for (ifa = origifa; ifa != NULL; ifa = ifa->ifa_next) {
2068         // ignore non IPv6 addresses
2069         if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family != AF_INET6)
2070             continue;
2071 
2072         // set scope ID to interface index
2073         ((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_scope_id =
2074             getIndex(sock, ifa->ifa_name);
2075 
2076         // add interface to the list
2077         ifs = addif(env, sock, ifa->ifa_name, ifs, ifa->ifa_addr, NULL,
2078                     AF_INET6,
2079                     translateIPv6AddressToPrefix((struct sockaddr_in6 *)
2080                                                  ifa->ifa_netmask));
2081 
2082         // if an exception occurred then free the list
2083         if ((*env)->ExceptionOccurred(env)) {
2084             freeifaddrs(origifa);
2085             freeif(ifs);
2086             return NULL;
2087         }
2088     }


2103     return (index == 0) ? -1 : index;
2104 #else
2105     struct ifreq if2;
2106     memset((char *)&if2, 0, sizeof(if2));
2107     strncpy(if2.ifr_name, name, sizeof(if2.ifr_name) - 1);
2108 
2109     if (ioctl(sock, SIOCGIFINDEX, (char *)&if2) < 0) {
2110         return -1;
2111     }
2112 
2113     return if2.ifr_index;
2114 #endif
2115 }
2116 
2117 /*
2118  * Gets the Hardware address (usually MAC address) for the named interface.
2119  * On return puts the data in buf, and returns the length, in byte, of the
2120  * MAC address. Returns -1 if there is no hardware address on that interface.
2121  */
2122 static int getMacAddress
2123   (JNIEnv *env, const char *ifname, const struct in_addr *addr,
2124    unsigned char *buf)
2125 {
2126     struct ifaddrs *ifa0, *ifa;
2127     struct sockaddr *saddr;
2128     int i;
2129 
2130     // grab the interface list
2131     if (!getifaddrs(&ifa0)) {
2132         // cycle through the interfaces
2133         for (i = 0, ifa = ifa0; ifa != NULL; ifa = ifa->ifa_next, i++) {
2134             saddr = ifa->ifa_addr;
2135             // link layer contains the MAC address
2136             if (saddr->sa_family == AF_LINK && !strcmp(ifname, ifa->ifa_name)) {
2137                 struct sockaddr_dl *sadl = (struct sockaddr_dl *) saddr;
2138                 // check the address has the correct length
2139                 if (sadl->sdl_alen == ETHER_ADDR_LEN) {
2140                     memcpy(buf, (sadl->sdl_data + sadl->sdl_nlen), ETHER_ADDR_LEN);
2141                     freeifaddrs(ifa0);
2142                     return ETHER_ADDR_LEN;
2143                 }
2144             }
2145         }
2146         freeifaddrs(ifa0);
2147     }
2148 
2149     return -1;
2150 }
2151 
2152 static int getMTU(JNIEnv *env, int sock, const char *ifname) {
2153     struct ifreq if2;
2154     memset((char *)&if2, 0, sizeof(if2));
2155     strncpy(if2.ifr_name, ifname, sizeof(if2.ifr_name) - 1);
2156 
2157     if (ioctl(sock, SIOCGIFMTU, (char *)&if2) < 0) {
2158         JNU_ThrowByNameWithMessageAndLastError


< prev index next >