From 0d50532c9f70a2b1fc89b5bb3fba1f2303ce0eed Mon Sep 17 00:00:00 2001 From: Jake McGinty Date: Fri, 26 Nov 2021 00:41:39 +0900 Subject: [PATCH] add temporary debug printlns --- wireguard-control-sys/c/wireguard.c | 13 +++++++++++-- wireguard-control/src/backends/kernel.rs | 10 +++++----- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/wireguard-control-sys/c/wireguard.c b/wireguard-control-sys/c/wireguard.c index 4941549..ae2ce94 100644 --- a/wireguard-control-sys/c/wireguard.c +++ b/wireguard-control-sys/c/wireguard.c @@ -1330,6 +1330,7 @@ static int parse_peers(const struct nlattr *attr, void *data) if (!ret) return ret; if (!(new_peer->flags & WGPEER_HAS_PUBLIC_KEY)) { + printf("netlink validity error: peer doesn't have public key (required)"); errno = ENXIO; return MNL_CB_ERROR; } @@ -1415,11 +1416,14 @@ int wg_get_device(wg_device **device, const char *device_name) try_again: *device = calloc(1, sizeof(wg_device)); - if (!*device) + if (!*device) { + printf("failed to calloc device struct"); return -errno; + } nlg = mnlg_socket_open(WG_GENL_NAME, WG_GENL_VERSION); if (!nlg) { + printf("failed to open netlink socket"); wg_free_device(*device); *device = NULL; return -errno; @@ -1428,11 +1432,13 @@ try_again: nlh = mnlg_msg_prepare(nlg, WG_CMD_GET_DEVICE, NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP); mnl_attr_put_strz(nlh, WGDEVICE_A_IFNAME, device_name); if (mnlg_socket_send(nlg, nlh) < 0) { + printf("failed to send netlink request"); ret = -errno; goto out; } errno = 0; if (mnlg_socket_recv_run(nlg, read_device_cb, *device) < 0) { + printf("failed to receive netlink response"); ret = errno ? -errno : -EINVAL; goto out; } @@ -1442,9 +1448,12 @@ out: if (nlg) mnlg_socket_close(nlg); if (ret) { + printf("out: ret was non-zero (%d)", ret); wg_free_device(*device); - if (ret == -EINTR) + if (ret == -EINTR) { + printf("out: trying again"); goto try_again; + } *device = NULL; } errno = -ret; diff --git a/wireguard-control/src/backends/kernel.rs b/wireguard-control/src/backends/kernel.rs index 5a04d5f..889e25d 100644 --- a/wireguard-control/src/backends/kernel.rs +++ b/wireguard-control/src/backends/kernel.rs @@ -243,6 +243,7 @@ fn encode_peers( *mut wireguard_control_sys::wg_peer, *mut wireguard_control_sys::wg_peer, ) { + println!("encoding {} peers", peers.len()); let mut first_peer = ptr::null_mut(); let mut last_peer: *mut wireguard_control_sys::wg_peer = ptr::null_mut(); @@ -253,10 +254,7 @@ fn encode_peers( public_key: peer.public_key.0, preshared_key: wireguard_control_sys::wg_key::default(), endpoint: encode_endpoint(peer.endpoint), - last_handshake_time: timespec64 { - tv_sec: 0, - tv_nsec: 0, - }, + last_handshake_time: timespec64::default(), tx_bytes: 0, rx_bytes: 0, persistent_keepalive_interval: 0, @@ -396,7 +394,9 @@ pub fn get_by_name(name: &InterfaceName) -> Result { let result = if ret == 0 && !device.is_null() { Ok(Device::from(unsafe { &*device })) } else { - Err(io::Error::last_os_error()) + let last_error = io::Error::last_os_error(); + println!("FFI ret code was {}, &device is {:p}, last OS error: {:?}", ret, device, last_error.raw_os_error()); + Err(last_error) }; unsafe { wireguard_control_sys::wg_free_device(device) };