summaryrefslogtreecommitdiffstats
path: root/src/platform/linux/uapi.rs
blob: fdf2bf06f79d4e5449f16379bef18fb3ff1ebfa8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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)
    }
}