Example usage for org.apache.hadoop.net NetUtils isValidSubnet

List of usage examples for org.apache.hadoop.net NetUtils isValidSubnet

Introduction

In this page you can find the example usage for org.apache.hadoop.net NetUtils isValidSubnet.

Prototype

public static boolean isValidSubnet(String subnet) 

Source Link

Usage

From source file:com.mellanox.r4h.DFSClient.java

License:Apache License

/**
 * Return the socket addresses to use with each configured
 * local interface. Local interfaces may be specified by IP
 * address, IP address range using CIDR notation, interface
 * name (e.g. eth0) or sub-interface name (e.g. eth0:0).
 * The socket addresses consist of the IPs for the interfaces
 * and the ephemeral port (port 0). If an IP, IP range, or
 * interface name matches an interface with sub-interfaces
 * only the IP of the interface is used. Sub-interfaces can
 * be used by specifying them explicitly (by IP or name).
 * //from  w w w  .j a va  2  s.  c  o  m
 * @return SocketAddresses for the configured local interfaces,
 *         or an empty array if none are configured
 * @throws UnknownHostException
 *             if a given interface name is invalid
 */
private static SocketAddress[] getLocalInterfaceAddrs(String interfaceNames[]) throws UnknownHostException {
    List<SocketAddress> localAddrs = new ArrayList<SocketAddress>();
    for (String interfaceName : interfaceNames) {
        if (InetAddresses.isInetAddress(interfaceName)) {
            localAddrs.add(new InetSocketAddress(interfaceName, 0));
        } else if (NetUtils.isValidSubnet(interfaceName)) {
            for (InetAddress addr : NetUtils.getIPs(interfaceName, false)) {
                localAddrs.add(new InetSocketAddress(addr, 0));
            }
        } else {
            for (String ip : DNS.getIPs(interfaceName, false)) {
                localAddrs.add(new InetSocketAddress(ip, 0));
            }
        }
    }
    return localAddrs.toArray(new SocketAddress[localAddrs.size()]);
}