wireguard-control: correctness fix in ApplyPayload

fixes #188
pull/186/head
Jake McGinty 2022-01-18 15:55:19 +09:00
parent b53376b9c8
commit 9829b4b3e9
1 changed files with 13 additions and 10 deletions

View File

@ -250,17 +250,19 @@ struct ApplyPayload {
iface: String,
nlas: Vec<WgDeviceAttrs>,
current_buffer_len: usize,
messages: Vec<GenlMessage<Wireguard>>,
queue: Vec<GenlMessage<Wireguard>>,
}
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<GenlMessage<Wireguard>> {
self.flush_nlas();
self.messages
self.queue
}
}