Skip to content

Non-breaking fixes for v0.3.1 - #7

Merged
zaphar merged 22 commits into
masterfrom
review-fixes
Nov 22, 2026
Merged

zaphar merged 22 commits into
masterfrom
review-fixes

Conversation

@zaphar

@zaphar zaphar commented Nov 22, 2026 •

Copy link
Copy Markdown
Owner

Non-breaking fixes from a design review of 0.3.0, as one commit per fix. cargo-semver-checks against v0.3.0 finds no API break, so this is 0.3.1.

Bugs

  • Serializing ICMPv4 Timestamp or Information messages panicked, so verify_checksum panicked on any received packet of these types.
  • ICMPv4 Parameter Problem could be built but not parsed.
  • IcmpSocket6::send_to failed on an unbound socket and computed the checksum with the wrong source when bound to ::. The kernel now computes it (RFC 3542 §3.1).
  • Truncation was guessed from a full buffer, so a reply that fit the buffer exactly failed. It now uses the kernel's MSG_TRUNC flag.
  • A blocking receive timeout returned WouldBlock; it now returns TimedOut, like the async sockets.
  • On Linux, identifier() stayed 0 after a send without bind. It now reads the socket's port on each call.
  • On macOS every datagram socket used identifier 0. It now uses the process ID, as macOS ping does.

Additions

  • Icmpv4Packet implements WithEchoReply.
  • verify_checksum_bytes verifies a checksum over the bytes as received.
  • The packet errors implement std::error::Error and can be downcast from the io::Error they convert to.
  • PartialEq on Icmpv4Message, Icmpv4Packet and Icmpv6Packet.

Behavior changes

  • The OS default TTL applies unless set_max_hops is called. Before, every socket used 50.
  • Socket options are set only when they change, not on every send and receive.
  • socket::Opts is unused and deprecated.

Internal

  • One socket core, generic over a private protocol trait, for all blocking sockets; the async core shares its receive and decode code.
  • Receive into Vec::spare_capacity_mut instead of casting the initialized buffer.

Tests and docs

  • Property tests (proptest) for parsing, serialization and checksums.
  • Loopback tests for exact-fit buffers, the timeout kind, the identifier, the async socket, and raw ICMPv4 and ICMPv6. CI runs the raw tests as root through a cargo runner.
  • The examples match replies on identifier and sequence.
  • The README examples compile and run as doctests.

Test plan

  • macOS 14: unit, property and doc tests; datagram and async loopback tests
  • Linux 6.18: the same, plus the raw tests in an unprivileged user and network namespace
  • CI: raw tests pass as root on macos-latest and ubuntu-latest

🤖 Generated with Claude Code

zaphar and others added 22 commits November 22, 2026 15:40
Icmpv4Message::get_bytes panicked for Timestamp, TimestampReply,
Information and InformationReply. The scratch buffer was empty after
the first append, and vec![0, 4] is two bytes, so the next write went
out of bounds. verify_checksum serializes the packet, so one received
packet of these types made it panic.

Write each field into the output buffer directly, and add round-trip
tests for the four types.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The crate can build a type 12 Parameter Problem packet but could not
parse one: parse_icmp returned UnrecognizedICMPType(12), so a received
Parameter Problem was an error.

Decode the pointer, the padding and the original header, and add a
round-trip test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Only Icmpv6Packet implemented WithEchoReply, and the trait docs said
echo replies are an ICMPv6 concern. ICMPv4 has echo replies too
(type 0), and the crate already parses them, so a program that answers
pings or builds test packets could not construct one.

Add the implementation, drop the incorrect doc line, and add a test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Converting a PacketParseError or IcmpPacketBuildError to io::Error
formatted it into a string, so callers of the socket methods could not
tell the packet error kind apart.

Implement std::error::Error for both types and wrap the error itself.
Callers can now downcast it with get_ref(). The ErrorKind (Other) and
the message text do not change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
verify_checksum serializes the parsed packet again, so it is only as
exact as the parser. The ICMPv4 Timestamp and Information messages keep
no bytes after their fixed fields, so a valid message with extra bytes
fails verification.

Add verify_checksum_bytes for both protocols. It sums the received
bytes and checks for 0xFFFF, so it does not depend on the parser.
Document the limit of verify_checksum, and sum in u64 so that no input
slice can overflow the sum.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The unit tests use fixed inputs only, and missed both the serializer
panic and the missing Parameter Problem parser.

Add proptest properties: parsing arbitrary bytes never panics, messages
serialize back to the bytes they were parsed from, and a checksummed
packet verifies with both verify_checksum and verify_checksum_bytes.
Derive PartialEq on Icmpv4Message, Icmpv4Packet and Icmpv6Packet so the
tests can compare packets.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The two ICMPv4 sockets shared Icmp4Core, but IcmpSocket6 repeated the
bind, buffer, option and receive logic.

