From b09d12225bf224a5aa295300b786cb529e53a44c Mon Sep 17 00:00:00 2001 From: Jake McGinty Date: Tue, 18 Jan 2022 15:12:22 +0900 Subject: [PATCH] wireguard-control: gracefully handle multiple Peers NLAs Fixes #187 --- wireguard-control/src/backends/kernel.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/wireguard-control/src/backends/kernel.rs b/wireguard-control/src/backends/kernel.rs index 4cf3baa..662fc73 100644 --- a/wireguard-control/src/backends/kernel.rs +++ b/wireguard-control/src/backends/kernel.rs @@ -141,10 +141,12 @@ impl<'a> TryFrom<&'a [WgDeviceAttrs]> for Device { let private_key = get_nla_value!(nlas, WgDeviceAttrs, PrivateKey).map(|key| Key(*key)); let listen_port = get_nla_value!(nlas, WgDeviceAttrs, ListenPort).cloned(); let fwmark = get_nla_value!(nlas, WgDeviceAttrs, Fwmark).cloned(); - let peers = get_nla_value!(nlas, WgDeviceAttrs, Peers) - .cloned() - .unwrap_or_default() - .into_iter() + let peers = nlas.iter() + .filter_map(|nla| match nla { + WgDeviceAttrs::Peers(peers) => Some(peers.clone()), + _ => None + }) + .flatten() .map(PeerInfo::try_from) .collect::, _>>()?; Ok(Device { @@ -385,7 +387,7 @@ pub fn get_by_name(name: &InterfaceName) -> Result { log::debug!( "get_by_name: parsed wireguard device {} with {} peer(s)", device.name, - device.peers.len() + device.peers.len(), ); Ok(device) }