use std::net::Ipv4Addr;
use bytes::Bytes;
use nex_core::mac::MacAddr;
use crate::{
dhcp::{DhcpHardwareType, DhcpHeader, DhcpOperation, DhcpPacket},
packet::Packet,
};
#[derive(Debug, Clone)]
pub struct DhcpPacketBuilder {
packet: DhcpPacket,
}
impl DhcpPacketBuilder {
pub fn new_discover(xid: u32, chaddr: MacAddr) -> Self {
let header = DhcpHeader {
op: DhcpOperation::Request,
htype: DhcpHardwareType::Ethernet,
hlen: 6,
hops: 0,
xid,
secs: 0,
flags: 0x8000, ciaddr: Ipv4Addr::UNSPECIFIED,
yiaddr: Ipv4Addr::UNSPECIFIED,
siaddr: Ipv4Addr::UNSPECIFIED,
giaddr: Ipv4Addr::UNSPECIFIED,
chaddr,
chaddr_pad: [0u8; 10].to_vec(),
sname: [0u8; 64].to_vec(),
file: [0u8; 128].to_vec(),
};
Self {
packet: DhcpPacket {
header,
payload: Bytes::new(),
},
}
}
pub fn payload(mut self, payload: Bytes) -> Self {
self.packet.payload = payload;
self
}
pub fn header_mut(&mut self) -> &mut DhcpHeader {
&mut self.packet.header
}
pub fn build(self) -> DhcpPacket {
self.packet
}
pub fn to_bytes(self) -> Bytes {
self.packet.to_bytes()
}
pub fn packet(&self) -> &DhcpPacket {
&self.packet
}
}