NetworkInterface.c 63.2 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
7
 * published by the Free Software Foundation.  Oracle designates this
D
duke 已提交
8
 * particular file as subject to the "Classpath" exception as provided
9
 * by Oracle in the LICENSE file that accompanied this code.
D
duke 已提交
10 11 12 13 14 15 16 17 18 19 20
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
21 22 23
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
24 25 26 27 28 29 30 31 32 33 34 35
 */

#include <errno.h>
#include <strings.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <net/if_arp.h>
36

37
#if defined(__solaris__)
D
duke 已提交
38 39 40
#include <sys/dlpi.h>
#include <fcntl.h>
#include <stropts.h>
41
#include <sys/sockio.h>
D
duke 已提交
42
#endif
43

44
#if defined(__linux__)
D
duke 已提交
45 46 47 48 49
#include <sys/ioctl.h>
#include <sys/utsname.h>
#include <stdio.h>
#endif

50 51 52 53 54 55 56
#if defined(_AIX)
#include <sys/ioctl.h>
#include <netinet/in6_var.h>
#include <sys/ndd_var.h>
#include <sys/kinfo.h>
#endif

57 58 59 60 61 62 63 64 65 66 67 68 69
#if defined(_ALLBSD_SOURCE)
#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/sockio.h>
#if defined(__APPLE__)
#include <net/ethernet.h>
#include <net/if_var.h>
#include <net/if_dl.h>
#include <netinet/in_var.h>
#include <ifaddrs.h>
#endif
#endif

D
duke 已提交
70 71 72 73
#include "jvm.h"
#include "jni_util.h"
#include "net_util.h"

74
#if defined(__linux__)
75 76 77 78 79 80
    #define _PATH_PROCNET_IFINET6 "/proc/net/if_inet6"
#elif defined(__solaris__)
    #ifndef SIOCGLIFHWADDR
        #define SIOCGLIFHWADDR _IOWR('i', 192, struct lifreq)
    #endif
    #define DEV_PREFIX "/dev/"
81 82
#endif

83 84 85 86 87 88
#ifdef LIFNAMSIZ
    #define IFNAMESIZE LIFNAMSIZ
#else
    #define IFNAMESIZE IFNAMSIZ
#endif

89 90 91 92 93 94 95 96 97
#define CHECKED_MALLOC3(_pointer, _type, _size) \
    do { \
        _pointer = (_type)malloc(_size); \
        if (_pointer == NULL) { \
            JNU_ThrowOutOfMemoryError(env, "Native heap allocation failed"); \
            return ifs; /* return untouched list */ \
        } \
    } while(0)

D
duke 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
typedef struct _netaddr  {
    struct sockaddr *addr;
    struct sockaddr *brdcast;
    short mask;
    int family; /* to make searches simple */
    struct _netaddr *next;
} netaddr;

typedef struct _netif {
    char *name;
    int index;
    char virtual;
    netaddr *addr;
    struct _netif *childs;
    struct _netif *next;
} netif;

/************************************************************************
 * NetworkInterface
 */

#include "java_net_NetworkInterface.h"

/************************************************************************
 * NetworkInterface
 */
jclass ni_class;
jfieldID ni_nameID;
jfieldID ni_indexID;
jfieldID ni_descID;
jfieldID ni_addrsID;
jfieldID ni_bindsID;
jfieldID ni_virutalID;
jfieldID ni_childsID;
jfieldID ni_parentID;
133
jfieldID ni_defaultIndexID;
D
duke 已提交
134 135 136 137 138 139 140 141
jmethodID ni_ctrID;

static jclass ni_ibcls;
static jmethodID ni_ibctrID;
static jfieldID ni_ibaddressID;
static jfieldID ni_ib4broadcastID;
static jfieldID ni_ib4maskID;

142
/** Private methods declarations **/
D
duke 已提交
143
static jobject createNetworkInterface(JNIEnv *env, netif *ifs);
144 145 146 147
static int     getFlags0(JNIEnv *env, jstring  ifname);

static netif  *enumInterfaces(JNIEnv *env);
static netif  *enumIPv4Interfaces(JNIEnv *env, int sock, netif *ifs);
D
duke 已提交
148

149
#if defined(AF_INET6)
150 151 152
static netif  *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs);
#endif

153
static netif  *addif(JNIEnv *env, int sock, const char *if_name, netif *ifs,
154 155 156
                     struct sockaddr *ifr_addrP,
                     struct sockaddr *ifr_broadaddrP,
                     int family, short prefix);
157 158 159 160 161
static void    freeif(netif *ifs);

static int     openSocket(JNIEnv *env, int proto);
static int     openSocketWithFallback(JNIEnv *env, const char *ifname);

162 163
static short   translateIPv4AddressToPrefix(struct sockaddr_in *addr);
static short   translateIPv6AddressToPrefix(struct sockaddr_in6 *addr);
164

165
static int     getIndex(int sock, const char *ifname);
166
static int     getFlags(int sock, const char *ifname, int *flags);
167
static int     getMacAddress(JNIEnv *env, const char *ifname,
168
                             const struct in_addr *addr, unsigned char *buf);
169 170
static int     getMTU(JNIEnv *env, int sock, const char *ifname);

171 172 173
#if defined(__solaris__)
static int     getMacFromDevice(JNIEnv *env, const char *ifname,
                                unsigned char *retbuf);
174
#endif
175 176

/******************* Java entry points *****************************/
D
duke 已提交
177 178 179 180 181 182

/*
 * Class:     java_net_NetworkInterface
 * Method:    init
 * Signature: ()V
 */
183 184 185 186
JNIEXPORT void JNICALL Java_java_net_NetworkInterface_init
  (JNIEnv *env, jclass cls)
{
    ni_class = (*env)->FindClass(env, "java/net/NetworkInterface");
187
    CHECK_NULL(ni_class);
D
duke 已提交
188
    ni_class = (*env)->NewGlobalRef(env, ni_class);
189
    CHECK_NULL(ni_class);
190
    ni_nameID = (*env)->GetFieldID(env, ni_class, "name", "Ljava/lang/String;");
191
    CHECK_NULL(ni_nameID);
D
duke 已提交
192
    ni_indexID = (*env)->GetFieldID(env, ni_class, "index", "I");
193
    CHECK_NULL(ni_indexID);
194 195
    ni_addrsID = (*env)->GetFieldID(env, ni_class, "addrs",
                                    "[Ljava/net/InetAddress;");
196
    CHECK_NULL(ni_addrsID);
197 198
    ni_bindsID = (*env)->GetFieldID(env, ni_class, "bindings",
                                    "[Ljava/net/InterfaceAddress;");
199
    CHECK_NULL(ni_bindsID);
200 201
    ni_descID = (*env)->GetFieldID(env, ni_class, "displayName",
                                   "Ljava/lang/String;");
202
    CHECK_NULL(ni_descID);
D
duke 已提交
203
    ni_virutalID = (*env)->GetFieldID(env, ni_class, "virtual", "Z");
204
    CHECK_NULL(ni_virutalID);
205 206
    ni_childsID = (*env)->GetFieldID(env, ni_class, "childs",
                                     "[Ljava/net/NetworkInterface;");
207
    CHECK_NULL(ni_childsID);
208 209
    ni_parentID = (*env)->GetFieldID(env, ni_class, "parent",
                                     "Ljava/net/NetworkInterface;");
210
    CHECK_NULL(ni_parentID);
D
duke 已提交
211
    ni_ctrID = (*env)->GetMethodID(env, ni_class, "<init>", "()V");
212
    CHECK_NULL(ni_ctrID);
D
duke 已提交
213
    ni_ibcls = (*env)->FindClass(env, "java/net/InterfaceAddress");
214
    CHECK_NULL(ni_ibcls);
D
duke 已提交
215
    ni_ibcls = (*env)->NewGlobalRef(env, ni_ibcls);
216
    CHECK_NULL(ni_ibcls);
D
duke 已提交
217
    ni_ibctrID = (*env)->GetMethodID(env, ni_ibcls, "<init>", "()V");
218
    CHECK_NULL(ni_ibctrID);
219 220
    ni_ibaddressID = (*env)->GetFieldID(env, ni_ibcls, "address",
                                        "Ljava/net/InetAddress;");
221
    CHECK_NULL(ni_ibaddressID);
222 223
    ni_ib4broadcastID = (*env)->GetFieldID(env, ni_ibcls, "broadcast",
                                           "Ljava/net/Inet4Address;");
224
    CHECK_NULL(ni_ib4broadcastID);
D
duke 已提交
225
    ni_ib4maskID = (*env)->GetFieldID(env, ni_ibcls, "maskLength", "S");
226
    CHECK_NULL(ni_ib4maskID);
227 228
    ni_defaultIndexID = (*env)->GetStaticFieldID(env, ni_class, "defaultIndex",
                                                 "I");
229 230 231
    CHECK_NULL(ni_defaultIndexID);

    initInetAddressIDs(env);
D
duke 已提交
232 233 234 235 236 237 238 239
}

/*
 * Class:     java_net_NetworkInterface
 * Method:    getByName0
 * Signature: (Ljava/lang/String;)Ljava/net/NetworkInterface;
 */
JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByName0
240 241
  (JNIEnv *env, jclass cls, jstring name)
{
D
duke 已提交
242 243
    netif *ifs, *curr;
    jboolean isCopy;
244
    const char* name_utf;
D
duke 已提交
245 246
    jobject obj = NULL;

247 248 249 250
    if (name != NULL) {
        name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
    } else {
        JNU_ThrowNullPointerException(env, "network interface name is NULL");
D
duke 已提交
251 252 253
        return NULL;
    }

254
    if (name_utf == NULL) {
255 256 257 258 259 260 261 262
        if (!(*env)->ExceptionCheck(env))
            JNU_ThrowOutOfMemoryError(env, NULL);
        return NULL;
    }

    ifs = enumInterfaces(env);
    if (ifs == NULL) {
        return NULL;
263
    }
264

265
    // search the list of interfaces based on name
D
duke 已提交
266 267 268 269 270 271 272 273
    curr = ifs;
    while (curr != NULL) {
        if (strcmp(name_utf, curr->name) == 0) {
            break;
        }
        curr = curr->next;
    }

274
    // if found create a NetworkInterface
275
    if (curr != NULL) {
D
duke 已提交
276 277 278
        obj = createNetworkInterface(env, curr);
    }

279
    // release the UTF string and interface list
D
duke 已提交
280 281 282 283 284 285 286 287
    (*env)->ReleaseStringUTFChars(env, name, name_utf);
    freeif(ifs);

    return obj;
}

