jdk/src/solaris/native/java/net/NetworkInterface.c
author chegar
Thu, 12 Jun 2008 17:28:08 +0100
changeset 709 4d98e12330e6
parent 2 90ce3da70b43
child 715 f16baef3a20e
permissions -rw-r--r--
6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured Summary: failover to IPv6 socket if IPv4 fails Reviewed-by: michaelm
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include <errno.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <strings.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include <netinet/in.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include <sys/types.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include <sys/socket.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include <arpa/inet.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include <net/if.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include <net/if_arp.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include <sys/dlpi.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#include <fcntl.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
#include <stropts.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
#include <sys/ioctl.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
#include <bits/ioctls.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
#include <linux/sockios.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
#include <sys/utsname.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
#include <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
#include <sys/sockio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
#define ifr_index ifr_ifindex
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
#define _PATH_PROCNET_IFINET6           "/proc/net/if_inet6"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
#include "jvm.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
#include "net_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
typedef struct _netaddr  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    struct sockaddr *addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    struct sockaddr *brdcast;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    short mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    int family; /* to make searches simple */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    struct _netaddr *next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
} netaddr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
typedef struct _netif {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    char *name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    char virtual;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    netaddr *addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    struct _netif *childs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    struct _netif *next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
} netif;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
/************************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
#include "java_net_NetworkInterface.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
/************************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
jclass ni_class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
jfieldID ni_nameID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
jfieldID ni_indexID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
jfieldID ni_descID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
jfieldID ni_addrsID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
jfieldID ni_bindsID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
jfieldID ni_virutalID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
jfieldID ni_childsID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
jfieldID ni_parentID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
jmethodID ni_ctrID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
static jclass ni_iacls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
static jclass ni_ia4cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
static jclass ni_ia6cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
static jclass ni_ibcls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
static jmethodID ni_ia4ctrID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
static jmethodID ni_ia6ctrID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
static jmethodID ni_ibctrID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
static jfieldID ni_iaaddressID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
static jfieldID ni_iafamilyID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
static jfieldID ni_ia6ipaddressID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
static jfieldID ni_ibaddressID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
static jfieldID ni_ib4broadcastID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
static jfieldID ni_ib4maskID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
static jobject createNetworkInterface(JNIEnv *env, netif *ifs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
static netif *enumInterfaces(JNIEnv *env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
static netif *enumIPv4Interfaces(JNIEnv *env, netif *ifs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
static netif *enumIPv6Interfaces(JNIEnv *env, netif *ifs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
static netif *addif(JNIEnv *env, netif *ifs, char *if_name, int index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    int family, struct sockaddr *new_addrP, int new_addrlen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    short prefix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
static void freeif(netif *ifs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
static struct sockaddr *getBroadcast(JNIEnv *env, const char *ifname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
static short getSubnet(JNIEnv *env, const char *ifname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * Method:    init
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * Signature: ()V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
Java_java_net_NetworkInterface_init(JNIEnv *env, jclass cls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    ni_class = (*env)->FindClass(env,"java/net/NetworkInterface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    ni_class = (*env)->NewGlobalRef(env, ni_class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    ni_nameID = (*env)->GetFieldID(env, ni_class,"name", "Ljava/lang/String;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    ni_indexID = (*env)->GetFieldID(env, ni_class, "index", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    ni_addrsID = (*env)->GetFieldID(env, ni_class, "addrs", "[Ljava/net/InetAddress;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    ni_bindsID = (*env)->GetFieldID(env, ni_class, "bindings", "[Ljava/net/InterfaceAddress;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    ni_descID = (*env)->GetFieldID(env, ni_class, "displayName", "Ljava/lang/String;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    ni_virutalID = (*env)->GetFieldID(env, ni_class, "virtual", "Z");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    ni_childsID = (*env)->GetFieldID(env, ni_class, "childs", "[Ljava/net/NetworkInterface;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    ni_parentID = (*env)->GetFieldID(env, ni_class, "parent", "Ljava/net/NetworkInterface;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    ni_ctrID = (*env)->GetMethodID(env, ni_class, "<init>", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    ni_iacls = (*env)->FindClass(env, "java/net/InetAddress");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    ni_iacls = (*env)->NewGlobalRef(env, ni_iacls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    ni_ia4cls = (*env)->FindClass(env, "java/net/Inet4Address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    ni_ia4cls = (*env)->NewGlobalRef(env, ni_ia4cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    ni_ia6cls = (*env)->FindClass(env, "java/net/Inet6Address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    ni_ia6cls = (*env)->NewGlobalRef(env, ni_ia6cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    ni_ibcls = (*env)->FindClass(env, "java/net/InterfaceAddress");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    ni_ibcls = (*env)->NewGlobalRef(env, ni_ibcls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    ni_ia4ctrID = (*env)->GetMethodID(env, ni_ia4cls, "<init>", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    ni_ia6ctrID = (*env)->GetMethodID(env, ni_ia6cls, "<init>", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    ni_ibctrID = (*env)->GetMethodID(env, ni_ibcls, "<init>", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    ni_iaaddressID = (*env)->GetFieldID(env, ni_iacls, "address", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    ni_iafamilyID = (*env)->GetFieldID(env, ni_iacls, "family", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    ni_ia6ipaddressID = (*env)->GetFieldID(env, ni_ia6cls, "ipaddress", "[B");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    ni_ibaddressID = (*env)->GetFieldID(env, ni_ibcls, "address", "Ljava/net/InetAddress;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    ni_ib4broadcastID = (*env)->GetFieldID(env, ni_ibcls, "broadcast", "Ljava/net/Inet4Address;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    ni_ib4maskID = (*env)->GetFieldID(env, ni_ibcls, "maskLength", "S");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 * Method:    getByName0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 * Signature: (Ljava/lang/String;)Ljava/net/NetworkInterface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByName0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    (JNIEnv *env, jclass cls, jstring name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    netif *ifs, *curr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    jboolean isCopy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    const char* name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    jobject obj = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    ifs = enumInterfaces(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    if (ifs == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Search the list of interface based on name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    curr = ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    while (curr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (strcmp(name_utf, curr->name) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        curr = curr->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /* if found create a NetworkInterface */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    if (curr != NULL) {;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        obj = createNetworkInterface(env, curr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /* release the UTF string and interface list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    (*env)->ReleaseStringUTFChars(env, name, name_utf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    freeif(ifs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    return obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
 * Method:    getByIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
 * Signature: (Ljava/lang/String;)Ljava/net/NetworkInterface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    (JNIEnv *env, jclass cls, jint index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    netif *ifs, *curr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    jobject obj = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    if (index <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    ifs = enumInterfaces(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    if (ifs == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * Search the list of interface based on index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    curr = ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    while (curr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        if (index == curr->index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        curr = curr->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    /* if found create a NetworkInterface */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    if (curr != NULL) {;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        obj = createNetworkInterface(env, curr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    freeif(ifs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    return obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
 * Method:    getByInetAddress0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
 * Signature: (Ljava/net/InetAddress;)Ljava/net/NetworkInterface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByInetAddress0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    (JNIEnv *env, jclass cls, jobject iaObj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    netif *ifs, *curr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    int family = (*env)->GetIntField(env, iaObj, ni_iafamilyID) == IPv4?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        AF_INET : AF_INET6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    jobject obj = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    jboolean match = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    ifs = enumInterfaces(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    if (ifs == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    curr = ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    while (curr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        netaddr *addrP = curr->addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
         * Iterate through each address on the interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        while (addrP != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            if (family == addrP->family) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                if (family == AF_INET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    int address1 = htonl(((struct sockaddr_in*)addrP->addr)->sin_addr.s_addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    int address2 = (*env)->GetIntField(env, iaObj, ni_iaaddressID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    if (address1 == address2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                        match = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                if (family == AF_INET6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                    jbyte *bytes = (jbyte *)&(((struct sockaddr_in6*)addrP->addr)->sin6_addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                    jbyteArray ipaddress = (*env)->GetObjectField(env, iaObj, ni_ia6ipaddressID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    jbyte caddr[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    (*env)->GetByteArrayRegion(env, ipaddress, 0, 16, caddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                    while (i < 16) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                        if (caddr[i] != bytes[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                        i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                    if (i >= 16) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                        match = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            if (match) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            addrP = addrP->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        if (match) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        curr = curr->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    /* if found create a NetworkInterface */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    if (match) {;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        obj = createNetworkInterface(env, curr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    freeif(ifs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    return obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
 * Method:    getAll
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
 * Signature: ()[Ljava/net/NetworkInterface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
JNIEXPORT jobjectArray JNICALL Java_java_net_NetworkInterface_getAll
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    (JNIEnv *env, jclass cls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    netif *ifs, *curr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    jobjectArray netIFArr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    jint arr_index, ifCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    ifs = enumInterfaces(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    if (ifs == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    /* count the interface */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    ifCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    curr = ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    while (curr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        ifCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        curr = curr->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    /* allocate a NetworkInterface array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    netIFArr = (*env)->NewObjectArray(env, ifCount, cls, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    if (netIFArr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        freeif(ifs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * Iterate through the interfaces, create a NetworkInterface instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * for each array element and populate the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    curr = ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    arr_index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    while (curr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        jobject netifObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        netifObj = createNetworkInterface(env, curr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        if (netifObj == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            freeif(ifs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        /* put the NetworkInterface into the array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        (*env)->SetObjectArrayElement(env, netIFArr, arr_index++, netifObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        curr = curr->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    freeif(ifs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    return netIFArr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
 * Create a NetworkInterface object, populate the name and index, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
 * populate the InetAddress array based on the IP addresses for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
 * interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
jobject createNetworkInterface(JNIEnv *env, netif *ifs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    jobject netifObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    jobject name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    jobjectArray addrArr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    jobjectArray bindArr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    jobjectArray childArr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    netaddr *addrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    jint addr_index, addr_count, bind_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    jint child_count, child_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    netaddr *addrP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    netif *childP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    jobject tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * Create a NetworkInterface object and populate it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    netifObj = (*env)->NewObject(env, ni_class, ni_ctrID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    name = (*env)->NewStringUTF(env, ifs->name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    if (netifObj == NULL || name == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    (*env)->SetObjectField(env, netifObj, ni_nameID, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    (*env)->SetObjectField(env, netifObj, ni_descID, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    (*env)->SetIntField(env, netifObj, ni_indexID, ifs->index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    (*env)->SetBooleanField(env, netifObj, ni_virutalID, ifs->virtual ? JNI_TRUE : JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * Count the number of address on this interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    addr_count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    addrP = ifs->addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    while (addrP != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        addr_count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        addrP = addrP->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * Create the array of InetAddresses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    addrArr = (*env)->NewObjectArray(env, addr_count,  ni_iacls, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    if (addrArr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    bindArr = (*env)->NewObjectArray(env, addr_count, ni_ibcls, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    if (bindArr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
      return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    addrP = ifs->addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    addr_index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    bind_index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    while (addrP != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        jobject iaObj = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        jobject ibObj = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        if (addrP->family == AF_INET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            iaObj = (*env)->NewObject(env, ni_ia4cls, ni_ia4ctrID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            if (iaObj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                 (*env)->SetIntField(env, iaObj, ni_iaaddressID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                     htonl(((struct sockaddr_in*)addrP->addr)->sin_addr.s_addr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            if (ibObj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
              (*env)->SetObjectField(env, ibObj, ni_ibaddressID, iaObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
              if (addrP->brdcast) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                jobject ia2Obj = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                ia2Obj = (*env)->NewObject(env, ni_ia4cls, ni_ia4ctrID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                if (ia2Obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                  (*env)->SetIntField(env, ia2Obj, ni_iaaddressID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                                      htonl(((struct sockaddr_in*)addrP->brdcast)->sin_addr.s_addr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                  (*env)->SetObjectField(env, ibObj, ni_ib4broadcastID, ia2Obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                  (*env)->SetShortField(env, ibObj, ni_ib4maskID, addrP->mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
              (*env)->SetObjectArrayElement(env, bindArr, bind_index++, ibObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        if (addrP->family == AF_INET6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            int scope=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            iaObj = (*env)->NewObject(env, ni_ia6cls, ni_ia6ctrID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            if (iaObj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                jbyteArray ipaddress = (*env)->NewByteArray(env, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                if (ipaddress == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                (*env)->SetByteArrayRegion(env, ipaddress, 0, 16,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                    (jbyte *)&(((struct sockaddr_in6*)addrP->addr)->sin6_addr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                if (!kernelIsV22()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                    scope = ((struct sockaddr_in6*)addrP->addr)->sin6_scope_id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                scope = ((struct sockaddr_in6*)addrP->addr)->sin6_scope_id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                if (scope != 0) { /* zero is default value, no need to set */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                    (*env)->SetIntField(env, iaObj, ia6_scopeidID, scope);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                    (*env)->SetBooleanField(env, iaObj, ia6_scopeidsetID, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                    (*env)->SetObjectField(env, iaObj, ia6_scopeifnameID, netifObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                (*env)->SetObjectField(env, iaObj, ni_ia6ipaddressID, ipaddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            if (ibObj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
              (*env)->SetObjectField(env, ibObj, ni_ibaddressID, iaObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
              (*env)->SetShortField(env, ibObj, ni_ib4maskID, addrP->mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
              (*env)->SetObjectArrayElement(env, bindArr, bind_index++, ibObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        if (iaObj == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        (*env)->SetObjectArrayElement(env, addrArr, addr_index++, iaObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        addrP = addrP->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * See if there is any virtual interface attached to this one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    child_count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    childP = ifs->childs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    while (childP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
      child_count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
      childP = childP->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    childArr = (*env)->NewObjectArray(env, child_count, ni_class, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    if (childArr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
      return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * Create the NetworkInterface instances for the sub-interfaces as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    child_index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    childP = ifs->childs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    while(childP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
      tmp = createNetworkInterface(env, childP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
      if (tmp == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
      (*env)->SetObjectField(env, tmp, ni_parentID, netifObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
      (*env)->SetObjectArrayElement(env, childArr, child_index++, tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
      childP = childP->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    (*env)->SetObjectField(env, netifObj, ni_addrsID, addrArr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    (*env)->SetObjectField(env, netifObj, ni_bindsID, bindArr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    (*env)->SetObjectField(env, netifObj, ni_childsID, childArr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    /* return the NetworkInterface */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    return netifObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
 * Enumerates all interfaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
static netif *enumInterfaces(JNIEnv *env) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    netif *ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * Enumerate IPv4 addresses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    ifs = enumIPv4Interfaces(env, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    if (ifs == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        if ((*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * If IPv6 is available then enumerate IPv6 addresses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        ifs = enumIPv6Interfaces(env, ifs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        if ((*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            freeif(ifs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    return ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
 * Enumerates and returns all IPv4 interfaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
static netif *enumIPv4Interfaces(JNIEnv *env, netif *ifs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    int sock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    struct ifconf ifc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    struct ifreq *ifreqP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    char *buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    int numifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    unsigned i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    unsigned bufsize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    sock = JVM_Socket(AF_INET, SOCK_DGRAM, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    if (sock < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
         * If EPROTONOSUPPORT is returned it means we don't have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
         * IPv4 support so don't throw an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        if (errno != EPROTONOSUPPORT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                             "Socket creation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        return ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    /* need to do a dummy SIOCGIFCONF to determine the buffer size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * SIOCGIFCOUNT doesn't work
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    ifc.ifc_buf = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                         "ioctl SIOCGIFCONF failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        close(sock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        return ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    bufsize = ifc.ifc_len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    if (ioctl(sock, SIOCGIFNUM, (char *)&numifs) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                         "ioctl SIOCGIFNUM failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        close(sock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        return ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    bufsize = numifs * sizeof (struct ifreq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
#endif /* __linux__ */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    buf = (char *)malloc(bufsize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    if (!buf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        JNU_ThrowOutOfMemoryError(env, "heap allocation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        (void) close(sock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        return ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    ifc.ifc_len = bufsize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    ifc.ifc_buf = buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                         "ioctl SIOCGIFCONF failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        (void) close(sock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        (void) free(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        return ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * Iterate through each interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    ifreqP = ifc.ifc_req;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    for (i=0; i<ifc.ifc_len/sizeof (struct ifreq); i++, ifreqP++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        struct ifreq if2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        memset((char *)&if2, 0, sizeof(if2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        strcpy(if2.ifr_name, ifreqP->ifr_name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
         * Try to get the interface index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
         * (Not supported on Solaris 2.6 or 7)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        if (ioctl(sock, SIOCGIFINDEX, (char *)&if2) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            index = if2.ifr_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            index = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
         * Add to the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        ifs = addif(env, ifs, ifreqP->ifr_name, index, AF_INET,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                    (struct sockaddr *)&(ifreqP->ifr_addr),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                    sizeof(struct sockaddr_in), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
         * If an exception occurred then free the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        if ((*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            close(sock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            free(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            freeif(ifs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * Free socket and buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    close(sock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    free(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    return ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
#if defined(__solaris__) && defined(AF_INET6)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
 * Enumerates and returns all IPv6 interfaces on Solaris
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
static netif *enumIPv6Interfaces(JNIEnv *env, netif *ifs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    int sock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    struct lifconf ifc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    struct lifreq *ifr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    char *buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    struct lifnum numifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    unsigned bufsize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    sock = JVM_Socket(AF_INET6, SOCK_DGRAM, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    if (sock < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                         "Failed to create IPv6 socket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        return ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * Get the interface count
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
    numifs.lifn_family = AF_UNSPEC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    numifs.lifn_flags = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    if (ioctl(sock, SIOCGLIFNUM, (char *)&numifs) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                         "ioctl SIOCGLIFNUM failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        close(sock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        return ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     *  Enumerate the interface configurations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
    bufsize = numifs.lifn_count * sizeof (struct lifreq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    buf = (char *)malloc(bufsize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    if (!buf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        JNU_ThrowOutOfMemoryError(env, "heap allocation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        (void) close(sock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        return ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    ifc.lifc_family = AF_UNSPEC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
    ifc.lifc_flags = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
    ifc.lifc_len = bufsize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    ifc.lifc_buf = buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    if (ioctl(sock, SIOCGLIFCONF, (char *)&ifc) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                         "ioctl SIOCGLIFCONF failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        close(sock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        free(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        return ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * Iterate through each interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    ifr = ifc.lifc_req;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
    for (n=0; n<numifs.lifn_count; n++, ifr++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        int index = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        struct lifreq if2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
         * Ignore non-IPv6 addresses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        if (ifr->lifr_addr.ss_family != AF_INET6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
         * Get the index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        memset((char *)&if2, 0, sizeof(if2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        strcpy(if2.lifr_name, ifr->lifr_name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        if (ioctl(sock, SIOCGLIFINDEX, (char *)&if2) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            struct sockaddr_in6 *s6= (struct sockaddr_in6 *)&(ifr->lifr_addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            index = if2.lifr_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            s6->sin6_scope_id = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        /* add to the list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        ifs = addif(env, ifs, ifr->lifr_name, index, AF_INET6,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                    (struct sockaddr *)&(ifr->lifr_addr),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                    sizeof(struct sockaddr_in6), (short) ifr->lifr_addrlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
         * If an exception occurred we return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        if ((*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
            close(sock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            free(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            return ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
    close(sock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
    free(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
    return ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
#if defined(__linux__) && defined(AF_INET6)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
 * Enumerates and returns all IPv6 interfaces on Linux
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
static netif *enumIPv6Interfaces(JNIEnv *env, netif *ifs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    FILE *f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
    char addr6[40], devname[20];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    char addr6p[8][5];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
    int plen, scope, dad_status, if_idx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
    uint8_t ipv6addr[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    if ((f = fopen(_PATH_PROCNET_IFINET6, "r")) != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %20s\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                      addr6p[0], addr6p[1], addr6p[2], addr6p[3],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                      addr6p[4], addr6p[5], addr6p[6], addr6p[7],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                  &if_idx, &plen, &scope, &dad_status, devname) != EOF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            struct netif *ifs_ptr = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            struct netif *last_ptr = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            struct sockaddr_in6 addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            sprintf(addr6, "%s:%s:%s:%s:%s:%s:%s:%s",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                    addr6p[0], addr6p[1], addr6p[2], addr6p[3],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                    addr6p[4], addr6p[5], addr6p[6], addr6p[7]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            inet_pton(AF_INET6, addr6, ipv6addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            memset(&addr, 0, sizeof(struct sockaddr_in6));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            memcpy((void*)addr.sin6_addr.s6_addr, (const void*)ipv6addr, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            addr.sin6_scope_id = if_idx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            ifs = addif(env, ifs, devname, if_idx, AF_INET6,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                        (struct sockaddr *)&addr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                        sizeof(struct sockaddr_in6), plen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
             * If an exception occurred then return the list as is.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            if ((*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                fclose(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                return ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        fclose(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
    return ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
 * Free an interface list (including any attached addresses)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
void freeif(netif *ifs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
    netif *currif = ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
    netif *child = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    while (currif != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        netaddr *addrP = currif->addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        while (addrP != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            netaddr *next = addrP->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            if (addrP->addr != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                free(addrP->addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            if (addrP->brdcast != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                free(addrP->brdcast);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
            free(addrP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            addrP = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        free(currif->name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
         * Don't forget to free the sub-interfaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        if (currif->childs != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
          freeif(currif->childs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        ifs = currif->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        free(currif);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        currif = ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
 * Add an interface to the list. If known interface just link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
 * a new netaddr onto the list. If new interface create new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
 * netif structure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
netif *addif(JNIEnv *env, netif *ifs, char *if_name, int index, int family,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
             struct sockaddr *new_addrP, int new_addrlen, short prefix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
  netif *currif = ifs, *parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
    netaddr *addrP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
#ifdef LIFNAMSIZ
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
    char name[LIFNAMSIZ];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
    char vname[LIFNAMSIZ];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
    char name[IFNAMSIZ];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
    char vname[IFNAMSIZ];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
    char *unit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
    int isVirtual = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     * If the interface name is a logical interface then we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     * remove the unit number so that we have the physical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     * interface (eg: hme0:1 -> hme0). NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * currently doesn't have any concept of physical vs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     * logical interfaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
    strcpy(name, if_name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     * Create and populate the netaddr node. If allocation fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     * return an un-updated list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
    addrP = (netaddr *)malloc(sizeof(netaddr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
    if (addrP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        addrP->addr = (struct sockaddr *)malloc(new_addrlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        if (addrP->addr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
            free(addrP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
            addrP = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
    if (addrP == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        JNU_ThrowOutOfMemoryError(env, "heap allocation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        return ifs; /* return untouched list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
    memcpy(addrP->addr, new_addrP, new_addrlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
    addrP->family = family;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
    addrP->brdcast = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
    addrP->mask = prefix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
    if (family == AF_INET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
      /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
       * Deal with brodcast addr & subnet mask
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
      addrP->brdcast = getBroadcast(env, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
      if (addrP->brdcast) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        addrP->mask = getSubnet(env, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
    vname[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
    unit = strchr(name, ':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
    if (unit != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
      /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
       * This is a virtual interface. If we are able to access the parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
       * we need to create a new entry if it doesn't exist yet *and* update
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
       * the 'parent' interface with the new records.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
      struct ifreq if2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
      int sock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
      int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
      sock = JVM_Socket(AF_INET, SOCK_DGRAM, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
      if (sock < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                                     "Socket creation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        return ifs; /* return untouched list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
      len = unit - name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
      if (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        // temporarily use vname to hold the parent name of the interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        // instead of creating another buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        memcpy(&vname, name, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        vname[len] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        memset((char *) &if2, 0, sizeof(if2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        strcpy(if2.ifr_name, vname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        if (ioctl(sock, SIOCGIFFLAGS, (char *)&if2) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
           // Got access to parent, so create it if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
           strcpy(vname, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
           *unit = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
          // failed to access parent interface do not create parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
          // We are a virtual interface with no parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
          isVirtual = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
          vname[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
      close(sock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     * Check if this is a "new" interface. Use the interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     * name for matching because index isn't supported on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     * Solaris 2.6 & 7.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
    while (currif != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        if (strcmp(name, currif->name) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        currif = currif->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     * If "new" then create an netif structure and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     * insert it onto the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
    if (currif == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        currif = (netif *)malloc(sizeof(netif));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        if (currif) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
            currif->name = strdup(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
            if (currif->name == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                free(currif);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                currif = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        if (currif == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            JNU_ThrowOutOfMemoryError(env, "heap allocation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
            return ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        currif->index = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        currif->addr = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        currif->childs = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        currif->virtual = isVirtual;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        currif->next = ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
        ifs = currif;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     * Finally insert the address on the interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
    addrP->next = currif->addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
    currif->addr = addrP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
    parent = currif;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     * Let's deal with the virtual interface now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
    if (vname[0]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
      netaddr *tmpaddr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
      currif = parent->childs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
      while (currif != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        if (strcmp(vname, currif->name) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
          break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        currif = currif->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
      if (currif == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        currif = (netif *)malloc(sizeof(netif));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        if (currif) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
          currif->name = strdup(vname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
          if (currif->name == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
            free(currif);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
            currif = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        if (currif == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
          JNU_ThrowOutOfMemoryError(env, "heap allocation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
          return ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        currif->index = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        currif->addr = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        /* Need to duplicate the addr entry? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        currif->virtual = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        currif->childs = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        currif->next = parent->childs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        parent->childs = currif;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
      tmpaddr = (netaddr *) malloc(sizeof(netaddr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
      if (tmpaddr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        JNU_ThrowOutOfMemoryError(env, "heap allocation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        return ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
      memcpy(tmpaddr, addrP, sizeof(netaddr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
      /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
       * Let's duplicate the address and broadcast address structures
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
       * if there are any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
      if (addrP->addr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
        tmpaddr->addr = malloc(new_addrlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        if (tmpaddr->addr != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
          memcpy(tmpaddr->addr, addrP->addr, new_addrlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
      if (addrP->brdcast != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        tmpaddr->brdcast = malloc(new_addrlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        if (tmpaddr->brdcast != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
          memcpy(tmpaddr->brdcast, addrP->brdcast, new_addrlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
      tmpaddr->next = currif->addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
      currif->addr = tmpaddr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
    return ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
 * Get flags from a NetworkInterface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
static short getFlags(JNIEnv *env, jstring name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
  int sock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
  struct ifreq if2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
  jboolean isCopy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
  const char* name_utf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
  short ret = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
  sock = JVM_Socket(AF_INET, SOCK_DGRAM, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
  if (sock < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
    NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
                                 "Socket creation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
  name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
  memset((char *) &if2, 0, sizeof(if2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
  strcpy(if2.ifr_name, name_utf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
  if (ioctl(sock, SIOCGIFFLAGS, (char *)&if2) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
    ret = if2.ifr_flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
  } else {
709
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1120
#if defined(__solaris__) && defined(AF_INET6)
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1121
    /* Try with an IPv6 socket in case the interface has only IPv6 addresses assigned to it */
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1122
    struct lifreq lifr;
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1123
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1124
    close(sock);
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1125
    sock = JVM_Socket(AF_INET6, SOCK_DGRAM, 0);
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1126
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1127
    if (sock < 0) {
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1128
      (*env)->ReleaseStringUTFChars(env, name, name_utf);
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1129
      NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1130
                                  "Socket creation failed");
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1131
      return -1;
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1132
    }
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1133
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1134
    memset((caddr_t)&lifr, 0, sizeof(lifr));
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1135
    strcpy((caddr_t)&(lifr.lifr_name), name_utf);
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1136
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1137
    if (ioctl(sock, SIOCGLIFFLAGS, (char *)&lifr) >= 0) {
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1138
      ret = lifr.lifr_flags;
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1139
    } else {
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1140
      NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1141
                               "IOCTL failed");
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1142
    }
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1143
#else
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
    NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
                                 "IOCTL failed");
709
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1146
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
  close(sock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
  /* release the UTF string and interface list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
  (*env)->ReleaseStringUTFChars(env, name, name_utf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
  return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
 * Returns the IPv4 broadcast address of a named interface, if it exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
 * Returns 0 if it doesn't have one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
static struct sockaddr *getBroadcast(JNIEnv *env, const char *ifname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
  int sock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
  unsigned int mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
  struct sockaddr *ret = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
  struct ifreq if2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
  short flag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
  sock = JVM_Socket(AF_INET, SOCK_DGRAM, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
  if (sock < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
    NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
                                 "Socket creation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
  memset((char *) &if2, 0, sizeof(if2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
  strcpy(if2.ifr_name, ifname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
  /* Let's make sure the interface does have a broadcast address */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
  if (ioctl(sock, SIOCGIFFLAGS, (char *)&if2) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
    flag = if2.ifr_flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
  } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
    NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
                                 "IOCTL failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
  if (flag & IFF_BROADCAST) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
    /* It does, let's retrieve it*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
    if (ioctl(sock, SIOCGIFBRDADDR, (char *)&if2) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
      ret = (struct sockaddr*) malloc(sizeof(struct sockaddr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
      memcpy(ret, &if2.ifr_broadaddr, sizeof(struct sockaddr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
      NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
                                   "IOCTL failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
  close(sock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
  return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
 * Returns the IPv4 subnet prefix length (aka subnet mask) for the named
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
 * interface, if it has one, otherwise return -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
static short getSubnet(JNIEnv *env, const char *ifname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
  int sock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
  unsigned int mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
  short ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
  struct ifreq if2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
  sock = JVM_Socket(AF_INET, SOCK_DGRAM, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
  if (sock < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
    NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
                                 "Socket creation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
  memset((char *) &if2, 0, sizeof(if2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
  strcpy(if2.ifr_name, ifname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
  if (ioctl(sock, SIOCGIFNETMASK, (char *)&if2) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
    mask = ntohl(((struct sockaddr_in*)&(if2.ifr_addr))->sin_addr.s_addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
    ret = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
    while (mask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
      mask <<= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
      ret++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
    close(sock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
  NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
                               "IOCTL failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
  close(sock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
  return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
#define DEV_PREFIX      "/dev/"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
 * Solaris specific DLPI code to get hardware address from a device.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
 * Unfortunately, at least up to Solaris X, you have to have special
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
 * privileges (i.e. be root).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
static int getMacFromDevice(JNIEnv *env, const char* ifname, unsigned char* retbuf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
  char style1dev[MAXPATHLEN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
  int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
  dl_phys_addr_req_t dlpareq;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
  dl_phys_addr_ack_t *dlpaack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
  struct strbuf msg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
  char buf[128];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
  int flags = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
  /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
   * Device is in /dev
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
   * e.g.: /dev/bge0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
  strcpy(style1dev, DEV_PREFIX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
  strcat(style1dev, ifname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
  if ((fd = open(style1dev, O_RDWR)) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     * Can't open it. We probably are missing the privilege.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     * We'll have to try something else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
  dlpareq.dl_primitive = DL_PHYS_ADDR_REQ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
  dlpareq.dl_addr_type = DL_CURR_PHYS_ADDR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
  msg.buf = (char *)&dlpareq;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
  msg.len = DL_PHYS_ADDR_REQ_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
  if (putmsg(fd, &msg, NULL, 0) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
    NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
                                 "putmsg failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
  dlpaack = (dl_phys_addr_ack_t *)buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
  msg.buf = (char *)buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
  msg.len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
  msg.maxlen = sizeof (buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
  if (getmsg(fd, &msg, NULL, &flags) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
    NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
                                 "getmsg failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
  if (msg.len < DL_PHYS_ADDR_ACK_SIZE ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
      dlpaack->dl_primitive != DL_PHYS_ADDR_ACK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
    JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
                    "Couldn't obtain phys addr\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
  memcpy(retbuf, &buf[dlpaack->dl_addr_offset], dlpaack->dl_addr_length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
  return dlpaack->dl_addr_length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
 * Get the Hardware address (usually MAC address) for the named interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
 * return puts the data in buf, and returns the length, in byte, of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
 * MAC address. Returns -1 if there is no hardware address on that interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
int getMacAddress(JNIEnv *env, const struct in_addr* addr, const char* ifname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
                  unsigned char *buf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
  int sock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
  static struct ifreq ifr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
  int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
  sock = JVM_Socket(AF_INET, SOCK_DGRAM, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
  if (sock < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
    NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
                                 "Socket creation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
  strcpy(ifr.ifr_name, ifname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
  if (ioctl(sock, SIOCGIFHWADDR, &ifr) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
    fprintf(stderr, "SIOCIFHWADDR: %s\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
            strerror(errno));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
    close(sock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
  memcpy(buf, &ifr.ifr_hwaddr.sa_data, IFHWADDRLEN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
  close(sock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
  for (i = 0; i < IFHWADDRLEN; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
    if (buf[i] != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
      return IFHWADDRLEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
  /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
   * All bytes to 0 means no hardware address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
  return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
  struct arpreq arpreq;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
  struct sockaddr_in* sin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
  struct sockaddr_in ipAddr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
  int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
  /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
   * On Solaris we have to use DLPI, but it will only work if we have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
   * privileged access (i.e. root). If that fails, we try a lookup
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
   * in the ARP table, which requires an IPv4 address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
  if ((len = getMacFromDevice(env, ifname, buf)) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
    return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
  if (addr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
     * No IPv4 address for that interface, so can't do an ARP lookup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
  sin = (struct sockaddr_in *) &arpreq.arp_pa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
  memset((char *) &arpreq, 0, sizeof(struct arpreq));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
  ipAddr.sin_port = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
  ipAddr.sin_family = AF_INET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
  memcpy(&ipAddr.sin_addr, addr, sizeof(struct in_addr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
  memcpy(&arpreq.arp_pa, &ipAddr, sizeof(struct sockaddr_in));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
  arpreq.arp_flags= ATF_PUBL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
  sock = JVM_Socket(AF_INET, SOCK_DGRAM, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
  if (sock < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
    NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
                                 "Socket creation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
  if (ioctl(sock, SIOCGARP, &arpreq) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
    close(sock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
    memcpy(buf, &arpreq.arp_ha.sa_data[0], 6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
    return 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
  if (errno != ENXIO) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
    // "No such device or address" means no hardware address, so it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
    // normal don't throw an exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
    NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
                                 "IOCTL failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
  close(sock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
  return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
 * Method:    isUp0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
 * Signature: (Ljava/lang/String;I)Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isUp0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
    (JNIEnv *env, jclass cls, jstring name, jint index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
    short val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
    val = getFlags(env, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
    if ( (val & IFF_UP) && (val &  IFF_RUNNING))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
      return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
    return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
 * Method:    isP2P0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
 * Signature: (Ljava/lang/String;I)Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isP2P0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
    (JNIEnv *env, jclass cls, jstring name, jint index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
    if (getFlags(env, name) & IFF_POINTOPOINT)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
      return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
    return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
 * Method:    isLoopback0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
 * Signature: (Ljava/lang/String;I)Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isLoopback0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
    (JNIEnv *env, jclass cls, jstring name, jint index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
    if (getFlags(env, name) & IFF_LOOPBACK)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
      return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
    return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
 * Method:    supportsMulticast0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
 * Signature: (Ljava/lang/String;I)Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_supportsMulticast0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
(JNIEnv *env, jclass cls, jstring name, jint index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
  short val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
  val = getFlags(env, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
  if (val & IFF_MULTICAST)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
    return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
  return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
 * Method:    getMacAddr0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
 * Signature: ([bLjava/lang/String;I)[b
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
JNIEXPORT jbyteArray JNICALL Java_java_net_NetworkInterface_getMacAddr0(JNIEnv *env, jclass class, jbyteArray addrArray, jstring name, jint index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
  jint addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
  jbyte caddr[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
  struct in_addr iaddr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
  jbyteArray ret = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
  unsigned char mac[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
  int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
  jboolean isCopy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
  const char* name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
  if (!IS_NULL(addrArray)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
    (*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
    addr = ((caddr[0]<<24) & 0xff000000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
    addr |= ((caddr[1] <<16) & 0xff0000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
    addr |= ((caddr[2] <<8) & 0xff00);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
    addr |= (caddr[3] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
    iaddr.s_addr = htonl(addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
    len = getMacAddress(env, &iaddr, name_utf, mac);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
  } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
    len = getMacAddress(env, NULL, name_utf, mac);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
  if (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
    ret = (*env)->NewByteArray(env, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
    if (IS_NULL(ret)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
      /* we may have memory to free at the end of this */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
      goto fexit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
    (*env)->SetByteArrayRegion(env, ret, 0, len, (jbyte *) (mac));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
 fexit:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
  /* release the UTF string and interface list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
  (*env)->ReleaseStringUTFChars(env, name, name_utf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
  return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
 * Class:       java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
 * Method:      getMTU0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
 * Signature:   ([bLjava/lang/String;I)I
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
JNIEXPORT jint JNICALL Java_java_net_NetworkInterface_getMTU0(JNIEnv *env, jclass class, jstring name, jint index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
  jboolean isCopy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
  int sock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
  struct ifreq if2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
  int ret = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
  const char* name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
  sock = JVM_Socket(AF_INET, SOCK_DGRAM, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
  if (sock < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
    NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
                                 "Socket creation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
  } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
    memset((char *) &if2, 0, sizeof(if2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
    strcpy(if2.ifr_name, name_utf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
    if (ioctl(sock, SIOCGIFMTU, (char *)&if2) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
      ret= if2.ifr_mtu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
      NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
                                   "IOCTL failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
#else /* Solaris */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
    struct   lifreq lifr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
    memset((caddr_t)&lifr, 0, sizeof(lifr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
    strcpy((caddr_t)&(lifr.lifr_name), name_utf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
    if (ioctl(sock, SIOCGLIFMTU, (caddr_t)&lifr) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
      ret = lifr.lifr_mtu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
    } else {
709
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1511
      /* Try wIth an IPv6 socket in case the interface has only IPv6 addresses assigned to it */
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1512
      close(sock);
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1513
      sock = JVM_Socket(AF_INET6, SOCK_DGRAM, 0);
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1514
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1515
      if (sock < 0) {
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1516
        (*env)->ReleaseStringUTFChars(env, name, name_utf);
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1517
        NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1518
                                     "Socket creation failed");
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1519
        return -1;
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1520
      }
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1521
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1522
      if (ioctl(sock, SIOCGLIFMTU, (caddr_t)&lifr) >= 0) {
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1523
        ret = lifr.lifr_mtu;
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1524
      } else {
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1525
        NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1526
                                     "IOCTL failed");
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1527
      }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
    close(sock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
  /* release the UTF string and interface list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
  (*env)->ReleaseStringUTFChars(env, name, name_utf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
  return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
}