Skip to content

Commit

Permalink
Extract out network_table
Browse files Browse the repository at this point in the history
  • Loading branch information
octol committed Oct 6, 2023
1 parent 1436462 commit f53e7fb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 22 deletions.
33 changes: 14 additions & 19 deletions common/wireguard/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,38 @@
#![cfg_attr(not(target_os = "linux"), allow(dead_code))]

use std::net::SocketAddr;

use dashmap::DashMap;
use network_table::NetworkTable;
use nym_task::TaskClient;
use tokio::sync::mpsc;

mod error;
mod event;
mod network_table;
mod platform;
mod setup;
mod udp_listener;
mod wg_tunnel;

use crate::event::Event;

// Currently the module related to setting up the virtual network device is platform specific.
#[cfg(target_os = "linux")]
use platform::linux::tun_device;

#[derive(Default)]
struct AllowedIps<T> {
pub ips: ip_network_table::IpNetworkTable<T>,
}

impl<T> AllowedIps<T> {
fn new() -> Self {
Self {
ips: ip_network_table::IpNetworkTable::new(),
}
}
}

type ActivePeers =
dashmap::DashMap<std::net::SocketAddr, tokio::sync::mpsc::UnboundedSender<crate::event::Event>>;

type PeersByIp = AllowedIps<tokio::sync::mpsc::UnboundedSender<crate::event::Event>>;
type ActivePeers = DashMap<SocketAddr, mpsc::UnboundedSender<Event>>;
type PeersByIp = NetworkTable<mpsc::UnboundedSender<Event>>;

#[cfg(target_os = "linux")]
pub async fn start_wireguard(
task_client: TaskClient,
) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
use std::sync::Arc;

// The set of active tunnels indexed by the peer's address
let active_peers = std::sync::Arc::new(ActivePeers::new());
let peers_by_ip = std::sync::Arc::new(std::sync::Mutex::new(AllowedIps::new()));
let active_peers = Arc::new(ActivePeers::new());
let peers_by_ip = Arc::new(std::sync::Mutex::new(NetworkTable::new()));

// Start the tun device that is used to relay traffic outbound
let tun_task_tx = tun_device::start_tun_device(peers_by_ip.clone());
Expand Down
12 changes: 12 additions & 0 deletions common/wireguard/src/network_table.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#[derive(Default)]
pub(crate) struct NetworkTable<T> {
pub ips: ip_network_table::IpNetworkTable<T>,
}

impl<T> NetworkTable<T> {
pub(crate) fn new() -> Self {
Self {
ips: ip_network_table::IpNetworkTable::new(),
}
}
}
6 changes: 3 additions & 3 deletions common/wireguard/src/wg_tunnel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use tokio::{
time::timeout,
};

use crate::{error::WgError, event::Event, AllowedIps};
use crate::{error::WgError, event::Event, NetworkTable};

const MAX_PACKET: usize = 65535;

Expand All @@ -29,7 +29,7 @@ pub struct WireGuardTunnel {
endpoint: Arc<tokio::sync::RwLock<SocketAddr>>,

// AllowedIPs for this peer
allowed_ips: AllowedIps<()>,
allowed_ips: NetworkTable<()>,

// `boringtun` tunnel, used for crypto & WG protocol
wg_tunnel: Arc<tokio::sync::Mutex<Tunn>>,
Expand Down Expand Up @@ -85,7 +85,7 @@ impl WireGuardTunnel {
// Signal close tunnel
let (close_tx, close_rx) = broadcast::channel(1);

let mut allowed_ips = AllowedIps::new();
let mut allowed_ips = NetworkTable::new();
allowed_ips.ips.insert(peer_allowed_ips, ());

let tunnel = WireGuardTunnel {
Expand Down

0 comments on commit f53e7fb

Please sign in to comment.