Skip to content

Commit

Permalink
stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
MalteJ committed Nov 23, 2024
1 parent ef0de56 commit 97e46fe
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 23 deletions.
22 changes: 11 additions & 11 deletions src/febgp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,6 @@ use std::str::FromStr;
use crate::session::BgpSession;


/// Represents a BGP peer, which can be an interface or an IP address (IPv4/IPv6).
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub enum BgpPeer {
Interface(String),
Ipv4Address(Ipv4Addr),
Ipv6Address(Ipv6Addr),
}


/// BGP Daemon is the FeBGP daemon. It's the best.
pub struct BgpDaemon {
params: BgpSessionParams,
Expand Down Expand Up @@ -53,7 +43,7 @@ impl BgpDaemon {
let mut session = BgpSession::new(params, rx, tx, peer);

tokio::spawn( async move {
session.start().await;
session.start().await.unwrap();
});
}

Expand All @@ -66,6 +56,16 @@ impl BgpDaemon {
}


/// Represents a BGP peer, which can be an interface or an IP address (IPv4/IPv6).
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub enum BgpPeer {
Interface(String),
Ipv4Address(Ipv4Addr),
Ipv6Address(Ipv6Addr),
}


#[derive(Debug, PartialEq)]
pub enum Prefix {
V4(Ipv4Addr, u8), // Holds an IPv4 address and prefix length
Expand Down
51 changes: 39 additions & 12 deletions src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ impl BgpSession {
Ok(_) => {
let message_head = params.decode_message_head(&buf).unwrap();
match message_head.0 {

// OPEN
BgpMessageType::Open => {
socket.read_exact(&mut buf[0..message_head.1]).unwrap();
let mut msg = BgpOpenMessage::new();
Expand All @@ -91,10 +93,14 @@ impl BgpSession {
msg.caps);
tx.send(BgpEvent::OpenMessageReceived).unwrap();
},

// KEEPALIVE
BgpMessageType::Keepalive => {
debug!("Keepalive received from {}", socket.peer_addr().unwrap());
tx.send(BgpEvent::KeepaliveReceived).unwrap();
},

// UPDATE
BgpMessageType::Update => {
socket.read_exact(&mut buf[0..message_head.1]).unwrap();
debug!("Update received from {}", socket.peer_addr().unwrap());
Expand All @@ -106,6 +112,8 @@ impl BgpSession {

tx.send(BgpEvent::UpdateMessageReceived).unwrap();
},

// NOTIFICATION
BgpMessageType::Notification => {
socket.read_exact(&mut buf[0..message_head.1]).unwrap();
let mut msg = BgpNotificationMessage::new();
Expand Down Expand Up @@ -162,16 +170,24 @@ impl BgpSession {
self.rx_event.recv().unwrap()
}

pub async fn start(&mut self) {
self.status = BgpState::Idle;
pub async fn start(&mut self) -> Result<(), String> {
if self.status != BgpState::Idle {
return Err(String::from("BGP FSM not in idle state!"));
}
self.tx_event.send(BgpEvent::ManualStart).expect("TODO: panic message");
debug!("YOLO!");
self.run_fsm().await;

Ok(())
}

async fn run_fsm(&mut self) {
loop {
match self.status {

BgpState::Idle => {
info!("BGP State: Idle");
match self.get_event().await {

BgpEvent::ManualStart => {
debug!("BGP Event: ManualStart");
self.status = self.connect().await;
Expand All @@ -183,42 +199,48 @@ impl BgpSession {
}
}

BgpState::Active => {
info!("BGP State: Active");
info!("Session entered Active state");
self.status = self.connect().await
}

BgpState::Connect => {
info!("BGP State: Connect");
match self.get_event().await {

BgpEvent::TransportEstablished => {
debug!("BGP Event: TransportEstablished");
self.status = self.send_open().await;
},

BgpEvent::TransportClosed => {
debug!("BGP Event: TransportClosed");
self.status = BgpState::Active;
self.status = self.connect().await;
},

event => {
error!("Unexpected event received. State: {:?}, Event: {:?}", self.status, event);
self.status = self.reset_connection();
},
}
}

BgpState::Active => {
info!("BGP State: Active");
info!("Session entered Active state");
self.status = self.connect().await
}

BgpState::OpenSent => {
info!("BGP State: OpenSent");
match self.get_event().await {

BgpEvent::OpenMessageSent => {
debug!("BGP Event: OpenMessageSent");
}

BgpEvent::OpenMessageReceived => {
debug!("BGP Event: OpenMessageReceived");
self.status = BgpState::OpenConfirm;
self.send_keepalive().await;
}

event => {
error!("Unexpected event received. State: {:?}, Event: {:?}", self.status, event);
self.status = self.reset_connection();
Expand All @@ -229,11 +251,13 @@ impl BgpSession {
BgpState::OpenConfirm => {
info!("BGP State: OpenConfirm");
match self.get_event().await {

BgpEvent::KeepaliveReceived => {
debug!("BGP Event: KeepaliveReceived");
self.status = BgpState::Established;
self.send_updates().await;
}

event => {
error!("Unexpected event received. State: {:?}, Event: {:?}", self.status, event);
self.status = self.reset_connection();
Expand All @@ -244,14 +268,17 @@ impl BgpSession {
BgpState::Established => {
info!("BGP State: Established");
match self.get_event().await {

BgpEvent::KeepaliveReceived => {
debug!("BGP Event: KeepaliveReceived");
self.send_keepalive().await;
}

BgpEvent::UpdateMessageReceived => {
debug!("BGP Event: UpdateMessageReceived");
// TODO: Implement
}

event => {
error!("Unexpected event received. State: {:?}, Event: {:?}", self.status, event);
self.status = self.reset_connection();
Expand All @@ -268,12 +295,12 @@ pub enum BgpState {
/// The initial state where BGP is idle and not attempting to connect.
Idle,

/// BGP is attempting to establish a TCP connection.
Connect,

/// BGP actively retries to connect after a failed attempt.
Active,

/// BGP is attempting to establish a TCP connection.
Connect,

/// BGP has sent an OPEN message and is waiting for a response.
OpenSent,

Expand Down

0 comments on commit 97e46fe

Please sign in to comment.