MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / read_envelope

Function read_envelope

nodedb-cluster/src/transport/server.rs:361–393  ·  view source on GitHub ↗

Read a complete authenticated envelope from a QUIC receive stream. Reads the fixed envelope pre-header (version + from_node_id + seq + inner_len), then the inner frame, then the MAC tag. Returns the full envelope bytes for caller-side parsing.

(recv: &mut quinn::RecvStream)

Source from the content-addressed store, hash-verified

359/// inner_len), then the inner frame, then the MAC tag. Returns the full
360/// envelope bytes for caller-side parsing.
361pub(crate) async fn read_envelope(recv: &mut quinn::RecvStream) -> Result<Vec<u8>> {
362 // Envelope header is version(1) + from_node_id(8) + seq(8) + inner_len(4).
363 const ENV_HDR_LEN: usize = 21;
364
365 let mut hdr = [0u8; ENV_HDR_LEN];
366 recv.read_exact(&mut hdr)
367 .await
368 .map_err(|e| ClusterError::Transport {
369 detail: format!("read envelope header: {e}"),
370 })?;
371
372 let inner_len = u32::from_le_bytes([hdr[17], hdr[18], hdr[19], hdr[20]]);
373 if inner_len > MAX_RPC_PAYLOAD_SIZE {
374 return Err(ClusterError::Codec {
375 detail: format!(
376 "envelope inner length {inner_len} exceeds maximum {MAX_RPC_PAYLOAD_SIZE}"
377 ),
378 });
379 }
380
381 let total = ENV_HDR_LEN + inner_len as usize + rpc_codec::MAC_LEN;
382 let mut buf = vec![0u8; total];
383 buf[..ENV_HDR_LEN].copy_from_slice(&hdr);
384 if total > ENV_HDR_LEN {
385 recv.read_exact(&mut buf[ENV_HDR_LEN..])
386 .await
387 .map_err(|e| ClusterError::Transport {
388 detail: format!("read envelope payload+mac: {e}"),
389 })?;
390 }
391
392 Ok(buf)
393}

Callers 3

handle_streamFunction · 0.85
try_send_onceMethod · 0.85

Calls 1

read_exactMethod · 0.45

Tested by

no test coverage detected