1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use crate::parser::bmp::error::ParserBmpError;
use crate::parser::DataBytes;

#[derive(Debug)]
pub struct PeerDownNotification {
    pub reason: u8,
    pub data: Option<Vec<u8>>,
}

pub fn parse_peer_down_notification(reader: &mut DataBytes) -> Result<PeerDownNotification, ParserBmpError> {
    let reason = reader.read_8b()?;
    let data: Option<Vec<u8>> = match reason {
        1 => {
            /*
            The local system closed the session.  Following the
            Reason is a BGP PDU containing a BGP NOTIFICATION message that
            would have been sent to the peer.
            */
            Some(reader.read_n_bytes(reader.bytes_left())?)
        },
        2 => {
            /*
            The local system closed the session.  No notification
            message was sent.  Following the reason code is a 2-byte field
            containing the code corresponding to the Finite State Machine
            (FSM) Event that caused the system to close the session (see
            Section 8.1 of [RFC4271]).  Two bytes both set to 0 are used to
            indicate that no relevant Event code is defined.
             */
            Some(reader.read_n_bytes(reader.bytes_left())?)
        },
        3 => {
            /*
            The remote system closed the session with a notification
            message.  Following the Reason is a BGP PDU containing the BGP
            NOTIFICATION message as received from the peer.
             */
            Some(reader.read_n_bytes(reader.bytes_left())?)
        },
        4 => {
            /*
            The remote system closed the session without a
            notification message.  This includes any unexpected termination of
            the transport session, so in some cases both the local and remote
            systems might consider this to apply.
             */
            None
        },
        5 => {
            /*
            Information for this peer will no longer be sent to the
            monitoring station for configuration reasons.  This does not,
            strictly speaking, indicate that the peer has gone down, but it
            does indicate that the monitoring station will not receive updates
            for the peer.
             */
            None
        },
        _ => {
            return Err(ParserBmpError::CorruptedBmpMessage)
        }
    };
    Ok(PeerDownNotification{
        reason,
        data
    })
}