Skip to content

feat: BGP (Border Gateway Protocol) Header Support#59

Open
jecsand838 wants to merge 11 commits into
vadorovsky:mainfrom
elastiflow:bgp
Open

feat: BGP (Border Gateway Protocol) Header Support#59
jecsand838 wants to merge 11 commits into
vadorovsky:mainfrom
elastiflow:bgp

Conversation

@jecsand838

@jecsand838 jecsand838 commented Jun 6, 2025

Copy link
Copy Markdown
Contributor

This PR introduces initial support for BGP (Border Gateway Protocol version 4) by adding the src/bgp.rs file. This lays the groundwork for structuring BGP messages within the network-types library.

Key Features Added:

  • BgpHdr Struct:

    • Represents the common BGP message header (19 octets: marker, length, type) as defined in RFC 4271, Section 4.1.
    • Includes an additional fixed-size byte array (specific_payload_bytes) to hold the initial fixed portion of message-specific payloads (i.e., for OPEN, UPDATE, NOTIFICATION, ROUTE-REFRESH messages). This design avoids heap allocations and unsafe unions while allowing access to these fields.
    • Provides methods to get/set common header fields (marker, length, msg_type).
  • Message-Specific Layout Structs:

    • OpenMsgLayout: Defines offsets and total length for fixed fields in BGP OPEN messages (RFC 4271, Section 4.2).
    • UpdateInitialMsgLayout: Defines offsets and total length for the initial fixed field (Withdrawn Routes Length) in BGP UPDATE messages (RFC 4271, Section 4.3).
    • NotificationMsgLayout: Defines offsets and total length for fixed fields in BGP NOTIFICATION messages (RFC 4271, Section 4.5).
    • RouteRefreshMsgLayout: Defines offsets and total length for fixed fields in BGP ROUTE-REFRESH messages (RFC 2918, Section 3).
  • Accessor Methods for Message-Specific Fields:

    • BgpHdr now includes methods to safely get and set fields for OPEN, UPDATE (initial part), NOTIFICATION, and ROUTE-REFRESH messages.
    • These methods internally use the msg_type to correctly interpret the specific_payload_bytes array based on the corresponding MsgLayout struct.
    • Getters return Option<T> to handle cases where the msg_type doesn't match or if the payload is unexpectedly short.
    • Setters return bool indicating success or failure ((i.e., if msg_type is incorrect or payload is too short).
    • The update_total_path_attr_len and set_update_total_path_attr_len methods are provided to access the "Total Path Attribute Length" field in UPDATE messages, which requires the full message byte slice due to the preceding variable-length "Withdrawn Routes" field.
  • Constants:

    • Defines message type constants (e.g., BGP_OPEN_MSG_TYPE).
  • Unit Tests


This change is Reviewable

@jecsand838

Copy link
Copy Markdown
Contributor Author

CC: @svencowart

@jecsand838 jecsand838 changed the title feat: Introduce BGP (Border Gateway Protocol) parsing structures feat: BGP (Border Gateway Protocol) Header Support Jun 9, 2025
Comment thread src/bgp.rs Outdated
Comment thread src/bgp.rs Outdated
Comment thread src/bgp.rs Outdated
Comment thread src/bgp.rs Outdated
Comment thread src/bgp.rs Outdated
Comment thread src/bgp.rs Outdated
Comment thread src/bgp.rs Outdated
Comment thread src/bgp.rs Outdated
@jecsand838
jecsand838 requested a review from svencowart June 10, 2025 06:11
jecsand838 and others added 11 commits June 30, 2025 09:57
Co-authored-by: Sven Cowart <svencowart@gmail.com>
Co-authored-by: Sven Cowart <svencowart@gmail.com>
…and extensibility in handling BGP UPDATE message components.
… efficient parsing, manipulation, and iteration of BGP message components.
…ations with explicit `new` methods for clarity and added zero-initialized constructors across key layouts.
@veronica-m-ef

Copy link
Copy Markdown
Contributor

@jecsand838 based on the changes, I suggest this text to be the new PR description.

This PR introduces initial support for BGP (Border Gateway Protocol version 4) by adding the src/bgp.rs file, laying the groundwork for structuring BGP messages within the network-types library. The design prioritizes type safety, explicit structure, and efficient parsing.

Key Features Added:

  • BgpHdr Enum:

    • Introduces an enum BgpHdr to represent the different types of BGP messages (Open, Update, Notification, KeepAlive, RouteRefresh). This provides a type-safe and ergonomic way to handle various BGP message headers.
  • BgpFixedHdr Struct:

    • Defines the BgpFixedHdr struct, which encapsulates the common 19-octet BGP message header (marker, length, type) as specified in RFC 4271, Section 4.1.
    • Includes methods to get and set common header fields (marker, length, msg_type).
  • Message-Specific Fixed Header Structs:

    • Introduces dedicated structs, each marked with #[repr(C, packed)], for the fixed-size portions of specific BGP message types:
      • BgpOpenFixedHdr: For BGP OPEN messages (RFC 4271, Section 4.2).
      • BgpUpdateFixedHdr: For the initial fixed fields (Withdrawn Routes Length, Total Path Attribute Length) in BGP UPDATE messages (RFC 4271, Section 4.3).
      • BgpNotificationFixedHdr: For BGP NOTIFICATION messages (RFC 4271, Section 4.5).
      • BgpRouteRefreshFixedHdr: For BGP ROUTE-REFRESH messages (RFC 2918, Section 3).
    • These structs contain the fields specific to their message type and provide direct accessor methods.
  • Composite BGP Header Structs:

    • New structs such as BgpOpenHdr, BgpUpdateHdr, BgpNotificationHdr, BgpKeepAliveHdr, and BgpRouteRefreshHdr are introduced.
    • Each of these composite structs combines the BgpFixedHdr with its respective message-specific fixed header (e.g., BgpOpenHdr contains fixed_hdr: BgpFixedHdr and bgp_open_fixed_hdr: BgpOpenFixedHdr).
    • They provide accessor methods for all fields within their scope, offering type-safe and direct access without the need for byte array interpretation.
  • parse_bgp_hdr! Macro:

    • A new macro parse_bgp_hdr! is added to simplify the parsing of a byte slice into the appropriate BgpHdr enum variant. This macro efficiently handles the common fixed header and then dispatches to the correct message-specific fixed header parsing based on the message type.
  • BgpMsgType Enum:

    • Defines a type-safe BgpMsgType enum (Open, Update, Notification, KeepAlive, RouteRefresh) for clearer representation and usage of BGP message types.
  • BgpError Enum:

    • Introduces a BgpError enum for structured error reporting related to BGP header parsing and field access (e.g., IncorrectMessageType, BufferTooShort).
  • Unit Tests:

    • Includes comprehensive unit tests to ensure the correctness of header parsing, field accessors, and the overall structure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants