Skip to content

Commit

Permalink
handle errors when peer_id does not exists
Browse files Browse the repository at this point in the history
also removed a number of unwraps
  • Loading branch information
digizeph committed Mar 30, 2024
1 parent 56c4d09 commit 8c55cac
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
16 changes: 8 additions & 8 deletions src/models/bgp/attributes/aspath.rs
Expand Up @@ -610,10 +610,10 @@ impl AsPath {
/// if it is either the leading path segment or is adjacent to a path
/// segment that is prepended.
/// ```
pub fn merge_aspath_as4path(aspath: &AsPath, as4path: &AsPath) -> Option<AsPath> {
pub fn merge_aspath_as4path(aspath: &AsPath, as4path: &AsPath) -> AsPath {
if aspath.route_len() < as4path.route_len() {
// Per RFC6793, if 2-byte AS path is shorter than 4-byte AS path, ignore 4-byte AS path
return Some(aspath.clone());
return aspath.clone();
}

let mut as4iter = as4path.segments.iter();
Expand Down Expand Up @@ -646,7 +646,7 @@ impl AsPath {
};
}

Some(AsPath { segments: new_segs })
AsPath { segments: new_segs }
}

/// Iterate through the originating ASNs of this path. This functionality is provided for
Expand Down Expand Up @@ -990,20 +990,20 @@ mod tests {
fn test_aspath_as4path_merge() {
let aspath = AsPath::from_sequence([1, 2, 3, 5]);
let as4path = AsPath::from_sequence([2, 3, 7]);
let newpath = AsPath::merge_aspath_as4path(&aspath, &as4path).unwrap();
let newpath = AsPath::merge_aspath_as4path(&aspath, &as4path);
assert_eq!(newpath.segments[0], AsPathSegment::sequence([1, 2, 3, 7]));

let aspath = AsPath::from_sequence([1, 2]);
let as4path = AsPath::from_sequence([2, 3, 7]);
let newpath = AsPath::merge_aspath_as4path(&aspath, &as4path).unwrap();
let newpath = AsPath::merge_aspath_as4path(&aspath, &as4path);
assert_eq!(newpath.segments[0], AsPathSegment::sequence([1, 2]));

let aspath = AsPath::from_segments(vec![
AsPathSegment::sequence([1, 2, 3, 5]),
AsPathSegment::set([7, 8]),
]);
let as4path = AsPath::from_sequence([6, 7, 8]);
let newpath = AsPath::merge_aspath_as4path(&aspath, &as4path).unwrap();
let newpath = AsPath::merge_aspath_as4path(&aspath, &as4path);
assert_eq!(newpath.segments.len(), 2);
assert_eq!(newpath.segments[0], AsPathSegment::sequence([1, 6, 7, 8]));
assert_eq!(newpath.segments[1], AsPathSegment::set([7, 8]));
Expand All @@ -1017,7 +1017,7 @@ mod tests {
AsPathSegment::sequence([8, 4, 6]),
AsPathSegment::set([11, 12]),
]);
let newpath = AsPath::merge_aspath_as4path(&aspath, &as4path).unwrap();
let newpath = AsPath::merge_aspath_as4path(&aspath, &as4path);
assert_eq!(newpath.segments.len(), 3);
assert_eq!(newpath.segments[0], AsPathSegment::sequence([1, 2]));
assert_eq!(newpath.segments[1], AsPathSegment::set([11, 12]));
Expand All @@ -1032,7 +1032,7 @@ mod tests {
AsPathSegment::sequence([7, 8]),
AsPathSegment::set([11, 12]),
]);
let newpath = AsPath::merge_aspath_as4path(&aspath, &as4path).unwrap();
let newpath = AsPath::merge_aspath_as4path(&aspath, &as4path);
assert_eq!(newpath.segments.len(), 3);
assert_eq!(newpath.segments[0], AsPathSegment::sequence([1, 7, 8]));
assert_eq!(newpath.segments[1], AsPathSegment::set([11, 12]));
Expand Down
33 changes: 22 additions & 11 deletions src/parser/mrt/mrt_elem.rs
Expand Up @@ -7,10 +7,10 @@
use crate::models::*;
use crate::parser::bgp::messages::parse_bgp_update_message;
use itertools::Itertools;
use log::warn;
use log::{error, warn};
use std::collections::HashMap;
use std::fmt::{Display, Formatter};
use std::net::IpAddr;
use std::net::{IpAddr, Ipv4Addr};

pub struct Elementor {
peer_table: Option<PeerIndexTable>,
Expand Down Expand Up @@ -198,7 +198,7 @@ impl Elementor {
(None, None) => None,
(Some(v), None) => Some(v),
(None, Some(v)) => Some(v),
(Some(v1), Some(v2)) => Some(AsPath::merge_aspath_as4path(&v1, &v2).unwrap()),
(Some(v1), Some(v2)) => Some(AsPath::merge_aspath_as4path(&v1, &v2)),

Check warning on line 201 in src/parser/mrt/mrt_elem.rs

View check run for this annotation

Codecov / codecov/patch

src/parser/mrt/mrt_elem.rs#L201

Added line #L201 was not covered by tests
};

let origin_asns = path
Expand Down Expand Up @@ -359,12 +359,19 @@ impl Elementor {
let prefix = t.prefix;
for e in t.rib_entries {
let pid = e.peer_index;
let peer = self
.peer_table
.as_ref()
.unwrap()
.get_peer_by_id(&pid)
.unwrap();
let peer = match self.peer_table.as_ref() {
None => {
error!("peer_table is None");
break;

Check warning on line 365 in src/parser/mrt/mrt_elem.rs

View check run for this annotation

Codecov / codecov/patch

src/parser/mrt/mrt_elem.rs#L364-L365

Added lines #L364 - L365 were not covered by tests
}
Some(table) => match table.get_peer_by_id(&pid) {
None => {
error!("peer ID {} not found in peer_index table", pid);
break;

Check warning on line 370 in src/parser/mrt/mrt_elem.rs

View check run for this annotation

Codecov / codecov/patch

src/parser/mrt/mrt_elem.rs#L369-L370

Added lines #L369 - L370 were not covered by tests
}
Some(peer) => peer,
},
};
let (
as_path,
as4_path, // Table dump v1 does not have 4-byte AS number
Expand All @@ -387,7 +394,7 @@ impl Elementor {
(Some(v), None) => Some(v),
(None, Some(v)) => Some(v),
(Some(v1), Some(v2)) => {
Some(AsPath::merge_aspath_as4path(&v1, &v2).unwrap())
Some(AsPath::merge_aspath_as4path(&v1, &v2))

Check warning on line 397 in src/parser/mrt/mrt_elem.rs

View check run for this annotation

Codecov / codecov/patch

src/parser/mrt/mrt_elem.rs#L397

Added line #L397 was not covered by tests
}
};

Expand Down Expand Up @@ -547,9 +554,13 @@ impl From<&BgpElem> for Attributes {
}

if let Some(v) = value.aggr_asn {
let aggregator_id = match value.aggr_ip {
Some(v) => v,
None => Ipv4Addr::UNSPECIFIED,

Check warning on line 559 in src/parser/mrt/mrt_elem.rs

View check run for this annotation

Codecov / codecov/patch

src/parser/mrt/mrt_elem.rs#L559

Added line #L559 was not covered by tests
};
values.push(AttributeValue::Aggregator {
asn: v,
id: value.aggr_ip.unwrap(),
id: aggregator_id,
is_as4: v.is_four_byte(),
});
}
Expand Down

0 comments on commit 8c55cac

Please sign in to comment.