LayerContext

Struct LayerContext 

Source
pub struct LayerContext {
    pub info: BTreeMap<String, Vec<u8>>,
}
Expand description

Context passed between network layers for routing decisions

This structure carries routing information through the protocol stack, allowing each layer to add or consume information needed for proper packet delivery. This is designed to be protocol-agnostic and avoids hard-coding specific protocol fields like IP addresses.

§Design Philosophy

  • Protocol-agnostic: No IP addresses or protocol-specific fields in core structure
  • Flexible key-value store: Each protocol layer can add/read arbitrary data
  • Layer composition: Enables proper separation without tight coupling
  • Follows @petitstrawberry’s guidance: Generic, not tied to specific protocols

§Example Flow

// TCP layer creates context with its info
let mut ctx = LayerContext::new();
ctx.set("tcp_src_port", &5000u16.to_be_bytes());
ctx.set("tcp_dst_port", &80u16.to_be_bytes());

// TCP layer sends to IP, IP adds its info
let ip_src = [192, 168, 1, 100];
let ip_dst = [192, 168, 1, 1];
ctx.set("ip_src", &ip_src);
ctx.set("ip_dst", &ip_dst);
ctx.set("ip_protocol", &[6]); // TCP

// IP layer sends to Ethernet, Ethernet performs ARP
// Ethernet can read "ip_dst" to determine MAC address

Fields§

§info: BTreeMap<String, Vec<u8>>

Protocol-agnostic key-value store for routing information Each layer can add/read arbitrary data needed for packet delivery

Common keys (convention, not enforced):

  • “ip_src”, “ip_dst”: IPv4/IPv6 addresses
  • “tcp_src_port”, “tcp_dst_port”: TCP ports
  • “udp_src_port”, “udp_dst_port”: UDP ports
  • “ip_protocol”: Protocol number (6=TCP, 17=UDP)
  • “ttl”: Time-to-live
  • “tos”: Type of service

Implementations§

Source§

impl LayerContext

Source

pub fn new() -> Self

Create a new empty LayerContext

Source

pub fn set(&mut self, key: &str, value: &[u8])

Set a value in the context

Source

pub fn get(&self, key: &str) -> Option<&[u8]>

Get a value from the context

Source

pub fn contains(&self, key: &str) -> bool

Check if a key exists

Trait Implementations§

Source§

impl Clone for LayerContext

Source§

fn clone(&self) -> LayerContext

Returns a duplicate of the value. Read more
1.0.0§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for LayerContext

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for LayerContext

Source§

fn default() -> LayerContext

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for LayerContext

§

impl RefUnwindSafe for LayerContext

§

impl Send for LayerContext

§

impl Sync for LayerContext

§

impl Unpin for LayerContext

§

impl UnwindSafe for LayerContext

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> CloneToUninit for T
where T: Clone,

§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.