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