use crate::network::types::{DhcpInfo, DhcpMessageType};
const MIN_DHCP_SIZE: usize = 240;
const DHCP_MAGIC_COOKIE: [u8; 4] = [0x63, 0x82, 0x53, 0x63];
const DHCP_OPT_HOSTNAME: u8 = 12;
const DHCP_OPT_MESSAGE_TYPE: u8 = 53;
const DHCP_OPT_END: u8 = 255;
pub(super) fn analyze_dhcp(payload: &[u8]) -> Option<DhcpInfo> {
if payload.len() < MIN_DHCP_SIZE {
return None;
}
if payload[236..240] != DHCP_MAGIC_COOKIE {
return None;
}
let client_mac = crate::network::oui::format_mac(&payload[28..34]);
let (message_type, hostname) = parse_dhcp_options(&payload[240..])?;
Some(DhcpInfo {
message_type,
hostname,
client_mac: Some(client_mac),
})
}
fn parse_dhcp_options(options: &[u8]) -> Option<(DhcpMessageType, Option<String>)> {
let mut message_type = None;
let mut hostname = None;
let mut offset = 0;
while offset < options.len() {
let opt_code = options[offset];
if opt_code == DHCP_OPT_END {
break;
}
if opt_code == 0 {
offset += 1;
continue;
}
if offset + 1 >= options.len() {
break;
}
let opt_len = options[offset + 1] as usize;
if offset + 2 + opt_len > options.len() {
break;
}
let opt_data = &options[offset + 2..offset + 2 + opt_len];
match opt_code {
DHCP_OPT_MESSAGE_TYPE if opt_len >= 1 => {
message_type = Some(parse_message_type(opt_data[0]));
}
DHCP_OPT_HOSTNAME => {
if let Ok(name) = std::str::from_utf8(opt_data) {
hostname = Some(name.to_string());
}
}
_ => {}
}
offset += 2 + opt_len;
}
Some((message_type?, hostname))
}
fn parse_message_type(value: u8) -> DhcpMessageType {
match value {
1 => DhcpMessageType::Discover,
2 => DhcpMessageType::Offer,
3 => DhcpMessageType::Request,
4 => DhcpMessageType::Decline,
5 => DhcpMessageType::Ack,
6 => DhcpMessageType::Nak,
7 => DhcpMessageType::Release,
8 => DhcpMessageType::Inform,
other => DhcpMessageType::Unknown(other),
}
}
#[cfg(test)]
mod tests {
use super::*;
fn build_dhcp_packet(msg_type: u8, hostname: Option<&str>, mac: &[u8; 6]) -> Vec<u8> {
let mut packet = vec![0u8; 240];
packet[28..34].copy_from_slice(mac);
packet[236..240].copy_from_slice(&DHCP_MAGIC_COOKIE);
packet.push(DHCP_OPT_MESSAGE_TYPE);
packet.push(1); packet.push(msg_type);
if let Some(name) = hostname {
packet.push(DHCP_OPT_HOSTNAME);
packet.push(name.len() as u8);
packet.extend_from_slice(name.as_bytes());
}
packet.push(DHCP_OPT_END);
packet
}
#[test]
fn test_dhcp_discover() {
let mac = [0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff];
let packet = build_dhcp_packet(1, Some("myhost"), &mac);
let info = analyze_dhcp(&packet).expect("should parse");
assert_eq!(info.message_type, DhcpMessageType::Discover);
assert_eq!(info.hostname, Some("myhost".to_string()));
assert_eq!(info.client_mac, Some("aa:bb:cc:dd:ee:ff".to_string()));
}
#[test]
fn test_dhcp_offer() {
let mac = [0x11, 0x22, 0x33, 0x44, 0x55, 0x66];
let packet = build_dhcp_packet(2, None, &mac);
let info = analyze_dhcp(&packet).expect("should parse");
assert_eq!(info.message_type, DhcpMessageType::Offer);
assert!(info.hostname.is_none());
}
#[test]
fn test_dhcp_request() {
let mac = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55];
let packet = build_dhcp_packet(3, Some("workstation-01"), &mac);
let info = analyze_dhcp(&packet).expect("should parse");
assert_eq!(info.message_type, DhcpMessageType::Request);
assert_eq!(info.hostname, Some("workstation-01".to_string()));
}
#[test]
fn test_dhcp_ack() {
let mac = [0x00, 0x00, 0x00, 0x00, 0x00, 0x01];
let packet = build_dhcp_packet(5, None, &mac);
let info = analyze_dhcp(&packet).expect("should parse");
assert_eq!(info.message_type, DhcpMessageType::Ack);
}
#[test]
fn test_dhcp_too_short() {
let packet = vec![0u8; 100];
assert!(analyze_dhcp(&packet).is_none());
}
#[test]
fn test_dhcp_bad_magic_cookie() {
let mut packet = vec![0u8; 250];
packet[236..240].copy_from_slice(&[0x00, 0x00, 0x00, 0x00]); assert!(analyze_dhcp(&packet).is_none());
}
#[test]
fn test_dhcp_release() {
let mac = [0xde, 0xad, 0xbe, 0xef, 0x00, 0x01];
let packet = build_dhcp_packet(7, None, &mac);
let info = analyze_dhcp(&packet).expect("should parse");
assert_eq!(info.message_type, DhcpMessageType::Release);
}
#[test]
fn test_dhcp_inform() {
let mac = [0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc];
let packet = build_dhcp_packet(8, Some("printer"), &mac);
let info = analyze_dhcp(&packet).expect("should parse");
assert_eq!(info.message_type, DhcpMessageType::Inform);
assert_eq!(info.hostname, Some("printer".to_string()));
}
}