Search in sources :

Example 1 with NetlinkSocket

use of android.net.netlink.NetlinkSocket in project platform_frameworks_base by android.

the class NetlinkSocketTest method testBasicWorkingGetNeighborsQuery.

@SmallTest
public void testBasicWorkingGetNeighborsQuery() throws Exception {
    NetlinkSocket s = new NetlinkSocket(OsConstants.NETLINK_ROUTE);
    assertNotNull(s);
    s.connectToKernel();
    NetlinkSocketAddress localAddr = s.getLocalAddress();
    assertNotNull(localAddr);
    assertEquals(0, localAddr.getGroupsMask());
    assertTrue(0 != localAddr.getPortId());
    final int TEST_SEQNO = 5;
    final byte[] request = RtNetlinkNeighborMessage.newGetNeighborsRequest(TEST_SEQNO);
    assertNotNull(request);
    final long TIMEOUT = 500;
    assertTrue(s.sendMessage(request, 0, request.length, TIMEOUT));
    int neighMessageCount = 0;
    int doneMessageCount = 0;
    while (doneMessageCount == 0) {
        ByteBuffer response = null;
        response = s.recvMessage(TIMEOUT);
        assertNotNull(response);
        assertTrue(StructNlMsgHdr.STRUCT_SIZE <= response.limit());
        assertEquals(0, response.position());
        assertEquals(ByteOrder.nativeOrder(), response.order());
        // Verify the messages at least appears minimally reasonable.
        while (response.remaining() > 0) {
            final NetlinkMessage msg = NetlinkMessage.parse(response);
            assertNotNull(msg);
            final StructNlMsgHdr hdr = msg.getHeader();
            assertNotNull(hdr);
            if (hdr.nlmsg_type == NetlinkConstants.NLMSG_DONE) {
                doneMessageCount++;
                continue;
            }
            assertEquals(NetlinkConstants.RTM_NEWNEIGH, hdr.nlmsg_type);
            assertTrue(msg instanceof RtNetlinkNeighborMessage);
            assertTrue((hdr.nlmsg_flags & StructNlMsgHdr.NLM_F_MULTI) != 0);
            assertEquals(TEST_SEQNO, hdr.nlmsg_seq);
            assertEquals(localAddr.getPortId(), hdr.nlmsg_pid);
            neighMessageCount++;
        }
    }
    assertEquals(1, doneMessageCount);
    // TODO: make sure this test passes sanely in airplane mode.
    assertTrue(neighMessageCount > 0);
    s.close();
}
Also used : StructNlMsgHdr(android.net.netlink.StructNlMsgHdr) NetlinkSocketAddress(android.system.NetlinkSocketAddress) NetlinkSocket(android.net.netlink.NetlinkSocket) ByteBuffer(java.nio.ByteBuffer) RtNetlinkNeighborMessage(android.net.netlink.RtNetlinkNeighborMessage) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 2 with NetlinkSocket

use of android.net.netlink.NetlinkSocket in project android_frameworks_base by DirtyUnicorns.

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 3 with NetlinkSocket

use of android.net.netlink.NetlinkSocket in project android_frameworks_base by DirtyUnicorns.

the class NetlinkSocketTest method testBasicWorkingGetNeighborsQuery.

public void testBasicWorkingGetNeighborsQuery() throws Exception {
    NetlinkSocket s = new NetlinkSocket(OsConstants.NETLINK_ROUTE);
    assertNotNull(s);
    s.connectToKernel();
    NetlinkSocketAddress localAddr = s.getLocalAddress();
    assertNotNull(localAddr);
    assertEquals(0, localAddr.getGroupsMask());
    assertTrue(0 != localAddr.getPortId());
    final int TEST_SEQNO = 5;
    final byte[] request = RtNetlinkNeighborMessage.newGetNeighborsRequest(TEST_SEQNO);
    assertNotNull(request);
    final long TIMEOUT = 500;
    assertTrue(s.sendMessage(request, 0, request.length, TIMEOUT));
    int neighMessageCount = 0;
    int doneMessageCount = 0;
    while (doneMessageCount == 0) {
        ByteBuffer response = null;
        response = s.recvMessage(TIMEOUT);
        assertNotNull(response);
        assertTrue(StructNlMsgHdr.STRUCT_SIZE <= response.limit());
        assertEquals(0, response.position());
        assertEquals(ByteOrder.nativeOrder(), response.order());
        // Verify the messages at least appears minimally reasonable.
        while (response.remaining() > 0) {
            final NetlinkMessage msg = NetlinkMessage.parse(response);
            assertNotNull(msg);
            final StructNlMsgHdr hdr = msg.getHeader();
            assertNotNull(hdr);
            if (hdr.nlmsg_type == NetlinkConstants.NLMSG_DONE) {
                doneMessageCount++;
                continue;
            }
            assertEquals(NetlinkConstants.RTM_NEWNEIGH, hdr.nlmsg_type);
            assertTrue(msg instanceof RtNetlinkNeighborMessage);
            assertTrue((hdr.nlmsg_flags & StructNlMsgHdr.NLM_F_MULTI) != 0);
            assertEquals(TEST_SEQNO, hdr.nlmsg_seq);
            assertEquals(localAddr.getPortId(), hdr.nlmsg_pid);
            neighMessageCount++;
        }
    }
    assertEquals(1, doneMessageCount);
    // TODO: make sure this test passes sanely in airplane mode.
    assertTrue(neighMessageCount > 0);
    s.close();
}
Also used : StructNlMsgHdr(android.net.netlink.StructNlMsgHdr) NetlinkSocketAddress(android.system.NetlinkSocketAddress) NetlinkSocket(android.net.netlink.NetlinkSocket) ByteBuffer(java.nio.ByteBuffer) RtNetlinkNeighborMessage(android.net.netlink.RtNetlinkNeighborMessage)

