pub mod default;
mod diagnostic;
pub mod fallible;
mod raw;
mod recovery;
mod route;
mod update;
pub use default::{ElemIterator, RecordIterator};
pub use diagnostic::{DiagnosticEvent, DiagnosticIterator};
pub use fallible::{FallibleElemIterator, FallibleRecordIterator};
pub use raw::RawRecordIterator;
pub use recovery::{
RecoveringElemIterator, RecoveringRecordIterator, RecoveryConfig, RecoveryError, RecoveryEvent,
RecoveryEvidence, RecoveryGap,
};
pub use route::{FallibleRouteIterator, RouteIterator};
pub use update::{
Bgp4MpUpdate, FallibleUpdateIterator, LegacyBgpUpdate, MrtUpdate, TableDumpV2Entry,
UpdateIterator,
};
use crate::models::BgpElem;
use crate::models::{MrtMessage, MrtRecord, TableDumpV2Message};
use crate::parser::BgpkitParser;
use crate::RawMrtRecord;
use crate::{Elementor, Filter, Filterable};
use std::io::Read;
use std::path::Path;
#[inline]
pub(crate) fn record_matches_filters(
record: &MrtRecord,
filters: &[Filter],
elementor: &mut Elementor,
) -> bool {
if filters.is_empty() {
return true;
}
if matches!(
&record.message,
MrtMessage::TableDumpV2Message(TableDumpV2Message::PeerIndexTable(_))
) {
let _ = elementor.record_to_elems(record.clone());
return true;
}
elementor
.record_to_elems(record.clone())
.iter()
.any(|element| element.match_filters(filters))
}
pub(crate) fn write_mrt_core_dump(enabled: bool, bytes: Option<Vec<u8>>) {
write_mrt_core_dump_to_path(enabled, bytes, "mrt_core_dump");
}
pub(crate) fn write_mrt_core_dump_to_path<P: AsRef<Path>>(
enabled: bool,
bytes: Option<Vec<u8>>,
path: P,
) {
if enabled {
if let Some(bytes) = bytes {
std::fs::write(path, bytes).expect("Unable to write to mrt_core_dump");
}
}
}
impl<R: Read> IntoIterator for BgpkitParser<R> {
type Item = BgpElem;
type IntoIter = ElemIterator<R>;
fn into_iter(self) -> Self::IntoIter {
ElemIterator::new(self)
}
}
impl<R> BgpkitParser<R> {
pub fn into_record_iter(self) -> RecordIterator<R> {
RecordIterator::new(self)
}
pub fn into_elem_iter(self) -> ElemIterator<R> {
ElemIterator::new(self)
}
pub fn into_raw_record_iter(self) -> RawRecordIterator<R> {
RawRecordIterator::new(self)
}
pub fn into_recovering_record_iter(
self,
config: RecoveryConfig,
) -> RecoveringRecordIterator<R> {
RecoveringRecordIterator::new(self, config)
}
pub fn into_recovering_elem_iter(self, config: RecoveryConfig) -> RecoveringElemIterator<R> {
RecoveringElemIterator::new(self, config)
}
pub fn into_update_iter(self) -> UpdateIterator<R> {
UpdateIterator::new(self)
}
pub fn into_route_iter(self) -> RouteIterator<R> {
RouteIterator::new(self)
}
pub fn into_fallible_record_iter(self) -> FallibleRecordIterator<R> {
FallibleRecordIterator::new(self)
}
pub fn into_fallible_elem_iter(self) -> FallibleElemIterator<R> {
FallibleElemIterator::new(self)
}
pub fn into_fallible_update_iter(self) -> FallibleUpdateIterator<R> {
FallibleUpdateIterator::new(self)
}
pub fn into_fallible_route_iter(self) -> FallibleRouteIterator<R> {
FallibleRouteIterator::new(self)
}
pub fn into_diagnostic_iter(self) -> DiagnosticIterator<R> {
DiagnosticIterator::new(self)
}
pub fn into_elementor_and_raw_record_iter(
self,
) -> (Elementor, impl Iterator<Item = RawMrtRecord>)
where
R: Read,
{
let mut raw_iter = RawRecordIterator::new(self).peekable();
let elementor = match raw_iter.peek().cloned().and_then(|r| r.parse().ok()) {
Some(MrtRecord {
message: MrtMessage::TableDumpV2Message(TableDumpV2Message::PeerIndexTable(pit)),
..
}) => {
raw_iter.next();
Elementor::with_peer_table(pit)
}
_ => Elementor::new(),
};
(elementor, raw_iter)
}
pub fn into_elementor_and_record_iter(self) -> (Elementor, impl Iterator<Item = MrtRecord>)
where
R: Read,
{
let mut record_iter = RecordIterator::new(self).peekable();
let elementor = match record_iter.peek().cloned() {
Some(MrtRecord {
message: MrtMessage::TableDumpV2Message(TableDumpV2Message::PeerIndexTable(pit)),
..
}) => {
record_iter.next();
Elementor::with_peer_table(pit)
}
_ => Elementor::new(),
};
(elementor, record_iter)
}
}