Search in sources :

Example 36 with InetAddress

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";
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) InetAddress(java.net.InetAddress)

Example 37 with InetAddress

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();
}
Also used : InetAddress(java.net.InetAddress)

Example 38 with InetAddress

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);
}
Also used : Inet6Address(java.net.Inet6Address) ProvisioningChange(android.net.LinkProperties.ProvisioningChange) RouteInfo(android.net.RouteInfo) InetAddress(java.net.InetAddress) LinkProperties(android.net.LinkProperties) HashMap(java.util.HashMap) Map(java.util.Map)

Example 39 with InetAddress

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;
}
Also used : NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Example 40 with InetAddress

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;
}
Also used : NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Aggregations

InetAddress (java.net.InetAddress)2214 UnknownHostException (java.net.UnknownHostException)422 IOException (java.io.IOException)294 Test (org.junit.Test)289 InetSocketAddress (java.net.InetSocketAddress)250 NetworkInterface (java.net.NetworkInterface)191 ArrayList (java.util.ArrayList)173 SocketException (java.net.SocketException)153 HashMap (java.util.HashMap)104 Inet6Address (java.net.Inet6Address)103 Inet4Address (java.net.Inet4Address)96 Socket (java.net.Socket)78 DatagramPacket (java.net.DatagramPacket)73 LinkAddress (android.net.LinkAddress)70 Token (org.apache.cassandra.dht.Token)67 DatagramSocket (java.net.DatagramSocket)65 Map (java.util.Map)59 RouteInfo (android.net.RouteInfo)56 LinkProperties (android.net.LinkProperties)52 ServerSocket (java.net.ServerSocket)52