Search in sources :

Example 6 with NetlinkMessage

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

the class RtNetlinkNeighborMessageTest method testParseRtmDelNeigh.

@SmallTest
public void testParseRtmDelNeigh() {
    final ByteBuffer byteBuffer = ByteBuffer.wrap(RTM_DELNEIGH);
    // For testing.
    byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
    final NetlinkMessage msg = NetlinkMessage.parse(byteBuffer);
    assertNotNull(msg);
    assertTrue(msg instanceof RtNetlinkNeighborMessage);
    final RtNetlinkNeighborMessage neighMsg = (RtNetlinkNeighborMessage) msg;
    final StructNlMsgHdr hdr = neighMsg.getHeader();
    assertNotNull(hdr);
    assertEquals(76, hdr.nlmsg_len);
    assertEquals(NetlinkConstants.RTM_DELNEIGH, hdr.nlmsg_type);
    assertEquals(0, hdr.nlmsg_flags);
    assertEquals(0, hdr.nlmsg_seq);
    assertEquals(0, hdr.nlmsg_pid);
    final StructNdMsg ndmsgHdr = neighMsg.getNdHeader();
    assertNotNull(ndmsgHdr);
    assertEquals((byte) OsConstants.AF_INET, ndmsgHdr.ndm_family);
    assertEquals(21, ndmsgHdr.ndm_ifindex);
    assertEquals(StructNdMsg.NUD_STALE, ndmsgHdr.ndm_state);
    final InetAddress destination = neighMsg.getDestination();
    assertNotNull(destination);
    assertEquals(InetAddress.parseNumericAddress("192.168.159.254"), destination);
}
Also used : StructNlMsgHdr(android.net.netlink.StructNlMsgHdr) StructNdMsg(android.net.netlink.StructNdMsg) NetlinkMessage(android.net.netlink.NetlinkMessage) ByteBuffer(java.nio.ByteBuffer) InetAddress(java.net.InetAddress) RtNetlinkNeighborMessage(android.net.netlink.RtNetlinkNeighborMessage) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 7 with NetlinkMessage

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

the class RtNetlinkNeighborMessageTest method testParseRtmNewNeigh.

@SmallTest
public void testParseRtmNewNeigh() {
    final ByteBuffer byteBuffer = ByteBuffer.wrap(RTM_NEWNEIGH);
    // For testing.
    byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
    final NetlinkMessage msg = NetlinkMessage.parse(byteBuffer);
    assertNotNull(msg);
    assertTrue(msg instanceof RtNetlinkNeighborMessage);
    final RtNetlinkNeighborMessage neighMsg = (RtNetlinkNeighborMessage) msg;
    final StructNlMsgHdr hdr = neighMsg.getHeader();
    assertNotNull(hdr);
    assertEquals(88, hdr.nlmsg_len);
    assertEquals(NetlinkConstants.RTM_NEWNEIGH, hdr.nlmsg_type);
    assertEquals(0, hdr.nlmsg_flags);
    assertEquals(0, hdr.nlmsg_seq);
    assertEquals(0, hdr.nlmsg_pid);
    final StructNdMsg ndmsgHdr = neighMsg.getNdHeader();
    assertNotNull(ndmsgHdr);
    assertEquals((byte) OsConstants.AF_INET6, ndmsgHdr.ndm_family);
    assertEquals(21, ndmsgHdr.ndm_ifindex);
    assertEquals(StructNdMsg.NUD_STALE, ndmsgHdr.ndm_state);
    final InetAddress destination = neighMsg.getDestination();
    assertNotNull(destination);
    assertEquals(InetAddress.parseNumericAddress("fe80::86c9:b2ff:fe6a:ed4b"), destination);
}
Also used : StructNlMsgHdr(android.net.netlink.StructNlMsgHdr) StructNdMsg(android.net.netlink.StructNdMsg) NetlinkMessage(android.net.netlink.NetlinkMessage) ByteBuffer(java.nio.ByteBuffer) InetAddress(java.net.InetAddress) RtNetlinkNeighborMessage(android.net.netlink.RtNetlinkNeighborMessage) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 8 with NetlinkMessage

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

the class NetlinkErrorMessageTest method testParseNlmErrorOk.

public void testParseNlmErrorOk() {
    final ByteBuffer byteBuffer = ByteBuffer.wrap(NLM_ERROR_OK);
    // For testing.
    byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
    final NetlinkMessage msg = NetlinkMessage.parse(byteBuffer);
    assertNotNull(msg);
    assertTrue(msg instanceof NetlinkErrorMessage);
    final NetlinkErrorMessage errorMsg = (NetlinkErrorMessage) msg;
    final StructNlMsgHdr hdr = errorMsg.getHeader();
    assertNotNull(hdr);
    assertEquals(36, hdr.nlmsg_len);
    assertEquals(NetlinkConstants.NLMSG_ERROR, hdr.nlmsg_type);
    assertEquals(0, hdr.nlmsg_flags);
    assertEquals(13606, hdr.nlmsg_seq);
    assertEquals(4196, hdr.nlmsg_pid);
    final StructNlMsgErr err = errorMsg.getNlMsgError();
    assertNotNull(err);
    assertEquals(0, err.error);
    assertNotNull(err.msg);
    assertEquals(48, err.msg.nlmsg_len);
    assertEquals(NetlinkConstants.RTM_NEWNEIGH, err.msg.nlmsg_type);
    assertEquals((NLM_F_REQUEST | NLM_F_ACK | NLM_F_REPLACE), err.msg.nlmsg_flags);
    assertEquals(13606, err.msg.nlmsg_seq);
    assertEquals(0, err.msg.nlmsg_pid);
}
Also used : StructNlMsgErr(android.net.netlink.StructNlMsgErr) NetlinkMessage(android.net.netlink.NetlinkMessage) NetlinkErrorMessage(android.net.netlink.NetlinkErrorMessage) ByteBuffer(java.nio.ByteBuffer)

Example 9 with NetlinkMessage

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

the class RtNetlinkNeighborMessageTest method testParseRtmGetNeighResponse.

public void testParseRtmGetNeighResponse() {
    final ByteBuffer byteBuffer = ByteBuffer.wrap(RTM_GETNEIGH_RESPONSE);
    // For testing.
    byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
    int messageCount = 0;
    while (byteBuffer.remaining() > 0) {
        final NetlinkMessage msg = NetlinkMessage.parse(byteBuffer);
        assertNotNull(msg);
        assertTrue(msg instanceof RtNetlinkNeighborMessage);
        final RtNetlinkNeighborMessage neighMsg = (RtNetlinkNeighborMessage) msg;
        final StructNlMsgHdr hdr = neighMsg.getHeader();
        assertNotNull(hdr);
        assertEquals(NetlinkConstants.RTM_NEWNEIGH, hdr.nlmsg_type);
        assertEquals(StructNlMsgHdr.NLM_F_MULTI, hdr.nlmsg_flags);
        assertEquals(0, hdr.nlmsg_seq);
        assertEquals(11070, hdr.nlmsg_pid);
        messageCount++;
    }
    // TODO: add more detailed spot checks.
    assertEquals(14, messageCount);
}
Also used : StructNlMsgHdr(android.net.netlink.StructNlMsgHdr) NetlinkMessage(android.net.netlink.NetlinkMessage) ByteBuffer(java.nio.ByteBuffer) RtNetlinkNeighborMessage(android.net.netlink.RtNetlinkNeighborMessage)

Example 10 with NetlinkMessage

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

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)

Aggregations

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