Java InetAddress Check isLocal(InetAddress a)

Here you can find the source of isLocal(InetAddress a)

Description

is Local

License

Open Source License

Declaration

public static boolean isLocal(InetAddress a) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

public class Main {
    public static boolean isLocal(InetAddress a) {
        List<InetAddress> localIPs = getLocalIPs();
        if (localIPs == null) {
            return false;
        } else {/*from w  ww  . j av  a 2s. c  o  m*/
            return localIPs.contains(a);
        }
    }

    public static List<InetAddress> getLocalIPs() {

        Enumeration<NetworkInterface> interfaces;

        try {
            interfaces = NetworkInterface.getNetworkInterfaces();
        } catch (SocketException e) {
            return null;
        }

        List<InetAddress> ips = new ArrayList<InetAddress>();

        while (interfaces.hasMoreElements()) {
            NetworkInterface current = interfaces.nextElement();

            if (current != null) {
                Enumeration<InetAddress> addresses = current.getInetAddresses();

                while (addresses.hasMoreElements()) {
                    InetAddress addr = addresses.nextElement();
                    if (addr != null) {
                        ips.add(addr);
                    }
                }
            }
        }

        return ips;

    }
}

Related

  1. isIPv6(InetAddress addr)
  2. isIPv6UniqueSiteLocal(InetAddress address)
  3. isLikelyBroadcast(InetAddress address)
  4. isLinkLocalIPv4Address(InetAddress add)
  5. isLinkLocalNetwork(InetAddress addr)
  6. isLocalAddress(InetAddress addr)
  7. isLocalAddress(InetAddress addr)
  8. isLocalAddress(InetAddress addr)
  9. isLocalAddress(InetAddress address)