zenoh_link_tls/lib.rs
1//
2// Copyright (c) 2023 ZettaScale Technology
3//
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
7// which is available at https://www.apache.org/licenses/LICENSE-2.0.
8//
9// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
10//
11// Contributors:
12// ZettaScale Zenoh Team, <zenoh@zettascale.tech>
13//
14
15//! ⚠️ WARNING ⚠️
16//!
17//! This crate is intended for Zenoh's internal use.
18//!
19//! [Click here for Zenoh's documentation](https://docs.rs/zenoh/latest/zenoh)
20use std::str::FromStr;
21
22use async_trait::async_trait;
23use zenoh_core::zconfigurable;
24use zenoh_link_commons::LocatorInspector;
25use zenoh_protocol::{
26 core::{Locator, Metadata, Reliability},
27 transport::BatchSize,
28};
29use zenoh_result::ZResult;
30
31mod unicast;
32mod utils;
33pub use unicast::*;
34pub use utils::TlsConfigurator;
35
36// Default MTU (TLS PDU) in bytes.
37// NOTE: Since TLS is a byte-stream oriented transport, theoretically it has
38// no limit regarding the MTU. However, given the batching strategy
39// adopted in Zenoh and the usage of 16 bits in Zenoh to encode the
40// payload length in byte-streamed, the TLS MTU is constrained to
41// 2^16 - 1 bytes (i.e., 65535).
42const TLS_MAX_MTU: BatchSize = BatchSize::MAX;
43pub const TLS_LOCATOR_PREFIX: &str = "tls";
44
45const IS_RELIABLE: bool = true;
46
47#[derive(Debug, Default, Clone, Copy)]
48pub struct TlsLocatorInspector;
49#[async_trait]
50impl LocatorInspector for TlsLocatorInspector {
51 fn protocol(&self) -> &str {
52 TLS_LOCATOR_PREFIX
53 }
54
55 async fn is_multicast(&self, _locator: &Locator) -> ZResult<bool> {
56 Ok(false)
57 }
58
59 fn is_reliable(&self, locator: &Locator) -> ZResult<bool> {
60 if let Some(reliability) = locator
61 .metadata()
62 .get(Metadata::RELIABILITY)
63 .map(Reliability::from_str)
64 .transpose()?
65 {
66 Ok(reliability == Reliability::Reliable)
67 } else {
68 Ok(IS_RELIABLE)
69 }
70 }
71}
72
73zconfigurable! {
74 // Default MTU (TLS PDU) in bytes.
75 static ref TLS_DEFAULT_MTU: BatchSize = TLS_MAX_MTU;
76 // The LINGER option causes the shutdown() call to block until (1) all application data is delivered
77 // to the remote end or (2) a timeout expires. The timeout is expressed in seconds.
78 // More info on the LINGER option and its dynamics can be found at:
79 // https://blog.netherlabs.nl/articles/2009/01/18/the-ultimate-so_linger-page-or-why-is-my-tcp-not-reliable
80 static ref TLS_LINGER_TIMEOUT: i32 = 10;
81 // Amount of time in microseconds to throttle the accept loop upon an error.
82 // Default set to 100 ms.
83 static ref TLS_ACCEPT_THROTTLE_TIME: u64 = 100_000;
84}