/*
 * Class:     java_net_NetworkInterface
288
 * Method:    getByIndex0
D
duke 已提交
289 290
 * Signature: (Ljava/lang/String;)Ljava/net/NetworkInterface;
 */
291
JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByIndex0
292 293
  (JNIEnv *env, jclass cls, jint index)
{
D
duke 已提交
294 295 296 297 298 299 300 301 302 303 304 305
    netif *ifs, *curr;
    jobject obj = NULL;

    if (index <= 0) {
        return NULL;
    }

    ifs = enumInterfaces(env);
    if (ifs == NULL) {
        return NULL;
    }

306
    // search the list of interfaces based on index
D
duke 已提交
307 308 309 310 311 312 313 314
    curr = ifs;
    while (curr != NULL) {
        if (index == curr->index) {
            break;
        }
        curr = curr->next;
    }

315
    // if found create a NetworkInterface
316
    if (curr != NULL) {
D
duke 已提交
317 318 319
        obj = createNetworkInterface(env, curr);
    }

320
    // release the interface list
D
duke 已提交
321
    freeif(ifs);
322

D
duke 已提交
323 324 325 326 327 328 329 330 331
    return obj;
}

/*
 * Class:     java_net_NetworkInterface
 * Method:    getByInetAddress0
 * Signature: (Ljava/net/InetAddress;)Ljava/net/NetworkInterface;
 */
JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByInetAddress0
332 333
  (JNIEnv *env, jclass cls, jobject iaObj)
{
D
duke 已提交
334
    netif *ifs, *curr;
335
#if defined(AF_INET6)
336
    int family = (getInetAddress_family(env, iaObj) == IPv4) ? AF_INET : AF_INET6;
C
chegar 已提交
337
#else
338
    int family =  AF_INET;
C
chegar 已提交
339
#endif
D
duke 已提交
340 341 342 343 344 345 346 347 348 349 350 351
    jobject obj = NULL;
    jboolean match = JNI_FALSE;

    ifs = enumInterfaces(env);
    if (ifs == NULL) {
        return NULL;
    }

    curr = ifs;
    while (curr != NULL) {
        netaddr *addrP = curr->addr;

352
        // iterate through each address on the interface
D
duke 已提交
353 354 355 356
        while (addrP != NULL) {

            if (family == addrP->family) {
                if (family == AF_INET) {
357
                    int address1 = htonl(
358
                        ((struct sockaddr_in *)addrP->addr)->sin_addr.s_addr);
359
                    int address2 = getInetAddress_addr(env, iaObj);
D
duke 已提交
360 361 362 363 364 365

                    if (address1 == address2) {
                        match = JNI_TRUE;
                        break;
                    }
                }
366
#if defined(AF_INET6)
D
duke 已提交
367
                if (family == AF_INET6) {
368 369
                    jbyte *bytes = (jbyte *)&(
                        ((struct sockaddr_in6*)addrP->addr)->sin6_addr);
D
duke 已提交
370 371
                    jbyte caddr[16];
                    int i;
M
michaelm 已提交
372
                    getInet6Address_ipaddress(env, iaObj, (char *)caddr);
D
duke 已提交
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
                    i = 0;
                    while (i < 16) {
                        if (caddr[i] != bytes[i]) {
                            break;
                        }
                        i++;
                    }
                    if (i >= 16) {
                        match = JNI_TRUE;
                        break;
                    }
                }
#endif
            }

            if (match) {
                break;
            }
            addrP = addrP->next;
        }

        if (match) {
            break;
        }
        curr = curr->next;
    }

400
    // if found create a NetworkInterface
401
    if (match) {
D
duke 已提交
402 403 404
        obj = createNetworkInterface(env, curr);
    }

405
    // release the interface list
D
duke 已提交
406
    freeif(ifs);
407

D
duke 已提交
408 409 410 411 412 413 414 415 416
    return obj;
}

/*
 * Class:     java_net_NetworkInterface
 * Method:    getAll
 * Signature: ()[Ljava/net/NetworkInterface;
 */
JNIEXPORT jobjectArray JNICALL Java_java_net_NetworkInterface_getAll
417 418
  (JNIEnv *env, jclass cls)
{
D
duke 已提交
419 420 421 422 423 424 425 426 427
    netif *ifs, *curr;
    jobjectArray netIFArr;
    jint arr_index, ifCount;

    ifs = enumInterfaces(env);
    if (ifs == NULL) {
        return NULL;
    }

428
    // count the interfaces
D
duke 已提交
429 430 431 432 433 434 435
    ifCount = 0;
    curr = ifs;
    while (curr != NULL) {
        ifCount++;
        curr = curr->next;
    }

436
    // allocate a NetworkInterface array
D
duke 已提交
437 438 439 440 441 442
    netIFArr = (*env)->NewObjectArray(env, ifCount, cls, NULL);
    if (netIFArr == NULL) {
        freeif(ifs);
        return NULL;
    }

443 444
    // iterate through the interfaces, create a NetworkInterface instance
    // for each array element and populate the object
D
duke 已提交
445 446 447 448 449 450 451 452 453 454 455
    curr = ifs;
    arr_index = 0;
    while (curr != NULL) {
        jobject netifObj;

        netifObj = createNetworkInterface(env, curr);
        if (netifObj == NULL) {
            freeif(ifs);
            return NULL;
        }

456
        // put the NetworkInterface into the array
D
duke 已提交
457 458 459 460 461
        (*env)->SetObjectArrayElement(env, netIFArr, arr_index++, netifObj);

        curr = curr->next;
    }

462
    // release the interface list
D
duke 已提交
463
    freeif(ifs);
464

D
duke 已提交
465 466 467
    return netIFArr;
}

468 469 470 471 472
/*
 * Class:     java_net_NetworkInterface
 * Method:    isUp0
 * Signature: (Ljava/lang/String;I)Z
 */
473 474 475
JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isUp0
  (JNIEnv *env, jclass cls, jstring name, jint index)
{
476 477 478 479 480 481 482 483 484
    int ret = getFlags0(env, name);
    return ((ret & IFF_UP) && (ret & IFF_RUNNING)) ? JNI_TRUE :  JNI_FALSE;
}

/*
 * Class:     java_net_NetworkInterface
 * Method:    isP2P0
 * Signature: (Ljava/lang/String;I)Z
 */
485 486 487
JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isP2P0
  (JNIEnv *env, jclass cls, jstring name, jint index)
{
488 489 490 491 492 493 494 495 496
    int ret = getFlags0(env, name);
    return (ret & IFF_POINTOPOINT) ? JNI_TRUE :  JNI_FALSE;
}

/*
 * Class:     java_net_NetworkInterface
 * Method:    isLoopback0
 * Signature: (Ljava/lang/String;I)Z
 */
497 498 499
JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isLoopback0
  (JNIEnv *env, jclass cls, jstring name, jint index)
{
500 501 502 503 504 505 506 507 508
    int ret = getFlags0(env, name);
    return (ret & IFF_LOOPBACK) ? JNI_TRUE :  JNI_FALSE;
}

/*
 * Class:     java_net_NetworkInterface
 * Method:    supportsMulticast0
 * Signature: (Ljava/lang/String;I)Z
 */
509 510 511
JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_supportsMulticast0
  (JNIEnv *env, jclass cls, jstring name, jint index)
{
512 513 514 515 516 517 518 519 520
    int ret = getFlags0(env, name);
    return (ret & IFF_MULTICAST) ? JNI_TRUE :  JNI_FALSE;
}

/*
 * Class:     java_net_NetworkInterface
 * Method:    getMacAddr0
 * Signature: ([bLjava/lang/String;I)[b
 */
521 522 523
JNIEXPORT jbyteArray JNICALL Java_java_net_NetworkInterface_getMacAddr0
  (JNIEnv *env, jclass cls, jbyteArray addrArray, jstring name, jint index)
{
524 525 526 527 528 529 530
    jint addr;
    jbyte caddr[4];
    struct in_addr iaddr;
    jbyteArray ret = NULL;
    unsigned char mac[16];
    int len;
    jboolean isCopy;
531
    const char *name_utf;
532

533 534 535 536 537
    if (name != NULL) {
        name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
    } else {
        JNU_ThrowNullPointerException(env, "network interface name is NULL");
        return NULL;
538
    }
539 540 541 542 543

    if (name_utf == NULL) {
        if (!(*env)->ExceptionCheck(env))
            JNU_ThrowOutOfMemoryError(env, NULL);
        return NULL;
544 545 546
    }

    if (!IS_NULL(addrArray)) {
547 548 549 550 551 552 553
        (*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr);
        addr = ((caddr[0]<<24) & 0xff000000);
        addr |= ((caddr[1] <<16) & 0xff0000);
        addr |= ((caddr[2] <<8) & 0xff00);
        addr |= (caddr[3] & 0xff);
        iaddr.s_addr = htonl(addr);
        len = getMacAddress(env, name_utf, &iaddr, mac);
554
    } else {
555
        len = getMacAddress(env, name_utf, NULL, mac);
556
    }
557

558
    if (len > 0) {
559 560 561 562
        ret = (*env)->NewByteArray(env, len);
        if (!IS_NULL(ret)) {
            (*env)->SetByteArrayRegion(env, ret, 0, len, (jbyte *)(mac));
        }
563 564
    }

565 566 567 568
    // release the UTF string and interface list
    (*env)->ReleaseStringUTFChars(env, name, name_utf);

    return ret;
569 570 571 572 573 574 575
}

/*
 * Class:       java_net_NetworkInterface
 * Method:      getMTU0
 * Signature:   ([bLjava/lang/String;I)I
 */
576 577 578
JNIEXPORT jint JNICALL Java_java_net_NetworkInterface_getMTU0
  (JNIEnv *env, jclass cls, jstring name, jint index)
{
579
    jboolean isCopy;
580
    int sock, ret = -1;
581
    const char* name_utf = NULL;
582

583 584 585 586 587 588
    if (name != NULL) {
        name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
    } else {
        JNU_ThrowNullPointerException(env, "network interface name is NULL");
        return ret;
    }
589

590
    if (name_utf == NULL) {
591 592 593
        if (!(*env)->ExceptionCheck(env))
            JNU_ThrowOutOfMemoryError(env, NULL);
        return ret;
594
    }
595

596
    if ((sock = openSocketWithFallback(env, name_utf)) < 0) {
597 598
        (*env)->ReleaseStringUTFChars(env, name, name_utf);
        return JNI_FALSE;
599 600 601 602 603 604 605 606 607 608 609 610 611 612
    }

    ret = getMTU(env, sock, name_utf);

    (*env)->ReleaseStringUTFChars(env, name, name_utf);

    close(sock);
    return ret;
}

