Search in sources :

Example 91 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 92 with Inet6Address

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

the class IPv6HostnamesITCase method getLocalIPv6Address.

private Inet6Address getLocalIPv6Address() {
    try {
        Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
        while (e.hasMoreElements()) {
            NetworkInterface netInterface = e.nextElement();
            // for each address of the network interface
            Enumeration<InetAddress> ee = netInterface.getInetAddresses();
            while (ee.hasMoreElements()) {
                InetAddress addr = ee.nextElement();
                if (addr instanceof Inet6Address && (!addr.isLoopbackAddress()) && (!addr.isAnyLocalAddress())) {
                    // see if it is possible to bind to the address
                    InetSocketAddress socketAddress = new InetSocketAddress(addr, 0);
                    try {
                        log.info("Considering address " + addr);
                        // test whether we can bind a socket to that address
                        log.info("Testing whether sockets can bind to " + addr);
                        ServerSocket sock = new ServerSocket();
                        sock.bind(socketAddress);
                        sock.close();
                        // test whether Akka's netty can bind to the address
                        log.info("Testing whether Akka can use " + addr);
                        try (NetUtils.Port port = NetUtils.getAvailablePort()) {
                            final RpcService rpcService = RpcSystem.load().localServiceBuilder(new Configuration()).withBindAddress(addr.getHostAddress()).withBindPort(port.getPort()).createAndStart();
                            rpcService.stopService().get();
                        }
                        log.info("Using address " + addr);
                        return (Inet6Address) addr;
                    } catch (IOException ignored) {
                    // fall through the loop
                    }
                }
            }
        }
        return null;
    } catch (Exception e) {
        return null;
    }
}
Also used : MiniClusterResourceConfiguration(org.apache.flink.runtime.testutils.MiniClusterResourceConfiguration) Configuration(org.apache.flink.configuration.Configuration) InetSocketAddress(java.net.InetSocketAddress) NetworkInterface(java.net.NetworkInterface) Inet6Address(java.net.Inet6Address) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) AssumptionViolatedException(org.junit.AssumptionViolatedException) IOException(java.io.IOException) NetUtils(org.apache.flink.util.NetUtils) RpcService(org.apache.flink.runtime.rpc.RpcService) InetAddress(java.net.InetAddress)

Example 93 with Inet6Address

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

the class PcapWriteHandler method completeTCPWrite.

/**
 * Write TCP/IP L3 and L2 here.
 *
 * @param srcAddr          {@link InetSocketAddress} Source Address of this Packet
 * @param dstAddr          {@link InetSocketAddress} Destination Address of this Packet
 * @param tcpBuf           {@link ByteBuf} containing TCP L4 Data
 * @param byteBufAllocator {@link ByteBufAllocator} for allocating bytes for TCP/IP L3 and L2 data.
 * @param ctx              {@link ChannelHandlerContext} for {@code fireExceptionCaught}
 */
private void completeTCPWrite(InetSocketAddress srcAddr, InetSocketAddress dstAddr, ByteBuf tcpBuf, ByteBufAllocator byteBufAllocator, ChannelHandlerContext ctx) {
    ByteBuf ipBuf = byteBufAllocator.buffer();
    ByteBuf ethernetBuf = byteBufAllocator.buffer();
    ByteBuf pcap = byteBufAllocator.buffer();
    try {
        if (srcAddr.getAddress() instanceof Inet4Address && dstAddr.getAddress() instanceof Inet4Address) {
            IPPacket.writeTCPv4(ipBuf, tcpBuf, NetUtil.ipv4AddressToInt((Inet4Address) srcAddr.getAddress()), NetUtil.ipv4AddressToInt((Inet4Address) dstAddr.getAddress()));
            EthernetPacket.writeIPv4(ethernetBuf, ipBuf);
        } else if (srcAddr.getAddress() instanceof Inet6Address && dstAddr.getAddress() instanceof Inet6Address) {
            IPPacket.writeTCPv6(ipBuf, tcpBuf, srcAddr.getAddress().getAddress(), dstAddr.getAddress().getAddress());
            EthernetPacket.writeIPv6(ethernetBuf, ipBuf);
        } else {
            logger.error("Source and Destination IP Address versions are not same. Source Address: {}, " + "Destination Address: {}", srcAddr.getAddress(), dstAddr.getAddress());
            return;
        }
        // Write Packet into Pcap
        pCapWriter.writePacket(pcap, ethernetBuf);
    } catch (IOException ex) {
        logger.error("Caught Exception While Writing Packet into Pcap", ex);
        ctx.fireExceptionCaught(ex);
    } finally {
        ipBuf.release();
        ethernetBuf.release();
        pcap.release();
    }
}
Also used : Inet4Address(java.net.Inet4Address) Inet6Address(java.net.Inet6Address) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf)

