pub(crate) mod dtls;
pub(crate) mod ice;
pub(crate) mod sctp;
pub use dtls::fingerprint::RTCDtlsFingerprint;
pub use dtls::parameters::RTCDtlsParameters;
pub use dtls::role::RTCDtlsRole;
pub use dtls::state::RTCDtlsTransportState;
use std::fmt;
pub use ice::candidate::{
CandidateConfig, CandidateHostConfig, CandidatePeerReflexiveConfig, CandidateRelayConfig,
CandidateServerReflexiveConfig, RTCIceCandidate, RTCIceCandidateInit,
RTCIceServerTransportProtocol, RTCIceTcpCandidateType,
};
pub use ice::candidate_pair::RTCIceCandidatePair;
pub use ice::candidate_type::RTCIceCandidateType;
pub use ice::parameters::RTCIceParameters;
pub use ice::protocol::RTCIceProtocol;
pub use ice::role::RTCIceRole;
pub use ice::server::RTCIceServer;
pub use ice::state::RTCIceTransportState;
pub use sctp::state::RTCSctpTransportState;
use crate::peer_connection::RTCPeerConnection;
use crate::peer_connection::handler::transport_set::TransportId;
use crate::peer_connection::state::RTCIceGatheringState;
pub use ice::component::RTCIceComponent;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct RTCTransportId(u64);
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum TransportKind {
Ice = 0,
Dtls = 1,
Sctp = 2,
}
impl RTCTransportId {
pub(crate) fn new(nonce: u64, kind: TransportKind) -> Self {
Self((nonce << 2) | (kind as u64))
}
}
impl fmt::Display for RTCTransportId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:016x}", self.0)
}
}
pub struct RTCIceTransport<'a> {
pub(crate) peer_connection: &'a RTCPeerConnection,
pub(crate) transport: TransportId,
}
impl RTCIceTransport<'_> {
pub fn id(&self) -> RTCTransportId {
self.peer_connection.ice_transport_of(self.transport).id
}
pub fn component(&self) -> RTCIceComponent {
self.peer_connection
.ice_transport_of(self.transport)
.component()
}
pub fn role(&self) -> RTCIceRole {
self.peer_connection
.ice_transport_of(self.transport)
.w3c_role()
}
pub fn state(&self) -> RTCIceTransportState {
self.peer_connection
.ice_transport_of(self.transport)
.state()
}
pub fn gathering_state(&self) -> RTCIceGatheringState {
self.peer_connection
.ice_transport_of(self.transport)
.gathering_state()
}
pub fn get_local_candidates(&self) -> Vec<RTCIceCandidate> {
self.peer_connection
.ice_transport_of(self.transport)
.get_local_candidates()
.unwrap_or_default()
}
pub fn get_remote_candidates(&self) -> Vec<RTCIceCandidate> {
self.peer_connection
.remote_description_candidates(self.transport)
}
pub fn get_candidate_pairs(&self) -> Vec<RTCIceCandidatePair> {
self.peer_connection
.ice_transport_of(self.transport)
.get_candidate_pairs()
}
pub fn get_selected_candidate_pair(&self) -> Option<RTCIceCandidatePair> {
if self.peer_connection.is_closed()
|| matches!(
self.state(),
RTCIceTransportState::New | RTCIceTransportState::Closed
)
{
return None;
}
self.peer_connection
.ice_transport_of(self.transport)
.get_selected_candidate_pair()
}
pub fn get_local_parameters(&self) -> Option<RTCIceParameters> {
self.peer_connection
.local_description_ice_parameters(self.transport)
}
pub fn get_remote_parameters(&self) -> Option<RTCIceParameters> {
self.peer_connection
.ice_transport_of(self.transport)
.get_remote_parameters()
}
}
pub struct RTCDtlsTransport<'a> {
pub(crate) peer_connection: &'a RTCPeerConnection,
pub(crate) transport: TransportId,
}
impl<'a> RTCDtlsTransport<'a> {
pub fn rtp_session(&self) -> usize {
self.transport.0
}
pub fn id(&self) -> RTCTransportId {
self.peer_connection.dtls_transport_of(self.transport).id
}
pub fn ice_transport(&self) -> RTCIceTransport<'a> {
RTCIceTransport {
peer_connection: self.peer_connection,
transport: self.transport,
}
}
pub fn state(&self) -> RTCDtlsTransportState {
self.peer_connection
.dtls_transport_of(self.transport)
.state()
}
pub fn get_remote_certificates(&self) -> &'a [Vec<u8>] {
self.peer_connection
.dtls_transport_of(self.transport)
.get_remote_certificates()
}
}
pub struct RTCSctpTransport<'a> {
pub(crate) peer_connection: &'a RTCPeerConnection,
}
impl<'a> RTCSctpTransport<'a> {
pub fn id(&self) -> RTCTransportId {
self.peer_connection.sctp_transport().id
}
pub fn transport(&self) -> RTCDtlsTransport<'a> {
RTCDtlsTransport {
peer_connection: self.peer_connection,
transport: self
.peer_connection
.pipeline_context
.transports
.data_transport_id(),
}
}
pub fn state(&self) -> RTCSctpTransportState {
self.peer_connection.sctp_transport().state()
}
pub fn max_message_size(&self) -> Option<u32> {
self.peer_connection.sctp_transport().max_message_size()
}
pub fn max_channels(&self) -> Option<u16> {
self.peer_connection.sctp_transport().max_channels()
}
pub fn congestion_window(&self) -> Option<u32> {
self.peer_connection
.sctp_transport()
.sctp_associations
.values()
.find(|association| !association.is_ended())
.map(|association| association.congestion_window())
}
pub fn association_stats(&self) -> Option<crate::sctp::AssociationStats> {
self.peer_connection
.sctp_transport()
.sctp_associations
.values()
.find(|association| !association.is_ended())
.map(|association| association.stats())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_three_transports_of_one_connection_are_distinct() {
let nonce = 0x0123_4567_89ab_cdef;
let ice = RTCTransportId::new(nonce, TransportKind::Ice);
let dtls = RTCTransportId::new(nonce, TransportKind::Dtls);
let sctp = RTCTransportId::new(nonce, TransportKind::Sctp);
assert_ne!(ice, dtls);
assert_ne!(dtls, sctp);
assert_ne!(ice, sctp);
}
#[test]
fn the_same_transport_of_two_connections_is_distinct() {
let a = RTCTransportId::new(1, TransportKind::Dtls);
let b = RTCTransportId::new(2, TransportKind::Dtls);
assert_ne!(a, b);
}
#[test]
fn the_same_transport_of_one_connection_compares_equal() {
let nonce = 0xfeed_face_dead_beef;
assert_eq!(
RTCTransportId::new(nonce, TransportKind::Dtls),
RTCTransportId::new(nonce, TransportKind::Dtls)
);
}
#[test]
fn nonces_differing_only_in_discarded_bits_still_collide_knowingly() {
let a = RTCTransportId::new(u64::MAX, TransportKind::Ice);
let b = RTCTransportId::new(u64::MAX >> 2, TransportKind::Ice);
assert_eq!(
a, b,
"documented: the top two bits of the nonce are not part of the id, \
leaving 62 bits of entropy rather than 64"
);
}
}