Skip to content

Commit

Permalink
add handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
yngrtc committed Mar 23, 2024
1 parent e66e706 commit e6279bb
Show file tree
Hide file tree
Showing 48 changed files with 1,083 additions and 478 deletions.
26 changes: 24 additions & 2 deletions rtc-dtls/src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ impl Endpoint {
Ok(())
}

/// Process close
pub fn close(&mut self, remote: SocketAddr) -> Option<DTLSConn> {
/// Process stop remote
pub fn stop(&mut self, remote: SocketAddr) -> Option<DTLSConn> {
if let Some(conn) = self.connections.get_mut(&remote) {
conn.close();
while let Some(payload) = conn.outgoing_raw_packet() {
Expand All @@ -127,6 +127,28 @@ impl Endpoint {
self.connections.remove(&remote)
}

/// Process close
pub fn close(&mut self) -> Result<()> {
for (remote_addr, conn) in self.connections.iter_mut() {
conn.close();
while let Some(payload) = conn.outgoing_raw_packet() {
self.transmits.push_back(Transmit {
now: Instant::now(),
transport: TransportContext {
local_addr: self.local_addr,
peer_addr: *remote_addr,
ecn: None,
protocol: self.protocol,
},
message: payload,
});
}
}
self.connections.clear();

Ok(())
}

/// Process an incoming UDP datagram
pub fn read(
&mut self,
Expand Down
1 change: 1 addition & 0 deletions rtc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pem = { version = "3.0.3", optional = true }
#cfg-if = "1.0.0"
lazy_static = "1.4.0"
smol_str = { version = "0.2.1", features = ["serde"] }
base64 = "0.22.0"

[dev-dependencies]
tokio-test = "0.4.4"
Expand Down
4 changes: 2 additions & 2 deletions rtc/src/api/setting_engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use ice::udp_network::UDPNetwork;*/
use ice::network_type::NetworkType;

use crate::constants::RECEIVE_MTU;
use crate::transports::dtls_transport::dtls_role::DTLSRole;
use crate::transports::ice_transport::ice_candidate_type::RTCIceCandidateType;
use crate::transport::dtls_transport::dtls_role::DTLSRole;
use crate::transport::ice_transport::ice_candidate_type::RTCIceCandidateType;
use shared::error::{Error, Result};

#[derive(Default, Clone)]
Expand Down
3 changes: 3 additions & 0 deletions rtc/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ pub(crate) const SDP_ATTRIBUTE_SIMULCAST: &str = "simulcast";
pub(crate) const GENERATED_CERTIFICATE_ORIGIN: &str = "WebRTC";
pub(crate) const SDES_REPAIR_RTP_STREAM_ID_URI: &str =
"urn:ietf:params:rtp-hdrext:sdes:repaired-rtp-stream-id";
pub(crate) const DEFAULT_SESSION_SRTP_REPLAY_PROTECTION_WINDOW: usize = 64;
pub(crate) const DEFAULT_SESSION_SRTCP_REPLAY_PROTECTION_WINDOW: usize = 64;
pub(crate) const DEFAULT_DTLS_REPLAY_PROTECTION_WINDOW: usize = 64;
112 changes: 112 additions & 0 deletions rtc/src/handlers/demuxer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
use crate::messages::{
DTLSMessageEvent, MessageEvent, RTPMessageEvent, STUNMessageEvent, TaggedMessageEvent,
};
use log::{debug, error};
use retty::channel::{Context, Handler};
use retty::transport::TaggedBytesMut;

/// match_range is a MatchFunc that accepts packets with the first byte in [lower..upper]
fn match_range(lower: u8, upper: u8, buf: &[u8]) -> bool {
if buf.is_empty() {
return false;
}
let b = buf[0];
b >= lower && b <= upper
}

/// MatchFuncs as described in RFC7983
/// <https://tools.ietf.org/html/rfc7983>
/// +----------------+
/// | [0..3] -+--> forward to STUN
/// | |
/// | [16..19] -+--> forward to ZRTP
/// | |
/// packet --> | [20..63] -+--> forward to DTLS
/// | |
/// | [64..79] -+--> forward to TURN Channel
/// | |
/// | [128..191] -+--> forward to RTP/RTCP
/// +----------------+
/// match_dtls is a MatchFunc that accepts packets with the first byte in [20..63]
/// as defied in RFC7983
fn match_dtls(b: &[u8]) -> bool {
match_range(20, 63, b)
}

/// match_srtp is a MatchFunc that accepts packets with the first byte in [128..191]
/// as defied in RFC7983
fn match_srtp(b: &[u8]) -> bool {
match_range(128, 191, b)
}

/// DemuxerHandler implements demuxing of STUN/DTLS/RTP/RTCP Protocol packets
#[derive(Default)]
pub struct DemuxerHandler;

impl DemuxerHandler {
pub fn new() -> Self {
DemuxerHandler
}
}

impl Handler for DemuxerHandler {
type Rin = TaggedBytesMut;
type Rout = TaggedMessageEvent;
type Win = TaggedMessageEvent;
type Wout = TaggedBytesMut;

fn name(&self) -> &str {
"DemuxerHandler"
}

fn handle_read(
&mut self,
ctx: &Context<Self::Rin, Self::Rout, Self::Win, Self::Wout>,
msg: Self::Rin,
) {
if msg.message.is_empty() {
error!("drop invalid packet due to zero length");
} else if match_dtls(&msg.message) {
ctx.fire_read(TaggedMessageEvent {
now: msg.now,
transport: msg.transport,
message: MessageEvent::Dtls(DTLSMessageEvent::Raw(msg.message)),
});
} else if match_srtp(&msg.message) {
ctx.fire_read(TaggedMessageEvent {
now: msg.now,
transport: msg.transport,
message: MessageEvent::Rtp(RTPMessageEvent::Raw(msg.message)),
});
} else {
ctx.fire_read(TaggedMessageEvent {
now: msg.now,
transport: msg.transport,
message: MessageEvent::Stun(STUNMessageEvent::Raw(msg.message)),
});
}
}

fn poll_write(
&mut self,
ctx: &Context<Self::Rin, Self::Rout, Self::Win, Self::Wout>,
) -> Option<Self::Wout> {
if let Some(msg) = ctx.fire_poll_write() {
match msg.message {
MessageEvent::Stun(STUNMessageEvent::Raw(message))
| MessageEvent::Dtls(DTLSMessageEvent::Raw(message))
| MessageEvent::Rtp(RTPMessageEvent::Raw(message)) => Some(TaggedBytesMut {
now: msg.now,
transport: msg.transport,
message,
}),
_ => {
debug!("drop non-RAW packet {:?}", msg.message);
None
}
}
} else {
None
}
}
}
Loading

0 comments on commit e6279bb

Please sign in to comment.