aboutsummaryrefslogtreecommitdiffstats
path: root/src/wireguard.rs
diff options
context:
space:
mode:
authorMathias Hall-Andersen <mathias@hall-andersen.dk>2019-09-14 12:43:09 +0200
committerMathias Hall-Andersen <mathias@hall-andersen.dk>2019-09-14 12:43:09 +0200
commitb31becda71feace70f96043cd39bbe022a054225 (patch)
treea632ea4bee35d70cf21dd2137120402203cf47f3 /src/wireguard.rs
parentTest for confirmation of key using staged packet (diff)
downloadwireguard-rs-b31becda71feace70f96043cd39bbe022a054225.tar.xz
wireguard-rs-b31becda71feace70f96043cd39bbe022a054225.zip
Begin work on the pure Wireguard implemenation
Start joining the handshake device and router device in the top-level Wireguard implemenation.
Diffstat (limited to 'src/wireguard.rs')
-rw-r--r--src/wireguard.rs75
1 files changed, 75 insertions, 0 deletions
diff --git a/src/wireguard.rs b/src/wireguard.rs
new file mode 100644
index 0000000..0bd5da7
--- /dev/null
+++ b/src/wireguard.rs
@@ -0,0 +1,75 @@
+use crate::handshake;
+use crate::router;
+use crate::types::{Bind, Tun};
+
+use byteorder::{ByteOrder, LittleEndian};
+
+use std::thread;
+
+use x25519_dalek::StaticSecret;
+
+pub struct Timers {}
+
+pub struct Events();
+
+impl router::Callbacks for Events {
+ type Opaque = Timers;
+
+ fn send(t: &Timers, size: usize, data: bool, sent: bool) {}
+
+ fn recv(t: &Timers, size: usize, data: bool, sent: bool) {}
+
+ fn need_key(t: &Timers) {}
+}
+
+pub struct Wireguard<T: Tun, B: Bind> {
+ router: router::Device<Events, T, B>,
+ handshake: Option<handshake::Device<()>>,
+}
+
+impl<T: Tun, B: Bind> Wireguard<T, B> {
+ fn new(tun: T, bind: B) -> Wireguard<T, B> {
+ let router = router::Device::new(num_cpus::get(), tun.clone(), bind.clone());
+
+ // start UDP read IO thread
+ {
+ let tun = tun.clone();
+ thread::spawn(move || {
+ loop {
+ // read UDP packet into vector
+ let size = tun.mtu() + 148; // maximum message size
+ let mut msg: Vec<u8> =
+ Vec::with_capacity(size + router::CAPACITY_MESSAGE_POSTFIX);
+ msg.resize(size, 0);
+ let (size, src) = bind.recv(&mut msg).unwrap(); // TODO handle error
+ msg.truncate(size);
+
+ // message type de-multiplexer
+ if msg.len() < 4 {
+ continue;
+ }
+ match LittleEndian::read_u32(&msg[..]) {
+ handshake::TYPE_COOKIE_REPLY
+ | handshake::TYPE_INITIATION
+ | handshake::TYPE_RESPONSE => {
+ // handshake message
+ }
+ router::TYPE_TRANSPORT => {
+ // transport message
+ }
+ _ => (),
+ }
+ }
+ });
+ }
+
+ // start TUN read IO thread
+
+ thread::spawn(move || {});
+
+ Wireguard {
+ router,
+ handshake: None,
+ }
+ }
+}