List of usage examples for java.net InetAddress isLoopbackAddress
public boolean isLoopbackAddress()
From source file:org.apache.carbondata.core.dictionary.service.AbstractDictionaryServer.java
public String findLocalIpAddress(Logger LOGGER) { try {/*w w w. ja v a 2 s .co m*/ String defaultIpOverride = System.getenv("SPARK_LOCAL_IP"); if (defaultIpOverride != null) { return defaultIpOverride; } else { InetAddress address = InetAddress.getLocalHost(); if (address.isLoopbackAddress()) { // Address resolves to something like 127.0.1.1, which happens on Debian; try to find // a better address using the local network interfaces // getNetworkInterfaces returns ifs in reverse order compared to ifconfig output order // on unix-like system. On windows, it returns in index order. // It's more proper to pick ip address following system output order. Enumeration<NetworkInterface> activeNetworkIFs = NetworkInterface.getNetworkInterfaces(); List<NetworkInterface> reOrderedNetworkIFs = new ArrayList<NetworkInterface>(); while (activeNetworkIFs.hasMoreElements()) { reOrderedNetworkIFs.add(activeNetworkIFs.nextElement()); } if (!SystemUtils.IS_OS_WINDOWS) { Collections.reverse(reOrderedNetworkIFs); } for (NetworkInterface ni : reOrderedNetworkIFs) { Enumeration<InetAddress> inetAddresses = ni.getInetAddresses(); while (inetAddresses.hasMoreElements()) { InetAddress addr = inetAddresses.nextElement(); if (!addr.isLinkLocalAddress() && !addr.isLoopbackAddress() && addr instanceof Inet4Address) { // We've found an address that looks reasonable! LOGGER.warn("Your hostname, " + InetAddress.getLocalHost().getHostName() + " resolves to a loopback address: " + address.getHostAddress() + "; using " + addr.getHostAddress() + " instead (on interface " + ni.getName() + ")"); LOGGER.warn("Set SPARK_LOCAL_IP if you need to bind to another address"); return addr.getHostAddress(); } } LOGGER.warn("Your hostname, " + InetAddress.getLocalHost().getHostName() + " resolves to a loopback address: " + address.getHostAddress() + ", but we couldn't find any external IP address!"); LOGGER.warn("Set SPARK_LOCAL_IP if you need to bind to another address"); } } return address.getHostAddress(); } } catch (UnknownHostException e) { LOGGER.error("do not get local host address:" + e.getMessage(), e); throw new RuntimeException(e); } catch (SocketException e) { LOGGER.error("do not get net work interface:" + e.getMessage(), e); throw new RuntimeException(e); } }
From source file:com.webtide.jetty.load.generator.web.MonitorServlet.java
private boolean isLoopbackAddress(String address) { try {/*from w w w . j av a 2 s.c o m*/ InetAddress addr = InetAddress.getByName(address); return addr.isLoopbackAddress(); } catch (UnknownHostException e) { LOG.warn("Warning: attempt to access statistics servlet from " + address, e); return false; } }
From source file:at.alladin.rmbt.shared.Helperfunctions.java
public static String IpType(InetAddress inetAddress) { try {//w w w . j a v a 2 s . co 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_" + ipVersion; if (inetAddress.isSiteLocalAddress()) return "site_local_" + ipVersion; if (inetAddress.isLinkLocalAddress()) return "link_local_" + ipVersion; if (inetAddress.isLoopbackAddress()) return "loopback_" + ipVersion; return "public_" + ipVersion; } catch (final IllegalArgumentException e) { return "illegal_ip"; } }
From source file:com.motelabs.chromemote.bridge.MainActivity.java
private String getLocalIpAddress() { try {//from w ww . j a v a2 s . c o m for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en .hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())) { String ipAddr = inetAddress.getHostAddress(); return ipAddr; } } } } catch (SocketException ex) { //Log.d(TAG, ex.toString()); } return null; }
From source file:org.apache.metron.enrichment.adapters.geo.GeoAdapter.java
@SuppressWarnings("unchecked") @Override/*from ww w .ja va2 s.c o m*/ public JSONObject enrich(CacheKey value) { JSONObject enriched = new JSONObject(); if (!resetConnectionIfNecessary()) { _LOG.error( "Enrichment failure, cannot maintain a connection to JDBC. Please check connection. In the meantime, I'm not enriching."); return enriched; } try { InetAddress addr = InetAddress.getByName(value.getValue(String.class)); if (addr.isAnyLocalAddress() || addr.isLoopbackAddress() || addr.isSiteLocalAddress() || addr.isMulticastAddress() || !ipvalidator.isValidInet4Address(value.getValue(String.class))) { return new JSONObject(); } String locidQuery = "select IPTOLOCID(\"" + value.getValue() + "\") as ANS"; ResultSet resultSet = statement.executeQuery(locidQuery); String locid = null; if (resultSet.next()) { locid = resultSet.getString("ANS"); } resultSet.close(); if (locid == null) return new JSONObject(); String geoQuery = "select * from location where locID = " + locid; resultSet = statement.executeQuery(geoQuery); if (resultSet.next()) { enriched.put("locID", resultSet.getString("locID")); enriched.put("country", resultSet.getString("country")); enriched.put("city", resultSet.getString("city")); enriched.put("postalCode", resultSet.getString("postalCode")); enriched.put("latitude", resultSet.getString("latitude")); enriched.put("longitude", resultSet.getString("longitude")); enriched.put("dmaCode", resultSet.getString("dmaCode")); enriched.put("location_point", enriched.get("latitude") + "," + enriched.get("longitude")); } resultSet.close(); } catch (Exception e) { _LOG.error("Enrichment failure: " + e.getMessage(), e); return new JSONObject(); } return enriched; }
From source file:org.apache.metron.enrichment.adapters.geo.GeoLiteDatabase.java
private boolean isIneligibleAddress(String ipStr, InetAddress addr) { return addr.isAnyLocalAddress() || addr.isLoopbackAddress() || addr.isSiteLocalAddress() || addr.isMulticastAddress() || !ipvalidator.isValidInet4Address(ipStr); }
From source file:org.smartfrog.test.unit.net.BasicNetworkingTest.java
/** * this only valid in an IPv6 context/*from w w w . ja v a 2 s .com*/ * @throws UnknownHostException network problems */ public void testIPv6() throws UnknownHostException { InetAddress addr; addr = InetAddress.getByName("::1"); logAddr("IPv6", addr); assertTrue(addr.isLoopbackAddress()); }
From source file:com.castlemock.web.basis.web.mvc.controller.AbstractController.java
/** * The method returns the local address (Not link local address, not loopback address) which the server is deployed on. * If the method does not find any INet4Address that is neither link local address and loopback address, the method * will return the the address 127.0.0.1 * @return Returns the local address or 127.0.0.1 if no address was found * @throws SocketException Upon failing to extract network interfaces *///from ww w . j av a 2s .c om public String getHostAddress() throws SocketException { if (endpointAddress != null && !endpointAddress.isEmpty()) { return endpointAddress; } final Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); while (networkInterfaces.hasMoreElements()) { final NetworkInterface networkInterface = networkInterfaces.nextElement(); final Enumeration<InetAddress> addresses = networkInterface.getInetAddresses(); while (addresses.hasMoreElements()) { final InetAddress address = addresses.nextElement(); if (!address.isLinkLocalAddress() && !address.isLoopbackAddress() && address instanceof Inet4Address) { return address.getHostAddress(); } } } return LOCAL_ADDRESS; }
From source file:org.mule.module.http.functional.listener.HttpListenerConfigFunctionalTestCase.java
private String getNonLocalhostIp() { try {/* w ww . ja va2s .c o m*/ Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces(); for (NetworkInterface networkInterface : Collections.list(nets)) { final Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses(); while (inetAddresses.hasMoreElements()) { InetAddress inetAddress = inetAddresses.nextElement(); if (!inetAddress.isLoopbackAddress() && IPADDRESS_PATTERN.matcher(inetAddress.getHostAddress()).find()) { return inetAddress.getHostAddress(); } } } throw new RuntimeException("Could not find network interface different from localhost"); } catch (SocketException e) { throw new RuntimeException(e); } }
From source file:org.smartfrog.test.unit.net.BasicNetworkingTest.java
/** * @throws UnknownHostException network problems *///from w w w . ja v a2 s. co m public void testLoopback() throws UnknownHostException { InetAddress addr; addr = InetAddress.getByName(null); logAddr("loopback", addr); assertTrue(addr.isLoopbackAddress()); }