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