Skip to main content

ruvix_net/
lib.rs

1//! # RuVix Network Stack
2//!
3//! This crate provides a minimal networking stack for the RuVix Cognition Kernel
4//! as specified in ADR-087 Phase E. It is designed to be `no_std` compatible with
5//! optional alloc support for dynamic allocation.
6//!
7//! ## Network Layers
8//!
9//! | Layer | Module | Purpose |
10//! |-------|--------|---------|
11//! | **Link** | `ethernet` | Ethernet II frame handling |
12//! | **Network** | `arp`, `ipv4`, `icmp` | Address resolution and IP routing |
13//! | **Transport** | `udp` | Connectionless datagram transport |
14//!
15//! ## Architecture
16//!
17//! ```text
18//! +------------------+
19//! |   Application    |
20//! +------------------+
21//!          |
22//! +------------------+
23//! |       UDP        |
24//! +------------------+
25//!          |
26//! +------------------+
27//! |    IPv4 + ICMP   |
28//! +------------------+
29//!          |
30//! +------------------+
31//! |  ARP Resolution  |
32//! +------------------+
33//!          |
34//! +------------------+
35//! |    Ethernet      |
36//! +------------------+
37//!          |
38//! +------------------+
39//! |  NetworkDevice   |  <-- Hardware abstraction
40//! +------------------+
41//! ```
42//!
43//! ## Features
44//!
45//! - `std`: Enable standard library support
46//! - `alloc`: Enable alloc crate support for heap allocation
47//!
48//! ## Example
49//!
50//! ```no_run
51//! use ruvix_net::{MacAddress, Ipv4Addr, UdpSocket, NetworkStack};
52//!
53//! // Create a network stack with device
54//! // let stack = NetworkStack::new(device, mac, ip);
55//!
56//! // Send UDP datagram
57//! // let socket = stack.udp_bind(8080).unwrap();
58//! // socket.send_to(&data, dest_addr, dest_port);
59//! ```
60
61#![no_std]
62#![forbid(unsafe_code)]
63#![deny(missing_docs)]
64#![deny(clippy::all)]
65#![warn(clippy::pedantic)]
66#![allow(clippy::module_name_repetitions)]
67
68#[cfg(feature = "alloc")]
69extern crate alloc;
70
71#[cfg(feature = "std")]
72extern crate std;
73
74pub mod arp;
75pub mod device;
76pub mod error;
77pub mod ethernet;
78pub mod icmp;
79pub mod ipv4;
80pub mod stack;
81pub mod udp;
82
83// Re-exports for convenience
84pub use arp::{ArpCache, ArpOperation, ArpPacket};
85pub use device::NetworkDevice;
86pub use error::NetError;
87pub use ethernet::{EtherType, EthernetFrame, MacAddress};
88pub use icmp::{IcmpHeader, IcmpType};
89pub use ipv4::{Ipv4Addr, Ipv4Header, Protocol};
90pub use stack::NetworkStack;
91pub use udp::{UdpHeader, UdpSocket};
92
93/// Maximum Transmission Unit (standard Ethernet).
94pub const MTU: usize = 1500;
95
96/// Ethernet header size in bytes.
97pub const ETHERNET_HEADER_SIZE: usize = 14;
98
99/// IPv4 header minimum size in bytes.
100pub const IPV4_HEADER_MIN_SIZE: usize = 20;
101
102/// UDP header size in bytes.
103pub const UDP_HEADER_SIZE: usize = 8;
104
105/// ICMP header size in bytes.
106pub const ICMP_HEADER_SIZE: usize = 8;
107
108/// ARP packet size in bytes (for Ethernet/IPv4).
109pub const ARP_PACKET_SIZE: usize = 28;
110
111/// Maximum UDP payload size for standard Ethernet.
112pub const MAX_UDP_PAYLOAD: usize = MTU - IPV4_HEADER_MIN_SIZE - UDP_HEADER_SIZE;