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:com.starit.diamond.client.processor.LocalConfigInfoProcessor.java

static String getHostAddress() {
    String address = "127.0.0.1";
    try {/*from   w ww  .  j a 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) {
        throw new RuntimeException("??", e);
    }
    return address;
}

From source file:Main.java

public static String filterIP(final InetAddress inetAddress) {
    try {//w  w  w.ja  v a 2 s.c o  m
        final String ipVersion;
        if (inetAddress instanceof Inet4Address)
            ipVersion = "ipv4";
        else if (inetAddress instanceof Inet6Address)
            ipVersion = "ipv6";
        else
            ipVersion = "ipv?";

        if (inetAddress.isAnyLocalAddress())
            return "wildcard";
        if (inetAddress.isSiteLocalAddress())
            return "site_local_" + ipVersion;
        if (inetAddress.isLinkLocalAddress())
            return "link_local_" + ipVersion;
        if (inetAddress.isLoopbackAddress())
            return "loopback_" + ipVersion;
        return inetAddress.getHostAddress();
    } catch (final IllegalArgumentException e) {
        return "illegal_ip";
    }
}

From source file:net.centro.rtb.monitoringcenter.infos.NodeInfo.java

private static String detectPublicIpAddress() {
    Enumeration<NetworkInterface> networkInterfaces = null;
    try {//from  w  ww  .ja  v a  2 s .  co m
        networkInterfaces = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e) {
        logger.debug("Unable to obtain network interfaces!", e);
        return null;
    }

    for (NetworkInterface networkInterface : Collections.list(networkInterfaces)) {
        boolean isLoopback = false;
        try {
            isLoopback = networkInterface.isLoopback();
        } catch (SocketException e) {
            logger.debug("Unable to identify if a network interface is a loopback or not");
        }

        if (!isLoopback) {
            Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
            while (inetAddresses.hasMoreElements()) {
                InetAddress inetAddress = inetAddresses.nextElement();
                if (Inet4Address.class.isInstance(inetAddress)) {
                    if (!inetAddress.isLoopbackAddress() && !inetAddress.isAnyLocalAddress()
                            && !inetAddress.isLinkLocalAddress() && !inetAddress.isSiteLocalAddress()) {
                        return inetAddress.getHostAddress();
                    }
                }
            }
        }
    }

    return null;
}

From source file:org.dd4t.core.util.HttpUtils.java

/**
 * Checking for local ip addresses, e.g.
 * <p/>/*from w ww.  ja  v  a  2s  . com*/
 * <pre>
 *     10.x.x.x
 *     172.[16-31].x.x
 *     192.168.x.x
 *     127.0.0.1
 * </pre>
 */
private static boolean isLocalDomainAddress(final String ipAddress) throws UnknownHostException {
    final InetAddress inetAddress = InetAddress.getByName(ipAddress);
    return inetAddress.isAnyLocalAddress() || inetAddress.isLinkLocalAddress()
            || inetAddress.isMulticastAddress() || inetAddress.isSiteLocalAddress();
}

From source file:Main.java

public static String getLocalIpAddress() {
    String result = "";
    boolean exit = false;
    try {//from w w  w. j  a  va2 s.c o m
        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.toString().indexOf(":")>=0)) {
                //                       return inetAddress.getHostAddress().toString();
                //                   }
                //                  Log.v("","ip="+inetAddress.getHostAddress()+
                //                        ", name="+intf.getName());
                if (inetAddress.isSiteLocalAddress()) {
                    result = inetAddress.getHostAddress();
                    //                       Log.v("","result="+result+", name="+intf.getName()+"-");
                    if (intf.getName().equals("wlan0")) {
                        exit = true;
                        break;
                    }
                }
            }
            if (exit)
                break;
        }
    } catch (SocketException ex) {
        Log.e(DEBUG_TAG, ex.toString());
        result = "192.168.0.1";
    }
    //      Log.v("","getLocalIpAddress result="+result);
    if (result.equals(""))
        result = "192.168.0.1";
    return result;
}