Example 4 with NetlinkSocket

use of android.net.netlink.NetlinkSocket 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 5 with NetlinkSocket

use of android.net.netlink.NetlinkSocket in project android_frameworks_base by crdroidandroid.

the class NetlinkSocketTest method testBasicWorkingGetNeighborsQuery.

public void testBasicWorkingGetNeighborsQuery() throws Exception {
    NetlinkSocket s = new NetlinkSocket(OsConstants.NETLINK_ROUTE);
    assertNotNull(s);
    s.connectToKernel();
    NetlinkSocketAddress localAddr = s.getLocalAddress();
    assertNotNull(localAddr);
    assertEquals(0, localAddr.getGroupsMask());
    assertTrue(0 != localAddr.getPortId());
    final int TEST_SEQNO = 5;
    final byte[] request = RtNetlinkNeighborMessage.newGetNeighborsRequest(TEST_SEQNO);
    assertNotNull(request);
    final long TIMEOUT = 500;
    assertTrue(s.sendMessage(request, 0, request.length, TIMEOUT));
    int neighMessageCount = 0;
    int doneMessageCount = 0;
    while (doneMessageCount == 0) {
        ByteBuffer response = null;
        response = s.recvMessage(TIMEOUT);
        assertNotNull(response);
        assertTrue(StructNlMsgHdr.STRUCT_SIZE <= response.limit());
        assertEquals(0, response.position());
        assertEquals(ByteOrder.nativeOrder(), response.order());
        // Verify the messages at least appears minimally reasonable.
        while (response.remaining() > 0) {
            final NetlinkMessage msg = NetlinkMessage.parse(response);
            assertNotNull(msg);
            final StructNlMsgHdr hdr = msg.getHeader();
            assertNotNull(hdr);
            if (hdr.nlmsg_type == NetlinkConstants.NLMSG_DONE) {
                doneMessageCount++;
                continue;
            }
            assertEquals(NetlinkConstants.RTM_NEWNEIGH, hdr.nlmsg_type);
            assertTrue(msg instanceof RtNetlinkNeighborMessage);
            assertTrue((hdr.nlmsg_flags & StructNlMsgHdr.NLM_F_MULTI) != 0);
            assertEquals(TEST_SEQNO, hdr.nlmsg_seq);
            assertEquals(localAddr.getPortId(), hdr.nlmsg_pid);
            neighMessageCount++;
        }
    }
    assertEquals(1, doneMessageCount);
    // TODO: make sure this test passes sanely in airplane mode.
    assertTrue(neighMessageCount > 0);
    s.close();
}
Also used : StructNlMsgHdr(android.net.netlink.StructNlMsgHdr) NetlinkSocketAddress(android.system.NetlinkSocketAddress) NetlinkSocket(android.net.netlink.NetlinkSocket) ByteBuffer(java.nio.ByteBuffer) RtNetlinkNeighborMessage(android.net.netlink.RtNetlinkNeighborMessage)

Aggregations

NetlinkSocket (android.net.netlink.NetlinkSocket)12 ErrnoException (android.system.ErrnoException)8 NetlinkSocketAddress (android.system.NetlinkSocketAddress)8 ByteBuffer (java.nio.ByteBuffer)8 NetlinkErrorMessage (android.net.netlink.NetlinkErrorMessage)4 NetlinkMessage (android.net.netlink.NetlinkMessage)4 RtNetlinkNeighborMessage (android.net.netlink.RtNetlinkNeighborMessage)4 StructNlMsgHdr (android.net.netlink.StructNlMsgHdr)4 InterruptedIOException (java.io.InterruptedIOException)4 SocketException (java.net.SocketException)4 SmallTest (android.test.suitebuilder.annotation.SmallTest)2