Search in sources :

Example 36 with SocketException

use of java.net.SocketException in project android_frameworks_base by ResurrectionRemix.

the class IpReachabilityMonitor method probeNeighbor.

/**
     * Make the kernel perform neighbor reachability detection (IPv4 ARP or IPv6 ND)
     * for the given IP address on the specified interface index.
     *
     * @return 0 if the request was successfully passed to the kernel; otherwise return
     *         a non-zero error code.
     */
private static int probeNeighbor(int ifIndex, InetAddress ip) {
    final String msgSnippet = "probing ip=" + ip.getHostAddress() + "%" + ifIndex;
    if (DBG) {
        Log.d(TAG, msgSnippet);
    }
    final byte[] msg = RtNetlinkNeighborMessage.newNewNeighborMessage(1, ip, StructNdMsg.NUD_PROBE, ifIndex, null);
    int errno = -OsConstants.EPROTO;
    try (NetlinkSocket nlSocket = new NetlinkSocket(OsConstants.NETLINK_ROUTE)) {
        final long IO_TIMEOUT = 300L;
        nlSocket.connectToKernel();
        nlSocket.sendMessage(msg, 0, msg.length, IO_TIMEOUT);
        final ByteBuffer bytes = nlSocket.recvMessage(IO_TIMEOUT);
        // recvMessage() guaranteed to not return null if it did not throw.
        final NetlinkMessage response = NetlinkMessage.parse(bytes);
        if (response != null && response instanceof NetlinkErrorMessage && (((NetlinkErrorMessage) response).getNlMsgError() != null)) {
            errno = ((NetlinkErrorMessage) response).getNlMsgError().error;
            if (errno != 0) {
                // TODO: consider ignoring EINVAL (-22), which appears to be
                // normal when probing a neighbor for which the kernel does
                // not already have / no longer has a link layer address.
                Log.e(TAG, "Error " + msgSnippet + ", errmsg=" + response.toString());
            }
        } else {
            String errmsg;
            if (response == null) {
                bytes.position(0);
                errmsg = "raw bytes: " + NetlinkConstants.hexify(bytes);
            } else {
                errmsg = response.toString();
            }
            Log.e(TAG, "Error " + msgSnippet + ", errmsg=" + errmsg);
        }
    } catch (ErrnoException e) {
        Log.e(TAG, "Error " + msgSnippet, e);
        errno = -e.errno;
    } catch (InterruptedIOException e) {
        Log.e(TAG, "Error " + msgSnippet, e);
        errno = -OsConstants.ETIMEDOUT;
    } catch (SocketException e) {
        Log.e(TAG, "Error " + msgSnippet, e);
        errno = -OsConstants.EIO;
    }
    return errno;
}
Also used : InterruptedIOException(java.io.InterruptedIOException) SocketException(java.net.SocketException) NetlinkSocket(android.net.netlink.NetlinkSocket) ErrnoException(android.system.ErrnoException) NetlinkMessage(android.net.netlink.NetlinkMessage) NetlinkErrorMessage(android.net.netlink.NetlinkErrorMessage) ByteBuffer(java.nio.ByteBuffer)

Example 37 with SocketException

use of java.net.SocketException in project android_frameworks_base by ResurrectionRemix.

the class ApfFilter method maybeStartFilter.

/**
     * Attempt to start listening for RAs and, if RAs are received, generating and installing
     * filters to ignore useless RAs.
     */
@VisibleForTesting
void maybeStartFilter() {
    FileDescriptor socket;
    try {
        mHardwareAddress = mNetworkInterface.getHardwareAddress();
        synchronized (this) {
            // Install basic filters
            installNewProgramLocked();
        }
        socket = Os.socket(AF_PACKET, SOCK_RAW, ETH_P_IPV6);
        PacketSocketAddress addr = new PacketSocketAddress((short) ETH_P_IPV6, mNetworkInterface.getIndex());
        Os.bind(socket, addr);
        NetworkUtils.attachRaFilter(socket, mApfCapabilities.apfPacketFormat);
    } catch (SocketException | ErrnoException e) {
        Log.e(TAG, "Error starting filter", e);
        return;
    }
    mReceiveThread = new ReceiveThread(socket);
    mReceiveThread.start();
}
Also used : SocketException(java.net.SocketException) PacketSocketAddress(android.system.PacketSocketAddress) ErrnoException(android.system.ErrnoException) FileDescriptor(java.io.FileDescriptor) VisibleForTesting(com.android.internal.annotations.VisibleForTesting)

Example 38 with SocketException

use of java.net.SocketException in project android_frameworks_base by ResurrectionRemix.

the class DhcpClient method initInterface.

private boolean initInterface() {
    try {
        mIface = NetworkInterface.getByName(mIfaceName);
        mHwAddr = mIface.getHardwareAddress();
        mInterfaceBroadcastAddr = new PacketSocketAddress(mIface.getIndex(), DhcpPacket.ETHER_BROADCAST);
        return true;
    } catch (SocketException | NullPointerException e) {
        Log.e(TAG, "Can't determine ifindex or MAC address for " + mIfaceName, e);
        return false;
    }
}
Also used : SocketException(java.net.SocketException) PacketSocketAddress(android.system.PacketSocketAddress)

Example 39 with SocketException

use of java.net.SocketException in project android_frameworks_base by ResurrectionRemix.

the class NetworkTest method testBindSocketOfInvalidFdThrows.

@SmallTest
public void testBindSocketOfInvalidFdThrows() throws Exception {
    final FileDescriptor fd = new FileDescriptor();
    assertFalse(fd.valid());
    try {
        mNetwork.bindSocket(fd);
        fail("SocketException not thrown");
    } catch (SocketException expected) {
    }
}
Also used : SocketException(java.net.SocketException) FileDescriptor(java.io.FileDescriptor) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 40 with SocketException

use of java.net.SocketException in project android_frameworks_base by ResurrectionRemix.

the class NetworkTest method testBindSocketOfNonSocketFdThrows.

@SmallTest
public void testBindSocketOfNonSocketFdThrows() throws Exception {
    final File devNull = new File("/dev/null");
    assertTrue(devNull.canRead());
    final FileInputStream fis = new FileInputStream(devNull);
    assertTrue(null != fis.getFD());
    assertTrue(fis.getFD().valid());
    try {
        mNetwork.bindSocket(fis.getFD());
        fail("SocketException not thrown");
    } catch (SocketException expected) {
    }
}
Also used : SocketException(java.net.SocketException) File(java.io.File) FileInputStream(java.io.FileInputStream) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Aggregations

SocketException (java.net.SocketException)925 IOException (java.io.IOException)349 InetAddress (java.net.InetAddress)254 NetworkInterface (java.net.NetworkInterface)240 UnknownHostException (java.net.UnknownHostException)153 Socket (java.net.Socket)146 SocketTimeoutException (java.net.SocketTimeoutException)118 ServerSocket (java.net.ServerSocket)114 DatagramSocket (java.net.DatagramSocket)95 Test (org.junit.Test)87 InetSocketAddress (java.net.InetSocketAddress)84 ArrayList (java.util.ArrayList)70 Inet4Address (java.net.Inet4Address)51 DatagramPacket (java.net.DatagramPacket)49 ConnectException (java.net.ConnectException)48 InputStream (java.io.InputStream)41 InterruptedIOException (java.io.InterruptedIOException)41 BindException (java.net.BindException)36 EOFException (java.io.EOFException)33 OutputStream (java.io.OutputStream)32