zenoh_link_commons/
unicast.rs

1//
2// Copyright (c) 2022 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//
14use alloc::{boxed::Box, string::String, sync::Arc, vec::Vec};
15use core::{
16    fmt,
17    hash::{Hash, Hasher},
18    ops::Deref,
19};
20use std::net::SocketAddr;
21
22use async_trait::async_trait;
23use serde::Serialize;
24use zenoh_protocol::{
25    core::{EndPoint, Locator},
26    transport::BatchSize,
27};
28use zenoh_result::ZResult;
29
30pub type LinkManagerUnicast = Arc<dyn LinkManagerUnicastTrait>;
31#[async_trait]
32pub trait LinkManagerUnicastTrait: Send + Sync {
33    async fn new_link(&self, endpoint: EndPoint) -> ZResult<LinkUnicast>;
34    async fn new_listener(&self, endpoint: EndPoint) -> ZResult<Locator>;
35    async fn del_listener(&self, endpoint: &EndPoint) -> ZResult<()>;
36    async fn get_listeners(&self) -> Vec<EndPoint>;
37    async fn get_locators(&self) -> Vec<Locator>;
38}
39pub type NewLinkChannelSender = flume::Sender<LinkUnicast>;
40
41pub trait ConstructibleLinkManagerUnicast<T>: Sized {
42    fn new(new_link_sender: NewLinkChannelSender, config: T) -> ZResult<Self>;
43}
44
45#[derive(Clone)]
46pub struct LinkUnicast(pub Arc<dyn LinkUnicastTrait>);
47
48#[async_trait]
49pub trait LinkUnicastTrait: Send + Sync {
50    fn get_mtu(&self) -> BatchSize;
51    fn get_src(&self) -> &Locator;
52    fn get_dst(&self) -> &Locator;
53    fn is_reliable(&self) -> bool;
54    fn is_streamed(&self) -> bool;
55    fn get_interface_names(&self) -> Vec<String>;
56    fn get_auth_id(&self) -> &LinkAuthId;
57    async fn write(&self, buffer: &[u8]) -> ZResult<usize>;
58    async fn write_all(&self, buffer: &[u8]) -> ZResult<()>;
59    async fn read(&self, buffer: &mut [u8]) -> ZResult<usize>;
60    async fn read_exact(&self, buffer: &mut [u8]) -> ZResult<()>;
61    async fn close(&self) -> ZResult<()>;
62}
63
64impl Deref for LinkUnicast {
65    type Target = Arc<dyn LinkUnicastTrait>;
66
67    #[inline]
68    fn deref(&self) -> &Self::Target {
69        &self.0
70    }
71}
72
73impl Eq for LinkUnicast {}
74
75impl PartialEq for LinkUnicast {
76    fn eq(&self, other: &Self) -> bool {
77        self.get_src() == other.get_src() && self.get_dst() == other.get_dst()
78    }
79}
80
81impl Hash for LinkUnicast {
82    fn hash<H: Hasher>(&self, state: &mut H) {
83        self.get_src().hash(state);
84        self.get_dst().hash(state);
85    }
86}
87
88impl fmt::Display for LinkUnicast {
89    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
90        write!(f, "{} => {}", self.get_src(), self.get_dst())
91    }
92}
93
94impl fmt::Debug for LinkUnicast {
95    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
96        f.debug_struct("Link")
97            .field("src", &self.get_src())
98            .field("dst", &self.get_dst())
99            .field("mtu", &self.get_mtu())
100            .field("is_reliable", &self.is_reliable())
101            .field("is_streamed", &self.is_streamed())
102            .finish()
103    }
104}
105
106impl From<Arc<dyn LinkUnicastTrait>> for LinkUnicast {
107    fn from(link: Arc<dyn LinkUnicastTrait>) -> LinkUnicast {
108        LinkUnicast(link)
109    }
110}
111
112pub fn get_ip_interface_names(addr: &SocketAddr) -> Vec<String> {
113    match zenoh_util::net::get_interface_names_by_addr(addr.ip()) {
114        Ok(interfaces) => {
115            tracing::trace!("get_interface_names for {:?}: {:?}", addr.ip(), interfaces);
116            interfaces
117        }
118        Err(e) => {
119            tracing::debug!("get_interface_names for {:?} failed: {:?}", addr.ip(), e);
120            vec![]
121        }
122    }
123}
124
125#[derive(Clone, Debug, Serialize, Hash, PartialEq, Eq)]
126pub enum LinkAuthId {
127    Tls(Option<String>),
128    Quic(Option<String>),
129    Tcp,
130    Udp,
131    Serial,
132    Unixpipe,
133    UnixsockStream,
134    Vsock,
135    Ws,
136}
137
138impl LinkAuthId {
139    pub fn get_cert_common_name(&self) -> Option<&str> {
140        match &self {
141            LinkAuthId::Tls(n) => n.as_ref().map(|s| s.as_ref()),
142            LinkAuthId::Quic(n) => n.as_ref().map(|s| s.as_ref()),
143            LinkAuthId::Tcp => None,
144            LinkAuthId::Udp => None,
145            LinkAuthId::Serial => None,
146            LinkAuthId::Unixpipe => None,
147            LinkAuthId::UnixsockStream => None,
148            LinkAuthId::Vsock => None,
149            LinkAuthId::Ws => None,
150        }
151    }
152}