diff --git a/src/bgp/message/nlri.rs b/src/bgp/message/nlri.rs index 32383993..4fdc3b8b 100644 --- a/src/bgp/message/nlri.rs +++ b/src/bgp/message/nlri.rs @@ -771,6 +771,12 @@ impl PathId { } } +impl From for u32 { + fn from(value: PathId) -> Self { + value.0 + } +} + impl fmt::Display for PathId { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.0) diff --git a/src/bgp/message/update.rs b/src/bgp/message/update.rs index 5e5fa477..928b8594 100644 --- a/src/bgp/message/update.rs +++ b/src/bgp/message/update.rs @@ -87,7 +87,7 @@ impl AsRef<[u8]> for UpdateMessage { /// attribute. Similarly, the `NEXT_HOP` path attribute was once mandatory for /// UPDATEs, but is now also part of the `MP_REACH_NLRI`, if present. /// -/// To accomodate for these hassles, the following methods are provided: +/// To accommodate for these hassles, the following methods are provided: /// /// * [`nlris()`][`UpdateMessage::nlris`] and /// [`withdrawals()`][`UpdateMessage::withdrawals`], @@ -320,7 +320,7 @@ impl UpdateMessage { /// will count the Error case, after which the iterator fuses. /// /// To retrieve all announcements if and only if all are validly parsed, - /// consder using `fn announcements_vec`. + /// consider using `fn announcements_vec`. /// /// Note that this iterator might contain NLRI of different AFI/SAFI /// types. @@ -747,7 +747,7 @@ impl UpdateMessage { //--- Communities -------------------------------------------------------- /// Returns an iterator over Standard Communities (RFC1997), if any. - pub fn communities>(&self) + fn _communities>(&self) -> Result, T>>, ParseError> { if let Some(WireformatPathAttribute::Communities(epa)) = self.path_attributes()?.get(PathAttributeType::Communities) { @@ -758,6 +758,14 @@ impl UpdateMessage { } } + pub fn communities(&self) -> Result, crate::bgp::communities::Community>>, ParseError> { + self._communities::() + } + + pub fn human_readable_communities(&self) -> Result, crate::bgp::communities::HumanReadableCommunity>>, ParseError> { + self._communities::() + } + /// Returns an iterator over Extended Communities (RFC4360), if any. pub fn ext_communities(&self) -> Result>>, ParseError> @@ -799,14 +807,14 @@ impl UpdateMessage { /// Returns an optional `Vec` containing all conventional, Extended and /// Large communities, if any, or None if none of the three appear in the /// path attributes of this message. - pub fn all_communities(&self) -> Result>, ParseError> + fn _all_communities(&self) -> Result>, ParseError> where T: From<[u8; 4]> + From + From + From { let mut res: Vec = Vec::new(); - if let Some(c) = self.communities()? { + if let Some(c) = self._communities()? { res.append(&mut c.collect::>()); } if let Some(c) = self.ext_communities()? { @@ -827,6 +835,14 @@ impl UpdateMessage { Ok(Some(res)) } } + + pub fn all_communities(&self) -> Result>, ParseError> { + self._all_communities::() + } + + pub fn all_human_readable_communities(&self) -> Result>, ParseError> { + self._all_communities::() + } } @@ -1413,7 +1429,7 @@ impl<'a, Octs: Octets> Iterator for NlriIter<'a, Octs> { match self.get_nlri() { Ok(n) => Some(Ok(n)), Err(e) => { - // Whenever an error occured, e.g. because the NLRI could not + // Whenever an error occurred, e.g. because the NLRI could not // be parsed, we return the error and 'fuse' the iterator by // advancing the parser, ensuring the next call to `next()` // returns a None. @@ -1968,7 +1984,6 @@ mod tests { #[test] fn pa_communities() { - use crate::bgp::communities::Community; // BGP UPDATE with 9 path attributes for 1 NLRI with Path Id, // includes both normal communities and extended communities. let buf = vec![ @@ -2004,11 +2019,11 @@ mod tests { )) ); - assert!(upd.communities::().unwrap().is_some()); - for c in upd.communities::().unwrap().unwrap() { + assert!(upd.communities().unwrap().is_some()); + for c in upd.communities().unwrap().unwrap() { println!("{:?}", c); } - assert!(upd.communities::().unwrap().unwrap() + assert!(upd.communities().unwrap().unwrap() .eq([ StandardCommunity::new(42.into(), Tag::new(518)).into(), Wellknown::NoExport.into(), @@ -2089,8 +2104,6 @@ mod tests { #[test] fn chained_community_iters() { - use crate::bgp::communities::Community; - let buf = vec![ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, @@ -2115,10 +2128,10 @@ mod tests { let upd: UpdateMessage<_> = Message::from_octets(&buf, Some(sc)) .unwrap().try_into().unwrap(); - for c in upd.all_communities::().unwrap().unwrap() { + for c in upd.all_communities().unwrap().unwrap() { println!("{}", c); } - assert!(upd.all_communities::().unwrap().unwrap() + assert!(upd.all_communities().unwrap().unwrap() .eq(&[ StandardCommunity::new(42.into(), Tag::new(518)).into(), Wellknown::NoExport.into(), diff --git a/src/bgp/message/update_builder.rs b/src/bgp/message/update_builder.rs index 496e81e6..853d23d6 100644 --- a/src/bgp/message/update_builder.rs +++ b/src/bgp/message/update_builder.rs @@ -1444,7 +1444,7 @@ mod tests { use octseq::Parser; - use crate::{addr::Prefix, bgp::communities::Community}; + use crate::addr::Prefix; use crate::asn::Asn; //use crate::bgp::communities::Wellknown; use crate::bgp::message::nlri::BasicNlri; @@ -1743,7 +1743,7 @@ mod tests { a_cnt += pdu.announcements().unwrap().count(); assert!(pdu.local_pref().unwrap().is_some()); assert!(pdu.multi_exit_disc().unwrap().is_some()); - assert_eq!(pdu.communities::().unwrap().unwrap().count(), 300); + assert_eq!(pdu.communities().unwrap().unwrap().count(), 300); } assert_eq!(a_cnt, prefixes_num.try_into().unwrap()); @@ -2187,7 +2187,7 @@ mod tests { assert!(prev_type_code < type_code); prev_type_code = type_code; } - assert_eq!(pdu.communities::().unwrap().unwrap().count(), 2); + assert_eq!(pdu.communities().unwrap().unwrap().count(), 2); } // TODO also do fn check(raw: Bytes)