jdk/src/java.base/unix/native/libnet/NetworkInterface.c
author msheppar
Fri, 12 Sep 2014 16:11:40 +0100
changeset 28549 a80ec9ad6324
parent 25859 3317bb8137f4
child 37898 ac36c71a44f9
permissions -rw-r--r--
8056264: Multicast support improvements Summary: avoid passing a null ifname string to GetStringUTFChars native fn calls within a NetworkInterface method call flows Reviewed-by: chegar, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
28549
a80ec9ad6324 8056264: Multicast support improvements
msheppar
parents: 25859
diff changeset
     2
 * Copyright (c) 2000, 2014, 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>
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
    29
#if defined(_ALLBSD_SOURCE) && defined(__OpenBSD__)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
    30
#include <sys/types.h>
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
    31
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include <netinet/in.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include <sys/types.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include <sys/socket.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include <arpa/inet.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#include <net/if.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
#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
    40
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
#include <sys/dlpi.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
#include <fcntl.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
#include <stropts.h>
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
    45
#include <sys/sockio.h>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
#endif
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
    47
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
#include <sys/ioctl.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
#include <bits/ioctls.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
#include <sys/utsname.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
#include <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
    55
#if defined(_AIX)
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
    56
#include <sys/ioctl.h>
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
    57
#include <netinet/in6_var.h>
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
    58
#include <sys/ndd_var.h>
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
    59
#include <sys/kinfo.h>
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
    60
#endif
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
    61
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
#define _PATH_PROCNET_IFINET6           "/proc/net/if_inet6"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
    66
#if defined(_ALLBSD_SOURCE)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
    67
#include <sys/param.h>
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
    68
#include <sys/ioctl.h>
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
    69
#include <sys/sockio.h>
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
    70
#if defined(__APPLE__)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
    71
#include <net/ethernet.h>
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
    72
#include <net/if_var.h>
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
    73
#include <net/if_dl.h>
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
    74
#include <netinet/in_var.h>
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
    75
#include <ifaddrs.h>
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
    76
#endif
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
    77
#endif
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
    78
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
#include "jvm.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
#include "net_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
typedef struct _netaddr  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    struct sockaddr *addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    struct sockaddr *brdcast;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    short mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    int family; /* to make searches simple */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    struct _netaddr *next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
} netaddr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
typedef struct _netif {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    char *name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    char virtual;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    netaddr *addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    struct _netif *childs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    struct _netif *next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
} netif;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
/************************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
#include "java_net_NetworkInterface.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
/************************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
jclass ni_class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
jfieldID ni_nameID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
jfieldID ni_indexID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
jfieldID ni_descID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
jfieldID ni_addrsID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
jfieldID ni_bindsID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
jfieldID ni_virutalID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
jfieldID ni_childsID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
jfieldID ni_parentID;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
   118
jfieldID ni_defaultIndexID;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
jmethodID ni_ctrID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
static jclass ni_ibcls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
static jmethodID ni_ibctrID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
static jfieldID ni_ibaddressID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
static jfieldID ni_ib4broadcastID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
static jfieldID ni_ib4maskID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   127
/** Private methods declarations **/
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
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
   129
