jdk/src/share/classes/java/net/NetworkInterface.java
author jccollet
Mon, 25 Aug 2008 14:38:20 +0200
changeset 1097 af4930f761df
parent 715 f16baef3a20e
child 1152 29d6145d1097
permissions -rw-r--r--
6717876: Make java.net.NetworkInterface.getIndex() public Summary: Make getIndex() and getByIndex() public. Required a name change in native code Reviewed-by: alanb, chegar, michaelm
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.net.SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.NoSuchElementException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.security.action.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * This class represents a Network Interface made up of a name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * and a list of IP addresses assigned to this interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * It is used to identify the local interface on which a multicast group
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * is joined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Interfaces are normally known by names such as "le0".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public final class NetworkInterface {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private String displayName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private InetAddress addrs[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private InterfaceAddress bindings[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private NetworkInterface childs[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private NetworkInterface parent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private boolean virtual = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        AccessController.doPrivileged(new LoadLibraryAction("net"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * Returns an NetworkInterface object with index set to 0 and name to null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * Setting such an interface on a MulticastSocket will cause the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * kernel to choose one interface for sending multicast packets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    NetworkInterface() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    NetworkInterface(String name, int index, InetAddress[] addrs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        this.index = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        this.addrs = addrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Get the name of this network interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @return the name of this network interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Convenience method to return an Enumeration with all or a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * subset of the InetAddresses bound to this network interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * If there is a security manager, its <code>checkConnect</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * method is called for each InetAddress. Only InetAddresses where
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * the <code>checkConnect</code> doesn't throw a SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * will be returned in the Enumeration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @return an Enumeration object with all or a subset of the InetAddresses
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * bound to this network interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public Enumeration<InetAddress> getInetAddresses() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        class checkedAddresses implements Enumeration<InetAddress> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            private int i=0, count=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            private InetAddress local_addrs[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            checkedAddresses() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                local_addrs = new InetAddress[addrs.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                SecurityManager sec = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                for (int j=0; j<addrs.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                        if (sec != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                            sec.checkConnect(addrs[j].getHostAddress(), -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                        local_addrs[count++] = addrs[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    } catch (SecurityException e) { }
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            public InetAddress nextElement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                if (i < count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    return local_addrs[i++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    throw new NoSuchElementException();
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
            public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                return (i < count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        return new checkedAddresses();
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Get a List of all or a subset of the <code>InterfaceAddresses</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * of this network interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * If there is a security manager, its <code>checkConnect</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * method is called with the InetAddress for each InterfaceAddress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Only InterfaceAddresses where the <code>checkConnect</code> doesn't throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * a SecurityException will be returned in the List.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @return a <code>List</code> object with all or a subset of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *         InterfaceAddresss of this network interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    public java.util.List<InterfaceAddress> getInterfaceAddresses() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        java.util.List<InterfaceAddress> lst = new java.util.ArrayList<InterfaceAddress>(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        SecurityManager sec = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        for (int j=0; j<bindings.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                if (sec != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    sec.checkConnect(bindings[j].getAddress().getHostAddress(), -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                lst.add(bindings[j]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            } catch (SecurityException e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        return lst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Get an Enumeration with all the subinterfaces (also known as virtual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * interfaces) attached to this network interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * For instance eth0:1 will be a subinterface to eth0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @return an Enumeration object with all of the subinterfaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * of this network interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public Enumeration<NetworkInterface> getSubInterfaces() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        class subIFs implements Enumeration<NetworkInterface> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            private int i=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            subIFs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            public NetworkInterface nextElement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                if (i < childs.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                    return childs[i++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                    throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                return (i < childs.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        return new subIFs();
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * Returns the parent NetworkInterface of this interface if this is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * a subinterface, or <code>null</code> if it is a physical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * (non virtual) interface or has no parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @return The <code>NetworkInterface</code> this interface is attached to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    public NetworkInterface getParent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        return parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    /**
1097
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   206
     * 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
   207
     * 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
   208
     * 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
   209
     * machines.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *
1097
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   211
     * @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
   212
     *         unknown
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   213
     * @see #getByIndex(int)
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   214
     * @since 1.7
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     */
1097
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   216
    public int getIndex() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * Get the display name of this network interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * A display name is a human readable String describing the network
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * device.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @return the display name of this network interface,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *         or null if no display name is available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    public String getDisplayName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        return displayName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * Searches for the network interface with the specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @param   name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *          The name of the network interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @return  A <tt>NetworkInterface</tt> with the specified name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *          or <tt>null</tt> if there is no network interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *          with the specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @throws  SocketException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *          If an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @throws  NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *          If the specified name is <tt>null</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    public static NetworkInterface getByName(String name) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        if (name == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        return getByName0(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * Get a network interface given its index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @param index an integer, the index of the interface
1097
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   258
     * @return the NetworkInterface obtained from its index, or {@code null} if
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   259
     *         there is no interface with such an index on the system
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   260
     * @throws  SocketException  if an I/O error occurs.
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   261
     * @throws  IllegalArgumentException if index has a negative value
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   262
     * @see #getIndex()
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   263
     * @since 1.7
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     */
1097
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   265
    public static NetworkInterface getByIndex(int index) throws SocketException {
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   266
        if (index < 0)
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   267
            throw new IllegalArgumentException("Interface index can't be negative");
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   268
        return getByIndex0(index);
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   269
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * Convenience method to search for a network interface that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * has the specified Internet Protocol (IP) address bound to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * If the specified IP address is bound to multiple network
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * interfaces it is not defined which network interface is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @param   addr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *          The <tt>InetAddress</tt> to search with.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * @return  A <tt>NetworkInterface</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *          or <tt>null</tt> if there is no network interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     *          with the specified IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @throws  SocketException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *          If an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @throws  NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *          If the specified address is <tt>null</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    public static NetworkInterface getByInetAddress(InetAddress addr) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        if (addr == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        return getByInetAddress0(addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * Returns all the interfaces on this machine. Returns null if no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * network interfaces could be found on this machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * NOTE: can use getNetworkInterfaces()+getInetAddresses()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *       to obtain all IP addresses for this node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * @return an Enumeration of NetworkInterfaces found on this machine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * @exception  SocketException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    public static Enumeration<NetworkInterface> getNetworkInterfaces()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        final NetworkInterface[] netifs = getAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        // specified to return null if no network interfaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        if (netifs == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        return new Enumeration<NetworkInterface>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            private int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            public NetworkInterface nextElement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                if (netifs != null && i < netifs.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                    NetworkInterface netif = netifs[i++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                    return netif;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                    throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                return (netifs != null && i < netifs.length);
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    private native static NetworkInterface[] getAll()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    private native static NetworkInterface getByName0(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
1097
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   341
    private native static NetworkInterface getByIndex0(int index)
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   342
        throws SocketException;
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 715
diff changeset
   343
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    private native static NetworkInterface getByInetAddress0(InetAddress addr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * Returns whether a network interface is up and running.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @return  <code>true</code> if the interface is up and running.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * @exception       SocketException if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    public boolean isUp() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        return isUp0(name, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * Returns whether a network interface is a loopback interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * @return  <code>true</code> if the interface is a loopback interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @exception       SocketException if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    public boolean isLoopback() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        return isLoopback0(name, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * Returns whether a network interface is a point to point interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * A typical point to point interface would be a PPP connection through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * a modem.
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 point to point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     *          interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * @exception       SocketException if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    public boolean isPointToPoint() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        return isP2P0(name, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * Returns whether a network interface supports multicasting or not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * @return  <code>true</code> if the interface supports Multicasting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * @exception       SocketException if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    public boolean supportsMulticast() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        return supportsMulticast0(name, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * Returns the hardware address (usually MAC) of the interface if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * has one and if it can be accessed given the current privileges.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * @return  a byte array containing the address or <code>null</code> if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     *          the address doesn't exist or is not accessible.
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
    public byte[] getHardwareAddress() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        for (InetAddress addr : addrs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            if (addr instanceof Inet4Address) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                return getMacAddr0(((Inet4Address)addr).getAddress(), name, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        return getMacAddr0(null, name, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * Returns the Maximum Transmission Unit (MTU) of this interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * @return the value of the MTU for that interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * @exception       SocketException if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    public int getMTU() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        return getMTU0(name, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * Returns whether this interface is a virtual interface (also called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * subinterface).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * Virtual interfaces are, on some systems, interfaces created as a child
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * of a physical interface and given different settings (like address or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * MTU). Usually the name of the interface will the name of the parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * followed by a colon (:) and a number identifying the child since there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * can be several virtual interfaces attached to a single physical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * @return <code>true</code> if this interface is a virtual interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    public boolean isVirtual() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        return virtual;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    private native static boolean isUp0(String name, int ind) throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    private native static boolean isLoopback0(String name, int ind) throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    private native static boolean supportsMulticast0(String name, int ind) throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    private native static boolean isP2P0(String name, int ind) throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    private native static byte[] getMacAddr0(byte[] inAddr, String name, int ind) throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    private native static int getMTU0(String name, int ind) throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * Compares this object against the specified object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * The result is <code>true</code> if and only if the argument is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * not <code>null</code> and it represents the same NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * as this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * Two instances of <code>NetworkInterface</code> represent the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * NetworkInterface if both name and addrs are the same for both.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * @param   obj   the object to compare against.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * @return  <code>true</code> if the objects are the same;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     *          <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * @see     java.net.InetAddress#getAddress()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        if ((obj == null) || !(obj instanceof NetworkInterface)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        NetworkInterface netIF = (NetworkInterface)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        if (name != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            if (netIF.getName() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                if (!name.equals(netIF.getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            if (netIF.getName() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        Enumeration newAddrs = netIF.getInetAddresses();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        for (i = 0; newAddrs.hasMoreElements();newAddrs.nextElement(), i++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        if (addrs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            if (i != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
             * Compare number of addresses (in the checked subset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            Enumeration e = getInetAddresses();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            for (; e.hasMoreElements(); count++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            if (i != count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        newAddrs = netIF.getInetAddresses();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        for (; newAddrs.hasMoreElements();) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            boolean equal = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            Enumeration thisAddrs = getInetAddresses();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            InetAddress newAddr = (InetAddress)newAddrs.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            for (; thisAddrs.hasMoreElements();) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                InetAddress thisAddr = (InetAddress)thisAddrs.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                if (thisAddr.equals(newAddr)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                    equal = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            if (!equal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        if (addrs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            for (int i = 0; i < addrs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                count += addrs[i].hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        String result = "name:";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        result += name == null? "null": name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        if (displayName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            result += " (" + displayName + ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        result += " index: "+index+" addresses:\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        for (Enumeration e = getInetAddresses(); e.hasMoreElements(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            InetAddress addr = (InetAddress)e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            result += addr+";\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    private static native void init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
}