Merge pull request #3 from junderw/junderw/minor-fixes

Some minor fixes.
This commit is contained in:
pj1234678
2023-10-30 20:54:00 -07:00
committed by GitHub
7 changed files with 132 additions and 169 deletions

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
/target
Cargo.lock

View File

@@ -8,6 +8,7 @@ fn main() {
server::Server::serve( server::Server::serve(
UdpSocket::bind("0.0.0.0:67").unwrap(), UdpSocket::bind("0.0.0.0:67").unwrap(),
Ipv4Addr::new(0, 0, 0, 0), Ipv4Addr::new(0, 0, 0, 0),
Ipv4Addr::new(0, 0, 0, 0),
MyServer {}, MyServer {},
); );
} }
@@ -24,7 +25,9 @@ impl server::Handler for MyServer {
}; };
println!( println!(
"{}\t{}\t{}\tOnline", "{}\t{}\t{}\tOnline",
time::OffsetDateTime::now_local().format("%Y-%m-%dT%H:%M:%S"), time::OffsetDateTime::try_now_local()
.unwrap()
.format("%Y-%m-%dT%H:%M:%S"),
chaddr(&in_packet.chaddr), chaddr(&in_packet.chaddr),
Ipv4Addr::from(req_ip) Ipv4Addr::from(req_ip)
); );

View File

