List of usage examples for java.net InetAddress getHostAddress
public String getHostAddress()
From source file:org.thoughtcrime.securesms.mms.MmsConnection.java
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 w ww. j a va 2 s .c om Log.w(TAG, "returning vacuous success since MMS radio is not in use"); return true; } byte[] ipAddressBytes = inetAddress.getAddress(); if (ipAddressBytes == null || ipAddressBytes.length != 4) { Log.w(TAG, "returning vacuous success since android.net package doesn't support IPv6"); return true; } Log.w(TAG, "Checking route to address: " + host + ", " + inetAddress.getHostAddress()); ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); int ipAddress = Conversions.byteArrayToIntLittleEndian(ipAddressBytes, 0); boolean routeToHostObtained = manager.requestRouteToHost(MmsRadio.TYPE_MOBILE_MMS, ipAddress); Log.w(TAG, "requestRouteToHost result: " + routeToHostObtained); return routeToHostObtained; }
From source file:com.buaa.cfs.utils.NetUtils.java
/** * Add all addresses associated with the given nif in the given subnet to the given list. *//*ww w . ja va 2 s. c o m*/ private static void addMatchingAddrs(NetworkInterface nif, SubnetInfo subnetInfo, List<InetAddress> addrs) { Enumeration<InetAddress> ifAddrs = nif.getInetAddresses(); while (ifAddrs.hasMoreElements()) { InetAddress ifAddr = ifAddrs.nextElement(); if (subnetInfo.isInRange(ifAddr.getHostAddress())) { addrs.add(ifAddr); } } }
From source file:com.petalmd.armor.AbstractUnitTest.java
public static String getNonLocalhostAddress() { try {// ww w .j a v a2s . c o m for (final Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en .hasMoreElements();) { final NetworkInterface intf = en.nextElement(); if (intf.isLoopback() || !intf.isUp()) { continue; } for (final Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr .hasMoreElements();) { final InetAddress ia = enumIpAddr.nextElement(); if (ia.isLoopbackAddress() || ia instanceof Inet6Address) { continue; } return ia.getHostAddress(); } } } catch (final SocketException e) { throw new RuntimeException(e); } System.out.println("ERROR: No non-localhost address available, will use localhost"); return "localhost"; }
From source file:com.icloud.framework.core.util.FBUtilities.java
public static String getIp() { String localip = null;// IP?IP String netip = null;// IP try {/*from ww w . j a va2 s .co m*/ Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces(); InetAddress ip = null; boolean finded = false;// ?IP while (netInterfaces.hasMoreElements() && !finded) { NetworkInterface ni = netInterfaces.nextElement(); Enumeration<InetAddress> address = ni.getInetAddresses(); while (address.hasMoreElements()) { ip = address.nextElement(); if (!ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) {// IP netip = ip.getHostAddress(); finded = true; break; } else if (ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) {// IP localip = ip.getHostAddress(); } } } } catch (SocketException e) { e.printStackTrace(); } if (netip != null && !"".equals(netip)) { return netip; } else { return localip; } }
From source file:de.madvertise.android.sdk.MadvertiseUtil.java
/** * Fetch the address of the enabled interface * /*from w w w . ja va2s . c o m*/ * @return ip address as string */ public static String getLocalIpAddress(MadvertiseViewCallbackListener listener) { try { 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()) { if (inetAddress instanceof Inet4Address) { return inetAddress.getHostAddress().toString(); } } } } } catch (SocketException e) { if (listener != null) { listener.onError(e); } e.printStackTrace(); } if (listener != null) { listener.onError(new IllegalArgumentException("Couldn't obtain the local ip address")); } return ""; }
From source file:com.cloud.utils.UriUtils.java
public static Pair<String, Integer> validateUrl(String format, String url) throws IllegalArgumentException { try {/*from w w w . ja v a 2 s . com*/ 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:jetbrains.buildServer.clouds.azure.connector.AzureApiConnector.java
private static ArrayList<ConfigurationSet> createConfigurationSetList(int port, String serverLocation) { ArrayList<ConfigurationSet> retval = new ArrayList<ConfigurationSet>(); final ConfigurationSet value = new ConfigurationSet(); value.setConfigurationSetType(ConfigurationSetTypes.NETWORKCONFIGURATION); final ArrayList<InputEndpoint> endpointsList = new ArrayList<InputEndpoint>(); value.setInputEndpoints(endpointsList); InputEndpoint endpoint = new InputEndpoint(); endpointsList.add(endpoint);//from w w w . ja v a2 s.co m endpoint.setLocalPort(port); endpoint.setPort(port); endpoint.setProtocol("TCP"); endpoint.setName(AzurePropertiesNames.ENDPOINT_NAME); final EndpointAcl acl = new EndpointAcl(); endpoint.setEndpointAcl(acl); final URI serverUri = URI.create(serverLocation); List<InetAddress> serverAddresses = new ArrayList<InetAddress>(); try { serverAddresses.addAll(Arrays.asList(InetAddress.getAllByName(serverUri.getHost()))); serverAddresses.add(InetAddress.getLocalHost()); } catch (UnknownHostException e) { LOG.warn("Unable to identify server name ip list", e); } final ArrayList<AccessControlListRule> aclRules = new ArrayList<AccessControlListRule>(); acl.setRules(aclRules); int order = 1; for (final InetAddress address : serverAddresses) { if (!(address instanceof Inet4Address)) { continue; } final AccessControlListRule rule = new AccessControlListRule(); rule.setOrder(order++); rule.setAction("Permit"); rule.setRemoteSubnet(address.getHostAddress() + "/32"); rule.setDescription("Server"); aclRules.add(rule); } retval.add(value); return retval; }
From source file:com.entertailion.android.slideshow.utils.Utils.java
public static final InetAddress getLocalInetAddress() { InetAddress selectedInetAddress = null; try {//from ww w .ja v a 2s . c o m for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en .hasMoreElements();) { NetworkInterface intf = en.nextElement(); if (intf.isUp()) { for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr .hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress()) { if (inetAddress instanceof Inet4Address) { // only // want // ipv4 // address if (inetAddress.getHostAddress().toString().charAt(0) != '0') { if (selectedInetAddress == null) { selectedInetAddress = inetAddress; } else if (intf.getName().startsWith("eth")) { // prefer // wired // interface selectedInetAddress = inetAddress; } } } } } } } return selectedInetAddress; } catch (Throwable e) { Log.e(LOG_TAG, "Failed to get the IP address", e); } return null; }
From source file:ca.psiphon.PsiphonTunnel.java
private static PrivateAddress selectPrivateAddress() throws Exception { // Select one of 10.0.0.1, 172.16.0.1, or 192.168.0.1 depending on // which private address range isn't in use. Map<String, PrivateAddress> candidates = new HashMap<String, PrivateAddress>(); candidates.put("10", new PrivateAddress("10.0.0.1", "10.0.0.0", 8, "10.0.0.2")); candidates.put("172", new PrivateAddress("172.16.0.1", "172.16.0.0", 12, "172.16.0.2")); candidates.put("192", new PrivateAddress("192.168.0.1", "192.168.0.0", 16, "192.168.0.2")); candidates.put("169", new PrivateAddress("169.254.1.1", "169.254.1.0", 24, "169.254.1.2")); List<NetworkInterface> netInterfaces; try {//from w w w .j a v a 2s . co m netInterfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); } catch (SocketException e) { throw new Exception("selectPrivateAddress failed", e); } for (NetworkInterface netInterface : netInterfaces) { for (InetAddress inetAddress : Collections.list(netInterface.getInetAddresses())) { String ipAddress = inetAddress.getHostAddress(); if (InetAddressUtils.isIPv4Address(ipAddress)) { if (ipAddress.startsWith("10.")) { candidates.remove("10"); } else if (ipAddress.length() >= 6 && ipAddress.substring(0, 6).compareTo("172.16") >= 0 && ipAddress.substring(0, 6).compareTo("172.31") <= 0) { candidates.remove("172"); } else if (ipAddress.startsWith("192.168")) { candidates.remove("192"); } } } } if (candidates.size() > 0) { return candidates.values().iterator().next(); } throw new Exception("no private address available"); }
From source file:com.mozilla.SUTAgentAndroid.SUTAgentAndroid.java
public static InetAddress getLocalInetAddress() throws SocketException { 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())) { return inetAddress; }/*w ww .j av a 2s .c o m*/ } } return null; }