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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
use crate::parser::bmp::error::ParserBmpError;
use crate::parser::ReadUtils;
use bytes::{Buf, Bytes};

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

pub fn parse_peer_down_notification(
    data: &mut Bytes,
) -> Result<PeerDownNotification, ParserBmpError> {
    let reason = data.read_u8()?;
    let bytes_left = data.remaining();
    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(data.read_n_bytes(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(data.read_n_bytes(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(data.read_n_bytes(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 })
}

#[cfg(test)]
mod tests {
    use super::*;
    use bytes::BufMut;

    #[test]
    fn test_parse_peer_down_notification() {
        // Test with reason `1`
        let mut data = bytes::BytesMut::new();
        data.put_u8(1);
        data.put_slice(&[0u8; 10]);
        let mut data = data.freeze();
        let result = parse_peer_down_notification(&mut data);
        assert!(result.is_ok());
        let peer_down_notification = result.unwrap();
        assert_eq!(peer_down_notification.reason, 1);
        assert_eq!(peer_down_notification.data.unwrap(), vec![0u8; 10]);

        // Test with reason `2`
        let mut data = bytes::BytesMut::new();
        data.put_u8(2);
        data.put_slice(&[0u8; 10]);
        let mut data = data.freeze();
        let result = parse_peer_down_notification(&mut data);
        assert!(result.is_ok());
        let peer_down_notification = result.unwrap();
        assert_eq!(peer_down_notification.reason, 2);
        assert_eq!(peer_down_notification.data.unwrap(), vec![0u8; 10]);

        // Test with reason `3`
        let mut data = bytes::BytesMut::new();
        data.put_u8(3);
        data.put_slice(&[0u8; 10]);
        let mut data = data.freeze();
        let result = parse_peer_down_notification(&mut data);
        assert!(result.is_ok());
        let peer_down_notification = result.unwrap();
        assert_eq!(peer_down_notification.reason, 3);
        assert_eq!(peer_down_notification.data.unwrap(), vec![0u8; 10]);

        // Test with reason `4`
        let mut data = bytes::BytesMut::new();
        data.put_u8(4);
        let mut data = data.freeze();
        let result = parse_peer_down_notification(&mut data);
        assert!(result.is_ok());
        let peer_down_notification = result.unwrap();
        assert_eq!(peer_down_notification.reason, 4);
        assert!(peer_down_notification.data.is_none());

        // Test with reason `5`
        let mut data = bytes::BytesMut::new();
        data.put_u8(5);
        let mut data = data.freeze();
        let result = parse_peer_down_notification(&mut data);
        assert!(result.is_ok());
        let peer_down_notification = result.unwrap();
        assert_eq!(peer_down_notification.reason, 5);
        assert!(peer_down_notification.data.is_none());

        // Test with invalid reason
        let mut data = bytes::BytesMut::new();
        data.put_u8(6);
        let mut data = data.freeze();
        let result = parse_peer_down_notification(&mut data);
        assert!(result.is_err());
    }
}