turn_rs/processor/
channel_data.rs

1use super::{Context, Response};
2use crate::StunClass;
3use faster_stun::ChannelData;
4
5/// process channel data
6///
7/// If the ChannelData message is received on a channel that is not bound
8/// to any peer, then the message is silently discarded.
9///
10/// On the client, it is RECOMMENDED that the client discard the
11/// ChannelData message if the client believes there is no active
12/// permission towards the peer.  On the server, the receipt of a
13/// ChannelData message MUST NOT refresh either the channel binding or
14/// the permission towards the peer.
15///
16/// On the server, if no errors are detected, the server relays the
17/// application data to the peer by forming a UDP datagram as follows:
18///
19/// * the source transport address is the relayed transport address of
20/// the allocation, where the allocation is determined by the 5-tuple
21/// on which the ChannelData message arrived;
22///
23/// * the destination transport address is the transport address to
24/// which the channel is bound;
25///
26/// * the data following the UDP header is the contents of the data
27/// field of the ChannelData message.
28///
29/// The resulting UDP datagram is then sent to the peer.  Note that if
30/// the Length field in the ChannelData message is 0, then there will be
31/// no data in the UDP datagram, but the UDP datagram is still formed and
32/// sent [(Section 4.1 of [RFC6263])](https://tools.ietf.org/html/rfc6263#section-4.1).
33pub fn process(ctx: Context, data: ChannelData<'_>) -> Option<Response<'_>> {
34    let addr = ctx.env.router.get_channel_bound(&ctx.addr, data.number)?;
35    let interface = ctx.env.router.get_interface(&addr)?;
36    let to = (&ctx.env.interface != &interface.addr).then(|| interface.addr);
37    Some(Response::new(data.buf, StunClass::Channel, Some(addr), to))
38}