use of java.net.NetworkInterface 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.NetworkInterface in project springside4 by springside.
the class NetUtil method findLocalAddressViaNetworkInterface.
/**
* 根据preferNamePrefix 与 defaultNicList的配置网卡,找出合适的网卡
*/
private static InetAddress findLocalAddressViaNetworkInterface() {
// 如果hostname +/etc/hosts 得到的是127.0.0.1, 则首选这块网卡
String preferNamePrefix = SystemPropertiesUtil.getString("localhost.prefer.nic.prefix", "LOCALHOST_PREFER_NIC_PREFIX", "bond0.");
// 如果hostname +/etc/hosts 得到的是127.0.0.1, 和首选网卡都不符合要求,则按顺序遍历下面的网卡
String defaultNicList = SystemPropertiesUtil.getString("localhost.default.nic.list", "LOCALHOST_DEFAULT_NIC_LIST", "bond0,eth0,em0,br0");
InetAddress resultAddress = null;
Map<String, NetworkInterface> candidateInterfaces = MapUtil.newHashMap();
// 遍历所有网卡,找出所有可用网卡,尝试找出符合prefer前缀的网卡
try {
for (Enumeration<NetworkInterface> allInterfaces = NetworkInterface.getNetworkInterfaces(); allInterfaces.hasMoreElements(); ) {
NetworkInterface nic = allInterfaces.nextElement();
// 检查网卡可用并支持广播
try {
if (!nic.isUp() || !nic.supportsMulticast()) {
continue;
}
} catch (SocketException e) {
continue;
}
// 检查是否符合prefer前缀
String name = nic.getName();
if (name.startsWith(preferNamePrefix)) {
// 检查有否非ipv6 非127.0.0.1的inetaddress
resultAddress = findAvailableInetAddress(nic);
if (resultAddress != null) {
return resultAddress;
}
} else {
// 不是Prefer前缀,先放入可选列表
candidateInterfaces.put(name, nic);
}
}
for (String nifName : defaultNicList.split(",")) {
NetworkInterface nic = candidateInterfaces.get(nifName);
if (nic != null) {
resultAddress = findAvailableInetAddress(nic);
if (resultAddress != null) {
return resultAddress;
}
}
}
} catch (SocketException e) {
return null;
}
return null;
}
use of java.net.NetworkInterface in project torodb by torodb.
the class ObjectIdFactory method createMachineId.
private static int createMachineId() {
int machineId;
try {
Hasher hasher = Hashing.crc32c().newHasher();
Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();
boolean atLeastOne = false;
while (nics.hasMoreElements()) {
NetworkInterface ni = nics.nextElement();
if (ni != null) {
byte[] macAddress = ni.getHardwareAddress();
if (macAddress != null) {
for (byte b : macAddress) {
atLeastOne = true;
hasher.putByte(b);
}
}
}
}
if (!atLeastOne) {
LOGGER.warn("Failed to calculate the machine id. A random number is used");
machineId = new SecureRandom().nextInt();
} else {
machineId = hasher.hash().asInt();
}
} catch (SocketException ex) {
LOGGER.warn("Failed to calculate the machine id. A random number is used");
machineId = new SecureRandom().nextInt();
}
return machineId & 0xFFFFFF;
}
use of java.net.NetworkInterface in project blade by biezhi.
the class IPKit method getRealIp.
/**
* @return 本机IP
* @throws SocketException
*/
public static String getRealIp() throws SocketException {
// 本地IP,如果没有配置外网IP则返回它
String localip = null;
// 外网IP
String netip = null;
Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
InetAddress ip = null;
// 是否找到外网IP
boolean finded = false;
while (netInterfaces.hasMoreElements() && !finded) {
NetworkInterface ni = netInterfaces.nextElement();
Enumeration<InetAddress> address = ni.getInetAddresses();
while (address.hasMoreElements()) {
ip = address.nextElement();
if (!ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) {
// 外网IP
netip = ip.getHostAddress();
finded = true;
break;
} else if (ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) {
// 内网IP
localip = ip.getHostAddress();
}
}
}
if (netip != null && !"".equals(netip)) {
return netip;
} else {
return localip;
}
}
use of java.net.NetworkInterface 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;
}
Aggregations