Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 902842d3 authored by Chiachang Wang's avatar Chiachang Wang
Browse files

Parse extra un-used TcpInfo

Parse tcp info only until tcpi_sndbuf_limited now. If device
kernel version >= 4.18, there will be some un-parsed bytes left
because the tcp_info struct was expanded. Skip the remaining
un-parsed part.

Bug: 145275899
Bug: 145328512
Test: atest NetworkStackTests NetworkStackNextTests
Change-Id: I56aa6be183b8b804898926dae2c47a497c49f893
parent 4a8d1d42
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -112,6 +112,10 @@ public class TcpInfo {
            }
        }
        mFieldsValues = Collections.unmodifiableMap(fields);
        // tcp_info structure grows over time as new fields are added. Jump to the end of the
        // structure, as unknown fields might remain at the end of the structure if the tcp_info
        // struct was expanded.
        bytes.position(Math.min(infolen + start, bytes.limit()));
    }

    @VisibleForTesting
+24 −0
Original line number Diff line number Diff line
@@ -90,6 +90,13 @@ public class TcpInfoTest {
    private static final byte[] TCP_INFO_BYTES =
            HexEncoding.decode(TCP_INFO_HEX.toCharArray(), false);

    private static final String EXPANDED_TCP_INFO_HEX = TCP_INFO_HEX
            + "00000000"         // tcpi_delivered
            + "00000000";        // tcpi_delivered_ce
    private static final byte[] EXPANDED_TCP_INFO_BYTES =
            HexEncoding.decode(EXPANDED_TCP_INFO_HEX.toCharArray(), false);
    private static final int EXPANDED_TCP_INFO_LENGTH =
            EXPANDED_TCP_INFO_BYTES.length - TCP_INFO_BYTES.length;
    @Test
    public void testParseTcpInfo() {
        final ByteBuffer buffer = ByteBuffer.wrap(TCP_INFO_BYTES);
@@ -99,6 +106,23 @@ public class TcpInfoTest {
        assertEquals(parsedInfo, new TcpInfo(expected));
    }

    @Test
    public void testParseTcpInfoExpanded() {
        final ByteBuffer buffer = ByteBuffer.wrap(EXPANDED_TCP_INFO_BYTES);
        final Map<TcpInfo.Field, Number> expected = makeTestTcpInfoHash();
        final TcpInfo parsedInfo =
                TcpInfo.parse(buffer, TCP_INFO_LENGTH_V1 + EXPANDED_TCP_INFO_LENGTH);

        assertEquals(parsedInfo, new TcpInfo(expected));
        assertEquals(buffer.limit(), buffer.position());

        // reset the index.
        buffer.position(0);
        final TcpInfo parsedInfoShorterLen = TcpInfo.parse(buffer, TCP_INFO_LENGTH_V1);
        assertEquals(parsedInfoShorterLen, new TcpInfo(expected));
        assertEquals(TCP_INFO_LENGTH_V1, buffer.position());
    }

    @Test
    public void testValidOffset() {
        final ByteBuffer buffer = ByteBuffer.wrap(TCP_INFO_BYTES);