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