src/java.base/share/classes/java/net/NetworkInterface.java
author alanb
Mon, 29 Apr 2019 09:03:06 +0100
branchniosocketimpl-branch
changeset 57341 733e9746d615
parent 52499 768b1c612100
child 55596 d01b345865d7
child 58678 9cf78a70fa4f
permissions -rw-r--r--
More cleanup and test improvements
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
52157
7593b9b8e8f2 8199110: Address Internet Addresses
vtewari
parents: 47216
diff changeset
     2
 * Copyright (c) 2000, 2018, 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: 5293
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: 5293
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: 5293
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5293
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5293
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
package java.net;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
31469
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
    28
import java.util.Arrays;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.NoSuchElementException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.AccessController;
31469
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
    32
import java.util.Spliterator;
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
    33
import java.util.Spliterators;
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
    34
import java.util.stream.Stream;
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
    35
import java.util.stream.StreamSupport;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * This class represents a Network Interface made up of a name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * and a list of IP addresses assigned to this interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * It is used to identify the local interface on which a multicast group
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * is joined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * Interfaces are normally known by names such as "le0".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
public final class NetworkInterface {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private String displayName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private InetAddress addrs[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private InterfaceAddress bindings[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private NetworkInterface childs[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private NetworkInterface parent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private boolean virtual = false;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9842
diff changeset
    56
    private static final NetworkInterface defaultInterface;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9842
diff changeset
    57
    private static final int defaultIndex; /* index of defaultInterface */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    static {
12559
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 12047
diff changeset
    60
        AccessController.doPrivileged(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 25859
diff changeset
    61
            new java.security.PrivilegedAction<>() {
12559
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 12047
diff changeset
    62
                public Void run() {
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 12047
diff changeset
    63
                    System.loadLibrary("net");
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 12047
diff changeset
    64
                    return null;
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 12047
diff changeset
    65
                }
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 12047
diff changeset
    66
            });
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 12047
diff changeset
    67
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        init();
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9842
diff changeset
    69
        defaultInterface = DefaultInterface.getDefault();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9842
diff changeset
    70
        if (defaultInterface != null) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9842
diff changeset
    71
            defaultIndex = defaultInterface.getIndex();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9842
diff changeset
    72
        } else {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9842
diff changeset
    73
            defaultIndex = 0;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9842
diff changeset
    74
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Returns an NetworkInterface object with index set to 0 and name to null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Setting such an interface on a MulticastSocket will cause the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * kernel to choose one interface for sending multicast packets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    NetworkInterface() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    NetworkInterface(String name, int index, InetAddress[] addrs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        this.index = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        this.addrs = addrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Get the name of this network interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @return the name of this network interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
31469
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   102
     * Get an Enumeration with all or a subset of the InetAddresses bound to
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   103
     * this network interface.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * <p>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   105
     * If there is a security manager, its {@code checkConnect}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * method is called for each InetAddress. Only InetAddresses where
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   107
     * the {@code checkConnect} doesn't throw a SecurityException
6872
b90e4fee471f 6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents: 5506
diff changeset
   108
     * will be returned in the Enumeration. However, if the caller has the
b90e4fee471f 6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents: 5506
diff changeset
   109
     * {@link NetPermission}("getNetworkInformation") permission, then all
b90e4fee471f 6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents: 5506
diff changeset
   110
     * InetAddresses are returned.
31469
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   111
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @return an Enumeration object with all or a subset of the InetAddresses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * bound to this network interface
31469
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   114
     * @see #inetAddresses()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public Enumeration<InetAddress> getInetAddresses() {
31469
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   117
        return enumerationFromArray(getCheckedInetAddresses());
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   118
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
31469
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   120
    /**
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   121
     * Get a Stream of all or a subset of the InetAddresses bound to this
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   122
     * network interface.
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   123
     * <p>
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   124
     * If there is a security manager, its {@code checkConnect}
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   125
     * method is called for each InetAddress. Only InetAddresses where
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   126
     * the {@code checkConnect} doesn't throw a SecurityException will be
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   127
     * returned in the Stream. However, if the caller has the
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   128
     * {@link NetPermission}("getNetworkInformation") permission, then all
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   129
     * InetAddresses are returned.
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   130
     *
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   131
     * @return a Stream object with all or a subset of the InetAddresses
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   132
     * bound to this network interface
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 32649
diff changeset
   133
     * @since 9
31469
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   134
     */
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   135
    public Stream<InetAddress> inetAddresses() {
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   136
        return streamFromArray(getCheckedInetAddresses());
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   137
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
31469
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   139
    private InetAddress[] getCheckedInetAddresses() {
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   140
        InetAddress[] local_addrs = new InetAddress[addrs.length];
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   141
        boolean trusted = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
31469
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   143
        SecurityManager sec = System.getSecurityManager();
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   144
        if (sec != null) {
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   145
            try {
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   146
                sec.checkPermission(new NetPermission("getNetworkInformation"));
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   147
            } catch (SecurityException e) {
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   148
                trusted = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
31469
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   151
        int i = 0;
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   152
        for (int j = 0; j < addrs.length; j++) {
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   153
            try {
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   154
                if (!trusted) {
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   155
                    sec.checkConnect(addrs[j].getHostAddress(), -1);
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   156
                }
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   157
                local_addrs[i++] = addrs[j];
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   158
            } catch (SecurityException e) { }
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   159
        }
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   160
        return Arrays.copyOf(local_addrs, i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   164
     * Get a List of all or a subset of the {@code InterfaceAddresses}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * of this network interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * <p>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   167
     * If there is a security manager, its {@code checkConnect}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * method is called with the InetAddress for each InterfaceAddress.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   169
     * Only InterfaceAddresses where the {@code checkConnect} doesn't throw
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * a SecurityException will be returned in the List.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   172
     * @return a {@code List} object with all or a subset of the
52499
768b1c612100 8213490: Networking area typos and inconsistencies cleanup
prappo
parents: 52157
diff changeset
   173
     *         InterfaceAddress of this network interface
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    public java.util.List<InterfaceAddress> getInterfaceAddresses() {
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 25859
diff changeset
   177
        java.util.List<InterfaceAddress> lst = new java.util.ArrayList<>(1);
44105
6ff4cfe526be 8175325: NetworkInterface.getInterfaceAddresses throws NPE when no addresses
msheppar
parents: 41969
diff changeset
   178
        if (bindings != null) {
6ff4cfe526be 8175325: NetworkInterface.getInterfaceAddresses throws NPE when no addresses
msheppar
parents: 41969
diff changeset
   179
            SecurityManager sec = System.getSecurityManager();
6ff4cfe526be 8175325: NetworkInterface.getInterfaceAddresses throws NPE when no addresses
msheppar
parents: 41969
diff changeset
   180
            for (int j=0; j<bindings.length; j++) {
6ff4cfe526be 8175325: NetworkInterface.getInterfaceAddresses throws NPE when no addresses
msheppar
parents: 41969
diff changeset
   181
                try {
6ff4cfe526be 8175325: NetworkInterface.getInterfaceAddresses throws NPE when no addresses
msheppar
parents: 41969
diff changeset
   182
                    if (sec != null) {
6ff4cfe526be 8175325: NetworkInterface.getInterfaceAddresses throws NPE when no addresses
msheppar
parents: 41969
diff changeset
   183
                        sec.checkConnect(bindings[j].getAddress().getHostAddress(), -1);
6ff4cfe526be 8175325: NetworkInterface.getInterfaceAddresses throws NPE when no addresses
msheppar
parents: 41969
diff changeset
   184
                    }
6ff4cfe526be 8175325: NetworkInterface.getInterfaceAddresses throws NPE when no addresses
msheppar
parents: 41969
diff changeset
   185
                    lst.add(bindings[j]);
6ff4cfe526be 8175325: NetworkInterface.getInterfaceAddresses throws NPE when no addresses
msheppar
parents: 41969
diff changeset
   186
                } catch (SecurityException e) { }
6ff4cfe526be 8175325: NetworkInterface.getInterfaceAddresses throws NPE when no addresses
msheppar
parents: 41969
diff changeset
   187
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        return lst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * Get an Enumeration with all the subinterfaces (also known as virtual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * interfaces) attached to this network interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * For instance eth0:1 will be a subinterface to eth0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @return an Enumeration object with all of the subinterfaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * of this network interface
31469
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   200
     * @see #subInterfaces()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public Enumeration<NetworkInterface> getSubInterfaces() {
31469
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   204
        return enumerationFromArray(childs);
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   205
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
31469
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   207
    /**
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   208
     * Get a Stream of all subinterfaces (also known as virtual
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   209
     * interfaces) attached to this network interface.
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   210
     *
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   211
     * @return a Stream object with all of the subinterfaces
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   212
     * of this network interface
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 32649
diff changeset
   213
     * @since 9
31469
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   214
     */
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   215
    public Stream<NetworkInterface> subInterfaces() {
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   216
        return streamFromArray(childs);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * Returns the parent NetworkInterface of this interface if this is
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   221
     * a subinterface, or {@code null} if it is a physical
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * (non virtual) interface or has no parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   224
     * @return The {@code NetworkInterface} this interface is attached to.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    public NetworkInterface getParent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        return parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    /**
1097
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   232
     * Returns the index of this network interface. The index is an integer greater
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   233
     * or equal to zero, or {@code -1} for unknown. This is a system specific value
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   234
     * and interfaces with the same name can have different indexes on different
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   235
     * machines.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *
1097
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   237
     * @return the index of this network interface or {@code -1} if the index is
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   238
     *         unknown
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   239
     * @see #getByIndex(int)
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   240
     * @since 1.7
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     */
1097
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   242
    public int getIndex() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * Get the display name of this network interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * A display name is a human readable String describing the network
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * device.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *
5293
be5f9ec7ca3b 6706251: api/java_net/NetworkInterface/index.html#misc: getDisplayName() returned non null but empty String
chegar
parents: 5180
diff changeset
   251
     * @return a non-empty string representing the display name of this network
be5f9ec7ca3b 6706251: api/java_net/NetworkInterface/index.html#misc: getDisplayName() returned non null but empty String
chegar
parents: 5180
diff changeset
   252
     *         interface, or null if no display name is available.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    public String getDisplayName() {
5293
be5f9ec7ca3b 6706251: api/java_net/NetworkInterface/index.html#misc: getDisplayName() returned non null but empty String
chegar
parents: 5180
diff changeset
   255
        /* strict TCK conformance */
be5f9ec7ca3b 6706251: api/java_net/NetworkInterface/index.html#misc: getDisplayName() returned non null but empty String
chegar
parents: 5180
diff changeset
   256
        return "".equals(displayName) ? null : displayName;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * Searches for the network interface with the specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @param   name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *          The name of the network interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   265
     * @return  A {@code NetworkInterface} with the specified name,
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   266
     *          or {@code null} if there is no network interface
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *          with the specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @throws  SocketException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *          If an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @throws  NullPointerException
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   273
     *          If the specified name is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    public static NetworkInterface getByName(String name) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        if (name == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        return getByName0(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * Get a network interface given its index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * @param index an integer, the index of the interface
1097
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   285
     * @return the NetworkInterface obtained from its index, or {@code null} if
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   286
     *         there is no interface with such an index on the system
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   287
     * @throws  SocketException  if an I/O error occurs.
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   288
     * @throws  IllegalArgumentException if index has a negative value
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   289
     * @see #getIndex()
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   290
     * @since 1.7
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     */
1097
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   292
    public static NetworkInterface getByIndex(int index) throws SocketException {
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   293
        if (index < 0)
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   294
            throw new IllegalArgumentException("Interface index can't be negative");
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   295
        return getByIndex0(index);
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   296
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * Convenience method to search for a network interface that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * has the specified Internet Protocol (IP) address bound to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * If the specified IP address is bound to multiple network
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * interfaces it is not defined which network interface is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * @param   addr
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   308
     *          The {@code InetAddress} to search with.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   310
     * @return  A {@code NetworkInterface}
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   311
     *          or {@code null} if there is no network interface
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     *          with the specified IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @throws  SocketException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     *          If an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * @throws  NullPointerException
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   318
     *          If the specified address is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    public static NetworkInterface getByInetAddress(InetAddress addr) throws SocketException {
5180
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 1152
diff changeset
   321
        if (addr == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            throw new NullPointerException();
5180
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 1152
diff changeset
   323
        }
52157
7593b9b8e8f2 8199110: Address Internet Addresses
vtewari
parents: 47216
diff changeset
   324
        if (addr instanceof Inet4Address) {
7593b9b8e8f2 8199110: Address Internet Addresses
vtewari
parents: 47216
diff changeset
   325
            Inet4Address inet4Address = (Inet4Address) addr;
7593b9b8e8f2 8199110: Address Internet Addresses
vtewari
parents: 47216
diff changeset
   326
            if (inet4Address.holder.family != InetAddress.IPv4) {
7593b9b8e8f2 8199110: Address Internet Addresses
vtewari
parents: 47216
diff changeset
   327
                throw new IllegalArgumentException("invalid family type: "
7593b9b8e8f2 8199110: Address Internet Addresses
vtewari
parents: 47216
diff changeset
   328
                        + inet4Address.holder.family);
7593b9b8e8f2 8199110: Address Internet Addresses
vtewari
parents: 47216
diff changeset
   329
            }
7593b9b8e8f2 8199110: Address Internet Addresses
vtewari
parents: 47216
diff changeset
   330
        } else if (addr instanceof Inet6Address) {
7593b9b8e8f2 8199110: Address Internet Addresses
vtewari
parents: 47216
diff changeset
   331
            Inet6Address inet6Address = (Inet6Address) addr;
7593b9b8e8f2 8199110: Address Internet Addresses
vtewari
parents: 47216
diff changeset
   332
            if (inet6Address.holder.family != InetAddress.IPv6) {
7593b9b8e8f2 8199110: Address Internet Addresses
vtewari
parents: 47216
diff changeset
   333
                throw new IllegalArgumentException("invalid family type: "
7593b9b8e8f2 8199110: Address Internet Addresses
vtewari
parents: 47216
diff changeset
   334
                        + inet6Address.holder.family);
7593b9b8e8f2 8199110: Address Internet Addresses
vtewari
parents: 47216
diff changeset
   335
            }
7593b9b8e8f2 8199110: Address Internet Addresses
vtewari
parents: 47216
diff changeset
   336
        } else {
7593b9b8e8f2 8199110: Address Internet Addresses
vtewari
parents: 47216
diff changeset
   337
            throw new IllegalArgumentException("invalid address type: " + addr);
5180
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 1152
diff changeset
   338
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        return getByInetAddress0(addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    /**
31469
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   343
     * Returns an {@code Enumeration} of all the interfaces on this machine. The
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   344
     * {@code Enumeration} contains at least one element, possibly representing
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   345
     * a loopback interface that only supports communication between entities on
13374
2a3bf352ef97 7120665: Change Java SE spec so that external networking not required
michaelm
parents: 12559
diff changeset
   346
     * this machine.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     *
31469
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   348
     * @apiNote this method can be used in combination with
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   349
     * {@link #getInetAddresses()} to obtain all IP addresses for this node
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * @return an Enumeration of NetworkInterfaces found on this machine
41969
e4c2f100927f 8164815: 3 JCK NetworkInterface tests fail on Raspberry Pi
msheppar
parents: 35302
diff changeset
   352
     * @exception  SocketException  if an I/O error occurs,
e4c2f100927f 8164815: 3 JCK NetworkInterface tests fail on Raspberry Pi
msheppar
parents: 35302
diff changeset
   353
     *             or if the platform does not have at least one configured
e4c2f100927f 8164815: 3 JCK NetworkInterface tests fail on Raspberry Pi
msheppar
parents: 35302
diff changeset
   354
     *             network interface.
31469
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   355
     * @see #networkInterfaces()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    public static Enumeration<NetworkInterface> getNetworkInterfaces()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        throws SocketException {
31469
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   359
        NetworkInterface[] netifs = getAll();
41969
e4c2f100927f 8164815: 3 JCK NetworkInterface tests fail on Raspberry Pi
msheppar
parents: 35302
diff changeset
   360
        if (netifs != null && netifs.length > 0) {
e4c2f100927f 8164815: 3 JCK NetworkInterface tests fail on Raspberry Pi
msheppar
parents: 35302
diff changeset
   361
            return enumerationFromArray(netifs);
e4c2f100927f 8164815: 3 JCK NetworkInterface tests fail on Raspberry Pi
msheppar
parents: 35302
diff changeset
   362
        } else {
e4c2f100927f 8164815: 3 JCK NetworkInterface tests fail on Raspberry Pi
msheppar
parents: 35302
diff changeset
   363
            throw new SocketException("No network interfaces configured");
e4c2f100927f 8164815: 3 JCK NetworkInterface tests fail on Raspberry Pi
msheppar
parents: 35302
diff changeset
   364
        }
31469
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   365
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
31469
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   367
    /**
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   368
     * Returns a {@code Stream} of all the interfaces on this machine.  The
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   369
     * {@code Stream} contains at least one interface, possibly representing a
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   370
     * loopback interface that only supports communication between entities on
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   371
     * this machine.
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   372
     *
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   373
     * @apiNote this method can be used in combination with
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   374
     * {@link #inetAddresses()}} to obtain a stream of all IP addresses for
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   375
     * this node, for example:
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   376
     * <pre> {@code
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   377
     * Stream<InetAddress> addrs = NetworkInterface.networkInterfaces()
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   378
     *     .flatMap(NetworkInterface::inetAddresses);
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   379
     * }</pre>
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   380
     *
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   381
     * @return a Stream of NetworkInterfaces found on this machine
41969
e4c2f100927f 8164815: 3 JCK NetworkInterface tests fail on Raspberry Pi
msheppar
parents: 35302
diff changeset
   382
     * @exception  SocketException  if an I/O error occurs,
e4c2f100927f 8164815: 3 JCK NetworkInterface tests fail on Raspberry Pi
msheppar
parents: 35302
diff changeset
   383
     *             or if the platform does not have at least one configured
e4c2f100927f 8164815: 3 JCK NetworkInterface tests fail on Raspberry Pi
msheppar
parents: 35302
diff changeset
   384
     *             network interface.
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 32649
diff changeset
   385
     * @since 9
31469
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   386
     */
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   387
    public static Stream<NetworkInterface> networkInterfaces()
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   388
        throws SocketException {
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   389
        NetworkInterface[] netifs = getAll();
41969
e4c2f100927f 8164815: 3 JCK NetworkInterface tests fail on Raspberry Pi
msheppar
parents: 35302
diff changeset
   390
        if (netifs != null && netifs.length > 0) {
e4c2f100927f 8164815: 3 JCK NetworkInterface tests fail on Raspberry Pi
msheppar
parents: 35302
diff changeset
   391
            return streamFromArray(netifs);
e4c2f100927f 8164815: 3 JCK NetworkInterface tests fail on Raspberry Pi
msheppar
parents: 35302
diff changeset
   392
        }  else {
e4c2f100927f 8164815: 3 JCK NetworkInterface tests fail on Raspberry Pi
msheppar
parents: 35302
diff changeset
   393
            throw new SocketException("No network interfaces configured");
e4c2f100927f 8164815: 3 JCK NetworkInterface tests fail on Raspberry Pi
msheppar
parents: 35302
diff changeset
   394
        }
31469
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   395
    }
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   396
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   397
    private static <T> Enumeration<T> enumerationFromArray(T[] a) {
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 25859
diff changeset
   398
        return new Enumeration<>() {
31469
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   399
            int i = 0;
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   400
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   401
            @Override
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   402
            public T nextElement() {
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   403
                if (i < a.length) {
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   404
                    return a[i++];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                    throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
31469
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   410
            @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            public boolean hasMoreElements() {
31469
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   412
                return i < a.length;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
31469
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   417
    private static <T> Stream<T> streamFromArray(T[] a) {
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   418
        return StreamSupport.stream(
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   419
                Spliterators.spliterator(
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   420
                        a,
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   421
                        Spliterator.DISTINCT | Spliterator.IMMUTABLE | Spliterator.NONNULL),
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   422
                false);
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   423
    }
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 29986
diff changeset
   424
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31469
diff changeset
   425
    private static native NetworkInterface[] getAll()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31469
diff changeset
   428
    private static native NetworkInterface getByName0(String name)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31469
diff changeset
   431
    private static native NetworkInterface getByIndex0(int index)
1097
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   432
        throws SocketException;
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   433
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31469
diff changeset
   434
    private static native NetworkInterface getByInetAddress0(InetAddress addr)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * Returns whether a network interface is up and running.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   440
     * @return  {@code true} if the interface is up and running.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * @exception       SocketException if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    public boolean isUp() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        return isUp0(name, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * Returns whether a network interface is a loopback interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   452
     * @return  {@code true} if the interface is a loopback interface.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * @exception       SocketException if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    public boolean isLoopback() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        return isLoopback0(name, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * Returns whether a network interface is a point to point interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * A typical point to point interface would be a PPP connection through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * a modem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   466
     * @return  {@code true} if the interface is a point to point
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     *          interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * @exception       SocketException if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    public boolean isPointToPoint() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        return isP2P0(name, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * Returns whether a network interface supports multicasting or not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   479
     * @return  {@code true} if the interface supports Multicasting.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * @exception       SocketException if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    public boolean supportsMulticast() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        return supportsMulticast0(name, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * Returns the hardware address (usually MAC) of the interface if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * has one and if it can be accessed given the current privileges.
6872
b90e4fee471f 6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents: 5506
diff changeset
   491
     * If a security manager is set, then the caller must have
b90e4fee471f 6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents: 5506
diff changeset
   492
     * the permission {@link NetPermission}("getNetworkInformation").
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   494
     * @return  a byte array containing the address, or {@code null} if
6872
b90e4fee471f 6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents: 5506
diff changeset
   495
     *          the address doesn't exist, is not accessible or a security
b90e4fee471f 6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents: 5506
diff changeset
   496
     *          manager is set and the caller does not have the permission
b90e4fee471f 6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents: 5506
diff changeset
   497
     *          NetPermission("getNetworkInformation")
b90e4fee471f 6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents: 5506
diff changeset
   498
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * @exception       SocketException if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    public byte[] getHardwareAddress() throws SocketException {
6872
b90e4fee471f 6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents: 5506
diff changeset
   503
        SecurityManager sec = System.getSecurityManager();
b90e4fee471f 6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents: 5506
diff changeset
   504
        if (sec != null) {
b90e4fee471f 6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents: 5506
diff changeset
   505
            try {
b90e4fee471f 6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents: 5506
diff changeset
   506
                sec.checkPermission(new NetPermission("getNetworkInformation"));
b90e4fee471f 6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents: 5506
diff changeset
   507
            } catch (SecurityException e) {
b90e4fee471f 6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents: 5506
diff changeset
   508
                if (!getInetAddresses().hasMoreElements()) {
b90e4fee471f 6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents: 5506
diff changeset
   509
                    // don't have connect permission to any local address
b90e4fee471f 6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents: 5506
diff changeset
   510
                    return null;
b90e4fee471f 6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents: 5506
diff changeset
   511
                }
b90e4fee471f 6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents: 5506
diff changeset
   512
            }
b90e4fee471f 6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents: 5506
diff changeset
   513
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        for (InetAddress addr : addrs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            if (addr instanceof Inet4Address) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                return getMacAddr0(((Inet4Address)addr).getAddress(), name, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        return getMacAddr0(null, name, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * Returns the Maximum Transmission Unit (MTU) of this interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * @return the value of the MTU for that interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * @exception       SocketException if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    public int getMTU() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        return getMTU0(name, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * Returns whether this interface is a virtual interface (also called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * subinterface).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * Virtual interfaces are, on some systems, interfaces created as a child
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * of a physical interface and given different settings (like address or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * MTU). Usually the name of the interface will the name of the parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * followed by a colon (:) and a number identifying the child since there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * can be several virtual interfaces attached to a single physical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   543
     * @return {@code true} if this interface is a virtual interface.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    public boolean isVirtual() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        return virtual;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31469
diff changeset
   550
    private static native boolean isUp0(String name, int ind) throws SocketException;
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31469
diff changeset
   551
    private static native boolean isLoopback0(String name, int ind) throws SocketException;
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31469
diff changeset
   552
    private static native boolean supportsMulticast0(String name, int ind) throws SocketException;
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31469
diff changeset
   553
    private static native boolean isP2P0(String name, int ind) throws SocketException;
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31469
diff changeset
   554
    private static native byte[] getMacAddr0(byte[] inAddr, String name, int ind) throws SocketException;
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31469
diff changeset
   555
    private static native int getMTU0(String name, int ind) throws SocketException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * Compares this object against the specified object.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   559
     * The result is {@code true} if and only if the argument is
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   560
     * not {@code null} and it represents the same NetworkInterface
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * as this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * <p>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   563
     * Two instances of {@code NetworkInterface} represent the same
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * NetworkInterface if both name and addrs are the same for both.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * @param   obj   the object to compare against.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   567
     * @return  {@code true} if the objects are the same;
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   568
     *          {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * @see     java.net.InetAddress#getAddress()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    public boolean equals(Object obj) {
7986
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   572
        if (!(obj instanceof NetworkInterface)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        }
7986
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   575
        NetworkInterface that = (NetworkInterface)obj;
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   576
        if (this.name != null ) {
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   577
            if (!this.name.equals(that.name)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        } else {
7986
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   581
            if (that.name != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        }
7986
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   585
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   586
        if (this.addrs == null) {
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   587
            return that.addrs == null;
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   588
        } else if (that.addrs == null) {
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   589
            return false;
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   590
        }
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   591
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   592
        /* Both addrs not null. Compare number of addresses */
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   593
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   594
        if (this.addrs.length != that.addrs.length) {
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   595
            return false;
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   596
        }
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   597
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   598
        InetAddress[] thatAddrs = that.addrs;
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   599
        int count = thatAddrs.length;
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   600
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   601
        for (int i=0; i<count; i++) {
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   602
            boolean found = false;
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   603
            for (int j=0; j<count; j++) {
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   604
                if (addrs[i].equals(thatAddrs[j])) {
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   605
                    found = true;
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   606
                    break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            }
7986
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   609
            if (!found) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    public int hashCode() {
7986
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   617
        return name == null? 0: name.hashCode();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        String result = "name:";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        result += name == null? "null": name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        if (displayName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            result += " (" + displayName + ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    }
9828
9c9dd9a1b11b 7013969: NetworkInterface.toString can reveal bindings
chegar
parents: 7986
diff changeset
   628
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    private static native void init();
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9842
diff changeset
   630
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9842
diff changeset
   631
    /**
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9842
diff changeset
   632
     * Returns the default network interface of this system
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9842
diff changeset
   633
     *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9842
diff changeset
   634
     * @return the default interface
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9842
diff changeset
   635
     */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9842
diff changeset
   636
    static NetworkInterface getDefault() {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9842
diff changeset
   637
        return defaultInterface;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9842
diff changeset
   638
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
}