/*** Private methods definitions ****/

static int getFlags0(JNIEnv *env, jstring name) {
    jboolean isCopy;
613 614
    int ret, sock, flags = 0;
    const char *name_utf;
615

616 617 618 619 620 621
    if (name != NULL) {
        name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
    } else {
        JNU_ThrowNullPointerException(env, "network interface name is NULL");
        return -1;
    }
622

623
    if (name_utf == NULL) {
624 625 626
        if (!(*env)->ExceptionCheck(env))
            JNU_ThrowOutOfMemoryError(env, NULL);
        return -1;
627
    }
628 629
    if ((sock = openSocketWithFallback(env, name_utf)) < 0) {
        (*env)->ReleaseStringUTFChars(env, name, name_utf);
630
        return -1;
631 632
    }

633
    ret = getFlags(sock, name_utf, &flags);
634 635 636 637 638

    close(sock);
    (*env)->ReleaseStringUTFChars(env, name, name_utf);

    if (ret < 0) {
639 640
        NET_ThrowByNameWithLastError
            (env, JNU_JAVANETPKG "SocketException", "getFlags() failed");
641 642 643
        return -1;
    }

644
    return flags;
645 646
}

D
duke 已提交
647
/*
648 649
 * Creates a NetworkInterface object, populates the name, the index, and
 * populates the InetAddress array based on the IP addresses for this
D
duke 已提交
650 651
 * interface.
 */
652
static jobject createNetworkInterface(JNIEnv *env, netif *ifs) {
D
duke 已提交
653 654 655 656 657
    jobject netifObj;
    jobject name;
    jobjectArray addrArr;
    jobjectArray bindArr;
    jobjectArray childArr;
658
    netaddr *addrs;
D
duke 已提交
659 660 661 662 663 664
    jint addr_index, addr_count, bind_index;
    jint child_count, child_index;
    netaddr *addrP;
    netif *childP;
    jobject tmp;

665
    // create a NetworkInterface object and populate it
D
duke 已提交
666
    netifObj = (*env)->NewObject(env, ni_class, ni_ctrID);
667
    CHECK_NULL_RETURN(netifObj, NULL);
D
duke 已提交
668
    name = (*env)->NewStringUTF(env, ifs->name);
669
    CHECK_NULL_RETURN(name, NULL);
D
duke 已提交
670 671 672
    (*env)->SetObjectField(env, netifObj, ni_nameID, name);
    (*env)->SetObjectField(env, netifObj, ni_descID, name);
    (*env)->SetIntField(env, netifObj, ni_indexID, ifs->index);
673 674
    (*env)->SetBooleanField(env, netifObj, ni_virutalID,
                            ifs->virtual ? JNI_TRUE : JNI_FALSE);
D
duke 已提交
675

676
    // count the number of addresses on this interface
D
duke 已提交
677 678 679 680 681 682 683
    addr_count = 0;
    addrP = ifs->addr;
    while (addrP != NULL) {
        addr_count++;
        addrP = addrP->next;
    }

684
    // create the array of InetAddresses
685
    addrArr = (*env)->NewObjectArray(env, addr_count,  ia_class, NULL);
D
duke 已提交
686 687 688 689 690 691
    if (addrArr == NULL) {
        return NULL;
    }

    bindArr = (*env)->NewObjectArray(env, addr_count, ni_ibcls, NULL);
    if (bindArr == NULL) {
692
       return NULL;
D
duke 已提交
693 694 695 696 697 698 699 700 701
    }
    addrP = ifs->addr;
    addr_index = 0;
    bind_index = 0;
    while (addrP != NULL) {
        jobject iaObj = NULL;
        jobject ibObj = NULL;

        if (addrP->family == AF_INET) {
702
            iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
D
duke 已提交
703
            if (iaObj) {
704 705
                setInetAddress_addr(env, iaObj, htonl(
                    ((struct sockaddr_in*)addrP->addr)->sin_addr.s_addr));
706 707
            } else {
                return NULL;
D
duke 已提交
708 709 710
            }
            ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID);
            if (ibObj) {
711 712
                (*env)->SetObjectField(env, ibObj, ni_ibaddressID, iaObj);
                if (addrP->brdcast) {
713
                    jobject ia2Obj = NULL;
714
                    ia2Obj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
715
                    if (ia2Obj) {
716 717 718
                        setInetAddress_addr(env, ia2Obj, htonl(
                            ((struct sockaddr_in*)addrP->brdcast)->sin_addr.s_addr));
                        (*env)->SetObjectField(env, ibObj, ni_ib4broadcastID, ia2Obj);
719 720
                    } else {
                        return NULL;
721
                    }
722 723 724
                }
                (*env)->SetShortField(env, ibObj, ni_ib4maskID, addrP->mask);
                (*env)->SetObjectArrayElement(env, bindArr, bind_index++, ibObj);
725 726
            } else {
                return NULL;
D
duke 已提交
727 728
            }
        }
729
#if defined(AF_INET6)
D
duke 已提交
730 731
        if (addrP->family == AF_INET6) {
            int scope=0;
732
            iaObj = (*env)->NewObject(env, ia6_class, ia6_ctrID);
D
duke 已提交
733
            if (iaObj) {
734 735
                jboolean ret = setInet6Address_ipaddress(env, iaObj,
                    (char *)&(((struct sockaddr_in6*)addrP->addr)->sin6_addr));
M
michaelm 已提交
736
                if (ret == JNI_FALSE) {
D
duke 已提交
737 738
                    return NULL;
                }
739

D
duke 已提交
740
                scope = ((struct sockaddr_in6*)addrP->addr)->sin6_scope_id;
741

D
duke 已提交
742
                if (scope != 0) { /* zero is default value, no need to set */
M
michaelm 已提交
743 744
                    setInet6Address_scopeid(env, iaObj, scope);
                    setInet6Address_scopeifname(env, iaObj, netifObj);
D
duke 已提交
745
                }
746 747
            } else {
                return NULL;
D
duke 已提交
748 749 750
            }
            ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID);
            if (ibObj) {
751 752 753
                (*env)->SetObjectField(env, ibObj, ni_ibaddressID, iaObj);
                (*env)->SetShortField(env, ibObj, ni_ib4maskID, addrP->mask);
                (*env)->SetObjectArrayElement(env, bindArr, bind_index++, ibObj);
754 755
            } else {
                return NULL;
D
duke 已提交
756 757 758 759 760 761 762 763
            }
        }
#endif

        (*env)->SetObjectArrayElement(env, addrArr, addr_index++, iaObj);
        addrP = addrP->next;
    }

764
    // see if there is any virtual interface attached to this one.
D
duke 已提交
765 766 767
    child_count = 0;
    childP = ifs->childs;
    while (childP) {
768 769
        child_count++;
        childP = childP->next;
D
duke 已提交
770 771 772 773
    }

    childArr = (*env)->NewObjectArray(env, child_count, ni_class, NULL);
    if (childArr == NULL) {
774
        return NULL;
D
duke 已提交
775 776
    }

777
    // create the NetworkInterface instances for the sub-interfaces as well
D
duke 已提交
778 779 780
    child_index = 0;
    childP = ifs->childs;
    while(childP) {
781 782 783 784 785 786 787
        tmp = createNetworkInterface(env, childP);
        if (tmp == NULL) {
            return NULL;
        }
        (*env)->SetObjectField(env, tmp, ni_parentID, netifObj);
        (*env)->SetObjectArrayElement(env, childArr, child_index++, tmp);
        childP = childP->next;
D
duke 已提交
788 789 790 791 792
    }
    (*env)->SetObjectField(env, netifObj, ni_addrsID, addrArr);
    (*env)->SetObjectField(env, netifObj, ni_bindsID, bindArr);
    (*env)->SetObjectField(env, netifObj, ni_childsID, childArr);

793
    // return the NetworkInterface
D
duke 已提交
794 795 796 797 798 799 800
    return netifObj;
}

/*
 * Enumerates all interfaces
 */
static netif *enumInterfaces(JNIEnv *env) {
801
    netif *ifs = NULL;
802
    int sock;
D
duke 已提交
803

804 805 806 807 808
    sock = openSocket(env, AF_INET);
    if (sock < 0 && (*env)->ExceptionOccurred(env)) {
        return NULL;
    }

809
    // enumerate IPv4 addresses
810 811 812
    ifs = enumIPv4Interfaces(env, sock, NULL);
    close(sock);

813
    // return partial list if an exception occurs in the middle of process ???
814 815
    if (ifs == NULL && (*env)->ExceptionOccurred(env)) {
        return NULL;
D
duke 已提交
816 817
    }

818
    // If IPv6 is available then enumerate IPv6 addresses.
819
#if defined(AF_INET6)
820 821
        // User can disable ipv6 explicitly by -Djava.net.preferIPv4Stack=true,
        // so we have to call ipv6_available()
822
        if (ipv6_available()) {
823 824 825 826 827
            sock = openSocket(env, AF_INET6);
            if (sock < 0 && (*env)->ExceptionOccurred(env)) {
                freeif(ifs);
                return NULL;
            }
D
duke 已提交
828

829 830
            ifs = enumIPv6Interfaces(env, sock, ifs);
            close(sock);
831

832 833 834 835 836
            if ((*env)->ExceptionOccurred(env)) {
                freeif(ifs);
                return NULL;
            }
        }
D
duke 已提交
837 838 839 840 841 842
#endif

    return ifs;
}

/*
843
 * Frees an interface list (including any attached addresses).
D
duke 已提交
844
 */
845
static void freeif(netif *ifs) {
846 847
    netif *currif = ifs;
    netif *child = NULL;
D
duke 已提交
848

849 850 851 852 853 854
    while (currif != NULL) {
        netaddr *addrP = currif->addr;
        while (addrP != NULL) {
            netaddr *next = addrP->next;
            free(addrP);
            addrP = next;
855
        }
D
duke 已提交
856

857
        // don't forget to free the sub-interfaces
858 859 860
        if (currif->childs != NULL) {
            freeif(currif->childs);
        }
D
duke 已提交
861

862 863 864
        ifs = currif->next;
        free(currif);
        currif = ifs;
D
duke 已提交
865
    }
866
}
D
duke 已提交
867

