1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright (c) 2024 Cyprien Avico <avicocyprien@yahoo.com>
//
// Licensed under the MIT License <LICENSE-MIT or http://opensource.org/licenses/MIT>.
// This file may not be copied, modified, or distributed except according to those terms.
//! # Packet Parser
//!
//! **Packet Parser** is a modular Rust library designed to analyze and decode raw network packets.
//!
//! This crate allows processing different layers of a network packet, starting from the data link layer
//! (Ethernet II) and moving down through the network, transport, and application layers.
//!
//! ## Features
//! - **Multi-layer analysis**: Supports data link, network, transport, and application layers.
//! - **Error management**: Detailed error handling to facilitate debugging.
//! - **Packet validation**: Built-in verification mechanisms to ensure data integrity.
//! - **Modular architecture**: Easily extendable to support new protocols.
//!
//! ## Usage Example
//!
//! ```rust
//! use packet_parser::DataLink;
//! use hex::decode;
//!
//! let hex_dump_data = "feaa81e86d1efeaa818ec864080045500034000000003d06206b36e6700dac140a0201bbc1087d7f02aa4e2b998e80100081748300000101080a9373c9c207ef14e3";
//! let packet = decode(hex_dump_data).expect("Hexadecimal conversion failed");
//!
//! match DataLink::try_from(packet.as_slice()) {
//! Ok(datalink) => println!("{:?}", datalink),
//! Err(e) => eprintln!("Parsing error: {:?}", e),
//! }
//! ```
//! ## Packet Structure
//!
//! The library parses packets into a hierarchical structure that can be represented as:
//!
//! ```text
//! Packet
//! ├── DataLink (Ethernet II)
//! │ ├── source_mac: String
//! │ ├── destination_mac: String
//! │ └── ether_type: EtherType
//! │
//! ├── Network (IPv4/IPv6)
//! │ ├── source_ip: IpAddr
//! │ ├── destination_ip: IpAddr
//! │ └── protocol: IpProtocol
//! │
//! ├── Transport (TCP/UDP/ICMP)
//! │ ├── source_port: Option<u16>
//! │ ├── destination_port: Option<u16>
//! │ └── protocol: TransportProtocol
//! │
//! └── Application
//! ├── protocol: ApplicationProtocol
//! └── payload: Vec<u8>
//! ```
//!
//! ## Example Flattened Struct
//!
//! For easier access to all fields, you can use a flattened structure:
//!
//! ```rust
//! use std::net::IpAddr;
//! use pnet::packet::ethernet::EtherType;
//! use pnet::packet::ip::IpNextHeaderProtocol as IpProtocol;
//! use packet_parser::parse::application::protocols::ApplicationProtocol;
//! use packet_parser::parse::transport::protocols::TransportProtocol;
//!
//! pub struct FlattenedPacket<'a> {
//! // Data Link Layer (Ethernet)
//! pub source_mac: String,
//! pub destination_mac: String,
//! pub ether_type: EtherType,
//!
//! // Network Layer
//! pub source_ip: IpAddr,
//! pub destination_ip: IpAddr,
//! pub ip_protocol: IpProtocol,
//!
//! // Transport Layer
//! pub source_port: Option<u16>,
//! pub destination_port: Option<u16>,
//! pub transport_protocol: TransportProtocol,
//!
//! // Application Layer
//! pub application_protocol: Option<ApplicationProtocol<'a>>,
//! pub payload: Vec<u8>,
//! }
//! ```
//!
//! This flattened structure provides direct access to all packet fields without
//! having to navigate through multiple layers of nested enums and structs.
/// Module handling format and integrity checks for packets.
/// Module for converting packet formats.
/// Module for displaying parsed data (internal use).
/// Centralized error management for the crate.
pub use ParsedPacketError;
/// Main module for packet analysis.
pub use Application;
/// Exports data link layer parsing functionality.
pub use DataLink;
pub use MacAddress;
pub use Internet;
pub use IpType;
pub use Transport;
/// Exports data link layer parsing functionality.
pub use PacketFlow;