jdk/src/share/classes/java/net/NetworkInterface.java
author mchung
Tue, 01 May 2012 19:45:34 -0700
changeset 12559 9456ceada8b1
parent 12047 320a714614e9
child 13374 2a3bf352ef97
permissions -rw-r--r--
7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary Reviewed-by: alanb, mullan, prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 7986
diff changeset
     2
 * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.NoSuchElementException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import sun.security.action.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * This class represents a Network Interface made up of a name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * and a list of IP addresses assigned to this interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * It is used to identify the local interface on which a multicast group
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * is joined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Interfaces are normally known by names such as "le0".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public final class NetworkInterface {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private String displayName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private InetAddress addrs[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private InterfaceAddress bindings[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private NetworkInterface childs[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private NetworkInterface parent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private boolean virtual = false;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9842
diff changeset
    52
    private static final NetworkInterface defaultInterface;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9842
diff changeset
    53
    private static final int defaultIndex; /* index of defaultInterface */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    static {
12559
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 12047
diff changeset
    56
        AccessController.doPrivileged(
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 12047
diff changeset
    57
            new java.security.PrivilegedAction<Void>() {
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 12047
diff changeset
    58
                public Void run() {
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 12047
diff changeset
    59
                    System.loadLibrary("net");
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 12047
diff changeset
    60
                    return null;
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 12047
diff changeset
    61
                }
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 12047
diff changeset
    62
            });
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 12047
diff changeset
    63
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        init();
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9842
diff changeset
    65
        defaultInterface = DefaultInterface.getDefault();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9842
diff changeset
    66
        if (defaultInterface != null) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9842
diff changeset
    67
            defaultIndex = defaultInterface.getIndex();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9842
diff changeset
    68
        } else {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9842
diff changeset
    69
            defaultIndex = 0;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9842
diff changeset
    70
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * Returns an NetworkInterface object with index set to 0 and name to null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Setting such an interface on a MulticastSocket will cause the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * kernel to choose one interface for sending multicast packets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    NetworkInterface() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    NetworkInterface(String name, int index, InetAddress[] addrs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        this.index = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        this.addrs = addrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Get the name of this network interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @return the name of this network interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * Convenience method to return an Enumeration with all or a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * subset of the InetAddresses bound to this network interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * If there is a security manager, its <code>checkConnect</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * method is called for each InetAddress. Only InetAddresses where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * the <code>checkConnect</code> doesn't throw a SecurityException
6872
b90e4fee471f 6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents: 5506
diff changeset
   104
     * 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
   105
     * {@link NetPermission}("getNetworkInformation") permission, then all
b90e4fee471f 6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents: 5506
diff changeset
   106
     * InetAddresses are returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @return an Enumeration object with all or a subset of the InetAddresses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * bound to this network interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public Enumeration<InetAddress> getInetAddresses() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        class checkedAddresses implements Enumeration<InetAddress> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            private int i=0, count=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            private InetAddress local_addrs[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            checkedAddresses() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                local_addrs = new InetAddress[addrs.length];
6872
b90e4fee471f 6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents: 5506
diff changeset
   119
                boolean trusted = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                SecurityManager sec = System.getSecurityManager();
6872
b90e4fee471f 6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents: 5506
diff changeset
   122
                if (sec != null) {
b90e4fee471f 6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents: 5506
diff changeset
   123
                    try {
b90e4fee471f 6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents: 5506
diff changeset
   124
                        sec.checkPermission(new NetPermission("getNetworkInformation"));
b90e4fee471f 6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents: 5506
diff changeset
   125
                    } catch (SecurityException e) {
b90e4fee471f 6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents: 5506
diff changeset
   126
                        trusted = false;
b90e4fee471f 6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents: 5506
diff changeset
   127
                    }
b90e4fee471f 6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents: 5506
diff changeset
   128
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                for (int j=0; j<addrs.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    try {
6872
b90e4fee471f 6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents: 5506
diff changeset
   131
                        if (sec != null && !trusted) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                            sec.checkConnect(addrs[j].getHostAddress(), -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                        local_addrs[count++] = addrs[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    } catch (SecurityException e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            public InetAddress nextElement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                if (i < count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    return local_addrs[i++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                return (i < count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        return new checkedAddresses();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Get a List of all or a subset of the <code>InterfaceAddresses</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * of this network interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * If there is a security manager, its <code>checkConnect</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * method is called with the InetAddress for each InterfaceAddress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * Only InterfaceAddresses where the <code>checkConnect</code> doesn't throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * a SecurityException will be returned in the List.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @return a <code>List</code> object with all or a subset of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *         InterfaceAddresss of this network interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public java.util.List<InterfaceAddress> getInterfaceAddresses() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        java.util.List<InterfaceAddress> lst = new java.util.ArrayList<InterfaceAddress>(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        SecurityManager sec = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        for (int j=0; j<bindings.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                if (sec != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    sec.checkConnect(bindings[j].getAddress().getHostAddress(), -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                lst.add(bindings[j]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            } catch (SecurityException e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        return lst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Get an Enumeration with all the subinterfaces (also known as virtual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * interfaces) attached to this network interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * For instance eth0:1 will be a subinterface to eth0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @return an Enumeration object with all of the subinterfaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * of this network interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    public Enumeration<NetworkInterface> getSubInterfaces() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        class subIFs implements Enumeration<NetworkInterface> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            private int i=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            subIFs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            public NetworkInterface nextElement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                if (i < childs.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                    return childs[i++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                    throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                return (i < childs.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        return new subIFs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * Returns the parent NetworkInterface of this interface if this is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * a subinterface, or <code>null</code> if it is a physical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * (non virtual) interface or has no parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @return The <code>NetworkInterface</code> this interface is attached to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    public NetworkInterface getParent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        return parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    /**
1097
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   230
     * 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
   231
     * 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
   232
     * 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
   233
     * machines.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
1097
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   235
     * @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
   236
     *         unknown
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   237
     * @see #getByIndex(int)
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   238
     * @since 1.7
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     */
1097
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   240
    public int getIndex() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * Get the display name of this network interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * A display name is a human readable String describing the network
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * device.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *
5293
be5f9ec7ca3b 6706251: api/java_net/NetworkInterface/index.html#misc: getDisplayName() returned non null but empty String
chegar
parents: 5180
diff changeset
   249
     * @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
   250
     *         interface, or null if no display name is available.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    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
   253
        /* strict TCK conformance */
be5f9ec7ca3b 6706251: api/java_net/NetworkInterface/index.html#misc: getDisplayName() returned non null but empty String
chegar
parents: 5180
diff changeset
   254
        return "".equals(displayName) ? null : displayName;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * Searches for the network interface with the specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @param   name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *          The name of the network interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @return  A <tt>NetworkInterface</tt> with the specified name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *          or <tt>null</tt> if there is no network interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *          with the specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @throws  SocketException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *          If an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @throws  NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *          If the specified name is <tt>null</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    public static NetworkInterface getByName(String name) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        if (name == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        return getByName0(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * Get a network interface given its index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * @param index an integer, the index of the interface
1097
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   283
     * @return the NetworkInterface obtained from its index, or {@code null} if
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   284
     *         there is no interface with such an index on the system
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   285
     * @throws  SocketException  if an I/O error occurs.
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   286
     * @throws  IllegalArgumentException if index has a negative value
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   287
     * @see #getIndex()
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   288
     * @since 1.7
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     */
1097
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   290
    public static NetworkInterface getByIndex(int index) throws SocketException {
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   291
        if (index < 0)
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   292
            throw new IllegalArgumentException("Interface index can't be negative");
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   293
        return getByIndex0(index);
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   294
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * Convenience method to search for a network interface that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * has the specified Internet Protocol (IP) address bound to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * If the specified IP address is bound to multiple network
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * interfaces it is not defined which network interface is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * @param   addr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *          The <tt>InetAddress</tt> to search with.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * @return  A <tt>NetworkInterface</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *          or <tt>null</tt> if there is no network interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     *          with the specified IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @throws  SocketException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *          If an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @throws  NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *          If the specified address is <tt>null</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    public static NetworkInterface getByInetAddress(InetAddress addr) throws SocketException {
5180
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 1152
diff changeset
   319
        if (addr == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            throw new NullPointerException();
5180
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 1152
diff changeset
   321
        }
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 1152
diff changeset
   322
        if (!(addr instanceof Inet4Address || addr instanceof Inet6Address)) {
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 1152
diff changeset
   323
            throw new IllegalArgumentException ("invalid address type");
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 1152
diff changeset
   324
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        return getByInetAddress0(addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * Returns all the interfaces on this machine. Returns null if no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * network interfaces could be found on this machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * NOTE: can use getNetworkInterfaces()+getInetAddresses()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *       to obtain all IP addresses for this node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @return an Enumeration of NetworkInterfaces found on this machine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @exception  SocketException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    public static Enumeration<NetworkInterface> getNetworkInterfaces()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        final NetworkInterface[] netifs = getAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        // specified to return null if no network interfaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        if (netifs == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        return new Enumeration<NetworkInterface>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            private int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            public NetworkInterface nextElement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                if (netifs != null && i < netifs.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                    NetworkInterface netif = netifs[i++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    return netif;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                    throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                return (netifs != null && i < netifs.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    private native static NetworkInterface[] getAll()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    private native static NetworkInterface getByName0(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
1097
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   370
    private native static NetworkInterface getByIndex0(int index)
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   371
        throws SocketException;
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   372
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    private native static NetworkInterface getByInetAddress0(InetAddress addr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * Returns whether a network interface is up and running.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * @return  <code>true</code> if the interface is up and running.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * @exception       SocketException if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    public boolean isUp() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        return isUp0(name, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * Returns whether a network interface is a loopback interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * @return  <code>true</code> if the interface is a loopback interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * @exception       SocketException if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    public boolean isLoopback() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        return isLoopback0(name, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * Returns whether a network interface is a point to point interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * A typical point to point interface would be a PPP connection through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * a modem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * @return  <code>true</code> if the interface is a point to point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     *          interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * @exception       SocketException if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    public boolean isPointToPoint() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        return isP2P0(name, index);
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
     * Returns whether a network interface supports multicasting or not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * @return  <code>true</code> if the interface supports Multicasting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * @exception       SocketException if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    public boolean supportsMulticast() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        return supportsMulticast0(name, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * Returns the hardware address (usually MAC) of the interface if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * 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
   430
     * 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
   431
     * the permission {@link NetPermission}("getNetworkInformation").
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     *
6872
b90e4fee471f 6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents: 5506
diff changeset
   433
     * @return  a byte array containing the address, or <code>null</code> if
b90e4fee471f 6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents: 5506
diff changeset
   434
     *          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
   435
     *          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
   436
     *          NetPermission("getNetworkInformation")
b90e4fee471f 6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents: 5506
diff changeset
   437
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * @exception       SocketException if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    public byte[] getHardwareAddress() throws SocketException {
6872
b90e4fee471f 6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents: 5506
diff changeset
   442
        SecurityManager sec = System.getSecurityManager();
b90e4fee471f 6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents: 5506
diff changeset
   443
        if (sec != null) {
b90e4fee471f 6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents: 5506
diff changeset
   444
            try {
b90e4fee471f 6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents: 5506
diff changeset
   445
                sec.checkPermission(new NetPermission("getNetworkInformation"));
b90e4fee471f 6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents: 5506
diff changeset
   446
            } catch (SecurityException e) {
b90e4fee471f 6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents: 5506
diff changeset
   447
                if (!getInetAddresses().hasMoreElements()) {
b90e4fee471f 6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents: 5506
diff changeset
   448
                    // don't have connect permission to any local address
b90e4fee471f 6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents: 5506
diff changeset
   449
                    return null;
b90e4fee471f 6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents: 5506
diff changeset
   450
                }
b90e4fee471f 6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents: 5506
diff changeset
   451
            }
b90e4fee471f 6952603: NetworkInterface reveals local network address to untrusted code
michaelm
parents: 5506
diff changeset
   452
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        for (InetAddress addr : addrs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            if (addr instanceof Inet4Address) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                return getMacAddr0(((Inet4Address)addr).getAddress(), name, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        return getMacAddr0(null, 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 the Maximum Transmission Unit (MTU) of this interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * @return the value of the MTU for that interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * @exception       SocketException if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    public int getMTU() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        return getMTU0(name, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * Returns whether this interface is a virtual interface (also called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * subinterface).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * Virtual interfaces are, on some systems, interfaces created as a child
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * of a physical interface and given different settings (like address or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * MTU). Usually the name of the interface will the name of the parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * followed by a colon (:) and a number identifying the child since there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * can be several virtual interfaces attached to a single physical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * @return <code>true</code> if this interface is a virtual interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    public boolean isVirtual() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        return virtual;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    private native static boolean isUp0(String name, int ind) throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    private native static boolean isLoopback0(String name, int ind) throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    private native static boolean supportsMulticast0(String name, int ind) throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    private native static boolean isP2P0(String name, int ind) throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    private native static byte[] getMacAddr0(byte[] inAddr, String name, int ind) throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    private native static int getMTU0(String name, int ind) throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * Compares this object against the specified object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * The result is <code>true</code> if and only if the argument is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * not <code>null</code> and it represents the same NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * as this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * Two instances of <code>NetworkInterface</code> represent the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * NetworkInterface if both name and addrs are the same for both.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * @param   obj   the object to compare against.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * @return  <code>true</code> if the objects are the same;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     *          <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * @see     java.net.InetAddress#getAddress()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    public boolean equals(Object obj) {
7986
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   511
        if (!(obj instanceof NetworkInterface)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        }
7986
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   514
        NetworkInterface that = (NetworkInterface)obj;
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   515
        if (this.name != null ) {
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   516
            if (!this.name.equals(that.name)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        } else {
7986
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   520
            if (that.name != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        }
7986
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   524
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   525
        if (this.addrs == null) {
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   526
            return that.addrs == null;
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   527
        } else if (that.addrs == null) {
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   528
            return false;
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   529
        }
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   530
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   531
        /* Both addrs not null. Compare number of addresses */
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   532
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   533
        if (this.addrs.length != that.addrs.length) {
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   534
            return false;
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   535
        }
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   536
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   537
        InetAddress[] thatAddrs = that.addrs;
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   538
        int count = thatAddrs.length;
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   539
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   540
        for (int i=0; i<count; i++) {
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   541
            boolean found = false;
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   542
            for (int j=0; j<count; j++) {
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   543
                if (addrs[i].equals(thatAddrs[j])) {
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   544
                    found = true;
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   545
                    break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            }
7986
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   548
            if (!found) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    public int hashCode() {
7986
350f0f3c453b 7003398: NetworkInterface equals() and hashCode() behaviors depend on permissions granted
michaelm
parents: 7668
diff changeset
   556
        return name == null? 0: name.hashCode();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        String result = "name:";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        result += name == null? "null": name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        if (displayName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            result += " (" + displayName + ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    }
9828
9c9dd9a1b11b 7013969: NetworkInterface.toString can reveal bindings
chegar
parents: 7986
diff changeset
   567
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    private static native void init();
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9842
diff changeset
   569
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9842
diff changeset
   570
    /**
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9842
diff changeset
   571
     * Returns the default network interface of this system
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9842
diff changeset
   572
     *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9842
diff changeset
   573
     * @return the default interface
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9842
diff changeset
   574
     */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9842
diff changeset
   575
    static NetworkInterface getDefault() {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9842
diff changeset
   576
        return defaultInterface;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9842
diff changeset
   577
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
}