turn/proto/
mod.rs

1#[cfg(test)]
2mod proto_test;
3
4pub mod addr;
5pub mod chandata;
6pub mod channum;
7pub mod data;
8pub mod dontfrag;
9pub mod evenport;
10pub mod lifetime;
11pub mod peeraddr;
12pub mod relayaddr;
13pub mod reqfamily;
14pub mod reqtrans;
15pub mod rsrvtoken;
16
17use std::fmt;
18
19use stun::message::*;
20
21// proto implements RFC 5766 Traversal Using Relays around NAT.
22
23/// `Protocol` is IANA assigned protocol number.
24#[derive(PartialEq, Eq, Default, Debug, Clone, Copy, Hash)]
25pub struct Protocol(pub u8);
26
27/// `PROTO_TCP` is IANA assigned protocol number for TCP.
28pub const PROTO_TCP: Protocol = Protocol(6);
29/// `PROTO_UDP` is IANA assigned protocol number for UDP.
30pub const PROTO_UDP: Protocol = Protocol(17);
31
32impl fmt::Display for Protocol {
33    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34        let others = format!("{}", self.0);
35        let s = match *self {
36            PROTO_UDP => "UDP",
37            PROTO_TCP => "TCP",
38            _ => others.as_str(),
39        };
40
41        write!(f, "{s}")
42    }
43}
44
45// Default ports for TURN from RFC 5766 Section 4.
46
47/// `DEFAULT_PORT` for TURN is same as STUN.
48pub const DEFAULT_PORT: u16 = stun::DEFAULT_PORT;
49/// `DEFAULT_TLSPORT` is for TURN over TLS and is same as STUN.
50pub const DEFAULT_TLS_PORT: u16 = stun::DEFAULT_TLS_PORT;
51
52/// Shorthand for create permission request type.
53pub fn create_permission_request() -> MessageType {
54    MessageType::new(METHOD_CREATE_PERMISSION, CLASS_REQUEST)
55}
56
57/// Shorthand for allocation request message type.
58pub fn allocate_request() -> MessageType {
59    MessageType::new(METHOD_ALLOCATE, CLASS_REQUEST)
60}
61
62/// Shorthand for send indication message type.
63pub fn send_indication() -> MessageType {
64    MessageType::new(METHOD_SEND, CLASS_INDICATION)
65}
66
67/// Shorthand for refresh request message type.
68pub fn refresh_request() -> MessageType {
69    MessageType::new(METHOD_REFRESH, CLASS_REQUEST)
70}