From 9829b4b3e923e07421458a646b520946f85932ff Mon Sep 17 00:00:00 2001 From: Jake McGinty Date: Tue, 18 Jan 2022 15:55:19 +0900 Subject: [PATCH] wireguard-control: correctness fix in ApplyPayload fixes #188 --- wireguard-control/src/backends/kernel.rs | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/wireguard-control/src/backends/kernel.rs b/wireguard-control/src/backends/kernel.rs index 5204a66..c6840ee 100644 --- a/wireguard-control/src/backends/kernel.rs +++ b/wireguard-control/src/backends/kernel.rs @@ -250,17 +250,19 @@ struct ApplyPayload { iface: String, nlas: Vec, current_buffer_len: usize, - messages: Vec>, + queue: Vec>, } impl ApplyPayload { fn new(iface: &InterfaceName) -> Self { let iface_str = iface.as_str_lossy().to_string(); + let nlas = vec![WgDeviceAttrs::IfName(iface_str.clone())]; + let current_buffer_len = nlas.as_slice().buffer_len(); Self { - iface: iface_str.clone(), - nlas: vec![WgDeviceAttrs::IfName(iface_str)], - messages: vec![], - current_buffer_len: 0, + iface: iface_str, + nlas, + queue: vec![], + current_buffer_len, } } @@ -270,14 +272,15 @@ impl ApplyPayload { .retain(|nla| !matches!(nla, WgDeviceAttrs::Peers(peers) if peers.is_empty())); let name = WgDeviceAttrs::IfName(self.iface.clone()); - self.current_buffer_len = name.buffer_len(); + let template = vec![name]; - if !self.nlas.is_empty() { + if !self.nlas.is_empty() && self.nlas != template { + self.current_buffer_len = template.as_slice().buffer_len(); let message = GenlMessage::from_payload(Wireguard { cmd: WireguardCmd::SetDevice, - nlas: std::mem::replace(&mut self.nlas, vec![name]), + nlas: std::mem::replace(&mut self.nlas, template), }); - self.messages.push(message); + self.queue.push(message); } } @@ -352,7 +355,7 @@ impl ApplyPayload { pub fn finish(mut self) -> Vec> { self.flush_nlas(); - self.messages + self.queue } }