Example usage for java.net InetAddress isLinkLocalAddress

List of usage examples for java.net InetAddress isLinkLocalAddress

Introduction

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

Prototype

public boolean isLinkLocalAddress() 

Source Link

Document

Utility routine to check if the InetAddress is an link 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.isLinkLocalAddress());
}

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. ja v  a 2s .c  o m
        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

/**
 * checks if the provided address is a global-scope ipv6 unicast address
 *//*from  ww w .  j a va2  s  . com*/
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:Main.java

public static String getLocalIpAddress() {
    try {//from   w w w.  ja  v  a2  s . c om
        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() && !inetAddress.isLinkLocalAddress()) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {
        Log.e("WifiPreference IpAddress", ex.toString());
    }
    return null;

}

From source file:Main.java

public static String getGPRS_IP() {
    try {//from www.ja v  a  2s  . c  om
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enu = intf.getInetAddresses(); enu.hasMoreElements();) {
                InetAddress inetAddress = enu.nextElement();
                if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress()) {
                    if (inetAddress.getHostAddress().toString().contains("::")) {
                        continue;
                    }
                    return inetAddress.getHostAddress();
                }
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }
    return null;

}

From source file:Main.java

public static NetworkInterface getActiveNetworkInterface() {

    Enumeration<NetworkInterface> interfaces = null;
    try {//from w w  w. j a  va2 s . c o  m
        interfaces = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e) {
        return null;
    }

    while (interfaces.hasMoreElements()) {
        NetworkInterface iface = interfaces.nextElement();
        Enumeration<InetAddress> inetAddresses = iface.getInetAddresses();

        /* Check if we have a non-local address. If so, this is the active
         * interface.
         *
         * This isn't a perfect heuristic: I have devices which this will
         * still detect the wrong interface on, but it will handle the
         * common cases of wifi-only and Ethernet-only.
         */
        while (inetAddresses.hasMoreElements()) {
            InetAddress addr = inetAddresses.nextElement();

            if (!(addr.isLoopbackAddress() || addr.isLinkLocalAddress())) {
                return iface;
            }
        }
    }

    return null;
}

From source file:Main.java

public static NetworkInterface getActiveNetworkInterface() {

    Enumeration<NetworkInterface> interfaces = null;
    try {/*www. j a  v  a  2s.  c  o m*/
        interfaces = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e) {
        return null;
    }

    while (interfaces.hasMoreElements()) {
        NetworkInterface iface = interfaces.nextElement();
        Enumeration<InetAddress> inetAddresses = iface.getInetAddresses();

        /* Check if we have a non-local address. If so, this is the active
         * interface.
         *
         * This isn't a perfect heuristic: I have devices which this will
         * still detect the wrong interface on, but it will handle the
         * common cases of wifi-only and Ethernet-only.
         */
        if (iface.getName().startsWith("w")) {
            //this is a perfect hack for getting wifi alone

            while (inetAddresses.hasMoreElements()) {
                InetAddress addr = inetAddresses.nextElement();

                if (!(addr.isLoopbackAddress() || addr.isLinkLocalAddress())) {
                    Log.d("LSSDP", "DisplayName" + iface.getDisplayName() + " Name " + iface.getName());

                    return iface;
                }
            }
        }
    }

    return null;
}

From source file:com.splout.db.common.GetIPAddresses.java

/**
 * Returns all available IP addresses./*  ww w .ja v  a2s  . c o m*/
 * <p/>
 * In error case or if no network connection is established, we return an empty list here.
 * <p/>
 * Loopback addresses are excluded - so 127.0.0.1 will not be never returned.
 * <p/>
 * The "primary" IP might not be the first one in the returned list.
 *
 * @return Returns all IP addresses (can be an empty list in error case or if network connection is missing).
 * @throws SocketException
 * @since 0.1.0
 */
public static Collection<InetAddress> getAllLocalIPs() throws SocketException {
    LinkedList<InetAddress> listAdr = new LinkedList<InetAddress>();

    Enumeration<NetworkInterface> nifs = NetworkInterface.getNetworkInterfaces();
    if (nifs == null)
        return listAdr;

    while (nifs.hasMoreElements()) {
        NetworkInterface nif = nifs.nextElement();
        // We ignore subinterfaces - as not yet needed.
        Enumeration<InetAddress> adrs = nif.getInetAddresses();
        while (adrs.hasMoreElements()) {
            InetAddress adr = adrs.nextElement();
            if (adr != null && !adr.isLoopbackAddress()
                    && (nif.isPointToPoint() || !adr.isLinkLocalAddress())) {
                log.info("Available site local address: " + adr);
                listAdr.add(adr);
            }
        }
    }
    return listAdr;
}

From source file:Main.java

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

    return 4;
}

From source file:org.opennms.core.test.kafka.JUnitKafkaServer.java

private static String getLocalhost() {
    String address = "localhost";
    try {// w w w  . ja  va 2  s.  c  om
        // This is a workaround for people using OS X Lion.  On Lion when a process tries to connect to a link-local
        // address it takes 5 seconds to establish the connection for some reason.  So instead of using 'localhost'
        // which could return the link-local address randomly, we'll manually resolve it and look for an address to
        // return that isn't link-local.  If for some reason we can't find an address that isn't link-local then
        // we'll fall back to the default lof just looking up 'localhost'.
        for (InetAddress a : InetAddress.getAllByName("localhost")) {
            if (!a.isLinkLocalAddress()) {
                address = a.getHostAddress();
                break;
            }
        }
    } catch (UnknownHostException e) {
        // Something went wrong, just default to the existing approach of using 'localhost'.
    }
    return address;
}