use of java.net.NetworkInterface in project CloudStack-archive by CloudStack-extras.
the class NetUtils method getLocalCidrs.
public static String[] getLocalCidrs() {
String defaultHostIp = getDefaultHostIp();
List<String> cidrList = new ArrayList<String>();
try {
for (NetworkInterface ifc : IteratorUtil.enumerationAsIterable(NetworkInterface.getNetworkInterfaces())) {
if (ifc.isUp() && !ifc.isVirtual() && !ifc.isLoopback()) {
for (InterfaceAddress address : ifc.getInterfaceAddresses()) {
InetAddress addr = address.getAddress();
int prefixLength = address.getNetworkPrefixLength();
if (prefixLength < 32 && prefixLength > 0) {
String ip = ipFromInetAddress(addr);
if (ip.equalsIgnoreCase(defaultHostIp))
cidrList.add(ipAndNetMaskToCidr(ip, getCidrNetmask(prefixLength)));
}
}
}
}
} catch (SocketException e) {
s_logger.warn("UnknownHostException in getLocalCidrs().", e);
}
return cidrList.toArray(new String[0]);
}
use of java.net.NetworkInterface in project AndroidUtilCode by Blankj.
the class NetworkUtils method getIPAddress.
/**
* 获取IP地址
* <p>需添加权限 {@code <uses-permission android:name="android.permission.INTERNET"/>}</p>
*
* @param useIPv4 是否用IPv4
* @return IP地址
*/
public static String getIPAddress(boolean useIPv4) {
try {
for (Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces(); nis.hasMoreElements(); ) {
NetworkInterface ni = nis.nextElement();
// 防止小米手机返回10.0.2.15
if (!ni.isUp())
continue;
for (Enumeration<InetAddress> addresses = ni.getInetAddresses(); addresses.hasMoreElements(); ) {
InetAddress inetAddress = addresses.nextElement();
if (!inetAddress.isLoopbackAddress()) {
String hostAddress = inetAddress.getHostAddress();
boolean isIPv4 = hostAddress.indexOf(':') < 0;
if (useIPv4) {
if (isIPv4)
return hostAddress;
} else {
if (!isIPv4) {
int index = hostAddress.indexOf('%');
return index < 0 ? hostAddress.toUpperCase() : hostAddress.substring(0, index).toUpperCase();
}
}
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return null;
}
use of java.net.NetworkInterface in project wifikeyboard by IvanVolosyuk.
the class WiFiKeyboard method getNetworkAddresses.
public static ArrayList<String> getNetworkAddresses() {
ArrayList<String> addrs = new ArrayList<String>();
try {
Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
while (ifaces.hasMoreElements()) {
NetworkInterface iface = ifaces.nextElement();
if ("lo".equals(iface.getName()))
continue;
Enumeration<InetAddress> addresses = iface.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress addr = addresses.nextElement();
addrs.add(addr.getHostAddress());
}
}
} catch (SocketException e) {
Debug.d("failed to get network interfaces");
}
return addrs;
}
use of java.net.NetworkInterface in project alluxio by Alluxio.
the class NetworkAddressUtils method getLocalIpAddress.
/**
* Gets a local IP address for the host this JVM is running on.
*
* @param timeoutMs Timeout in milliseconds to use for checking that a possible local IP is
* reachable
* @return the local ip address, which is not a loopback address and is reachable
*/
public static synchronized String getLocalIpAddress(int timeoutMs) {
if (sLocalIP != null) {
return sLocalIP;
}
try {
InetAddress address = InetAddress.getLocalHost();
LOG.debug("address: {} isLoopbackAddress: {}, with host {} {}", address, address.isLoopbackAddress(), address.getHostAddress(), address.getHostName());
// address e.g. a broadcast address
if (!isValidAddress(address, timeoutMs)) {
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
// possible.
if (!WINDOWS) {
List<NetworkInterface> netIFs = Collections.list(networkInterfaces);
Collections.reverse(netIFs);
networkInterfaces = Collections.enumeration(netIFs);
}
while (networkInterfaces.hasMoreElements()) {
NetworkInterface ni = networkInterfaces.nextElement();
Enumeration<InetAddress> addresses = ni.getInetAddresses();
while (addresses.hasMoreElements()) {
address = addresses.nextElement();
// Address must not be link local or loopback. And it must be reachable
if (isValidAddress(address, timeoutMs)) {
sLocalIP = address.getHostAddress();
return sLocalIP;
}
}
}
LOG.warn("Your hostname, " + InetAddress.getLocalHost().getHostName() + " resolves to" + " a loopback/non-reachable address: " + address.getHostAddress() + ", but we couldn't find any external IP address!");
}
sLocalIP = address.getHostAddress();
return sLocalIP;
} catch (IOException e) {
throw Throwables.propagate(e);
}
}
use of java.net.NetworkInterface in project nhin-d by DirectProject.
the class IPUtils method getDNSLocalIps.
public static String[] getDNSLocalIps() {
final List<String> ips = new ArrayList<String>();
ips.add("127.0.0.1");
try {
final Enumeration<NetworkInterface> netInts = NetworkInterface.getNetworkInterfaces();
while (netInts.hasMoreElements()) {
final NetworkInterface netInt = netInts.nextElement();
final Enumeration<InetAddress> addrs = netInt.getInetAddresses();
while (addrs.hasMoreElements()) {
final InetAddress addr = addrs.nextElement();
if (!(addr instanceof Inet6Address))
ips.add(addr.getHostAddress());
}
}
} catch (Exception e) {
}
return ips.toArray(new String[ips.size()]);
}
Aggregations