static int     getFlags0(JNIEnv *env, jstring  ifname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   131
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
   132
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
   133
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
#ifdef AF_INET6
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   135
static netif  *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   138
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
   139
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
   140
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   141
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
   142
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
   143
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   144
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   145
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
   146
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
   147
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
   148
13564
3571e232ab03 7190254: NetworkInterface getFlags implementation should support full integer bit range for flags value
chegar
parents: 13245
diff changeset
   149
static int     getFlags(int sock, const char *ifname, int *flags);
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   150
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
   151
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
   152
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   153
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   154
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   155
#ifdef __solaris__
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   156
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
   157
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
   158
8c19a790b7c1 7079012: test/java/net/NetworkInterface/NetParamsTest.java fails with SocketException getting mac address
michaelm
parents: 9035
diff changeset
   159
#ifndef SIOCGLIFHWADDR
8c19a790b7c1 7079012: test/java/net/NetworkInterface/NetParamsTest.java fails with SocketException getting mac address
michaelm
parents: 9035
diff changeset
   160
#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
   161
#endif
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   162
10605
8c19a790b7c1 7079012: test/java/net/NetworkInterface/NetParamsTest.java fails with SocketException getting mac address
michaelm
parents: 9035
diff changeset
   163
#endif
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   164
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   165
/******************* Java entry points *****************************/
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 * Method:    init
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 * Signature: ()V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
Java_java_net_NetworkInterface_init(JNIEnv *env, jclass cls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    ni_class = (*env)->FindClass(env,"java/net/NetworkInterface");
21409
246ccfb4a0f0 8026880: NetworkInterface native initializing code should check fieldID values
chegar
parents: 20848
diff changeset
   175
    CHECK_NULL(ni_class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    ni_class = (*env)->NewGlobalRef(env, ni_class);
21409
246ccfb4a0f0 8026880: NetworkInterface native initializing code should check fieldID values
chegar
parents: 20848
diff changeset
   177
    CHECK_NULL(ni_class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    ni_nameID = (*env)->GetFieldID(env, ni_class,"name", "Ljava/lang/String;");
21409
246ccfb4a0f0 8026880: NetworkInterface native initializing code should check fieldID values
chegar
parents: 20848
diff changeset
   179
    CHECK_NULL(ni_nameID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    ni_indexID = (*env)->GetFieldID(env, ni_class, "index", "I");
21409
246ccfb4a0f0 8026880: NetworkInterface native initializing code should check fieldID values
chegar
parents: 20848
diff changeset
   181
    CHECK_NULL(ni_indexID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    ni_addrsID = (*env)->GetFieldID(env, ni_class, "addrs", "[Ljava/net/InetAddress;");
21409
246ccfb4a0f0 8026880: NetworkInterface native initializing code should check fieldID values
chegar
parents: 20848
diff changeset
   183
    CHECK_NULL(ni_addrsID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    ni_bindsID = (*env)->GetFieldID(env, ni_class, "bindings", "[Ljava/net/InterfaceAddress;");
21409
246ccfb4a0f0 8026880: NetworkInterface native initializing code should check fieldID values
chegar
parents: 20848
diff changeset
   185
    CHECK_NULL(ni_bindsID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    ni_descID = (*env)->GetFieldID(env, ni_class, "displayName", "Ljava/lang/String;");
21409
246ccfb4a0f0 8026880: NetworkInterface native initializing code should check fieldID values
chegar
parents: 20848
diff changeset
   187
    CHECK_NULL(ni_descID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    ni_virutalID = (*env)->GetFieldID(env, ni_class, "virtual", "Z");
21409
246ccfb4a0f0 8026880: NetworkInterface native initializing code should check fieldID values
chegar
parents: 20848
diff changeset
   189
    CHECK_NULL(ni_virutalID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    ni_childsID = (*env)->GetFieldID(env, ni_class, "childs", "[Ljava/net/NetworkInterface;");
21409
246ccfb4a0f0 8026880: NetworkInterface native initializing code should check fieldID values
chegar
parents: 20848
diff changeset
   191
    CHECK_NULL(ni_childsID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    ni_parentID = (*env)->GetFieldID(env, ni_class, "parent", "Ljava/net/NetworkInterface;");
21409
246ccfb4a0f0 8026880: NetworkInterface native initializing code should check fieldID values
chegar
parents: 20848
diff changeset
   193
    CHECK_NULL(ni_parentID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    ni_ctrID = (*env)->GetMethodID(env, ni_class, "<init>", "()V");
21409
246ccfb4a0f0 8026880: NetworkInterface native initializing code should check fieldID values
chegar
parents: 20848
diff changeset
   195
    CHECK_NULL(ni_ctrID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    ni_ibcls = (*env)->FindClass(env, "java/net/InterfaceAddress");
21409
246ccfb4a0f0 8026880: NetworkInterface native initializing code should check fieldID values
chegar
parents: 20848
diff changeset
   197
    CHECK_NULL(ni_ibcls);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    ni_ibcls = (*env)->NewGlobalRef(env, ni_ibcls);
21409
246ccfb4a0f0 8026880: NetworkInterface native initializing code should check fieldID values
chegar
parents: 20848
diff changeset
   199
    CHECK_NULL(ni_ibcls);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    ni_ibctrID = (*env)->GetMethodID(env, ni_ibcls, "<init>", "()V");
21409
246ccfb4a0f0 8026880: NetworkInterface native initializing code should check fieldID values
chegar
parents: 20848
diff changeset
   201
    CHECK_NULL(ni_ibctrID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    ni_ibaddressID = (*env)->GetFieldID(env, ni_ibcls, "address", "Ljava/net/InetAddress;");
21409
246ccfb4a0f0 8026880: NetworkInterface native initializing code should check fieldID values
chegar
parents: 20848
diff changeset
   203
    CHECK_NULL(ni_ibaddressID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    ni_ib4broadcastID = (*env)->GetFieldID(env, ni_ibcls, "broadcast", "Ljava/net/Inet4Address;");
21409
246ccfb4a0f0 8026880: NetworkInterface native initializing code should check fieldID values
chegar
parents: 20848
diff changeset
   205
    CHECK_NULL(ni_ib4broadcastID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    ni_ib4maskID = (*env)->GetFieldID(env, ni_ibcls, "maskLength", "S");
21409
246ccfb4a0f0 8026880: NetworkInterface native initializing code should check fieldID values
chegar
parents: 20848
diff changeset
   207
    CHECK_NULL(ni_ib4maskID);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
   208
    ni_defaultIndexID = (*env)->GetStaticFieldID(env, ni_class, "defaultIndex", "I");
22646
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 22605
diff changeset
   209
    CHECK_NULL(ni_defaultIndexID);
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 22605
diff changeset
   210
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 22605
diff changeset
   211
    initInetAddressIDs(env);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
 * Method:    getByName0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
 * Signature: (Ljava/lang/String;)Ljava/net/NetworkInterface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByName0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    (JNIEnv *env, jclass cls, jstring name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    netif *ifs, *curr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    jboolean isCopy;
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   225
    const char* name_utf;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    jobject obj = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    ifs = enumInterfaces(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    if (ifs == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   233
    name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
23563
71ed058849d4 8025293: JNI exception pending checks in java.net
msheppar
parents: 23015
diff changeset
   234
    if (name_utf == NULL) {
71ed058849d4 8025293: JNI exception pending checks in java.net
msheppar
parents: 23015
diff changeset
   235
       if (!(*env)->ExceptionCheck(env))
71ed058849d4 8025293: JNI exception pending checks in java.net
msheppar
parents: 23015
diff changeset
   236
           JNU_ThrowOutOfMemoryError(env, NULL);
71ed058849d4 8025293: JNI exception pending checks in java.net
msheppar
parents: 23015
diff changeset
   237
       return NULL;
71ed058849d4 8025293: JNI exception pending checks in java.net
msheppar
parents: 23015
diff changeset
   238
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * Search the list of interface based on name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    curr = ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    while (curr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        if (strcmp(name_utf, curr->name) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        curr = curr->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    /* if found create a NetworkInterface */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    if (curr != NULL) {;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        obj = createNetworkInterface(env, curr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    /* release the UTF string and interface list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    (*env)->ReleaseStringUTFChars(env, name, name_utf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    freeif(ifs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    return obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
 * Class:     java_net_NetworkInterface
1097
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 910
diff changeset
   265
 * Method:    getByIndex0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
 * Signature: (Ljava/lang/String;)Ljava/net/NetworkInterface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
 */
1097
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 910
diff changeset
   268
JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByIndex0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    (JNIEnv *env, jclass cls, jint index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    netif *ifs, *curr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    jobject obj = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    if (index <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    ifs = enumInterfaces(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    if (ifs == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * Search the list of interface based on index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    curr = ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    while (curr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        if (index == curr->index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        curr = curr->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    /* if found create a NetworkInterface */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    if (curr != NULL) {;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        obj = createNetworkInterface(env, curr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    freeif(ifs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    return obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
 * Method:    getByInetAddress0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
 * Signature: (Ljava/net/InetAddress;)Ljava/net/NetworkInterface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByInetAddress0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    (JNIEnv *env, jclass cls, jobject iaObj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    netif *ifs, *curr;
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   311
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 2163
diff changeset
   312
#ifdef AF_INET6
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   313
    int family = (getInetAddress_family(env, iaObj) == IPv4) ? AF_INET : AF_INET6;
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 2163
diff changeset
   314
#else
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   315
    int family =  AF_INET;
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 2163
diff changeset
   316
#endif
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   317
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    jobject obj = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    jboolean match = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    ifs = enumInterfaces(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    if (ifs == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    curr = ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    while (curr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        netaddr *addrP = curr->addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
         * Iterate through each address on the interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        while (addrP != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            if (family == addrP->family) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                if (family == AF_INET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    int address1 = htonl(((struct sockaddr_in*)addrP->addr)->sin_addr.s_addr);
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   338
                    int address2 = getInetAddress_addr(env, iaObj);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    if (address1 == address2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                        match = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                if (family == AF_INET6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    jbyte *bytes = (jbyte *)&(((struct sockaddr_in6*)addrP->addr)->sin6_addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    jbyte caddr[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    int i;
20821
e0d0a585aa49 8015743: Address internet addresses
michaelm
parents: 17921
diff changeset
   351
                    getInet6Address_ipaddress(env, iaObj, (char *)caddr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                    while (i < 16) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                        if (caddr[i] != bytes[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                        i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                    if (i >= 16) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                        match = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            if (match) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            addrP = addrP->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        if (match) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        curr = curr->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    /* if found create a NetworkInterface */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    if (match) {;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        obj = createNetworkInterface(env, curr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    freeif(ifs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    return obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
 * Method:    getAll
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
 * Signature: ()[Ljava/net/NetworkInterface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
JNIEXPORT jobjectArray JNICALL Java_java_net_NetworkInterface_getAll
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    (JNIEnv *env, jclass cls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    netif *ifs, *curr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    jobjectArray netIFArr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    jint arr_index, ifCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    ifs = enumInterfaces(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    if (ifs == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    /* count the interface */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    ifCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    curr = ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    while (curr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        ifCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        curr = curr->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    /* allocate a NetworkInterface array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    netIFArr = (*env)->NewObjectArray(env, ifCount, cls, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    if (netIFArr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        freeif(ifs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * Iterate through the interfaces, create a NetworkInterface instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * for each array element and populate the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    curr = ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    arr_index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    while (curr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        jobject netifObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        netifObj = createNetworkInterface(env, curr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        if (netifObj == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            freeif(ifs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        /* put the NetworkInterface into the array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        (*env)->SetObjectArrayElement(env, netIFArr, arr_index++, netifObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        curr = curr->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    freeif(ifs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    return netIFArr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   447
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   448
/*
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   449
 * Class:     java_net_NetworkInterface
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   450
 * Method:    isUp0
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   451
 * 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
   452
 */
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   453
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
   454
    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
   455
    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
   456
}
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   457
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   458
/*
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   459
 * Class:     java_net_NetworkInterface
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   460
 * Method:    isP2P0
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   461
 * 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
   462
 */
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   463
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
   464
    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
   465
    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
   466
}
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   467
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   468
/*
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   469
 * Class:     java_net_NetworkInterface
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   470
 * Method:    isLoopback0
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   471
 * 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
   472
 */
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   473
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
   474
    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
   475
    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
   476
}
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
/*
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   479
 * Class:     java_net_NetworkInterface
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   480
 * Method:    supportsMulticast0
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   481
 * 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
   482
 */
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   483
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
   484
    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
   485
    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
   486
}
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   487
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   488
/*
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   489
 * Class:     java_net_NetworkInterface
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   490
 * Method:    getMacAddr0
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   491
 * 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
   492
 */
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   493
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
   494
    jint addr;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   495
    jbyte caddr[4];
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   496
    struct in_addr iaddr;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   497
    jbyteArray ret = NULL;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   498
    unsigned char mac[16];
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   499
    int len;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   500
    int sock;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   501
    jboolean isCopy;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   502
    const char* name_utf;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   503
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   504
    name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
23563
71ed058849d4 8025293: JNI exception pending checks in java.net
msheppar
parents: 23015
diff changeset
   505
    if (name_utf == NULL) {
71ed058849d4 8025293: JNI exception pending checks in java.net
msheppar
parents: 23015
diff changeset
   506
       if (!(*env)->ExceptionCheck(env))
71ed058849d4 8025293: JNI exception pending checks in java.net
msheppar
parents: 23015
diff changeset
   507
           JNU_ThrowOutOfMemoryError(env, NULL);
71ed058849d4 8025293: JNI exception pending checks in java.net
msheppar
parents: 23015
diff changeset
   508
       return NULL;
71ed058849d4 8025293: JNI exception pending checks in java.net
msheppar
parents: 23015
diff changeset
   509
    }
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   510
    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
   511
       (*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
   512
       return JNI_FALSE;
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
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   515
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   516
    if (!IS_NULL(addrArray)) {
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   517
       (*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
   518
       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
   519
       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
   520
       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
   521
       addr |= (caddr[3] & 0xff);
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   522
       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
   523
       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
   524
    } else {
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   525
       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
   526
    }
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   527
    if (len > 0) {
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   528
       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
   529
       if (IS_NULL(ret)) {
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   530
          /* 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
   531
          goto fexit;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   532
       }
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   533
       (*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
   534
    }
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   535
 fexit:
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   536
   /* 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
   537
   (*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
   538
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   539
   close(sock);
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   540
   return ret;
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
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   543
/*
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   544
 * Class:       java_net_NetworkInterface
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   545
 * Method:      getMTU0
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   546
 * 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
   547
 */
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
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
   550
    jboolean isCopy;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   551
    int ret = -1;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   552
    int sock;
28549
a80ec9ad6324 8056264: Multicast support improvements
msheppar
parents: 25859
diff changeset
   553
    const char* name_utf = NULL;
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   554
28549
a80ec9ad6324 8056264: Multicast support improvements
msheppar
parents: 25859
diff changeset
   555
    if (name != NULL) {
a80ec9ad6324 8056264: Multicast support improvements
msheppar
parents: 25859
diff changeset
   556
        name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
a80ec9ad6324 8056264: Multicast support improvements
msheppar
parents: 25859
diff changeset
   557
    } else {
a80ec9ad6324 8056264: Multicast support improvements
msheppar
parents: 25859
diff changeset
   558
        JNU_ThrowNullPointerException(env, "network interface name is NULL");
a80ec9ad6324 8056264: Multicast support improvements
msheppar
parents: 25859
diff changeset
   559
        return ret;
a80ec9ad6324 8056264: Multicast support improvements
msheppar
parents: 25859
diff changeset
   560
    }
23563
71ed058849d4 8025293: JNI exception pending checks in java.net
msheppar
parents: 23015
diff changeset
   561
    if (name_utf == NULL) {
71ed058849d4 8025293: JNI exception pending checks in java.net
msheppar
parents: 23015
diff changeset
   562
       if (!(*env)->ExceptionCheck(env))
71ed058849d4 8025293: JNI exception pending checks in java.net
msheppar
parents: 23015
diff changeset
   563
           JNU_ThrowOutOfMemoryError(env, NULL);
71ed058849d4 8025293: JNI exception pending checks in java.net
msheppar
parents: 23015
diff changeset
   564
       return ret;
71ed058849d4 8025293: JNI exception pending checks in java.net
msheppar
parents: 23015
diff changeset
   565
    }
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   566
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   567
    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
   568
       (*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
   569
       return JNI_FALSE;
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
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   572
    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
   573
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   574
    (*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
   575
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   576
    close(sock);
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   577
    return ret;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   578
}
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   579
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   580
/*** Private methods definitions ****/
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   581
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   582
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
   583
    jboolean isCopy;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   584
    int ret, sock;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   585
    const char* name_utf;
13564
3571e232ab03 7190254: NetworkInterface getFlags implementation should support full integer bit range for flags value
chegar
parents: 13245
diff changeset
   586
    int flags = 0;
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   587
28549
a80ec9ad6324 8056264: Multicast support improvements
msheppar
parents: 25859
diff changeset
   588
    if (name != NULL) {
a80ec9ad6324 8056264: Multicast support improvements
msheppar
parents: 25859
diff changeset
   589
        name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
a80ec9ad6324 8056264: Multicast support improvements
msheppar
parents: 25859
diff changeset
   590
    } else {
a80ec9ad6324 8056264: Multicast support improvements
msheppar
parents: 25859
diff changeset
   591
        JNU_ThrowNullPointerException(env, "network interface name is NULL");
a80ec9ad6324 8056264: Multicast support improvements
msheppar
parents: 25859
diff changeset
   592
        return -1;
a80ec9ad6324 8056264: Multicast support improvements
msheppar
parents: 25859
diff changeset
   593
    }
a80ec9ad6324 8056264: Multicast support improvements
msheppar
parents: 25859
diff changeset
   594
23563
71ed058849d4 8025293: JNI exception pending checks in java.net
msheppar
parents: 23015
diff changeset
   595
    if (name_utf == NULL) {
71ed058849d4 8025293: JNI exception pending checks in java.net
msheppar
parents: 23015
diff changeset
   596
       if (!(*env)->ExceptionCheck(env))
71ed058849d4 8025293: JNI exception pending checks in java.net
msheppar
parents: 23015
diff changeset
   597
           JNU_ThrowOutOfMemoryError(env, NULL);
71ed058849d4 8025293: JNI exception pending checks in java.net
msheppar
parents: 23015
diff changeset
   598
       return -1;
71ed058849d4 8025293: JNI exception pending checks in java.net
msheppar
parents: 23015
diff changeset
   599
    }
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   600
    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
   601
        (*env)->ReleaseStringUTFChars(env, name, name_utf);
19411
b8942467963c 8022584: Memory leak in some NetworkInterface methods
igerasim
parents: 17921
diff changeset
   602
        return -1;
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   603
    }
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   604
13564
3571e232ab03 7190254: NetworkInterface getFlags implementation should support full integer bit range for flags value
chegar
parents: 13245
diff changeset
   605
    ret = getFlags(sock, name_utf, &flags);
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   606
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   607
    close(sock);
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   608
    (*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
   609
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   610
    if (ret < 0) {
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   611
        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
   612
        return -1;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   613
    }
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   614
13564
3571e232ab03 7190254: NetworkInterface getFlags implementation should support full integer bit range for flags value
chegar
parents: 13245
diff changeset
   615
    return flags;
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   616
}
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   617
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   618
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   619
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   620
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
 * Create a NetworkInterface object, populate the name and index, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
 * populate the InetAddress array based on the IP addresses for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
 * interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
 */
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   626
jobject createNetworkInterface(JNIEnv *env, netif *ifs) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    jobject netifObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    jobject name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    jobjectArray addrArr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    jobjectArray bindArr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    jobjectArray childArr;
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   632
    netaddr *addrs;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    jint addr_index, addr_count, bind_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    jint child_count, child_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    netaddr *addrP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    netif *childP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    jobject tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * Create a NetworkInterface object and populate it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    netifObj = (*env)->NewObject(env, ni_class, ni_ctrID);
23563
71ed058849d4 8025293: JNI exception pending checks in java.net
msheppar
parents: 23015
diff changeset
   643
    CHECK_NULL_RETURN(netifObj, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    name = (*env)->NewStringUTF(env, ifs->name);
23563
71ed058849d4 8025293: JNI exception pending checks in java.net
msheppar
parents: 23015
diff changeset
   645
    CHECK_NULL_RETURN(name, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    (*env)->SetObjectField(env, netifObj, ni_nameID, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    (*env)->SetObjectField(env, netifObj, ni_descID, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    (*env)->SetIntField(env, netifObj, ni_indexID, ifs->index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
    (*env)->SetBooleanField(env, netifObj, ni_virutalID, ifs->virtual ? JNI_TRUE : JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * Count the number of address on this interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    addr_count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    addrP = ifs->addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    while (addrP != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        addr_count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        addrP = addrP->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * Create the array of InetAddresses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     */
22646
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 22605
diff changeset
   664
    addrArr = (*env)->NewObjectArray(env, addr_count,  ia_class, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    if (addrArr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    bindArr = (*env)->NewObjectArray(env, addr_count, ni_ibcls, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    if (bindArr == NULL) {
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   671
       return NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    addrP = ifs->addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    addr_index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    bind_index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    while (addrP != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        jobject iaObj = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        jobject ibObj = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        if (addrP->family == AF_INET) {
22646
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 22605
diff changeset
   681
            iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            if (iaObj) {
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   683
                 setInetAddress_addr(env, iaObj, htonl(((struct sockaddr_in*)addrP->addr)->sin_addr.s_addr));
23563
71ed058849d4 8025293: JNI exception pending checks in java.net
msheppar
parents: 23015
diff changeset
   684
            } else {
71ed058849d4 8025293: JNI exception pending checks in java.net
msheppar
parents: 23015
diff changeset
   685
                return NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            if (ibObj) {
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   689
                 (*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
   690
                 if (addrP->brdcast) {
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   691
                    jobject ia2Obj = NULL;
22646
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 22605
diff changeset
   692
                    ia2Obj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   693
                    if (ia2Obj) {
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   694
                       setInetAddress_addr(env, ia2Obj, htonl(((struct sockaddr_in*)addrP->brdcast)->sin_addr.s_addr));
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   695
                       (*env)->SetObjectField(env, ibObj, ni_ib4broadcastID, ia2Obj);
23563
71ed058849d4 8025293: JNI exception pending checks in java.net
msheppar
parents: 23015
diff changeset
   696
                    } else {
71ed058849d4 8025293: JNI exception pending checks in java.net
msheppar
parents: 23015
diff changeset
   697
                        return NULL;
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   698
                    }
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   699
                 }
17921
8ae76f95800a 7107883: getNetworkPrefixLength() does not return correct prefix length
chegar
parents: 17423
diff changeset
   700
                 (*env)->SetShortField(env, ibObj, ni_ib4maskID, addrP->mask);
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   701
                 (*env)->SetObjectArrayElement(env, bindArr, bind_index++, ibObj);
23563
71ed058849d4 8025293: JNI exception pending checks in java.net
msheppar
parents: 23015
diff changeset
   702
            } else {
71ed058849d4 8025293: JNI exception pending checks in java.net
msheppar
parents: 23015
diff changeset
   703
                return NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        if (addrP->family == AF_INET6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            int scope=0;
22646
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 22605
diff changeset
   710
            iaObj = (*env)->NewObject(env, ia6_class, ia6_ctrID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            if (iaObj) {
22956
322ce28a7e20 8034182: Misc. warnings in java.net code
alanb
parents: 22646
diff changeset
   712
                jboolean ret = setInet6Address_ipaddress(env, iaObj, (char *)&(((struct sockaddr_in6*)addrP->addr)->sin6_addr));
20821
e0d0a585aa49 8015743: Address internet addresses
michaelm
parents: 17921
diff changeset
   713
                if (ret == JNI_FALSE) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                }
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   716
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                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
   718
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                if (scope != 0) { /* zero is default value, no need to set */
20821
e0d0a585aa49 8015743: Address internet addresses
michaelm
parents: 17921
diff changeset
   720
                    setInet6Address_scopeid(env, iaObj, scope);
e0d0a585aa49 8015743: Address internet addresses
michaelm
parents: 17921
diff changeset
   721
                    setInet6Address_scopeifname(env, iaObj, netifObj);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                }
23563
71ed058849d4 8025293: JNI exception pending checks in java.net
msheppar
parents: 23015
diff changeset
   723
            } else {
71ed058849d4 8025293: JNI exception pending checks in java.net
msheppar
parents: 23015
diff changeset
   724
                return NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            if (ibObj) {
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   728
                (*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
   729
                (*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
   730
                (*env)->SetObjectArrayElement(env, bindArr, bind_index++, ibObj);
23563
71ed058849d4 8025293: JNI exception pending checks in java.net
msheppar
parents: 23015
diff changeset
   731
            } else {
71ed058849d4 8025293: JNI exception pending checks in java.net
msheppar
parents: 23015
diff changeset
   732
                return NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        (*env)->SetObjectArrayElement(env, addrArr, addr_index++, iaObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        addrP = addrP->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * See if there is any virtual interface attached to this one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    child_count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    childP = ifs->childs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    while (childP) {
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   747
        child_count++;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   748
        childP = childP->next;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    childArr = (*env)->NewObjectArray(env, child_count, ni_class, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    if (childArr == NULL) {
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   753
        return NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * Create the NetworkInterface instances for the sub-interfaces as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
    child_index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    childP = ifs->childs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    while(childP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
      tmp = createNetworkInterface(env, childP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
      if (tmp == NULL) {
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   765
         return NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
      (*env)->SetObjectField(env, tmp, ni_parentID, netifObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
      (*env)->SetObjectArrayElement(env, childArr, child_index++, tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
      childP = childP->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    (*env)->SetObjectField(env, netifObj, ni_addrsID, addrArr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    (*env)->SetObjectField(env, netifObj, ni_bindsID, bindArr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    (*env)->SetObjectField(env, netifObj, ni_childsID, childArr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
    /* return the NetworkInterface */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    return netifObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
 * Enumerates all interfaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
static netif *enumInterfaces(JNIEnv *env) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
    netif *ifs;
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   784
    int sock;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * Enumerate IPv4 addresses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     */
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   789
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   790
    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
   791
    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
   792
        return NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   795
    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
   796
    close(sock);
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   797
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   798
    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
   799
        return NULL;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   800
    }
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   801
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13564
diff changeset
   802
    /* return partial list if an exception occurs in the middle of process ???*/
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   803
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * If IPv6 is available then enumerate IPv6 addresses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
#ifdef AF_INET6
6108
d5bfa0bc6123 6964714: NetworkInterface getInetAddresses enumerates IPv6 addresses if java.net.preferIPvStack property set
dsamersoff
parents: 6002
diff changeset
   808
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13564
diff changeset
   809
        /* User can disable ipv6 explicitly by -Djava.net.preferIPv4Stack=true,
6108
d5bfa0bc6123 6964714: NetworkInterface getInetAddresses enumerates IPv6 addresses if java.net.preferIPvStack property set
dsamersoff
parents: 6002
diff changeset
   810
         * 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
   811
         */
d5bfa0bc6123 6964714: NetworkInterface getInetAddresses enumerates IPv6 addresses if java.net.preferIPvStack property set
dsamersoff
parents: 6002
diff changeset
   812
        if (ipv6_available()) {
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   813
6108
d5bfa0bc6123 6964714: NetworkInterface getInetAddresses enumerates IPv6 addresses if java.net.preferIPvStack property set
dsamersoff
parents: 6002
diff changeset
   814
           sock =  openSocket(env, AF_INET6);
d5bfa0bc6123 6964714: NetworkInterface getInetAddresses enumerates IPv6 addresses if java.net.preferIPvStack property set
dsamersoff
parents: 6002
diff changeset
   815
           if (sock < 0 && (*env)->ExceptionOccurred(env)) {
d5bfa0bc6123 6964714: NetworkInterface getInetAddresses enumerates IPv6 addresses if java.net.preferIPvStack property set
dsamersoff
parents: 6002
diff changeset
   816
               freeif(ifs);
d5bfa0bc6123 6964714: NetworkInterface getInetAddresses enumerates IPv6 addresses if java.net.preferIPvStack property set
dsamersoff
parents: 6002
diff changeset
   817
               return NULL;
d5bfa0bc6123 6964714: NetworkInterface getInetAddresses enumerates IPv6 addresses if java.net.preferIPvStack property set
dsamersoff
parents: 6002
diff changeset
   818
           }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
6108
d5bfa0bc6123 6964714: NetworkInterface getInetAddresses enumerates IPv6 addresses if java.net.preferIPvStack property set
dsamersoff
parents: 6002
diff changeset
   820
           ifs = enumIPv6Interfaces(env, sock, ifs);
d5bfa0bc6123 6964714: NetworkInterface getInetAddresses enumerates IPv6 addresses if java.net.preferIPvStack property set
dsamersoff
parents: 6002
diff changeset
   821
           close(sock);
d5bfa0bc6123 6964714: NetworkInterface getInetAddresses enumerates IPv6 addresses if java.net.preferIPvStack property set
dsamersoff
parents: 6002
diff changeset
   822
d5bfa0bc6123 6964714: NetworkInterface getInetAddresses enumerates IPv6 addresses if java.net.preferIPvStack property set
dsamersoff
parents: 6002
diff changeset
   823
           if ((*env)->ExceptionOccurred(env)) {
d5bfa0bc6123 6964714: NetworkInterface getInetAddresses enumerates IPv6 addresses if java.net.preferIPvStack property set
dsamersoff
parents: 6002
diff changeset
   824
              freeif(ifs);
d5bfa0bc6123 6964714: NetworkInterface getInetAddresses enumerates IPv6 addresses if java.net.preferIPvStack property set
dsamersoff
parents: 6002
diff changeset
   825
              return NULL;
d5bfa0bc6123 6964714: NetworkInterface getInetAddresses enumerates IPv6 addresses if java.net.preferIPvStack property set
dsamersoff
parents: 6002
diff changeset
   826
           }
d5bfa0bc6123 6964714: NetworkInterface getInetAddresses enumerates IPv6 addresses if java.net.preferIPvStack property set
dsamersoff
parents: 6002
diff changeset
   827
d5bfa0bc6123 6964714: NetworkInterface getInetAddresses enumerates IPv6 addresses if java.net.preferIPvStack property set
dsamersoff
parents: 6002
diff changeset
   828
       }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
    return ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   834
#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
   835
       do{ \
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   836
        _pointer = (_type)malloc( _size ); \
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   837
        if (_pointer == NULL) { \
13245
7ab3ef5b9520 7181353: Update error message to distinguish native OOM and java OOM in net
zhouyx
parents: 12047
diff changeset
   838
            JNU_ThrowOutOfMemoryError(env, "Native heap allocation failed"); \
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   839
            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
   840
        } \
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   841
       } while(0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
 * Free an interface list (including any attached addresses)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
void freeif(netif *ifs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    netif *currif = ifs;
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   849
    netif *child = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
    while (currif != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        netaddr *addrP = currif->addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        while (addrP != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            netaddr *next = addrP->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
            free(addrP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            addrP = next;
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   857
         }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
5969
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
            * 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
   861
            */
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   862
          if (currif->childs != NULL) {
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   863
                freeif(currif->childs);
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   864
          }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   866
          ifs = currif->next;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   867
          free(currif);
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   868
          currif = ifs;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
17423
1c681c88107c 8013140: Heap corruption with NetworkInterface.getByInetAddress() and long i/f name
khazra
parents: 16870
diff changeset
   872
netif *addif(JNIEnv *env, int sock, const char * if_name,
1c681c88107c 8013140: Heap corruption with NetworkInterface.getByInetAddress() and long i/f name
khazra
parents: 16870
diff changeset
   873
             netif *ifs, struct sockaddr* ifr_addrP, int family,
1c681c88107c 8013140: Heap corruption with NetworkInterface.getByInetAddress() and long i/f name
khazra
parents: 16870
diff changeset
   874
             short prefix)
1c681c88107c 8013140: Heap corruption with NetworkInterface.getByInetAddress() and long i/f name
khazra
parents: 16870
diff changeset
   875
{
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   876
    netif *currif = ifs, *parent;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    netaddr *addrP;
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   878
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
   879
#ifdef LIFNAMSIZ
17423
1c681c88107c 8013140: Heap corruption with NetworkInterface.getByInetAddress() and long i/f name
khazra
parents: 16870
diff changeset
   880
    int ifnam_size = LIFNAMSIZ;
1c681c88107c 8013140: Heap corruption with NetworkInterface.getByInetAddress() and long i/f name
khazra
parents: 16870
diff changeset
   881
    char name[LIFNAMSIZ], vname[LIFNAMSIZ];
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
   882
#else
17423
1c681c88107c 8013140: Heap corruption with NetworkInterface.getByInetAddress() and long i/f name
khazra
parents: 16870
diff changeset
   883
    int ifnam_size = IFNAMSIZ;
1c681c88107c 8013140: Heap corruption with NetworkInterface.getByInetAddress() and long i/f name
khazra
parents: 16870
diff changeset
   884
    char name[IFNAMSIZ], vname[IFNAMSIZ];
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
   885
#endif
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   886
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   887
    char  *name_colonP;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   888
    int mask;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
    int isVirtual = 0;
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   890
    int addr_size;
13564
3571e232ab03 7190254: NetworkInterface getFlags implementation should support full integer bit range for flags value
chegar
parents: 13245
diff changeset
   891
    int flags = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * If the interface name is a logical interface then we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * remove the unit number so that we have the physical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * interface (eg: hme0:1 -> hme0). NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * currently doesn't have any concept of physical vs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     * logical interfaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     */
17423
1c681c88107c 8013140: Heap corruption with NetworkInterface.getByInetAddress() and long i/f name
khazra
parents: 16870
diff changeset
   900
    strncpy(name, if_name, ifnam_size);
1c681c88107c 8013140: Heap corruption with NetworkInterface.getByInetAddress() and long i/f name
khazra
parents: 16870
diff changeset
   901
    name[ifnam_size - 1] = '\0';
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   902
    *vname = 0;
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
     * Create and populate the netaddr node. If allocation fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     * return an un-updated list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     */
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   908
    /*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
   909
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   910
#ifdef AF_INET6
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   911
    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
   912
#else
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   913
    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
   914
#endif
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   915
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   916
    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
   917
    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
   918
    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
   919
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
    addrP->family = family;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
    addrP->brdcast = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
    addrP->mask = prefix;
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   923
    addrP->next = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
    if (family == AF_INET) {
17921
8ae76f95800a 7107883: getNetworkPrefixLength() does not return correct prefix length
chegar
parents: 17423
diff changeset
   925
       // Deal with broadcast addr & subnet mask
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   926
       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
   927
       addrP->brdcast = getBroadcast(env, sock, name,  brdcast_to );
23563
71ed058849d4 8025293: JNI exception pending checks in java.net
msheppar
parents: 23015
diff changeset
   928
       if ((*env)->ExceptionCheck(env) == JNI_TRUE) {
71ed058849d4 8025293: JNI exception pending checks in java.net
msheppar
parents: 23015
diff changeset
   929
           return ifs;
71ed058849d4 8025293: JNI exception pending checks in java.net
msheppar
parents: 23015
diff changeset
   930
       }
71ed058849d4 8025293: JNI exception pending checks in java.net
msheppar
parents: 23015
diff changeset
   931
       if ((mask = getSubnet(env, sock, name)) != -1) {
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   932
           addrP->mask = mask;
23563
71ed058849d4 8025293: JNI exception pending checks in java.net
msheppar
parents: 23015
diff changeset
   933
       } else if((*env)->ExceptionCheck(env)) {
71ed058849d4 8025293: JNI exception pending checks in java.net
msheppar
parents: 23015
diff changeset
   934
           return ifs;
71ed058849d4 8025293: JNI exception pending checks in java.net
msheppar
parents: 23015
diff changeset
   935
       }
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   936
     }
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   937
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   938
    /**
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13564
diff changeset
   939
     * Deal with virtual interface with colon notation e.g. eth0:1
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   940
     */
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   941
    name_colonP = strchr(name, ':');
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   942
    if (name_colonP != NULL) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
      /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
       * This is a virtual interface. If we are able to access the parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
       * we need to create a new entry if it doesn't exist yet *and* update
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
       * the 'parent' interface with the new records.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
       */
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   948
        *name_colonP = 0;
13564
3571e232ab03 7190254: NetworkInterface getFlags implementation should support full integer bit range for flags value
chegar
parents: 13245
diff changeset
   949
        if (getFlags(sock, name, &flags) < 0 || flags < 0) {
2058
577525f89bd4 6800805: java.net.NetworkInterface.getNetworkInterfaces() does not list IPv6 network interfaces correctly
chegar
parents: 1097
diff changeset
   950
            // 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
   951
            // 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
   952
            isVirtual = 1;
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   953
            *name_colonP = ':';
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
        else{
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   956
           // 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
   957
           // 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
   958
            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
   959
            vname[name_colonP - name] = ':';
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   960
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     * Check if this is a "new" interface. Use the interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * name for matching because index isn't supported on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * Solaris 2.6 & 7.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
    while (currif != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        if (strcmp(name, currif->name) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        currif = currif->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     * If "new" then create an netif structure and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     * insert it onto the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
    if (currif == NULL) {
17423
1c681c88107c 8013140: Heap corruption with NetworkInterface.getByInetAddress() and long i/f name
khazra
parents: 16870
diff changeset
   980
         CHECKED_MALLOC3(currif, netif *, sizeof(netif) + ifnam_size);
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   981
         currif->name = (char *) currif+sizeof(netif);
17423
1c681c88107c 8013140: Heap corruption with NetworkInterface.getByInetAddress() and long i/f name
khazra
parents: 16870
diff changeset
   982
         strncpy(currif->name, name, ifnam_size);
1c681c88107c 8013140: Heap corruption with NetworkInterface.getByInetAddress() and long i/f name
khazra
parents: 16870
diff changeset
   983
         currif->name[ifnam_size - 1] = '\0';
6002
4df5b9bcf842 6967937: Scope id no longer being set after 6931566
chegar
parents: 5969
diff changeset
   984
         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
   985
         currif->addr = NULL;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   986
         currif->childs = NULL;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   987
         currif->virtual = isVirtual;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   988
         currif->next = ifs;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
   989
         ifs = currif;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     * Finally insert the address on the interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
    addrP->next = currif->addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
    currif->addr = addrP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    parent = currif;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     * Let's deal with the virtual interface now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
    if (vname[0]) {
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1004
        netaddr *tmpaddr;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1006
        currif = parent->childs;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1008
        while (currif != NULL) {
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1009
            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
  1010
                break;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1011
            }
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1012
            currif = currif->next;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        }
5969
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
        if (currif == NULL) {
17423
1c681c88107c 8013140: Heap corruption with NetworkInterface.getByInetAddress() and long i/f name
khazra
parents: 16870
diff changeset
  1016
            CHECKED_MALLOC3(currif, netif *, sizeof(netif) + ifnam_size);
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1017
            currif->name = (char *) currif + sizeof(netif);
17423
1c681c88107c 8013140: Heap corruption with NetworkInterface.getByInetAddress() and long i/f name
khazra
parents: 16870
diff changeset
  1018
            strncpy(currif->name, vname, ifnam_size);
1c681c88107c 8013140: Heap corruption with NetworkInterface.getByInetAddress() and long i/f name
khazra
parents: 16870
diff changeset
  1019
            currif->name[ifnam_size - 1] = '\0';
6002
4df5b9bcf842 6967937: Scope id no longer being set after 6931566
chegar
parents: 5969
diff changeset
  1020
            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
  1021
            currif->addr = NULL;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1022
           /* 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
  1023
            currif->virtual = 1;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1024
            currif->childs = NULL;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1025
            currif->next = parent->childs;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1026
            parent->childs = currif;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1027
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1029
        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
  1030
        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
  1031
        if (addrP->addr != NULL) {
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1032
            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
  1033
            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
  1034
        }
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
        if (addrP->brdcast != NULL) {
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1037
            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
  1038
            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
  1039
        }
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1040
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1041
        tmpaddr->next = currif->addr;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1042
        currif->addr = tmpaddr;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
    return ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1048
/* 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
  1049
 * proto is AF_INET/AF_INET6
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
 */
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1051
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
  1052
    int sock;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1053
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22956
diff changeset
  1054
    if ((sock = socket(proto, SOCK_DGRAM, 0)) < 0) {
5969
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
         * 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
  1057
         * 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
  1058
         */
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1059
        if (errno != EPROTONOSUPPORT) {
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1060
            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
  1061
        }
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1062
        return -1;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1063
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1065
    return sock;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1066
}
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1067
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1068
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1069
/** Linux, AIX **/
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1070
#if defined(__linux__) || defined(_AIX)
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1071
/* 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
  1072
 * 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
  1073
 */
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1074
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1075
#ifdef AF_INET6
28549
a80ec9ad6324 8056264: Multicast support improvements
msheppar
parents: 25859
diff changeset
  1076
// unused arg ifname and struct if2
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1077
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
  1078
    int sock;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1079
    struct ifreq if2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22956
diff changeset
  1081
     if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1082
         if (errno == EPROTONOSUPPORT){
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22956
diff changeset
  1083
              if ( (sock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0 ){
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1084
                 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
  1085
                 return -1;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1086
              }
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1087
         }
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1088
         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
  1089
             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
  1090
             return -1;
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
   }
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
     /* 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
  1095
        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
  1096
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1097
       return sock;
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
#else
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1101
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
  1102
    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
  1103
}
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1104
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1106
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
  1107
    struct ifconf ifc;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1108
    struct ifreq *ifreqP;
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1109
    char *buf = NULL;
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1110
    int numifs;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1111
    unsigned i;
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1112
    int siocgifconfRequest = SIOCGIFCONF;
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1113
709
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1114
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1115
#if defined(__linux__)
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1116
    /* 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
  1117
     * SIOCGIFCOUNT doesn't work
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1118
     */
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1119
    ifc.ifc_buf = NULL;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1120
    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
  1121
        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
  1122
        return ifs;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1123
    }
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1124
#elif defined(_AIX)
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1125
    ifc.ifc_buf = NULL;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1126
    if (ioctl(sock, SIOCGSIZIFCONF, &(ifc.ifc_len)) < 0) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1127
        NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "ioctl SIOCGSIZIFCONF failed");
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1128
        return ifs;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1129
    }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1130
#endif /* __linux__ */
709
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1131
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1132
    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
  1133
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1134
    ifc.ifc_buf = buf;
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1135
#if defined(_AIX)
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1136
    siocgifconfRequest = CSIOCGIFCONF;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1137
#endif
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1138
    if (ioctl(sock, siocgifconfRequest, (char *)&ifc) < 0) {
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1139
        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
  1140
        (void) free(buf);
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1141
        return ifs;
709
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1142
    }
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1143
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1144
    /*
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1145
     * Iterate through each interface
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1146
     */
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1147
    ifreqP = ifc.ifc_req;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1148
    for (i=0; i<ifc.ifc_len/sizeof (struct ifreq); i++, ifreqP++) {
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1149
#if defined(_AIX)
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1150
        if (ifreqP->ifr_addr.sa_family != AF_INET) continue;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1151
#endif
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1152
        /*
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1153
         * Add to the list
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
        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
  1156
5969
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
         * 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
  1159
         */
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1160
        if ((*env)->ExceptionOccurred(env)) {
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1161
            free(buf);
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1162
            freeif(ifs);
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1163
            return NULL;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1164
        }
709
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
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
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1167
    /*
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1168
     * Free socket and buffer
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1169
     */
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1170
    free(buf);
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1171
    return ifs;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1172
}
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1173
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1174
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1175
/*
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1176
 * 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
  1177
 */
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1178
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1179
#if defined(AF_INET6) && defined(__linux__)
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1180
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
  1181
    FILE *f;
8554
14e626e9407b 7022269: clean up fscanf usage in Linux networking native code
chegar
parents: 8549
diff changeset
  1182
    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
  1183
    char addr6p[8][5];
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1184
    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
  1185
    uint8_t ipv6addr[16];
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1186
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1187
    if ((f = fopen(_PATH_PROCNET_IFINET6, "r")) != NULL) {
11355
c2cb710f1bdd 7078386: NetworkInterface.getNetworkInterfaces() may return corrupted results on linux
michaelm
parents: 10605
diff changeset
  1188
        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
  1189
                         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
  1190
                         &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
  1191
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1192
            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
  1193
            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
  1194
            struct sockaddr_in6 addr;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1195
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1196
            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
  1197
                           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
  1198
            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
  1199
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1200
            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
  1201
            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
  1202
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1203
            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
  1204
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1205
            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
  1206
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1207
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1208
            /*
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1209
             * 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
  1210
             */
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1211
            if ((*env)->ExceptionOccurred(env)) {
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1212
                fclose(f);
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1213
                return ifs;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1214
            }
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
       fclose(f);
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1217
    }
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1218
    return ifs;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1219
}
709
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1220
#endif
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1221
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1223
/*
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1224
 * Enumerates and returns all IPv6 interfaces on AIX
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1225
 */
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1226
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1227
#if defined(AF_INET6) && defined(_AIX)
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1228
static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1229
    struct ifconf ifc;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1230
    struct ifreq *ifreqP;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1231
    char *buf;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1232
    int numifs;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1233
    unsigned i;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1234
    unsigned bufsize;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1235
    char *cp, *cplimit;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1236
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1237
    /* use SIOCGSIZIFCONF to get size for  SIOCGIFCONF */
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1238
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1239
    ifc.ifc_buf = NULL;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1240
    if (ioctl(sock, SIOCGSIZIFCONF, &(ifc.ifc_len)) < 0) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1241
        NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException",
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1242
                        "ioctl SIOCGSIZIFCONF failed");
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1243
        return ifs;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1244
    }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1245
    bufsize = ifc.ifc_len;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1246
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1247
    buf = (char *)malloc(bufsize);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1248
    if (!buf) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1249
        JNU_ThrowOutOfMemoryError(env, "Network interface native buffer allocation failed");
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1250
        return ifs;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1251
    }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1252
    ifc.ifc_len = bufsize;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1253
    ifc.ifc_buf = buf;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1254
    if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1255
        NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException",
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1256
                       "ioctl CSIOCGIFCONF failed");
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1257
        free(buf);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1258
        return ifs;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1259
    }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1260
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1261
    /*
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1262
     * Iterate through each interface
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1263
     */
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1264
    ifreqP = ifc.ifc_req;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1265
    cp = (char *)ifc.ifc_req;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1266
    cplimit = cp + ifc.ifc_len;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1267
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1268
    for ( ; cp < cplimit; cp += (sizeof(ifreqP->ifr_name) + MAX((ifreqP->ifr_addr).sa_len, sizeof(ifreqP->ifr_addr)))) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1269
        ifreqP = (struct ifreq *)cp;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1270
        struct ifreq if2;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1271
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1272
        memset((char *)&if2, 0, sizeof(if2));
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1273
        strcpy(if2.ifr_name, ifreqP->ifr_name);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1274
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1275
        /*
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1276
         * Skip interface that aren't UP
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1277
         */
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1278
        if (ioctl(sock, SIOCGIFFLAGS, (char *)&if2) >= 0) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1279
            if (!(if2.ifr_flags & IFF_UP)) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1280
                continue;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1281
            }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1282
        }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1283
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1284
        if (ifreqP->ifr_addr.sa_family != AF_INET6)
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1285
            continue;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1286
22605
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 22597
diff changeset
  1287
        if (ioctl(sock, SIOCGIFSITE6, (char *)&if2) >= 0) {
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 22597
diff changeset
  1288
            struct sockaddr_in6 *s6= (struct sockaddr_in6 *)&(ifreqP->ifr_addr);
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 22597
diff changeset
  1289
            s6->sin6_scope_id = if2.ifr_site6;
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 22597
diff changeset
  1290
        }
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 22597
diff changeset
  1291
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1292
        /*
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1293
         * Add to the list
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1294
         */
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1295
        ifs = addif(env, sock, ifreqP->ifr_name, ifs,
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1296
                    (struct sockaddr *)&(ifreqP->ifr_addr),
22605
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 22597
diff changeset
  1297
                    AF_INET6, 0);
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1298
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1299
        /*
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1300
         * If an exception occurred then free the list
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1301
         */
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1302
        if ((*env)->ExceptionOccurred(env)) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1303
            free(buf);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1304
            freeif(ifs);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1305
            return NULL;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1306
        }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1307
    }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1308
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1309
    /*
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1310
     * Free socket and buffer
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1311
     */
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1312
    free(buf);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1313
    return ifs;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1314
}
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1315
#endif
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1316
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1317
6002
4df5b9bcf842 6967937: Scope id no longer being set after 6931566
chegar
parents: 5969
diff changeset
  1318
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
  1319
     /*
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1320
      * 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
  1321
      */
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1322
#if defined(_AIX)
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1323
    return if_nametoindex(name);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1324
#else
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1325
    struct ifreq if2;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1326
    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
  1327
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1328
    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
  1329
        return -1;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1330
    }
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1331
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1332
    return if2.ifr_ifindex;
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1333
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
 * Returns the IPv4 broadcast address of a named interface, if it exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
 * Returns 0 if it doesn't have one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
 */
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1340
static struct sockaddr *getBroadcast(JNIEnv *env, int sock, const char *ifname, struct sockaddr *brdcast_store) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
  struct sockaddr *ret = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
  struct ifreq if2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
  memset((char *) &if2, 0, sizeof(if2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
  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
  1346
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
  /* 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
  1348
  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
  1349
      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
  1350
      return ret;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
  }
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1352
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1353
  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
  1354
      /* 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
  1355
      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
  1356
          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
  1357
          return ret;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1358
      }
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1359
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1360
      ret = brdcast_store;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
      memcpy(ret, &if2.ifr_broadaddr, sizeof(struct sockaddr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
  }
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1363
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
  return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
 * Returns the IPv4 subnet prefix length (aka subnet mask) for the named
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
 * interface, if it has one, otherwise return -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
 */
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1371
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
  1372
    unsigned int mask;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1373
    short ret;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1374
    struct ifreq if2;
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
    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
  1377
    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
  1378
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1379
    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
  1380
        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
  1381
        return -1;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1382
    }
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1383
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1384
    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
  1385
    ret = 0;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1386
    while (mask) {
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1387
       mask <<= 1;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1388
       ret++;
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
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1391
    return ret;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1392
}
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1393
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1394
/**
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1395
 * 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
  1396
 * 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
  1397
 * 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
  1398
 */
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1399
static int getMacAddress(JNIEnv *env, int sock, const char* ifname, const struct in_addr* addr, unsigned char *buf) {
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1400
#if defined (_AIX)
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1401
    int size;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1402
    struct kinfo_ndd *nddp;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1403
    void *end;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1404
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1405
    size = getkerninfo(KINFO_NDD, 0, 0, 0);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1406
    if (size == 0) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1407
        return -1;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1408
    }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1409
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1410
    if (size < 0) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1411
        perror("getkerninfo 1");
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1412
        return -1;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1413
    }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1414
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1415
    nddp = (struct kinfo_ndd *)malloc(size);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1416
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1417
    if (!nddp) {
23563
71ed058849d4 8025293: JNI exception pending checks in java.net
msheppar
parents: 23015
diff changeset
  1418
        JNU_ThrowOutOfMemoryError(env, "Network interface getMacAddress native buffer allocation failed");
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1419
        return -1;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1420
    }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1421
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1422
    if (getkerninfo(KINFO_NDD, nddp, &size, 0) < 0) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1423
        perror("getkerninfo 2");
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1424
        return -1;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1425
    }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1426
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1427
    end = (void *)nddp + size;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1428
    while ((void *)nddp < end) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1429
        if (!strcmp(nddp->ndd_alias, ifname) ||
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1430
                !strcmp(nddp->ndd_name, ifname)) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1431
            bcopy(nddp->ndd_addr, buf, 6);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1432
            return 6;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1433
        } else {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1434
            nddp++;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1435
        }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1436
    }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1437
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1438
    return -1;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1439
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1440
#elif defined(__linux__)
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1441
    static struct ifreq ifr;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1442
    int i;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1444
    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
  1445
    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
  1446
        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
  1447
        return -1;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1448
    }
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1449
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1450
    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
  1451
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1452
   /*
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1453
    * 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
  1454
    */
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1455
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1456
    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
  1457
        if (buf[i] != 0)
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1458
            return IFHWADDRLEN;
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
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
    return -1;
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 21409
diff changeset
  1462
#endif
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1463
}
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1464
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1465
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
  1466
    struct ifreq if2;
28549
a80ec9ad6324 8056264: Multicast support improvements
msheppar
parents: 25859
diff changeset
  1467
    memset((char *) &if2, 0, sizeof(if2));
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1468
28549
a80ec9ad6324 8056264: Multicast support improvements
msheppar
parents: 25859
diff changeset
  1469
    if (ifname != NULL) {
a80ec9ad6324 8056264: Multicast support improvements
msheppar
parents: 25859
diff changeset
  1470
        strcpy(if2.ifr_name, ifname);
a80ec9ad6324 8056264: Multicast support improvements
msheppar
parents: 25859
diff changeset
  1471
    } else {
a80ec9ad6324 8056264: Multicast support improvements
msheppar
parents: 25859
diff changeset
  1472
        JNU_ThrowNullPointerException(env, "network interface name is NULL");
a80ec9ad6324 8056264: Multicast support improvements
msheppar
parents: 25859
diff changeset
  1473
        return -1;
a80ec9ad6324 8056264: Multicast support improvements
msheppar
parents: 25859
diff changeset
  1474
    }
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1475
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1476
    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
  1477
        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
  1478
        return -1;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1479
    }
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1480
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1481
    return  if2.ifr_mtu;
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
13564
3571e232ab03 7190254: NetworkInterface getFlags implementation should support full integer bit range for flags value
chegar
parents: 13245
diff changeset
  1484
static int getFlags(int sock, const char *ifname, int *flags) {
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1485
  struct ifreq if2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
  memset((char *) &if2, 0, sizeof(if2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
  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
  1489
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1490
  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
  1491
      return -1;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1492
  }
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1493
13564
3571e232ab03 7190254: NetworkInterface getFlags implementation should support full integer bit range for flags value
chegar
parents: 13245
diff changeset
  1494
  if (sizeof(if2.ifr_flags) == sizeof(short)) {
3571e232ab03 7190254: NetworkInterface getFlags implementation should support full integer bit range for flags value
chegar
parents: 13245
diff changeset
  1495
      *flags = (if2.ifr_flags & 0xffff);
3571e232ab03 7190254: NetworkInterface getFlags implementation should support full integer bit range for flags value
chegar
parents: 13245
diff changeset
  1496
  } else {
3571e232ab03 7190254: NetworkInterface getFlags implementation should support full integer bit range for flags value
chegar
parents: 13245
diff changeset
  1497
      *flags = if2.ifr_flags;
3571e232ab03 7190254: NetworkInterface getFlags implementation should support full integer bit range for flags value
chegar
parents: 13245
diff changeset
  1498
  }
3571e232ab03 7190254: NetworkInterface getFlags implementation should support full integer bit range for flags value
chegar
parents: 13245
diff changeset
  1499
  return 0;
5969
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
#endif
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
/** Solaris **/
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1505
#ifdef __solaris__
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1506
/* 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
  1507
 * 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
  1508
 */
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1509
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1510
#ifdef AF_INET6
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1511
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
  1512
    int sock, alreadyV6 = 0;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1513
    struct lifreq if2;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1514
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22956
diff changeset
  1515
     if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1516
         if (errno == EPROTONOSUPPORT){
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22956
diff changeset
  1517
              if ( (sock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0 ){
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1518
                 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
  1519
                 return -1;
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
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1522
              alreadyV6=1;
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
         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
  1525
             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
  1526
             return -1;
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
   }
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1529
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1530
     /**
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13564
diff changeset
  1531
      * Solaris requires that we have an IPv6 socket to query an
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13564
diff changeset
  1532
      * interface without an IPv4 address - check it here.
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13564
diff changeset
  1533
      * POSIX 1 require the kernel to return ENOTTY if the call is
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13564
diff changeset
  1534
      * inappropriate for a device e.g. the NETMASK for a device having IPv6
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13564
diff changeset
  1535
      * only address but not all devices follow the standard so
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13564
diff changeset
  1536
      * fall back on any error. It's not an ecologically friendly gesture
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13564
diff changeset
  1537
      * but more reliable.
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1538
      */
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 (! alreadyV6 ){
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1541
        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
  1542
        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
  1543
        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
  1544
                close(sock);
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22956
diff changeset
  1545
                if ( (sock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0 ){
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1546
                      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
  1547
                      return -1;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1548
                }
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1549
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
    }
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1551
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1552
    return sock;
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
#else
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1556
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
  1557
    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
  1558
}
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1559
#endif
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1560
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1561
/*
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1562
 * Enumerates and returns all IPv4 interfaces
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13564
diff changeset
  1563
 * (linux verision)
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1564
 */
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1565
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1566
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
  1567
     return enumIPvXInterfaces(env,sock, ifs, AF_INET);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1570
#ifdef AF_INET6
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1571
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
  1572
    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
  1573
}
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1574
#endif
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1575
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1576
/*
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1577
   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
  1578
   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
  1579
 */
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1580
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
  1581
    struct lifconf ifc;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1582
    struct lifreq *ifr;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1583
    int n;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1584
    char *buf;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1585
    struct lifnum numifs;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1586
    unsigned bufsize;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1587
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
     * Get the interface count
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1590
     */
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1591
    numifs.lifn_family = family;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1592
    numifs.lifn_flags = 0;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1593
    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
  1594
        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
  1595
        return ifs;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1596
    }
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1597
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1598
    /*
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1599
     *  Enumerate the interface configurations
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1600
     */
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1601
    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
  1602
    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
  1603
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1604
    ifc.lifc_family = family;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1605
    ifc.lifc_flags = 0;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1606
    ifc.lifc_len = bufsize;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1607
    ifc.lifc_buf = buf;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1608
    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
  1609
        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
  1610
        free(buf);
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1611
        return ifs;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1612
    }
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1613
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1614
    /*
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1615
     * Iterate through each interface
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1616
     */
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1617
    ifr = ifc.lifc_req;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1618
    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
  1619
        int index = -1;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1620
        struct lifreq if2;
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
        /*
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1623
        * 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
  1624
        */
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1625
        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
  1626
            continue;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1627
        }
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1628
6002
4df5b9bcf842 6967937: Scope id no longer being set after 6931566
chegar
parents: 5969
diff changeset
  1629
#ifdef AF_INET6
4df5b9bcf842 6967937: Scope id no longer being set after 6931566
chegar
parents: 5969
diff changeset
  1630
        if (ifr->lifr_addr.ss_family == AF_INET6) {
4df5b9bcf842 6967937: Scope id no longer being set after 6931566
chegar
parents: 5969
diff changeset
  1631
            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
  1632
            s6->sin6_scope_id = getIndex(sock, ifr->lifr_name);
4df5b9bcf842 6967937: Scope id no longer being set after 6931566
chegar
parents: 5969
diff changeset
  1633
        }
4df5b9bcf842 6967937: Scope id no longer being set after 6931566
chegar
parents: 5969
diff changeset
  1634
#endif
4df5b9bcf842 6967937: Scope id no longer being set after 6931566
chegar
parents: 5969
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
        /* add to the list */
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1637
        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
  1638
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1639
        /*
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1640
        * 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
  1641
        */
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1642
        if ((*env)->ExceptionOccurred(env)) {
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1643
            free(buf);
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1644
            return ifs;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1645
        }
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1646
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1647
   }
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1648
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1649
    free(buf);
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1650
    return ifs;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1651
}
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1652
6002
4df5b9bcf842 6967937: Scope id no longer being set after 6931566
chegar
parents: 5969
diff changeset
  1653
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
  1654
   /*
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1655
    * 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
  1656
    * (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
  1657
    */
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1658
    struct lifreq if2;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1659
    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
  1660
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1661
    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
  1662
        return -1;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1663
    }
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1664
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1665
    return if2.lifr_index;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1666
}
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1667
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1668
/**
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1669
 * 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
  1670
 * 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
  1671
 */
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1672
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
  1673
    struct sockaddr *ret = NULL;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1674
    struct lifreq if2;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1675
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1676
    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
  1677
    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
  1678
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1679
    /* 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
  1680
    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
  1681
        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
  1682
        return ret;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1683
    }
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1684
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1685
    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
  1686
        /* 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
  1687
        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
  1688
            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
  1689
            return ret;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1690
        }
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1691
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1692
        ret = brdcast_store;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1693
        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
  1694
    }
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1695
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1696
    return ret;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1697
}
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1698
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1699
/**
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1700
 * 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
  1701
 * 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
  1702
 */
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1703
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
  1704
    unsigned int mask;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1705
    short ret;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1706
    struct lifreq if2;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1707
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1708
    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
  1709
    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
  1710
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1711
    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
  1712
        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
  1713
        return -1;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1714
    }
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1715
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1716
    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
  1717
    ret = 0;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1718
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1719
    while (mask) {
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1720
       mask <<= 1;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1721
       ret++;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1722
    }
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1723
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1724
    return ret;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1725
}
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1726
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1727
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1728
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1729
#define DEV_PREFIX  "/dev/"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
 * Solaris specific DLPI code to get hardware address from a device.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
 * Unfortunately, at least up to Solaris X, you have to have special
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
 * privileges (i.e. be root).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
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
  1737
    char style1dev[MAXPATHLEN];
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1738
    int fd;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1739
    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
  1740
    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
  1741
    struct strbuf msg;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1742
    char buf[128];
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1743
    int flags = 0;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1744
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1745
   /**
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1746
    * Device is in /dev
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1747
    * e.g.: /dev/bge0
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1748
    */
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1749
    strcpy(style1dev, DEV_PREFIX);
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1750
    strcat(style1dev, ifname);
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1751
    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
  1752
        /*
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1753
         * 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
  1754
         * 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
  1755
         */
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1756
         return 0;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1757
    }
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1758
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1759
    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
  1760
    dlpareq.dl_addr_type = DL_CURR_PHYS_ADDR;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1762
    msg.buf = (char *)&dlpareq;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1763
    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
  1764
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1765
    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
  1766
        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
  1767
        return -1;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1768
    }
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1769
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1770
    dlpaack = (dl_phys_addr_ack_t *)buf;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1772
    msg.buf = (char *)buf;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1773
    msg.len = 0;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1774
    msg.maxlen = sizeof (buf);
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1775
    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
  1776
        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
  1777
        return -1;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1778
    }
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1779
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1780
    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
  1781
        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
  1782
        return -1;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1783
    }
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1784
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1785
    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
  1786
    return dlpaack->dl_addr_length;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
 * Get the Hardware address (usually MAC address) for the named interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
 * return puts the data in buf, and returns the length, in byte, of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
 * MAC address. Returns -1 if there is no hardware address on that interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
 */
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1794
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
  1795
    struct arpreq arpreq;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1796
    struct sockaddr_in* sin;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1797
    struct sockaddr_in ipAddr;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1798
    int len, i;
10605
8c19a790b7c1 7079012: test/java/net/NetworkInterface/NetParamsTest.java fails with SocketException getting mac address
michaelm
parents: 9035
diff changeset
  1799
    struct lifreq lif;
8c19a790b7c1 7079012: test/java/net/NetworkInterface/NetParamsTest.java fails with SocketException getting mac address
michaelm
parents: 9035
diff changeset
  1800
8c19a790b7c1 7079012: test/java/net/NetworkInterface/NetParamsTest.java fails with SocketException getting mac address
michaelm
parents: 9035
diff changeset
  1801
    /* 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
  1802
     * try the old way.
8c19a790b7c1 7079012: test/java/net/NetworkInterface/NetParamsTest.java fails with SocketException getting mac address
michaelm
parents: 9035
diff changeset
  1803
     */
8c19a790b7c1 7079012: test/java/net/NetworkInterface/NetParamsTest.java fails with SocketException getting mac address
michaelm
parents: 9035
diff changeset
  1804
    memset(&lif, 0, sizeof(lif));
8c19a790b7c1 7079012: test/java/net/NetworkInterface/NetParamsTest.java fails with SocketException getting mac address
michaelm
parents: 9035
diff changeset
  1805
    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
  1806
8c19a790b7c1 7079012: test/java/net/NetworkInterface/NetParamsTest.java fails with SocketException getting mac address
michaelm
parents: 9035
diff changeset
  1807
    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
  1808
        struct sockaddr_dl *sp;
8c19a790b7c1 7079012: test/java/net/NetworkInterface/NetParamsTest.java fails with SocketException getting mac address
michaelm
parents: 9035
diff changeset
  1809
        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
  1810
        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
  1811
        return sp->sdl_alen;
8c19a790b7c1 7079012: test/java/net/NetworkInterface/NetParamsTest.java fails with SocketException getting mac address
michaelm
parents: 9035
diff changeset
  1812
    }
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1813
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1814
   /**
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1815
    * 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
  1816
    * 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
  1817
    * 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
  1818
    */
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1819
    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
  1820
        /*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
  1821
10605
8c19a790b7c1 7079012: test/java/net/NetworkInterface/NetParamsTest.java fails with SocketException getting mac address
michaelm
parents: 9035
diff changeset
  1822
        if (addr == NULL) {
8c19a790b7c1 7079012: test/java/net/NetworkInterface/NetParamsTest.java fails with SocketException getting mac address
michaelm
parents: 9035
diff changeset
  1823
            /**
8c19a790b7c1 7079012: test/java/net/NetworkInterface/NetParamsTest.java fails with SocketException getting mac address
michaelm
parents: 9035
diff changeset
  1824
             * 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
  1825
             */
8c19a790b7c1 7079012: test/java/net/NetworkInterface/NetParamsTest.java fails with SocketException getting mac address
michaelm
parents: 9035
diff changeset
  1826
             return -1;
8c19a790b7c1 7079012: test/java/net/NetworkInterface/NetParamsTest.java fails with SocketException getting mac address
michaelm
parents: 9035
diff changeset
  1827
         }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
10605
8c19a790b7c1 7079012: test/java/net/NetworkInterface/NetParamsTest.java fails with SocketException getting mac address
michaelm
parents: 9035
diff changeset
  1829
         len = 6; //???
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1830
10605
8c19a790b7c1 7079012: test/java/net/NetworkInterface/NetParamsTest.java fails with SocketException getting mac address
michaelm
parents: 9035
diff changeset
  1831
         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
  1832
         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
  1833
         ipAddr.sin_port = 0;
8c19a790b7c1 7079012: test/java/net/NetworkInterface/NetParamsTest.java fails with SocketException getting mac address
michaelm
parents: 9035
diff changeset
  1834
         ipAddr.sin_family = AF_INET;
8c19a790b7c1 7079012: test/java/net/NetworkInterface/NetParamsTest.java fails with SocketException getting mac address
michaelm
parents: 9035
diff changeset
  1835
         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
  1836
         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
  1837
         arpreq.arp_flags= ATF_PUBL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
10605
8c19a790b7c1 7079012: test/java/net/NetworkInterface/NetParamsTest.java fails with SocketException getting mac address
michaelm
parents: 9035
diff changeset
  1839
         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
  1840
             return -1;
8c19a790b7c1 7079012: test/java/net/NetworkInterface/NetParamsTest.java fails with SocketException getting mac address
michaelm
parents: 9035
diff changeset
  1841
         }
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1842
10605
8c19a790b7c1 7079012: test/java/net/NetworkInterface/NetParamsTest.java fails with SocketException getting mac address
michaelm
parents: 9035
diff changeset
  1843
         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
  1844
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1846
    /*
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1847
     * All bytes to 0 means no hardware address.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1850
    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
  1851
      if (buf[i] != 0)
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1852
         return len;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1853
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1855
    return -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1858
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
  1859
    struct lifreq if2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1861
    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
  1862
    strcpy(if2.lifr_name, ifname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1864
    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
  1865
        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
  1866
        return -1;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1867
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1869
    return  if2.lifr_mtu;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
13564
3571e232ab03 7190254: NetworkInterface getFlags implementation should support full integer bit range for flags value
chegar
parents: 13245
diff changeset
  1873
static int getFlags(int sock, const char *ifname, int *flags) {
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1874
     struct   lifreq lifr;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1875
     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
  1876
     strcpy((caddr_t)&(lifr.lifr_name), ifname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1878
     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
  1879
         return -1;
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1880
     }
709
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1881
13564
3571e232ab03 7190254: NetworkInterface getFlags implementation should support full integer bit range for flags value
chegar
parents: 13245
diff changeset
  1882
     *flags = lifr.lifr_flags;
3571e232ab03 7190254: NetworkInterface getFlags implementation should support full integer bit range for flags value
chegar
parents: 13245
diff changeset
  1883
     return 0;
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1884
}
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1885
709
4d98e12330e6 6628569: api/java_net/MulticastSocket/descriptions.html#setTTL fails is ipv6 configured
chegar
parents: 2
diff changeset
  1886
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 2163
diff changeset
  1887
#endif
5969
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1888
76a4031d39bd 6931566: NetworkInterface is not working when interface name is more than 15 characters long
dsamersoff
parents: 5506
diff changeset
  1889
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1890
/** BSD **/
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1891
#ifdef _ALLBSD_SOURCE
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1892
/* Open socket for further ioct calls, try v4 socket first and
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1893
 * if it falls return v6 socket
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1894
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1895
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1896
#ifdef AF_INET6
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1897
static int openSocketWithFallback(JNIEnv *env, const char *ifname){
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1898
    int sock;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1899
    struct ifreq if2;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1900
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22956
diff changeset
  1901
     if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1902
         if (errno == EPROTONOSUPPORT){
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22956
diff changeset
  1903
              if ( (sock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0 ){
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1904
                 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "IPV6 Socket creation failed");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1905
                 return -1;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1906
              }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1907
         }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1908
         else{ // errno is not NOSUPPORT
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1909
             NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "IPV4 Socket creation failed");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1910
             return -1;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1911
         }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1912
   }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1913
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1914
   return sock;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1915
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1916
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1917
#else
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1918
static int openSocketWithFallback(JNIEnv *env, const char *ifname){
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1919
    return openSocket(env,AF_INET);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1920
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1921
#endif
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1922
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1923
/*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1924
 * Enumerates and returns all IPv4 interfaces
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1925
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1926
static netif *enumIPv4Interfaces(JNIEnv *env, int sock, netif *ifs) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1927
    struct ifaddrs *ifa, *origifa;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1928
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1929
    if (getifaddrs(&origifa) != 0) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1930
        NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException",
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1931
                         "getifaddrs() function failed");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1932
        return ifs;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1933
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1934
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1935
    for (ifa = origifa; ifa != NULL; ifa = ifa->ifa_next) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1936
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1937
        /*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1938
         * Skip non-AF_INET entries.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1939
         */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1940
        if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family != AF_INET)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1941
            continue;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1942
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1943
        /*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1944
         * Add to the list.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1945
         */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1946
        ifs = addif(env, sock, ifa->ifa_name, ifs, ifa->ifa_addr, AF_INET, 0);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1947
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1948
        /*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1949
         * If an exception occurred then free the list.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1950
         */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1951
        if ((*env)->ExceptionOccurred(env)) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1952
            freeifaddrs(origifa);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1953
            freeif(ifs);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1954
            return NULL;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1955
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1956
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1957
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1958
    /*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1959
     * Free socket and buffer
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1960
     */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1961
    freeifaddrs(origifa);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1962
    return ifs;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1963
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1964
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1965
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1966
/*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1967
 * Enumerates and returns all IPv6 interfaces on Linux
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1968
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1969
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1970
#ifdef AF_INET6
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1971
/*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1972
 * Determines the prefix on BSD for IPv6 interfaces.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1973
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1974
static
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1975
int prefix(void *val, int size) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1976
    u_char *name = (u_char *)val;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1977
    int byte, bit, plen = 0;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1978
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1979
    for (byte = 0; byte < size; byte++, plen += 8)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1980
        if (name[byte] != 0xff)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1981
            break;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1982
    if (byte == size)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1983
        return (plen);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1984
    for (bit = 7; bit != 0; bit--, plen++)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1985
        if (!(name[byte] & (1 << bit)))
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1986
            break;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1987
    for (; bit != 0; bit--)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1988
        if (name[byte] & (1 << bit))
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1989
            return (0);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1990
    byte++;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1991
    for (; byte < size; byte++)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1992
        if (name[byte])
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1993
            return (0);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1994
    return (plen);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1995
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1996
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1997
/*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1998
 * Enumerates and returns all IPv6 interfaces on BSD
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  1999
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2000
static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2001
    struct ifaddrs *ifa, *origifa;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2002
    struct sockaddr_in6 *sin6;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2003
    struct in6_ifreq ifr6;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2004
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2005
    if (getifaddrs(&origifa) != 0) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2006
        NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException",
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2007
                         "getifaddrs() function failed");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2008
        return ifs;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2009
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2010
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2011
    for (ifa = origifa; ifa != NULL; ifa = ifa->ifa_next) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2012
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2013
        /*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2014
         * Skip non-AF_INET6 entries.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2015
         */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2016
        if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family != AF_INET6)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2017
            continue;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2018
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2019
        memset(&ifr6, 0, sizeof(ifr6));
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2020
        strlcpy(ifr6.ifr_name, ifa->ifa_name, sizeof(ifr6.ifr_name));
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2021
        memcpy(&ifr6.ifr_addr, ifa->ifa_addr, MIN(sizeof(ifr6.ifr_addr), ifa->ifa_addr->sa_len));
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2022
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2023
        if (ioctl(sock, SIOCGIFNETMASK_IN6, (caddr_t)&ifr6) < 0) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2024
            NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException",
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2025
                             "ioctl SIOCGIFNETMASK_IN6 failed");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2026
            freeifaddrs(origifa);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2027
            freeif(ifs);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2028
            return NULL;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2029
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2030
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2031
        /* Add to the list.  */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2032
        sin6 = (struct sockaddr_in6 *)&ifr6.ifr_addr;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2033
        ifs = addif(env, sock, ifa->ifa_name, ifs, ifa->ifa_addr, AF_INET6,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2034
                    prefix(&sin6->sin6_addr, sizeof(struct in6_addr)));
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2035
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2036
        /* If an exception occurred then free the list.  */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2037
        if ((*env)->ExceptionOccurred(env)) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2038
            freeifaddrs(origifa);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2039
            freeif(ifs);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2040
            return NULL;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2041
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2042
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2043
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2044
    /*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2045
     * Free socket and ifaddrs buffer
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2046
     */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2047
    freeifaddrs(origifa);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2048
    return ifs;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2049
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2050
#endif
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2051
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2052
static int getIndex(int sock, const char *name){
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2053
#ifdef __FreeBSD__
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2054
     /*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2055
      * Try to get the interface index
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2056
      * (Not supported on Solaris 2.6 or 7)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2057
      */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2058
    struct ifreq if2;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2059
    strcpy(if2.ifr_name, name);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2060
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2061
    if (ioctl(sock, SIOCGIFINDEX, (char *)&if2) < 0) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2062
        return -1;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2063
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2064
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2065
    return if2.ifr_index;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2066
#else
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2067
    /*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2068
     * Try to get the interface index using BSD specific if_nametoindex
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2069
     */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2070
    int index = if_nametoindex(name);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2071
    return (index == 0) ? -1 : index;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2072
#endif
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2073
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2074
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2075
/**
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2076
 * Returns the IPv4 broadcast address of a named interface, if it exists.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2077
 * Returns 0 if it doesn't have one.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2078
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2079
static struct sockaddr *getBroadcast(JNIEnv *env, int sock, const char *ifname, struct sockaddr *brdcast_store) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2080
  struct sockaddr *ret = NULL;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2081
  struct ifreq if2;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2082
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2083
  memset((char *) &if2, 0, sizeof(if2));
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2084
  strcpy(if2.ifr_name, ifname);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2085
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2086
  /* Let's make sure the interface does have a broadcast address */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2087
  if (ioctl(sock, SIOCGIFFLAGS, (char *)&if2) < 0) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2088
      NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGIFFLAGS failed");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2089
      return ret;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2090
  }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2091
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2092
  if (if2.ifr_flags & IFF_BROADCAST) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2093
      /* It does, let's retrieve it*/
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2094
      if (ioctl(sock, SIOCGIFBRDADDR, (char *)&if2) < 0) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2095
          NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGIFBRDADDR failed");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2096
          return ret;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2097
      }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2098
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2099
      ret = brdcast_store;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2100
      memcpy(ret, &if2.ifr_broadaddr, sizeof(struct sockaddr));
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2101
  }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2102
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2103
  return ret;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2104
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2105
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2106
/**
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2107
 * Returns the IPv4 subnet prefix length (aka subnet mask) for the named
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2108
 * interface, if it has one, otherwise return -1.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2109
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2110
static short getSubnet(JNIEnv *env, int sock, const char *ifname) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2111
    unsigned int mask;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2112
    short ret;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2113
    struct ifreq if2;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2114
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2115
    memset((char *) &if2, 0, sizeof(if2));
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2116
    strcpy(if2.ifr_name, ifname);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2117
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2118
    if (ioctl(sock, SIOCGIFNETMASK, (char *)&if2) < 0) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2119
        NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGIFNETMASK failed");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2120
        return -1;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2121
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2122
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2123
    mask = ntohl(((struct sockaddr_in*)&(if2.ifr_addr))->sin_addr.s_addr);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2124
    ret = 0;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2125
    while (mask) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2126
       mask <<= 1;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2127
       ret++;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2128
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2129
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2130
    return ret;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2131
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2132
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2133
/**
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2134
 * Get the Hardware address (usually MAC address) for the named interface.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2135
 * return puts the data in buf, and returns the length, in byte, of the
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2136
 * MAC address. Returns -1 if there is no hardware address on that interface.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2137
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2138
static int getMacAddress(JNIEnv *env, int sock, const char* ifname, const struct in_addr* addr, unsigned char *buf) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2139
    struct ifaddrs *ifa0, *ifa;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2140
    struct sockaddr *saddr;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2141
    int i;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2142
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2143
    /* Grab the interface list */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2144
    if (!getifaddrs(&ifa0)) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2145
        /* Cycle through the interfaces */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2146
        for (i = 0, ifa = ifa0; ifa != NULL; ifa = ifa->ifa_next, i++) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2147
            saddr = ifa->ifa_addr;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2148
            /* Link layer contains the MAC address */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2149
            if (saddr->sa_family == AF_LINK && !strcmp(ifname, ifa->ifa_name)) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2150
                struct sockaddr_dl *sadl = (struct sockaddr_dl *) saddr;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2151
                /* Check the address is the correct length */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2152
                if (sadl->sdl_alen == ETHER_ADDR_LEN) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2153
                    memcpy(buf, (sadl->sdl_data + sadl->sdl_nlen), ETHER_ADDR_LEN);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2154
                    freeifaddrs(ifa0);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2155
                    return ETHER_ADDR_LEN;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2156
                }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2157
            }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2158
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2159
        freeifaddrs(ifa0);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2160
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2161
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2162
    return -1;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2163
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2164
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2165
static int getMTU(JNIEnv *env, int sock,  const char *ifname) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2166
    struct ifreq if2;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2167
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2168
    memset((char *) &if2, 0, sizeof(if2));
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2169
    strcpy(if2.ifr_name, ifname);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2170
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2171
    if (ioctl(sock, SIOCGIFMTU, (char *)&if2) < 0) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2172
        NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGIFMTU failed");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2173
        return -1;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2174
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2175
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2176
    return  if2.ifr_mtu;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2177
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2178
13564
3571e232ab03 7190254: NetworkInterface getFlags implementation should support full integer bit range for flags value
chegar
parents: 13245
diff changeset
  2179
static int getFlags(int sock, const char *ifname, int *flags) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2180
  struct ifreq if2;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2181
  int ret = -1;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2182
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2183
  memset((char *) &if2, 0, sizeof(if2));
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2184
  strcpy(if2.ifr_name, ifname);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2185
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2186
  if (ioctl(sock, SIOCGIFFLAGS, (char *)&if2) < 0){
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2187
      return -1;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2188
  }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2189
13564
3571e232ab03 7190254: NetworkInterface getFlags implementation should support full integer bit range for flags value
chegar
parents: 13245
diff changeset
  2190
  if (sizeof(if2.ifr_flags) == sizeof(short)) {
3571e232ab03 7190254: NetworkInterface getFlags implementation should support full integer bit range for flags value
chegar
parents: 13245
diff changeset
  2191
    *flags = (if2.ifr_flags & 0xffff);
3571e232ab03 7190254: NetworkInterface getFlags implementation should support full integer bit range for flags value
chegar
parents: 13245
diff changeset
  2192
  } else {
3571e232ab03 7190254: NetworkInterface getFlags implementation should support full integer bit range for flags value
chegar
parents: 13245
diff changeset
  2193
    *flags = if2.ifr_flags;
3571e232ab03 7190254: NetworkInterface getFlags implementation should support full integer bit range for flags value
chegar
parents: 13245
diff changeset
  2194
  }
3571e232ab03 7190254: NetworkInterface getFlags implementation should support full integer bit range for flags value
chegar
parents: 13245
diff changeset
  2195
  return 0;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2196
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2197
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11355
diff changeset
  2198
#endif