Methods Summary |
---|
public boolean | equals(java.lang.Object obj)Compares the specified object to this {@code NetworkInterface} and
returns whether they are equal or not. The object must be an instance of
{@code NetworkInterface} with the same name, {@code displayName} and list
of network interfaces to be equal.
// just return true if it is the exact same object
if (obj == this) {
return true;
}
if (obj instanceof NetworkInterface) {
/*
* make sure that some simple checks pass. If the name is not the
* same then we are sure it is not the same one. We don't check the
* hashcode as it is generated from the name which we check
*/
NetworkInterface netif = (NetworkInterface) obj;
if (netif.getIndex() != interfaceIndex) {
return false;
}
if (!(name.equals("")) && (!netif.getName().equals(name))) { //$NON-NLS-1$
return false;
}
if ((name.equals("")) && (!netif.getName().equals(displayName))) { //$NON-NLS-1$
return false;
}
// now check that the internet addresses are the same
Enumeration<InetAddress> netifAddresses = netif.getInetAddresses();
Enumeration<InetAddress> localifAddresses = getInetAddresses();
if ((netifAddresses == null) && (localifAddresses != null)) {
return false;
}
if ((netifAddresses == null) && (localifAddresses == null)) {
// neither have any addresses so they are the same
return true;
}
if (netifAddresses != null) {
while (netifAddresses.hasMoreElements()
&& localifAddresses.hasMoreElements()) {
if (!(localifAddresses.nextElement()).equals(netifAddresses
.nextElement())) {
return false;
}
}
/*
* now make sure that they had the same number of internet
* addresses, if not they are not the same interface
*/
if (netifAddresses.hasMoreElements()
|| localifAddresses.hasMoreElements()) {
return false;
}
}
return true;
}
return false;
|
public static java.net.NetworkInterface | getByInetAddress(java.net.InetAddress address)Gets the specific network interface according to the given address.
if (address == null) {
throw new NullPointerException(Msg.getString("K0331")); //$NON-NLS-1$
}
/*
* get the list of interfaces, and then loop through the list. For each
* interface loop through the associated set of internet addresses and
* see if one matches. If so return that network interface
*/
Enumeration<NetworkInterface> interfaces = getNetworkInterfaces();
if (interfaces != null) {
while (interfaces.hasMoreElements()) {
NetworkInterface netif = interfaces.nextElement();
/*
* to be compatible use the raw addresses without any security
* filtering
*/
// Enumeration netifAddresses = netif.getInetAddresses();
if ((netif.addresses != null) && (netif.addresses.length != 0)) {
Enumeration<InetAddress> netifAddresses = (new Vector<InetAddress>(
Arrays.asList(netif.addresses))).elements();
if (netifAddresses != null) {
while (netifAddresses.hasMoreElements()) {
if (address.equals(netifAddresses.nextElement())) {
return netif;
}
}
}
}
}
}
return null;
|
public static java.net.NetworkInterface | getByName(java.lang.String interfaceName)Gets the specific network interface according to a given name.
if (interfaceName == null) {
throw new NullPointerException(Msg.getString("K0330")); //$NON-NLS-1$
}
/*
* get the list of interfaces, and then loop through the list to look
* for one with a matching name
*/
Enumeration<NetworkInterface> interfaces = getNetworkInterfaces();
if (interfaces != null) {
while (interfaces.hasMoreElements()) {
NetworkInterface netif = interfaces.nextElement();
if (netif.getName().equals(interfaceName)) {
return netif;
}
}
}
return null;
|
public java.lang.String | getDisplayName()Gets the human-readable name associated with this network interface.
/*
* we should return the display name unless it is blank in this case
* return the name so that something is displayed.
*/
if (!(displayName.equals(""))) { //$NON-NLS-1$
return displayName;
}
return name;
|
java.net.InetAddress | getFirstAddress()Returns the first address for the network interface. This is used in the
natives when we need one of the addresses for the interface and any one
will do
if ((addresses != null) && (addresses.length >= 1)) {
return addresses[0];
}
return null;
|
int | getIndex()Returns the index for the network interface. Unless the system supports
IPV6 this will be 0.
return interfaceIndex;
|
public java.util.Enumeration | getInetAddresses()Gets a list of addresses bound to this network interface.
/*
* create new vector from which Enumeration to be returned can be
* generated set the initial capacity to be the number of addresses for
* the network interface which is the maximum required size
*/
/*
* return an empty enumeration if there are no addresses associated with
* the interface
*/
if (addresses == null) {
return new Vector<InetAddress>(0).elements();
}
/*
* for those configuration that support the security manager we only
* return addresses for which checkConnect returns true
*/
Vector<InetAddress> accessibleAddresses = new Vector<InetAddress>(
addresses.length);
/*
* get the security manager. If one does not exist just return the full
* list
*/
SecurityManager security = System.getSecurityManager();
if (security == null) {
return (new Vector<InetAddress>(Arrays.asList(addresses)))
.elements();
}
/*
* ok security manager exists so check each address and return those
* that pass
*/
for (InetAddress element : addresses) {
if (security != null) {
try {
/*
* since we don't have a port in this case we pass in
* NO_PORT
*/
security.checkConnect(element.getHostName(),
CHECK_CONNECT_NO_PORT);
accessibleAddresses.add(element);
} catch (SecurityException e) {
}
}
}
Enumeration<InetAddress> theAccessibleElements = accessibleAddresses
.elements();
if (theAccessibleElements.hasMoreElements()) {
return accessibleAddresses.elements();
}
return new Vector<InetAddress>(0).elements();
|
public java.lang.String | getName()Gets the name associated with this network interface.
return name;
|
public static java.util.Enumeration | getNetworkInterfaces()Gets a list of all network interfaces available on the local system or
{@code null} if no interface is available.
NetworkInterface[] interfaces = getNetworkInterfacesImpl();
if (interfaces == null) {
return null;
}
for (NetworkInterface netif : interfaces) {
// Ensure that current NetworkInterface is bound to at least
// one InetAddress before processing
if (netif.addresses != null) {
for (InetAddress addr : netif.addresses) {
if (16 == addr.ipaddress.length) {
if (addr.isLinkLocalAddress()
|| addr.isSiteLocalAddress()) {
((Inet6Address) addr).scopedIf = netif;
((Inet6Address) addr).ifname = netif.name;
((Inet6Address) addr).scope_ifname_set = true;
}
}
}
}
}
return (new Vector<NetworkInterface>(Arrays.asList(interfaces)))
.elements();
|
private static native java.net.NetworkInterface[] | getNetworkInterfacesImpl()This {@code native} method returns the list of network interfaces
supported by the system. An array is returned which is easier to generate
and which can easily be converted into the required enumeration on the
java side.
|
public int | hashCode()Gets the hashcode for this {@code NetworkInterface} instance. Since the
name should be unique for each network interface the hashcode is
generated using this name.
if (hashCode == 0) {
hashCode = name.hashCode();
}
return hashCode;
|
public java.lang.String | toString()Gets a string containing a concise, human-readable description of this
network interface.
StringBuilder string = new StringBuilder(25);
string.append("["); //$NON-NLS-1$
string.append(name);
string.append("]["); //$NON-NLS-1$
string.append(displayName);
string.append("]"); //$NON-NLS-1$
/*
* get the addresses through this call to make sure we only reveal those
* that we should
*/
Enumeration<InetAddress> theAddresses = getInetAddresses();
if (theAddresses != null) {
while (theAddresses.hasMoreElements()) {
InetAddress nextAddress = theAddresses.nextElement();
string.append("["); //$NON-NLS-1$
string.append(nextAddress.toString());
string.append("]"); //$NON-NLS-1$
}
}
return string.toString();
|