pnet_packet/
dhcp.rs

1use crate::PrimitiveValues;
2
3use alloc::vec::Vec;
4
5use pnet_base::core_net::Ipv4Addr;
6use pnet_base::MacAddr;
7use pnet_macros::packet;
8use pnet_macros_support::types::*;
9
10/// Represents an Dhcp operation.
11#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
12pub struct DhcpOperation(pub u8);
13
14impl DhcpOperation {
15    /// Create a new `ArpOperation`.
16    pub fn new(value: u8) -> Self {
17        DhcpOperation(value)
18    }
19}
20
21impl PrimitiveValues for DhcpOperation {
22    type T = (u8,);
23    fn to_primitive_values(&self) -> (u8,) {
24        (self.0,)
25    }
26}
27
28/// The Dhcp protocol operations.
29#[allow(non_snake_case)]
30#[allow(non_upper_case_globals)]
31pub mod DhcpOperations {
32    use super::DhcpOperation;
33
34    /// DHCP request
35    pub const Request: DhcpOperation = DhcpOperation(1);
36
37    /// Dhcp reply
38    pub const Reply: DhcpOperation = DhcpOperation(2);
39}
40
41/// Represents the Dhcp hardware types.
42#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
43pub struct DhcpHardwareType(pub u8);
44
45impl DhcpHardwareType {
46    /// Create a new `DhcpHardwareType`.
47    pub fn new(value: u8) -> Self {
48        DhcpHardwareType(value)
49    }
50}
51
52impl PrimitiveValues for DhcpHardwareType {
53    type T = (u8,);
54    fn to_primitive_values(&self) -> (u8,) {
55        (self.0,)
56    }
57}
58
59/// The Dhcp protocol hardware types.
60#[allow(non_snake_case)]
61#[allow(non_upper_case_globals)]
62pub mod DhcpHardwareTypes {
63    use super::DhcpHardwareType;
64
65    /// Ethernet
66    pub const Ethernet: DhcpHardwareType = DhcpHardwareType(1);
67}
68
69/// Represents an DHCP Packet.
70#[packet]
71#[allow(non_snake_case)]
72pub struct Dhcp {
73    #[construct_with(u8)]
74    pub op: DhcpOperation,
75    #[construct_with(u8)]
76    pub htype: DhcpHardwareType,
77    pub hlen: u8,
78    pub hops: u8,
79    pub xid: u32be,
80    pub secs: u16be,
81    pub flags: u16be,
82    #[construct_with(u8, u8, u8, u8)]
83    pub ciaddr: Ipv4Addr,
84    #[construct_with(u8, u8, u8, u8)]
85    pub yiaddr: Ipv4Addr,
86    #[construct_with(u8, u8, u8, u8)]
87    pub siaddr: Ipv4Addr,
88    #[construct_with(u8, u8, u8, u8)]
89    pub giaddr: Ipv4Addr,
90    #[construct_with(u8, u8, u8, u8, u8, u8)]
91    pub chaddr: MacAddr,
92    #[length = "10"]
93    pub chaddr_pad: Vec<u8>,
94    #[length = "64"]
95    pub sname: Vec<u8>,
96    #[length = "128"]
97    pub file: Vec<u8>,
98    #[payload]
99    pub options: Vec<u8>,
100}