From 530b7dd97f6b2b68fc5ddc406ed39a2a6d762b35 Mon Sep 17 00:00:00 2001 From: Xnoe Date: Wed, 16 Nov 2022 15:52:48 +0000 Subject: [PATCH] Send ClientID to the DHCP Server, Send Hostname to the DHCP Server --- src/main.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/main.rs b/src/main.rs index 4bb6908..d8a110a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,6 +12,8 @@ use std::collections::HashMap; use std::io::Write; +use std::str; + fn create_dhcp_packet( xid: u32, mac: MacAddress, @@ -92,10 +94,20 @@ fn dhcp_client(name: String, index: i32, mac: [u8; 6]) { let mut socket = rawsocket::create_raw_socket(index, mac).unwrap(); let client_mac = MacAddress::from_bytes(&mac).unwrap(); + let client_id: Vec = vec![1].into_iter().chain(mac.to_vec().into_iter()).collect(); let mut client_addr: Option = None; let mut server_addr: Option = None; + // Determine hostname. + let mut hostname_raw: [u8; 64] = [0; 64]; + unsafe { + libc::gethostname(&mut hostname_raw as *mut _ as *mut libc::c_char, 64); + } + let length = hostname_raw.iter().position(|&i| i == 0).unwrap(); + let hostname = str::from_utf8(&hostname_raw[..length]).unwrap_or("").to_owned(); + println!("Hostname: {}", hostname); + // DHCP transaction loop 'dhcp_transaction: loop { let xid: u32 = rng.gen(); @@ -115,6 +127,8 @@ fn dhcp_client(name: String, index: i32, mac: [u8; 6]) { None, vec![ DHCPOption::DHCPMessageType(DHCPMessageType::DHCPDiscover), + DHCPOption::HostName(hostname.clone()), + DHCPOption::ClientIdentifier(client_id.clone()), DHCPOption::End, ], ) @@ -269,6 +283,8 @@ fn dhcp_client(name: String, index: i32, mac: [u8; 6]) { DHCPOption::RequestIPAddress(client_addr.unwrap()), DHCPOption::ServerIdentifier(server_addr.unwrap()), DHCPOption::ParameterRequest(vec![1, 3, 6, 28, 121]), + DHCPOption::HostName(hostname.clone()), + DHCPOption::ClientIdentifier(client_id.clone()), DHCPOption::End, ], )