From 55c3a18d0044fa953d393cb35163e38e65d1d928 Mon Sep 17 00:00:00 2001 From: Jake McGinty Date: Sun, 21 Nov 2021 15:46:09 +0900 Subject: [PATCH] wireguard-control(-sys): add FFI upgrade script and run it --- .../bindgen-bindings/bindings.rs | 60 +++++++++---------- wireguard-control-sys/c/wireguard.h | 12 ++-- wireguard-control-sys/upgrade.sh | 26 ++++++++ wireguard-control/src/backends/kernel.rs | 10 ++-- 4 files changed, 68 insertions(+), 40 deletions(-) create mode 100755 wireguard-control-sys/upgrade.sh diff --git a/wireguard-control-sys/bindgen-bindings/bindings.rs b/wireguard-control-sys/bindgen-bindings/bindings.rs index e6f404a..a45a3fd 100644 --- a/wireguard-control-sys/bindgen-bindings/bindings.rs +++ b/wireguard-control-sys/bindgen-bindings/bindings.rs @@ -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::(), + ::std::mem::size_of::(), 28usize, - concat!("Size of: ", stringify!(wg_peer__bindgen_ty_1)) + concat!("Size of: ", stringify!(wg_endpoint)) ); assert_eq!( - ::std::mem::align_of::(), + ::std::mem::align_of::(), 4usize, - concat!("Alignment of ", stringify!(wg_peer__bindgen_ty_1)) + concat!("Alignment of ", stringify!(wg_endpoint)) ); assert_eq!( - unsafe { &(*(::std::ptr::null::())).addr as *const _ as usize }, + unsafe { &(*(::std::ptr::null::())).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::())).addr4 as *const _ as usize }, + unsafe { &(*(::std::ptr::null::())).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::())).addr6 as *const _ as usize }, + unsafe { &(*(::std::ptr::null::())).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::::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!( diff --git a/wireguard-control-sys/c/wireguard.h b/wireguard-control-sys/c/wireguard.h index fbd8765..328fcb4 100644 --- a/wireguard-control-sys/c/wireguard.h +++ b/wireguard-control-sys/c/wireguard.h @@ -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; diff --git a/wireguard-control-sys/upgrade.sh b/wireguard-control-sys/upgrade.sh new file mode 100755 index 0000000..4932867 --- /dev/null +++ b/wireguard-control-sys/upgrade.sh @@ -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' \ No newline at end of file diff --git a/wireguard-control/src/backends/kernel.rs b/wireguard-control/src/backends/kernel.rs index c574f03..0e4663e 100644 --- a/wireguard-control/src/backends/kernel.rs +++ b/wireguard-control/src/backends/kernel.rs @@ -141,7 +141,7 @@ fn parse_allowed_ips(peer: &wireguard_control_sys::wg_peer) -> Vec { result } -fn parse_endpoint(endpoint: &wireguard_control_sys::wg_peer__bindgen_ty_1) -> Option { +fn parse_endpoint(endpoint: &wireguard_control_sys::wg_endpoint) -> Option { 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) -> wireguard_control_sys::wg_peer__bindgen_ty_1 { +fn encode_endpoint(endpoint: Option) -> 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) -> 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) -> wireguard_control_sys::wg_pe }; peer }, - None => wireguard_control_sys::wg_peer__bindgen_ty_1::default(), + None => wireguard_control_sys::wg_endpoint::default(), } }