List of usage examples for org.apache.http.conn.util InetAddressUtils isIPv4Address
public static boolean isIPv4Address(final String input)
From source file:io.ucoin.ucoinj.core.client.model.bma.gson.EndpointAdapter.java
@Override public NetworkPeering.Endpoint read(JsonReader reader) throws IOException { if (reader.peek() == com.google.gson.stream.JsonToken.NULL) { reader.nextNull();//w w w.ja v a 2 s . c om return null; } String ept = reader.nextString(); ArrayList<String> parts = new ArrayList<>(Arrays.asList(ept.split(" "))); NetworkPeering.Endpoint endpoint = new NetworkPeering.Endpoint(); endpoint.port = Integer.parseInt(parts.remove(parts.size() - 1)); for (String word : parts) { if (InetAddressUtils.isIPv4Address(word)) { endpoint.ipv4 = word; } else if (InetAddressUtils.isIPv6Address(word)) { endpoint.ipv6 = word; } else if (word.startsWith("http")) { endpoint.url = word; } else { try { endpoint.protocol = EndpointProtocol.valueOf(word); } catch (IllegalArgumentException e) { // skip this part } } } if (endpoint.protocol == null) { endpoint.protocol = EndpointProtocol.UNDEFINED; } return endpoint; }
From source file:org.namelessrom.devicecontrol.net.NetworkInfo.java
public static String getIpAddress(final boolean useIPv4) { List<NetworkInterface> interfaces; try {// w ww . j av a 2 s . c om interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); } catch (Exception e) { interfaces = null; } if (interfaces == null) return "0.0.0.0"; for (NetworkInterface intf : interfaces) { final List<InetAddress> addrs = Collections.list(intf.getInetAddresses()); for (final InetAddress addr : addrs) { if (!addr.isLoopbackAddress()) { final String sAddr = addr.getHostAddress().toUpperCase(); boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr); if (useIPv4) { if (isIPv4) { return sAddr; } } else { if (!isIPv4) { final int delim = sAddr.indexOf('%'); // drop ip6 port suffix return ((delim < 0) ? sAddr : sAddr.substring(0, delim)); } } } } } return "0.0.0.0"; }
From source file:camp.pixels.signage.util.NetworkInterfaces.java
/** * @param interfaceName eth0, wlan0 or NULL=use first interface * @param useIPv4/*w ww . j a v a 2s . c o m*/ * @return address or empty string */ @SuppressLint("DefaultLocale") public static String getIPAddress(String interfaceName, boolean useIPv4) { try { List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface intf : interfaces) { if (interfaceName != null) { if (!intf.getName().equalsIgnoreCase(interfaceName)) continue; } if (!VALID_INTERFACES.contains(intf.getName())) continue; List<InetAddress> addrs = Collections.list(intf.getInetAddresses()); for (InetAddress addr : addrs) { if (!addr.isLoopbackAddress()) { String sAddr = addr.getHostAddress().toUpperCase(); boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr); if (useIPv4) { if (isIPv4) return sAddr; } else { if (!isIPv4) { int delim = sAddr.indexOf('%'); // drop ip6 port suffix return delim < 0 ? sAddr : sAddr.substring(0, delim); } } } } } } catch (SocketException ex) { } return ""; }
From source file:org.mariotaku.twidere.util.net.HostResolvedSocketFactory.java
@Override public Socket createSocket(String host, int port) throws IOException { final String resolvedHost = resolver.resolve(host); if (resolvedHost != null && !resolvedHost.equals(host)) { if (InetAddressUtils.isIPv6Address(resolvedHost)) { final byte[] resolvedAddress = Inet6Address.getByName(resolvedHost).getAddress(); return new Socket(InetAddress.getByAddress(host, resolvedAddress), port); } else if (InetAddressUtils.isIPv4Address(resolvedHost)) { final byte[] resolvedAddress = Inet4Address.getByName(resolvedHost).getAddress(); return new Socket(InetAddress.getByAddress(host, resolvedAddress), port); }// ww w . j a v a2s.c o m } return defaultFactory.createSocket(host, port); }
From source file:com.mashape.galileo.agent.common.IPAddressParserUtil.java
private static boolean isValidIp(final String ip) { String ipAdress = ip.toLowerCase().trim(); return InetAddressUtils.isIPv4Address(ipAdress) || InetAddressUtils.isIPv6Address(ipAdress); }
From source file:com.orangelabs.rcs.platform.network.AndroidNetworkFactory.java
/** * Returns the local IP address of a given network interface * //from ww w . j av a 2 s . c om * @param dnsEntry remote address to find an according local socket address * @param type the type of the network interface, should be either * {@link android.net.ConnectivityManager#TYPE_WIFI} or {@link android.net.ConnectivityManager#TYPE_MOBILE} * @return Address */ // Changed by Deutsche Telekom public String getLocalIpAddress(DnsResolvedFields dnsEntry, int type) { String ipAddress = null; try { // What kind of remote address (P-CSCF) are we trying to reach? boolean isIpv4 = InetAddressUtils.isIPv4Address(dnsEntry.ipAddress); // check all available interfaces for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); (en != null) && en.hasMoreElements();) { NetworkInterface netIntf = (NetworkInterface) en.nextElement(); for (Enumeration<InetAddress> addr = netIntf.getInetAddresses(); addr.hasMoreElements();) { InetAddress inetAddress = addr.nextElement(); ipAddress = IpAddressUtils.extractHostAddress(inetAddress.getHostAddress()); // if IP address version doesn't match to remote address // version then skip if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress() && (InetAddressUtils.isIPv4Address(ipAddress) == isIpv4)) { String intfName = netIntf.getDisplayName().toLowerCase(); // some devices do list several interfaces though only // one is active if (((type == ConnectivityManager.TYPE_WIFI) && intfName.startsWith("wlan")) || ((type == ConnectivityManager.TYPE_MOBILE) && !intfName.startsWith("wlan"))) { return ipAddress; } } } } } catch (Exception e) { if (logger.isActivated()) { logger.error("getLocalIpAddress failed with ", e); } } return ipAddress; }
From source file:eu.betaas.betaasandroidapp.configuration.Configuration.java
private void setDeviceIp() { try {// w w w .j a va 2 s . c om List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface intf : interfaces) { List<InetAddress> addrs = Collections.list(intf.getInetAddresses()); for (InetAddress addr : addrs) { if (!addr.isLoopbackAddress()) { String sAddr = addr.getHostAddress().toUpperCase(); boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr); if (isIPv4) { deviceIP = sAddr; } } } } } catch (Exception ex) { deviceIP = null; } }
From source file:nl.verheulconsultants.monitorisp.service.Utilities.java
/** * Use the org.apache.httpcomponents class library to validate Ip4 and Ip6 addresses. * * @param ip the ip/*from www .j av a2 s .co m*/ * @return check if the ip is valid ipv4 or ipv6 */ private static boolean isValidIp(final String ip) { return InetAddressUtils.isIPv4Address(ip) || InetAddressUtils.isIPv6Address(ip); }
From source file:org.springframework.cloud.consul.discovery.ConsulDiscoveryClientCustomizedTests.java
private void assertNotIpAddress(ServiceInstance instance) { assertFalse("host is an ip address", InetAddressUtils.isIPv4Address(instance.getHost())); }
From source file:com.andclient.SetupConnectionActivity.java
private void saveSettingsOnClick(View v) { int keyboardPort = 0, mousePort = 0; EditText et = (EditText) findViewById(R.id.editTextServerIP); String ip = et.getText().toString(); boolean correctIp = InetAddressUtils.isIPv4Address(ip); if (!correctIp) { Toast.makeText(SetupConnectionActivity.this, "Server IP is not a correct IPv4 address", Toast.LENGTH_SHORT).show(); }//from w w w. j av a2 s. c o m et = (EditText) findViewById(R.id.editTextMousePort); String txt = et.getText().toString(); if (txt.length() != 0) { try { mousePort = Integer.parseInt(txt); } catch (NumberFormatException e) { Toast.makeText(SetupConnectionActivity.this, "Mouse port is not a number", Toast.LENGTH_SHORT) .show(); } if (mousePort < 0) { Toast.makeText(SetupConnectionActivity.this, "Mouse port must be greater than 0", Toast.LENGTH_SHORT).show(); } } et = (EditText) findViewById(R.id.editTextKeyboardPort); txt = et.getText().toString(); if (txt.length() != 0) { try { keyboardPort = Integer.parseInt(txt); } catch (NumberFormatException e) { Toast.makeText(SetupConnectionActivity.this, "Keyboard port must be an integer", Toast.LENGTH_SHORT) .show(); } if (keyboardPort < 0) { Toast.makeText(SetupConnectionActivity.this, "Keyboard port must be greater than 0", Toast.LENGTH_SHORT).show(); } } if (correctIp && mousePort > 0 && keyboardPort > 0) { SharedPreferences.Editor editor = mSettings.edit(); editor.putString(StartActivity.PREF_SERVER_IP, ip).putInt(StartActivity.PREF_MOUSE_PORT, mousePort) .putInt(StartActivity.PREF_KEYBOARD_PORT, keyboardPort); editor.commit(); TextView tv = (TextView) findViewById(R.id.textViewSaveData); tv.setTextColor(Color.GREEN); tv.setText("Settings saved."); tv.setVisibility(View.VISIBLE); } else { TextView tv = (TextView) findViewById(R.id.textViewSaveData); tv.setText("Incorrect input data."); tv.setTextColor(Color.RED); tv.setVisibility(View.VISIBLE); } }