summaryrefslogtreecommitdiffstats
path: root/src/platform
diff options
context:
space:
mode:
authorMathias Hall-Andersen <mathias@hall-andersen.dk>2019-11-15 15:32:36 +0100
committerMathias Hall-Andersen <mathias@hall-andersen.dk>2019-11-15 15:32:36 +0100
commit05710c455f1c759cf9bc1a1eaa6307fe564f15cc (patch)
treec671d703d0db93a9bc8f27d0e2b02d0422120352 /src/platform
parentInitial version of full UAPI parser (diff)
downloadwireguard-rs-05710c455f1c759cf9bc1a1eaa6307fe564f15cc.tar.xz
wireguard-rs-05710c455f1c759cf9bc1a1eaa6307fe564f15cc.zip
Update UAPI semantics for remove
Diffstat (limited to 'src/platform')
-rw-r--r--src/platform/bind.rs2
-rw-r--r--src/platform/dummy/bind.rs2
-rw-r--r--src/platform/dummy/tun.rs2
-rw-r--r--src/platform/linux/mod.rs6
-rw-r--r--src/platform/linux/tun.rs2
-rw-r--r--src/platform/linux/uapi.rs31
-rw-r--r--src/platform/linux/udp.rs76
-rw-r--r--src/platform/mod.rs5
-rw-r--r--src/platform/tun.rs2
-rw-r--r--src/platform/uapi.rs16
10 files changed, 125 insertions, 19 deletions
diff --git a/src/platform/bind.rs b/src/platform/bind.rs
index 1a234c7..1055f37 100644
--- a/src/platform/bind.rs
+++ b/src/platform/bind.rs
@@ -37,7 +37,7 @@ pub trait Owner: Send {
/// On some platforms the application can itself bind to a socket.
/// This enables configuration using the UAPI interface.
-pub trait Platform: Bind {
+pub trait PlatformBind: Bind {
type Owner: Owner;
/// Bind to a new port, returning the reader/writer and
diff --git a/src/platform/dummy/bind.rs b/src/platform/dummy/bind.rs
index 2c30486..b42483a 100644
--- a/src/platform/dummy/bind.rs
+++ b/src/platform/dummy/bind.rs
@@ -216,7 +216,7 @@ impl Owner for VoidOwner {
}
}
-impl Platform for PairBind {
+impl PlatformBind for PairBind {
type Owner = VoidOwner;
fn bind(_port: u16) -> Result<(Vec<Self::Reader>, Self::Writer, Self::Owner), Self::Error> {
Err(BindError::Disconnected)
diff --git a/src/platform/dummy/tun.rs b/src/platform/dummy/tun.rs
index 185b328..569bf1c 100644
--- a/src/platform/dummy/tun.rs
+++ b/src/platform/dummy/tun.rs
@@ -192,7 +192,7 @@ impl TunTest {
}
}
-impl Platform for TunTest {
+impl PlatformTun for TunTest {
fn create(_name: &str) -> Result<(Vec<Self::Reader>, Self::Writer, Self::MTU), Self::Error> {
Err(TunError::Disconnected)
}
diff --git a/src/platform/linux/mod.rs b/src/platform/linux/mod.rs
index 7d6a61c..82731de 100644
--- a/src/platform/linux/mod.rs
+++ b/src/platform/linux/mod.rs
@@ -1,5 +1,7 @@
mod tun;
+mod uapi;
mod udp;
-pub use tun::LinuxTun;
-pub use udp::LinuxBind;
+pub use tun::LinuxTun as Tun;
+pub use uapi::LinuxUAPI as UAPI;
+pub use udp::LinuxBind as Bind;
diff --git a/src/platform/linux/tun.rs b/src/platform/linux/tun.rs
index 090569a..0bbae81 100644
--- a/src/platform/linux/tun.rs
+++ b/src/platform/linux/tun.rs
@@ -125,7 +125,7 @@ impl Tun for LinuxTun {
type MTU = LinuxTunMTU;
}
-impl Platform for LinuxTun {
+impl PlatformTun for LinuxTun {
fn create(name: &str) -> Result<(Vec<Self::Reader>, Self::Writer, Self::MTU), Self::Error> {
// construct request struct
let mut req = Ifreq {
diff --git a/src/platform/linux/uapi.rs b/src/platform/linux/uapi.rs
new file mode 100644
index 0000000..fdf2bf0
--- /dev/null
+++ b/src/platform/linux/uapi.rs
@@ -0,0 +1,31 @@
+use super::super::uapi::*;
+
+use std::fs;
+use std::io;
+use std::os::unix::net::{UnixListener, UnixStream};
+
+const SOCK_DIR: &str = "/var/run/wireguard/";
+
+pub struct LinuxUAPI {}
+
+impl PlatformUAPI for LinuxUAPI {
+ type Error = io::Error;
+ type Bind = UnixListener;
+
+ fn bind(name: &str) -> Result<UnixListener, io::Error> {
+ let socket_path = format!("{}{}.sock", SOCK_DIR, name);
+ let _ = fs::create_dir_all(SOCK_DIR);
+ let _ = fs::remove_file(&socket_path);
+ UnixListener::bind(socket_path)
+ }
+}
+
+impl BindUAPI for UnixListener {
+ type Stream = UnixStream;
+ type Error = io::Error;
+
+ fn accept(&self) -> Result<UnixStream, io::Error> {
+ let (stream, _) = self.accept()?;
+ Ok(stream)
+ }
+}
diff --git a/src/platform/linux/udp.rs b/src/platform/linux/udp.rs
index 52e4c45..d3d61b6 100644
--- a/src/platform/linux/udp.rs
+++ b/src/platform/linux/udp.rs
@@ -1,26 +1,82 @@
use super::super::bind::*;
use super::super::Endpoint;
-use std::net::SocketAddr;
+use std::io;
+use std::net::{SocketAddr, UdpSocket};
+use std::sync::Arc;
-pub struct LinuxEndpoint {}
+#[derive(Clone)]
+pub struct LinuxBind(Arc<UdpSocket>);
-pub struct LinuxBind {}
+pub struct LinuxOwner(Arc<UdpSocket>);
-impl Endpoint for LinuxEndpoint {
+impl Endpoint for SocketAddr {
fn clear_src(&mut self) {}
fn from_address(addr: SocketAddr) -> Self {
- LinuxEndpoint {}
+ addr
}
fn into_address(&self) -> SocketAddr {
- "127.0.0.1:6060".parse().unwrap()
+ *self
}
}
-/*
-impl Bind for PlatformBind {
- type Endpoint = PlatformEndpoint;
+impl Reader<SocketAddr> for LinuxBind {
+ type Error = io::Error;
+
+ fn read(&self, buf: &mut [u8]) -> Result<(usize, SocketAddr), Self::Error> {
+ self.0.recv_from(buf)
+ }
+}
+
+impl Writer<SocketAddr> for LinuxBind {
+ type Error = io::Error;
+
+ fn write(&self, buf: &[u8], dst: &SocketAddr) -> Result<(), Self::Error> {
+ self.0.send_to(buf, dst)?;
+ Ok(())
+ }
+}
+
+impl Owner for LinuxOwner {
+ type Error = io::Error;
+
+ fn get_port(&self) -> u16 {
+ 1337
+ }
+
+ fn get_fwmark(&self) -> Option<u32> {
+ None
+ }
+
+ fn set_fwmark(&mut self, value: Option<u32>) -> Option<Self::Error> {
+ None
+ }
+}
+
+impl Drop for LinuxOwner {
+ fn drop(&mut self) {}
+}
+
+impl Bind for LinuxBind {
+ type Error = io::Error;
+ type Endpoint = SocketAddr;
+ type Reader = LinuxBind;
+ type Writer = LinuxBind;
+}
+
+impl PlatformBind for LinuxBind {
+ type Owner = LinuxOwner;
+
+ fn bind(port: u16) -> Result<(Vec<Self::Reader>, Self::Writer, Self::Owner), Self::Error> {
+ let socket = UdpSocket::bind(format!("0.0.0.0:{}", port))?;
+ let socket = Arc::new(socket);
+
+ Ok((
+ vec![LinuxBind(socket.clone())],
+ LinuxBind(socket.clone()),
+ LinuxOwner(socket),
+ ))
+ }
}
-*/
diff --git a/src/platform/mod.rs b/src/platform/mod.rs
index ecd559a..99707e3 100644
--- a/src/platform/mod.rs
+++ b/src/platform/mod.rs
@@ -2,14 +2,15 @@ mod endpoint;
pub mod bind;
pub mod tun;
+pub mod uapi;
pub use endpoint::Endpoint;
#[cfg(target_os = "linux")]
-mod linux;
+pub mod linux;
#[cfg(test)]
pub mod dummy;
#[cfg(target_os = "linux")]
-pub use linux::LinuxTun as TunInstance;
+pub use linux as plt;
diff --git a/src/platform/tun.rs b/src/platform/tun.rs
index f49d4af..c92304a 100644
--- a/src/platform/tun.rs
+++ b/src/platform/tun.rs
@@ -56,6 +56,6 @@ pub trait Tun: Send + Sync + 'static {
}
/// On some platforms the application can create the TUN device itself.
-pub trait Platform: Tun {
+pub trait PlatformTun: Tun {
fn create(name: &str) -> Result<(Vec<Self::Reader>, Self::Writer, Self::MTU), Self::Error>;
}
diff --git a/src/platform/uapi.rs b/src/platform/uapi.rs
new file mode 100644
index 0000000..6922a9c
--- /dev/null
+++ b/src/platform/uapi.rs
@@ -0,0 +1,16 @@
+use std::error::Error;
+use std::io::{Read, Write};
+
+pub trait BindUAPI {
+ type Stream: Read + Write;
+ type Error: Error;
+
+ fn accept(&self) -> Result<Self::Stream, Self::Error>;
+}
+
+pub trait PlatformUAPI {
+ type Error: Error;
+ type Bind: BindUAPI;
+
+ fn bind(name: &str) -> Result<Self::Bind, Self::Error>;
+}