868 869 870 871
static netif *addif(JNIEnv *env, int sock, const char *if_name, netif *ifs,
                    struct sockaddr *ifr_addrP,
                    struct sockaddr *ifr_broadaddrP,
                    int family, short prefix)
872
{
873 874
    netif *currif = ifs, *parent;
    netaddr *addrP;
875
    char name[IFNAMESIZE], vname[IFNAMESIZE];
876
    char *name_colonP;
877 878
    int isVirtual = 0;
    int addr_size;
D
duke 已提交
879

880 881 882 883
    // If the interface name is a logical interface then we remove the unit
    // number so that we have the physical interface (eg: hme0:1 -> hme0).
    // NetworkInterface currently doesn't have any concept of physical vs.
    // logical interfaces.
884 885
    strncpy(name, if_name, IFNAMESIZE);
    name[IFNAMESIZE - 1] = '\0';
886
    *vname = 0;
D
duke 已提交
887

888 889
    // Create and populate the netaddr node. If allocation fails
    // return an un-updated list.
890

891
    // Allocate for addr and brdcast at once
D
duke 已提交
892

893
#if defined(AF_INET6)
894 895
    addr_size = (family == AF_INET) ? sizeof(struct sockaddr_in)
                                    : sizeof(struct sockaddr_in6);
896 897 898
#else
    addr_size = sizeof(struct sockaddr_in);
#endif
D
duke 已提交
899

900 901
    CHECKED_MALLOC3(addrP, netaddr *, sizeof(netaddr) + 2 * addr_size);
    addrP->addr = (struct sockaddr *)((char *)addrP + sizeof(netaddr));
902
    memcpy(addrP->addr, ifr_addrP, addr_size);
D
duke 已提交
903

904 905 906
    addrP->family = family;
    addrP->mask = prefix;
    addrP->next = 0;
907

908 909 910 911 912 913 914 915
    // for IPv4 add broadcast address
    if (family == AF_INET && ifr_broadaddrP != NULL) {
        addrP->brdcast = (struct sockaddr *)
                             ((char *)addrP + sizeof(netaddr) + addr_size);
        memcpy(addrP->brdcast, ifr_broadaddrP, addr_size);
    } else {
        addrP->brdcast = NULL;
    }
916

917
    // Deal with virtual interface with colon notation e.g. eth0:1
918 919
    name_colonP = strchr(name, ':');
    if (name_colonP != NULL) {
920
        int flags = 0;
921 922 923
        // This is a virtual interface. If we are able to access the parent
        // we need to create a new entry if it doesn't exist yet *and* update
        // the 'parent' interface with the new records.
924
        *name_colonP = 0;
925
        if (getFlags(sock, name, &flags) < 0 || flags < 0) {
926 927 928 929
            // failed to access parent interface do not create parent.
            // We are a virtual interface with no parent.
            isVirtual = 1;
            *name_colonP = ':';
930
        } else {
931 932 933 934
            // Got access to parent, so create it if necessary.
            // Save original name to vname and truncate name by ':'
            memcpy(vname, name, sizeof(vname));
            vname[name_colonP - name] = ':';
935 936
        }
    }
D
duke 已提交
937

938 939
    // Check if this is a "new" interface. Use the interface name for
    // matching because index isn't supported on Solaris 2.6 & 7.
940 941 942 943 944
    while (currif != NULL) {
        if (strcmp(name, currif->name) == 0) {
            break;
        }
        currif = currif->next;
D
duke 已提交
945
    }
946

947
    // If "new" then create a netif structure and insert it into the list.
948
    if (currif == NULL) {
949
         CHECKED_MALLOC3(currif, netif *, sizeof(netif) + IFNAMESIZE);
950
         currif->name = (char *)currif + sizeof(netif);
951 952
         strncpy(currif->name, name, IFNAMESIZE);
         currif->name[IFNAMESIZE - 1] = '\0';
953
         currif->index = getIndex(sock, name);
954 955 956 957 958
         currif->addr = NULL;
         currif->childs = NULL;
         currif->virtual = isVirtual;
         currif->next = ifs;
         ifs = currif;
D
duke 已提交
959 960
    }

961
    // Finally insert the address on the interface
962 963
    addrP->next = currif->addr;
    currif->addr = addrP;
D
duke 已提交
964

965 966
    parent = currif;

967
    // Deal with the virtual interface now.
968 969 970 971 972 973 974 975 976 977
    if (vname[0]) {
        netaddr *tmpaddr;

        currif = parent->childs;

        while (currif != NULL) {
            if (strcmp(vname, currif->name) == 0) {
                break;
            }
            currif = currif->next;
D
duke 已提交
978 979
        }

980
        if (currif == NULL) {
981
            CHECKED_MALLOC3(currif, netif *, sizeof(netif) + IFNAMESIZE);
982
            currif->name = (char *)currif + sizeof(netif);
983 984
            strncpy(currif->name, vname, IFNAMESIZE);
            currif->name[IFNAMESIZE - 1] = '\0';
985
            currif->index = getIndex(sock, vname);
986
            currif->addr = NULL; // Need to duplicate the addr entry?
987 988 989 990 991 992
            currif->virtual = 1;
            currif->childs = NULL;
            currif->next = parent->childs;
            parent->childs = currif;
        }

993
        CHECKED_MALLOC3(tmpaddr, netaddr *, sizeof(netaddr) + 2 * addr_size);
994 995
        memcpy(tmpaddr, addrP, sizeof(netaddr));
        if (addrP->addr != NULL) {
996 997
            tmpaddr->addr = (struct sockaddr *)
                ((char*)tmpaddr + sizeof(netaddr));
998 999 1000 1001
            memcpy(tmpaddr->addr, addrP->addr, addr_size);
        }

        if (addrP->brdcast != NULL) {
1002 1003
            tmpaddr->brdcast = (struct sockaddr *)
                ((char *)tmpaddr + sizeof(netaddr) + addr_size);
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
            memcpy(tmpaddr->brdcast, addrP->brdcast, addr_size);
        }

        tmpaddr->next = currif->addr;
        currif->addr = tmpaddr;
    }

    return ifs;
}

1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
/*
 * Determines the prefix value for an AF_INET subnet address.
 */
static short translateIPv4AddressToPrefix(struct sockaddr_in *addr) {
    short prefix = 0;
    unsigned int mask = ntohl(addr->sin_addr.s_addr);
    while (mask) {
        mask <<= 1;
        prefix++;
    }
    return prefix;
}

/*
 * Determines the prefix value for an AF_INET6 subnet address.
 */
static short translateIPv6AddressToPrefix(struct sockaddr_in6 *addr) {
    short prefix = 0;
    u_char *addrBytes = (u_char *)&(addr->sin6_addr);
    unsigned int byte, bit;

    for (byte = 0; byte < sizeof(struct in6_addr); byte++, prefix += 8) {
        if (addrBytes[byte] != 0xff) {
            break;
        }
    }
    if (byte != sizeof(struct in6_addr)) {
        for (bit = 7; bit != 0; bit--, prefix++) {
            if (!(addrBytes[byte] & (1 << bit))) {
                break;
            }
        }
        for (; bit != 0; bit--) {
            if (addrBytes[byte] & (1 << bit)) {
                prefix = 0;
                break;
            }
        }
        if (prefix > 0) {
            byte++;
            for (; byte < sizeof(struct in6_addr); byte++) {
                if (addrBytes[byte]) {
                    prefix = 0;
                }
            }
        }
    }

    return prefix;
}

1065 1066
/*
 * Opens a socket for further ioct calls. proto is one of AF_INET or AF_INET6.
1067
 */
1068
static int openSocket(JNIEnv *env, int proto) {
1069 1070 1071
    int sock;

    if ((sock = JVM_Socket(proto, SOCK_DGRAM, 0)) < 0) {
1072 1073
        // If EPROTONOSUPPORT is returned it means we don't have
        // support for this proto so don't throw an exception.
1074
        if (errno != EPROTONOSUPPORT) {
1075 1076
            NET_ThrowByNameWithLastError
                (env, JNU_JAVANETPKG "SocketException", "Socket creation failed");
D
duke 已提交
1077
        }
1078 1079
        return -1;
    }
D
duke 已提交
1080

1081 1082 1083
    return sock;
}

1084 1085
/** Linux **/
#if defined(__linux__)
1086

1087
#if defined(AF_INET6)
1088
/*
1089 1090
 * Opens a socket for further ioctl calls. Tries AF_INET socket first and
 * if it fails return AF_INET6 socket.
1091 1092
 */
