List of usage examples for org.apache.http.conn.util InetAddressUtils isIPv4Address
public static boolean isIPv4Address(final String input)
From source file:fr.cnes.sitools.security.filter.SecurityFilter.java
/** * Transform an address from string to long * //from w w w .j a v a 2 s . c om * @param ip * the String representing an ip address * @return the long representation of the given ip address */ private long getAddress(String ip) { boolean isIPv4 = InetAddressUtils.isIPv4Address(ip); if (isIPv4) { StringTokenizer st = new StringTokenizer(ip, "."); long a1 = Integer.parseInt(st.nextToken()); long a2 = Integer.parseInt(st.nextToken()); long a3 = Integer.parseInt(st.nextToken()); long a4 = Integer.parseInt(st.nextToken()); return a1 << 24 | a2 << 16 | a3 << 8 | a4; } boolean isIPv6 = InetAddressUtils.isIPv6Address(ip); if (isIPv6) { getContext().getLogger().warning("SecurityFilter: IPV6 Address cannot match IPV4 mask"); return 0; } getContext().getLogger().warning("SecurityFilter: Not a valid IP Address"); return 0; }
From source file:com.google.appinventor.components.runtime.BlockyTalky.java
private static String getIPAddress(boolean useIPv4) { try {/*w w w.j a v a2 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 (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 (Exception ex) { } // for now eat exceptions return ""; }
From source file:com.seleuco.mame4droid.NetPlay.java
public String getIPAddress() { try {//from w ww.j av a2s. co m Enumeration<NetworkInterface> ifaceList; NetworkInterface selectedIface = null; // First look for a WLAN interface ifaceList = NetworkInterface.getNetworkInterfaces(); while (selectedIface == null && ifaceList.hasMoreElements()) { NetworkInterface intf = ifaceList.nextElement(); if (intf.getName().startsWith("wlan")) { 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) return sAddr; } } } } // If we didn't find that, look for an Ethernet interface ifaceList = NetworkInterface.getNetworkInterfaces(); while (selectedIface == null && ifaceList.hasMoreElements()) { NetworkInterface intf = ifaceList.nextElement(); if (intf.getName().startsWith("eth")) { 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) return sAddr; } } } } 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) return sAddr; } } } } catch (Exception ex) { } return null; }
From source file:com.dmsl.anyplace.utils.NetworkUtils.java
public static String getLocalIP(boolean useIPv4) { String ip = "No Local IP assigned"; try {//from w w w. j a v a 2s . co m List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface ni : interfaces) { List<InetAddress> addresses = Collections.list(ni.getInetAddresses()); for (InetAddress ia : addresses) { if (ia != null && !ia.isLoopbackAddress()) { String sAddr = ia.getHostAddress().toUpperCase(Locale.ENGLISH); boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr); if (useIPv4) { if (isIPv4) { ip = sAddr; } } else { if (!isIPv4) { int delim = sAddr.indexOf('%'); ip = delim < 0 ? sAddr : sAddr.substring(0, delim); } } } } } } catch (Exception ex) { ip = "Unknown Error, Report it!"; } return ip; }
From source file:org.getlantern.firetweet.util.net.FiretweetHostAddressResolver.java
private static boolean isValidIpAddress(final String address) { if (isEmpty(address)) return false; return InetAddressUtils.isIPv4Address(address) || InetAddressUtils.isIPv6Address(address); }
From source file:com.bluetooth.activities.WiFiControl.java
/** * This method gets an IPv4 address for the user to enter in the browser. * /* w w w .j a va 2 s . c o m*/ * @return The IP */ private String getLocalIpAddress() { 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() && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())) { return inetAddress.getHostAddress().toString(); } } } } catch (SocketException e) { e.printStackTrace(); } return null; }
From source file:com.example.android.toyvpn.ToyVpnService.java
public String getLocalIpAddress() { String ipv4;/*from w w w . ja v a2s.co m*/ 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(); // System.out.println("ip1--:" + inetAddress); // System.out.println("ip2--:" + inetAddress.getHostAddress()); // for getting IPV4 format if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ipv4 = inetAddress.getHostAddress())) { String ip = inetAddress.getHostAddress().toString(); // System.out.println("ip---::" + ip); // return inetAddress.getHostAddress().toString(); return ipv4; } } } } catch (Exception ex) { Log.e("IP Address", ex.toString()); } return null; }
From source file:com.rovemonteux.silvertunnel.netlib.layer.tor.common.TCPStreamProperties.java
public InetAddress getAddr() { if (addr == null && hostname != null && !hostname.isEmpty() && InetAddressUtils.isIPv4Address(hostname)) { try {//from ww w . j ava2s. c o m String[] octets = hostname.split("\\."); byte[] ip = new byte[4]; ip[0] = (byte) Integer.parseInt(octets[0]); ip[1] = (byte) Integer.parseInt(octets[1]); ip[2] = (byte) Integer.parseInt(octets[2]); ip[3] = (byte) Integer.parseInt(octets[3]); return InetAddress.getByAddress(ip); } catch (UnknownHostException e) { return addr; } } return addr; }
From source file:org.jivesoftware.smack.ConnectionConfiguration.java
/** * Returns the host to use when establishing the connection. The host and port to use * might have been resolved by a DNS lookup as specified by the XMPP spec (and therefore * may not match the {@link #getServiceName service name}. * * @return the host to use when establishing the connection. */// w w w. j a va2 s . co m public String getHost() { // ?IP???IP? if (!InetAddressUtils.isIPv4Address(host) && !InetAddressUtils.isIPv6Address(host)) { try { InetAddress address = InetAddress.getByName(host); host = address.getHostAddress(); Log.d(LOG_TAG, "transform host name to host address:" + host); } catch (UnknownHostException e) { Log.e(LOG_TAG, e.getMessage(), e); } } return host; }