Struct bgpkit_parser::models::attributes::AsPath
source · pub struct AsPath {
pub segments: Vec<AsPathSegment>,
}
Fields§
§segments: Vec<AsPathSegment>
Implementations§
source§impl AsPath
impl AsPath
pub fn new() -> AsPath
sourcepub fn from_sequence<S: AsRef<[u32]>>(seq: S) -> Self
pub fn from_sequence<S: AsRef<[u32]>>(seq: S) -> Self
Shorthand for creating an AsPath
consisting of a single AsSequence
segment.
pub fn from_segments(segments: Vec<AsPathSegment>) -> AsPath
sourcepub fn append_segment(&mut self, segment: AsPathSegment)
pub fn append_segment(&mut self, segment: AsPathSegment)
Adds a new segment to the end of the path. This will change the origin of the path. No validation or merging the segment is performed during this step.
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Check if the path is empty. Note that a non-empty path may have a route length of 0 due to empty segments or confederation segments.
sourcepub fn route_len(&self) -> usize
pub fn route_len(&self) -> usize
Get the total length of the routes this path represents. For example, if this route contained a sequence of 5 ASNs followed by a set of 3 ASNs, the total route length would be 6.
Confederation segments do not count towards the total route length. This means it is possible to have a non-empty AsPath with a length of 0.
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Get the number of segments that make up this path. For the number of ASNs in routes represented by this path, use AsPath::route_len.
sourcepub fn num_route_variations(&self) -> u64
pub fn num_route_variations(&self) -> u64
Get the total number of routes this path represents. This function assumes the total number of route variations can be represented by a u64.
sourcepub fn contains_asn(&self, x: Asn) -> bool
pub fn contains_asn(&self, x: Asn) -> bool
Checks if any segments of this AsPath contain the following ASN.
sourcepub fn coalesce(&mut self)
pub fn coalesce(&mut self)
Coalesce this AsPath into the minimum number of segments required without changing the values along the path. This can be helpful as some BGP servers will prepend additional segments without coalescing sequences. For de-duplicating see AsPath::dedup_coalesce.
Changes applied by this function:
- Merge adjacent AS_SEQUENCE segments
- Merge adjacent AS_CONFED_SEQUENCE segments
- Removing empty AS_SEQUENCE and AS_CONFED_SEQUENCE segments
let mut a = AsPath::from_segments(vec![
AsPathSegment::sequence([]),
AsPathSegment::sequence([1, 2]),
AsPathSegment::sequence([]),
AsPathSegment::sequence([2]),
AsPathSegment::set([2]),
AsPathSegment::set([5, 3, 3, 2]),
]);
let expected = AsPath::from_segments(vec![
AsPathSegment::sequence([1, 2, 2]),
AsPathSegment::set([2]),
AsPathSegment::set([5, 3, 3, 2]),
]);
a.coalesce();
assert_eq!(a, expected);
If there is only one segment, no changes will occur. This function will not attempt to deduplicate sequences or alter sets.
sourcepub fn dedup_coalesce(&mut self)
pub fn dedup_coalesce(&mut self)
A more aggressive version of AsPath::coalesce which also de-duplicates ASNs within this path and converts sets of a single ASN to sequences. Some BGP servers will prepend their own ASN multiple times when announcing a path to artificially increase the route length and make the route seem less less desirable to peers.This function is best suited for use-cases which only care about transitions between ASes along the path.
Changes applied by this function:
- Merge adjacent AS_SEQUENCE segments
- Merge adjacent AS_CONFED_SEQUENCE segments
- Removing empty AS_SEQUENCE and AS_CONFED_SEQUENCE segments
- De-duplicate ASNs in AS_SEQUENCE and AS_CONFED_SEQUENCE segments
- Sort and de-duplicate ASNs in AS_SET and AS_CONFED_SET segments
- Convert AS_SET and AS_CONFED_SET segments with exactly 1 element to sequences
let mut a = AsPath::from_segments(vec![
AsPathSegment::sequence([1, 2]),
AsPathSegment::sequence([]),
AsPathSegment::sequence([2]),
AsPathSegment::set([2]),
AsPathSegment::set([5, 3, 3, 2]),
]);
let expected = AsPath::from_segments(vec![
AsPathSegment::sequence([1, 2]),
AsPathSegment::set([2, 3, 5]),
]);
a.dedup_coalesce();
assert_eq!(a, expected);
sourcepub fn has_equivalent_routing(&self, other: &Self) -> bool
pub fn has_equivalent_routing(&self, other: &Self) -> bool
Checks if two paths correspond to equivalent routes. Unlike a == b
, this function will
ignore duplicate ASNs by comparing the coalesced versions of each path.
This is equivalent to AsPath::eq after calling AsPath::dedup_coalesce on both paths.
sourcepub fn required_asn_length(&self) -> AsnLength
pub fn required_asn_length(&self) -> AsnLength
Get the length of ASN required to store all of the ASNs within this path
pub fn iter_segments(&self) -> SegmentIter<'_>
pub fn iter_segments_mut(&mut self) -> SegmentIterMut<'_>
pub fn into_segments_iter(self) -> SegmentIntoIter
sourcepub fn iter_routes<D>(&self) -> AsPathRouteIter<'_, D> ⓘwhere
D: FromIterator<Asn>,
pub fn iter_routes<D>(&self) -> AsPathRouteIter<'_, D> ⓘwhere
D: FromIterator<Asn>,
Gets an iterator over all possible routes this path represents.
sourcepub fn merge_aspath_as4path(aspath: &AsPath, as4path: &AsPath) -> AsPath
pub fn merge_aspath_as4path(aspath: &AsPath, as4path: &AsPath) -> AsPath
Construct AsPath from AS_PATH and AS4_PATH
https://datatracker.ietf.org/doc/html/rfc6793#section-4.2.3
If the number of AS numbers in the AS_PATH attribute is less than the
number of AS numbers in the AS4_PATH attribute, then the AS4_PATH
attribute SHALL be ignored, and the AS_PATH attribute SHALL be taken
as the AS path information.
If the number of AS numbers in the AS_PATH attribute is larger than
or equal to the number of AS numbers in the AS4_PATH attribute, then
the AS path information SHALL be constructed by taking as many AS
numbers and path segments as necessary from the leading part of the
AS_PATH attribute, and then prepending them to the AS4_PATH attribute
so that the AS path information has a number of AS numbers identical
to that of the AS_PATH attribute. Note that a valid
AS_CONFED_SEQUENCE or AS_CONFED_SET path segment SHALL be prepended
if it is either the leading path segment or is adjacent to a path
segment that is prepended.
sourcepub fn iter_origins(&self) -> impl '_ + Iterator<Item = Asn>
pub fn iter_origins(&self) -> impl '_ + Iterator<Item = Asn>
Iterate through the originating ASNs of this path. This functionality is provided for completeness, but in almost all cases this iterator should only contain a single element.
sourcepub fn get_origin_opt(&self) -> Option<Asn>
pub fn get_origin_opt(&self) -> Option<Asn>
This function serves as a alternative to AsPath::iter_origins which attempts to make the
assumption that a path can only have exactly one origin. If a path does not have exactly 1
origin (such as when empty or ending in a set), then None
will be returned instead.
sourcepub fn get_collector_opt(&self) -> Option<Asn>
pub fn get_collector_opt(&self) -> Option<Asn>
This function optionally returns the first hop in the AS hop, which is considered as the collector ASN of the message.
pub fn to_u32_vec_opt(&self, dedup: bool) -> Option<Vec<u32>>
Trait Implementations§
source§impl From<AsPath> for AttributeValue
impl From<AsPath> for AttributeValue
Defaults to using AS_PATH
(as opposed to AS4_PATH
) when choosing attribute type.
source§impl<'a> IntoIterator for &'a AsPath
impl<'a> IntoIterator for &'a AsPath
Iterates over all route variations the given AsPath
represents.
source§impl IntoIterator for AsPath
impl IntoIterator for AsPath
Iterates over all route variations the given AsPath
represents.
source§impl PartialEq for AsPath
impl PartialEq for AsPath
impl Eq for AsPath
impl StructuralPartialEq for AsPath
Auto Trait Implementations§
impl Freeze for AsPath
impl RefUnwindSafe for AsPath
impl Send for AsPath
impl Sync for AsPath
impl Unpin for AsPath
impl UnwindSafe for AsPath
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.