Skip to content

Commit

Permalink
src: centrifuge dhcp
Browse files Browse the repository at this point in the history
  • Loading branch information
krishpranav committed Aug 29, 2021
1 parent 3822d5b commit 3137416
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/centrifuge/dhcp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use crate::structs::dhcp::*;
use crate::structs::{self, CentrifugeError};

fn wrap_packet(dhcp: &dhcp4r::packet::Packet, packet: structs::dhcp::Packet) -> structs::dhcp::DHCP {
use crate::structs::dhcp::DHCP::*;

match dhcp.option(dhcp4r::options::DHCP_MESSAGE_TYPE) {
Some(dhcp4r::options::DhcpOption::DhcpMessageType(msg_type)) => {
match msg_type {
dhcp4r::options::MessageType::Ack => ACK(packet),
dhcp4r::options::MessageType::Decline => DECLINE(packet),
dhcp4r::options::MessageType::Discover => DISCOVER(packet),
dhcp4r::options::MessageType::Inform => INFORM(packet),
dhcp4r::options::MessageType::Nak => NAK(packet),
dhcp4r::options::MessageType::Offer => OFFER(packet),
dhcp4r::options::MessageType::Release => RELEASE(packet),
dhcp4r::options::MessageType::Request => REQUEST(packet),
}
}
_ => UNKNOWN(packet),
}
}


pub fn extract(remaining: &[u8]) -> Result<structs::dhcp::DHCP, CentrifugeError> {

let dhcp = match dhcp4r::packet::Packet::from(remaining) {
Ok(dhcp) => dhcp,
Err(_err) => return Err(CentrifugeError::InvalidPacket),
};

let mut packet = Packet::new(dhcp.ciaddr, dhcp.yiaddr, dhcp.siaddr, dhcp.chaddr);

for option in &dhcp.options {
use dhcp4r::options::*;
match option {
DhcpOption::RequestedIpAddress(addr) => packet.requested_ip_address = Some(*addr),
DhcpOption::HostName(hostname) => packet.hostname = Some(hostname.to_string()),
DhcpOption::Router(router) => packet.router = Some(router.to_vec()),
DhcpOption::DomainNameServer(server) => packet.domain_name_server = Some(server.to_vec()),
_ => (),
}
}

Ok(wrap_packet(&dhcp, packet))
}

0 comments on commit 3137416

Please sign in to comment.