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