-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wireguard: add packet relayer (#4032)
* wip * wip: first step in putting in place forward channels * Setup event loop for packet relayer * tuntaskresponse * wip * tun task response channel * Update comment * done * formatting * nits * Add comment
- Loading branch information
Showing
6 changed files
with
196 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use std::{collections::HashMap, sync::Arc}; | ||
|
||
use tap::TapFallible; | ||
use tokio::sync::mpsc::{self}; | ||
|
||
use crate::{ | ||
event::Event, | ||
tun_task_channel::{TunTaskResponseRx, TunTaskTx}, | ||
}; | ||
|
||
// The tunnels send packets to the packet relayer, which then relays it to the tun device. And | ||
// conversely, it's where the tun device send responses to, which are relayed back to the correct | ||
// tunnel. | ||
pub(crate) struct PacketRelayer { | ||
// Receive packets from the various tunnels | ||
packet_rx: mpsc::Receiver<(u64, Vec<u8>)>, | ||
|
||
// After receive from tunnels, send to the tun device | ||
tun_task_tx: TunTaskTx, | ||
|
||
// Receive responses from the tun device | ||
tun_task_response_rx: TunTaskResponseRx, | ||
|
||
// After receiving from the tun device, relay back to the correct tunnel | ||
peers_by_tag: Arc<std::sync::Mutex<HashMap<u64, mpsc::UnboundedSender<Event>>>>, | ||
} | ||
|
||
impl PacketRelayer { | ||
pub(crate) fn new( | ||
tun_task_tx: TunTaskTx, | ||
tun_task_response_rx: TunTaskResponseRx, | ||
peers_by_tag: Arc<std::sync::Mutex<HashMap<u64, mpsc::UnboundedSender<Event>>>>, | ||
) -> (Self, mpsc::Sender<(u64, Vec<u8>)>) { | ||
let (packet_tx, packet_rx) = mpsc::channel(16); | ||
( | ||
Self { | ||
packet_rx, | ||
tun_task_tx, | ||
tun_task_response_rx, | ||
peers_by_tag, | ||
}, | ||
packet_tx, | ||
) | ||
} | ||
|
||
pub(crate) async fn run(mut self) { | ||
loop { | ||
tokio::select! { | ||
Some((tag, packet)) = self.packet_rx.recv() => { | ||
log::info!("Sent packet to tun device with tag: {tag}"); | ||
self.tun_task_tx.send((tag, packet)).unwrap(); | ||
}, | ||
Some((tag, packet)) = self.tun_task_response_rx.recv() => { | ||
log::info!("Received response from tun device with tag: {tag}"); | ||
self.peers_by_tag.lock().unwrap().get(&tag).and_then(|tx| { | ||
tx.send(Event::Ip(packet.into())).tap_err(|e| log::error!("{e}")).ok() | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub(crate) fn start(self) { | ||
tokio::spawn(async move { self.run().await }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.