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 things:
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.
17//!
18//! This is based on the following standards:
19//! - [RFC5766]
20//!
21//! [RFC5766]: https://tools.ietf.org/html/rfc5766
22
23pub use stun_types as stun;
24use stun_types::message::LongTermCredentials;
25pub mod attribute;
26pub mod channel;
27pub mod message;
28
29/// Initialize the library.
30pub fn debug_init() {
31    attribute::attributes_init();
32}
33
34/// Credentials used for a TURN user.
35#[derive(Debug, Clone)]
36pub struct TurnCredentials {
37    username: String,
38    password: String,
39}
40
41impl TurnCredentials {
42    /// Transform these credentials into some `LongTermCredentials` for use in a STUN context.
43    pub fn into_long_term_credentials(self, realm: &str) -> LongTermCredentials {
44        LongTermCredentials::new(self.username, self.password, realm.to_string())
45    }
46
47    /// Construct a new set of [`TurnCredentials`]
48    pub fn new(username: &str, password: &str) -> Self {
49        Self {
50            username: username.to_owned(),
51            password: password.to_owned(),
52        }
53    }
54
55    /// The username of the credentials.
56    pub fn username(&self) -> &str {
57        &self.username
58    }
59
60    /// The password of the credentials.
61    pub fn password(&self) -> &str {
62        &self.password
63    }
64}
65
66#[cfg(test)]
67mod tests {
68    use tracing::subscriber::DefaultGuard;
69    use tracing_subscriber::layer::SubscriberExt;
70    use tracing_subscriber::Layer;
71
72    pub fn test_init_log() -> DefaultGuard {
73        crate::debug_init();
74        let level_filter = std::env::var("TURN_LOG")
75            .or(std::env::var("RUST_LOG"))
76            .ok()
77            .and_then(|var| var.parse::<tracing_subscriber::filter::Targets>().ok())
78            .unwrap_or(
79                tracing_subscriber::filter::Targets::new().with_default(tracing::Level::TRACE),
80            );
81        let registry = tracing_subscriber::registry().with(
82            tracing_subscriber::fmt::layer()
83                .with_file(true)
84                .with_line_number(true)
85                .with_level(true)
86                .with_target(false)
87                .with_test_writer()
88                .with_filter(level_filter),
89        );
90        tracing::subscriber::set_default(registry)
91    }
92}