Encode `msg` and wrap it in an authenticated envelope destined for `to`. Returns the bytes to hand to `UdpSocket::send_to`. `to` is retained in the signature as documentation (callers pass the destination they are about to `send_to`), but the outbound seq is a single sender-global counter — see `PeerSeqSender`.
(auth: &SwimAuth, _to: SocketAddr, msg: &SwimMessage)
| 101 | /// destination they are about to `send_to`), but the outbound seq is a |
| 102 | /// single sender-global counter — see `PeerSeqSender`. |
| 103 | pub fn wrap(auth: &SwimAuth, _to: SocketAddr, msg: &SwimMessage) -> Result<Vec<u8>, SwimError> { |
| 104 | let inner = codec::encode(msg)?; |
| 105 | let seq = auth.seq_out.next(); |
| 106 | let mut out = Vec::with_capacity(auth_envelope::ENVELOPE_OVERHEAD + inner.len()); |
| 107 | auth_envelope::write_envelope(auth.local_addr_hash, seq, &inner, &auth.mac_key, &mut out) |
| 108 | .map_err(|e| SwimError::Encode { |
| 109 | detail: format!("swim envelope: {e}"), |
| 110 | })?; |
| 111 | Ok(out) |
| 112 | } |
| 113 | |
| 114 | /// Parse an inbound datagram: verify MAC, bind envelope's advertised |
| 115 | /// origin to the observed remote address, reject replays, then decode |