Skip to main content

rtci_shared/
lib.rs

1#![warn(rust_2018_idioms)]
2#![warn(missing_docs)]
3#![allow(dead_code)]
4
5//! Shared types and utilities for the Sans-I/O WebRTC stack.
6//!
7//! This crate holds what every other crate in the [`rtci`](https://docs.rs/rtc) stack needs:
8//! the common [`Error`](error::Error) type, the [`Marshal`](marshal::Marshal)/[`Unmarshal`](marshal::Unmarshal)
9//! traits that every protocol codec implements, and the transport plumbing that carries
10//! bytes between the network and a protocol state machine.
11//!
12//! # Key types
13//!
14//! * [`TransportContext`] / [`TransportMessage`] — a datagram plus the 4-tuple and protocol
15//!   it arrived on or should be sent on. Every layer in the stack passes these around
16//!   instead of touching sockets.
17//! * [`marshal`] — [`Marshal`](marshal::Marshal), [`Unmarshal`](marshal::Unmarshal) and
18//!   [`MarshalSize`](marshal::MarshalSize), the wire-format traits shared by STUN, RTP,
19//!   RTCP, SDP, DTLS and SCTP.
20//! * [`error`] — the crate-wide [`Error`](error::Error) enum and `Result` alias, re-exported
21//!   by the higher-level crates so callers import from one place.
22//! * [`replay_detector`] — replay protection shared by DTLS and SRTP. (Cryptography
23//!   itself lives in the separate `rtci-crypto` crate, behind `RTCCryptoProvider`.)
24//! * [`tcp_framing`] — RFC 4571 length-prefixed framing, for ICE-TCP candidates.
25//! * [`turn_framing`] — splitting a TURN stream into its self-delimiting messages, for TURN
26//!   over TCP.
27//! * [`stream`] — stream transports without sockets: connection ids, the commands an engine
28//!   gives its driver (listen, connect, close, reset) and the events it gets back, framing of
29//!   arbitrarily segmented reads, an optional security layer (TLS) and back-pressure.
30//! * [`ifaces`] — local interface enumeration used during ICE candidate gathering.
31//!
32//! # Feature flags
33//!
34//! `crypto`, `ifaces`, `marshal` and `replay` are all enabled by default; each gates the
35//! correspondingly named module so that dependents can compile only what they use.
36//!
37//! # Example
38//!
39//! Every protocol codec in the stack implements the same three traits, so encoding and decoding
40//! look the same whichever layer you are at:
41//!
42//! ```
43//! use bytes::Bytes;
44//! use rtci_shared::marshal::{Marshal, MarshalSize, Unmarshal};
45//!
46//! # fn round_trip<T: Marshal + Unmarshal + PartialEq + std::fmt::Debug>(value: T)
47//! # -> Result<(), Box<dyn std::error::Error>> {
48//! // Size the buffer, encode into it, then decode the result back.
49//! let n = value.marshal_size();
50//! let encoded = value.marshal()?;
51//! assert_eq!(encoded.len(), n);
52//!
53//! let mut buf = Bytes::from(encoded.to_vec());
54//! assert_eq!(T::unmarshal(&mut buf)?, value);
55//! # Ok(())
56//! # }
57//! ```
58//!
59//! Most applications do not depend on this crate directly — the [`rtci`](https://docs.rs/rtc)
60//! crate re-exports what it needs as `rtci::shared`.
61
62#[cfg(target_family = "windows")]
63#[macro_use]
64extern crate bitflags;
65
66#[cfg(feature = "ifaces")]
67/// Local network interface enumeration, used to gather ICE host candidates.
68pub mod ifaces;
69
70#[cfg(feature = "marshal")]
71/// The wire-format traits every protocol codec in the stack implements.
72pub mod marshal;
73
74#[cfg(feature = "replay")]
75/// Replay protection for sequence-numbered packets, as DTLS and SRTP require.
76pub mod replay_detector;
77
78/// The crate-wide error type shared by every protocol in the stack.
79pub mod error;
80/// `serde` helpers for types that have no natural serialized form, such as [`std::time::Instant`].
81pub mod serde;
82pub mod stream;
83pub mod tcp_framing;
84/// Conversions between monotonic, Unix and NTP time.
85pub mod time;
86pub(crate) mod transport;
87pub mod turn_framing;
88/// Small shared helpers: packet demultiplexing predicates and random-string generation.
89pub mod util;
90
91pub use transport::{
92    EcnCodepoint, FiveTuple, FourTuple, TaggedBytesMut, TransportContext, TransportMessage,
93    TransportProtocol,
94};