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