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

The following examples show how to use java.net.NetworkInterface#getIndex() . 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: NetworkDiagnostics.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private static Integer getInterfaceIndex(String ifname) {
    try {
        NetworkInterface ni = NetworkInterface.getByName(ifname);
        return ni.getIndex();
    } catch (NullPointerException | SocketException e) {
        return null;
    }
}
 
Example 2
Source File: InetUtils.java    From summerframework with Apache License 2.0 5 votes vote down vote up
public static InetAddress getVpnHostInfo() {
    InetAddress result = null;
    try {
        int lowest = Integer.MAX_VALUE;
        for (Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();
            nics.hasMoreElements();) {
            NetworkInterface ifc = nics.nextElement();
            if (ifc.isUp()) {
                LOGGER.error("Testing interface: " + ifc.getDisplayName());
                if (ifc.getIndex() < lowest || result == null) {
                    lowest = ifc.getIndex();
                } else if (result != null) {
                    continue;
                }
                for (Enumeration<InetAddress> addrs = ifc.getInetAddresses(); addrs.hasMoreElements();) {
                    InetAddress address = addrs.nextElement();
                    if (address instanceof Inet4Address && !address.isLoopbackAddress()
                        && address.getHostAddress().startsWith("172.")) {
                        LOGGER.error("Found non-loopback interface: " + ifc.getDisplayName());
                        result = address;
                    }
                }

            }
        }
    } catch (IOException ex) {
        LOGGER.error("Cannot get first non-loopback address", ex);
    }

    return result;

}
 
Example 3
Source File: NetUtils.java    From saluki with Apache License 2.0 5 votes vote down vote up
private static InetAddress getLocalAddress0() {
  InetAddress result = null;
  try {
    int lowest = Integer.MAX_VALUE;
    for (Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces(); nics
        .hasMoreElements();) {
      NetworkInterface ifc = nics.nextElement();
      if (ifc.isUp()) {
        logger.trace("Testing interface: " + ifc.getDisplayName());
        if (ifc.getIndex() < lowest || result == null) {
          lowest = ifc.getIndex();
        } else if (result != null) {
          continue;
        }
        for (Enumeration<InetAddress> addrs = ifc.getInetAddresses(); addrs.hasMoreElements();) {
          InetAddress address = addrs.nextElement();
          if (address instanceof Inet4Address && !address.isLoopbackAddress()) {
            logger.trace("Found non-loopback interface: " + ifc.getDisplayName());
            result = address;
          }
        }

      }
    }
  } catch (IOException ex) {
    logger.error("Cannot get first non-loopback address", ex);
  }
  if (result != null && isValidAddress(result)) {
    return result;
  }
  try {
    return InetAddress.getLocalHost();
  } catch (UnknownHostException e) {
    logger.warn("Unable to retrieve localhost");
  }
  return null;
}
 
Example 4
Source File: InetUtils.java    From summerframework with Apache License 2.0 4 votes vote down vote up
public InetAddress findFirstNonLoopbackAddress() {
    InetAddress result = null;
    try {
        int lowest = Integer.MAX_VALUE;
        for (Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();
            nics.hasMoreElements();) {
            NetworkInterface ifc = nics.nextElement();
            if (ifc.isUp()) {
                log.trace("Testing interface: " + ifc.getDisplayName());
                if (ifc.getIndex() < lowest || result == null) {
                    lowest = ifc.getIndex();
                } else if (result != null) {
                    continue;
                }

                if (!ignoreInterface(ifc.getDisplayName())) {
                    for (Enumeration<InetAddress> addrs = ifc.getInetAddresses(); addrs.hasMoreElements();) {
                        InetAddress address = addrs.nextElement();
                        if (address instanceof Inet4Address && !address.isLoopbackAddress()
                            && !ignoreAddress(address)) {
                            log.trace("Found non-loopback interface: " + ifc.getDisplayName());
                            result = address;
                        }
                    }
                }

            }
        }
    } catch (IOException ex) {
        log.error("Cannot get first non-loopback address", ex);
    }

    if (result != null) {
        return result;
    }

    try {
        return InetAddress.getLocalHost();
    } catch (UnknownHostException e) {
        log.warn("Unable to retrieve localhost");
    }

    return null;
}
 
Example 5
Source File: InetUtils.java    From summerframework with Apache License 2.0 4 votes vote down vote up
public InetAddress findFirstNonLoopbackAddress() {
    InetAddress result = null;
    try {
        int lowest = Integer.MAX_VALUE;
        for (Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();
            nics.hasMoreElements();) {
            NetworkInterface ifc = nics.nextElement();
            if (ifc.isUp()) {
                LOGGER.error("Testing interface: " + ifc.getDisplayName());
                if (ifc.getIndex() < lowest || result == null) {
                    lowest = ifc.getIndex();
                } else if (result != null) {
                    continue;
                }

                if (!ignoreInterface(ifc.getDisplayName())) {
                    for (Enumeration<InetAddress> addrs = ifc.getInetAddresses(); addrs.hasMoreElements();) {
                        InetAddress address = addrs.nextElement();
                        LOGGER.error("Test...: " + address + "==" + (address instanceof Inet4Address
                            && !address.isLoopbackAddress() && !ignoreAddress(address)));
                        if (address instanceof Inet4Address && !address.isLoopbackAddress()
                            && !ignoreAddress(address)) {
                            LOGGER.error("Found non-loopback interface: " + ifc.getDisplayName());
                            result = address;
                        }
                    }
                }
            }
        }
    } catch (IOException ex) {
        LOGGER.error("Cannot get first non-loopback address", ex);
    }

    if (result != null) {
        return result;
    }

    try {
        return InetAddress.getLocalHost();
    } catch (UnknownHostException e) {
        LOGGER.warn("Unable to retrieve localhost");
    }

    return null;
}
 
