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