wireguard-control: fix iface attr not being in first message

pull/186/head
Jake McGinty 2022-01-09 23:11:20 -06:00
parent 68d3b09f3a
commit a122077473
1 changed files with 11 additions and 11 deletions

View File

@ -251,16 +251,17 @@ struct ApplyPayload {
impl ApplyPayload { impl ApplyPayload {
fn new(iface: &InterfaceName) -> Self { fn new(iface: &InterfaceName) -> Self {
let iface_str = iface.as_str_lossy().to_string();
Self { Self {
iface: iface.as_str_lossy().to_string(), iface: iface_str.clone(),
nlas: vec![], nlas: vec![WgDeviceAttrs::IfName(iface_str)],
messages: vec![], messages: vec![],
current_buffer_len: 0, current_buffer_len: 0,
} }
} }
fn flush_nlas(&mut self) { fn flush_nlas(&mut self) {
// cleanup: clear out any empty peer lists. // // cleanup: clear out any empty peer lists.
self.nlas self.nlas
.retain(|nla| !matches!(nla, WgDeviceAttrs::Peers(peers) if peers.len() == 0)); .retain(|nla| !matches!(nla, WgDeviceAttrs::Peers(peers) if peers.len() == 0));
@ -268,10 +269,11 @@ impl ApplyPayload {
self.current_buffer_len = name.buffer_len(); self.current_buffer_len = name.buffer_len();
if !self.nlas.is_empty() { if !self.nlas.is_empty() {
self.messages.push(GenlMessage::from_payload(Wireguard { let message = GenlMessage::from_payload(Wireguard {
cmd: WireguardCmd::SetDevice, cmd: WireguardCmd::SetDevice,
nlas: std::mem::replace(&mut self.nlas, vec![name]), nlas: std::mem::replace(&mut self.nlas, vec![name]),
})); });
self.messages.push(message);
} }
} }
@ -293,12 +295,10 @@ impl ApplyPayload {
.iter() .iter()
.any(|nla| matches!(nla, WgDeviceAttrs::Peers(_))); .any(|nla| matches!(nla, WgDeviceAttrs::Peers(_)));
let peer_buffer_len = peer.as_slice().buffer_len() + 4; let peer_buffer_len = peer.as_slice().buffer_len() + 4;
let additional_buffer_len = peer_buffer_len let mut additional_buffer_len = peer_buffer_len;
+ if needs_peer_nla { if needs_peer_nla {
EMPTY_PEERS.buffer_len() additional_buffer_len += EMPTY_PEERS.buffer_len();
} else { }
0
};
if (self.current_buffer_len + additional_buffer_len) > MAX_GENL_PAYLOAD_LENGTH { if (self.current_buffer_len + additional_buffer_len) > MAX_GENL_PAYLOAD_LENGTH {
self.flush_nlas(); self.flush_nlas();
needs_peer_nla = true; needs_peer_nla = true;