Skip to main content

bgp_packet/
parser.rs

1use std::fmt::Display;
2
3use nom::error::{ErrorKind, ParseError};
4
5use crate::constants::AddressFamilyId;
6
7#[derive(Debug, Default)]
8pub struct ParserContext {
9    /// Whether thi parser is being run with a peer that is RFC6793 compliant.
10    pub four_octet_asn: Option<bool>,
11    /// Which address family should be parsed by default with this parser.
12    pub address_family: Option<AddressFamilyId>,
13}
14
15#[derive(Debug)]
16pub enum ToWireError {
17    /// There was not enough space in the output buffer to serialize the data into.
18    OutBufferOverflow,
19    /// Another error.
20    Other(eyre::Error),
21}
22
23impl Display for ToWireError {
24    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
25        match self {
26            ToWireError::OutBufferOverflow => write!(f, "OutBufferOverflow"),
27            ToWireError::Other(report) => report.fmt(f),
28        }
29    }
30}
31
32impl std::error::Error for ToWireError {}
33
34// Custom error type for the parser.
35#[derive(Debug)]
36pub enum BgpParserError<I> {
37    /// ProtocolError encapsulates protocol level semantics problems that require
38    /// action from the caller of the parser.
39    ProtocolError(ProtocolErrorValues),
40    CustomText(&'static str),
41    Eyre(eyre::ErrReport),
42    Nom(I, ErrorKind),
43}
44
45impl<I> ParseError<I> for BgpParserError<I> {
46    fn from_error_kind(input: I, kind: ErrorKind) -> Self {
47        BgpParserError::Nom(input, kind)
48    }
49    fn append(_: I, _: ErrorKind, other: Self) -> Self {
50        other
51    }
52}
53
54#[derive(Debug)]
55pub enum ProtocolErrorValues {
56    /// `UnsupportedVersion` is returned when the BGP Open message received is for a
57    /// version other than one we support. The unsupported version number is the value.
58    UnsupportedVersion(u8),
59    /// `UnknownOpenOption` is returned with the open option type code that is unknown.
60    UnknownOpenOption(u8),
61}