From source file:org.votingsystem.util.HttpHelper.java

public static String getLocalIP() throws SocketException {
    Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
    for (NetworkInterface netint : Collections.list(nets)) {
        Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
        for (InetAddress inetAddress : Collections.list(inetAddresses)) {
            if (inetAddress.isSiteLocalAddress()) {
                String inetAddressStr = inetAddress.toString();
                while (inetAddressStr.startsWith("/"))
                    inetAddressStr = inetAddressStr.substring(1);
                return inetAddressStr;
            }/*from  w  w  w. ja  va 2 s  .  com*/

        }
    }
    return null;
}

From source file:at.alladin.rmbt.shared.Helperfunctions.java

public static boolean isIPLocal(final InetAddress adr) {
    return adr.isLinkLocalAddress() || adr.isLoopbackAddress() || adr.isSiteLocalAddress();
}

From source file:org.wso2.carbon.apimgt.hybrid.gateway.configurator.Configurator.java

/**
 * Retrieve host name, mac address of the device
 *
 * @return details Map<String, String>
 *///w  w w  . j  ava2  s.c  o m
protected static Map<String, String> getDeviceDetails() {
    InetAddress ip;
    String hostName = "";
    String macAddress = ConfigConstants.DEFAULT_MAC_ADDRESS;
    Map<String, String> details = new HashMap();
    try {
        ip = InetAddress.getLocalHost();
        hostName = ip.getHostName();
        Enumeration<NetworkInterface> networkInterfaceEnumeration = NetworkInterface.getNetworkInterfaces();
        while (networkInterfaceEnumeration.hasMoreElements()) {
            NetworkInterface networkInterface = networkInterfaceEnumeration.nextElement();
            Enumeration<InetAddress> enumeration = networkInterface.getInetAddresses();
            for (; enumeration.hasMoreElements();) {
                InetAddress address = enumeration.nextElement();
                if (!address.isLoopbackAddress() && !address.isLinkLocalAddress()
                        && address.isSiteLocalAddress()) {
                    byte[] mac = networkInterface.getHardwareAddress();
                    if (mac != null) {
                        StringBuilder sb = new StringBuilder();
                        for (int i = 0; i < mac.length; i++) {
                            //Construct mac address
                            sb.append(String.format("%02X%s", mac[i],
                                    (i < mac.length - 1) ? ConfigConstants.DELIMITER : ""));
                        }
                        macAddress = sb.toString();
                        break;
                    }
                }
            }
        }
    } catch (UnknownHostException | SocketException e) {
        log.error("Error while retrieving mac address", e);
        Runtime.getRuntime().exit(1);
    }
    details.put(ConfigConstants.HOST_NAME, hostName);
    details.put(ConfigConstants.MAC_ADDRESS, macAddress);
    return details;
}

From source file:com.icloud.framework.core.util.FBUtilities.java

public static String getIp() {
    String localip = null;// IP?IP
    String netip = null;// IP
    try {//  w w w  .  ja va  2 s .  c  o 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:at.alladin.rmbt.shared.Helperfunctions.java

public static String IpType(InetAddress inetAddress) {
    try {// w  w  w  . j a  v a  2s.  com
        final String ipVersion;
        if (inetAddress instanceof Inet4Address)
            ipVersion = "ipv4";
        else if (inetAddress instanceof Inet6Address)
            ipVersion = "ipv6";
        else
            ipVersion = "ipv?";

        if (inetAddress.isAnyLocalAddress())
            return "wildcard_" + ipVersion;
        if (inetAddress.isSiteLocalAddress())
            return "site_local_" + ipVersion;
        if (inetAddress.isLinkLocalAddress())
            return "link_local_" + ipVersion;
        if (inetAddress.isLoopbackAddress())
            return "loopback_" + ipVersion;
        return "public_" + ipVersion;

    } catch (final IllegalArgumentException e) {
        return "illegal_ip";
    }
}