Module tcp_framing

Module tcp_framing 

Source
Expand description

TCP framing utilities for ICE over TCP (RFC 4571/RFC 6544).

When using ICE over TCP, all messages must be framed with a 2-byte big-endian length prefix. This module provides helper functions for encoding and decoding framed packets without performing any I/O.

§Protocol Format (RFC 4571 Section 2)

 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
-----------------------------------------------------------------
|             LENGTH            |  STUN/DTLS/RTP packet ...     |
-----------------------------------------------------------------

§Example Usage

use rtc_shared::tcp_framing::{frame_packet, TcpFrameDecoder};

// Encoding: add framing header to outbound packet
let packet = b"STUN message data";
let framed = frame_packet(packet);
// Send `framed` over TCP...

// Decoding: parse framed packets from inbound TCP data
let mut decoder = TcpFrameDecoder::new();
// Feed data as it arrives from TCP...
decoder.extend_from_slice(&framed);
while let Some(packet) = decoder.next_packet() {
    // Process complete packet
    assert_eq!(packet, b"STUN message data");
}

Structs§

TcpFrameDecoder
A stateful decoder for RFC 4571 framed TCP packets.

Constants§

FRAMING_HEADER_LEN
Length of the framing header (2 bytes for length prefix).
MAX_FRAMED_PACKET_SIZE
Maximum packet size that can be framed (u16::MAX = 65535 bytes).

Functions§

frame_packet
Adds RFC 4571 framing header to a packet.
frame_packet_to
Adds RFC 4571 framing header to a packet, writing into a provided buffer.