Expand description
Splitting a TURN-over-TCP byte stream into STUN and ChannelData messages. Splitting a TURN byte stream back into messages (RFC 8656 §12.5).
Over TCP or TLS, TURN sends STUN messages and ChannelData messages back to
back with no framing of their own: each is self-delimiting. This is not the
RFC 4571 length prefix that ICE-TCP uses — see tcp_framing
for that — and a stream carries one or the other, never both.
STUN 00xxxxxx ........ | length (16) | magic cookie, id ... | attributes
total = 20 + length
ChannelData 01xxxxxx ........ | length (16) | data | padding to a multiple of 4
total = 4 + length rounded up to 4The first two bits tell them apart. Anything else cannot be TURN, and since a stream has no resynchronisation point it is an error that ends the stream.
use rtc_shared::turn_framing::TurnStreamDecoder;
// A ChannelData message on channel 0x4000 carrying three bytes, padded to 8.
let wire = [0x40, 0x00, 0x00, 0x03, b'a', b'b', b'c', 0x00];
let mut decoder = TurnStreamDecoder::new();
decoder.extend_from_slice(&wire[..5]);
assert_eq!(decoder.next_message().unwrap(), None); // not all of it yet
decoder.extend_from_slice(&wire[5..]);
assert_eq!(decoder.next_message().unwrap(), Some(wire.to_vec()));Structs§
- Turn
Stream Decoder - Sans-IO splitter for a TURN stream: push bytes as they arrive, pop whole messages. A message comes out exactly as it was on the wire, ChannelData padding included, which TURN’s decoders accept.