use of java.net.InterfaceAddress in project DistributedFractalNetwork by Budder21.
the class Utils method getBroadcastAddress.
/**
* This method finds and returns the broadcast InetAddress of the specific
* router being accessed
*
* @return the broadcast InetAddress of the specific router being accessed
*/
public static InetAddress getBroadcastAddress() {
try {
Set<InetAddress> set = new LinkedHashSet<>();
Enumeration<NetworkInterface> nicList = NetworkInterface.getNetworkInterfaces();
for (; nicList.hasMoreElements(); ) {
NetworkInterface nic = nicList.nextElement();
if (nic.isUp() && !nic.isLoopback()) {
for (InterfaceAddress ia : nic.getInterfaceAddresses()) set.add(ia.getBroadcast());
}
}
return Arrays.asList(set.toArray(new InetAddress[0])).get(0);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
use of java.net.InterfaceAddress in project nutz by nutzam.
the class Networks method networkItems.
public static Map<String, NetworkItem> networkItems() {
Map<String, NetworkItem> netFaces = new LinkedHashMap<String, NetworkItem>();
try {
Enumeration<NetworkInterface> network = NetworkInterface.getNetworkInterfaces();
while (network.hasMoreElements()) {
NetworkItem netItem = new NetworkItem();
NetworkInterface face = network.nextElement();
byte[] data = face.getHardwareAddress();
try {
if (data != null && data.length > 0) {
StringBuilder sb = new StringBuilder();
for (byte b : data) sb.append(Strings.toHex(b, 2));
netItem.setMac(sb.toString().toUpperCase());
if (netItem.getMac().startsWith("000000000"))
continue;
}
} catch (Throwable e) {
}
List<InterfaceAddress> addrs = face.getInterfaceAddresses();
if (addrs != null && !addrs.isEmpty()) {
for (InterfaceAddress interfaceAddress : addrs) {
InetAddress iaddr = interfaceAddress.getAddress();
String ip = iaddr.getHostAddress();
if (ip == null || ip.length() == 0)
continue;
if (!netItem.hasName())
netItem.setName(iaddr.getHostName());
if (ip.contains("."))
netItem.setIpv4(ip);
else
netItem.setIpv6(ip);
}
}
netItem.setMtu(face.getMTU());
netItem.setDisplay(face.getDisplayName());
if (!netItem.hasName()) {
netItem.setName(face.getName());
}
if (netItem.getIpv4() == null && netItem.getMac() == null && netItem.getMtu() < 1 && !face.getName().startsWith("eth"))
continue;
netFaces.put(face.getName(), netItem);
}
} catch (Throwable e) {
}
if (Lang.isWin() && netFaces.size() > 0) {
for (Entry<String, NetworkItem> en : netFaces.entrySet()) {
NetworkItem item = en.getValue();
if (item != null && ipOk(item.getIpv4()) && item.getIpv4().startsWith("10.")) {
netFaces.put("tun0", item);
break;
}
}
}
return netFaces;
}
use of java.net.InterfaceAddress in project Fling by entertailion.
the class FlingFrame method getPreferredInetAddress.
private InterfaceAddress getPreferredInetAddress(String prefix) {
InterfaceAddress selectedInterfaceAddress = null;
try {
Enumeration<NetworkInterface> list = NetworkInterface.getNetworkInterfaces();
while (list.hasMoreElements()) {
NetworkInterface iface = list.nextElement();
if (iface == null)
continue;
Log.d(LOG_TAG, "interface=" + iface.getName());
Iterator<InterfaceAddress> it = iface.getInterfaceAddresses().iterator();
while (it.hasNext()) {
InterfaceAddress interfaceAddress = it.next();
if (interfaceAddress == null)
continue;
InetAddress address = interfaceAddress.getAddress();
Log.d(LOG_TAG, "address=" + address);
if (address instanceof Inet4Address) {
// same subnet as the selected ChromeCast device
if (address.getHostAddress().toString().startsWith(prefix)) {
return interfaceAddress;
}
}
}
}
} catch (Exception ex) {
}
return selectedInterfaceAddress;
}
use of java.net.InterfaceAddress in project calcite by apache.
the class HttpServer method findLocalIpAddress.
private static String findLocalIpAddress() throws IOException {
String defaultIpOverride = System.getenv("CALCITE_LOCAL_IP");
if (defaultIpOverride != null) {
return defaultIpOverride;
} else {
final InetAddress address = InetAddress.getLocalHost();
if (address.isLoopbackAddress()) {
// interfaces.
for (NetworkInterface ni : iterable(NetworkInterface.getNetworkInterfaces())) {
for (InterfaceAddress interfaceAddress : ni.getInterfaceAddresses()) {
final InetAddress addr = interfaceAddress.getAddress();
if (!addr.isLinkLocalAddress() && !addr.isLoopbackAddress() && addr instanceof Inet4Address) {
// We've found an address that looks reasonable!
logWarning("Your hostname, " + InetAddress.getLocalHost().getHostName() + " resolves to a loopback address: " + address.getHostAddress() + "; using " + addr.getHostAddress() + " instead (on interface " + ni.getName() + ")");
logWarning("Set CALCITE_LOCAL_IP if you need to bind to another address");
return addr.getHostAddress();
}
}
}
logWarning("Your hostname, " + InetAddress.getLocalHost().getHostName() + " resolves to a loopback address: " + address.getHostAddress() + ", but we couldn't find any external IP address!");
logWarning("Set CALCITE_LOCAL_IP if you need to bind to another address");
}
return address.getHostAddress();
}
}
use of java.net.InterfaceAddress in project cloudstack by apache.
the class NetUtils method getLocalCidrs.
public static String[] getLocalCidrs() {
final String defaultHostIp = getDefaultHostIp();
final List<String> cidrList = new ArrayList<String>();
try {
for (final NetworkInterface ifc : IteratorUtil.enumerationAsIterable(NetworkInterface.getNetworkInterfaces())) {
if (ifc.isUp() && !ifc.isVirtual() && !ifc.isLoopback()) {
for (final InterfaceAddress address : ifc.getInterfaceAddresses()) {
final InetAddress addr = address.getAddress();
final int prefixLength = address.getNetworkPrefixLength();
if (prefixLength < MAX_CIDR && prefixLength > 0) {
final String ip = addr.getHostAddress();
if (ip.equalsIgnoreCase(defaultHostIp)) {
cidrList.add(ipAndNetMaskToCidr(ip, getCidrNetmask(prefixLength)));
}
}
}
}
}
} catch (final SocketException e) {
s_logger.warn("UnknownHostException in getLocalCidrs().", e);
}
return cidrList.toArray(new String[0]);
}
Aggregations