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