refactor(proto): unify request codecs
Replace the three per-kind request encoders and decoders with one directional pair, ahead of introducing irtt-server. Sender side: encode_request(RequestToEncode, hmac_key) constructs OPEN, CLOSE and ECHO requests from borrowed values. Params and the ECHO payload are borrowed rather than owned, removing a Params clone and a payload Vec from the client's hot paths. Wire output is byte-identical; all captured vectors still pass unchanged. Receiver side: decode_request(packet) structurally classifies an inbound request with no HMAC key, no negotiated Params, no session state and no allocation. A server must extract a token before it knows which key applies, so authentication is a separate step. Classification follows the verified server spec: reserved bits and FLAG_REPLY are rejected, OPEN takes precedence over CLOSE and over an echo-shaped body, and only the structural minimums (4/12/16, plus 16 with FLAG_HMAC) are enforced. Trailing bytes are tolerated, no negotiated length ceiling is applied, and a zero token is structurally valid. OPEN parameters stay encoded bytes so an authenticating receiver can verify before decoding untrusted data, and everything after an ECHO sequence number is exposed as an opaque tail. Add verify_packet_hmac(key, packet), which locates the standard field itself and rejects a packet lacking FLAG_HMAC. hmac_present reports packet layout, never authentication. The offset-based verify_hmac stays public as the low-level primitive for hand-built and captured wire vectors, matching the existing compute_hmac pair. Refactor the private envelope into structural header decoding, codec flag rules, HMAC presence policy and verification as separate steps. The reply codecs compose the same steps in the same order, so decode_open_reply and decode_echo_reply keep their exact error ordering and the client's fatal-versus-ignore classification is unchanged. Reply decoding stays state-specific: the client already knows which reply to expect, and the two need different semantic context. Migrate the production client and every fake peer to the new API; the fake peers now admit requests the way a server does, instead of parsing packets by hand.
This commit is contained in:
18 files changed
+1296
-820
No files matched your search
@@ -12,15 +12,28 @@
|
||||
//! performs no additional validation, so callers that build `Params` manually
|
||||
//! are responsible for validating those values before sending them.
|
||||
//!
|
||||
//! The crate also provides packet layout calculation, open/echo/close packet
|
||||
//! encoding and decoding, and optional HMAC placement, computation, and
|
||||
//! verification helpers.
|
||||
//! The crate also provides packet layout calculation, packet encoding and
|
||||
//! decoding, and optional HMAC placement, computation, and verification
|
||||
//! helpers.
|
||||
//! Encoder `hmac_key` arguments are authoritative: `Some(key)` adds
|
||||
//! `FLAG_HMAC`, while `None` removes it from caller-supplied reply flags.
|
||||
//!
|
||||
//! # Directional model
|
||||
//!
|
||||
//! Requests use one encoder and one decoder for all three request kinds:
|
||||
//! [`encode_request`] builds a request from borrowed sender-side values, and
|
||||
//! [`decode_request`] structurally classifies an inbound request without a key,
|
||||
//! negotiated [`Params`], or any session state. [`verify_packet_hmac`]
|
||||
//! authenticates a packet separately, once the applicable key is known —
|
||||
//! `FLAG_HMAC` being present is a statement about layout, not about validity.
|
||||
//!
|
||||
//! Replies keep type-specific decoders ([`decode_open_reply`],
|
||||
//! [`decode_echo_reply`]) because a client already knows from its session state
|
||||
//! which reply kind to expect, and the two need genuinely different semantic
|
||||
//! context.
|
||||
//!
|
||||
#![forbid(unsafe_code)]
|
||||
|
||||
pub mod close;
|
||||
pub mod echo;
|
||||
mod envelope;
|
||||
pub mod error;
|
||||
@@ -29,22 +42,19 @@ pub mod hmac;
|
||||
pub mod layout;
|
||||
pub mod open;
|
||||
pub mod params;
|
||||
pub mod request;
|
||||
pub mod varint;
|
||||
|
||||
pub use close::{decode_close_request, encode_close_request, CloseRequest};
|
||||
pub use echo::{
|
||||
decode_echo_reply, decode_echo_request, encode_echo_reply, encode_echo_request, EchoReply,
|
||||
EchoRequest, TimestampFields,
|
||||
};
|
||||
pub use echo::{decode_echo_reply, encode_echo_reply, EchoReply, TimestampFields};
|
||||
pub use error::{ProtoError, Result};
|
||||
pub use flags::*;
|
||||
pub use hmac::{compute_hmac, compute_hmac_in_place, verify_hmac};
|
||||
pub use hmac::{compute_hmac, compute_hmac_in_place, verify_hmac, verify_packet_hmac};
|
||||
pub use layout::{echo_header_len, echo_packet_len, PacketLayout};
|
||||
pub use open::{
|
||||
decode_open_reply, decode_open_request, encode_open_reply, encode_open_request, OpenReply,
|
||||
OpenRequest,
|
||||
};
|
||||
pub use open::{decode_open_reply, encode_open_reply, OpenReply};
|
||||
pub use params::{Clock, Params, ReceivedStats, ServerFill, StampAt, MAX_SERVER_FILL_BYTES};
|
||||
pub use request::{
|
||||
decode_request, encode_request, DecodedRequest, DecodedRequestKind, RequestToEncode,
|
||||
};
|
||||
|
||||
pub const MAGIC: [u8; 3] = [0x14, 0xA7, 0x5B];
|
||||
pub const PROTOCOL_VERSION: i64 = 1;
|
||||
|
||||
Reference in new issue
Block a user