jdk/src/share/classes/java/net/NetworkInterface.java
author xdono
Thu, 02 Oct 2008 19:58:32 -0700
changeset 1247 b4c26443dee5
parent 1152 29d6145d1097
child 5180 8161f879d704
permissions -rw-r--r--
6754988: Update copyright year Summary: Update for files that have been modified starting July 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 82
diff changeset
     2
 * Copyright 2000-2008 Sun Microsystems, Inc.  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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * will be returned in the Enumeration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @return an Enumeration object with all or a subset of the InetAddresses
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * bound to this network interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public Enumeration<InetAddress> getInetAddresses() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        class checkedAddresses implements Enumeration<InetAddress> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            private int i=0, count=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            private InetAddress local_addrs[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            checkedAddresses() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                local_addrs = new InetAddress[addrs.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                SecurityManager sec = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                for (int j=0; j<addrs.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                        if (sec != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                            sec.checkConnect(addrs[j].getHostAddress(), -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                        local_addrs[count++] = addrs[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    } catch (SecurityException e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            public InetAddress nextElement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                if (i < count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    return local_addrs[i++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                return (i < count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        return new checkedAddresses();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Get a List of all or a subset of the <code>InterfaceAddresses</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * of this network interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * If there is a security manager, its <code>checkConnect</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * method is called with the InetAddress for each InterfaceAddress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Only InterfaceAddresses where the <code>checkConnect</code> doesn't throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * a SecurityException will be returned in the List.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @return a <code>List</code> object with all or a subset of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *         InterfaceAddresss of this network interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public java.util.List<InterfaceAddress> getInterfaceAddresses() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        java.util.List<InterfaceAddress> lst = new java.util.ArrayList<InterfaceAddress>(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        SecurityManager sec = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        for (int j=0; j<bindings.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                if (sec != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    sec.checkConnect(bindings[j].getAddress().getHostAddress(), -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                lst.add(bindings[j]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            } catch (SecurityException e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        return lst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Get an Enumeration with all the subinterfaces (also known as virtual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * interfaces) attached to this network interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * For instance eth0:1 will be a subinterface to eth0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @return an Enumeration object with all of the subinterfaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * of this network interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public Enumeration<NetworkInterface> getSubInterfaces() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        class subIFs implements Enumeration<NetworkInterface> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            private int i=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            subIFs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            public NetworkInterface nextElement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                if (i < childs.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    return childs[i++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                    throw new NoSuchElementException();
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
            public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                return (i < childs.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        return new subIFs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * Returns the parent NetworkInterface of this interface if this is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * a subinterface, or <code>null</code> if it is a physical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * (non virtual) interface or has no parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @return The <code>NetworkInterface</code> this interface is attached to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    public NetworkInterface getParent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        return parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    /**
1097
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   205
     * 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
   206
     * 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
   207
     * 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
   208
     * machines.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *
1097
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   210
     * @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
   211
     *         unknown
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   212
     * @see #getByIndex(int)
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   213
     * @since 1.7
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     */
1097
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   215
    public int getIndex() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * Get the display name of this network interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * A display name is a human readable String describing the network
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * device.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @return the display name of this network interface,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     *         or null if no display name is available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    public String getDisplayName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        return displayName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * Searches for the network interface with the specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @param   name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *          The name of the network interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @return  A <tt>NetworkInterface</tt> with the specified name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *          or <tt>null</tt> if there is no network interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *          with the specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @throws  SocketException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *          If an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @throws  NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *          If the specified name is <tt>null</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    public static NetworkInterface getByName(String name) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        if (name == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        return getByName0(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * Get a network interface given its index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @param index an integer, the index of the interface
1097
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   257
     * @return the NetworkInterface obtained from its index, or {@code null} if
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   258
     *         there is no interface with such an index on the system
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   259
     * @throws  SocketException  if an I/O error occurs.
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   260
     * @throws  IllegalArgumentException if index has a negative value
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   261
     * @see #getIndex()
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   262
     * @since 1.7
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     */
1097
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   264
    public static NetworkInterface getByIndex(int index) throws SocketException {
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   265
        if (index < 0)
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   266
            throw new IllegalArgumentException("Interface index can't be negative");
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   267
        return getByIndex0(index);
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   268
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * Convenience method to search for a network interface that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * has the specified Internet Protocol (IP) address bound to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * If the specified IP address is bound to multiple network
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * interfaces it is not defined which network interface is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @param   addr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *          The <tt>InetAddress</tt> to search with.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * @return  A <tt>NetworkInterface</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *          or <tt>null</tt> if there is no network interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *          with the specified IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @throws  SocketException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *          If an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @throws  NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *          If the specified address is <tt>null</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    public static NetworkInterface getByInetAddress(InetAddress addr) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        if (addr == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        return getByInetAddress0(addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * Returns all the interfaces on this machine. Returns null if no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * network interfaces could be found on this machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * NOTE: can use getNetworkInterfaces()+getInetAddresses()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     *       to obtain all IP addresses for this node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * @return an Enumeration of NetworkInterfaces found on this machine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * @exception  SocketException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    public static Enumeration<NetworkInterface> getNetworkInterfaces()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        final NetworkInterface[] netifs = getAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        // specified to return null if no network interfaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        if (netifs == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        return new Enumeration<NetworkInterface>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            private int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            public NetworkInterface nextElement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                if (netifs != null && i < netifs.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                    NetworkInterface netif = netifs[i++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                    return netif;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                    throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                return (netifs != null && i < netifs.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    private native static NetworkInterface[] getAll()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    private native static NetworkInterface getByName0(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
1097
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   340
    private native static NetworkInterface getByIndex0(int index)
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   341
        throws SocketException;
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   342
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    private native static NetworkInterface getByInetAddress0(InetAddress addr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * Returns whether a network interface is up and running.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * @return  <code>true</code> if the interface is up and running.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @exception       SocketException if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    public boolean isUp() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        return isUp0(name, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * Returns whether a network interface is a loopback interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * @return  <code>true</code> if the interface is a loopback interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * @exception       SocketException if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    public boolean isLoopback() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        return isLoopback0(name, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * Returns whether a network interface is a point to point interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * A typical point to point interface would be a PPP connection through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * a modem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * @return  <code>true</code> if the interface is a point to point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     *          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 isPointToPoint() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        return isP2P0(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 supports multicasting or not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * @return  <code>true</code> if the interface supports Multicasting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * @exception       SocketException if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    public boolean supportsMulticast() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        return supportsMulticast0(name, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * Returns the hardware address (usually MAC) of the interface if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * has one and if it can be accessed given the current privileges.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * @return  a byte array containing the address or <code>null</code> if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     *          the address doesn't exist or is not accessible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * @exception       SocketException if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    public byte[] getHardwareAddress() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        for (InetAddress addr : addrs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            if (addr instanceof Inet4Address) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                return getMacAddr0(((Inet4Address)addr).getAddress(), 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
        return getMacAddr0(null, 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 the Maximum Transmission Unit (MTU) of this interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * @return the value of the MTU for that interface.
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
    public int getMTU() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        return getMTU0(name, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * Returns whether this interface is a virtual interface (also called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * subinterface).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * Virtual interfaces are, on some systems, interfaces created as a child
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * of a physical interface and given different settings (like address or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * MTU). Usually the name of the interface will the name of the parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * followed by a colon (:) and a number identifying the child since there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * can be several virtual interfaces attached to a single physical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * @return <code>true</code> if this interface is a virtual interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    public boolean isVirtual() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        return virtual;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    private native static boolean isUp0(String name, int ind) throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    private native static boolean isLoopback0(String name, int ind) throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    private native static boolean supportsMulticast0(String name, int ind) throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    private native static boolean isP2P0(String name, int ind) throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    private native static byte[] getMacAddr0(byte[] inAddr, String name, int ind) throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    private native static int getMTU0(String name, int ind) throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * Compares this object against the specified object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * The result is <code>true</code> if and only if the argument is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * not <code>null</code> and it represents the same NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * as this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * Two instances of <code>NetworkInterface</code> represent the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * NetworkInterface if both name and addrs are the same for both.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * @param   obj   the object to compare against.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * @return  <code>true</code> if the objects are the same;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     *          <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * @see     java.net.InetAddress#getAddress()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        if ((obj == null) || !(obj instanceof NetworkInterface)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        NetworkInterface netIF = (NetworkInterface)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        if (name != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            if (netIF.getName() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                if (!name.equals(netIF.getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            if (netIF.getName() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        Enumeration newAddrs = netIF.getInetAddresses();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        for (i = 0; newAddrs.hasMoreElements();newAddrs.nextElement(), i++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        if (addrs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            if (i != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
             * Compare number of addresses (in the checked subset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            Enumeration e = getInetAddresses();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            for (; e.hasMoreElements(); count++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            if (i != count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        newAddrs = netIF.getInetAddresses();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        for (; newAddrs.hasMoreElements();) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            boolean equal = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            Enumeration thisAddrs = getInetAddresses();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            InetAddress newAddr = (InetAddress)newAddrs.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            for (; thisAddrs.hasMoreElements();) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                InetAddress thisAddr = (InetAddress)thisAddrs.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                if (thisAddr.equals(newAddr)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                    equal = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            if (!equal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        if (addrs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            for (int i = 0; i < addrs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                count += addrs[i].hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        String result = "name:";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        result += name == null? "null": name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        if (displayName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            result += " (" + displayName + ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        result += " index: "+index+" addresses:\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        for (Enumeration e = getInetAddresses(); e.hasMoreElements(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            InetAddress addr = (InetAddress)e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            result += addr+";\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    private static native void init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
}