Search in sources :

Example 21 with Inet6Address

use of java.net.Inet6Address in project cassandra by apache.

the class DatabaseDescriptorTest method selectSuitableInterface.

/*
     * Server only accepts interfaces by name if they have a single address
     * OS X seems to always have an ipv4 and ipv6 address on all interfaces which means some tests fail
     * if not checked for and skipped
     */
@BeforeClass
public static void selectSuitableInterface() throws Exception {
    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
    while (interfaces.hasMoreElements()) {
        NetworkInterface intf = interfaces.nextElement();
        System.out.println("Evaluating " + intf.getName());
        if (intf.isLoopback()) {
            suitableInterface = intf;
            boolean hasIPv4 = false;
            boolean hasIPv6 = false;
            Enumeration<InetAddress> addresses = suitableInterface.getInetAddresses();
            while (addresses.hasMoreElements()) {
                if (addresses.nextElement() instanceof Inet6Address)
                    hasIPv6 = true;
                else
                    hasIPv4 = true;
            }
            hasIPv4andIPv6 = hasIPv4 && hasIPv6;
            return;
        }
    }
}
Also used : NetworkInterface(java.net.NetworkInterface) Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress) BeforeClass(org.junit.BeforeClass)

Example 22 with Inet6Address

use of java.net.Inet6Address in project springside4 by springside.

the class NetUtil method initLocalAddress.

/**
	 * 初始化本地地址
	 */
private static void initLocalAddress() {
    NetworkInterface nic = null;
    // 根据命令行执行hostname获得本机hostname, 与/etc/hosts 中该hostname的第一条ip配置,获得ip地址
    try {
        localAddress = InetAddress.getLocalHost();
        nic = NetworkInterface.getByInetAddress(localAddress);
    } catch (Exception ignored) {
    // NOSONAR
    }
    // 如果结果为空,或是一个loopback地址(127.0.0.1), 或是ipv6地址,再遍历网卡尝试获取
    if (localAddress == null || nic == null || localAddress.isLoopbackAddress() || localAddress instanceof Inet6Address) {
        InetAddress lookedUpAddr = findLocalAddressViaNetworkInterface();
        // 仍然不符合要求,只好使用127.0.0.1
        try {
            localAddress = lookedUpAddr != null ? lookedUpAddr : InetAddress.getByName("127.0.0.1");
        } catch (UnknownHostException ignored) {
        // NOSONAR
        }
    }
    localHost = IPUtil.toString(localAddress);
    logger.info("localhost is {}", localHost);
}
Also used : UnknownHostException(java.net.UnknownHostException) NetworkInterface(java.net.NetworkInterface) Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress) UnknownHostException(java.net.UnknownHostException) SocketException(java.net.SocketException)

Example 23 with Inet6Address

use of java.net.Inet6Address in project hazelcast by hazelcast.

the class AddressHelper method getPossibleSocketAddresses.

public static Collection<InetSocketAddress> getPossibleSocketAddresses(int port, String scopedAddress, int portTryCount) {
    InetAddress inetAddress = null;
    try {
        inetAddress = InetAddress.getByName(scopedAddress);
    } catch (UnknownHostException ignored) {
        Logger.getLogger(AddressHelper.class).finest("Address not available", ignored);
    }
    int possiblePort = port;
    if (possiblePort == -1) {
        possiblePort = INITIAL_FIRST_PORT;
    }
    final Collection<InetSocketAddress> socketAddresses = new LinkedList<InetSocketAddress>();
    if (inetAddress == null) {
        for (int i = 0; i < portTryCount; i++) {
            socketAddresses.add(new InetSocketAddress(scopedAddress, possiblePort + i));
        }
    } else if (inetAddress instanceof Inet4Address) {
        for (int i = 0; i < portTryCount; i++) {
            socketAddresses.add(new InetSocketAddress(inetAddress, possiblePort + i));
        }
    } else if (inetAddress instanceof Inet6Address) {
        final Collection<Inet6Address> addresses = getPossibleInetAddressesFor((Inet6Address) inetAddress);
        for (Inet6Address inet6Address : addresses) {
            for (int i = 0; i < portTryCount; i++) {
                socketAddresses.add(new InetSocketAddress(inet6Address, possiblePort + i));
            }
        }
    }
    return socketAddresses;
}
Also used : Inet4Address(java.net.Inet4Address) UnknownHostException(java.net.UnknownHostException) InetSocketAddress(java.net.InetSocketAddress) Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress) LinkedList(java.util.LinkedList)

Example 24 with Inet6Address

use of java.net.Inet6Address in project hazelcast by hazelcast.

the class DefaultAddressPicker method pickMatchingAddress.

private AddressDefinition pickMatchingAddress(Collection<InterfaceDefinition> interfaces) throws SocketException {
    Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
    boolean preferIPv4Stack = preferIPv4Stack();
    while (networkInterfaces.hasMoreElements()) {
        NetworkInterface ni = networkInterfaces.nextElement();
        Enumeration<InetAddress> e = ni.getInetAddresses();
        while (e.hasMoreElements()) {
            InetAddress inetAddress = e.nextElement();
            if (preferIPv4Stack && inetAddress instanceof Inet6Address) {
                continue;
            }
            if (interfaces != null && !interfaces.isEmpty()) {
                AddressDefinition address = match(inetAddress, interfaces);
                if (address != null) {
                    return address;
                }
            } else if (!inetAddress.isLoopbackAddress()) {
                return new AddressDefinition(inetAddress);
            }
        }
    }
    return null;
}
Also used : NetworkInterface(java.net.NetworkInterface) Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress) AddressUtil.fixScopeIdAndGetInetAddress(com.hazelcast.util.AddressUtil.fixScopeIdAndGetInetAddress)

Example 25 with Inet6Address

use of java.net.Inet6Address in project hazelcast by hazelcast.

the class AddressUtil method findRealInet6Address.

private static Inet6Address findRealInet6Address(Inet6Address inet6Address) throws SocketException {
    Inet6Address resultInetAddress = null;
    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
    while (interfaces.hasMoreElements()) {
        NetworkInterface ni = interfaces.nextElement();
        Enumeration<InetAddress> addresses = ni.getInetAddresses();
        while (addresses.hasMoreElements()) {
            InetAddress address = addresses.nextElement();
            if (!isInet6Compatible(address, inet6Address)) {
                continue;
            }
            if (resultInetAddress != null) {
                throw new IllegalArgumentException("This address " + inet6Address + " is bound to more than one network interface!");
            }
            resultInetAddress = (Inet6Address) address;
        }
    }
    return resultInetAddress;
}
Also used : NetworkInterface(java.net.NetworkInterface) Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress)

Aggregations

Inet6Address (java.net.Inet6Address)137 InetAddress (java.net.InetAddress)92 Inet4Address (java.net.Inet4Address)45 NetworkInterface (java.net.NetworkInterface)28 LinkAddress (android.net.LinkAddress)21 IpPrefix (android.net.IpPrefix)19 IOException (java.io.IOException)18 UnknownHostException (java.net.UnknownHostException)17 LinkProperties (android.net.LinkProperties)15 SocketException (java.net.SocketException)15 RouteInfo (android.net.RouteInfo)14 InetSocketAddress (java.net.InetSocketAddress)12 ByteBuffer (java.nio.ByteBuffer)9 HashMap (java.util.HashMap)8 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)7 RaParams (android.net.ip.RouterAdvertisementDaemon.RaParams)5 Pair (android.util.Pair)5 ProvisioningChange (android.net.LinkProperties.ProvisioningChange)4 StructNdMsg (android.net.netlink.StructNdMsg)4