Java Code Examples for java.net.NetworkInterface#getByIndex()

The following examples show how to use java.net.NetworkInterface#getByIndex() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MiscUtils.java    From RISE-V2G with MIT License 6 votes vote down vote up
public static byte[] getMacAddress() {
	String networkInterfaceConfig = getPropertyValue("network.interface").toString();
	NetworkInterface nif = null;
	byte[] macAddress = null;
	
	try {
		if (System.getProperty("os.name").toLowerCase().indexOf("win") >= 0) {
			nif = NetworkInterface.getByIndex(Integer.parseInt(networkInterfaceConfig));
		} else {
			nif = NetworkInterface.getByName(networkInterfaceConfig);
		}
		macAddress = nif.getHardwareAddress();
	} catch (SocketException e) {
		getLogger().error("Failed to retrieve local mac address (SocketException)", e);
	}
	
	return macAddress;
}
 
Example 2
Source File: MQTT_PVConn.java    From phoebus with Eclipse Public License 1.0 6 votes vote down vote up
private void generateClientID()
{
    try
    {
        NetworkInterface nwi = NetworkInterface.getByIndex(0);
        byte mac[] = nwi.getHardwareAddress();
        clientID = String.valueOf(mac) + String.valueOf(randInt);
    }
    catch(Exception e)
    {
        try {
            InetAddress address = InetAddress.getLocalHost();
            clientID = address.getCanonicalHostName() + String.valueOf(randInt);
        }
        catch(Exception e2)
        {
            clientID = String.valueOf(randInt);
        }
    }

    //System MAC address (or hostname) + random integer + object hash... hopefully unique?
    clientID += "-" + System.identityHashCode(this);
}
 
Example 3
Source File: OspfConfigUtil.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Returns interface IP by index.
 *
 * @param interfaceIndex interface index
 * @return interface IP by index
 */
private static Ip4Address getInterfaceIp(int interfaceIndex) {
    Ip4Address ipAddress = null;
    try {
        NetworkInterface networkInterface = NetworkInterface.getByIndex(interfaceIndex);
        Enumeration ipAddresses = networkInterface.getInetAddresses();
        while (ipAddresses.hasMoreElements()) {
            InetAddress address = (InetAddress) ipAddresses.nextElement();
            if (!address.isLinkLocalAddress()) {
                ipAddress = Ip4Address.valueOf(address.getAddress());
                break;
            }
        }
    } catch (Exception e) {
        log.debug("Error while getting Interface IP by index");
        return OspfUtil.DEFAULTIP;
    }
    return ipAddress;
}
 
Example 4
Source File: Controller.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Returns interface IP by index.
 *
 * @param interfaceIndex interface index
 * @return interface IP by index
 */
private Ip4Address getInterfaceIp(int interfaceIndex) {
    Ip4Address ipAddress = null;
    try {
        NetworkInterface networkInterface = NetworkInterface.getByIndex(interfaceIndex);
        Enumeration ipAddresses = networkInterface.getInetAddresses();
        while (ipAddresses.hasMoreElements()) {
            InetAddress address = (InetAddress) ipAddresses.nextElement();
            if (!address.isLinkLocalAddress()) {
                ipAddress = Ip4Address.valueOf(address.getAddress());
                break;
            }
        }
    } catch (Exception e) {
        log.debug("Error while getting Interface IP by index");
        return OspfUtil.DEFAULTIP;
    }

    return ipAddress;
}
 
Example 5
Source File: Controller.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Returns interface IP by index.
 *
 * @param interfaceIndex interface index
 * @return interface IP by index
 */
private Ip4Address getInterfaceIp(int interfaceIndex) {
    Ip4Address ipAddress = null;
    try {
        NetworkInterface networkInterface = NetworkInterface.getByIndex(interfaceIndex);
        Enumeration ipAddresses = networkInterface.getInetAddresses();
        while (ipAddresses.hasMoreElements()) {
            InetAddress address = (InetAddress) ipAddresses.nextElement();
            if (!address.isLinkLocalAddress()) {
                ipAddress = Ip4Address.valueOf(address.getAddress());
                break;
            }
        }
    } catch (Exception e) {
        log.debug("Error while getting Interface IP by index");
        return IsisConstants.DEFAULTIP;
    }
    return ipAddress;
}
 
