List of usage examples for java.net InetAddress isLinkLocalAddress
public boolean isLinkLocalAddress()
From source file:org.ofbiz.passport.util.PassportUtil.java
public static String getEnvPrefixByHost(HttpServletRequest request) { String prefix = "test"; try {//from w ww . j a v a 2 s . c om InetAddress[] addresses = InetAddress.getAllByName(request.getServerName()); for (InetAddress address : addresses) { if (address.isAnyLocalAddress() || address.isLinkLocalAddress() || address.isLoopbackAddress()) { return prefix; } } prefix = "live"; } catch (UnknownHostException e) { Debug.logError(e.getMessage(), module); } return prefix; }
From source file:com.vinexs.tool.NetworkManager.java
public static InetAddress getInetAddress(Context context) { if (haveNetwork(context)) { return null; }/*from w w w .ja va 2s. c om*/ if (isWifiNetwork(context)) { int ipAddress = ((WifiManager) context.getSystemService(Context.WIFI_SERVICE)).getConnectionInfo() .getIpAddress(); if (ipAddress == 0) { return null; } return intToInet(ipAddress); } try { Enumeration<NetworkInterface> netinterfaces = NetworkInterface.getNetworkInterfaces(); while (netinterfaces.hasMoreElements()) { NetworkInterface netinterface = netinterfaces.nextElement(); Enumeration<InetAddress> adresses = netinterface.getInetAddresses(); while (adresses.hasMoreElements()) { InetAddress address = adresses.nextElement(); if (!address.isLoopbackAddress() && !address.isLinkLocalAddress()) { return address; } } } } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:eu.eubrazilcc.lvl.core.util.NetworkingUtils.java
/** * Gets the first public IP address of the host. If no public address are found, one of the private * IPs are randomly selected. Otherwise, it returns {@code localhost}. * @return the first public IP address of the host. *///from w ww. j av a2 s . co m public static final String getInet4Address() { String inet4Address = null; final List<String> localAddresses = new ArrayList<String>(); try { final Enumeration<NetworkInterface> networks = NetworkInterface.getNetworkInterfaces(); if (networks != null) { final List<NetworkInterface> ifs = Collections.list(networks); for (int i = 0; i < ifs.size() && inet4Address == null; i++) { final Enumeration<InetAddress> inetAddresses = ifs.get(i).getInetAddresses(); if (inetAddresses != null) { final List<InetAddress> addresses = Collections.list(inetAddresses); for (int j = 0; j < addresses.size() && inet4Address == null; j++) { final InetAddress address = addresses.get(j); if (address instanceof Inet4Address && !address.isAnyLocalAddress() && !address.isLinkLocalAddress() && !address.isLoopbackAddress() && StringUtils.isNotBlank(address.getHostAddress())) { final String hostAddress = address.getHostAddress().trim(); if (!hostAddress.startsWith("10.") && !hostAddress.startsWith("172.16.") && !hostAddress.startsWith("192.168.")) { inet4Address = hostAddress; } else { localAddresses.add(hostAddress); } LOGGER.trace( "IP found - Name: " + address.getHostName() + ", Addr: " + hostAddress); } } } } } } catch (Exception e) { LOGGER.warn("Failed to discover public IP address for this host", e); } return (StringUtils.isNotBlank(inet4Address) ? inet4Address : (!localAddresses.isEmpty() ? localAddresses.get(new Random().nextInt(localAddresses.size())) : "localhost")).trim(); }
From source file:org.dd4t.core.util.HttpUtils.java
/** * Checking for local ip addresses, e.g. * <p/>//from w ww . ja v a 2 s. c om * <pre> * 10.x.x.x * 172.[16-31].x.x * 192.168.x.x * 127.0.0.1 * </pre> */ private static boolean isLocalDomainAddress(final String ipAddress) throws UnknownHostException { final InetAddress inetAddress = InetAddress.getByName(ipAddress); return inetAddress.isAnyLocalAddress() || inetAddress.isLinkLocalAddress() || inetAddress.isMulticastAddress() || inetAddress.isSiteLocalAddress(); }
From source file:net.centro.rtb.monitoringcenter.infos.NodeInfo.java
private static String detectPublicIpAddress() { Enumeration<NetworkInterface> networkInterfaces = null; try {//from w ww. j a v a 2 s . c o m networkInterfaces = NetworkInterface.getNetworkInterfaces(); } catch (SocketException e) { logger.debug("Unable to obtain network interfaces!", e); return null; } for (NetworkInterface networkInterface : Collections.list(networkInterfaces)) { boolean isLoopback = false; try { isLoopback = networkInterface.isLoopback(); } catch (SocketException e) { logger.debug("Unable to identify if a network interface is a loopback or not"); } if (!isLoopback) { Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses(); while (inetAddresses.hasMoreElements()) { InetAddress inetAddress = inetAddresses.nextElement(); if (Inet4Address.class.isInstance(inetAddress)) { if (!inetAddress.isLoopbackAddress() && !inetAddress.isAnyLocalAddress() && !inetAddress.isLinkLocalAddress() && !inetAddress.isSiteLocalAddress()) { return inetAddress.getHostAddress(); } } } } } return null; }
From source file:org.opendaylight.lispflowmapping.implementation.lisp.MapServer.java
private static InetAddress getLocalAddress() { try {/*from w w w. jav a 2 s .c om*/ Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface current = interfaces.nextElement(); LOG.debug("Interface " + current.toString()); if (!current.isUp() || current.isLoopback() || current.isVirtual()) { continue; } Enumeration<InetAddress> addresses = current.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress current_addr = addresses.nextElement(); // Skip loopback and link local addresses if (current_addr.isLoopbackAddress() || current_addr.isLinkLocalAddress()) { continue; } LOG.debug(current_addr.getHostAddress()); return current_addr; } } } catch (SocketException se) { LOG.debug("Caught socket exceptio", se); } return null; }
From source file:com.cloud.utils.UriUtils.java
public static Pair<String, Integer> validateUrl(String format, String url) throws IllegalArgumentException { try {//from www . j ava 2 s . c om URI uri = new URI(url); if ((uri.getScheme() == null) || (!uri.getScheme().equalsIgnoreCase("http") && !uri.getScheme().equalsIgnoreCase("https") && !uri.getScheme().equalsIgnoreCase("file"))) { throw new IllegalArgumentException("Unsupported scheme for url: " + url); } int port = uri.getPort(); if (!(port == 80 || port == 8080 || port == 443 || port == -1)) { throw new IllegalArgumentException("Only ports 80, 8080 and 443 are allowed"); } if (port == -1 && uri.getScheme().equalsIgnoreCase("https")) { port = 443; } else if (port == -1 && uri.getScheme().equalsIgnoreCase("http")) { port = 80; } String host = uri.getHost(); try { InetAddress hostAddr = InetAddress.getByName(host); if (hostAddr.isAnyLocalAddress() || hostAddr.isLinkLocalAddress() || hostAddr.isLoopbackAddress() || hostAddr.isMulticastAddress()) { throw new IllegalArgumentException("Illegal host specified in url"); } if (hostAddr instanceof Inet6Address) { throw new IllegalArgumentException( "IPV6 addresses not supported (" + hostAddr.getHostAddress() + ")"); } } catch (UnknownHostException uhe) { throw new IllegalArgumentException("Unable to resolve " + host); } // verify format if (format != null) { String uripath = uri.getPath(); checkFormat(format, uripath); } return new Pair<String, Integer>(host, port); } catch (URISyntaxException use) { throw new IllegalArgumentException("Invalid URL: " + url); } }
From source file:Main.java
public static String filterIP(final InetAddress inetAddress) { try {/*w w w. j a v a 2 s . c o m*/ final String ipVersion; if (inetAddress instanceof Inet4Address) ipVersion = "ipv4"; else if (inetAddress instanceof Inet6Address) ipVersion = "ipv6"; else ipVersion = "ipv?"; if (inetAddress.isAnyLocalAddress()) return "wildcard"; if (inetAddress.isSiteLocalAddress()) return "site_local_" + ipVersion; if (inetAddress.isLinkLocalAddress()) return "link_local_" + ipVersion; if (inetAddress.isLoopbackAddress()) return "loopback_" + ipVersion; return inetAddress.getHostAddress(); } catch (final IllegalArgumentException e) { return "illegal_ip"; } }
From source file:at.alladin.rmbt.shared.Helperfunctions.java
public static boolean isIPLocal(final InetAddress adr) { return adr.isLinkLocalAddress() || adr.isLoopbackAddress() || adr.isSiteLocalAddress(); }
From source file:org.wso2.carbon.apimgt.hybrid.gateway.configurator.Configurator.java
/** * Retrieve host name, mac address of the device * * @return details Map<String, String> */// w w w . ja va 2 s . c o m protected static Map<String, String> getDeviceDetails() { InetAddress ip; String hostName = ""; String macAddress = ConfigConstants.DEFAULT_MAC_ADDRESS; Map<String, String> details = new HashMap(); try { ip = InetAddress.getLocalHost(); hostName = ip.getHostName(); Enumeration<NetworkInterface> networkInterfaceEnumeration = NetworkInterface.getNetworkInterfaces(); while (networkInterfaceEnumeration.hasMoreElements()) { NetworkInterface networkInterface = networkInterfaceEnumeration.nextElement(); Enumeration<InetAddress> enumeration = networkInterface.getInetAddresses(); for (; enumeration.hasMoreElements();) { InetAddress address = enumeration.nextElement(); if (!address.isLoopbackAddress() && !address.isLinkLocalAddress() && address.isSiteLocalAddress()) { byte[] mac = networkInterface.getHardwareAddress(); if (mac != null) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < mac.length; i++) { //Construct mac address sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? ConfigConstants.DELIMITER : "")); } macAddress = sb.toString(); break; } } } } } catch (UnknownHostException | SocketException e) { log.error("Error while retrieving mac address", e); Runtime.getRuntime().exit(1); } details.put(ConfigConstants.HOST_NAME, hostName); details.put(ConfigConstants.MAC_ADDRESS, macAddress); return details; }