Exemplo n.º 1
0
  public static String[] getLocalCidrs() {
    String defaultHostIp = getDefaultHostIp();

    List<String> cidrList = new ArrayList<String>();
    try {
      for (NetworkInterface ifc :
          IteratorUtil.enumerationAsIterable(NetworkInterface.getNetworkInterfaces())) {
        if (ifc.isUp() && !ifc.isVirtual() && !ifc.isLoopback()) {
          for (InterfaceAddress address : ifc.getInterfaceAddresses()) {
            InetAddress addr = address.getAddress();
            int prefixLength = address.getNetworkPrefixLength();
            if (prefixLength < 32 && prefixLength > 0) {
              String ip = ipFromInetAddress(addr);
              if (ip.equalsIgnoreCase(defaultHostIp))
                cidrList.add(ipAndNetMaskToCidr(ip, getCidrNetmask(prefixLength)));
            }
          }
        }
      }
    } catch (SocketException e) {
      s_logger.warn("UnknownHostException in getLocalCidrs().", e);
    }

    return cidrList.toArray(new String[0]);
  }
Exemplo n.º 2
0
  public static InetAddress[] getAllLocalInetAddresses() {
    List<InetAddress> addrList = new ArrayList<InetAddress>();
    try {
      for (NetworkInterface ifc :
          IteratorUtil.enumerationAsIterable(NetworkInterface.getNetworkInterfaces())) {
        if (ifc.isUp() && !ifc.isVirtual()) {
          for (InetAddress addr : IteratorUtil.enumerationAsIterable(ifc.getInetAddresses())) {
            addrList.add(addr);
          }
        }
      }
    } catch (SocketException e) {
      s_logger.warn("SocketException in getAllLocalInetAddresses().", e);
    }

    InetAddress[] addrs = new InetAddress[addrList.size()];
    if (addrList.size() > 0) {
      System.arraycopy(addrList.toArray(), 0, addrs, 0, addrList.size());
    }
    return addrs;
  }