use super::*;
pub(super) fn parse_peer_index_vec(data: &[u8]) -> Result<Vec<Peer>, ParserError> {
let table =
crate::parser::mrt::messages::table_dump_v2::peer_index_table::parse_peer_index_table(
&mut bytes::Bytes::copy_from_slice(data),
)?;
let mut peers_by_index = table.id_peer_map;
let count = peers_by_index
.keys()
.max()
.map_or(0, |max| *max as usize + 1);
let mut peers = Vec::with_capacity(count);
for index in 0..count {
match peers_by_index.remove(&(index as u16)) {
Some(peer) => peers.push(peer),
None => {
return Err(ParserError::ParseError(format!(
"peer index table skips index {index}"
)))
}
}
}
Ok(peers)
}
pub(super) fn attribute_block_walks(attrs: &[u8]) -> bool {
matches!(scan_attr_block(attrs), AttrBlockScan::Usable { .. })
}
fn append_nlri(
section: &[u8],
afi: Afi,
safi: Safi,
add_path: bool,
elem_type: ElemType,
out: &mut Vec<UpdateEntry>,
) {
if section.is_empty() {
return;
}
let Ok(prefixes) = crate::parser::utils::parse_nlri_list(
bytes::Bytes::copy_from_slice(section),
add_path,
&afi,
) else {
return;
};
let flags = afi_flags(&afi, safi, add_path);
out.extend(prefixes.into_iter().map(|prefix| UpdateEntry {
prefix,
elem_type,
flags,
}));
}
fn afi_flags(afi: &Afi, safi: Safi, add_path: bool) -> u8 {
let mut flags = 0u8;
if add_path {
flags |= crate::parser::mrt::frame::FLAG_ADD_PATH;
}
if *afi == Afi::Ipv6 {
flags |= FLAG_AFI_IPV6;
}
if safi == Safi::Multicast {
flags |= FLAG_SAFI_MULTICAST;
}
flags
}
pub(super) fn update_record_elems<'a>(
state: &'a mut ElemViewState,
body_len: usize,
header: CommonHeader,
peer_ip: IpAddr,
peer_asn: Asn,
timestamp: f64,
) -> Option<RecordElems<'a>> {
let (offset, len) = state.update_attrs;
let (entries, next_hop) = (&state.update_entries, state.update_next_hop);
let attrs = state.body_buf.get(offset..offset.checked_add(len)?)?;
if offset + len > body_len {
return None;
}
Some(RecordElems::update(
header, entries, attrs, peer_ip, peer_asn, timestamp, next_hop,
))
}
fn warn_on_truncated_attribute(attrs: &[u8]) {
let mut rest = attrs;
while let Some((_, value, next)) = next_attr_tlv(rest) {
let _ = value;
rest = next;
}
if rest.len() >= 3 {
warn!(
"attribute encodes a length longer than the remaining attribute data; ignoring the rest"
);
}
}
pub(crate) fn fill_update_entries<'a>(
body: &'a [u8],
subtype: u16,
out: &mut Vec<UpdateEntry>,
) -> Option<(&'a [u8], Option<IpAddr>)> {
out.clear();
let add_path = match Bgp4MpType::try_from(subtype).ok()? {
Bgp4MpType::MessageAs4 | Bgp4MpType::MessageAs4Local => false,
Bgp4MpType::MessageAs4Addpath | Bgp4MpType::MessageLocalAs4Addpath => true,
_ => return None,
};
let mut rest = body.get(4 * 2..)?;
if rest.len() < 4 {
return None;
}
let ip_len = match u16::from_be_bytes([rest[2], rest[3]]) {
1 => 4,
2 => 16,
_ => return None,
};
rest = rest.get(4 + ip_len * 2..)?;
if rest.len() < 19 || rest[18] != 2 {
return None;
}
let message_len = u16::from_be_bytes([rest[16], rest[17]]) as usize;
if message_len < 19 {
return None;
}
rest = rest.get(19..message_len.min(rest.len()))?;
if rest.len() < 2 {
return None;
}
let withdrawn_len = u16::from_be_bytes([rest[0], rest[1]]) as usize;
rest = rest.get(2..)?;
if rest.len() < withdrawn_len + 2 {
return None;
}
let withdrawn = &rest[..withdrawn_len];
rest = &rest[withdrawn_len..];
let attr_len = u16::from_be_bytes([rest[0], rest[1]]) as usize;
rest = rest.get(2..)?;
if rest.len() < attr_len {
return None;
}
let attrs = &rest[..attr_len];
let announced = &rest[attr_len..];
if !attribute_block_walks(attrs) {
return None;
}
warn_on_truncated_attribute(attrs);
let mut next_hop: Option<IpAddr> = None;
let mut reach: Option<crate::models::Nlri> = None;
let mut unreach: Option<crate::models::Nlri> = None;
let mut plain_next_hop: Option<IpAddr> = None;
let mut pos = 0usize;
while let Some((code, start, end)) = next_attr_bounds(attrs, pos) {
let value = &attrs[start..end];
match code {
3 => {
let mut value = value;
if let Ok(address) = value.read_address(&Afi::Ipv4) {
plain_next_hop = Some(address);
}
}
14 => {
if let Ok(crate::models::AttributeValue::MpReachNlri(nlri)) =
crate::parser::bgp::attributes::parse_nlri(
bytes::Bytes::copy_from_slice(value),
&None,
&None,
&None,
true,
add_path,
)
{
next_hop = nlri.next_hop.map(|next_hop| next_hop.global_addr());
reach = Some(nlri);
}
}
15 => {
if let Ok(crate::models::AttributeValue::MpUnreachNlri(nlri)) =
crate::parser::bgp::attributes::parse_nlri(
bytes::Bytes::copy_from_slice(value),
&None,
&None,
&None,
false,
add_path,
)
{
unreach = Some(nlri);
}
}
_ => {}
}
pos = end;
}
let next_hop = plain_next_hop.or(next_hop);
append_nlri(
announced,
Afi::Ipv4,
Safi::Unicast,
add_path,
ElemType::ANNOUNCE,
out,
);
if let Some(nlri) = reach {
push_nlri_entries(nlri, add_path, ElemType::ANNOUNCE, out);
}
append_nlri(
withdrawn,
Afi::Ipv4,
Safi::Unicast,
add_path,
ElemType::WITHDRAW,
out,
);
if let Some(nlri) = unreach {
push_nlri_entries(nlri, add_path, ElemType::WITHDRAW, out);
}
Some((attrs, next_hop))
}
fn push_nlri_entries(
nlri: crate::models::Nlri,
_add_path: bool,
elem_type: ElemType,
out: &mut Vec<UpdateEntry>,
) {
let flags = afi_flags(&nlri.afi, nlri.safi, _add_path);
out.extend(nlri.prefixes.into_iter().map(|prefix| UpdateEntry {
prefix,
elem_type,
flags,
}));
}