turn_types/
lib.rs

1// Copyright (C) 2025 Matthew Waters <matthew@centricular.com>
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9#![deny(missing_debug_implementations)]
10#![deny(missing_docs)]
11
12//! # turn-types
13//!
14//! `turn-types` provides an implementation for two main things related to TURN clients and servers:
15//! 1. TURN specific STUN attributes using the [stun-types] crate.
16//! 2. Parsing to and from the actual data sent and received on the wire between a TURN client and
17//!    a TURN server.
18//!
19//! This crate implements the STUN attributes and methods presented in the following standards:
20//! - [RFC5766]: Traversal Using Relays around NAT (TURN).
21//! - [RFC6062]: Traversal Using Relays around NAT (TURN) Extensions for TCP Allocations
22//! - [RFC6156]: Traversal Using Relays around NAT (TURN) Extension for IPv6
23//! - [RFC8656]: Traversal Using Relays around NAT (TURN): Relay Extensions to Session
24//!   Traversal Utilities for NAT (STUN)
25//!
26//! [stun-types]: https://docs.rs/stun-types/latest/stun_types
27//! [RFC5766]: https://tools.ietf.org/html/rfc5766
28//! [RFC6062]: https://tools.ietf.org/html/rfc6062
29//! [RFC6156]: https://tools.ietf.org/html/rfc6156
30//! [RFC8656]: https://tools.ietf.org/html/rfc8656
31
32#![no_std]
33
34extern crate alloc;
35
36#[cfg(any(feature = "std", test))]
37extern crate std;
38
39pub use stun_types as stun;
40use stun_types::message::LongTermCredentials;
41pub mod attribute;
42pub mod channel;
43pub mod message;
44pub mod tcp;
45pub mod transmit;
46
47/// Public prelude.
48pub mod prelude {
49    pub use crate::transmit::DelayedTransmitBuild;
50}
51
52use alloc::borrow::ToOwned;
53use alloc::string::{String, ToString};
54pub use stun_types::AddressFamily;
55
56/// Initialize some debugging functionality of the library.
57///
58/// It is not required to call this function, however doing so allows debug functionality of
59/// stun-types to print much more human readable descriptions of attributes and messages.
60pub fn debug_init() {
61    attribute::attributes_init();
62    message::debug_init();
63}
64
65/// Credentials used for a TURN user.
66#[derive(Debug, Clone)]
67pub struct TurnCredentials {
68    username: String,
69    password: String,
70}
71
72impl TurnCredentials {
73    /// Transform these credentials into some `LongTermCredentials` for use in a STUN context.
74    pub fn into_long_term_credentials(self, realm: &str) -> LongTermCredentials {
75        LongTermCredentials::new(self.username, self.password, realm.to_string())
76    }
77
78    /// Construct a new set of [`TurnCredentials`]
79    pub fn new(username: &str, password: &str) -> Self {
80        Self {
81            username: username.to_owned(),
82            password: password.to_owned(),
83        }
84    }
85
86    /// The username of the credentials.
87    pub fn username(&self) -> &str {
88        &self.username
89    }
90
91    /// The password of the credentials.
92    pub fn password(&self) -> &str {
93        &self.password
94    }
95}
96
97#[cfg(test)]
98mod tests {
99    use tracing::subscriber::DefaultGuard;
100    use tracing_subscriber::layer::SubscriberExt;
101    use tracing_subscriber::Layer;
102
103    pub fn test_init_log() -> DefaultGuard {
104        crate::debug_init();
105        let level_filter = std::env::var("TURN_LOG")
106            .or(std::env::var("RUST_LOG"))
107            .ok()
108            .and_then(|var| var.parse::<tracing_subscriber::filter::Targets>().ok())
109            .unwrap_or(
110                tracing_subscriber::filter::Targets::new().with_default(tracing::Level::TRACE),
111            );
112        let registry = tracing_subscriber::registry().with(
113            tracing_subscriber::fmt::layer()
114                .with_file(true)
115                .with_line_number(true)
116                .with_level(true)
117                .with_target(false)
118                .with_test_writer()
119                .with_filter(level_filter),
120        );
121        tracing::subscriber::set_default(registry)
122    }
123}