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