private static InetAddress getIpAddress(AddressSelectionCondition condition) throws SocketException { // Before we connect somewhere, we cannot be sure about what we'd be bound to; however, // we only connect when the message where client ID is, is long constructed. Thus, // just use whichever IP address we can find. Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface current = interfaces.nextElement(); if (!current.isUp() || current.isLoopback() || current.isVirtual()) continue; Enumeration<InetAddress> addresses = current.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress addr = addresses.nextElement(); if (addr.isLoopbackAddress()) continue; if (condition.isAcceptableAddress(addr)) { return addr; } } } throw new SocketException("Can't get our ip address, interfaces are: " + interfaces); }
@Override public byte[] call() throws Exception { boolean up = networkInterface.isUp(); boolean loopback = networkInterface.isLoopback(); boolean virtual = networkInterface.isVirtual(); if (loopback || virtual || !up) { throw new Exception("not suitable interface"); } byte[] address = networkInterface.getHardwareAddress(); if (address != null) { byte[] paddedAddress = UUIDGenerator.getZeroPaddedSixBytes(address); if (UUIDGenerator.isBlackList(address)) { throw new Exception("black listed address"); } if (paddedAddress != null) { return paddedAddress; } } throw new Exception("invalid network interface"); } });
NetworkInterface iface = entry.getKey(); InetAddress inetAddr = entry.getValue(); if (iface.isVirtual()) { continue;
try { NetworkInterface network = interfaces.nextElement(); if (network.isLoopback() || network.isVirtual() || !network.isUp()) { continue;
try { NetworkInterface network = interfaces.nextElement(); if (network.isLoopback() || network.isVirtual() || !network.isUp()) { continue;
try { NetworkInterface network = interfaces.nextElement(); if (network.isLoopback() || network.isVirtual() || !network.isUp()) { continue;
NetworkInterface iface = entry.getKey(); InetAddress inetAddr = entry.getValue(); if (iface.isVirtual()) { continue;
log.info(String.format("PointToPoint? %s", networkInterface.isPointToPoint())); log.info(String.format("Supports multicast? %s", networkInterface.supportsMulticast())); log.info(String.format("Virtual? %s", networkInterface.isVirtual())); log.info(String.format("Hardware address: %s", Arrays.toString(networkInterface.getHardwareAddress()))); log.info(String.format("MTU: %s", networkInterface.getMTU()));
NetworkInterface iface = entry.getKey(); InetAddress inetAddr = entry.getValue(); if (iface.isVirtual()) { continue;
public static InetAddress[] getAllLocalInetAddresses() { final List<InetAddress> addrList = new ArrayList<InetAddress>(); try { for (final NetworkInterface ifc : IteratorUtil.enumerationAsIterable(NetworkInterface.getNetworkInterfaces())) { if (ifc.isUp() && !ifc.isVirtual()) { for (final InetAddress addr : IteratorUtil.enumerationAsIterable(ifc.getInetAddresses())) { addrList.add(addr); } } } } catch (final SocketException e) { s_logger.warn("SocketException in getAllLocalInetAddresses().", e); } final InetAddress[] addrs = new InetAddress[addrList.size()]; if (addrList.size() > 0) { System.arraycopy(addrList.toArray(), 0, addrs, 0, addrList.size()); } return addrs; }
/** * docker环境中,有时无法通过InetAddress.getLocalHost()获取 ,会报unknown host Exception, system error * 此时,通过遍历网卡接口的方式规避,出来的数据不一定对 */ private static void doGetIpv4AddressFromNetworkInterface() throws SocketException { Enumeration<NetworkInterface> iterNetwork = NetworkInterface.getNetworkInterfaces(); while (iterNetwork.hasMoreElements()) { NetworkInterface network = iterNetwork.nextElement(); if (!network.isUp() || network.isLoopback() || network.isVirtual()) { continue; } Enumeration<InetAddress> iterAddress = network.getInetAddresses(); while (iterAddress.hasMoreElements()) { InetAddress address = iterAddress.nextElement(); if (address.isAnyLocalAddress() || address.isLoopbackAddress() || address.isMulticastAddress() || Inet6Address.class.isInstance(address)) { continue; } if (Inet4Address.class.isInstance(address)) { LOGGER.info( "add network interface name:" + network.getName() + ",host address:" + address.getHostAddress()); allInterfaceAddresses.put(network.getName(), address); } } } }
private @Nullable String getFirstLocalIPv4Address() { try { String hostAddress = null; final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { final NetworkInterface current = interfaces.nextElement(); if (!current.isUp() || current.isLoopback() || current.isVirtual() || current.isPointToPoint()) { continue; } final Enumeration<InetAddress> addresses = current.getInetAddresses(); while (addresses.hasMoreElements()) { final InetAddress currentAddr = addresses.nextElement(); if (currentAddr.isLoopbackAddress() || (currentAddr instanceof Inet6Address)) { continue; } if (hostAddress != null) { LOGGER.warn("Found multiple local interfaces - ignoring {}", currentAddr.getHostAddress()); } else { hostAddress = currentAddr.getHostAddress(); } } } return hostAddress; } catch (SocketException ex) { LOGGER.error("Could not retrieve network interface: {}", ex.getMessage(), ex); return null; } }
while (interfaces.hasMoreElements()) { final NetworkInterface current = interfaces.nextElement(); if (!current.isUp() || current.isLoopback() || current.isVirtual() || current.isPointToPoint()) { continue;
/** format network interface flags */ private static String formatFlags(NetworkInterface nic) throws SocketException { StringBuilder flags = new StringBuilder(); if (nic.isUp()) { flags.append("UP "); } if (nic.supportsMulticast()) { flags.append("MULTICAST "); } if (nic.isLoopback()) { flags.append("LOOPBACK "); } if (nic.isPointToPoint()) { flags.append("POINTOPOINT "); } if (nic.isVirtual()) { flags.append("VIRTUAL "); } flags.append("mtu:").append(nic.getMTU()); flags.append(" index:").append(nic.getIndex()); return flags.toString(); } }
private @Nullable String getIPv4inSubnet(String ipAddress, String subnetMask) { try { final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { final NetworkInterface current = interfaces.nextElement(); if (!current.isUp() || current.isLoopback() || current.isVirtual() || current.isPointToPoint()) { continue; } for (InterfaceAddress ifAddr : current.getInterfaceAddresses()) { InetAddress addr = ifAddr.getAddress(); if (addr.isLoopbackAddress() || (addr instanceof Inet6Address)) { continue; } String ipv4AddressOnInterface = addr.getHostAddress(); String subnetStringOnInterface = getIpv4NetAddress(ipv4AddressOnInterface, ifAddr.getNetworkPrefixLength()) + "/" + String.valueOf(ifAddr.getNetworkPrefixLength()); String configuredSubnetString = getIpv4NetAddress(ipAddress, Short.parseShort(subnetMask)) + "/" + subnetMask; // use first IP within this subnet if (subnetStringOnInterface.equals(configuredSubnetString)) { return ipv4AddressOnInterface; } } } } catch (SocketException ex) { LOGGER.error("Could not retrieve network interface: {}", ex.getMessage(), ex); } return null; }
log.info(String.format("PointToPoint? %s", networkInterface.isPointToPoint())); log.info(String.format("Supports multicast? %s", networkInterface.supportsMulticast())); log.info(String.format("Virtual? %s", networkInterface.isVirtual())); log.info(String.format("Hardware address: %s", Arrays.toString(networkInterface.getHardwareAddress()))); log.info(String.format("MTU: %s", networkInterface.getMTU()));
public static String[] getLocalCidrs() { final String defaultHostIp = getDefaultHostIp(); final List<String> cidrList = new ArrayList<String>(); try { for (final NetworkInterface ifc : IteratorUtil.enumerationAsIterable(NetworkInterface.getNetworkInterfaces())) { if (ifc.isUp() && !ifc.isVirtual() && !ifc.isLoopback()) { for (final InterfaceAddress address : ifc.getInterfaceAddresses()) { final InetAddress addr = address.getAddress(); final int prefixLength = address.getNetworkPrefixLength(); if (prefixLength < MAX_CIDR && prefixLength > 0) { final String ip = addr.getHostAddress(); if (ip.equalsIgnoreCase(defaultHostIp)) { cidrList.add(ipAndNetMaskToCidr(ip, getCidrNetmask(prefixLength))); } } } } } } catch (final SocketException e) { s_logger.warn("UnknownHostException in getLocalCidrs().", e); } return cidrList.toArray(new String[0]); }
!nic.isVirtual() && !nicName.startsWith("vnif") && !nicName.startsWith("vnbr") && !nicName.startsWith("peth") && !nicName.startsWith("vif") && !nicName.startsWith("virbr") && !nicName.contains(":")) { final String[] info = NetUtils.getNicParams(nicName);
NetworkInterface iface = entry.getKey(); InetAddress inetAddr = entry.getValue(); if (iface.isVirtual()) { continue;
/** * Checks given network interface and returns true when it should not be used for picking address. Reasons for skipping are * the interface is: down, virtual or loopback. */ private boolean skipInterface(NetworkInterface ni) throws SocketException { boolean skipInterface = !ni.isUp() || ni.isVirtual() || ni.isLoopback(); if (skipInterface && logger.isFineEnabled()) { logger.fine("Skipping NetworkInterface '" + ni.getName() + "': isUp=" + ni.isUp() + ", isVirtual=" + ni.isVirtual() + ", isLoopback=" + ni.isLoopback()); } return skipInterface; }