Example 6
Source File: MiscUtils.java    From RISE-V2G with MIT License 5 votes vote down vote up
/**
 * Determines the link-local IPv6 address which is configured on the network interface provided
 * in the properties file.
 * @return The link-local address given as a String
 */
public static Inet6Address getLinkLocalAddress() {
	String networkInterfaceConfig = getPropertyValue("network.interface").toString();
	
	NetworkInterface nif = null;
	
	try {
		if (System.getProperty("os.name").toLowerCase().indexOf("win") >= 0) {
			nif = NetworkInterface.getByIndex(Integer.parseInt(networkInterfaceConfig));
		} else {
			nif = NetworkInterface.getByName(networkInterfaceConfig);
		}
		Enumeration<InetAddress> inetAddresses = nif.getInetAddresses();
		
		while (inetAddresses.hasMoreElements()) {
			InetAddress inetAddress = inetAddresses.nextElement();
			
			if (inetAddress.getClass() == Inet6Address.class && inetAddress.isLinkLocalAddress()) {
				return (Inet6Address) inetAddress;
			}
		}
		
		getLogger().fatal("No IPv6 link-local address found on the network interface '" +
				nif.getDisplayName() + "' configured in the properties file");
	} catch (SocketException e) {
		getLogger().fatal("SocketException while trying to get network interface for configured name " +
						  networkInterfaceConfig + "'", e); 
	} catch (NullPointerException | NumberFormatException e2) {
		getLogger().fatal("No network interface for configured network interface index '" + 
						  networkInterfaceConfig + "' found");
	}
	
	return null;
}
 
Example 7
Source File: Controller.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Returns interface MAC by index.
 *
 * @param interfaceIndex interface index
 * @return interface IP by index
 */
private MacAddress getInterfaceMac(int interfaceIndex) {
    MacAddress macAddress = null;
    try {
        NetworkInterface networkInterface = NetworkInterface.getByIndex(interfaceIndex);
        macAddress = MacAddress.valueOf(networkInterface.getHardwareAddress());
    } catch (Exception e) {
        log.debug("Error while getting Interface IP by index");
        return macAddress;
    }

    return macAddress;
}
 
Example 8
Source File: NetworkInterfaceAwareSocketFactory.java    From cyberduck with GNU General Public License v3.0 4 votes vote down vote up
private NetworkInterface findIPv6Interface(Inet6Address address) throws IOException {
    if(blacklisted.isEmpty()) {
        if(log.isDebugEnabled()) {
            log.debug("Ignore IP6 default network interface setup with empty blacklist");
        }
        return null;
    }
    if(address.getScopeId() != 0) {
        if(log.isDebugEnabled()) {
            log.debug(String.format("Ignore IP6 default network interface setup for address with scope identifier %d", address.getScopeId()));
        }
        return null;
    }
    // If we find an interface name en0 that supports IPv6 make it the default.
    // We must use the index of the network interface. Referencing the interface by name will still
    // set the scope id to '0' referencing the awdl0 interface that is first in the list of enumerated
    // network interfaces instead of its correct index in <code>java.net.Inet6Address</code>
    // Use private API to defer the numeric format for the address
    List<Integer> indexes = new ArrayList<Integer>();
    final Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces();
    while(enumeration.hasMoreElements()) {
        indexes.add(enumeration.nextElement().getIndex());
    }
    for(Integer index : indexes) {
        final NetworkInterface n = NetworkInterface.getByIndex(index);
        if(log.isDebugEnabled()) {
            log.debug(String.format("Evaluate interface with %s index %d", n, index));
        }
        if(!n.isUp()) {
            if(log.isDebugEnabled()) {
                log.debug(String.format("Ignore interface %s not up", n));
            }
            continue;
        }
        if(blacklisted.contains(n.getName())) {
            log.warn(String.format("Ignore network interface %s disabled with blacklist", n));
            continue;
        }
        for(InterfaceAddress i : n.getInterfaceAddresses()) {
            if(i.getAddress() instanceof Inet6Address) {
                if(log.isInfoEnabled()) {
                    log.info(String.format("Selected network interface %s", n));
                }
                return n;
            }
        }
        log.warn(String.format("No IPv6 for interface %s", n));
    }
    log.warn("No network interface found for IPv6");
    return null;
}