Make the core generic over a private Family trait. It holds what differs
between the protocols: domain and protocol, the hop-limit option, the
address and packet types, and how a received message is decoded.
IcmpSocket6 now uses the core. The public API does not change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The async core repeated the steps after a read in the blocking core:
the truncation check and the decoding of the message.

Make the async core generic over Family like the blocking core, and
have both call the same decode_received, to_sockaddr and local_port
helpers. The public API does not change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Both cores cast the initialized receive buffer to &mut [MaybeUninit<u8>].
That is sound only because socket2 promises never to write uninitialized
bytes into it. The TODO waited for Vec::spare_capacity_mut, which has
been stable since Rust 1.60.

Receive into the spare capacity and set the length to the bytes read,
in one recv_into helper that both cores use. The async sockets now take
the blocking core directly in into_async.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Truncation was guessed: a read that filled the buffer was reported as
truncated, so a reply that fit the buffer exactly failed. recvfrom never
returns a partial buffer for a truncated datagram, so the guess had no
false negatives, only false positives.

Receive with recv_from_vectored and check RecvFlags::is_truncated, which
reports the kernel's MSG_TRUNC. Add a loopback test for a reply that
fills the buffer exactly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The blocking sockets use SO_RCVTIMEO, whose expiry is EAGAIN on Unix,
so rcv_from returned WouldBlock. The async sockets return TimedOut for
the same event.

Map WouldBlock to TimedOut when a timeout is set, share the error
between both modes, and document the kind on set_timeout. Add a
loopback test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Every socket set a TTL (hop limit) of 50 on each send, so a caller who
never called set_max_hops still got 50 instead of the OS default,
which is usually 64.

Set the hop limit only after set_max_hops. The sockets now keep their
options in a private type. The public Opts struct was not used by any
public API, and changing its fields would break the API, so it is left
in place and deprecated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The sockets called setsockopt for the hop limit on every send and for
the read timeout on every blocking receive. The kernel keeps an option
once set, so the repeated calls did nothing but cost a system call.

Remember the last applied value and call setsockopt only when the
option changed. An error from setsockopt still appears at the next
send or receive.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
IcmpSocket6::send_to computed the checksum with the bound address as
the source. send_to failed on an unbound socket, and a socket bound to
:: sent a checksum computed with the wrong source address. It worked
only because the kernel computes the checksum again.

RFC 3542 section 3.1 requires the kernel to compute the checksum of
ICMPv6 packets sent on a raw socket, and Linux, macOS and the BSDs do
this. Send with a zero checksum and drop the bind requirement. Add a
raw loopback test that sends from an unbound socket and verifies the
reply; it needs root or CAP_NET_RAW, so it is ignored by default.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Icmpv6Packet::verify_checksum needs the address a packet arrived on,
which IcmpSocket6 does not report. Callers could not see that they do
not need it for packets from the socket: the kernel verifies the
checksum of every ICMPv6 packet a raw socket receives and drops the
packets that fail.

Say so in the verify_checksum docs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The datagram sockets saved their identifier at bind. On Linux, a send
before bind makes the kernel bind the socket and put its new port in
the identifier field, but identifier() still returned 0, so a caller
matching replies against it missed every reply.

Read the local port from the socket on each call to identifier(), in
the blocking and async datagram sockets. Add a loopback test that sends
without binding.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
On macOS a datagram ICMP socket has no port, so every datagram socket
used identifier 0 and replies could not be matched to a socket. Tested
on macOS 14: the kernel keeps the identifier the sender sets and
delivers every reply to every datagram ICMP socket.

Use the low 16 bits of the process ID on macOS, as macOS ping does.
Linux keeps the local port. Document that macOS callers must filter
replies, and add a loopback test that the reply carries identifier().

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The examples accepted any echo reply from the target. They ignored the
identifier, and the raw examples also ignored the sequence, so they
could take another process's reply or a late reply as their own. The
async example ignored identifier(), although the socket docs say to
match on it.

Match on source, identifier and sequence in all three examples, and
remove the outdated TODO about filtering.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Real-kernel tests covered only the blocking ICMPv4 datagram socket. CI
ran the ignored tests with the smol feature, but no async test existed,
and the raw sockets were never tested on the wire.

Add a raw ICMPv4 loopback test next to the raw ICMPv6 one, and an async
datagram loopback test. In CI, run the raw tests as root through a cargo
runner (sudo -E), so only the test binary runs privileged, and limit the
unprivileged step to the datagram tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The README examples did not compile: they had no imports, and
"payload".to_bytes() does not exist. The API docs link pointed to 0.2.0.

Add the imports, use b"payload".to_vec(), unwrap the builder results,
and link to the current docs. Include the README as a doctest so the
examples keep compiling; the socket example is no_run because it needs
privileges.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@zaphar
zaphar merged commit df83946 into master Nov 22, 2026
4 checks passed
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.

1 participant