Skip to main content

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