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
// Copyright 2021 Vladimir Melnikov.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! This module contains BGP messages
use crate::error::*;
use crate::*;
pub mod attributes;
pub mod keepalive;
pub mod notification;
pub mod open;
pub use open::*;
pub mod update;
pub use update::*;
/// trait BgpMessage represents BGP protocol message
pub trait BgpMessage {
fn decode_from(&mut self, peer: &BgpSessionParams, buf: &[u8]) -> Result<(), BgpError>;
fn encode_to(&self, peer: &BgpSessionParams, buf: &mut [u8]) -> Result<usize, BgpError>;
}
/// Bgp message type: open, update, notification or keepalive.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BgpMessageType {
Open,
Update,
Notification,
Keepalive,
}
impl BgpMessageType {
/// decodes BGP message type from byte code
pub fn decode_from(code: u8) -> Result<BgpMessageType, BgpError> {
match code {
1 => Ok(BgpMessageType::Open),
2 => Ok(BgpMessageType::Update),
3 => Ok(BgpMessageType::Notification),
4 => Ok(BgpMessageType::Keepalive),
_ => Err(BgpError::static_str("Invalid message type")),
}
}
/// encodes BGP message type into the byte code
pub fn encode(&self) -> u8 {
match self {
BgpMessageType::Open => 1,
BgpMessageType::Update => 2,
BgpMessageType::Notification => 3,
BgpMessageType::Keepalive => 4,
}
}
}