List of usage examples for java.net InetAddress isSiteLocalAddress
public boolean isSiteLocalAddress()
From source file:org.fdroid.enigtext.mms.MmsCommunication.java
private static void checkRouteToHost(Context context, String host, boolean usingMmsRadio) throws IOException { InetAddress inetAddress = InetAddress.getByName(host); if (!usingMmsRadio) { if (inetAddress.isSiteLocalAddress()) { throw new IOException("RFC1918 address in non-MMS radio situation!"); }/*from www. j a v a 2 s . c om*/ return; } Log.w("MmsCommunication", "Checking route to address: " + host + " , " + inetAddress.getHostAddress()); byte[] ipAddressBytes = inetAddress.getAddress(); if (ipAddressBytes != null && ipAddressBytes.length == 4) { int ipAddress = Conversions.byteArrayToIntLittleEndian(ipAddressBytes, 0); ConnectivityManager manager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); if (!manager.requestRouteToHost(MmsDownloader.TYPE_MOBILE_MMS, ipAddress)) throw new IOException("Connection manager could not obtain route to host."); } }
From source file:org.kuali.rice.core.api.util.RiceUtilities.java
/** * @return the current environment's IP address, taking into account the Internet connection to any of the available * machine's Network interfaces. Examples of the outputs can be in octatos or in IPV6 format. * * fec0:0:0:9:213:e8ff:fef1:b717%4 siteLocal: true isLoopback: false isIPV6: true * ============================================ 130.212.150.216 <<<<<<<<<<<------------- This is the one we * want to grab so that we can. siteLocal: false address the DSP on the network. isLoopback: false isIPV6: * false ==> lo ============================================ 0:0:0:0:0:0:0:1%1 siteLocal: false isLoopback: * true isIPV6: true ============================================ 127.0.0.1 siteLocal: false isLoopback: * true isIPV6: false//ww w.j a va 2 s.com */ private static String getCurrentEnvironmentNetworkIp() { Enumeration<NetworkInterface> netInterfaces = null; try { netInterfaces = NetworkInterface.getNetworkInterfaces(); } catch (SocketException e) { LOG.error("Somehow we have a socket error...", e); return "127.0.0.1"; } while (netInterfaces.hasMoreElements()) { NetworkInterface ni = netInterfaces.nextElement(); Enumeration<InetAddress> address = ni.getInetAddresses(); while (address.hasMoreElements()) { InetAddress addr = address.nextElement(); if (!addr.isLoopbackAddress() && !addr.isSiteLocalAddress() && !(addr.getHostAddress().indexOf(":") > -1)) { return addr.getHostAddress(); } } } try { return InetAddress.getLocalHost().getHostAddress(); } catch (UnknownHostException e) { return "127.0.0.1"; } }
From source file:com.groupon.odo.proxylib.Utils.java
/** * This function returns the first external IP address encountered * * @return IP address or null/*from ww w .j av a 2s.c o m*/ * @throws Exception */ public static String getPublicIPAddress() throws Exception { final String IPV4_REGEX = "\\A(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}\\z"; String ipAddr = null; Enumeration e = NetworkInterface.getNetworkInterfaces(); while (e.hasMoreElements()) { NetworkInterface n = (NetworkInterface) e.nextElement(); Enumeration ee = n.getInetAddresses(); while (ee.hasMoreElements()) { InetAddress i = (InetAddress) ee.nextElement(); // Pick the first non loop back address if ((!i.isLoopbackAddress() && i.isSiteLocalAddress()) || i.getHostAddress().matches(IPV4_REGEX)) { ipAddr = i.getHostAddress(); break; } } if (ipAddr != null) { break; } } return ipAddr; }
From source file:com.vaadin.tests.tb3.PrivateTB3Configuration.java
/** * Tries to automatically determine the IP address of the machine the test * is running on./*from w w w .j ava 2s . c o m*/ * * @return An IP address of one of the network interfaces in the machine. * @throws RuntimeException * if there was an error or no IP was found */ private static String findAutoHostname() { try { Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface nwInterface = interfaces.nextElement(); if (!nwInterface.isUp() || nwInterface.isLoopback() || nwInterface.isVirtual()) { continue; } Enumeration<InetAddress> addresses = nwInterface.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress address = addresses.nextElement(); if (address.isLoopbackAddress()) { continue; } if (address.isSiteLocalAddress()) { return address.getHostAddress(); } } } } catch (SocketException e) { throw new RuntimeException("Could not enumerate "); } throw new RuntimeException("No compatible (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16) ip address found."); }
From source file:com.tingtingapps.securesms.mms.LegacyMmsConnection.java
@SuppressWarnings("TryWithIdenticalCatches") protected static boolean checkRouteToHost(Context context, String host, boolean usingMmsRadio) throws IOException { InetAddress inetAddress = InetAddress.getByName(host); if (!usingMmsRadio) { if (inetAddress.isSiteLocalAddress()) { throw new IOException("RFC1918 address in non-MMS radio situation!"); }//w w w . j a v a 2 s .c o m Log.w(TAG, "returning vacuous success since MMS radio is not in use"); return true; } if (inetAddress == null) { throw new IOException("Unable to lookup host: InetAddress.getByName() returned null."); } byte[] ipAddressBytes = inetAddress.getAddress(); if (ipAddressBytes == null) { Log.w(TAG, "resolved IP address bytes are null, returning true to attempt a connection anyway."); return true; } Log.w(TAG, "Checking route to address: " + host + ", " + inetAddress.getHostAddress()); ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); try { final Method requestRouteMethod = manager.getClass().getMethod("requestRouteToHostAddress", Integer.TYPE, InetAddress.class); final boolean routeToHostObtained = (Boolean) requestRouteMethod.invoke(manager, MmsRadio.TYPE_MOBILE_MMS, inetAddress); Log.w(TAG, "requestRouteToHostAddress(" + inetAddress + ") -> " + routeToHostObtained); return routeToHostObtained; } catch (NoSuchMethodException nsme) { Log.w(TAG, nsme); } catch (IllegalAccessException iae) { Log.w(TAG, iae); } catch (InvocationTargetException ite) { Log.w(TAG, ite); } final int ipAddress = Conversions.byteArrayToIntLittleEndian(ipAddressBytes, 0); final boolean routeToHostObtained = manager.requestRouteToHost(MmsRadio.TYPE_MOBILE_MMS, ipAddress); Log.w(TAG, "requestRouteToHost(" + ipAddress + ") -> " + routeToHostObtained); return routeToHostObtained; }
From source file:org.apache.htrace.core.TracerId.java
/** * <p>Get the best IP address that represents this node.</p> * * This is complicated since nodes can have multiple network interfaces, * and each network interface can have multiple IP addresses. What we're * looking for here is an IP address that will serve to identify this node * to HTrace. So we prefer site-local addresess (i.e. private ones on the * LAN) to publicly routable interfaces. If there are multiple addresses * to choose from, we select the one which comes first in textual sort * order. This should ensure that we at least consistently call each node * by a single name./*from ww w .j a v a 2 s. co m*/ */ static String getBestIpString() { Enumeration<NetworkInterface> ifaces; try { ifaces = NetworkInterface.getNetworkInterfaces(); } catch (SocketException e) { LOG.error("Error getting network interfaces", e); return "127.0.0.1"; } TreeSet<String> siteLocalCandidates = new TreeSet<String>(); TreeSet<String> candidates = new TreeSet<String>(); while (ifaces.hasMoreElements()) { NetworkInterface iface = ifaces.nextElement(); for (Enumeration<InetAddress> addrs = iface.getInetAddresses(); addrs.hasMoreElements();) { InetAddress addr = addrs.nextElement(); if (!addr.isLoopbackAddress()) { if (addr.isSiteLocalAddress()) { siteLocalCandidates.add(addr.getHostAddress()); } else { candidates.add(addr.getHostAddress()); } } } } if (!siteLocalCandidates.isEmpty()) { return siteLocalCandidates.first(); } if (!candidates.isEmpty()) { return candidates.first(); } return "127.0.0.1"; }
From source file:NetworkUtil.java
/** * @return the current environment's IP address, taking into account the Internet connection to any of the available * machine's Network interfaces. Examples of the outputs can be in octats or in IPV6 format. * <pre>// w w w .j a va2 s .c om * ==> wlan0 * * fec0:0:0:9:213:e8ff:fef1:b717%4 * siteLocal: true * isLoopback: false isIPV6: true * 130.212.150.216 <<<<<<<<<<<------------- This is the one we want to grab so that we can. * siteLocal: false address the DSP on the network. * isLoopback: false * isIPV6: false * * ==> lo * 0:0:0:0:0:0:0:1%1 * siteLocal: false * isLoopback: true * isIPV6: true * 127.0.0.1 * siteLocal: false * isLoopback: true * isIPV6: false * </pre> */ public static String getCurrentEnvironmentNetworkIp() { if (currentHostIpAddress == null) { Enumeration<NetworkInterface> netInterfaces = null; try { netInterfaces = NetworkInterface.getNetworkInterfaces(); while (netInterfaces.hasMoreElements()) { NetworkInterface ni = netInterfaces.nextElement(); Enumeration<InetAddress> address = ni.getInetAddresses(); while (address.hasMoreElements()) { InetAddress addr = address.nextElement(); // log.debug("Inetaddress:" + addr.getHostAddress() + " loop? " + addr.isLoopbackAddress() + " local? " // + addr.isSiteLocalAddress()); if (!addr.isLoopbackAddress() && addr.isSiteLocalAddress() && !(addr.getHostAddress().indexOf(":") > -1)) { currentHostIpAddress = addr.getHostAddress(); } } } if (currentHostIpAddress == null) { currentHostIpAddress = "127.0.0.1"; } } catch (SocketException e) { // log.error("Somehow we have a socket error acquiring the host IP... Using loopback instead..."); currentHostIpAddress = "127.0.0.1"; } } return currentHostIpAddress; }
From source file:org.magnum.dataup.VideoController.java
private static InetAddress getLocalHostLANAddress() throws UnknownHostException { try {/*from w ww .j av a 2 s. c o m*/ InetAddress candidateAddress = null; // Iterate all NICs (network interface cards)... for (Enumeration ifaces = NetworkInterface.getNetworkInterfaces(); ifaces.hasMoreElements();) { NetworkInterface iface = (NetworkInterface) ifaces.nextElement(); // Iterate all IP addresses assigned to each card... for (Enumeration inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) { InetAddress inetAddr = (InetAddress) inetAddrs.nextElement(); if (!inetAddr.isLoopbackAddress()) { if (inetAddr.isSiteLocalAddress()) { // Found non-loopback site-local address. Return it immediately... return inetAddr; } else if (candidateAddress == null) { // Found non-loopback address, but not necessarily site-local. // Store it as a candidate to be returned if site-local address is not subsequently found... candidateAddress = inetAddr; // Note that we don't repeatedly assign non-loopback non-site-local addresses as candidates, // only the first. For subsequent iterations, candidate will be non-null. } } } } if (candidateAddress != null) { // We did not find a site-local address, but we found some other non-loopback address. // Server might have a non-site-local address assigned to its NIC (or it might be running // IPv6 which deprecates the "site-local" concept). // Return this non-loopback candidate address... return candidateAddress; } // At this point, we did not find a non-loopback address. // Fall back to returning whatever InetAddress.getLocalHost() returns... InetAddress jdkSuppliedAddress = InetAddress.getLocalHost(); if (jdkSuppliedAddress == null) { throw new UnknownHostException( "The JDK InetAddress.getLocalHost() method unexpectedly returned null."); } return jdkSuppliedAddress; } catch (Exception e) { UnknownHostException unknownHostException = new UnknownHostException( "Failed to determine LAN address: " + e); unknownHostException.initCause(e); throw unknownHostException; } }
From source file:org.thoughtcrime.securesms.mms.LegacyMmsConnection.java
@SuppressWarnings("TryWithIdenticalCatches") protected static boolean checkRouteToHost(Context context, String host, boolean usingMmsRadio) throws IOException { InetAddress inetAddress = InetAddress.getByName(host); if (!usingMmsRadio) { if (inetAddress.isSiteLocalAddress()) { throw new IOException("RFC1918 address in non-MMS radio situation!"); }/*from ww w . ja v a 2 s. com*/ Log.w(TAG, "returning vacuous success since MMS radio is not in use"); return true; } if (inetAddress == null) { throw new IOException("Unable to lookup host: InetAddress.getByName() returned null."); } byte[] ipAddressBytes = inetAddress.getAddress(); if (ipAddressBytes == null) { Log.w(TAG, "resolved IP address bytes are null, returning true to attempt a connection anyway."); return true; } Log.i(TAG, "Checking route to address: " + host + ", " + inetAddress.getHostAddress()); ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); try { final Method requestRouteMethod = manager.getClass().getMethod("requestRouteToHostAddress", Integer.TYPE, InetAddress.class); final boolean routeToHostObtained = (Boolean) requestRouteMethod.invoke(manager, MmsRadio.TYPE_MOBILE_MMS, inetAddress); Log.i(TAG, "requestRouteToHostAddress(" + inetAddress + ") -> " + routeToHostObtained); return routeToHostObtained; } catch (NoSuchMethodException nsme) { Log.w(TAG, nsme); } catch (IllegalAccessException iae) { Log.w(TAG, iae); } catch (InvocationTargetException ite) { Log.w(TAG, ite); } return false; }
From source file:de.pawlidi.openaletheia.utils.AletheiaUtils.java
/** * // w w w. j a va2s . c o m * @return * @throws UnknownHostException */ public static InetAddress getLocalIpAddress() throws UnknownHostException { try { InetAddress localAddress = null; // load all existed network interfaces for (Enumeration<?> networkInterfaces = NetworkInterface.getNetworkInterfaces(); networkInterfaces .hasMoreElements();) { NetworkInterface networkInterface = (NetworkInterface) networkInterfaces.nextElement(); for (Enumeration<?> ipAddresses = networkInterface.getInetAddresses(); ipAddresses .hasMoreElements();) { InetAddress ipAddress = (InetAddress) ipAddresses.nextElement(); if (!ipAddress.isLoopbackAddress()) { if (ipAddress.isSiteLocalAddress()) { return ipAddress; } else if (localAddress == null) { localAddress = ipAddress; } } } } if (localAddress != null) { return localAddress; } // try to get localhost address localAddress = InetAddress.getLocalHost(); if (localAddress == null) { throw new UnknownHostException("Could not load localhost ip address"); } return localAddress; } catch (Exception e) { UnknownHostException unknownHostException = new UnknownHostException( "Could not load localhost ip address " + e); unknownHostException.initCause(e); throw unknownHostException; } }