static int openSocketWithFallback(JNIEnv *env, const char *ifname) {
1093 1094
    int sock;

1095 1096 1097
    if ((sock = JVM_Socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
        if (errno == EPROTONOSUPPORT) {
            if ((sock = JVM_Socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1098 1099
                NET_ThrowByNameWithLastError
                    (env, JNU_JAVANETPKG "SocketException", "IPV6 Socket creation failed");
1100 1101 1102
                return -1;
            }
        } else { // errno is not NOSUPPORT
1103 1104
            NET_ThrowByNameWithLastError
                (env, JNU_JAVANETPKG "SocketException", "IPV4 Socket creation failed");
1105 1106 1107
            return -1;
        }
    }
1108

1109 1110 1111
    // Linux starting from 2.6.? kernel allows ioctl call with either IPv4 or
    // IPv6 socket regardless of type of address of an interface.
    return sock;
1112 1113
}
#else
1114
static int openSocketWithFallback(JNIEnv *env, const char *ifname) {
1115
    return openSocket(env, AF_INET);
1116 1117 1118
}
#endif

1119 1120 1121
/*
 * Enumerates and returns all IPv4 interfaces on Linux.
 */
1122 1123 1124
static netif *enumIPv4Interfaces(JNIEnv *env, int sock, netif *ifs) {
    struct ifconf ifc;
    struct ifreq *ifreqP;
1125
    char *buf = NULL;
1126 1127
    unsigned i;

1128
    // do a dummy SIOCGIFCONF to determine the buffer size
1129
    // SIOCGIFCOUNT doesn't work
1130 1131
    ifc.ifc_buf = NULL;
    if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
1132 1133
        NET_ThrowByNameWithLastError
            (env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGIFCONF) failed");
1134 1135
        return ifs;
    }
1136

1137
    // call SIOCGIFCONF to enumerate the interfaces
1138
    CHECKED_MALLOC3(buf, char *, ifc.ifc_len);
1139
    ifc.ifc_buf = buf;
1140 1141 1142
    if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
        NET_ThrowByNameWithLastError
            (env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGIFCONF) failed");
1143
        free(buf);
1144 1145
        return ifs;
    }
D
duke 已提交
1146

1147
    // iterate through each interface
1148
    ifreqP = ifc.ifc_req;
1149
    for (i = 0; i < ifc.ifc_len / sizeof(struct ifreq); i++, ifreqP++) {
1150 1151 1152
        struct sockaddr addr, broadaddr, *broadaddrP = NULL;
        short prefix = 0;

1153
        // ignore non IPv4 addresses
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
        if (ifreqP->ifr_addr.sa_family != AF_INET) {
            continue;
        }

        // save socket address
        memcpy(&addr, &(ifreqP->ifr_addr), sizeof(struct sockaddr));

        // determine broadcast address, if applicable
        if ((ioctl(sock, SIOCGIFFLAGS, ifreqP) == 0) &&
            ifreqP->ifr_flags & IFF_BROADCAST) {

            // restore socket address to ifreqP
            memcpy(&(ifreqP->ifr_addr), &addr, sizeof(struct sockaddr));

            if (ioctl(sock, SIOCGIFBRDADDR, ifreqP) == 0) {
                memcpy(&broadaddr, &(ifreqP->ifr_broadaddr),
                       sizeof(struct sockaddr));
                broadaddrP = &broadaddr;
            }
        }

        // restore socket address to ifreqP
        memcpy(&(ifreqP->ifr_addr), &addr, sizeof(struct sockaddr));

        // determine netmask
        if (ioctl(sock, SIOCGIFNETMASK, ifreqP) == 0) {
            prefix = translateIPv4AddressToPrefix(
                         (struct sockaddr_in *)&(ifreqP->ifr_netmask));
        }

        // add interface to the list
1185
        ifs = addif(env, sock, ifreqP->ifr_name, ifs,
1186
                    &addr, broadaddrP, AF_INET, prefix);
1187

1188
        // in case of exception, free interface list and buffer and return NULL
D
duke 已提交
1189 1190
        if ((*env)->ExceptionOccurred(env)) {
            free(buf);
1191 1192
            freeif(ifs);
            return NULL;
D
duke 已提交
1193 1194 1195
        }
    }

1196
    // free buffer
D
duke 已提交
1197 1198 1199 1200
    free(buf);
    return ifs;
}

1201
#if defined(AF_INET6)
1202

D
duke 已提交
1203
/*
1204
 * Enumerates and returns all IPv6 interfaces on Linux.
D
duke 已提交
1205
 */
1206
static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs) {
D
duke 已提交
1207
    FILE *f;
1208
    char devname[21], addr6p[8][5];
1209
    int prefix, scope, dad_status, if_idx;
D
duke 已提交
1210 1211

    if ((f = fopen(_PATH_PROCNET_IFINET6, "r")) != NULL) {
1212
        while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %08x %02x %02x %02x %20s\n",
1213 1214 1215
                      addr6p[0], addr6p[1], addr6p[2], addr6p[3],
                      addr6p[4], addr6p[5], addr6p[6], addr6p[7],
                      &if_idx, &prefix, &scope, &dad_status, devname) != EOF) {
1216

1217
            char addr6[40];
D
duke 已提交
1218 1219 1220
            struct sockaddr_in6 addr;

            sprintf(addr6, "%s:%s:%s:%s:%s:%s:%s:%s",
1221 1222
                    addr6p[0], addr6p[1], addr6p[2], addr6p[3],
                    addr6p[4], addr6p[5], addr6p[6], addr6p[7]);
D
duke 已提交
1223 1224

            memset(&addr, 0, sizeof(struct sockaddr_in6));
1225
            inet_pton(AF_INET6, addr6, (void*)addr.sin6_addr.s6_addr);
1226

1227
            // set scope ID to interface index
D
duke 已提交
1228 1229
            addr.sin6_scope_id = if_idx;

1230
            // add interface to the list
1231
            ifs = addif(env, sock, devname, ifs, (struct sockaddr *)&addr,
1232
                        NULL, AF_INET6, (short)prefix);
1233

1234
            // if an exception occurred then return the list as is
D
duke 已提交
1235
            if ((*env)->ExceptionOccurred(env)) {
1236
                break;
D
duke 已提交
1237
            }
1238 1239
       }
       fclose(f);
D
duke 已提交
1240 1241 1242 1243
    }
    return ifs;
}

1244
#endif /* AF_INET6 */
D
duke 已提交
1245

1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
/*
 * Try to get the interface index.
 */
static int getIndex(int sock, const char *name) {
    struct ifreq if2;
    memset((char *)&if2, 0, sizeof(if2));
    strncpy(if2.ifr_name, name, sizeof(if2.ifr_name) - 1);

    if (ioctl(sock, SIOCGIFINDEX, (char *)&if2) < 0) {
        return -1;
    }

    return if2.ifr_ifindex;
}
1260

1261
/*
1262 1263 1264
 * Gets the Hardware address (usually MAC address) for the named interface.
 * On return puts the data in buf, and returns the length, in byte, of the
 * MAC address. Returns -1 if there is no hardware address on that interface.
1265
 */
1266
static int getMacAddress
1267
  (JNIEnv *env, const char *ifname, const struct in_addr *addr,
1268 1269 1270
   unsigned char *buf)
{
    static struct ifreq ifr;
1271 1272 1273 1274 1275 1276
    int i, sock;

    if ((sock = openSocketWithFallback(env, ifname)) < 0) {
        return -1;
    }

1277 1278 1279 1280 1281
    memset((char *)&ifr, 0, sizeof(ifr));
    strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
    if (ioctl(sock, SIOCGIFHWADDR, &ifr) < 0) {
        NET_ThrowByNameWithLastError
            (env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGIFHWADDR) failed");
1282
        close(sock);
1283 1284 1285
        return -1;
    }

1286
    close(sock);
1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
    memcpy(buf, &ifr.ifr_hwaddr.sa_data, IFHWADDRLEN);

    // all bytes to 0 means no hardware address
    for (i = 0; i < IFHWADDRLEN; i++) {
        if (buf[i] != 0)
            return IFHWADDRLEN;
    }

    return -1;
}

static int getMTU(JNIEnv *env, int sock, const char *ifname) {
    struct ifreq if2;
    memset((char *)&if2, 0, sizeof(if2));
    strncpy(if2.ifr_name, ifname, sizeof(if2.ifr_name) - 1);

    if (ioctl(sock, SIOCGIFMTU, (char *)&if2) < 0) {
        NET_ThrowByNameWithLastError
            (env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGIFMTU) failed");
        return -1;
    }

    return if2.ifr_mtu;
}

static int getFlags(int sock, const char *ifname, int *flags) {
    struct ifreq if2;
    memset((char *)&if2, 0, sizeof(if2));
    strncpy(if2.ifr_name, ifname, sizeof(if2.ifr_name) - 1);

    if (ioctl(sock, SIOCGIFFLAGS, (char *)&if2) < 0) {
        return -1;
    }

    if (sizeof(if2.ifr_flags) == sizeof(short)) {
        *flags = (if2.ifr_flags & 0xffff);
    } else {
        *flags = if2.ifr_flags;
    }
    return 0;
}

#endif /* __linux__ */

/** AIX **/
#if defined(_AIX)

#if defined(AF_INET6)
/*
 * Opens a socket for further ioctl calls. Tries AF_INET socket first and
 * if it fails return AF_INET6 socket.
 */
