Skip to content

Commit

Permalink
Locator to EndPoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Mallets committed Aug 26, 2021
1 parent 33c16af commit 2f766b6
Show file tree
Hide file tree
Showing 39 changed files with 1,674 additions and 1,074 deletions.
404 changes: 404 additions & 0 deletions zenoh/src/net/link/endpoint.rs

Large diffs are not rendered by default.

20 changes: 4 additions & 16 deletions zenoh/src/net/link/manager.rs
Expand Up @@ -21,7 +21,7 @@ use super::tls::LinkManagerUnicastTls;
use super::udp::{LinkManagerMulticastUdp, LinkManagerUnicastUdp};
#[cfg(all(feature = "transport_unixsock-stream", target_family = "unix"))]
use super::unixsock_stream::LinkManagerUnicastUnixSocketStream;
use super::{EndPoint, LinkMulticast, LinkUnicast, Locator, LocatorProperty, LocatorProtocol};
use super::{EndPoint, LinkMulticast, LinkUnicast, Locator, LocatorProtocol};
use crate::net::transport::TransportManager;
use async_std::sync::Arc;
use async_trait::async_trait;
Expand All @@ -34,16 +34,8 @@ use zenoh_util::zerror;
pub type LinkManagerUnicast = Arc<dyn LinkManagerUnicastTrait>;
#[async_trait]
pub trait LinkManagerUnicastTrait: Send + Sync {
async fn new_link(
&self,
endpoint: &EndPoint,
property: Option<&LocatorProperty>,
) -> ZResult<LinkUnicast>;
async fn new_listener(
&self,
endpoint: &EndPoint,
property: Option<&LocatorProperty>,
) -> ZResult<Locator>;
async fn new_link(&self, endpoint: &EndPoint) -> ZResult<LinkUnicast>;
async fn new_listener(&self, endpoint: &EndPoint) -> ZResult<Locator>;
async fn del_listener(&self, endpoint: &EndPoint) -> ZResult<()>;
fn get_listeners(&self) -> Vec<Locator>;
fn get_locators(&self) -> Vec<Locator>;
Expand Down Expand Up @@ -78,11 +70,7 @@ impl LinkManagerBuilderUnicast {
/*************************************/
#[async_trait]
pub trait LinkManagerMulticastTrait: Send + Sync {
async fn new_link(
&self,
endpoint: &EndPoint,
property: Option<&LocatorProperty>,
) -> ZResult<LinkMulticast>;
async fn new_link(&self, endpoint: &EndPoint) -> ZResult<LinkMulticast>;
}

pub type LinkManagerMulticast = Arc<dyn LinkManagerMulticastTrait>;
Expand Down
155 changes: 155 additions & 0 deletions zenoh/src/net/link/quic/endpoint.rs
@@ -0,0 +1,155 @@
//
// Copyright (c) 2017, 2020 ADLINK Technology Inc.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
//
// Contributors:
// ADLINK zenoh team, <zenoh@adlink-labs.tech>
//
use super::config::*;
use super::*;
use async_std::net::{SocketAddr, ToSocketAddrs};
use std::fmt;
use std::str::FromStr;
use webpki::{DnsName, DnsNameRef};
use zenoh_util::core::{ZError, ZErrorKind, ZResult};
use zenoh_util::properties::config::*;
use zenoh_util::properties::Properties;

#[allow(unreachable_patterns)]
pub(super) async fn get_quic_addr(address: &LocatorAddress) -> ZResult<SocketAddr> {
match &address {
LocatorAddress::Quic(addr) => match addr {
LocatorQuic::SocketAddr(addr) => Ok(*addr),
LocatorQuic::DnsName(addr) => match addr.to_socket_addrs().await {
Ok(mut addr_iter) => {
if let Some(addr) = addr_iter.next() {
Ok(addr)
} else {
let e = format!("Couldn't resolve QUIC locator address: {}", addr);
zerror!(ZErrorKind::InvalidLocator { descr: e })
}
}
Err(e) => {
let e = format!("{}: {}", e, addr);
zerror!(ZErrorKind::InvalidLocator { descr: e })
}
},
},
_ => {
let e = format!("Not a QUIC locator address: {}", address);
return zerror!(ZErrorKind::InvalidLocator { descr: e });
}
}
}

#[allow(unreachable_patterns)]
pub(super) async fn get_quic_dns(address: &LocatorAddress) -> ZResult<DnsName> {
match &address {
LocatorAddress::Quic(addr) => match addr {
LocatorQuic::SocketAddr(addr) => {
let e = format!("Couldn't get domain from SocketAddr: {}", addr);
zerror!(ZErrorKind::InvalidLocator { descr: e })
}
LocatorQuic::DnsName(addr) => {
// Separate the domain from the port.
// E.g. zenoh.io:7447 returns (zenoh.io, 7447).
let split: Vec<&str> = addr.split(':').collect();
match split.get(0) {
Some(dom) => {
let domain = DnsNameRef::try_from_ascii_str(dom).map_err(|e| {
let e = e.to_string();
zerror2!(ZErrorKind::InvalidLocator { descr: e })
})?;
Ok(domain.to_owned())
}
None => {
let e = format!("Couldn't get domain for: {}", addr);
zerror!(ZErrorKind::InvalidLocator { descr: e })
}
}
}
},
_ => {
let e = format!("Not a QUIC locator address: {}", address);
return zerror!(ZErrorKind::InvalidLocator { descr: e });
}
}
}

/*************************************/
/* LOCATOR */
/*************************************/
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum LocatorQuic {
SocketAddr(SocketAddr),
DnsName(String),
}

impl LocatorQuic {
pub fn is_multicast(&self) -> bool {
false
}
}

impl FromStr for LocatorQuic {
type Err = ZError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.parse() {
Ok(addr) => Ok(LocatorQuic::SocketAddr(addr)),
Err(_) => Ok(LocatorQuic::DnsName(s.to_string())),
}
}
}

impl fmt::Display for LocatorQuic {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
LocatorQuic::SocketAddr(addr) => write!(f, "{}", addr)?,
LocatorQuic::DnsName(addr) => write!(f, "{}", addr)?,
}
Ok(())
}
}

/*************************************/
/* LOCATOR CONFIG */
/*************************************/
pub struct LocatorConfigQuic;

impl LocatorConfigQuic {
pub fn from_config(config: &ConfigProperties) -> ZResult<Option<Properties>> {
let mut properties = Properties::default();

if let Some(tls_ca_certificate) = config.get(&ZN_TLS_ROOT_CA_CERTIFICATE_KEY) {
properties.insert(
TLS_ROOT_CA_CERTIFICATE_FILE.into(),
tls_ca_certificate.into(),
);
}
if let Some(tls_server_private_key) = config.get(&ZN_TLS_SERVER_PRIVATE_KEY_KEY) {
properties.insert(
TLS_SERVER_PRIVATE_KEY_FILE.into(),
tls_server_private_key.into(),
);
}
if let Some(tls_server_certificate) = config.get(&ZN_TLS_SERVER_CERTIFICATE_KEY) {
properties.insert(
TLS_SERVER_CERTIFICATE_FILE.into(),
tls_server_certificate.into(),
);
}

if properties.is_empty() {
Ok(None)
} else {
Ok(Some(properties))
}
}
}

0 comments on commit 2f766b6

Please sign in to comment.