Example usage for java.net Inet6Address getScopeId

List of usage examples for java.net Inet6Address getScopeId

Introduction

In this page you can find the example usage for java.net Inet6Address getScopeId.

Prototype

public int getScopeId() 

Source Link

Document

Returns the numeric scopeId, if this instance is associated with an interface.

Usage

From source file:eu.stratosphere.nephele.discovery.DiscoveryService.java

/**
 * Returns the set of broadcast addresses available to the network interfaces of this host. In case of IPv6 the set
 * contains the IPv6 multicast address to reach all nodes on the local link. Moreover, all addresses of the loopback
 * interfaces are added to the set./*from w w  w . j a v a  2  s . co m*/
 * 
 * @return (possibly empty) set of broadcast addresses reachable by this host
 */
private static Set<InetAddress> getBroadcastAddresses() {

    final Set<InetAddress> broadcastAddresses = new HashSet<InetAddress>();

    // get all network interfaces
    Enumeration<NetworkInterface> ie = null;
    try {
        ie = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e) {
        LOG.error("Could not collect network interfaces of host", e);
        return broadcastAddresses;
    }

    while (ie.hasMoreElements()) {
        NetworkInterface nic = ie.nextElement();
        try {
            if (!nic.isUp()) {
                continue;
            }

            if (nic.isLoopback()) {
                for (InterfaceAddress adr : nic.getInterfaceAddresses()) {
                    broadcastAddresses.add(adr.getAddress());
                }
            } else {

                // check all IPs bound to network interfaces
                for (InterfaceAddress adr : nic.getInterfaceAddresses()) {

                    if (adr == null) {
                        continue;
                    }

                    // collect all broadcast addresses
                    if (USE_IPV6) {
                        try {
                            final InetAddress interfaceAddress = adr.getAddress();
                            if (interfaceAddress instanceof Inet6Address) {
                                final Inet6Address ipv6Address = (Inet6Address) interfaceAddress;
                                final InetAddress multicastAddress = InetAddress.getByName(IPV6MULTICASTADDRESS
                                        + "%" + Integer.toString(ipv6Address.getScopeId()));
                                broadcastAddresses.add(multicastAddress);
                            }

                        } catch (UnknownHostException e) {
                            LOG.error(e);
                        }
                    } else {
                        final InetAddress broadcast = adr.getBroadcast();
                        if (broadcast != null) {
                            broadcastAddresses.add(broadcast);
                        }
                    }
                }
            }

        } catch (SocketException e) {
            LOG.error("Socket exception when checking " + nic.getName() + ". " + "Ignoring this device.", e);
        }
    }

    return broadcastAddresses;
}

From source file:ch.cyberduck.core.socket.NetworkInterfaceAwareSocketFactory.java

private NetworkInterface findIPv6Interface(Inet6Address address) throws IOException {
    if (blacklisted.isEmpty()) {
        if (log.isDebugEnabled()) {
            log.debug("Ignore IP6 default network interface setup with empty blacklist");
        }/*from   w  w w. j a v a2s . com*/
        return null;
    }
    if (address.getScopeId() != 0) {
        if (log.isDebugEnabled()) {
            log.debug(String.format(
                    "Ignore IP6 default network interface setup for address with scope identifier %d",
                    address.getScopeId()));
        }
        return null;
    }
    // If we find an interface name en0 that supports IPv6 make it the default.
    // We must use the index of the network interface. Referencing the interface by name will still
    // set the scope id to '0' referencing the awdl0 interface that is first in the list of enumerated
    // network interfaces instead of its correct index in <code>java.net.Inet6Address</code>
    // Use private API to defer the numeric format for the address
    List<Integer> indexes = new ArrayList<Integer>();
    final Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces();
    while (enumeration.hasMoreElements()) {
        indexes.add(enumeration.nextElement().getIndex());
    }
    for (Integer index : indexes) {
        final NetworkInterface n = NetworkInterface.getByIndex(index);
        if (log.isDebugEnabled()) {
            log.debug(String.format("Evaluate interface with %s index %d", n, index));
        }
        if (!n.isUp()) {
            if (log.isDebugEnabled()) {
                log.debug(String.format("Ignore interface %s not up", n));
            }
            continue;
        }
        if (blacklisted.contains(n.getName())) {
            log.warn(String.format("Ignore network interface %s disabled with blacklist", n));
            continue;
        }
        for (InterfaceAddress i : n.getInterfaceAddresses()) {
            if (i.getAddress() instanceof Inet6Address) {
                if (log.isInfoEnabled()) {
                    log.info(String.format("Selected network interface %s", n));
                }
                return n;
            }
        }
        log.warn(String.format("No IPv6 for interface %s", n));
    }
    log.warn("No network interface found for IPv6");
    return null;
}