wireguard-control(-sys): add FFI upgrade script and run it

pull/178/head
Jake McGinty 2021-11-21 15:46:09 +09:00
parent b5ebb8e8d7
commit 55c3a18d00
4 changed files with 68 additions and 40 deletions

View File

@ -539,70 +539,55 @@ impl ::std::ops::BitAndAssign for wg_peer_flags {
pub struct wg_peer_flags(pub ::std::os::raw::c_uint);
#[repr(C)]
#[derive(Copy, Clone)]
pub struct wg_peer {
pub flags: wg_peer_flags,
pub public_key: wg_key,
pub preshared_key: wg_key,
pub endpoint: wg_peer__bindgen_ty_1,
pub last_handshake_time: timespec64,
pub rx_bytes: u64,
pub tx_bytes: u64,
pub persistent_keepalive_interval: u16,
pub first_allowedip: *mut wg_allowedip,
pub last_allowedip: *mut wg_allowedip,
pub next_peer: *mut wg_peer,
}
#[repr(C)]
#[derive(Copy, Clone)]
pub union wg_peer__bindgen_ty_1 {
pub union wg_endpoint {
pub addr: sockaddr,
pub addr4: sockaddr_in,
pub addr6: sockaddr_in6,
}
#[test]
fn bindgen_test_layout_wg_peer__bindgen_ty_1() {
fn bindgen_test_layout_wg_endpoint() {
assert_eq!(
::std::mem::size_of::<wg_peer__bindgen_ty_1>(),
::std::mem::size_of::<wg_endpoint>(),
28usize,
concat!("Size of: ", stringify!(wg_peer__bindgen_ty_1))
concat!("Size of: ", stringify!(wg_endpoint))
);
assert_eq!(
::std::mem::align_of::<wg_peer__bindgen_ty_1>(),
::std::mem::align_of::<wg_endpoint>(),
4usize,
concat!("Alignment of ", stringify!(wg_peer__bindgen_ty_1))
concat!("Alignment of ", stringify!(wg_endpoint))
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<wg_peer__bindgen_ty_1>())).addr as *const _ as usize },
unsafe { &(*(::std::ptr::null::<wg_endpoint>())).addr as *const _ as usize },
0usize,
concat!(
"Offset of field: ",
stringify!(wg_peer__bindgen_ty_1),
stringify!(wg_endpoint),
"::",
stringify!(addr)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<wg_peer__bindgen_ty_1>())).addr4 as *const _ as usize },
unsafe { &(*(::std::ptr::null::<wg_endpoint>())).addr4 as *const _ as usize },
0usize,
concat!(
"Offset of field: ",
stringify!(wg_peer__bindgen_ty_1),
stringify!(wg_endpoint),
"::",
stringify!(addr4)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<wg_peer__bindgen_ty_1>())).addr6 as *const _ as usize },
unsafe { &(*(::std::ptr::null::<wg_endpoint>())).addr6 as *const _ as usize },
0usize,
concat!(
"Offset of field: ",
stringify!(wg_peer__bindgen_ty_1),
stringify!(wg_endpoint),
"::",
stringify!(addr6)
)
);
}
impl Default for wg_peer__bindgen_ty_1 {
impl Default for wg_endpoint {
fn default() -> Self {
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
unsafe {
@ -611,11 +596,26 @@ impl Default for wg_peer__bindgen_ty_1 {
}
}
}
impl ::std::fmt::Debug for wg_peer__bindgen_ty_1 {
impl ::std::fmt::Debug for wg_endpoint {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
write!(f, "wg_peer__bindgen_ty_1 {{ union }}")
write!(f, "wg_endpoint {{ union }}")
}
}
#[repr(C)]
#[derive(Copy, Clone)]
pub struct wg_peer {
pub flags: wg_peer_flags,
pub public_key: wg_key,
pub preshared_key: wg_key,
pub endpoint: wg_endpoint,
pub last_handshake_time: timespec64,
pub rx_bytes: u64,
pub tx_bytes: u64,
pub persistent_keepalive_interval: u16,
pub first_allowedip: *mut wg_allowedip,
pub last_allowedip: *mut wg_allowedip,
pub next_peer: *mut wg_peer,
}
#[test]
fn bindgen_test_layout_wg_peer() {
assert_eq!(

View File

@ -40,17 +40,19 @@ enum wg_peer_flags {
WGPEER_HAS_PERSISTENT_KEEPALIVE_INTERVAL = 1U << 4
};
typedef union wg_endpoint {
struct sockaddr addr;
struct sockaddr_in addr4;
struct sockaddr_in6 addr6;
} wg_endpoint;
typedef struct wg_peer {
enum wg_peer_flags flags;
wg_key public_key;
wg_key preshared_key;
union {
struct sockaddr addr;
struct sockaddr_in addr4;
struct sockaddr_in6 addr6;
} endpoint;
wg_endpoint endpoint;
struct timespec64 last_handshake_time;
uint64_t rx_bytes, tx_bytes;

View File

@ -0,0 +1,26 @@
#!/bin/bash -e
# This script modified from https://github.com/rusqlite/rusqlite/blob/master/libsqlite3-sys/upgrade.sh
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
CUR_DIR=$(pwd -P)
echo "$SCRIPT_DIR"
cd "$SCRIPT_DIR" || { echo "fatal error" >&2; exit 1; }
cargo clean
mkdir -p "$SCRIPT_DIR/../target"
pushd "$SCRIPT_DIR/c"
curl -O https://raw.githubusercontent.com/WireGuard/wireguard-tools/master/contrib/embeddable-wg-library/wireguard.c
curl -O https://raw.githubusercontent.com/WireGuard/wireguard-tools/master/contrib/embeddable-wg-library/wireguard.h
popd
# Regenerate bindgen file
rm -f "bindgen-bindings/bindings.rs"
# Just to make sure there is only one bindgen.rs file in target dir
find "$SCRIPT_DIR/../target" -type f -name bindings.rs -exec rm {} \;
cargo build --features "buildtime_bindgen"
find "$SCRIPT_DIR/../target" -type f -name bindings.rs -exec mv {} "$SCRIPT_DIR/bindgen-bindings/bindings.rs" \;
# Sanity checks
cd "$SCRIPT_DIR" || { echo "fatal error" >&2; exit 1; }
cargo test
echo 'You should increment the version in Cargo.toml'

View File

@ -141,7 +141,7 @@ fn parse_allowed_ips(peer: &wireguard_control_sys::wg_peer) -> Vec<AllowedIp> {
result
}
fn parse_endpoint(endpoint: &wireguard_control_sys::wg_peer__bindgen_ty_1) -> Option<SocketAddr> {
fn parse_endpoint(endpoint: &wireguard_control_sys::wg_endpoint) -> Option<SocketAddr> {
let addr = unsafe { endpoint.addr };
match i32::from(addr.sa_family) {
libc::AF_INET => {
@ -205,10 +205,10 @@ fn encode_allowedips(
(first_ip, last_ip)
}
fn encode_endpoint(endpoint: Option<SocketAddr>) -> wireguard_control_sys::wg_peer__bindgen_ty_1 {
fn encode_endpoint(endpoint: Option<SocketAddr>) -> wireguard_control_sys::wg_endpoint {
match endpoint {
Some(SocketAddr::V4(s)) => {
let mut peer = wireguard_control_sys::wg_peer__bindgen_ty_1::default();
let mut peer = wireguard_control_sys::wg_endpoint::default();
peer.addr4 = wireguard_control_sys::sockaddr_in {
sin_family: libc::AF_INET as u16,
sin_addr: wireguard_control_sys::in_addr {
@ -220,7 +220,7 @@ fn encode_endpoint(endpoint: Option<SocketAddr>) -> wireguard_control_sys::wg_pe
peer
},
Some(SocketAddr::V6(s)) => {
let mut peer = wireguard_control_sys::wg_peer__bindgen_ty_1::default();
let mut peer = wireguard_control_sys::wg_endpoint::default();
let in6_addr = wireguard_control_sys::in6_addr__bindgen_ty_1 {
__u6_addr8: s.ip().octets(),
};
@ -233,7 +233,7 @@ fn encode_endpoint(endpoint: Option<SocketAddr>) -> wireguard_control_sys::wg_pe
};
peer
},
None => wireguard_control_sys::wg_peer__bindgen_ty_1::default(),
None => wireguard_control_sys::wg_endpoint::default(),
}
}