Skip to main content

rustbgpd_wire/
lib.rs

1//! rustbgpd-wire — BGP message codec
2//!
3//! Pure codec library for BGP message encoding and decoding.
4//! Zero internal dependencies. This crate is independently publishable.
5//!
6//! # Message Types
7//!
8//! - [`OpenMessage`] — BGP OPEN with capability negotiation
9//! - [`NotificationMessage`] — BGP NOTIFICATION with error codes
10//! - [`UpdateMessage`] — BGP UPDATE (wire-level framing, raw bytes in M0)
11//! - `Keepalive` — represented as [`Message::Keepalive`] unit variant
12//!
13//! # Entry Points
14//!
15//! - [`decode_message`] — decode a complete BGP message from bytes
16//! - [`encode_message`] — encode a BGP message to bytes
17//! - [`peek_message_length`] — check if a
18//!   complete message is available (for transport framing)
19//!
20//! # Invariants
21//!
22//! - Maximum message size: 4096 bytes (RFC 4271 §4.1)
23//! - No panics on malformed input — all paths return `Result`
24//! - No `unsafe` code
25
26#![deny(unsafe_code)]
27#![deny(clippy::all)]
28#![warn(clippy::pedantic)]
29
30/// Path attribute types and codec (`ORIGIN`, `AS_PATH`, `NEXT_HOP`, etc.).
31pub mod attribute;
32/// BGP capability negotiation types and codec (RFC 5492).
33pub mod capability;
34/// Wire-format constants: markers, lengths, type codes.
35pub mod constants;
36/// Decode and encode error types.
37pub mod error;
38/// FlowSpec NLRI types and codec (RFC 8955 / RFC 8956).
39pub mod flowspec;
40/// BGP message header codec (RFC 4271 §4.1).
41pub mod header;
42/// KEEPALIVE message encoding and validation.
43pub mod keepalive;
44/// Top-level BGP message enum and codec dispatch.
45pub mod message;
46/// NLRI prefix types and codec (IPv4, IPv6, Add-Path).
47pub mod nlri;
48/// NOTIFICATION error codes, subcodes, and shutdown communication.
49pub mod notification;
50/// NOTIFICATION message struct and codec.
51pub mod notification_msg;
52/// OPEN message struct and codec.
53pub mod open;
54/// ROUTE-REFRESH message struct and codec (RFC 2918 / RFC 7313).
55pub mod route_refresh;
56/// UPDATE message struct, codec, and builder.
57pub mod update;
58/// UPDATE attribute semantic validation (RFC 4271 §6.3).
59pub mod validate;
60
61// Re-export primary public API
62pub use capability::{
63    AddPathFamily, AddPathMode, Afi, Capability, ExtendedNextHopFamily, GracefulRestartFamily,
64    LlgrFamily, Safi,
65};
66pub use constants::{EXTENDED_MAX_MESSAGE_LEN, MAX_MESSAGE_LEN};
67pub use error::{DecodeError, EncodeError};
68pub use header::{BgpHeader, MessageType, peek_message_length};
69pub use message::{Message, decode_message, encode_message, encode_message_with_limit};
70pub use notification::NotificationCode;
71pub use notification_msg::NotificationMessage;
72pub use open::OpenMessage;
73pub use route_refresh::{RouteRefreshMessage, RouteRefreshSubtype};
74pub use update::{Ipv4UnicastMode, UpdateMessage};
75
76/// RPKI origin validation state per RFC 6811.
77#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
78pub enum RpkiValidation {
79    /// A VRP covers the prefix and the origin AS matches.
80    Valid,
81    /// A VRP covers the prefix but the origin AS does not match.
82    Invalid,
83    /// No VRP covers the prefix.
84    #[default]
85    NotFound,
86}
87
88impl std::fmt::Display for RpkiValidation {
89    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
90        match self {
91            Self::Valid => write!(f, "valid"),
92            Self::Invalid => write!(f, "invalid"),
93            Self::NotFound => write!(f, "not_found"),
94        }
95    }
96}
97
98impl std::str::FromStr for RpkiValidation {
99    type Err = String;
100
101    fn from_str(s: &str) -> Result<Self, Self::Err> {
102        match s {
103            "valid" => Ok(Self::Valid),
104            "invalid" => Ok(Self::Invalid),
105            "not_found" => Ok(Self::NotFound),
106            other => Err(format!(
107                "unknown RPKI validation state {other:?}, expected \"valid\", \"invalid\", or \"not_found\""
108            )),
109        }
110    }
111}
112
113// Re-export attribute types
114pub use attribute::{
115    AsPath, AsPathSegment, ExtendedCommunity, LargeCommunity, MpReachNlri, MpUnreachNlri, Origin,
116    PathAttribute, RawAttribute, is_private_asn,
117};
118pub use nlri::{Ipv4NlriEntry, Ipv4Prefix, Ipv6Prefix, NlriEntry, Prefix};
119pub use update::ParsedUpdate;
120pub use validate::{UpdateError, is_valid_ipv6_nexthop};
121
122// Re-export FlowSpec types
123pub use flowspec::{
124    BitmaskMatch, FlowSpecAction, FlowSpecComponent, FlowSpecPrefix, FlowSpecRule,
125    Ipv6PrefixOffset, NumericMatch,
126};
127
128// Well-known communities (RFC 1997 + RFC 9494)
129/// `LLGR_STALE` community (RFC 9494 §4.6): marks a route as long-lived stale.
130pub const COMMUNITY_LLGR_STALE: u32 = 0xFFFF_0006;
131/// `NO_LLGR` community (RFC 9494 §4.7): this route must not enter LLGR stale phase.
132pub const COMMUNITY_NO_LLGR: u32 = 0xFFFF_0007;
133
134// Re-export RPKI types
135// (RpkiValidation is defined above in this file)