use of java.net.InetAddress in project platform_frameworks_base by android.
the class ConnectivityPacketSummary method getIpAddressString.
private static String getIpAddressString(ByteBuffer ip, int byteLength) {
if (ip == null || ip.remaining() < byteLength)
return "invalid";
byte[] bytes = new byte[byteLength];
ip.get(bytes, 0, byteLength);
try {
InetAddress addr = InetAddress.getByAddress(bytes);
return addr.getHostAddress();
} catch (UnknownHostException uhe) {
return "unknown";
}
}
use of java.net.InetAddress in project platform_frameworks_base by android.
the class IpReachabilityMonitor method probeAll.
public void probeAll() {
final List<InetAddress> ipProbeList;
synchronized (mLock) {
ipProbeList = new ArrayList<>(mIpWatchList.keySet());
}
if (!ipProbeList.isEmpty() && mRunning) {
// Keep the CPU awake long enough to allow all ARP/ND
// probes a reasonable chance at success. See b/23197666.
//
// The wakelock we use is (by default) refcounted, and this version
// of acquire(timeout) queues a release message to keep acquisitions
// and releases balanced.
mWakeLock.acquire(getProbeWakeLockDuration());
}
for (InetAddress target : ipProbeList) {
if (!mRunning) {
break;
}
final int returnValue = probeNeighbor(mInterfaceIndex, target);
logEvent(IpReachabilityEvent.PROBE, returnValue);
}
mLastProbeTimeMs = SystemClock.elapsedRealtime();
}
use of java.net.InetAddress in project platform_frameworks_base by android.
the class IpReachabilityMonitor method handleNeighborLost.
private void handleNeighborLost(String msg) {
InetAddress ip = null;
final ProvisioningChange delta;
synchronized (mLock) {
LinkProperties whatIfLp = new LinkProperties(mLinkProperties);
for (Map.Entry<InetAddress, Short> entry : mIpWatchList.entrySet()) {
if (entry.getValue() != StructNdMsg.NUD_FAILED) {
continue;
}
ip = entry.getKey();
for (RouteInfo route : mLinkProperties.getRoutes()) {
if (ip.equals(route.getGateway())) {
whatIfLp.removeRoute(route);
}
}
if (avoidingBadLinks() || !(ip instanceof Inet6Address)) {
// We should do this unconditionally, but alas we cannot: b/31827713.
whatIfLp.removeDnsServer(ip);
}
}
delta = LinkProperties.compareProvisioning(mLinkProperties, whatIfLp);
}
if (delta == ProvisioningChange.LOST_PROVISIONING) {
final String logMsg = "FAILURE: LOST_PROVISIONING, " + msg;
Log.w(TAG, logMsg);
if (mCallback != null) {
// TODO: remove |ip| when the callback signature no longer has
// an InetAddress argument.
mCallback.notifyLost(ip, logMsg);
}
}
logNudFailed(delta);
}
use of java.net.InetAddress in project otter 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.InetAddress in project otter by alibaba.
the class AddressUtils method isHostIp.
/**
* 判断该ip是否为本机ip,一台机器可能同时有多个IP
*
* @param ip
* @return
*/
public static boolean isHostIp(String ip) {
InetAddress localAddress = null;
try {
localAddress = InetAddress.getLocalHost();
if (isValidHostAddress(localAddress) && (localAddress.getHostAddress().equals(ip) || localAddress.getHostName().equals(ip))) {
return true;
}
} 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) && address.getHostAddress().equals(ip)) {
return true;
}
} 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());
}
return false;
}
Aggregations