From 170f612ad28b0068934f8fd9db5c692f332048bc Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Tue, 21 May 2024 14:05:33 +0200 Subject: [PATCH] parse interface announce message --- iroh-net/src/net/interfaces/bsd.rs | 45 +++++++++++++++++++++++++++++- iroh-net/src/net/netmon/bsd.rs | 1 + 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/iroh-net/src/net/interfaces/bsd.rs b/iroh-net/src/net/interfaces/bsd.rs index cc6f5a7574a..072ee254955 100644 --- a/iroh-net/src/net/interfaces/bsd.rs +++ b/iroh-net/src/net/interfaces/bsd.rs @@ -232,6 +232,7 @@ pub enum WireMessage { Interface(InterfaceMessage), InterfaceAddr(InterfaceAddrMessage), InterfaceMulticastAddr(InterfaceMulticastAddrMessage), + InterfaceAnnounce(InterfaceAnnounceMessage), } /// Safely convert a some bytes from a slice into a u16. @@ -374,7 +375,34 @@ impl WireFormat { Ok(Some(WireMessage::InterfaceMulticastAddr(m))) } MessageType::InterfaceAnnounce => { - todo!() + if data.len() < self.body_off { + return Err(RouteError::MessageTooShort); + } + let l = u16_from_ne_range(data, ..2)?; + if data.len() < l as usize { + return Err(RouteError::InvalidMessage); + } + + let mut name = String::new(); + for i in 0..16 { + if data[6 + i] != 0 { + continue; + } + name = std::str::from_utf8(&data[6..6 + i]) + .map_err(|_| RouteError::InvalidAddress)? + .to_string(); + break; + } + + let m = InterfaceAnnounceMessage { + version: data[2] as _, + r#type: data[3] as _, + index: u16_from_ne_range(data, 4..6)? as _, + what: u16_from_ne_range(data, 22..24)? as _, + name, + }; + + Ok(Some(WireMessage::InterfaceAnnounce(m))) } } } @@ -545,6 +573,21 @@ pub struct InterfaceMulticastAddrMessage { pub addrs: Vec, } +/// Interface announce message. +#[derive(Debug)] +pub struct InterfaceAnnounceMessage { + /// message version + pub version: isize, + /// message type + pub r#type: isize, + /// interface index + pub index: isize, + /// interface name + pub name: String, + /// what type of announcement + pub what: isize, +} + /// Represents a type of routing information base. type RIBType = i32; diff --git a/iroh-net/src/net/netmon/bsd.rs b/iroh-net/src/net/netmon/bsd.rs index 0956f5337f0..aa3bc7c47a3 100644 --- a/iroh-net/src/net/netmon/bsd.rs +++ b/iroh-net/src/net/netmon/bsd.rs @@ -93,6 +93,7 @@ pub(super) fn is_interesting_message(msg: &WireMessage) -> bool { true } + WireMessage::InterfaceAnnounce(_) => false, } }