Example 6
Source File: InetTools.java    From pampas with Apache License 2.0 4 votes vote down vote up
public InetAddress findFirstNonLoopbackAddress() {
        InetAddress result = null;
        try {
            int lowest = Integer.MAX_VALUE;
            for (Enumeration<NetworkInterface> nics = NetworkInterface
                    .getNetworkInterfaces(); nics.hasMoreElements(); ) {
                NetworkInterface ifc = nics.nextElement();
                if (ifc.isUp()) {
//                    log.trace("Testing interface: " + ifc.getDisplayName());
                    if (ifc.getIndex() < lowest || result == null) {
                        lowest = ifc.getIndex();
                    } else if (result != null) {
                        continue;
                    }

                    // @formatter:off
                    if (!ignoreInterface(ifc.getDisplayName())) {
                        for (Enumeration<InetAddress> addrs = ifc
                                .getInetAddresses(); addrs.hasMoreElements(); ) {
                            InetAddress address = addrs.nextElement();
                            if (address instanceof Inet4Address && !address.isLoopbackAddress() && !ignoreAddress(address)) {
//                                log.trace("Found non-loopback interface: " + ifc.getDisplayName());
                                result = address;
                            }
                        }
                    }
                    // @formatter:on
                }
            }
        } catch (IOException ex) {
            log.error("Cannot get first non-loopback address", ex);
        }

        if (result != null) {
            return result;
        }

        try {
            return InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            log.warn("Unable to retrieve localhost");
        }

        return null;
    }
 
Example 7
Source File: DatagramChannelImpl.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
@Override
public <T> DatagramChannel setOption(SocketOption<T> name, T value)
    throws IOException
{
    Objects.requireNonNull(name);
    if (!supportedOptions().contains(name))
        throw new UnsupportedOperationException("'" + name + "' not supported");
    if (!name.type().isInstance(value))
        throw new IllegalArgumentException("Invalid value '" + value + "'");

    synchronized (stateLock) {
        ensureOpen();

        ProtocolFamily family = familyFor(name);

        if (name == StandardSocketOptions.IP_MULTICAST_IF) {
            NetworkInterface interf = (NetworkInterface)value;
            if (family == StandardProtocolFamily.INET6) {
                int index = interf.getIndex();
                if (index == -1)
                    throw new IOException("Network interface cannot be identified");
                Net.setInterface6(fd, index);
            } else {
                // need IPv4 address to identify interface
                Inet4Address target = Net.anyInet4Address(interf);
                if (target == null)
                    throw new IOException("Network interface not configured for IPv4");
                int targetAddress = Net.inet4AsInt(target);
                Net.setInterface4(fd, targetAddress);
            }
            return this;
        }
        if (name == StandardSocketOptions.SO_REUSEADDR
            && Net.useExclusiveBind() && localAddress != null) {
            reuseAddressEmulated = true;
            this.isReuseAddress = (Boolean)value;
        }

        // remaining options don't need any special handling
        Net.setSocketOption(fd, family, name, value);
        return this;
    }
}
 
Example 8
Source File: InetUtils.java    From spring-cloud-commons with Apache License 2.0 4 votes vote down vote up
public InetAddress findFirstNonLoopbackAddress() {
	InetAddress result = null;
	try {
		int lowest = Integer.MAX_VALUE;
		for (Enumeration<NetworkInterface> nics = NetworkInterface
				.getNetworkInterfaces(); nics.hasMoreElements();) {
			NetworkInterface ifc = nics.nextElement();
			if (ifc.isUp()) {
				this.log.trace("Testing interface: " + ifc.getDisplayName());
				if (ifc.getIndex() < lowest || result == null) {
					lowest = ifc.getIndex();
				}
				else if (result != null) {
					continue;
				}

				// @formatter:off
				if (!ignoreInterface(ifc.getDisplayName())) {
					for (Enumeration<InetAddress> addrs = ifc
							.getInetAddresses(); addrs.hasMoreElements();) {
						InetAddress address = addrs.nextElement();
						if (address instanceof Inet4Address
								&& !address.isLoopbackAddress()
								&& isPreferredAddress(address)) {
							this.log.trace("Found non-loopback interface: "
									+ ifc.getDisplayName());
							result = address;
						}
					}
				}
				// @formatter:on
			}
		}
	}
	catch (IOException ex) {
		this.log.error("Cannot get first non-loopback address", ex);
	}

	if (result != null) {
		return result;
	}

	try {
		return InetAddress.getLocalHost();
	}
	catch (UnknownHostException e) {
		this.log.warn("Unable to retrieve localhost");
	}

	return null;
}