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>
impl<D: NetworkDevice> NetworkStack<D>
Sourcepub fn new(device: D, config: StackConfig) -> Self
pub fn new(device: D, config: StackConfig) -> Self
Creates a new network stack.
Sourcepub fn mac_address(&self) -> MacAddress
pub fn mac_address(&self) -> MacAddress
Returns the MAC address of the device.
Sourcepub const fn ip_address(&self) -> Ipv4Addr
pub const fn ip_address(&self) -> Ipv4Addr
Returns the IP address.
Sourcepub const fn state(&self) -> StackState
pub const fn state(&self) -> StackState
Returns the stack state.
Sourcepub fn tick(&mut self, current_time: u64)
pub fn tick(&mut self, current_time: u64)
Updates the current time (must be called periodically).
Sourcepub fn is_local(&self, dest: Ipv4Addr) -> bool
pub fn is_local(&self, dest: Ipv4Addr) -> bool
Determines if a destination is on the local network.
Sourcepub fn resolve_next_hop(
&mut self,
dest: Ipv4Addr,
) -> NetResult<Option<MacAddress>>
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.
Sourcepub fn send_arp_request(&mut self, target_ip: Ipv4Addr) -> NetResult<()>
pub fn send_arp_request(&mut self, target_ip: Ipv4Addr) -> NetResult<()>
Sends an ARP request for the given IP.
Sourcepub fn send_udp(
&mut self,
src_port: u16,
dst_ip: Ipv4Addr,
dst_port: u16,
payload: &[u8],
) -> NetResult<()>
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.
Sourcepub fn send_ping(
&mut self,
dst_ip: Ipv4Addr,
identifier: u16,
sequence: u16,
data: &[u8],
) -> NetResult<()>
pub fn send_ping( &mut self, dst_ip: Ipv4Addr, identifier: u16, sequence: u16, data: &[u8], ) -> NetResult<()>
Sends an ICMP echo request (ping).
Sourcepub fn receive(&mut self, buf: &mut [u8]) -> NetResult<Option<ReceivedPacket>>
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).
Sourcepub fn device_mut(&mut self) -> &mut D
pub fn device_mut(&mut self) -> &mut D
Returns a mutable reference to the underlying device.