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);
}
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);
}
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));
}
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;
}
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);
}
Aggregations