Skip to content

Commit

Permalink
Move map struc definitions to udp_listener
Browse files Browse the repository at this point in the history
  • Loading branch information
octol committed Oct 6, 2023
1 parent f53e7fb commit 843f1ae
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 19 deletions.
18 changes: 3 additions & 15 deletions common/wireguard/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
#![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;
Expand All @@ -15,24 +8,19 @@ 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;

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,
task_client: nym_task::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 = Arc::new(ActivePeers::new());
let peers_by_ip = Arc::new(std::sync::Mutex::new(NetworkTable::new()));
let active_peers = Arc::new(udp_listener::ActivePeers::new());
let peers_by_ip = Arc::new(std::sync::Mutex::new(network_table::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
2 changes: 1 addition & 1 deletion common/wireguard/src/platform/linux/tun_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use tokio::{
use crate::{
event::Event,
setup::{TUN_BASE_NAME, TUN_DEVICE_ADDRESS, TUN_DEVICE_NETMASK},
PeersByIp,
udp_listener::PeersByIp,
};

fn setup_tokio_tun_device(name: &str, address: Ipv4Addr, netmask: Ipv4Addr) -> tokio_tun::Tun {
Expand Down
11 changes: 9 additions & 2 deletions common/wireguard/src/udp_listener.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
use std::{net::SocketAddr, sync::Arc};

use dashmap::DashMap;
use futures::StreamExt;
use log::error;
use nym_task::TaskClient;
use tap::TapFallible;
use tokio::{net::UdpSocket, sync::mpsc::UnboundedSender};
use tokio::{
net::UdpSocket,
sync::mpsc::{self, UnboundedSender},
};

use crate::{
event::Event,
network_table::NetworkTable,
setup::{self, WG_ADDRESS, WG_PORT},
ActivePeers, PeersByIp,
};

const MAX_PACKET: usize = 65535;

pub(crate) type ActivePeers = DashMap<SocketAddr, mpsc::UnboundedSender<Event>>;
pub(crate) type PeersByIp = NetworkTable<mpsc::UnboundedSender<Event>>;

pub(crate) async fn start_udp_listener(
tun_task_tx: UnboundedSender<Vec<u8>>,
active_peers: Arc<ActivePeers>,
Expand Down
2 changes: 1 addition & 1 deletion 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, NetworkTable};
use crate::{error::WgError, event::Event, network_table::NetworkTable};

const MAX_PACKET: usize = 65535;

Expand Down

0 comments on commit 843f1ae

Please sign in to comment.