jdk/src/solaris/native/java/net/NetworkInterface.c
author xdono
Tue, 28 Jul 2009 12:12:54 -0700
changeset 3288 db82a42da273
parent 2163 1586577d5107
child 4814 dbf72872f8d2
permissions -rw-r--r--
6862919: Update copyright year Summary: Update copyright for files that have been modified in 2009, up to 07/09 Reviewed-by: tbell, ohair
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
2163
1586577d5107 6814575: Update copyright year
xdono
parents: 2058
diff changeset
     2
 * Copyright 2000-2009 Sun Microsystems, Inc.  All Rights Reserved.
2
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
1097
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 910
diff changeset
   209
 * Method:    getByIndex0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
 * Signature: (Ljava/lang/String;)Ljava/net/NetworkInterface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
 */
1097
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 910
diff changeset
   212
JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByIndex0
2
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
    jint addr_index, addr_count, bind_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    jint child_count, child_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    netaddr *addrP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    netif *childP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    jobject tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * Create a NetworkInterface object and populate it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    netifObj = (*env)->NewObject(env, ni_class, ni_ctrID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    name = (*env)->NewStringUTF(env, ifs->name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    if (netifObj == NULL || name == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    (*env)->SetObjectField(env, netifObj, ni_nameID, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    (*env)->SetObjectField(env, netifObj, ni_descID, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    (*env)->SetIntField(env, netifObj, ni_indexID, ifs->index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    (*env)->SetBooleanField(env, netifObj, ni_virutalID, ifs->virtual ? JNI_TRUE : JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * Count the number of address on this interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    addr_count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    addrP = ifs->addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    while (addrP != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        addr_count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        addrP = addrP->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * Create the array of InetAddresses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    addrArr = (*env)->NewObjectArray(env, addr_count,  ni_iacls, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    if (addrArr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    bindArr = (*env)->NewObjectArray(env, addr_count, ni_ibcls, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    if (bindArr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
      return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    addrP = ifs->addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    addr_index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    bind_index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    while (addrP != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        jobject iaObj = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        jobject ibObj = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        if (addrP->family == AF_INET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            iaObj = (*env)->NewObject(env, ni_ia4cls, ni_ia4ctrID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            if (iaObj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                 (*env)->SetIntField(env, iaObj, ni_iaaddressID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                     htonl(((struct sockaddr_in*)addrP->addr)->sin_addr.s_addr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            if (ibObj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
              (*env)->SetObjectField(env, ibObj, ni_ibaddressID, iaObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
              if (addrP->brdcast) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                jobject ia2Obj = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                ia2Obj = (*env)->NewObject(env, ni_ia4cls, ni_ia4ctrID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                if (ia2Obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                  (*env)->SetIntField(env, ia2Obj, ni_iaaddressID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                                      htonl(((struct sockaddr_in*)addrP->brdcast)->sin_addr.s_addr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                  (*env)->SetObjectField(env, ibObj, ni_ib4broadcastID, ia2Obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                  (*env)->SetShortField(env, ibObj, ni_ib4maskID, addrP->mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
              (*env)->SetObjectArrayElement(env, bindArr, bind_index++, ibObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        if (addrP->family == AF_INET6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            int scope=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            iaObj = (*env)->NewObject(env, ni_ia6cls, ni_ia6ctrID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            if (iaObj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                jbyteArray ipaddress = (*env)->NewByteArray(env, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                if (ipaddress == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                (*env)->SetByteArrayRegion(env, ipaddress, 0, 16,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                    (jbyte *)&(((struct sockaddr_in6*)addrP->addr)->sin6_addr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                if (!kernelIsV22()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                    scope = ((struct sockaddr_in6*)addrP->addr)->sin6_scope_id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                scope = ((struct sockaddr_in6*)addrP->addr)->sin6_scope_id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                if (scope != 0) { /* zero is default value, no need to set */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                    (*env)->SetIntField(env, iaObj, ia6_scopeidID, scope);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                    (*env)->SetBooleanField(env, iaObj, ia6_scopeidsetID, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                    (*env)->SetObjectField(env, iaObj, ia6_scopeifnameID, netifObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                (*env)->SetObjectField(env, iaObj, ni_ia6ipaddressID, ipaddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            if (ibObj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
              (*env)->SetObjectField(env, ibObj, ni_ibaddressID, iaObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
              (*env)->SetShortField(env, ibObj, ni_ib4maskID, addrP->mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
              (*env)->SetObjectArrayElement(env, bindArr, bind_index++, ibObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        if (iaObj == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        (*env)->SetObjectArrayElement(env, addrArr, addr_index++, iaObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        addrP = addrP->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * See if there is any virtual interface attached to this one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    child_count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    childP = ifs->childs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    while (childP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
      child_count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
      childP = childP->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    childArr = (*env)->NewObjectArray(env, child_count, ni_class, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    if (childArr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
      return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * Create the NetworkInterface instances for the sub-interfaces as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    child_index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    childP = ifs->childs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    while(childP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
      tmp = createNetworkInterface(env, childP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
      if (tmp == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
      (*env)->SetObjectField(env, tmp, ni_parentID, netifObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
      (*env)->SetObjectArrayElement(env, childArr, child_index++, tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
      childP = childP->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    (*env)->SetObjectField(env, netifObj, ni_addrsID, addrArr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    (*env)->SetObjectField(env, netifObj, ni_bindsID, bindArr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    (*env)->SetObjectField(env, netifObj, ni_childsID, childArr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    /* return the NetworkInterface */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    return netifObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
 * Enumerates all interfaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
static netif *enumInterfaces(JNIEnv *env) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    netif *ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * Enumerate IPv4 addresses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    ifs = enumIPv4Interfaces(env, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    if (ifs == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        if ((*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        }
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
     * If IPv6 is available then enumerate IPv6 addresses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        ifs = enumIPv6Interfaces(env, ifs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        if ((*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            freeif(ifs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    return ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
}
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
 * Enumerates and returns all IPv4 interfaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
static netif *enumIPv4Interfaces(JNIEnv *env, netif *ifs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    int sock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    struct ifconf ifc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    struct ifreq *ifreqP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    char *buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    int numifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    unsigned i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    unsigned bufsize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    sock = JVM_Socket(AF_INET, SOCK_DGRAM, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    if (sock < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
         * If EPROTONOSUPPORT is returned it means we don't have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
         * IPv4 support so don't throw an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        if (errno != EPROTONOSUPPORT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                             "Socket creation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        return ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    /* need to do a dummy SIOCGIFCONF to determine the buffer size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * SIOCGIFCOUNT doesn't work
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    ifc.ifc_buf = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                         "ioctl SIOCGIFCONF failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        close(sock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        return ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    bufsize = ifc.ifc_len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    if (ioctl(sock, SIOCGIFNUM, (char *)&numifs) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                         "ioctl SIOCGIFNUM failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        close(sock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        return ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    bufsize = numifs * sizeof (struct ifreq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
#endif /* __linux__ */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    buf = (char *)malloc(bufsize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    if (!buf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        JNU_ThrowOutOfMemoryError(env, "heap allocation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        (void) close(sock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        return ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    ifc.ifc_len = bufsize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    ifc.ifc_buf = buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                         "ioctl SIOCGIFCONF failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        (void) close(sock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        (void) free(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        return ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * Iterate through each interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    ifreqP = ifc.ifc_req;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    for (i=0; i<ifc.ifc_len/sizeof (struct ifreq); i++, ifreqP++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        struct ifreq if2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        memset((char *)&if2, 0, sizeof(if2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        strcpy(if2.ifr_name, ifreqP->ifr_name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
         * Try to get the interface index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
         * (Not supported on Solaris 2.6 or 7)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        if (ioctl(sock, SIOCGIFINDEX, (char *)&if2) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            index = if2.ifr_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            index = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
         * Add to the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        ifs = addif(env, ifs, ifreqP->ifr_name, index, AF_INET,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                    (struct sockaddr *)&(ifreqP->ifr_addr),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                    sizeof(struct sockaddr_in), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
         * If an exception occurred then free the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        if ((*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            close(sock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            free(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            freeif(ifs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        }
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
     * Free socket and buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    close(sock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    free(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    return ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
#if defined(__solaris__) && defined(AF_INET6)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
 * Enumerates and returns all IPv6 interfaces on Solaris
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
static netif *enumIPv6Interfaces(JNIEnv *env, netif *ifs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    int sock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    struct lifconf ifc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    struct lifreq *ifr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    char *buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    struct lifnum numifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    unsigned bufsize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    sock = JVM_Socket(AF_INET6, SOCK_DGRAM, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    if (sock < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                         "Failed to create IPv6 socket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        return ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * Get the interface count
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    numifs.lifn_family = AF_UNSPEC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
    numifs.lifn_flags = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    if (ioctl(sock, SIOCGLIFNUM, (char *)&numifs) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                         "ioctl SIOCGLIFNUM failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        close(sock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        return ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     *  Enumerate the interface configurations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    bufsize = numifs.lifn_count * sizeof (struct lifreq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
    buf = (char *)malloc(bufsize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    if (!buf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        JNU_ThrowOutOfMemoryError(env, "heap allocation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        (void) close(sock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        return ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    ifc.lifc_family = AF_UNSPEC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    ifc.lifc_flags = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
    ifc.lifc_len = bufsize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
    ifc.lifc_buf = buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    if (ioctl(sock, SIOCGLIFCONF, (char *)&ifc) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                         "ioctl SIOCGLIFCONF failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        close(sock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        free(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        return ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * Iterate through each interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    ifr = ifc.lifc_req;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    for (n=0; n<numifs.lifn_count; n++, ifr++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        int index = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        struct lifreq if2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
         * Ignore non-IPv6 addresses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        if (ifr->lifr_addr.ss_family != AF_INET6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
         * Get the index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        memset((char *)&if2, 0, sizeof(if2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        strcpy(if2.lifr_name, ifr->lifr_name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        if (ioctl(sock, SIOCGLIFINDEX, (char *)&if2) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            struct sockaddr_in6 *s6= (struct sockaddr_in6 *)&(ifr->lifr_addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            index = if2.lifr_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            s6->sin6_scope_id = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        /* add to the list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        ifs = addif(env, ifs, ifr->lifr_name, index, AF_INET6,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                    (struct sockaddr *)&(ifr->lifr_addr),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                    sizeof(struct sockaddr_in6), (short) ifr->lifr_addrlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
         * If an exception occurred we return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        if ((*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            close(sock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
            free(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            return ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        }
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
    close(sock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
    free(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
    return ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
#if defined(__linux__) && defined(AF_INET6)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
 * Enumerates and returns all IPv6 interfaces on Linux
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
static netif *enumIPv6Interfaces(JNIEnv *env, netif *ifs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
    FILE *f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    char addr6[40], devname[20];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
    char addr6p[8][5];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    int plen, scope, dad_status, if_idx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
    uint8_t ipv6addr[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    if ((f = fopen(_PATH_PROCNET_IFINET6, "r")) != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %20s\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                      addr6p[0], addr6p[1], addr6p[2], addr6p[3],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                      addr6p[4], addr6p[5], addr6p[6], addr6p[7],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                  &if_idx, &plen, &scope, &dad_status, devname) != EOF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            struct sockaddr_in6 addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            sprintf(addr6, "%s:%s:%s:%s:%s:%s:%s:%s",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                    addr6p[0], addr6p[1], addr6p[2], addr6p[3],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                    addr6p[4], addr6p[5], addr6p[6], addr6p[7]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            inet_pton(AF_INET6, addr6, ipv6addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            memset(&addr, 0, sizeof(struct sockaddr_in6));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            memcpy((void*)addr.sin6_addr.s6_addr, (const void*)ipv6addr, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            addr.sin6_scope_id = if_idx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            ifs = addif(env, ifs, devname, if_idx, AF_INET6,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                        (struct sockaddr *)&addr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                        sizeof(struct sockaddr_in6), plen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
             * If an exception occurred then return the list as is.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            if ((*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                fclose(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                return ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        fclose(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
    return ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
 * Free an interface list (including any attached addresses)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
void freeif(netif *ifs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
    netif *currif = ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
    while (currif != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        netaddr *addrP = currif->addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        while (addrP != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            netaddr *next = addrP->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
            if (addrP->addr != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                free(addrP->addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            if (addrP->brdcast != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                free(addrP->brdcast);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            free(addrP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            addrP = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        free(currif->name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
         * Don't forget to free the sub-interfaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        if (currif->childs != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
          freeif(currif->childs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        ifs = currif->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        free(currif);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        currif = ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
 * Add an interface to the list. If known interface just link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
 * a new netaddr onto the list. If new interface create new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
 * netif structure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
netif *addif(JNIEnv *env, netif *ifs, char *if_name, int index, int family,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
             struct sockaddr *new_addrP, int new_addrlen, short prefix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
  netif *currif = ifs, *parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
    netaddr *addrP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
#ifdef LIFNAMSIZ
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    char name[LIFNAMSIZ];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
    char vname[LIFNAMSIZ];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
    char name[IFNAMSIZ];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
    char vname[IFNAMSIZ];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
    char *unit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
    int isVirtual = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     * If the interface name is a logical interface then we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     * remove the unit number so that we have the physical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     * interface (eg: hme0:1 -> hme0). NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     * currently doesn't have any concept of physical vs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     * logical interfaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    strcpy(name, if_name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     * Create and populate the netaddr node. If allocation fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * return an un-updated list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
    addrP = (netaddr *)malloc(sizeof(netaddr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
    if (addrP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        addrP->addr = (struct sockaddr *)malloc(new_addrlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        if (addrP->addr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            free(addrP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            addrP = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
    if (addrP == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        JNU_ThrowOutOfMemoryError(env, "heap allocation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        return ifs; /* return untouched list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
    memcpy(addrP->addr, new_addrP, new_addrlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
    addrP->family = family;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
    addrP->brdcast = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
    addrP->mask = prefix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
    if (family == AF_INET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
      /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
       * Deal with brodcast addr & subnet mask
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
      addrP->brdcast = getBroadcast(env, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
      if (addrP->brdcast) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        addrP->mask = getSubnet(env, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
    vname[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
    unit = strchr(name, ':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
    if (unit != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
      /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
       * This is a virtual interface. If we are able to access the parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
       * we need to create a new entry if it doesn't exist yet *and* update
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
       * the 'parent' interface with the new records.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
      struct ifreq if2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
      int sock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
      int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
      sock = JVM_Socket(AF_INET, SOCK_DGRAM, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
      if (sock < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                                     "Socket creation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        return ifs; /* return untouched list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
      len = unit - name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
      if (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        // temporarily use vname to hold the parent name of the interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        // instead of creating another buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        memcpy(&vname, name, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        vname[len] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        memset((char *) &if2, 0, sizeof(if2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        strcpy(if2.ifr_name, vname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        if (ioctl(sock, SIOCGIFFLAGS, (char *)&if2) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
           // Got access to parent, so create it if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
           strcpy(vname, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
           *unit = '\0';
2058
577525f89bd4 6800805: java.net.NetworkInterface.getNetworkInterfaces() does not list IPv6 network interfaces correctly
chegar
parents: 1097
diff changeset
   972
        } else {
577525f89bd4 6800805: java.net.NetworkInterface.getNetworkInterfaces() does not list IPv6 network interfaces correctly
chegar
parents: 1097
diff changeset
   973
#if defined(__solaris__) && defined(AF_INET6)
577525f89bd4 6800805: java.net.NetworkInterface.getNetworkInterfaces() does not list IPv6 network interfaces correctly
chegar
parents: 1097
diff changeset
   974
          struct   lifreq lifr;
577525f89bd4 6800805: java.net.NetworkInterface.getNetworkInterfaces() does not list IPv6 network interfaces correctly
chegar
parents: 1097
diff changeset
   975
          memset((char *) &lifr, 0, sizeof(lifr));
577525f89bd4 6800805: java.net.NetworkInterface.getNetworkInterfaces() does not list IPv6 network interfaces correctly
chegar
parents: 1097
diff changeset
   976
          strcpy(lifr.lifr_name, vname);
577525f89bd4 6800805: java.net.NetworkInterface.getNetworkInterfaces() does not list IPv6 network interfaces correctly
chegar
parents: 1097
diff changeset
   977
577525f89bd4 6800805: java.net.NetworkInterface.getNetworkInterfaces() does not list IPv6 network interfaces correctly
chegar
parents: 1097
diff changeset
   978
          /* Try with an IPv6 socket in case the interface has only IPv6
577525f89bd4 6800805: java.net.NetworkInterface.getNetworkInterfaces() does not list IPv6 network interfaces correctly
chegar
parents: 1097
diff changeset
   979
           * addresses assigned to it */
577525f89bd4 6800805: java.net.NetworkInterface.getNetworkInterfaces() does not list IPv6 network interfaces correctly
chegar
parents: 1097
diff changeset
   980
          close(sock);
577525f89bd4 6800805: java.net.NetworkInterface.getNetworkInterfaces() does not list IPv6 network interfaces correctly
chegar
parents: 1097
diff changeset
   981
          sock = JVM_Socket(AF_INET6, SOCK_DGRAM, 0);
577525f89bd4 6800805: java.net.NetworkInterface.getNetworkInterfaces() does not list IPv6 network interfaces correctly
chegar
parents: 1097
diff changeset
   982
577525f89bd4 6800805: java.net.NetworkInterface.getNetworkInterfaces() does not list IPv6 network interfaces correctly
chegar
parents: 1097
diff changeset
   983
          if (sock < 0) {
577525f89bd4 6800805: java.net.NetworkInterface.getNetworkInterfaces() does not list IPv6 network interfaces correctly
chegar
parents: 1097
diff changeset
   984
            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
577525f89bd4 6800805: java.net.NetworkInterface.getNetworkInterfaces() does not list IPv6 network interfaces correctly
chegar
parents: 1097
diff changeset
   985
                                         "Socket creation failed");
577525f89bd4 6800805: java.net.NetworkInterface.getNetworkInterfaces() does not list IPv6 network interfaces correctly
chegar
parents: 1097
diff changeset
   986
            return ifs; /* return untouched list */
577525f89bd4 6800805: java.net.NetworkInterface.getNetworkInterfaces() does not list IPv6 network interfaces correctly
chegar
parents: 1097
diff changeset
   987
          }
577525f89bd4 6800805: java.net.NetworkInterface.getNetworkInterfaces() does not list IPv6 network interfaces correctly
chegar
parents: 1097
diff changeset
   988
577525f89bd4 6800805: java.net.NetworkInterface.getNetworkInterfaces() does not list IPv6 network interfaces correctly
chegar
parents: 1097
diff changeset
   989
          if (ioctl(sock, SIOCGLIFFLAGS, (char *)&lifr) >= 0) {
577525f89bd4 6800805: java.net.NetworkInterface.getNetworkInterfaces() does not list IPv6 network interfaces correctly
chegar
parents: 1097
diff changeset
   990
            // Got access to parent, so create it if necessary.
577525f89bd4 6800805: java.net.NetworkInterface.getNetworkInterfaces() does not list IPv6 network interfaces correctly
chegar
parents: 1097
diff changeset
   991
            strcpy(vname, name);
577525f89bd4 6800805: java.net.NetworkInterface.getNetworkInterfaces() does not list IPv6 network interfaces correctly
chegar
parents: 1097
diff changeset
   992
            *unit = '\0';
577525f89bd4 6800805: java.net.NetworkInterface.getNetworkInterfaces() does not list IPv6 network interfaces correctly
chegar
parents: 1097
diff changeset
   993
          } else {
577525f89bd4 6800805: java.net.NetworkInterface.getNetworkInterfaces() does not list IPv6 network interfaces correctly
chegar
parents: 1097
diff changeset
   994
            // failed to access parent interface do not create parent.
577525f89bd4 6800805: java.net.NetworkInterface.getNetworkInterfaces() does not list IPv6 network interfaces correctly
chegar
parents: 1097
diff changeset
   995
            // We are a virtual interface with no parent.
577525f89bd4 6800805: java.net.NetworkInterface.getNetworkInterfaces() does not list IPv6 network interfaces correctly
chegar
parents: 1097
diff changeset
   996
            isVirtual = 1;
577525f89bd4 6800805: java.net.NetworkInterface.getNetworkInterfaces() does not list IPv6 network interfaces correctly
chegar
parents: 1097
diff changeset
   997
            vname[0] = 0;
577525f89bd4 6800805: java.net.NetworkInterface.getNetworkInterfaces() does not list IPv6 network interfaces correctly
chegar
parents: 1097
diff changeset
   998
          }
577525f89bd4 6800805: java.net.NetworkInterface.getNetworkInterfaces() does not list IPv6 network interfaces correctly
chegar
parents: 1097
diff changeset
   999
#else
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
          // failed to access parent interface do not create parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
          // We are a virtual interface with no parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
          isVirtual = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
          vname[0] = 0;
2058
577525f89bd4 6800805: java.net.NetworkInterface.getNetworkInterfaces() does not list IPv6 network interfaces correctly
chegar
parents: 1097
diff changeset
  1004
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
      close(sock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     * Check if this is a "new" interface. Use the interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     * name for matching because index isn't supported on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     * Solaris 2.6 & 7.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
    while (currif != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        if (strcmp(name, currif->name) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        currif = currif->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     * If "new" then create an netif structure and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     * insert it onto the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
    if (currif == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        currif = (netif *)malloc(sizeof(netif));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        if (currif) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
            currif->name = strdup(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
            if (currif->name == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
                free(currif);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
                currif = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        if (currif == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            JNU_ThrowOutOfMemoryError(env, "heap allocation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
            return ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
        currif->index = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
        currif->addr = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
        currif->childs = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        currif->virtual = isVirtual;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
        currif->next = ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
        ifs = currif;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     * Finally insert the address on the interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
    addrP->next = currif->addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
    currif->addr = addrP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
    parent = currif;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * Let's deal with the virtual interface now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
    if (vname[0]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
      netaddr *tmpaddr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
      currif = parent->childs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
      while (currif != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        if (strcmp(vname, currif->name) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
          break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        currif = currif->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
      if (currif == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
        currif = (netif *)malloc(sizeof(netif));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        if (currif) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
          currif->name = strdup(vname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
          if (currif->name == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
            free(currif);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
            currif = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
        if (currif == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
          JNU_ThrowOutOfMemoryError(env, "heap allocation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
          return ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        currif->index = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        currif->addr = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        /* Need to duplicate the addr entry? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        currif->virtual = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        currif->childs = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        currif->next = parent->childs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        parent->childs = currif;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
      tmpaddr = (netaddr *) malloc(sizeof(netaddr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
      if (tmpaddr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        JNU_ThrowOutOfMemoryError(env, "heap allocation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
        return ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
      memcpy(tmpaddr, addrP, sizeof(netaddr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
      /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
       * Let's duplicate the address and broadcast address structures
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
       * if there are any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
      if (addrP->addr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        tmpaddr->addr = malloc(new_addrlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
        if (tmpaddr->addr != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
          memcpy(tmpaddr->addr, addrP->addr, new_addrlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
      if (addrP->brdcast != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        tmpaddr->brdcast = malloc(new_addrlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        if (tmpaddr->brdcast != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
          memcpy(tmpaddr->brdcast, addrP->brdcast, new_addrlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
      tmpaddr->next = currif->addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
      currif->addr = tmpaddr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
    return ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
 * Get flags from a NetworkInterface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
static short getFlags(JNIEnv *env, jstring name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
  int sock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
  struct ifreq if2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
  jboolean isCopy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
  const char* name_utf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
  short ret = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
  sock = JVM_Socket(AF_INET, SOCK_DGRAM, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
  if (sock < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
    NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
                                 "Socket creation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
  name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
  memset((char *) &if2, 0, sizeof(if2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
  strcpy(if2.ifr_name, name_utf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
  if (ioctl(sock, SIOCGIFFLAGS, (char *)&if2) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
    ret = if2.ifr_flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
  } else {
709
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1142
#if defined(__solaris__) && defined(AF_INET6)
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1143
    /* 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
  1144
    struct lifreq lifr;
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1145
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1146
    close(sock);
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1147
    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
  1148
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1149
    if (sock < 0) {
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1150
      (*env)->ReleaseStringUTFChars(env, name, name_utf);
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1151
      NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1152
                                  "Socket creation failed");
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1153
      return -1;
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1154
    }
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1155
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1156
    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
  1157
    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
  1158
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1159
    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
  1160
      ret = lifr.lifr_flags;
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1161
    } else {
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1162
      NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1163
                               "IOCTL failed");
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1164
    }
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1165
#else
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
    NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
                                 "IOCTL failed");
709
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1168
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
  close(sock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
  /* release the UTF string and interface list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
  (*env)->ReleaseStringUTFChars(env, name, name_utf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
  return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
 * Returns the IPv4 broadcast address of a named interface, if it exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
 * Returns 0 if it doesn't have one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
static struct sockaddr *getBroadcast(JNIEnv *env, const char *ifname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
  int sock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
  struct sockaddr *ret = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
  struct ifreq if2;
910
1f53246fb014 6729881: Compiler warning in networking native code
chegar
parents: 715
diff changeset
  1185
  short flag = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
  sock = JVM_Socket(AF_INET, SOCK_DGRAM, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
  if (sock < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
    NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
                                 "Socket creation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
  memset((char *) &if2, 0, sizeof(if2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
  strcpy(if2.ifr_name, ifname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
  /* Let's make sure the interface does have a broadcast address */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
  if (ioctl(sock, SIOCGIFFLAGS, (char *)&if2) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
    flag = if2.ifr_flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
  } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
    NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
                                 "IOCTL failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
  if (flag & IFF_BROADCAST) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
    /* It does, let's retrieve it*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
    if (ioctl(sock, SIOCGIFBRDADDR, (char *)&if2) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
      ret = (struct sockaddr*) malloc(sizeof(struct sockaddr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
      memcpy(ret, &if2.ifr_broadaddr, sizeof(struct sockaddr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
      NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
                                   "IOCTL failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
  close(sock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
  return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
 * Returns the IPv4 subnet prefix length (aka subnet mask) for the named
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
 * interface, if it has one, otherwise return -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
static short getSubnet(JNIEnv *env, const char *ifname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
  int sock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
  unsigned int mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
  short ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
  struct ifreq if2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
  sock = JVM_Socket(AF_INET, SOCK_DGRAM, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
  if (sock < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
    NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
                                 "Socket creation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
  memset((char *) &if2, 0, sizeof(if2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
  strcpy(if2.ifr_name, ifname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
  if (ioctl(sock, SIOCGIFNETMASK, (char *)&if2) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
    mask = ntohl(((struct sockaddr_in*)&(if2.ifr_addr))->sin_addr.s_addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
    ret = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
    while (mask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
      mask <<= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
      ret++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
    close(sock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
  NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
                               "IOCTL failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
  close(sock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
  return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
#define DEV_PREFIX      "/dev/"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
 * Solaris specific DLPI code to get hardware address from a device.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
 * Unfortunately, at least up to Solaris X, you have to have special
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
 * privileges (i.e. be root).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
static int getMacFromDevice(JNIEnv *env, const char* ifname, unsigned char* retbuf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
  char style1dev[MAXPATHLEN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
  int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
  dl_phys_addr_req_t dlpareq;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
  dl_phys_addr_ack_t *dlpaack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
  struct strbuf msg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
  char buf[128];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
  int flags = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
  /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
   * Device is in /dev
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
   * e.g.: /dev/bge0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
  strcpy(style1dev, DEV_PREFIX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
  strcat(style1dev, ifname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
  if ((fd = open(style1dev, O_RDWR)) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
     * Can't open it. We probably are missing the privilege.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
     * We'll have to try something else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
  dlpareq.dl_primitive = DL_PHYS_ADDR_REQ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
  dlpareq.dl_addr_type = DL_CURR_PHYS_ADDR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
  msg.buf = (char *)&dlpareq;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
  msg.len = DL_PHYS_ADDR_REQ_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
  if (putmsg(fd, &msg, NULL, 0) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
    NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
                                 "putmsg failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
  dlpaack = (dl_phys_addr_ack_t *)buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
  msg.buf = (char *)buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
  msg.len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
  msg.maxlen = sizeof (buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
  if (getmsg(fd, &msg, NULL, &flags) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
    NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
                                 "getmsg failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
  if (msg.len < DL_PHYS_ADDR_ACK_SIZE ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
      dlpaack->dl_primitive != DL_PHYS_ADDR_ACK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
    JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
                    "Couldn't obtain phys addr\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
  memcpy(retbuf, &buf[dlpaack->dl_addr_offset], dlpaack->dl_addr_length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
  return dlpaack->dl_addr_length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
 * Get the Hardware address (usually MAC address) for the named interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
 * return puts the data in buf, and returns the length, in byte, of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
 * MAC address. Returns -1 if there is no hardware address on that interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
int getMacAddress(JNIEnv *env, const struct in_addr* addr, const char* ifname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
                  unsigned char *buf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
  int sock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
  static struct ifreq ifr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
  int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
  sock = JVM_Socket(AF_INET, SOCK_DGRAM, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
  if (sock < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
    NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
                                 "Socket creation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
  strcpy(ifr.ifr_name, ifname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
  if (ioctl(sock, SIOCGIFHWADDR, &ifr) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
    fprintf(stderr, "SIOCIFHWADDR: %s\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
            strerror(errno));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
    close(sock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
  memcpy(buf, &ifr.ifr_hwaddr.sa_data, IFHWADDRLEN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
  close(sock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
  for (i = 0; i < IFHWADDRLEN; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
    if (buf[i] != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
      return IFHWADDRLEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
  /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
   * All bytes to 0 means no hardware address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
  return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
  struct arpreq arpreq;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
  struct sockaddr_in* sin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
  struct sockaddr_in ipAddr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
  int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
  /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
   * On Solaris we have to use DLPI, but it will only work if we have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
   * privileged access (i.e. root). If that fails, we try a lookup
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
   * in the ARP table, which requires an IPv4 address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
  if ((len = getMacFromDevice(env, ifname, buf)) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
    return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
  if (addr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
     * No IPv4 address for that interface, so can't do an ARP lookup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
  sin = (struct sockaddr_in *) &arpreq.arp_pa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
  memset((char *) &arpreq, 0, sizeof(struct arpreq));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
  ipAddr.sin_port = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
  ipAddr.sin_family = AF_INET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
  memcpy(&ipAddr.sin_addr, addr, sizeof(struct in_addr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
  memcpy(&arpreq.arp_pa, &ipAddr, sizeof(struct sockaddr_in));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
  arpreq.arp_flags= ATF_PUBL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
  sock = JVM_Socket(AF_INET, SOCK_DGRAM, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
  if (sock < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
    NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
                                 "Socket creation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
  if (ioctl(sock, SIOCGARP, &arpreq) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
    close(sock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
    memcpy(buf, &arpreq.arp_ha.sa_data[0], 6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
    return 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
  if (errno != ENXIO) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
    // "No such device or address" means no hardware address, so it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
    // normal don't throw an exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
    NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
                                 "IOCTL failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
  close(sock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
  return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
 * Method:    isUp0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
 * Signature: (Ljava/lang/String;I)Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isUp0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
    (JNIEnv *env, jclass cls, jstring name, jint index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
    short val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
    val = getFlags(env, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
    if ( (val & IFF_UP) && (val &  IFF_RUNNING))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
      return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
    return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
 * Method:    isP2P0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
 * Signature: (Ljava/lang/String;I)Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isP2P0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
    (JNIEnv *env, jclass cls, jstring name, jint index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
    if (getFlags(env, name) & IFF_POINTOPOINT)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
      return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
    return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
 * Method:    isLoopback0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
 * Signature: (Ljava/lang/String;I)Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isLoopback0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
    (JNIEnv *env, jclass cls, jstring name, jint index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
    if (getFlags(env, name) & IFF_LOOPBACK)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
      return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
    return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
 * Method:    supportsMulticast0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
 * Signature: (Ljava/lang/String;I)Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_supportsMulticast0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
(JNIEnv *env, jclass cls, jstring name, jint index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
  short val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
  val = getFlags(env, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
  if (val & IFF_MULTICAST)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
    return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
  return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
 * Method:    getMacAddr0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
 * Signature: ([bLjava/lang/String;I)[b
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
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
  1462
  jint addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
  jbyte caddr[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
  struct in_addr iaddr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
  jbyteArray ret = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
  unsigned char mac[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
  int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
  jboolean isCopy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
  const char* name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
  if (!IS_NULL(addrArray)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
    (*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
    addr = ((caddr[0]<<24) & 0xff000000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
    addr |= ((caddr[1] <<16) & 0xff0000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
    addr |= ((caddr[2] <<8) & 0xff00);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
    addr |= (caddr[3] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
    iaddr.s_addr = htonl(addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
    len = getMacAddress(env, &iaddr, name_utf, mac);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
  } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
    len = getMacAddress(env, NULL, name_utf, mac);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
  if (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
    ret = (*env)->NewByteArray(env, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
    if (IS_NULL(ret)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
      /* we may have memory to free at the end of this */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
      goto fexit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
    (*env)->SetByteArrayRegion(env, ret, 0, len, (jbyte *) (mac));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
 fexit:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
  /* release the UTF string and interface list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
  (*env)->ReleaseStringUTFChars(env, name, name_utf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
  return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
 * Class:       java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
 * Method:      getMTU0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
 * Signature:   ([bLjava/lang/String;I)I
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
JNIEXPORT jint JNICALL Java_java_net_NetworkInterface_getMTU0(JNIEnv *env, jclass class, jstring name, jint index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
  jboolean isCopy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
  int sock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
  struct ifreq if2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
  int ret = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
  const char* name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
  sock = JVM_Socket(AF_INET, SOCK_DGRAM, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
  if (sock < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
    NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
                                 "Socket creation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
  } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
    memset((char *) &if2, 0, sizeof(if2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
    strcpy(if2.ifr_name, name_utf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
    if (ioctl(sock, SIOCGIFMTU, (char *)&if2) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
      ret= if2.ifr_mtu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
      NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
                                   "IOCTL failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
#else /* Solaris */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
    struct   lifreq lifr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
    memset((caddr_t)&lifr, 0, sizeof(lifr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
    strcpy((caddr_t)&(lifr.lifr_name), name_utf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
    if (ioctl(sock, SIOCGLIFMTU, (caddr_t)&lifr) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
      ret = lifr.lifr_mtu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
    } else {
709
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1532
      /* 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
  1533
      close(sock);
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1534
      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
  1535
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1536
      if (sock < 0) {
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1537
        (*env)->ReleaseStringUTFChars(env, name, name_utf);
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1538
        NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1539
                                     "Socket creation failed");
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1540
        return -1;
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1541
      }
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1542
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1543
      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
  1544
        ret = lifr.lifr_mtu;
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1545
      } else {
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1546
        NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1547
                                     "IOCTL failed");
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1548
      }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
    close(sock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
  /* release the UTF string and interface list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
  (*env)->ReleaseStringUTFChars(env, name, name_utf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
  return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
}