Repository navigation
Non-breaking fixes for v0.3.1 - #7
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Non-breaking fixes from a design review of 0.3.0, as one commit per fix.
cargo-semver-checksagainst v0.3.0 finds no API break, so this is 0.3.1.Bugs
verify_checksumpanicked on any received packet of these types.IcmpSocket6::send_tofailed on an unbound socket and computed the checksum with the wrong source when bound to::. The kernel now computes it (RFC 3542 §3.1).MSG_TRUNCflag.WouldBlock; it now returnsTimedOut, like the async sockets.identifier()stayed 0 after a send withoutbind. It now reads the socket's port on each call.pingdoes.Additions
Icmpv4PacketimplementsWithEchoReply.verify_checksum_bytesverifies a checksum over the bytes as received.std::error::Errorand can be downcast from theio::Errorthey convert to.PartialEqonIcmpv4Message,Icmpv4PacketandIcmpv6Packet.Behavior changes
set_max_hopsis called. Before, every socket used 50.socket::Optsis unused and deprecated.Internal
Vec::spare_capacity_mutinstead of casting the initialized buffer.Tests and docs
Test plan
🤖 Generated with Claude Code