From 46f2a1e24c4089f1da518c43e3c088f971c7c819 Mon Sep 17 00:00:00 2001 From: Jake McGinty Date: Tue, 11 May 2021 13:52:06 +0900 Subject: [PATCH] wgctrl-rs: fix ipv6 endpoint encoding (thanks @mk-fg) Previously, it remained the `Default::default()` value. Added test to validate correctness --- wgctrl-rs/src/backends/kernel.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/wgctrl-rs/src/backends/kernel.rs b/wgctrl-rs/src/backends/kernel.rs index c87acbb..23392fa 100644 --- a/wgctrl-rs/src/backends/kernel.rs +++ b/wgctrl-rs/src/backends/kernel.rs @@ -223,14 +223,14 @@ fn encode_endpoint(endpoint: Option) -> wgctrl_sys::wg_peer__bindgen }, Some(SocketAddr::V6(s)) => { let mut peer = wgctrl_sys::wg_peer__bindgen_ty_1::default(); + let in6_addr = wgctrl_sys::in6_addr__bindgen_ty_1{__u6_addr8: s.ip().octets()}; peer.addr6 = wgctrl_sys::sockaddr_in6 { sin6_family: libc::AF_INET6 as u16, - sin6_addr: Default::default(), + sin6_addr: wgctrl_sys::in6_addr{__in6_u: in6_addr}, sin6_port: u16::to_be(s.port()), sin6_flowinfo: 0, sin6_scope_id: 0, }; - unsafe { peer.addr6 }.sin6_addr.__in6_u.__u6_addr8 = s.ip().octets(); peer }, None => wgctrl_sys::wg_peer__bindgen_ty_1::default(), @@ -524,3 +524,19 @@ impl Key { Self::from_base64(&base64::encode(&bytes)) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_encode_endpoint() -> Result<(), Box> { + let endpoint = Some("1.2.3.4:51820".parse()?); + let endpoint6: Option = Some("[2001:db8:1::1]:51820".parse()?); + let encoded = encode_endpoint(endpoint); + let encoded6 = encode_endpoint(endpoint6); + assert_eq!(endpoint, parse_endpoint(&encoded)); + assert_eq!(endpoint6, parse_endpoint(&encoded6)); + Ok(()) + } +} \ No newline at end of file