use of java.net.NetworkInterface in project cloudstack by apache.
the class NetUtils method getMacAddressAsLong.
public static long getMacAddressAsLong(final InetAddress address) {
long macAddressAsLong = 0;
try {
final NetworkInterface ni = NetworkInterface.getByInetAddress(address);
final byte[] mac = ni.getHardwareAddress();
for (int i = 0; i < mac.length; i++) {
macAddressAsLong |= (long) (mac[i] & 0xff) << (mac.length - i - 1) * 8;
}
} catch (final SocketException e) {
s_logger.error("SocketException when trying to retrieve MAC address", e);
}
return macAddressAsLong;
}
use of java.net.NetworkInterface in project ignite by apache.
the class IgniteUtils method allLocalIps.
/**
* Gets a list of all local non-loopback IPs known to this JVM.
* Note that this will include both IPv4 and IPv6 addresses (even if one "resolves"
* into another). Loopbacks will be skipped.
*
* @return List of all known local IPs (empty list if no addresses available).
*/
public static synchronized Collection<String> allLocalIps() {
List<String> ips = new ArrayList<>(4);
try {
Enumeration<NetworkInterface> itfs = NetworkInterface.getNetworkInterfaces();
if (itfs != null) {
for (NetworkInterface itf : asIterable(itfs)) {
if (!itf.isLoopback()) {
Enumeration<InetAddress> addrs = itf.getInetAddresses();
for (InetAddress addr : asIterable(addrs)) {
String hostAddr = addr.getHostAddress();
if (!addr.isLoopbackAddress() && !ips.contains(hostAddr))
ips.add(hostAddr);
}
}
}
}
} catch (SocketException ignore) {
return Collections.emptyList();
}
Collections.sort(ips);
return ips;
}
use of java.net.NetworkInterface in project ignite by apache.
the class IgniteUtils method resolveLocalAddresses.
/**
* Returns host names consistent with {@link #resolveLocalHost(String)}. So when it returns
* a common address this method returns single host name, and when a wildcard address passed
* this method tries to collect addresses of all available interfaces.
*
* @param locAddr Local address to resolve.
* @param allHostNames If {@code true} then include host names for all addresses.
* @return Resolved available addresses and host names of given local address.
* @throws IOException If failed.
* @throws IgniteCheckedException If no network interfaces found.
*/
public static IgniteBiTuple<Collection<String>, Collection<String>> resolveLocalAddresses(InetAddress locAddr, boolean allHostNames) throws IOException, IgniteCheckedException {
assert locAddr != null;
Collection<String> addrs = new ArrayList<>();
Collection<String> hostNames = new ArrayList<>();
if (locAddr.isAnyLocalAddress()) {
IgniteBiTuple<Collection<String>, Collection<String>> res = allHostNames ? cachedLocalAddrAllHostNames : cachedLocalAddr;
if (res == null) {
List<InetAddress> locAddrs = new ArrayList<>();
for (NetworkInterface itf : asIterable(NetworkInterface.getNetworkInterfaces())) {
for (InetAddress addr : asIterable(itf.getInetAddresses())) {
if (!addr.isLinkLocalAddress())
locAddrs.add(addr);
}
}
locAddrs = filterReachable(locAddrs);
for (InetAddress addr : locAddrs) addresses(addr, addrs, hostNames, allHostNames);
if (F.isEmpty(addrs))
throw new IgniteCheckedException("No network addresses found (is networking enabled?).");
res = F.t(addrs, hostNames);
if (allHostNames)
cachedLocalAddrAllHostNames = res;
else
cachedLocalAddr = res;
}
return res;
}
addresses(locAddr, addrs, hostNames, allHostNames);
return F.t(addrs, hostNames);
}
use of java.net.NetworkInterface in project ignite by apache.
the class IgniteUtils method allLocalMACs.
/**
* Gets a list of all local enabled MACs known to this JVM. It
* is using hardware address of the network interface that is not guaranteed to be
* MAC addresses (but in most cases it is).
* <p>
* Note that if network interface is disabled - its MAC won't be included. All
* local network interfaces are probed including loopbacks. Virtual interfaces
* (sub-interfaces) are skipped.
* <p>
* Note that on linux getHardwareAddress() can return null from time to time
* if NetworkInterface.getHardwareAddress() method is called from many threads.
*
* @return List of all known enabled local MACs or empty list
* if no MACs could be found.
*/
public static synchronized Collection<String> allLocalMACs() {
List<String> macs = new ArrayList<>(3);
try {
Enumeration<NetworkInterface> itfs = NetworkInterface.getNetworkInterfaces();
if (itfs != null) {
for (NetworkInterface itf : asIterable(itfs)) {
byte[] hwAddr = itf.getHardwareAddress();
// Loopback produces empty MAC.
if (hwAddr != null && hwAddr.length > 0) {
String mac = byteArray2HexString(hwAddr);
if (!macs.contains(mac))
macs.add(mac);
}
}
}
} catch (SocketException ignore) {
return Collections.emptyList();
}
Collections.sort(macs);
return macs;
}
use of java.net.NetworkInterface in project ignite by apache.
the class IgniteUtils method resetLocalHost.
/**
* @return Local host.
* @throws IOException If attempt to get local host failed.
*/
private static synchronized InetAddress resetLocalHost() throws IOException {
locHost = null;
String sysLocHost = IgniteSystemProperties.getString(IGNITE_LOCAL_HOST);
if (sysLocHost != null)
sysLocHost = sysLocHost.trim();
if (!F.isEmpty(sysLocHost))
locHost = InetAddress.getByName(sysLocHost);
else {
List<NetworkInterface> itfs = new ArrayList<>();
for (NetworkInterface itf : asIterable(NetworkInterface.getNetworkInterfaces())) itfs.add(itf);
Collections.sort(itfs, new Comparator<NetworkInterface>() {
@Override
public int compare(NetworkInterface itf1, NetworkInterface itf2) {
// Interfaces whose name starts with 'e' should go first.
return itf1.getName().compareTo(itf2.getName());
}
});
// It should not take longer than 2 seconds to reach
// local address on any network.
int reachTimeout = 2000;
for (NetworkInterface itf : itfs) {
boolean found = false;
for (InetAddress addr : asIterable(itf.getInetAddresses())) {
if (!addr.isLoopbackAddress() && !addr.isLinkLocalAddress() && reachable(itf, addr, reachTimeout)) {
locHost = addr;
found = true;
break;
}
}
if (found)
break;
}
}
if (locHost == null)
locHost = InetAddress.getLocalHost();
return locHost;
}
Aggregations