Search in sources :

Example 6 with Inet6Address

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

the class DefaultHostsFileEntriesResolverTest method shouldntFindWhenAddressTypeDoesntMatch.

@Test
public void shouldntFindWhenAddressTypeDoesntMatch() {
    Map<String, Inet4Address> inet4Entries = new HashMap<String, Inet4Address>();
    Map<String, Inet6Address> inet6Entries = new HashMap<String, Inet6Address>();
    inet4Entries.put("localhost", NetUtil.LOCALHOST4);
    DefaultHostsFileEntriesResolver resolver = new DefaultHostsFileEntriesResolver(new HostsFileEntries(inet4Entries, inet6Entries));
    InetAddress address = resolver.address("localhost", ResolvedAddressTypes.IPV6_ONLY);
    Assert.assertNull("Should pick an IPv6 address", address);
}
Also used : Inet4Address(java.net.Inet4Address) HashMap(java.util.HashMap) Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 7 with Inet6Address

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

the class DefaultHostsFileEntriesResolverTest method shouldPickIpv4WhenBothAreDefinedButIpv4IsPreferred.

@Test
public void shouldPickIpv4WhenBothAreDefinedButIpv4IsPreferred() {
    Map<String, Inet4Address> inet4Entries = new HashMap<String, Inet4Address>();
    Map<String, Inet6Address> inet6Entries = new HashMap<String, Inet6Address>();
    inet4Entries.put("localhost", NetUtil.LOCALHOST4);
    inet6Entries.put("localhost", NetUtil.LOCALHOST6);
    DefaultHostsFileEntriesResolver resolver = new DefaultHostsFileEntriesResolver(new HostsFileEntries(inet4Entries, inet6Entries));
    InetAddress address = resolver.address("localhost", ResolvedAddressTypes.IPV4_PREFERRED);
    Assert.assertTrue("Should pick an IPv4 address", address instanceof Inet4Address);
}
Also used : Inet4Address(java.net.Inet4Address) HashMap(java.util.HashMap) Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 8 with Inet6Address

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

the class NativeTest method testAddressIpv6.

@Test
public void testAddressIpv6() throws Exception {
    Inet6Address address = NetUtil.LOCALHOST6;
    InetSocketAddress inetAddress = new InetSocketAddress(address, 9999);
    byte[] bytes = new byte[24];
    ByteBuffer buffer = ByteBuffer.wrap(bytes);
    buffer.put(address.getAddress());
    buffer.putInt(address.getScopeId());
    buffer.putInt(inetAddress.getPort());
    Assert.assertEquals(inetAddress, address(buffer.array(), 0, bytes.length));
}
Also used : InetSocketAddress(java.net.InetSocketAddress) Inet6Address(java.net.Inet6Address) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 9 with Inet6Address

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

the class SocatPeerToPeerCopy method findIpv6.

private Inet6Address findIpv6(OpsTarget target) throws OpsException {
    Command command = Command.build("cat /proc/net/if_inet6");
    ProcessExecution execution = target.executeCommand(command);
    String inet6 = execution.getStdOut();
    // This didn't work for some reason (??)
    // String inet6 = target.readTextFile(new File("/proc/net/if_inet6"));
    List<Inet6Address> addresses = Lists.newArrayList();
    for (String line : Splitter.on('\n').split(inet6)) {
        line = line.trim();
        if (line.isEmpty()) {
            continue;
        }
        List<String> tokens = Lists.newArrayList(Splitter.on(CharMatcher.WHITESPACE).omitEmptyStrings().split(line));
        if (tokens.size() != 6) {
            throw new IllegalStateException("Cannot parse ipv6 address line: " + line);
        }
        String addressString = tokens.get(0);
        byte[] addr = Hex.fromHex(addressString);
        Inet6Address address;
        try {
            address = (Inet6Address) InetAddress.getByAddress(addr);
        } catch (UnknownHostException e) {
            throw new IllegalStateException("Error parsing IP address: " + line);
        }
        addresses.add(address);
    }
    IpRange publicIpv6 = IpRange.parse("2000::/3");
    for (Inet6Address address : addresses) {
        if (publicIpv6.isInRange(address)) {
            return address;
        }
    }
    return null;
}
Also used : IpRange(org.platformlayer.ops.networks.IpRange) UnknownHostException(java.net.UnknownHostException) Command(org.platformlayer.ops.Command) ProcessExecution(org.platformlayer.ops.process.ProcessExecution) Inet6Address(java.net.Inet6Address)

Example 10 with Inet6Address

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

the class SocatPeerToPeerCopy method findChannel.

private InetAddressPair findChannel(OpsTarget src, OpsTarget target) throws OpsException {
    InetAddress srcAddress = ((SshOpsTarget) src).getHost();
    InetAddress targetAddress = ((SshOpsTarget) target).getHost();
    if (srcAddress instanceof Inet4Address) {
        if (targetAddress instanceof Inet6Address) {
            Inet6Address srcIpv6 = findIpv6(src);
            if (srcIpv6 != null) {
                srcAddress = srcIpv6;
            } else {
                throw new UnsupportedOperationException();
            }
        }
    } else {
        if (targetAddress instanceof Inet4Address) {
            Inet6Address targetIpv6 = findIpv6(target);
            if (targetIpv6 != null) {
                targetAddress = targetIpv6;
            } else {
                throw new UnsupportedOperationException();
            }
        }
    }
    return new InetAddressPair(srcAddress, targetAddress);
}
Also used : Inet4Address(java.net.Inet4Address) SshOpsTarget(org.platformlayer.ops.SshOpsTarget) 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