use crate::{bgp::message::OpenMessage, typeenum};
use bytes::Bytes;
use log::debug;
use std::time::Instant;
use super::session::BasicConfig;
#[cfg(feature = "serde")]
use serde::{Serialize, Deserialize};
#[derive(Clone, Copy, Debug)]
#[allow(dead_code)]
pub struct SessionAttributes {
state: State, connect_retry_counter: usize,
connect_retry_timer: u16, connect_retry_time: u16, connect_retry_last_tick: Option<Instant>, hold_timer: u16, hold_time: u16, keepalive_timer: u16,
keepalive_time: u16,
delay_open: bool, delay_open_time: u16,
passive_tcp_establishment: bool,
send_notification_without_open: bool,
}
impl SessionAttributes {
pub fn new() -> Self {
Self::default()
}
pub fn from_basic_config(config: &BasicConfig) -> Self {
let mut res = Self::default();
if let Some(hold_time) = config.hold_time {
res.set_hold_time(hold_time);
}
res
}
pub fn from_bgp_config<C: super::session::BgpConfig>(config: &C) -> Self {
let mut res = Self::default();
if let Some(hold_time) = config.hold_time() {
res.set_hold_time(hold_time);
}
res
}
pub const fn hold_time(&self) -> u16 {
self.hold_time
}
pub fn set_hold_time(&mut self, hold_time: u16) {
self.hold_time = hold_time;
}
pub const fn state(self) -> State {
self.state
}
pub const fn connect_retry_time(&self) -> u16 {
self.connect_retry_time
}
pub fn reset_connect_retry_counter(&mut self) {
self.connect_retry_counter = 0;
}
pub fn increase_connect_retry_counter(&mut self) {
self.connect_retry_counter += 1;
}
pub fn enable_delay_open(&mut self) {
self.delay_open = true;
}
pub const fn delay_open(&self) -> bool {
self.delay_open
}
pub const fn delay_open_time(&self) -> u16 {
self.delay_open_time
}
pub const fn passive_tcp_establishment(&self) -> bool {
self.passive_tcp_establishment
}
pub const fn notification_without_open(&self) -> bool {
self.send_notification_without_open
}
pub fn set_state(&mut self, state: State) {
debug!("FSM {:?} -> {:?}", &self.state, state);
self.state = state;
}
}
impl Default for SessionAttributes {
fn default() -> Self {
Self {
state: State::Idle,
connect_retry_counter: 0,
connect_retry_timer: 120,
connect_retry_time: 120,
connect_retry_last_tick: None,
hold_timer: 90,
hold_time: 90,
keepalive_timer: 30,
keepalive_time: 30,
delay_open: false,
delay_open_time: 10,
passive_tcp_establishment: true,
send_notification_without_open: true,
}
}
}
typeenum!(State, u16,
{
1 => Idle,
2 => Connect,
3 => Active,
4 => OpenSent,
5 => OpenConfirm,
6 => Established,
}
);
#[derive(Clone, Debug)]
pub enum Event {
ManualStart, ManualStop,
AutomaticStart, ManualStartWithPassiveTcpEstablishment, AutomaticStartWithPassiveTcpEstablishment,
ConnectRetryTimerExpires, HoldTimerExpires, KeepaliveTimerExpires,
DelayOpenTimerExpires,
TcpCrAcked, TcpConnectionConfirmed, TcpConnectionFails,
BgpOpen(OpenMessage<Bytes>),
BgpHeaderErr, BgpOpenMsgErr,
NotifMsgVerErr, NotifMsg, KeepaliveMsg, UpdateMsg, UpdateMsgErr,
BgpOpenWithDelayOpenTimerRunning(OpenMessage<Bytes>),
}