Skip to content

refactor(dpi/snmp): collapse find_pdu_type match, drop unreachable Unknown variant#279

Merged
domcyrus merged 1 commit into
domcyrus:mainfrom
0xghost42:refactor/snmp-pdu-match
May 18, 2026
Merged

refactor(dpi/snmp): collapse find_pdu_type match, drop unreachable Unknown variant#279
domcyrus merged 1 commit into
domcyrus:mainfrom
0xghost42:refactor/snmp-pdu-match

Conversation

@0xghost42
Copy link
Copy Markdown
Contributor

What

find_pdu_type gates its PDU-tag lookup with an outer range check, then dispatches inside with a match that includes an Unknown(other) catch-all:

for &byte in data.iter().take(50) {
    if (PDU_GET_REQUEST..=PDU_REPORT).contains(&byte) {
        return Some(match byte {
            PDU_GET_REQUEST => SnmpPduType::GetRequest,
            ...
            PDU_REPORT => SnmpPduType::Report,
            other => SnmpPduType::Unknown(other),  // <-- unreachable
        });
    }
}

The match arms cover all nine consecutive constants 0xA0..=0xA8 explicitly, and the outer contains excludes anything outside that range — so the Unknown(other) arm has no value it can ever match.

Changes

  1. Inline the gate into the match. _ => continue skips non-PDU bytes so the loop scans through header noise in a single pass without the redundant contains check.

    let pdu_type = match byte {
        PDU_GET_REQUEST => SnmpPduType::GetRequest,
        ...
        PDU_REPORT => SnmpPduType::Report,
        _ => continue,
    };
    return Some(pdu_type);
  2. Drop SnmpPduType::Unknown(u8). With the construction site removed, cargo clippy flags the variant as dead code. The parser never produces it, and the previous Display arm UNKNOWN({}) was only reachable from a code path that didn't exist — deleting it keeps the public type honest about what values it can hold.

Why it matters

  • One pass instead of two checks against the same byte.
  • The type now matches the parser's behavior: nine known PDU tags, nothing else. If a tenth PDU type ever needs adding (e.g. SNMPv3 Report was a late addition for a reason), the fix lives in the match — there's no Unknown-shaped pretense that we already handle it.
  • Removes a clippy::dead_code waiver the codebase didn't actually have but would have needed to silence.

Tests

Added three tests for find_pdu_type that exercise the loop directly:

  • test_find_pdu_type_skips_non_pdu_bytes — leading INTEGER (request-ID) bytes followed by PDU_GET_BULK_REQUEST; the scan must skip the noise and land on the tag. This is the real-world v1/v2c layout: community string is parsed by analyze_snmp, then find_pdu_type is handed the remainder which starts with non-PDU bytes.
  • test_find_pdu_type_returns_none_when_no_pdu — payload with no PDU tag returns None.
  • test_find_pdu_type_caps_search_at_50_bytes — PDU tag at offset 60 must not be found (locks the existing scan cap).
$ cargo test --quiet -p rustnet-monitor snmp
running 9 tests
.........
test result: ok. 9 passed; 0 failed; 0 ignored; 0 measured

$ cargo clippy --all-targets
(clean — including the previously-warned dead-code path)

Full suite stays green (327 passing).

No wire-format behavior change for any input the parser already accepted.

…known variant

`find_pdu_type` gated its match with `if (PDU_GET_REQUEST..=PDU_REPORT).contains(&byte)` and then mapped every value in that range explicitly — leaving `Unknown(other)` as the only catch-all arm, which the outer check made unreachable. Fold the gate into the match using `_ => continue` so non-PDU bytes are skipped in a single pass.

With the construction site gone, `SnmpPduType::Unknown(u8)` becomes truly dead (the compiler raised `dead_code` on it). Drop the variant and its Display arm rather than leaving a phantom for a path the parser never produces.

Add tests for `find_pdu_type` that exercise the loop directly:

- skip a leading INTEGER (request-ID) and land on `GetBulkRequest`
- return None when no PDU tag is present
- enforce the 50-byte search cap (PDU at offset 60 must not be found)

The first one is the most interesting — it pins down the contract that the scan tolerates header noise between the community string and the PDU tag, which is the real-world layout for v1/v2c packets.
@domcyrus
Copy link
Copy Markdown
Owner

@0xghost42 Thanks a lot for your PR. This looks good to me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants