aboutsummaryrefslogtreecommitdiffstats
path: root/src/uapi.rs
blob: 85afb91d13e66b52f01d7bcc7d5b03a6dc95dcdf (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//! Mapping to the `WireGuard` user API

#![allow(dead_code)]

use std::convert::{AsMut, AsRef};
use std::fmt::{Debug, Result, Formatter};
use std::marker::PhantomData;
use std::mem::transmute;

use libc::{in_addr, in6_addr, sockaddr, sockaddr_in, sockaddr_in6, timeval};

const IFNAMSIZ: usize = 16;
const WG_KEY_LEN: usize = 32;

#[repr(C)]
#[derive(Clone)]
/// Represents a union field
pub struct UnionField<T>(PhantomData<T>);

impl<T> UnionField<T> {
    /// Creates a new `UnionField`
    pub fn new() -> Self {
        UnionField(PhantomData)
    }
}

impl<T: Clone> Copy for UnionField<T> {}

impl<T> AsRef<T> for UnionField<T> {
    fn as_ref(&self) -> &T {
        unsafe { transmute(self) }
    }
}

impl<T> AsMut<T> for UnionField<T> {
    fn as_mut(&mut self) -> &mut T {
        unsafe { transmute(self) }
    }
}


impl<T> Debug for UnionField<T> {
    fn fmt(&self, fmt: &mut Formatter) -> Result {
        fmt.write_str("Union")
    }
}

#[repr(C)]
#[derive(Debug, Clone, Copy)]
/// A `WireGuard` device
pub struct WgDevice {
    /// The name of the interface
    pub interface: [u8; IFNAMSIZ],

    /// Interface flags
    pub flags: u32,

    /// The `WireGuard` public key
    pub public_key: [u8; WG_KEY_LEN],

    /// The `WireGuard` private key
    pub private_key: [u8; WG_KEY_LEN],

    /// The `WireGuard` pre-shared key
    pub preshared_key: [u8; WG_KEY_LEN],

    /// The wirewall mark
    pub fwmark: u32,

    /// The port of the device
    pub port: u16,

    /// The peers
    pub peers: Peers,
}

#[repr(C)]
#[derive(Debug, Clone, Copy)]
/// `WireGuard` peer union
pub struct Peers {
    /// The number of peers
    pub num_peers: UnionField<u16>,

    /// The overall peer size
    pub peers_size: UnionField<u32>,

    /// The union field size as placeholder
    pub union_size: u32,
}

#[repr(C)]
#[derive(Debug, Clone, Copy)]
/// A `WireGuard` IP mask
pub struct WgIpMask {
    /// The network family
    pub family: i32,

    /// The network address
    pub addr: Addr,

    /// Classless Inter-Domain Routing
    pub cidr: u8,
}

#[repr(C)]
#[derive(Debug, Clone, Copy)]
/// A `WireGuard` network address
pub struct Addr {
    /// IP version 4
    pub ip4: UnionField<in_addr>,

    /// IP version 6
    pub ip6: UnionField<in6_addr>,

    /// The union field size as placeholder
    pub union_size: [u32; 4usize],
}

#[repr(C)]
#[derive(Clone, Copy)]
/// A `WireGuard` peer
pub struct WgPeer {
    /// The public key
    pub public_key: [u8; 32usize],

    /// Set flags for the peer
    pub flags: u32,

    /// The endpoint of the peer
    pub endpoint: WgEndpoint,

    /// Time of the last handshake
    pub last_handshake_time: timeval,

    /// Received bytes
    pub rx_bytes: u64,

    /// Sent bytes
    pub tx_bytes: u64,

    /// The persistent keep alive interval
    pub persistent_keepalive_interval: u16,

    /// The amount of IP masks
    pub num_ipmasks: u16,
}

#[repr(C)]
#[derive(Debug, Clone, Copy)]
/// A `WireGuard` endpoint type
pub struct WgEndpoint {
    /// The socket address
    pub addr: UnionField<sockaddr>,

    /// The IPv4 socket address
    pub addr4: UnionField<sockaddr_in>,

    /// The IPv6 socket address
    pub addr6: UnionField<sockaddr_in6>,

    /// The union field size as placeholder
    pub union_size: [u32; 7usize],
}