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 addressFields§
§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
impl LayerContext
Trait Implementations§
Source§impl Clone for LayerContext
impl Clone for LayerContext
Source§fn clone(&self) -> LayerContext
fn clone(&self) -> LayerContext
Returns a duplicate of the value. Read more
1.0.0§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for LayerContext
impl Debug for LayerContext
Source§impl Default for LayerContext
impl Default for LayerContext
Source§fn default() -> LayerContext
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 Twhere
T: 'static + ?Sized,
impl<T> Any for Twhere
T: 'static + ?Sized,
§impl<T> Borrow<T> for Twhere
T: ?Sized,
impl<T> Borrow<T> for Twhere
T: ?Sized,
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§unsafe fn clone_to_uninit(&self, dest: *mut u8)
unsafe fn clone_to_uninit(&self, dest: *mut u8)
🔬This is a nightly-only experimental API. (
clone_to_uninit)