pub struct Packet {
pub header: Header<Ipv4Address>,
pub pdu: Vec<Segment>,
}Expand description
Represents a Network Layer Packet. It contains one or more Segments.
Fields§
§header: Header<Ipv4Address>§pdu: Vec<Segment>Implementations§
Source§impl Packet
impl Packet
Sourcepub fn new(header: Header<Ipv4Address>, pdu: Vec<Segment>) -> Self
pub fn new(header: Header<Ipv4Address>, pdu: Vec<Segment>) -> Self
Constructs a new instance of the layer.
Examples found in repository?
examples/main_tester.rs (lines 23-26)
6fn main() {
7 println!("--- DTP Simple Encapsulation Example ---");
8
9 // 1. The Payload: Start with the data you want to send.
10 let my_payload = Bytes::from_static(b"Hello, DTP!");
11 println!("\n[1] Application Data: '{}'", String::from_utf8_lossy(&my_payload));
12
13 // 2. Transport Layer: Encapsulate the data in a Segment.
14 // This layer adds source and destination port numbers.
15 let transport_segment = Segment::new(
16 Header::new(54321, 443), // Source Port (dynamic) -> Destination Port (HTTPS)
17 my_payload,
18 );
19 println!("\n[2] Encapsulated into a Transport Segment...");
20
21 // 3. Network Layer: Place the Segment into a Packet.
22 // This layer adds source and destination IP addresses.
23 let network_packet = Packet::new(
24 Header::new(0x7F000001, 0x08080808), // 127.0.0.1 -> 8.8.8.8 (Google DNS)
25 vec![transport_segment],
26 );
27 println!("\n[3] Encapsulated into a Network Packet...");
28
29 // 4. Data Link Layer: Place the Packet into a Frame.
30 // This is the final layer, adding MAC addresses for the local network link.
31 let datalink_frame = Frame::new(
32 Header::new(
33 [0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF], // Your device's MAC Address
34 [0x11, 0x22, 0x33, 0x44, 0x55, 0x66], // The gateway/router's MAC Address
35 ),
36 vec![network_packet],
37 );
38 println!("\n[4] Encapsulated into a Data Link Frame...");
39
40 // 5. Final Result: Print the fully constructed frame.
41 // The custom Display implementation shows the beautiful, nested structure.
42 println!("\n--- Final Frame Ready for Transmission ---\n");
43 println!("{}", datalink_frame);
44}Trait Implementations§
Source§impl<'a> IntoIterator for &'a Packet
impl<'a> IntoIterator for &'a Packet
Source§impl IntoIterator for Packet
impl IntoIterator for Packet
impl StructuralPartialEq for Packet
Auto Trait Implementations§
impl Freeze for Packet
impl RefUnwindSafe for Packet
impl Send for Packet
impl Sync for Packet
impl Unpin for Packet
impl UnwindSafe for Packet
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more