aboutsummaryrefslogtreecommitdiffstats
path: root/src/types.rs
blob: 77a104d98c60857ea8de953469e406c070916cf0 (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
32
33
34
35
36
37
38
use base64;
use std::fmt::{self, Display, Formatter};
use std::net::IpAddr;
use std::time::Duration;
use udp::Endpoint;

#[derive(Clone, Debug, Default)]
pub struct PeerInfo {
    pub pub_key: [u8; 32],
    pub psk: Option<[u8; 32]>,
    pub endpoint: Option<Endpoint>,
    pub allowed_ips: Vec<(IpAddr, u32)>,
    pub keepalive: Option<u16>,
}

impl PeerInfo {
    pub fn persistent_keepalive(&self) -> Option<Duration> {
        match self.keepalive {
            Some(keepalive) if keepalive > 0 => Some(Duration::from_secs(u64::from(keepalive))),
            _ => None
        }
    }
}

impl Display for PeerInfo {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        let encoded = base64::encode(&self.pub_key);
        write!(f, "{}...{}", &encoded[..4], &encoded[encoded.len()-4..])
    }
}

#[derive(Clone, Debug, Default)]
pub struct InterfaceInfo {
    pub private_key: Option<[u8; 32]>,
    pub pub_key: Option<[u8; 32]>,
    pub listen_port: Option<u16>,
    pub fwmark: Option<u32>,
}