Skip to main content

NetworkStack

Struct NetworkStack 

Source
pub struct NetworkStack<D: NetworkDevice> { /* private fields */ }
Expand description

Minimal network stack for no_std environments.

This stack supports:

  • Ethernet II frames
  • ARP resolution
  • IPv4 routing
  • UDP datagrams
  • ICMP echo (ping)

Implementations§

Source§

impl<D: NetworkDevice> NetworkStack<D>

Source

pub fn new(device: D, config: StackConfig) -> Self

Creates a new network stack.

Source

pub fn mac_address(&self) -> MacAddress

Returns the MAC address of the device.

Source

pub const fn ip_address(&self) -> Ipv4Addr

Returns the IP address.

Source

pub const fn state(&self) -> StackState

Returns the stack state.

Source

pub const fn arp_cache(&self) -> &ArpCache

Returns a reference to the ARP cache.

Source

pub fn tick(&mut self, current_time: u64)

Updates the current time (must be called periodically).

Source

pub fn is_local(&self, dest: Ipv4Addr) -> bool

Determines if a destination is on the local network.

Source

pub fn resolve_next_hop( &mut self, dest: Ipv4Addr, ) -> NetResult<Option<MacAddress>>

Gets the next-hop MAC address for a destination.

If the destination is local, resolves via ARP. If remote, returns the gateway’s MAC.

Source

pub fn send_arp_request(&mut self, target_ip: Ipv4Addr) -> NetResult<()>

Sends an ARP request for the given IP.

Source

pub fn send_udp( &mut self, src_port: u16, dst_ip: Ipv4Addr, dst_port: u16, payload: &[u8], ) -> NetResult<()>

Sends a UDP datagram.

Returns NetError::ArpNotFound if the destination MAC is not cached. In this case, an ARP request has been sent and the caller should retry.

Source

pub fn send_ping( &mut self, dst_ip: Ipv4Addr, identifier: u16, sequence: u16, data: &[u8], ) -> NetResult<()>

Sends an ICMP echo request (ping).

Source

pub fn receive(&mut self, buf: &mut [u8]) -> NetResult<Option<ReceivedPacket>>

Processes a received frame.

Returns information about the received packet if it contains application-layer data (e.g., UDP payload).

Source

pub const fn device(&self) -> &D

Returns a reference to the underlying device.

Source

pub fn device_mut(&mut self) -> &mut D

Returns a mutable reference to the underlying device.

Auto Trait Implementations§

§

impl<D> Freeze for NetworkStack<D>
where D: Freeze,

§

impl<D> RefUnwindSafe for NetworkStack<D>
where D: RefUnwindSafe,

§

impl<D> Send for NetworkStack<D>
where D: Send,

§

impl<D> Sync for NetworkStack<D>
where D: Sync,

§

impl<D> Unpin for NetworkStack<D>
where D: Unpin,

§

impl<D> UnsafeUnpin for NetworkStack<D>
where D: UnsafeUnpin,

§

impl<D> UnwindSafe for NetworkStack<D>
where D: UnwindSafe,

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

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

Source§

type Error = Infallible

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

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

Performs the conversion.
Source§

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

Source§

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

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

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

Performs the conversion.