Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

packet: add DHCP support #581

Merged
merged 3 commits into from Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
100 changes: 100 additions & 0 deletions pnet_packet/src/dhcp.rs
@@ -0,0 +1,100 @@
use crate::PrimitiveValues;

use alloc::vec::Vec;

use pnet_base::core_net::Ipv4Addr;
use pnet_base::MacAddr;
use pnet_macros::packet;
use pnet_macros_support::types::*;

/// Represents an Dhcp operation.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct DhcpOperation(pub u8);

impl DhcpOperation {
/// Create a new `ArpOperation`.
pub fn new(value: u8) -> Self {
DhcpOperation(value)
}
}

impl PrimitiveValues for DhcpOperation {
type T = (u8,);
fn to_primitive_values(&self) -> (u8,) {
(self.0,)
}
}

/// The Dhcp protocol operations.
#[allow(non_snake_case)]
#[allow(non_upper_case_globals)]
pub mod DhcpOperations {
use super::DhcpOperation;

/// DHCP request
pub const Request: DhcpOperation = DhcpOperation(1);

/// Dhcp reply
pub const Reply: DhcpOperation = DhcpOperation(2);
}

/// Represents the Dhcp hardware types.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct DhcpHardwareType(pub u8);

impl DhcpHardwareType {
/// Create a new `DhcpHardwareType`.
pub fn new(value: u8) -> Self {
DhcpHardwareType(value)
}
}

impl PrimitiveValues for DhcpHardwareType {
type T = (u8,);
fn to_primitive_values(&self) -> (u8,) {
(self.0,)
}
}

/// The Dhcp protocol hardware types.
#[allow(non_snake_case)]
#[allow(non_upper_case_globals)]
pub mod DhcpHardwareTypes {
use super::DhcpHardwareType;

/// Ethernet
pub const Ethernet: DhcpHardwareType = DhcpHardwareType(1);
}

/// Represents an DHCP Packet.
#[packet]
#[allow(non_snake_case)]
pub struct Dhcp {
#[construct_with(u8)]
pub op: DhcpOperation,
#[construct_with(u8)]
pub htype: DhcpHardwareType,
pub hlen: u8,
pub hops: u8,
pub xid: u32be,
pub secs: u16be,
pub flags: u16be,
#[construct_with(u8, u8, u8, u8)]
pub ciaddr: Ipv4Addr,
#[construct_with(u8, u8, u8, u8)]
pub yiaddr: Ipv4Addr,
#[construct_with(u8, u8, u8, u8)]
pub siaddr: Ipv4Addr,
#[construct_with(u8, u8, u8, u8)]
pub giaddr: Ipv4Addr,
#[construct_with(u8, u8, u8, u8, u8, u8)]
pub chaddr: MacAddr,
#[length = "10"]
pub chaddr_pad: Vec<u8>,
#[length = "64"]
pub sname: Vec<u8>,
#[length = "128"]
pub file: Vec<u8>,
#[payload]
pub options: Vec<u8>,
}
1 change: 1 addition & 0 deletions pnet_packet/src/lib.rs
Expand Up @@ -23,6 +23,7 @@ extern crate pnet_macros;
pub use pnet_macros_support::packet::*;

pub mod arp;
pub mod dhcp;
pub mod ethernet;
pub mod gre;
pub mod icmp;
Expand Down