Struct bgpkit_parser::models::AsPath

source ·
pub struct AsPath {
    pub segments: Vec<AsPathSegment>,
}

Fields§

§segments: Vec<AsPathSegment>

Implementations§

source§

impl AsPath

source

pub fn new() -> AsPath

source

pub fn from_sequence<S: AsRef<[u32]>>(seq: S) -> Self

Shorthand for creating an AsPath consisting of a single AsSequence segment.

source

pub fn from_segments(segments: Vec<AsPathSegment>) -> AsPath

source

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.

source

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.

source

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.

source

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.

source

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.

source

pub fn contains_asn(&self, x: Asn) -> bool

Checks if any segments of this AsPath contain the following ASN.

source

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.

source

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);
source

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.

source

pub fn required_asn_length(&self) -> AsnLength

Get the length of ASN required to store all of the ASNs within this path

source

pub fn iter_segments(&self) -> SegmentIter<'_>

source

pub fn iter_segments_mut(&mut self) -> SegmentIterMut<'_>

source

pub fn into_segments_iter(self) -> SegmentIntoIter

source

pub fn iter_routes<D>(&self) -> AsPathRouteIter<'_, D>
where D: FromIterator<Asn>,

Gets an iterator over all possible routes this path represents.

source

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.
source

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.

source

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.

source

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.

source

pub fn to_u32_vec_opt(&self, dedup: bool) -> Option<Vec<u32>>

Trait Implementations§

source§

impl Clone for AsPath

source§

fn clone(&self) -> AsPath

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for AsPath

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for AsPath

source§

fn default() -> AsPath

Returns the “default value” for a type. Read more
source§

impl Display for AsPath

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl From<AsPath> for AttributeValue

Defaults to using AS_PATH (as opposed to AS4_PATH) when choosing attribute type.

source§

fn from(path: AsPath) -> Self

Converts to this type from the input type.
source§

impl Hash for AsPath

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<'a> IntoIterator for &'a AsPath

Iterates over all route variations the given AsPath represents.

§

type Item = Vec<Asn>

The type of the elements being iterated over.
§

type IntoIter = AsPathRouteIter<'a, Vec<Asn>>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl IntoIterator for AsPath

Iterates over all route variations the given AsPath represents.

§

type Item = Vec<Asn>

The type of the elements being iterated over.
§

type IntoIter = AsPathRouteIter<'static, Vec<Asn>>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl PartialEq for AsPath

source§

fn eq(&self, other: &AsPath) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for AsPath

source§

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> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more