Example usage for java.net InetAddress isSiteLocalAddress

List of usage examples for java.net InetAddress isSiteLocalAddress

Introduction

In this page you can find the example usage for java.net InetAddress isSiteLocalAddress.

Prototype

public boolean isSiteLocalAddress() 

Source Link

Document

Utility routine to check if the InetAddress is a site local address.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    InetAddress address = InetAddress.getByName("web.mit.edu");
    System.out.println("Name: " + address.getHostName());
    System.out.println("Addr: " + address.getHostAddress());
    System.out.println(address.isSiteLocalAddress());
}

From source file:Main.java

static public boolean isValidIpAddress(String ip) {
    boolean v4 = InetAddressUtils.isIPv4Address(ip);
    boolean v6 = InetAddressUtils.isIPv6Address(ip);
    if (!v4 && !v6)
        return false;
    try {//  w  w w. j  a v  a2  s .  c om
        InetAddress inet = InetAddress.getByName(ip);
        return inet.isLinkLocalAddress() || inet.isSiteLocalAddress();
    } catch (UnknownHostException e) {
        //Log.e(TAG, e.toString());
        return false;
    }
}

From source file:Main.java

public static String getLocalIpAddress() throws Exception {
    String ipAddress = null;/*from   w  w  w . j ava2  s  .  co m*/

    Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();

    while (en.hasMoreElements()) {
        NetworkInterface e = en.nextElement();
        Enumeration<InetAddress> addresses = e.getInetAddresses();
        while (addresses.hasMoreElements()) {
            InetAddress address = addresses.nextElement();
            if (!address.isLoopbackAddress() && address.isSiteLocalAddress()) {
                ipAddress = address.getHostName();
                break;
            }
        }
    }

    if (ipAddress == null) {
        ipAddress = InetAddress.getLocalHost().getHostAddress();
    }

    return ipAddress;
}

From source file:cn.leancloud.diamond.server.utils.SystemConfig.java

private static String getHostAddress() {
    String address = "127.0.0.1";
    try {// ww w  . ja  va  2s  .  co m
        Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
        while (en.hasMoreElements()) {
            NetworkInterface ni = en.nextElement();
            Enumeration<InetAddress> ads = ni.getInetAddresses();
            while (ads.hasMoreElements()) {
                InetAddress ip = ads.nextElement();
                if (!ip.isLoopbackAddress() && ip.isSiteLocalAddress()) {
                    return ip.getHostAddress();
                }
            }
        }
    } catch (Exception e) {
    }
    return address;
}

From source file:org.apache.metron.enrichment.adapters.maxmind.MaxMindDbUtilities.java

/**
 * Determines if an address isn't eligible for getting appropriate results from the underlying database.
 * @param ipStr The IP String/*  ww w  .  j  av  a  2 s.  co  m*/
 * @param addr The addr to be tested
 * @return True if ineligible, false otherwise
 */
public static boolean isIneligibleAddress(String ipStr, InetAddress addr) {
    return addr.isAnyLocalAddress() || addr.isLoopbackAddress() || addr.isSiteLocalAddress()
            || addr.isMulticastAddress() || !ipvalidator.isValidInet4Address(ipStr);
}

From source file:Main.java

/**
 * checks if the provided address is a global-scope ipv6 unicast address
 *//*from  w  w  w .  ja  va 2  s .  co  m*/
public static boolean isGlobalAddressV6(InetAddress addr) {
    return addr instanceof Inet6Address && !addr.isAnyLocalAddress() && !addr.isLinkLocalAddress()
            && !addr.isLoopbackAddress() && !addr.isMulticastAddress() && !addr.isSiteLocalAddress()
            && !((Inet6Address) addr).isIPv4CompatibleAddress();
}

From source file:com.apporiented.hermesftp.utils.NetUtils.java

/**
 * Returns the network address of a particular network interface.
 * /*from  w ww .  j a v a  2 s .  c o m*/
 * @param ni The network interface.
 * @return The machine address of a particular network interface.
 */
public static InetAddress getMachineAddress(NetworkInterface ni) {
    Enumeration<InetAddress> addrs = ni.getInetAddresses();
    InetAddress mAddr = null;
    while (addrs.hasMoreElements()) {
        InetAddress addr = addrs.nextElement();
        if (addr.isSiteLocalAddress()) {
            mAddr = addr;
        }
    }
    return mAddr;
}

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!");
        }// www .  ja  v a  2  s  .c  o m
        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:de.undercouch.gradle.tasks.download.TestBase.java

/**
 * Get a site local IP4 address from the current node's interfaces
 * @return the IP address or <code>null</code> if the address
 * could not be obtained// w  w w  .j  a  v  a2s  .  co m
 * @throws SocketException if an I/O error occurs
 */
private static String findSiteLocal() throws SocketException {
    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
    while (interfaces.hasMoreElements()) {
        NetworkInterface n = interfaces.nextElement();
        Enumeration<InetAddress> addresses = n.getInetAddresses();
        while (addresses.hasMoreElements()) {
            InetAddress i = addresses.nextElement();
            if (i.isSiteLocalAddress() && i instanceof Inet4Address) {
                return i.getHostAddress();
            }
        }
    }
    return null;
}

From source file:Main.java

private static int scoreAddress(InetAddress addr) {
    if (addr.isAnyLocalAddress()) {
        return 0;
    }/*w  ww .  ja v  a  2 s .  c o  m*/
    if (addr.isMulticastAddress()) {
        return 1;
    }
    if (addr.isLinkLocalAddress()) {
        return 2;
    }
    if (addr.isSiteLocalAddress()) {
        return 3;
    }

    return 4;
}