use of java.net.NetworkInterface in project MSEC by Tencent.
the class ServiceFactory method parseListenConf.
public static void parseListenConf() throws Exception {
String listenConf = getConfig("SRPC", "listen");
if (listenConf != null) {
int pos = listenConf.indexOf(' ');
if (pos >= 0)
listenConf = listenConf.substring(0, pos);
pos = listenConf.indexOf(':');
if (pos > 0) {
listenIP = listenConf.substring(0, pos);
listenConf = listenConf.substring(pos + 1);
}
pos = listenConf.indexOf('/');
String listenPortStr = listenConf;
if (pos > 0) {
listenPortStr = listenConf.substring(0, pos);
listenType = listenConf.substring(pos + 1);
if (listenType.compareToIgnoreCase("tcp") != 0 && listenType.compareToIgnoreCase("udp") != 0) {
throw new Exception("Invalid listen type: " + listenType);
}
}
try {
listenPort = Integer.valueOf(listenPortStr);
} catch (NumberFormatException ex) {
throw new Exception("Invalid listen port: " + listenPortStr);
}
if (listenIP != null && !listenIP.isEmpty()) {
Enumeration interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface intf = (NetworkInterface) interfaces.nextElement();
if (intf.getName().compareToIgnoreCase(listenIP) != 0)
continue;
// Enumerate InetAddresses of this network interface
Enumeration addresses = intf.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress address = (InetAddress) addresses.nextElement();
listenIP = address.getHostAddress();
break;
}
}
}
}
}
use of java.net.NetworkInterface in project XPrivacy by M66B.
the class XNetworkInterface method after.
@Override
protected void after(XParam param) throws Throwable {
if (getRestrictionName().equals(PrivacyManager.cInternet)) {
// Internet: fake offline state
if (mMethod == Methods.getByIndex || mMethod == Methods.getByInetAddress || mMethod == Methods.getByName || mMethod == Methods.getNetworkInterfaces) {
if (param.getResult() != null && isRestricted(param))
param.setResult(null);
} else
Util.log(this, Log.WARN, "Unknown method=" + param.method.getName());
} else if (getRestrictionName().equals(PrivacyManager.cNetwork)) {
// Network
NetworkInterface ni = (NetworkInterface) param.thisObject;
if (ni != null)
if (param.getResult() != null && isRestricted(param))
if (mMethod == Methods.getHardwareAddress) {
String mac = (String) PrivacyManager.getDefacedProp(Binder.getCallingUid(), "MAC");
long lMac = Long.parseLong(mac.replace(":", ""), 16);
byte[] address = ByteBuffer.allocate(8).putLong(lMac).array();
param.setResult(address);
} else if (mMethod == Methods.getInetAddresses) {
@SuppressWarnings("unchecked") Enumeration<InetAddress> addresses = (Enumeration<InetAddress>) param.getResult();
List<InetAddress> listAddress = new ArrayList<InetAddress>();
for (InetAddress address : Collections.list(addresses)) if (address.isAnyLocalAddress() || address.isLoopbackAddress())
listAddress.add(address);
else
listAddress.add((InetAddress) PrivacyManager.getDefacedProp(Binder.getCallingUid(), "InetAddress"));
param.setResult(Collections.enumeration(listAddress));
} else if (mMethod == Methods.getInterfaceAddresses) {
@SuppressWarnings("unchecked") List<InterfaceAddress> listAddress = (List<InterfaceAddress>) param.getResult();
for (InterfaceAddress address : listAddress) {
// address
try {
Field fieldAddress = InterfaceAddress.class.getDeclaredField("address");
fieldAddress.setAccessible(true);
fieldAddress.set(address, PrivacyManager.getDefacedProp(Binder.getCallingUid(), "InetAddress"));
} catch (Throwable ex) {
Util.bug(this, ex);
}
// broadcastAddress
try {
Field fieldBroadcastAddress = InterfaceAddress.class.getDeclaredField("broadcastAddress");
fieldBroadcastAddress.setAccessible(true);
fieldBroadcastAddress.set(address, PrivacyManager.getDefacedProp(Binder.getCallingUid(), "InetAddress"));
} catch (Throwable ex) {
Util.bug(this, ex);
}
}
} else
Util.log(this, Log.WARN, "Unknown method=" + param.method.getName());
}
}
use of java.net.NetworkInterface in project eureka by Netflix.
the class SystemUtil method getServerIPv4.
/**
* Returns server IP address (v4 or v6) bound to local NIC. If multiple NICs are present, choose 'eth0' or 'en0' or
* any one with name ending with 0. If non found, take first on the list that is not localhost. If no NIC
* present (relevant only for desktop deployments), return loopback address.
*/
public static String getServerIPv4() {
String candidateAddress = null;
try {
Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();
while (nics.hasMoreElements()) {
NetworkInterface nic = nics.nextElement();
Enumeration<InetAddress> inetAddresses = nic.getInetAddresses();
while (inetAddresses.hasMoreElements()) {
String address = inetAddresses.nextElement().getHostAddress();
String nicName = nic.getName();
if (nicName.startsWith("eth0") || nicName.startsWith("en0")) {
return address;
}
if (nicName.endsWith("0") || candidateAddress == null) {
candidateAddress = address;
}
}
}
} catch (SocketException e) {
throw new RuntimeException("Cannot resolve local network address", e);
}
return candidateAddress == null ? "127.0.0.1" : candidateAddress;
}
use of java.net.NetworkInterface in project canal by alibaba.
the class AddressUtils method getHostAddress.
public static InetAddress getHostAddress() {
InetAddress localAddress = null;
try {
localAddress = InetAddress.getLocalHost();
if (isValidHostAddress(localAddress)) {
return localAddress;
}
} catch (Throwable e) {
logger.warn("Failed to retriving local host ip address, try scan network card ip address. cause: " + e.getMessage());
}
try {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
if (interfaces != null) {
while (interfaces.hasMoreElements()) {
try {
NetworkInterface network = interfaces.nextElement();
Enumeration<InetAddress> addresses = network.getInetAddresses();
if (addresses != null) {
while (addresses.hasMoreElements()) {
try {
InetAddress address = addresses.nextElement();
if (isValidHostAddress(address)) {
return address;
}
} catch (Throwable e) {
logger.warn("Failed to retriving network card ip address. cause:" + e.getMessage());
}
}
}
} catch (Throwable e) {
logger.warn("Failed to retriving network card ip address. cause:" + e.getMessage());
}
}
}
} catch (Throwable e) {
logger.warn("Failed to retriving network card ip address. cause:" + e.getMessage());
}
logger.error("Could not get local host ip address, will use 127.0.0.1 instead.");
return localAddress;
}
use of java.net.NetworkInterface in project druid by alibaba.
the class MonitorClient method getLocalIPAddress.
public static InetAddress getLocalIPAddress() {
try {
Enumeration<?> netInterfaces = NetworkInterface.getNetworkInterfaces();
InetAddress inetAddress = null;
while (netInterfaces.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();
Enumeration<?> e2 = ni.getInetAddresses();
while (e2.hasMoreElements()) {
inetAddress = (InetAddress) e2.nextElement();
if (!inetAddress.isLoopbackAddress() && !inetAddress.getHostAddress().contains(":")) {
return inetAddress;
}
}
}
} catch (Exception e) {
LOG.error("getLocalIP error", e);
}
return null;
}
Aggregations