Example 94 with Inet6Address

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

the class DnsNameResolverTest method testResolveIpv6WithScopeId0.

private void testResolveIpv6WithScopeId0(boolean resolveAll) throws Exception {
    DnsNameResolver resolver = newResolver().build();
    String address = "fe80:0:0:0:1c31:d1d1:4824:72a9";
    int scopeId = 15;
    String addressString = address + '%' + scopeId;
    byte[] bytes = NetUtil.createByteArrayFromIpAddressString(address);
    Inet6Address inet6Address = Inet6Address.getByAddress(null, bytes, scopeId);
    try {
        final InetAddress addr;
        if (resolveAll) {
            List<InetAddress> addressList = resolver.resolveAll(addressString).getNow();
            assertEquals(1, addressList.size());
            addr = addressList.get(0);
        } else {
            addr = resolver.resolve(addressString).getNow();
        }
        assertEquals(inet6Address, addr);
    } finally {
        resolver.close();
    }
}
Also used : Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress)

Example 95 with Inet6Address

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

the class DnsNameResolver method anyInterfaceSupportsIpV6.

/**
 * Returns {@code true} if any {@link NetworkInterface} supports {@code IPv6}, {@code false} otherwise.
 */
private static boolean anyInterfaceSupportsIpV6() {
    try {
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements()) {
            NetworkInterface iface = interfaces.nextElement();
            Enumeration<InetAddress> addresses = iface.getInetAddresses();
            while (addresses.hasMoreElements()) {
                InetAddress inetAddress = addresses.nextElement();
                if (inetAddress instanceof Inet6Address && !inetAddress.isAnyLocalAddress() && !inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress()) {
                    return true;
                }
            }
        }
    } catch (SocketException e) {
        logger.debug("Unable to detect if any interface supports IPv6, assuming IPv4-only", e);
    // ignore
    }
    return false;
}
Also used : SocketException(java.net.SocketException) NetworkInterface(java.net.NetworkInterface) Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress)

Aggregations

Inet6Address (java.net.Inet6Address)286 InetAddress (java.net.InetAddress)196 Inet4Address (java.net.Inet4Address)110 NetworkInterface (java.net.NetworkInterface)54 UnknownHostException (java.net.UnknownHostException)51 SocketException (java.net.SocketException)32 InetSocketAddress (java.net.InetSocketAddress)31 IOException (java.io.IOException)29 LinkAddress (android.net.LinkAddress)28 Test (org.junit.Test)26 RouteInfo (android.net.RouteInfo)21 IpPrefix (android.net.IpPrefix)19 ArrayList (java.util.ArrayList)19 LinkProperties (android.net.LinkProperties)15 ByteBuffer (java.nio.ByteBuffer)9 InterfaceAddress (java.net.InterfaceAddress)7 StringJoiner (java.util.StringJoiner)7 PrintWriter (java.io.PrintWriter)6 HashMap (java.util.HashMap)6 Test (org.junit.jupiter.api.Test)6