@@ -1,12 +1,9 @@
#[macro_use(u32_bytes, bytes_u32)]
extern crate dhcp4r;
use std::collections::HashMap; use std::collections::HashMap;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::net::{Ipv4Addr, UdpSocket}; use std::net::{Ipv4Addr, UdpSocket};
use std::ops::Add; use std::ops::Add;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
use std::fs::File;
use std::io::{BufRead, BufReader};
use dhcp4r::{options, packet, server}; use dhcp4r::{options, packet, server};
@@ -24,42 +21,41 @@ const LEASE_DURATION_SECS: u32 = 86400;
const LEASE_NUM: u32 = 252; const LEASE_NUM: u32 = 252;
// Derived constants // Derived constants
const IP_START_NUM: u32 = bytes_u32!(IP_START); const IP_START_NUM: u32 = u32::from_be_bytes(IP_START);
const INFINITE_LEASE: Option<Instant> = None; // Special value for infinite lease const INFINITE_LEASE: Option<Instant> = None; // Special value for infinite lease
fn main() { fn main() {
let socket = UdpSocket::bind("0.0.0.0:67").unwrap(); let socket = UdpSocket::bind("0.0.0.0:67").unwrap();
socket.set_broadcast(true).unwrap(); socket.set_broadcast(true).unwrap();
let mut leases: HashMap<Ipv4Addr, ([u8; 6], Option<Instant>)> = HashMap::new(); let mut leases: HashMap<Ipv4Addr, ([u8; 6], Option<Instant>)> = HashMap::new();
// Read and populate leases from the file // Read and populate leases from the file
if let Ok(file) = File::open("leases") { if let Ok(file) = File::open("leases") {
let reader = BufReader::new(file); let reader = BufReader::new(file);
for line in reader.lines() { for line in reader.lines() {
if let Ok(line) = line { if let Ok(line) = line {
let parts: Vec<&str> = line.split(',').collect(); let parts: Vec<&str> = line.split(',').collect();
if parts.len() == 2 { if parts.len() == 2 {
let mac_parts: Vec<u8> = parts[0] let mac_parts: Vec<u8> = parts[0]
.split(':') .split(':')
.filter_map(|part| u8::from_str_radix(part, 16).ok()) .filter_map(|part| u8::from_str_radix(part, 16).ok())
.collect(); .collect();
if mac_parts.len() == 6 { if mac_parts.len() == 6 {
let mut mac = [0u8; 6]; let mut mac = [0u8; 6];
mac.copy_from_slice(&mac_parts); mac.copy_from_slice(&mac_parts);
let ip = parts[1].trim().parse::<Ipv4Addr>().unwrap(); let ip = parts[1].trim().parse::<Ipv4Addr>().unwrap();
leases.insert(ip, (mac, INFINITE_LEASE)); leases.insert(ip, (mac, INFINITE_LEASE));
}
} }
} }
} }
} else {
eprintln!("Failed to open leases file. Continuing...");
//return;
} }
} else {
eprintln!("Failed to open leases file. Continuing...");
//return;
}
let ms = MyServer { let ms = MyServer {
leases, leases,
last_lease: 0, last_lease: 0,
@@ -79,10 +75,9 @@ impl server::Handler for MyServer {
fn handle_request(&mut self, server: &server::Server, in_packet: packet::Packet) { fn handle_request(&mut self, server: &server::Server, in_packet: packet::Packet) {
match in_packet.message_type() { match in_packet.message_type() {
Ok(options::MessageType::Discover) => { Ok(options::MessageType::Discover) => {
// Otherwise prefer existing (including expired if available) // Otherwise prefer existing (including expired if available)
if let Some(ip) = self.current_lease(&in_packet.chaddr) { if let Some(ip) = self.current_lease(&in_packet.chaddr) {
println!("Sending Reply to discover"); println!("Sending Reply to discover");
reply(server, options::MessageType::Offer, in_packet, &ip); reply(server, options::MessageType::Offer, in_packet, &ip);
return; return;
} }
@@ -93,7 +88,7 @@ impl server::Handler for MyServer {
&in_packet.chaddr, &in_packet.chaddr,
&((IP_START_NUM + &self.last_lease).into()), &((IP_START_NUM + &self.last_lease).into()),
) { ) {
println!("Sending Reply to discover"); println!("Sending Reply to discover");
reply( reply(
server, server,
options::MessageType::Offer, options::MessageType::Offer,
@@ -108,29 +103,35 @@ impl server::Handler for MyServer {
Ok(options::MessageType::Request) => { Ok(options::MessageType::Request) => {
// Ignore requests to alternative DHCP server // Ignore requests to alternative DHCP server
if !server.for_this_server(&in_packet) { if !server.for_this_server(&in_packet) {
//println!("Not for this server"); //println!("Not for this server");
// return; // return;
} }
let req_ip = match in_packet.option(options::REQUESTED_IP_ADDRESS) { let req_ip = match in_packet.option(options::REQUESTED_IP_ADDRESS) {
Some(options::DhcpOption::RequestedIpAddress(x)) => *x, Some(options::DhcpOption::RequestedIpAddress(x)) => *x,
_ => in_packet.ciaddr, _ => in_packet.ciaddr,
}; };
for (ip, (mac, _)) in &self.leases { for (ip, (mac, _)) in &self.leases {
println!("IP: {:?}, MAC: {:?}", ip, mac); println!("IP: {:?}, MAC: {:?}", ip, mac);
} }
if let Some(ip) = self.current_lease(&in_packet.chaddr) { if let Some(ip) = self.current_lease(&in_packet.chaddr) {
println!("Found Current Lease"); println!("Found Current Lease");
reply(server, options::MessageType::Ack, in_packet, &ip); reply(server, options::MessageType::Ack, in_packet, &ip);
return; return;
} }
if !&self.available(&in_packet.chaddr, &req_ip) { if !&self.available(&in_packet.chaddr, &req_ip) {
println!("Sending Reply to Request"); println!("Sending Reply to Request");
nak(server, in_packet, "Requested IP not available"); nak(server, in_packet, "Requested IP not available");
return; return;
} }
self.leases.insert(req_ip, (in_packet.chaddr, Some(Instant::now().add(self.lease_duration)))); self.leases.insert(
println!("Sending Reply to Request"); req_ip,
(
in_packet.chaddr,
Some(Instant::now().add(self.lease_duration)),
),
);
println!("Sending Reply to Request");
reply(server, options::MessageType::Ack, in_packet, &req_ip); reply(server, options::MessageType::Ack, in_packet, &req_ip);
} }
@@ -162,8 +163,7 @@ impl MyServer {
None => true, None => true,
} }
} }
fn current_lease(&self, chaddr: &[u8; 6]) -> Option<Ipv4Addr> { fn current_lease(&self, chaddr: &[u8; 6]) -> Option<Ipv4Addr> {
for (i, v) in &self.leases { for (i, v) in &self.leases {
if v.0 == *chaddr { if v.0 == *chaddr {
return Some(*i); return Some(*i);

View File

@@ -1,33 +1,7 @@
pub mod options; pub mod options;
pub mod packet; pub mod packet;
pub mod server; pub mod server;
/// Converts a u32 to 4 bytes (Big endian)
#[macro_export]
macro_rules! u32_bytes {
( $x:expr ) => {
[
($x >> 24) as u8,
($x >> 16) as u8,
($x >> 8) as u8,
$x as u8,
]
};
}
/// Converts 4 bytes to a u32 (Big endian)
#[macro_export]
macro_rules! bytes_u32 {
( $x:expr ) => {
($x[0] as u32) * (1 << 24)
+ ($x[1] as u32) * (1 << 16)
+ ($x[2] as u32) * (1 << 8)
+ ($x[3] as u32)
};
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
#[test] #[test]

View File

@@ -364,4 +364,4 @@ impl MessageType {
_ => Err(format!("Invalid DHCP Message Type: {:?}", val)), _ => Err(format!("Invalid DHCP Message Type: {:?}", val)),
} }
} }
} }

View File

@@ -19,8 +19,6 @@ pub enum ErrorKind {
type IResult<I, O> = Result<(I, O), CustomErr<I>>; type IResult<I, O> = Result<(I, O), CustomErr<I>>;
/// DHCP Packet Structure /// DHCP Packet Structure
#[derive(Debug)] #[derive(Debug)]
pub struct Packet { pub struct Packet {
@@ -75,7 +73,7 @@ where
remaining = input; remaining = input;
} }
Err(CustomErr::NomError(_)) => return Ok((remaining, acc)), Err(CustomErr::NomError(_)) => return Ok((remaining, acc)),
Err(e) => return Err(e), Err(e) => return Err(e),
} }
} }
} }
@@ -85,12 +83,14 @@ pub fn decode_option(input: &[u8]) -> IResult<&[u8], DhcpOption> {
assert!(code != END); assert!(code != END);
let (input, len) = custom_be_u8(input)?; let (input, len) = custom_be_u8(input)?;
let (input, data) = custom_take(len.into())(input)?; let (input, data) = custom_take(len.into())(input)?;
let option = match code { let option = match code {
DHCP_MESSAGE_TYPE => DhcpOption::DhcpMessageType(match MessageType::from(custom_be_u8(data)?.1) { DHCP_MESSAGE_TYPE => {
Ok(x) => x, DhcpOption::DhcpMessageType(match MessageType::from(custom_be_u8(data)?.1) {
Err(_) => return Err(CustomErr::UnrecognizedMessageType), Ok(x) => x,
}), Err(_) => return Err(CustomErr::UnrecognizedMessageType),
})
}
SERVER_IDENTIFIER => DhcpOption::ServerIdentifier(decode_ipv4(data)?.1), SERVER_IDENTIFIER => DhcpOption::ServerIdentifier(decode_ipv4(data)?.1),
PARAMETER_REQUEST_LIST => DhcpOption::ParameterRequestList(data.to_vec()), PARAMETER_REQUEST_LIST => DhcpOption::ParameterRequestList(data.to_vec()),
REQUESTED_IP_ADDRESS => DhcpOption::RequestedIpAddress(decode_ipv4(data)?.1), REQUESTED_IP_ADDRESS => DhcpOption::RequestedIpAddress(decode_ipv4(data)?.1),
@@ -133,7 +133,7 @@ fn custom_tag<'a>(tag: &'static [u8]) -> impl Fn(&'a [u8]) -> IResult<&'a [u8],
} }
} }
fn custom_be_u8(input: &[u8]) -> IResult<&[u8], u8> { fn custom_be_u8(input: &[u8]) -> IResult<&[u8], u8> {
if input.len() < 1 { if input.is_empty() {
return Err(CustomErr::InvalidHlen); return Err(CustomErr::InvalidHlen);
} }
@@ -174,7 +174,6 @@ fn decode(input: &[u8]) -> IResult<&[u8], Packet> {
let (input, giaddr) = decode_ipv4(input)?; let (input, giaddr) = decode_ipv4(input)?;
if hlen != 6 { if hlen != 6 {
return Err(CustomErr::InvalidHlen); return Err(CustomErr::InvalidHlen);
} }
let (_, chaddr) = custom_take(6usize)(input)?; let (_, chaddr) = custom_take(6usize)(input)?;
@@ -182,20 +181,14 @@ fn decode(input: &[u8]) -> IResult<&[u8], Packet> {
let input = options_input; let input = options_input;
let (input, _) = custom_tag(&COOKIE)(input)?; let (input, _) = custom_tag(&COOKIE)(input)?;
let mut options = Vec::new(); let mut options = Vec::new();
let mut rest = input; let mut rest = input;
loop { while let Ok((new_rest, option)) = decode_option(rest) {
match decode_option(rest) { rest = new_rest;
Ok((new_rest, option)) => { options.push(option);
rest = new_rest; if rest.starts_with(&[END]) {
options.push(option); break;
if rest.starts_with(&[END]) {
break;
}
}
Err(_) => break,
} }
} }
@@ -223,18 +216,12 @@ fn decode(input: &[u8]) -> IResult<&[u8], Packet> {
impl Packet { impl Packet {
pub fn from(input: &[u8]) -> Result<Packet, CustomErr<&[u8]>> { pub fn from(input: &[u8]) -> Result<Packet, CustomErr<&[u8]>> {
Ok(decode(input)?.1) Ok(decode(input)?.1)
} }
/// Extracts requested option payload from packet if available /// Extracts requested option payload from packet if available
pub fn option(&self, code: u8) -> Option<&DhcpOption> { pub fn option(&self, code: u8) -> Option<&DhcpOption> {
for option in &self.options { self.options.iter().find(|&option| option.code() == code)
if option.code() == code {
return Some(&option);
}
}
None
} }
/// Convenience function for extracting a packet's message type. /// Convenience function for extracting a packet's message type.
@@ -248,58 +235,58 @@ impl Packet {
None => Err("Packet does not have MessageType option".to_string()), None => Err("Packet does not have MessageType option".to_string()),
} }
} }
pub fn encode<'a>(&'a self, p: &'a mut [u8]) -> &[u8] { pub fn encode<'a>(&'a self, p: &'a mut [u8]) -> &[u8] {
let broadcast_flag = if self.broadcast { 128 } else { 0 }; let broadcast_flag = if self.broadcast { 128 } else { 0 };
let mut length = 240; let mut length = 240;
p[..12].copy_from_slice(&[ p[..12].copy_from_slice(&[
if self.reply { BOOT_REPLY } else { BOOT_REQUEST }, if self.reply { BOOT_REPLY } else { BOOT_REQUEST },
1, 1,
6, 6,
self.hops, self.hops,
((self.xid >> 24) & 0xFF) as u8, ((self.xid >> 24) & 0xFF) as u8,
((self.xid >> 16) & 0xFF) as u8, ((self.xid >> 16) & 0xFF) as u8,
((self.xid >> 8) & 0xFF) as u8, ((self.xid >> 8) & 0xFF) as u8,
(self.xid & 0xFF) as u8, (self.xid & 0xFF) as u8,
(self.secs >> 8) as u8, (self.secs >> 8) as u8,
(self.secs & 255) as u8, (self.secs & 255) as u8,
broadcast_flag, broadcast_flag,
0, 0,
]); ]);
p[12..16].copy_from_slice(&self.ciaddr.octets()); p[12..16].copy_from_slice(&self.ciaddr.octets());
p[16..20].copy_from_slice(&self.yiaddr.octets()); p[16..20].copy_from_slice(&self.yiaddr.octets());
p[20..24].copy_from_slice(&self.siaddr.octets()); p[20..24].copy_from_slice(&self.siaddr.octets());
p[24..28].copy_from_slice(&self.giaddr.octets()); p[24..28].copy_from_slice(&self.giaddr.octets());
p[28..34].copy_from_slice(&self.chaddr); p[28..34].copy_from_slice(&self.chaddr);
p[34..236].fill(0); p[34..236].fill(0);
p[236..240].copy_from_slice(&COOKIE); p[236..240].copy_from_slice(&COOKIE);
for option in &self.options { for option in &self.options {
let option = option.to_raw(); let option = option.to_raw();
let option_len = option.data.len(); let option_len = option.data.len();
if length + 2 + option_len >= 272 { if length + 2 + option_len >= 272 {
break; break;
}
if let Some(dest) = p.get_mut(length..length + 2 + option_len) {
dest[0] = option.code;
dest[1] = option_len as u8;
dest[2..].copy_from_slice(&option.data);
}
length += 2 + option_len;
} }
if let Some(dest) = p.get_mut(length..length + 2 + option_len) {
dest[0] = option.code; if let Some(end_segment) = p.get_mut(length..length + 1) {
dest[1] = option_len as u8; end_segment[0] = END;
dest[2..].copy_from_slice(&option.data);
} }
length += 2 + option_len; length += 1;
}
if let Some(end_segment) = p.get_mut(length..length + 1) { if let Some(pad_segment) = p.get_mut(length..272) {
end_segment[0] = END; pad_segment.fill(PAD);
} }
length += 1;
if let Some(pad_segment) = p.get_mut(length..272) { &p[..length]
pad_segment.fill(PAD);
} }
&p[..length]
}
} }
const COOKIE: [u8; 4] = [99, 130, 83, 99]; const COOKIE: [u8; 4] = [99, 130, 83, 99];

View File

@@ -1,3 +1,5 @@
//! This is a convenience module that simplifies the writing of a DHCP server service.
use std::cell::Cell; use std::cell::Cell;
use std::net::{IpAddr, Ipv4Addr, SocketAddr, UdpSocket}; use std::net::{IpAddr, Ipv4Addr, SocketAddr, UdpSocket};
@@ -5,8 +7,6 @@ use crate::options;
use crate::options::{DhcpOption, MessageType}; use crate::options::{DhcpOption, MessageType};
use crate::packet::*; use crate::packet::*;
///! This is a convenience module that simplifies the writing of a DHCP server service.
pub struct Server { pub struct Server {
out_buf: Cell<[u8; 1500]>, out_buf: Cell<[u8; 1500]>,
socket: UdpSocket, socket: UdpSocket,
@@ -22,12 +22,12 @@ pub trait Handler {
pub fn filter_options_by_req(opts: &mut Vec<DhcpOption>, req_params: &[u8]) { pub fn filter_options_by_req(opts: &mut Vec<DhcpOption>, req_params: &[u8]) {
let mut pos = 0; let mut pos = 0;
let h = &[ let h = &[
options::DHCP_MESSAGE_TYPE as u8, options::DHCP_MESSAGE_TYPE,
options::SERVER_IDENTIFIER as u8, options::SERVER_IDENTIFIER,
options::SUBNET_MASK as u8, options::SUBNET_MASK,
options::IP_ADDRESS_LEASE_TIME as u8, options::IP_ADDRESS_LEASE_TIME,
options::DOMAIN_NAME_SERVER as u8, options::DOMAIN_NAME_SERVER,
options::ROUTER as u8, options::ROUTER,
] as &[u8]; ] as &[u8];
// Process options from req_params // Process options from req_params
@@ -74,7 +74,7 @@ impl Server {
pub fn serve<H: Handler>( pub fn serve<H: Handler>(
udp_soc: UdpSocket, udp_soc: UdpSocket,
server_ip: Ipv4Addr, server_ip: Ipv4Addr,
broadcast_ip: Ipv4Addr, broadcast_ip: Ipv4Addr,
mut handler: H, mut handler: H,
) -> std::io::Error { ) -> std::io::Error {
let mut in_buf: [u8; 1500] = [0; 1500]; let mut in_buf: [u8; 1500] = [0; 1500];
@@ -82,9 +82,8 @@ impl Server {
out_buf: Cell::new([0; 1500]), out_buf: Cell::new([0; 1500]),
socket: udp_soc, socket: udp_soc,
server_ip, server_ip,
broadcast_ip, broadcast_ip,
src: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 0), src: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 0),
}; };
loop { loop {
match s.socket.recv_from(&mut in_buf) { match s.socket.recv_from(&mut in_buf) {
@@ -92,7 +91,7 @@ impl Server {
Ok((l, src)) => { Ok((l, src)) => {
if let Ok(p) = Packet::from(&in_buf[..l]) { if let Ok(p) = Packet::from(&in_buf[..l]) {
s.src = src; s.src = src;
handler.handle_request(&s, p); handler.handle_request(&s, p);
} }
} }
@@ -133,7 +132,7 @@ impl Server {
if let Some(DhcpOption::ParameterRequestList(prl)) = if let Some(DhcpOption::ParameterRequestList(prl)) =
req_packet.option(options::PARAMETER_REQUEST_LIST) req_packet.option(options::PARAMETER_REQUEST_LIST)
{ {
filter_options_by_req(&mut opts, &prl); filter_options_by_req(&mut opts, prl);
} }
self.send(Packet { self.send(Packet {
@@ -154,21 +153,19 @@ impl Server {
/// Checks the packet see if it was intended for this DHCP server (as opposed to some other also on the network). /// Checks the packet see if it was intended for this DHCP server (as opposed to some other also on the network).
pub fn for_this_server(&self, packet: &Packet) -> bool { pub fn for_this_server(&self, packet: &Packet) -> bool {
match packet.option(options::SERVER_IDENTIFIER) { match packet.option(options::SERVER_IDENTIFIER) {
Some(DhcpOption::ServerIdentifier(x)) => { Some(DhcpOption::ServerIdentifier(x)) => x == &self.server_ip,
x == &self.server_ip
},
_ => false, _ => false,
} }
} }
/// Encodes and sends a DHCP packet back to the client. /// Encodes and sends a DHCP packet back to the client.
pub fn send(&self, p: Packet) -> std::io::Result<usize> { pub fn send(&self, p: Packet) -> std::io::Result<usize> {
let mut addr = self.src; let mut addr = self.src;
if p.broadcast || addr.ip() == IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)) { if p.broadcast || addr.ip() == IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)) {
addr.set_ip(std::net::IpAddr::V4(self.broadcast_ip)); addr.set_ip(std::net::IpAddr::V4(self.broadcast_ip));
} }
println!("Sending Response to: {:?}", addr); // Print the address println!("Sending Response to: {:?}", addr); // Print the address
self.socket.send_to(p.encode(&mut self.out_buf.get()), addr) self.socket.send_to(p.encode(&mut self.out_buf.get()), addr)
} }
} }