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