static int openSocketWithFallback(JNIEnv *env, const char *ifname) {
    int sock;

1342
    if ((sock = JVM_Socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1343
        if (errno == EPROTONOSUPPORT) {
1344
            if ((sock = JVM_Socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367
                NET_ThrowByNameWithLastError
                    (env, JNU_JAVANETPKG "SocketException", "IPV6 Socket creation failed");
                return -1;
            }
        } else { // errno is not NOSUPPORT
            NET_ThrowByNameWithLastError
                (env, JNU_JAVANETPKG "SocketException", "IPV4 Socket creation failed");
            return -1;
        }
    }

    return sock;
}
#else
static int openSocketWithFallback(JNIEnv *env, const char *ifname) {
    return openSocket(env, AF_INET);
}
#endif

/*
 * Enumerates and returns all IPv4 interfaces on AIX.
 */
static netif *enumIPv4Interfaces(JNIEnv *env, int sock, netif *ifs) {
1368 1369
    struct ifconf ifc;
    struct ifreq *ifreqP;
1370
    char *buf = NULL;
1371 1372
    unsigned i;

1373
    // call SIOCGSIZIFCONF to get the size of SIOCGIFCONF buffer
1374
    if (ioctl(sock, SIOCGSIZIFCONF, &(ifc.ifc_len)) < 0) {
1375 1376
        NET_ThrowByNameWithLastError
            (env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGSIZIFCONF) failed");
1377 1378 1379
        return ifs;
    }

1380 1381 1382 1383
    // call CSIOCGIFCONF instead of SIOCGIFCONF where interface
    // records will always have sizeof(struct ifreq) length.
    // Be aware that only IPv4 data is complete this way.
    CHECKED_MALLOC3(buf, char *, ifc.ifc_len);
1384
    ifc.ifc_buf = buf;
1385 1386 1387
    if (ioctl(sock, CSIOCGIFCONF, (char *)&ifc) < 0) {
        NET_ThrowByNameWithLastError
            (env, JNU_JAVANETPKG "SocketException", "ioctl(CSIOCGIFCONF) failed");
1388 1389 1390 1391
        free(buf);
        return ifs;
    }

1392
    // iterate through each interface
1393
    ifreqP = ifc.ifc_req;
1394 1395 1396
    for (i = 0; i < ifc.ifc_len / sizeof(struct ifreq); i++, ifreqP++) {
        struct sockaddr addr, broadaddr, *broadaddrP = NULL;
        short prefix = 0;
1397

1398
        // ignore non IPv4 addresses
1399 1400 1401 1402 1403 1404
        if (ifreqP->ifr_addr.sa_family != AF_INET) {
            continue;
        }

        // save socket address
        memcpy(&addr, &(ifreqP->ifr_addr), sizeof(struct sockaddr));
1405

1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416
        // determine broadcast address, if applicable
        if ((ioctl(sock, SIOCGIFFLAGS, ifreqP) == 0) &&
            ifreqP->ifr_flags & IFF_BROADCAST) {

            // restore socket address to ifreqP
            memcpy(&(ifreqP->ifr_addr), &addr, sizeof(struct sockaddr));

            if (ioctl(sock, SIOCGIFBRDADDR, ifreqP) == 0) {
                memcpy(&broadaddr, &(ifreqP->ifr_broadaddr),
                       sizeof(struct sockaddr));
                broadaddrP = &broadaddr;
1417 1418 1419
            }
        }

1420 1421
        // restore socket address to ifreqP
        memcpy(&(ifreqP->ifr_addr), &addr, sizeof(struct sockaddr));
1422

1423 1424 1425 1426
        // determine netmask
        if (ioctl(sock, SIOCGIFNETMASK, ifreqP) == 0) {
            prefix = translateIPv4AddressToPrefix(
                         (struct sockaddr_in *)&(ifreqP->ifr_addr));
1427 1428
        }

1429
        // add interface to the list
1430
        ifs = addif(env, sock, ifreqP->ifr_name, ifs,
1431
                    &addr, broadaddrP, AF_INET, prefix);
1432

1433
        // in case of exception, free interface list and buffer and return NULL
1434 1435 1436 1437 1438 1439 1440
        if ((*env)->ExceptionOccurred(env)) {
            free(buf);
            freeif(ifs);
            return NULL;
        }
    }

1441
    // free buffer
1442 1443 1444 1445
    free(buf);
    return ifs;
}

1446
#if defined(AF_INET6)
1447

1448 1449 1450 1451 1452 1453
/*
 * Enumerates and returns all IPv6 interfaces on AIX.
 */
static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs) {
    struct ifconf ifc;
    struct ifreq *ifreqP;
1454
    char *buf, *cp, *cplimit;
1455

1456 1457 1458 1459 1460
    // call SIOCGSIZIFCONF to get size for SIOCGIFCONF buffer
    if (ioctl(sock, SIOCGSIZIFCONF, &(ifc.ifc_len)) < 0) {
        NET_ThrowByNameWithLastError
            (env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGSIZIFCONF) failed");
        return ifs;
1461 1462
    }

1463 1464 1465 1466 1467 1468 1469 1470 1471
    // call SIOCGIFCONF to enumerate the interfaces
    CHECKED_MALLOC3(buf, char *, ifc.ifc_len);
    ifc.ifc_buf = buf;
    if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
        NET_ThrowByNameWithLastError
            (env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGIFCONF) failed");
        free(buf);
        return ifs;
    }
1472

1473
    // iterate through each interface
1474 1475 1476
    ifreqP = ifc.ifc_req;
    cp = (char *)ifc.ifc_req;
    cplimit = cp + ifc.ifc_len;
D
duke 已提交
1477

1478 1479 1480 1481 1482 1483
    for (; cp < cplimit;
         cp += (sizeof(ifreqP->ifr_name) +
                MAX((ifreqP->ifr_addr).sa_len, sizeof(ifreqP->ifr_addr))))
    {
        ifreqP = (struct ifreq *)cp;
        short prefix = 0;
D
duke 已提交
1484

1485
        // ignore non IPv6 addresses
1486 1487
        if (ifreqP->ifr_addr.sa_family != AF_INET6) {
            continue;
1488
        }
D
duke 已提交
1489

1490 1491 1492 1493 1494 1495 1496 1497 1498
        // determine netmask
        struct in6_ifreq if6;
        memset((char *)&if6, 0, sizeof(if6));
        strncpy(if6.ifr_name, ifreqP->ifr_name, sizeof(if6.ifr_name) - 1);
        memcpy(&(if6.ifr_Addr), &(ifreqP->ifr_addr),
               sizeof(struct sockaddr_in6));
        if (ioctl(sock, SIOCGIFNETMASK6, (char *)&if6) >= 0) {
            prefix = translateIPv6AddressToPrefix(&(if6.ifr_Addr));
        }
1499

1500 1501 1502
        // set scope ID to interface index
        ((struct sockaddr_in6 *)&(ifreqP->ifr_addr))->sin6_scope_id =
            getIndex(sock, ifreqP->ifr_name);
D
duke 已提交
1503

1504 1505 1506 1507
        // add interface to the list
        ifs = addif(env, sock, ifreqP->ifr_name, ifs,
                    (struct sockaddr *)&(ifreqP->ifr_addr),
                    NULL, AF_INET6, prefix);
D
duke 已提交
1508

1509 1510 1511 1512 1513 1514
        // if an exception occurred then free the list
        if ((*env)->ExceptionOccurred(env)) {
            free(buf);
            freeif(ifs);
            return NULL;
        }
D
duke 已提交
1515
    }
1516

1517 1518 1519 1520
    // free buffer
    free(buf);
    return ifs;
}
D
duke 已提交
1521

1522 1523 1524 1525 1526 1527 1528 1529
#endif /* AF_INET6 */

/*
 * Try to get the interface index.
 */
static int getIndex(int sock, const char *name) {
    int index = if_nametoindex(name);
    return (index == 0) ? -1 : index;
1530 1531
}

1532 1533 1534
/*
 * Gets the Hardware address (usually MAC address) for the named interface.
 * On return puts the data in buf, and returns the length, in byte, of the
1535 1536
 * MAC address. Returns -1 if there is no hardware address on that interface.
 */
1537
static int getMacAddress
1538
  (JNIEnv *env, const char *ifname, const struct in_addr *addr,
1539 1540
   unsigned char *buf)
{
1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557
    int size;
    struct kinfo_ndd *nddp;
    void *end;

    size = getkerninfo(KINFO_NDD, 0, 0, 0);
    if (size == 0) {
        return -1;
    }

    if (size < 0) {
        perror("getkerninfo 1");
        return -1;
    }

    nddp = (struct kinfo_ndd *)malloc(size);

    if (!nddp) {
1558 1559
        JNU_ThrowOutOfMemoryError(env,
            "Network interface getMacAddress native buffer allocation failed");
1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579
        return -1;
    }

    if (getkerninfo(KINFO_NDD, nddp, &size, 0) < 0) {
        perror("getkerninfo 2");
        return -1;
    }

    end = (void *)nddp + size;
    while ((void *)nddp < end) {
        if (!strcmp(nddp->ndd_alias, ifname) ||
                !strcmp(nddp->ndd_name, ifname)) {
            bcopy(nddp->ndd_addr, buf, 6);
            return 6;
        } else {
            nddp++;
        }
    }

    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 1589
        NET_ThrowByNameWithLastError
            (env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGIFMTU) failed");
1590 1591 1592
        return -1;
    }

1593
    return if2.ifr_mtu;
1594
}
D
duke 已提交
1595

1596
static int getFlags(int sock, const char *ifname, int *flags) {
1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610
    struct ifreq if2;
    memset((char *)&if2, 0, sizeof(if2));
    strncpy(if2.ifr_name, ifname, sizeof(if2.ifr_name) - 1);

    if (ioctl(sock, SIOCGIFFLAGS, (char *)&if2) < 0) {
        return -1;
    }

    if (sizeof(if2.ifr_flags) == sizeof(short)) {
        *flags = (if2.ifr_flags & 0xffff);
    } else {
        *flags = if2.ifr_flags;
    }
    return 0;
1611 1612
}

1613
#endif /* _AIX */
1614 1615

/** Solaris **/
1616
#if defined(__solaris__)
1617

1618
#if defined(AF_INET6)
1619
/*
1620 1621
 * Opens a socket for further ioctl calls. Tries AF_INET socket first and
 * if it fails return AF_INET6 socket.
1622 1623
 */
