libpnet / libpnet/libpnet

PacketFoobar::packet_size(_packet: &Foobar) is broken

Open
#519 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
2.6k
Forks
329
PR merge metrics
No merged PRs in 30d

Description

packet_size is supposed to return "The size (in bytes) of an instance when converted into a byte-array".

However, this doesn't work correctly when complex variable length fields are present.

Consider the following test:

#[test]
fn test_tcp_packet_size() {
    let tcp = Tcp{
        source: 36328,
        destination: 443,
        sequence: 2012456774,
        acknowledgement: 825842053,
        data_offset: 8,
        reserved: 0,
        flags: 24,
        window: 32,
        checksum: 39435,
        urgent_ptr: 0,
        options: vec![
            TcpOption::nop(),
            TcpOption::nop(),
            TcpOption::timestamp(0xdeadbeef, 0xdeadbeef),
        ],
        payload: vec![],
    };

    // fixed fields + nop + nop + timestamp
    let expected_length = 20 + 1 + 1 + 10;

    assert_eq!((tcp.data_offset as usize) * 4 + tcp.payload.len(), expected_length);
    assert_eq!(MutableTcpPacket::packet_size(&tcp), expected_length);
}

The test will fail:

thread 'tcp::test_tcp_packet_size' panicked at 'assertion failed: `(left == right)`
  left: `23`,
 right: `32`', src/tcp.rs:464:5

I took a look at how these macros were expanded:

/// The size (in bytes) of a Tcp instance when converted into
/// a byte-array
#[inline]
pub fn packet_size(_packet: &Tcp) -> usize {
    20 + _packet.options.len() + _packet.payload.len()
}

Instead of calculating actual size in bytes, it just returns a number of TCP options, which is wrong.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the failing test and generated packet_size function shown in the issue, using src/tcp.rs:464 as the failure location. Trace how the packet macros calculate sizes for variable-length TCP options, then run the TCP packet-size test and confirm that the timestamp option contributes its byte length rather than only the option count.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
networking
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.