Example usage for java.net NetworkInterface getSubInterfaces

List of usage examples for java.net NetworkInterface getSubInterfaces

Introduction

In this page you can find the example usage for java.net NetworkInterface getSubInterfaces.

Prototype

public Enumeration<NetworkInterface> getSubInterfaces() 

Source Link

Document

Get an Enumeration with all the subinterfaces (also known as virtual interfaces) attached to this network interface.

Usage

From source file:Main.java

/** Finds a network interface of sub-interface with the given name */
public static NetworkInterface getByName(String name) throws SocketException {
    if (name == null)
        return null;
    Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
    while (en.hasMoreElements()) {
        NetworkInterface intf = en.nextElement();
        if (intf.getName().equals(name))
            return intf;
        Enumeration<NetworkInterface> en2 = intf.getSubInterfaces();
        while (en2.hasMoreElements()) {
            NetworkInterface intf2 = en2.nextElement();
            if (intf2.getName().equals(name)) {
                return intf2;
            }/*from ww w .j a  v  a 2s . c o  m*/
        }
    }
    return null;
}

From source file:com.buaa.cfs.net.DNS.java

/**
 * @param nif network interface to get addresses for
 *
 * @return set containing addresses for each subinterface of nif, see below for the rationale for using an ordered
 * set//from  w w  w. j a v  a 2s  .  com
 */
private static LinkedHashSet<InetAddress> getSubinterfaceInetAddrs(NetworkInterface nif) {
    LinkedHashSet<InetAddress> addrs = new LinkedHashSet<InetAddress>();
    Enumeration<NetworkInterface> subNifs = nif.getSubInterfaces();
    while (subNifs.hasMoreElements()) {
        NetworkInterface subNif = subNifs.nextElement();
        addrs.addAll(Collections.list(subNif.getInetAddresses()));
    }
    return addrs;
}

From source file:com.buaa.cfs.utils.NetUtils.java

/**
 * Return an InetAddress for each interface that matches the given subnet specified using CIDR notation.
 *
 * @param subnet              subnet specified using CIDR notation
 * @param returnSubinterfaces whether to return IPs associated with subinterfaces
 *
 * @throws IllegalArgumentException if subnet is invalid
 *///from  w ww  . ja v a2 s  .  c  o m
public static List<InetAddress> getIPs(String subnet, boolean returnSubinterfaces) {
    List<InetAddress> addrs = new ArrayList<InetAddress>();
    SubnetInfo subnetInfo = new SubnetUtils(subnet).getInfo();
    Enumeration<NetworkInterface> nifs;

    try {
        nifs = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e) {
        LOG.error("Unable to get host interfaces", e);
        return addrs;
    }

    while (nifs.hasMoreElements()) {
        NetworkInterface nif = nifs.nextElement();
        // NB: adding addresses even if the nif is not up
        addMatchingAddrs(nif, subnetInfo, addrs);

        if (!returnSubinterfaces) {
            continue;
        }
        Enumeration<NetworkInterface> subNifs = nif.getSubInterfaces();
        while (subNifs.hasMoreElements()) {
            addMatchingAddrs(subNifs.nextElement(), subnetInfo, addrs);
        }
    }
    return addrs;
}

From source file:Main.java

private static void displayInterfaceInformation(NetworkInterface netint) throws SocketException {
    System.out.printf("Display name: %s%n", netint.getDisplayName());
    System.out.printf("Name: %s%n", netint.getName());
    Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
    for (InetAddress inetAddress : Collections.list(inetAddresses)) {
        System.out.printf("InetAddress: %s%n", inetAddress);
    }/*w w w  .j  av  a  2 s .  c  om*/

    System.out.printf("Parent: %s%n", netint.getParent());
    System.out.printf("Up? %s%n", netint.isUp());
    System.out.printf("Loopback? %s%n", netint.isLoopback());
    System.out.printf("PointToPoint? %s%n", netint.isPointToPoint());
    System.out.printf("Supports multicast? %s%n", netint.isVirtual());
    System.out.printf("Virtual? %s%n", netint.isVirtual());
    System.out.printf("Hardware address: %s%n", Arrays.toString(netint.getHardwareAddress()));
    System.out.printf("MTU: %s%n", netint.getMTU());

    List<InterfaceAddress> interfaceAddresses = netint.getInterfaceAddresses();
    for (InterfaceAddress addr : interfaceAddresses) {
        System.out.printf("InterfaceAddress: %s%n", addr.getAddress());
    }
    System.out.printf("%n");
    Enumeration<NetworkInterface> subInterfaces = netint.getSubInterfaces();
    for (NetworkInterface networkInterface : Collections.list(subInterfaces)) {
        System.out.printf("%nSubInterface%n");
        displayInterfaceInformation(networkInterface);
    }
    System.out.printf("%n");
}

From source file:org.trafodion.rest.util.NetworkConfiguration.java

public NetworkConfiguration(Configuration conf) throws Exception {

    this.conf = conf;
    ia = InetAddress.getLocalHost();

    String dnsInterface = conf.get(Constants.REST_DNS_INTERFACE, Constants.DEFAULT_REST_DNS_INTERFACE);
    if (dnsInterface.equalsIgnoreCase("default")) {
        intHostAddress = extHostAddress = ia.getHostAddress();
        canonicalHostName = ia.getCanonicalHostName();
        LOG.info("Using local host [" + canonicalHostName + "," + extHostAddress + "]");
    } else {/*from  w  w  w .  j ava2s  .com*/
        // For all nics get all hostnames and addresses   
        // and try to match against rest.dns.interface property 
        Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();
        while (nics.hasMoreElements() && !matchedInterface) {
            InetAddress inet = null;
            NetworkInterface ni = nics.nextElement();
            LOG.info("Found interface [" + ni.getDisplayName() + "]");
            if (dnsInterface.equalsIgnoreCase(ni.getDisplayName())) {
                LOG.info("Matched specified interface [" + ni.getName() + "]");
                inet = getInetAddress(ni);
                getCanonicalHostName(ni, inet);
            } else {
                Enumeration<NetworkInterface> subIfs = ni.getSubInterfaces();
                for (NetworkInterface subIf : Collections.list(subIfs)) {
                    LOG.debug("Sub Interface Display name [" + subIf.getDisplayName() + "]");
                    if (dnsInterface.equalsIgnoreCase(subIf.getDisplayName())) {
                        LOG.info("Matched subIf [" + subIf.getName() + "]");
                        inet = getInetAddress(subIf);
                        getCanonicalHostName(subIf, inet);
                        break;
                    }
                }
            }
        }
    }

    if (!matchedInterface)
        checkCloud();
}