static int openSocketWithFallback(JNIEnv *env, const char *ifname) {
1624 1625 1626
    int sock, alreadyV6 = 0;
    struct lifreq if2;

1627 1628 1629
    if ((sock = JVM_Socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
        if (errno == EPROTONOSUPPORT) {
            if ((sock = JVM_Socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1630 1631
                NET_ThrowByNameWithLastError
                    (env, JNU_JAVANETPKG "SocketException", "IPV6 Socket creation failed");
1632 1633
                return -1;
            }
1634
            alreadyV6 = 1;
1635
        } else { // errno is not NOSUPPORT
1636 1637
            NET_ThrowByNameWithLastError
                (env, JNU_JAVANETPKG "SocketException", "IPV4 Socket creation failed");
1638 1639 1640
            return -1;
        }
    }
1641

1642 1643 1644 1645 1646 1647 1648 1649 1650
    // Solaris requires that we have an IPv6 socket to query an  interface
    // without an IPv4 address - check it here. POSIX 1 require the kernel to
    // return ENOTTY if the call is inappropriate for a device e.g. the NETMASK
    // for a device having IPv6 only address but not all devices follow the
    // standard so fall back on any error. It's not an ecologically friendly
    // gesture but more reliable.
    if (!alreadyV6) {
        memset((char *)&if2, 0, sizeof(if2));
        strncpy(if2.lifr_name, ifname, sizeof(if2.lifr_name) - 1);
1651
        if (ioctl(sock, SIOCGLIFNETMASK, (char *)&if2) < 0) {
1652 1653
            close(sock);
            if ((sock = JVM_Socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1654 1655
                NET_ThrowByNameWithLastError
                    (env, JNU_JAVANETPKG "SocketException", "IPV6 Socket creation failed");
1656 1657
                return -1;
            }
1658 1659
        }
    }
D
duke 已提交
1660

1661 1662
    return sock;
}
1663
#else
1664
static int openSocketWithFallback(JNIEnv *env, const char *ifname) {
1665
    return openSocket(env, AF_INET);
1666 1667 1668 1669
}
#endif

/*
1670
 * Enumerates and returns all IPv4 interfaces on Solaris.
1671 1672 1673
 */
static netif *enumIPv4Interfaces(JNIEnv *env, int sock, netif *ifs) {
    struct lifconf ifc;
1674
    struct lifreq *ifreqP;
1675
    struct lifnum numifs;
1676 1677
    char *buf = NULL;
    unsigned i;
D
duke 已提交
1678

1679
    // call SIOCGLIFNUM to get the interface count
1680
    numifs.lifn_family = AF_INET;
1681 1682
    numifs.lifn_flags = 0;
    if (ioctl(sock, SIOCGLIFNUM, (char *)&numifs) < 0) {
1683 1684
        NET_ThrowByNameWithLastError
            (env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGLIFNUM) failed");
1685
        return ifs;
D
duke 已提交
1686 1687
    }

1688 1689 1690
    // call SIOCGLIFCONF to enumerate the interfaces
    ifc.lifc_len = numifs.lifn_count * sizeof(struct lifreq);
    CHECKED_MALLOC3(buf, char *, ifc.lifc_len);
1691
    ifc.lifc_buf = buf;
1692 1693
    ifc.lifc_family = AF_INET;
    ifc.lifc_flags = 0;
1694
    if (ioctl(sock, SIOCGLIFCONF, (char *)&ifc) < 0) {
1695 1696
        NET_ThrowByNameWithLastError
            (env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGLIFCONF) failed");
1697 1698
        free(buf);
        return ifs;
D
duke 已提交
1699 1700
    }

1701 1702 1703 1704
    // iterate through each interface
    ifreqP = ifc.lifc_req;
    for (i = 0; i < numifs.lifn_count; i++, ifreqP++) {
        struct sockaddr addr, *broadaddrP = NULL;
D
duke 已提交
1705

1706
        // ignore non IPv4 addresses
1707
        if (ifreqP->lifr_addr.ss_family != AF_INET) {
1708 1709
            continue;
        }
D
duke 已提交
1710

1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724
        // save socket address
        memcpy(&addr, &(ifreqP->lifr_addr), sizeof(struct sockaddr));

        // determine broadcast address, if applicable
        if ((ioctl(sock, SIOCGLIFFLAGS, ifreqP) == 0) &&
            ifreqP->lifr_flags & IFF_BROADCAST) {

            // restore socket address to ifreqP
            memcpy(&(ifreqP->lifr_addr), &addr, sizeof(struct sockaddr));

            // query broadcast address and set pointer to it
            if (ioctl(sock, SIOCGLIFBRDADDR, ifreqP) == 0) {
                broadaddrP = (struct sockaddr *)&(ifreqP->lifr_broadaddr);
            }
1725 1726
        }

1727
        // add to the list
1728 1729
        ifs = addif(env, sock, ifreqP->lifr_name, ifs,
                    &addr, broadaddrP, AF_INET, (short)ifreqP->lifr_addrlen);
D
duke 已提交
1730

1731
        // if an exception occurred we return immediately
1732 1733 1734
        if ((*env)->ExceptionOccurred(env)) {
            free(buf);
            return ifs;
D
duke 已提交
1735
        }
1736
   }
D
duke 已提交
1737

1738
    // free buffer
1739
    free(buf);
D
duke 已提交
1740 1741 1742
    return ifs;
}

1743
#if defined(AF_INET6)
D
duke 已提交
1744

1745
/*
1746
 * Enumerates and returns all IPv6 interfaces on Solaris.
D
duke 已提交
1747
 */
1748 1749 1750 1751 1752 1753
static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs) {
    struct lifconf ifc;
    struct lifreq *ifreqP;
    struct lifnum numifs;
    char *buf = NULL;
    unsigned i;
D
duke 已提交
1754

1755
    // call SIOCGLIFNUM to get the interface count
1756 1757 1758 1759 1760 1761
    numifs.lifn_family = AF_INET6;
    numifs.lifn_flags = 0;
    if (ioctl(sock, SIOCGLIFNUM, (char *)&numifs) < 0) {
        NET_ThrowByNameWithLastError
            (env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGLIFNUM) failed");
        return ifs;
D
duke 已提交
1762
    }
1763

1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780
    // call SIOCGLIFCONF to enumerate the interfaces
    ifc.lifc_len = numifs.lifn_count * sizeof(struct lifreq);
    CHECKED_MALLOC3(buf, char *, ifc.lifc_len);
    ifc.lifc_buf = buf;
    ifc.lifc_family = AF_INET6;
    ifc.lifc_flags = 0;
    if (ioctl(sock, SIOCGLIFCONF, (char *)&ifc) < 0) {
        NET_ThrowByNameWithLastError
            (env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGLIFCONF) failed");
        free(buf);
        return ifs;
    }

    // iterate through each interface
    ifreqP = ifc.lifc_req;
    for (i = 0; i < numifs.lifn_count; i++, ifreqP++) {

1781
        // ignore non IPv6 addresses
1782 1783
        if (ifreqP->lifr_addr.ss_family != AF_INET6) {
            continue;
1784 1785
        }

1786 1787 1788
        // set scope ID to interface index
        ((struct sockaddr_in6 *)&(ifreqP->lifr_addr))->sin6_scope_id =
            getIndex(sock, ifreqP->lifr_name);
1789

1790 1791 1792 1793 1794 1795 1796 1797 1798 1799
        // add to the list
        ifs = addif(env, sock, ifreqP->lifr_name, ifs,
                    (struct sockaddr *)&(ifreqP->lifr_addr),
                    NULL, AF_INET6, (short)ifreqP->lifr_addrlen);

        // if an exception occurred we return immediately
        if ((*env)->ExceptionOccurred(env)) {
            free(buf);
            return ifs;
        }
1800
    }
1801 1802 1803 1804

    // free buffer
    free(buf);
    return ifs;
D
duke 已提交
1805 1806
}

1807 1808
#endif /* AF_INET6 */

1809
/*
1810 1811
 * Try to get the interface index.
 * (Not supported on Solaris 2.6 or 7)
D
duke 已提交
1812
 */
1813
static int getIndex(int sock, const char *name) {
1814
    struct lifreq if2;
1815
    memset((char *)&if2, 0, sizeof(if2));
1816
    strncpy(if2.lifr_name, name, sizeof(if2.lifr_name) - 1);
D
duke 已提交
1817

1818
    if (ioctl(sock, SIOCGLIFINDEX, (char *)&if2) < 0) {
1819 1820 1821
        return -1;
    }

1822
    return if2.lifr_index;
D
duke 已提交
1823 1824
}

1825
/*
D
duke 已提交
1826 1827 1828 1829
 * Solaris specific DLPI code to get hardware address from a device.
 * Unfortunately, at least up to Solaris X, you have to have special
 * privileges (i.e. be root).
 */
1830
static int getMacFromDevice
1831
  (JNIEnv *env, const char *ifname, unsigned char *retbuf)
1832
{
1833 1834 1835 1836 1837 1838 1839 1840
    char style1dev[MAXPATHLEN];
    int fd;
    dl_phys_addr_req_t dlpareq;
    dl_phys_addr_ack_t *dlpaack;
    struct strbuf msg;
    char buf[128];
    int flags = 0;

1841
    // Device is in /dev.  e.g.: /dev/bge0
1842 1843 1844
    strcpy(style1dev, DEV_PREFIX);
    strcat(style1dev, ifname);
    if ((fd = open(style1dev, O_RDWR)) < 0) {
1845 1846 1847
        // Can't open it. We probably are missing the privilege.
        // We'll have to try something else
        return 0;
1848 1849 1850 1851 1852 1853 1854 1855 1856
    }

    dlpareq.dl_primitive = DL_PHYS_ADDR_REQ;
    dlpareq.dl_addr_type = DL_CURR_PHYS_ADDR;

    msg.buf = (char *)&dlpareq;
    msg.len = DL_PHYS_ADDR_REQ_SIZE;

    if (putmsg(fd, &msg, NULL, 0) < 0) {
1857 1858
        NET_ThrowByNameWithLastError
            (env, JNU_JAVANETPKG "SocketException", "putmsg() failed");
1859 1860 1861 1862 1863 1864 1865 1866 1867
        return -1;
    }

    dlpaack = (dl_phys_addr_ack_t *)buf;

    msg.buf = (char *)buf;
    msg.len = 0;
    msg.maxlen = sizeof (buf);
    if (getmsg(fd, &msg, NULL, &flags) < 0) {
1868 1869
        NET_ThrowByNameWithLastError
            (env, JNU_JAVANETPKG "SocketException", "getmsg() failed");
1870 1871 1872 1873
        return -1;
    }

    if (msg.len < DL_PHYS_ADDR_ACK_SIZE || dlpaack->dl_primitive != DL_PHYS_ADDR_ACK) {
1874 1875
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
                        "Couldn't obtain phys addr\n");
1876 1877
        return -1;
    }
D
duke 已提交
1878

1879 1880
    memcpy(retbuf, &buf[dlpaack->dl_addr_offset], dlpaack->dl_addr_length);
    return dlpaack->dl_addr_length;
D
duke 已提交
1881 1882
}

1883 1884 1885
/*
 * Gets the Hardware address (usually MAC address) for the named interface.
 * On return puts the data in buf, and returns the length, in byte, of the
D
duke 已提交
1886 1887
 * MAC address. Returns -1 if there is no hardware address on that interface.
 */
1888
static int getMacAddress
1889
  (JNIEnv *env, const char *ifname, const struct in_addr *addr,
1890 1891
   unsigned char *buf)
{
1892
    struct lifreq if2;
1893 1894 1895 1896 1897
    int len, i, sock;

    if ((sock = openSocketWithFallback(env, ifname)) < 0) {
        return -1;
    }
1898

1899 1900
    // First, try the new (S11) SIOCGLIFHWADDR ioctl(). If that fails
    // try the old way.
1901 1902
    memset((char *)&if2, 0, sizeof(if2));
    strncpy(if2.lifr_name, ifname, sizeof(if2.lifr_name) - 1);
1903

1904
    if (ioctl(sock, SIOCGLIFHWADDR, &if2) != -1) {
1905
        struct sockaddr_dl *sp;
1906
        sp = (struct sockaddr_dl *)&if2.lifr_addr;
1907
        memcpy(buf, &sp->sdl_data[0], sp->sdl_alen);
1908
        close(sock);
1909 1910
        return sp->sdl_alen;
    }
1911

1912 1913 1914
    // On Solaris we have to use DLPI, but it will only work if we have
    // privileged access (i.e. root). If that fails, we try a lookup
    // in the ARP table, which requires an IPv4 address.
1915
    if (((len = getMacFromDevice(env, ifname, buf)) == 0) && (addr != NULL)) {
1916 1917 1918 1919
        struct arpreq arpreq;
        struct sockaddr_in *sin;
        struct sockaddr_in ipAddr;

1920
        len = 6; //???
D
duke 已提交
1921

1922 1923 1924 1925 1926 1927 1928
        sin = (struct sockaddr_in *)&arpreq.arp_pa;
        memset((char *)&arpreq, 0, sizeof(struct arpreq));
        ipAddr.sin_port = 0;
        ipAddr.sin_family = AF_INET;
        memcpy(&ipAddr.sin_addr, addr, sizeof(struct in_addr));
        memcpy(&arpreq.arp_pa, &ipAddr, sizeof(struct sockaddr_in));
        arpreq.arp_flags= ATF_PUBL;
D
duke 已提交
1929

1930 1931 1932 1933
        if (ioctl(sock, SIOCGARP, &arpreq) < 0) {
            close(sock);
            return -1;
        }
1934

1935
        memcpy(buf, &arpreq.arp_ha.sa_data[0], len);
1936
    }
1937
    close(sock);
D
duke 已提交
1938

1939
    // all bytes to 0 means no hardware address
1940
    for (i = 0; i < len; i++) {
1941 1942
        if (buf[i] != 0)
            return len;
1943
    }
D
duke 已提交
1944

1945
    return -1;
D
duke 已提交
1946 1947
}

1948
static int getMTU(JNIEnv *env, int sock, const char *ifname) {
1949
    struct lifreq if2;
1950 1951
    memset((char *)&if2, 0, sizeof(if2));
    strncpy(if2.lifr_name, ifname, sizeof(if2.lifr_name) - 1);
D
duke 已提交
1952

1953
    if (ioctl(sock, SIOCGLIFMTU, (char *)&if2) < 0) {
1954 1955
        NET_ThrowByNameWithLastError
            (env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGLIFMTU) failed");
1956 1957
        return -1;
    }
D
duke 已提交
1958

1959
    return if2.lifr_mtu;
D
duke 已提交
1960 1961
}

1962
static int getFlags(int sock, const char *ifname, int *flags) {
1963 1964 1965
    struct lifreq if2;
    memset((char *)&if2, 0, sizeof(if2));
    strncpy(if2.lifr_name, ifname, sizeof(if2.lifr_name) - 1);
D
duke 已提交
1966

1967 1968 1969
    if (ioctl(sock, SIOCGLIFFLAGS, (char *)&if2) < 0) {
        return -1;
    }
D
duke 已提交
1970

1971 1972
    *flags = if2.lifr_flags;
    return 0;
1973
}
D
duke 已提交
1974

1975
#endif /* __solaris__ */
1976

1977
/** BSD **/
1978
#if defined(_ALLBSD_SOURCE)
1979

1980
#if defined(AF_INET6)
1981
/*
1982 1983
 * Opens a socket for further ioctl calls. Tries AF_INET socket first and
 * if it fails return AF_INET6 socket.
1984 1985
 */
static int openSocketWithFallback(JNIEnv *env, const char *ifname) {
1986 1987
    int sock;

1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000
    if ((sock = JVM_Socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
        if (errno == EPROTONOSUPPORT) {
            if ((sock = JVM_Socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
                NET_ThrowByNameWithLastError
                    (env, JNU_JAVANETPKG "SocketException", "IPV6 Socket creation failed");
                return -1;
            }
        } else { // errno is not NOSUPPORT
            NET_ThrowByNameWithLastError
                (env, JNU_JAVANETPKG "SocketException", "IPV4 Socket creation failed");
            return -1;
        }
    }
2001

2002
    return sock;
2003 2004
}
#else
2005
static int openSocketWithFallback(JNIEnv *env, const char *ifname) {
2006
    return openSocket(env, AF_INET);
2007 2008 2009 2010
}
#endif

/*
2011
 * Enumerates and returns all IPv4 interfaces on BSD.
2012 2013 2014 2015 2016
 */
static netif *enumIPv4Interfaces(JNIEnv *env, int sock, netif *ifs) {
    struct ifaddrs *ifa, *origifa;

    if (getifaddrs(&origifa) != 0) {
2017 2018
        NET_ThrowByNameWithLastError
            (env, JNU_JAVANETPKG "SocketException", "getifaddrs() failed");
2019 2020 2021 2022
        return ifs;
    }

    for (ifa = origifa; ifa != NULL; ifa = ifa->ifa_next) {
2023
        struct sockaddr *broadaddrP = NULL;
2024

2025
        // ignore non IPv4 addresses
2026 2027 2028
        if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family != AF_INET)
            continue;

2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039
        // set ifa_broadaddr, if there is one
        if ((ifa->ifa_flags & IFF_POINTOPOINT) == 0 &&
            ifa->ifa_flags & IFF_BROADCAST) {
            broadaddrP = ifa->ifa_dstaddr;
        }

        // add interface to the list
        ifs = addif(env, sock, ifa->ifa_name, ifs, ifa->ifa_addr,
                    broadaddrP, AF_INET,
                    translateIPv4AddressToPrefix((struct sockaddr_in *)
                                                 ifa->ifa_netmask));
2040

2041
        // if an exception occurred then free the list
2042 2043 2044 2045 2046 2047 2048
        if ((*env)->ExceptionOccurred(env)) {
            freeifaddrs(origifa);
            freeif(ifs);
            return NULL;
        }
    }

2049
    // free ifaddrs buffer
2050 2051 2052 2053
    freeifaddrs(origifa);
    return ifs;
}

2054
#if defined(AF_INET6)
2055 2056

/*
2057
 * Enumerates and returns all IPv6 interfaces on BSD.
2058 2059 2060 2061 2062
 */
static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs) {
    struct ifaddrs *ifa, *origifa;

    if (getifaddrs(&origifa) != 0) {
2063 2064
        NET_ThrowByNameWithLastError
            (env, JNU_JAVANETPKG "SocketException", "getifaddrs() failed");
2065 2066 2067 2068
        return ifs;
    }

    for (ifa = origifa; ifa != NULL; ifa = ifa->ifa_next) {
2069
        // ignore non IPv6 addresses
2070 2071 2072
        if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family != AF_INET6)
            continue;

2073 2074 2075
        // set scope ID to interface index
        ((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_scope_id =
            getIndex(sock, ifa->ifa_name);
2076

2077 2078 2079 2080 2081
        // add interface to the list
        ifs = addif(env, sock, ifa->ifa_name, ifs, ifa->ifa_addr, NULL,
                    AF_INET6,
                    translateIPv6AddressToPrefix((struct sockaddr_in6 *)
                                                 ifa->ifa_netmask));
2082

2083
        // if an exception occurred then free the list
2084 2085 2086 2087 2088 2089 2090
        if ((*env)->ExceptionOccurred(env)) {
            freeifaddrs(origifa);
            freeif(ifs);
            return NULL;
        }
    }

2091
    // free ifaddrs buffer
2092 2093 2094 2095
    freeifaddrs(origifa);
    return ifs;
}

2096 2097 2098 2099 2100
#endif /* AF_INET6 */

/*
 * Try to get the interface index.
 */
2101
static int getIndex(int sock, const char *name) {
2102 2103 2104 2105
#if !defined(__FreeBSD__)
    int index = if_nametoindex(name);
    return (index == 0) ? -1 : index;
#else
2106
    struct ifreq if2;
2107 2108
    memset((char *)&if2, 0, sizeof(if2));
    strncpy(if2.ifr_name, name, sizeof(if2.ifr_name) - 1);
2109 2110 2111 2112 2113 2114 2115 2116 2117

    if (ioctl(sock, SIOCGIFINDEX, (char *)&if2) < 0) {
        return -1;
    }

    return if2.ifr_index;
#endif
}

2118 2119
/*
 * Gets the Hardware address (usually MAC address) for the named interface.
2120
 * On return puts the data in buf, and returns the length, in byte, of the
2121 2122
 * MAC address. Returns -1 if there is no hardware address on that interface.
 */
2123
static int getMacAddress
2124
  (JNIEnv *env, const char *ifname, const struct in_addr *addr,
2125 2126
   unsigned char *buf)
{
2127 2128 2129 2130
    struct ifaddrs *ifa0, *ifa;
    struct sockaddr *saddr;
    int i;

2131
    // grab the interface list
2132
    if (!getifaddrs(&ifa0)) {
2133
        // cycle through the interfaces
2134 2135
        for (i = 0, ifa = ifa0; ifa != NULL; ifa = ifa->ifa_next, i++) {
            saddr = ifa->ifa_addr;
2136
            // link layer contains the MAC address
2137 2138
            if (saddr->sa_family == AF_LINK && !strcmp(ifname, ifa->ifa_name)) {
                struct sockaddr_dl *sadl = (struct sockaddr_dl *) saddr;
2139
                // check the address has the correct length
2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152
                if (sadl->sdl_alen == ETHER_ADDR_LEN) {
                    memcpy(buf, (sadl->sdl_data + sadl->sdl_nlen), ETHER_ADDR_LEN);
                    freeifaddrs(ifa0);
                    return ETHER_ADDR_LEN;
                }
            }
        }
        freeifaddrs(ifa0);
    }

    return -1;
}

2153
static int getMTU(JNIEnv *env, int sock, const char *ifname) {
2154
    struct ifreq if2;
2155 2156
    memset((char *)&if2, 0, sizeof(if2));
    strncpy(if2.ifr_name, ifname, sizeof(if2.ifr_name) - 1);
2157 2158

    if (ioctl(sock, SIOCGIFMTU, (char *)&if2) < 0) {
2159 2160
        NET_ThrowByNameWithLastError
            (env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGIFMTU) failed");
2161 2162 2163
        return -1;
    }

2164
    return if2.ifr_mtu;
2165 2166
}

2167
static int getFlags(int sock, const char *ifname, int *flags) {
2168 2169 2170
    struct ifreq if2;
    memset((char *)&if2, 0, sizeof(if2));
    strncpy(if2.ifr_name, ifname, sizeof(if2.ifr_name) - 1);
2171

2172 2173 2174
    if (ioctl(sock, SIOCGIFFLAGS, (char *)&if2) < 0) {
        return -1;
    }
2175

2176 2177 2178 2179 2180 2181
    if (sizeof(if2.ifr_flags) == sizeof(short)) {
        *flags = (if2.ifr_flags & 0xffff);
    } else {
        *flags = if2.ifr_flags;
    }
    return 0;
2182
}
2183
#endif /* _ALLBSD_SOURCE */