Skip to content

Commit

Permalink
Merge pull request #49 from bgpkit/feature_extended_types
Browse files Browse the repository at this point in the history
Use extended bgp-models types
  • Loading branch information
digizeph authored Dec 19, 2021
2 parents 3c782c1 + 71023fb commit 552a22a
Show file tree
Hide file tree
Showing 16 changed files with 118 additions and 94 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bgpkit-parser"
version = "0.7.0-alpha.1"
version = "0.7.0-alpha.2"
authors = ["Mingwei Zhang <[email protected]>"]
edition = "2018"
readme = "README.md"
Expand All @@ -26,9 +26,10 @@ ipnetwork = {version="0.18", default-features=false}
enum-primitive-derive = "0.2"
num-traits = "0.1"
chrono = "0.4"
bgp-models = "0.6.3"
regex = "1"

bgp-models = "0.7.0-alpha.1"

# logging
log="0.4"
env_logger="0.9"
Expand Down
2 changes: 1 addition & 1 deletion examples/parse-files-from-broker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn main() {
// iterating through the parser. the iterator returns `BgpElem` one at a time.
let elems = parser.into_elem_iter().map(|elem|{
if let Some(origins) = &elem.origin_asns {
if origins.contains(&13335) {
if origins.contains(&13335.into()) {
Some(elem)
} else {
None
Expand Down
2 changes: 1 addition & 1 deletion examples/real-time-routeviews-kafka-openbmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn consume_and_print(group: String, topic: String, brokers: Vec<String>) -> Resu
m.bgp_message,
header.timestamp,
&per_peer_header.peer_ip,
&per_peer_header.peer_asn
&per_peer_header.peer_asn.into()
)
{
info!("{}", elem);
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ fn main(){
// iterating through the parser. the iterator returns `BgpElem` one at a time.
let elems = parser.into_elem_iter().map(|elem|{
if let Some(origins) = &elem.origin_asns {
if origins.contains(&13335) {
if origins.contains(&13335.into()) {
Some(elem)
} else {
None
Expand Down Expand Up @@ -339,4 +339,6 @@ pub use parser::bmp::parse_openbmp_msg;
pub use parser::bmp::parse_bmp_msg;
pub use parser::bmp::parse_openbmp_header;
pub use parser::rislive::parse_ris_live_message;
pub use parser::mrt::parse_mrt_record;
pub use parser::filter::*;
pub use parser::utils::ReadUtils;
83 changes: 40 additions & 43 deletions src/parser/bgp/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl AttributeParser {
AttrType::NEXT_HOP => self.parse_next_hop(&mut attr_input, &afi),
AttrType::MULTI_EXIT_DISCRIMINATOR => self.parse_med(&mut attr_input),
AttrType::LOCAL_PREFERENCE => self.parse_local_pref(&mut attr_input),
AttrType::ATOMIC_AGGREGATE => Ok(Attribute::AtomicAggregate(AtomicAggregate::AG)),
AttrType::ATOMIC_AGGREGATE => Ok(AttributeValue::AtomicAggregate(AtomicAggregate::AG)),
AttrType::AGGREGATOR => self.parse_aggregator(&mut attr_input, asn_len, &afi),
AttrType::ORIGINATOR_ID => self.parse_originator_id(&mut attr_input, &afi),
AttrType::CLUSTER_LIST => self.parse_clusters(&mut attr_input, &afi),
Expand All @@ -114,17 +114,17 @@ impl AttributeParser {
AttrType::DEVELOPMENT => {
let mut buf=Vec::with_capacity(length as usize);
attr_input.read_to_end(&mut buf)?;
Ok(Attribute::Development(buf))
Ok(AttributeValue::Development(buf))
},
_ => {
let mut buf=Vec::with_capacity(length as usize);
attr_input.read_to_end(&mut buf)?;
Err(crate::error::ParserErrorKind::Unsupported(format!("unsupported attribute type: {:?}", attr_type)))
}
};
let _attr = match attr{
Ok(v) => {
attributes.push(v);
match attr{
Ok(value) => {
attributes.push(Attribute{value, flag, attr_type});
}
Err(e) => {
if partial {
Expand All @@ -145,23 +145,23 @@ impl AttributeParser {
Ok(attributes)
}

fn parse_origin<T: Read>(&self, input: &mut Take<T>) -> Result<Attribute, ParserErrorKind> {
fn parse_origin<T: Read>(&self, input: &mut Take<T>) -> Result<AttributeValue, ParserErrorKind> {
let origin = input.read_u8()?;
match Origin::from_u8(origin) {
Some(v) => Ok(Attribute::Origin(v)),
Some(v) => Ok(AttributeValue::Origin(v)),
None => {
return Err(crate::error::ParserErrorKind::UnknownAttr(format!("Failed to parse attribute type: origin")))
}
}
}

fn parse_as_path<T: Read>(&self, input: &mut Take<T>, asn_len: &AsnLength) -> Result<Attribute, ParserErrorKind> {
fn parse_as_path<T: Read>(&self, input: &mut Take<T>, asn_len: &AsnLength) -> Result<AttributeValue, ParserErrorKind> {
let mut output = AsPath::new();
while input.limit() > 0 {
let segment = self.parse_as_segment(input, asn_len)?;
output.add_segment(segment);
}
Ok(Attribute::AsPath(output))
Ok(AttributeValue::AsPath(output))
}

fn parse_as_segment<T: Read>(&self, input: &mut Take<T>, asn_len: &AsnLength) -> Result<AsPathSegment, ParserErrorKind> {
Expand All @@ -180,40 +180,37 @@ impl AttributeParser {
}
}

fn parse_next_hop<T: Read>(&self, input: &mut Take<T>, afi: &Option<Afi>) -> Result<Attribute, ParserErrorKind> {
fn parse_next_hop<T: Read>(&self, input: &mut Take<T>, afi: &Option<Afi>) -> Result<AttributeValue, ParserErrorKind> {
if let Some(afi) = afi {
if *afi==Afi::Ipv6{
print!("break here");
}
Ok(input.read_address(afi).map(Attribute::NextHop)?)
Ok(input.read_address(afi).map(AttributeValue::NextHop)?)
} else {
Ok(input.read_address(&Afi::Ipv4).map(Attribute::NextHop)?)
Ok(input.read_address(&Afi::Ipv4).map(AttributeValue::NextHop)?)
}
}

fn parse_med<T: Read>(&self, input: &mut Take<T>) -> Result<Attribute, ParserErrorKind> {
fn parse_med<T: Read>(&self, input: &mut Take<T>) -> Result<AttributeValue, ParserErrorKind> {
Ok(input
.read_u32::<BigEndian>()
.map(Attribute::MultiExitDiscriminator)?)
.map(AttributeValue::MultiExitDiscriminator)?)
}

fn parse_local_pref<T: Read>(&self, input: &mut Take<T>) -> Result<Attribute, ParserErrorKind> {
fn parse_local_pref<T: Read>(&self, input: &mut Take<T>) -> Result<AttributeValue, ParserErrorKind> {
Ok(input
.read_u32::<BigEndian>()
.map(Attribute::LocalPreference)?)
.map(AttributeValue::LocalPreference)?)
}

fn parse_aggregator<T: Read>(&self, input: &mut Take<T>, asn_len: &AsnLength, afi: &Option<Afi>) -> Result<Attribute, ParserErrorKind> {
fn parse_aggregator<T: Read>(&self, input: &mut Take<T>, asn_len: &AsnLength, afi: &Option<Afi>) -> Result<AttributeValue, ParserErrorKind> {
let asn = input.read_asn(asn_len)?;
let afi = match afi {
None => { &Afi::Ipv4 }
Some(a) => {a}
};
let addr = input.read_address(afi)?;
Ok(Attribute::Aggregator(asn, addr))
Ok(AttributeValue::Aggregator(asn, addr))
}

fn parse_regular_communities<T: Read>(&self, input: &mut Take<T>) -> Result<Attribute, ParserErrorKind> {
fn parse_regular_communities<T: Read>(&self, input: &mut Take<T>) -> Result<AttributeValue, ParserErrorKind> {
const COMMUNITY_NO_EXPORT: u32 = 0xFFFFFF01;
const COMMUNITY_NO_ADVERTISE: u32 = 0xFFFFFF02;
const COMMUNITY_NO_EXPORT_SUBCONFED: u32 = 0xFFFFFF03;
Expand All @@ -226,36 +223,36 @@ impl AttributeParser {
COMMUNITY_NO_ADVERTISE => Community::NoAdvertise,
COMMUNITY_NO_EXPORT_SUBCONFED => Community::NoExportSubConfed,
value => {
let asn = (value >> 16) & 0xffff;
let asn = Asn{asn: ((value >> 16) & 0xffff) as i32, len: AsnLength::Bits16};
let value = (value & 0xffff) as u16;
Community::Custom(asn, value)
}
}
)
}
Ok(Attribute::Communities(communities))
Ok(AttributeValue::Communities(communities))
}

fn parse_originator_id<T: Read>(&self, input: &mut Take<T>, afi: &Option<Afi>) -> Result<Attribute, ParserErrorKind> {
fn parse_originator_id<T: Read>(&self, input: &mut Take<T>, afi: &Option<Afi>) -> Result<AttributeValue, ParserErrorKind> {
let afi = match afi {
None => { &Afi::Ipv4 }
Some(a) => {a}
};
let addr = input.read_address(afi)?;
Ok(Attribute::OriginatorId(addr))
Ok(AttributeValue::OriginatorId(addr))
}

#[allow(unused)]
fn parse_cluster_id<T: Read>(&self, input: &mut Take<T>, afi: &Option<Afi>) -> Result<Attribute, ParserErrorKind> {
fn parse_cluster_id<T: Read>(&self, input: &mut Take<T>, afi: &Option<Afi>) -> Result<AttributeValue, ParserErrorKind> {
let afi = match afi {
None => { &Afi::Ipv4 }
Some(a) => {a}
};
let addr = input.read_address(afi)?;
Ok(Attribute::Clusters(vec![addr]))
Ok(AttributeValue::Clusters(vec![addr]))
}

fn parse_clusters<T: Read>(&self, input: &mut Take<T>, afi: &Option<Afi>) -> Result<Attribute, ParserErrorKind> {
fn parse_clusters<T: Read>(&self, input: &mut Take<T>, afi: &Option<Afi>) -> Result<AttributeValue, ParserErrorKind> {
// FIXME: in https://tools.ietf.org/html/rfc4456, the CLUSTER_LIST is a set of CLUSTER_ID each represented by a 4-byte number
let mut clusters = Vec::new();
while input.limit() > 0 {
Expand All @@ -266,7 +263,7 @@ impl AttributeParser {
let addr = input.read_address(afi)?;
clusters.push(addr);
}
Ok(Attribute::Clusters(clusters))
Ok(AttributeValue::Clusters(clusters))
}

///
Expand All @@ -289,7 +286,7 @@ impl AttributeParser {
afi: &Option<Afi>, safi: &Option<Safi>,
prefixes: &Option<Vec<NetworkPrefix>>,
reachable: bool,
) -> Result<Attribute, ParserErrorKind> {
) -> Result<AttributeValue, ParserErrorKind> {
let mut buf=Vec::with_capacity(input.limit() as usize);
input.read_to_end(&mut buf)?;
let first_byte_zero = buf[0]==0;
Expand Down Expand Up @@ -358,8 +355,8 @@ impl AttributeParser {

// Reserved field, should ignore
match reachable {
true => Ok(Attribute::MpReachNlri(Nlri {afi,safi, next_hop, prefixes})),
false => Ok(Attribute::MpUnreachNlri(Nlri {afi,safi, next_hop, prefixes}))
true => Ok(AttributeValue::MpReachNlri(Nlri {afi,safi, next_hop, prefixes})),
false => Ok(AttributeValue::MpUnreachNlri(Nlri {afi,safi, next_hop, prefixes}))
}
}

Expand Down Expand Up @@ -388,7 +385,7 @@ impl AttributeParser {
fn parse_large_communities<T: Read>(
&self,
input: &mut Take<T>,
) -> Result<Attribute, ParserErrorKind> {
) -> Result<AttributeValue, ParserErrorKind> {
let mut communities = Vec::new();
while input.limit() > 0 {
let global_administrator = input.read_u32::<BigEndian>()?;
Expand All @@ -398,13 +395,13 @@ impl AttributeParser {
];
communities.push(LargeCommunity::new(global_administrator, local_data));
}
Ok(Attribute::LargeCommunities(communities))
Ok(AttributeValue::LargeCommunities(communities))
}

fn parse_extended_community<T: Read>(
&self,
input: &mut Take<T>,
) -> Result<Attribute, ParserErrorKind> {
) -> Result<AttributeValue, ParserErrorKind> {
let mut communities = Vec::new();
while input.limit() > 0 {
let ec_type_u8 = input.read_8b()?;
Expand Down Expand Up @@ -432,7 +429,7 @@ impl AttributeParser {
ExtendedCommunity::TransitiveTwoOctetAsSpecific( TwoOctetAsSpecific{
ec_type: ec_type_u8,
ec_subtype: sub_type,
global_administrator: global as u32,
global_administrator: Asn{asn:global as i32, len: AsnLength::Bits16},
local_administrator: <[u8; 4]>::try_from(local).unwrap()
} )
}
Expand All @@ -443,7 +440,7 @@ impl AttributeParser {
ExtendedCommunity::NonTransitiveTwoOctetAsSpecific( TwoOctetAsSpecific{
ec_type: ec_type_u8,
ec_subtype: sub_type,
global_administrator: global as u32,
global_administrator: Asn{asn:global as i32, len: AsnLength::Bits16},
local_administrator: <[u8; 4]>::try_from(local).unwrap()
} )
}
Expand Down Expand Up @@ -477,7 +474,7 @@ impl AttributeParser {
ExtendedCommunity::TransitiveFourOctetAsSpecific( FourOctetAsSpecific{
ec_type: ec_type_u8,
ec_subtype: sub_type,
global_administrator: global,
global_administrator: Asn{asn:global as i32, len: AsnLength::Bits32},
local_administrator: <[u8; 2]>::try_from(local).unwrap()
} )
}
Expand All @@ -488,7 +485,7 @@ impl AttributeParser {
ExtendedCommunity::NonTransitiveFourOctetAsSpecific( FourOctetAsSpecific{
ec_type: ec_type_u8,
ec_subtype: sub_type,
global_administrator: global,
global_administrator: Asn{asn:global as i32, len: AsnLength::Bits32},
local_administrator: <[u8; 2]>::try_from(local).unwrap()
} )
}
Expand All @@ -514,13 +511,13 @@ impl AttributeParser {

communities.push(ec);
}
Ok(Attribute::ExtendedCommunities(communities))
Ok(AttributeValue::ExtendedCommunities(communities))
}

fn parse_ipv6_extended_community<T: Read>(
&self,
input: &mut Take<T>,
) -> Result<Attribute, ParserErrorKind> {
) -> Result<AttributeValue, ParserErrorKind> {
let mut communities = Vec::new();
while input.limit() > 0 {
let ec_type_u8 = input.read_8b()?;
Expand All @@ -537,7 +534,7 @@ impl AttributeParser {
);
communities.push(ec);
}
Ok(Attribute::ExtendedCommunities(communities))
Ok(AttributeValue::ExtendedCommunities(communities))
}
}

4 changes: 2 additions & 2 deletions src/parser/bgp/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ pub fn parse_bgp_notification_message<T: Read>(input: &mut T, bgp_msg_length: u6
})
}

pub fn parse_bgp_open_message<T: Read>(input: &mut T, ) -> Result<BgpOpenMessage, ParserErrorKind> {
pub fn parse_bgp_open_message<T: Read>(input: &mut T) -> Result<BgpOpenMessage, ParserErrorKind> {
let version = input.read_8b()?;
let asn = input.read_16b()? as Asn;
let asn = Asn{asn: input.read_16b()? as i32, len: AsnLength::Bits16};
let hold_time = input.read_16b()?;
let sender_ip = input.read_ipv4_address()?;
let opt_parm_len = input.read_8b()?;
Expand Down
3 changes: 2 additions & 1 deletion src/parser/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,9 @@ impl Filterable for BgpElem {
fn match_filter(&self, filter: &Filter) -> bool {
match filter {
Filter::OriginAsn(v) => {
let asn: Asn = (*v).into();
if let Some(origins) = &self.origin_asns {
origins.contains(v)
origins.contains(&asn)
} else {
false
}
Expand Down
Loading

0 comments on commit 552a22a

Please sign in to comment.