Run cargo fmt

This commit is contained in:
junderw
2023-10-30 20:13:40 -07:00
parent e8d62af6c3
commit 4a85f1dcbb
5 changed files with 112 additions and 113 deletions

View File

@@ -25,7 +25,9 @@ impl server::Handler for MyServer {
};
println!(
"{}\t{}\t{}\tOnline",
time::OffsetDateTime::try_now_local().unwrap().format("%Y-%m-%dT%H:%M:%S"),
time::OffsetDateTime::try_now_local()
.unwrap()
.format("%Y-%m-%dT%H:%M:%S"),
chaddr(&in_packet.chaddr),
Ipv4Addr::from(req_ip)
);

View File

@@ -1,9 +1,9 @@
use std::collections::HashMap;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::net::{Ipv4Addr, UdpSocket};
use std::ops::Add;
use std::time::{Duration, Instant};
use std::fs::File;
use std::io::{BufRead, BufReader};
use dhcp4r::{options, packet, server};
@@ -24,14 +24,13 @@ const LEASE_NUM: u32 = 252;
const IP_START_NUM: u32 = u32::from_be_bytes(IP_START);
const INFINITE_LEASE: Option<Instant> = None; // Special value for infinite lease
fn main() {
let socket = UdpSocket::bind("0.0.0.0:67").unwrap();
socket.set_broadcast(true).unwrap();
let mut leases: HashMap<Ipv4Addr, ([u8; 6], Option<Instant>)> = HashMap::new();
// 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);
for line in reader.lines() {
if let Ok(line) = line {
@@ -52,10 +51,10 @@ if let Ok(file) = File::open("leases") {
}
}
}
} else {
} else {
eprintln!("Failed to open leases file. Continuing...");
//return;
}
}
let ms = MyServer {
leases,
@@ -76,7 +75,6 @@ impl server::Handler for MyServer {
fn handle_request(&mut self, server: &server::Server, in_packet: packet::Packet) {
match in_packet.message_type() {
Ok(options::MessageType::Discover) => {
// Otherwise prefer existing (including expired if available)
if let Some(ip) = self.current_lease(&in_packet.chaddr) {
println!("Sending Reply to discover");
@@ -126,7 +124,13 @@ impl server::Handler for MyServer {
nak(server, in_packet, "Requested IP not available");
return;
}
self.leases.insert(req_ip, (in_packet.chaddr, Some(Instant::now().add(self.lease_duration))));
self.leases.insert(
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);
}
@@ -160,7 +164,6 @@ impl MyServer {
}
}
fn current_lease(&self, chaddr: &[u8; 6]) -> Option<Ipv4Addr> {
for (i, v) in &self.leases {
if v.0 == *chaddr {
return Some(*i);

View File

@@ -19,8 +19,6 @@ pub enum ErrorKind {
type IResult<I, O> = Result<(I, O), CustomErr<I>>;
/// DHCP Packet Structure
#[derive(Debug)]
pub struct Packet {
@@ -87,10 +85,12 @@ pub fn decode_option(input: &[u8]) -> IResult<&[u8], DhcpOption> {
let (input, len) = custom_be_u8(input)?;
let (input, data) = custom_take(len.into())(input)?;
let option = match code {
DHCP_MESSAGE_TYPE => DhcpOption::DhcpMessageType(match MessageType::from(custom_be_u8(data)?.1) {
DHCP_MESSAGE_TYPE => {
DhcpOption::DhcpMessageType(match MessageType::from(custom_be_u8(data)?.1) {
Ok(x) => x,
Err(_) => return Err(CustomErr::UnrecognizedMessageType),
}),
})
}
SERVER_IDENTIFIER => DhcpOption::ServerIdentifier(decode_ipv4(data)?.1),
PARAMETER_REQUEST_LIST => DhcpOption::ParameterRequestList(data.to_vec()),
REQUESTED_IP_ADDRESS => DhcpOption::RequestedIpAddress(decode_ipv4(data)?.1),
@@ -174,7 +174,6 @@ fn decode(input: &[u8]) -> IResult<&[u8], Packet> {
let (input, giaddr) = decode_ipv4(input)?;
if hlen != 6 {
return Err(CustomErr::InvalidHlen);
}
let (_, chaddr) = custom_take(6usize)(input)?;
@@ -182,7 +181,6 @@ fn decode(input: &[u8]) -> IResult<&[u8], Packet> {
let input = options_input;
let (input, _) = custom_tag(&COOKIE)(input)?;
let mut options = Vec::new();
let mut rest = input;
@@ -223,7 +221,6 @@ fn decode(input: &[u8]) -> IResult<&[u8], Packet> {
impl Packet {
pub fn from(input: &[u8]) -> Result<Packet, CustomErr<&[u8]>> {
Ok(decode(input)?.1)
}
@@ -299,7 +296,7 @@ impl Packet {
}
&p[..length]
}
}
}
const COOKIE: [u8; 4] = [99, 130, 83, 99];

View File

@@ -84,7 +84,6 @@ impl Server {
server_ip,
broadcast_ip,
src: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 0),
};
loop {
match s.socket.recv_from(&mut in_buf) {
@@ -154,15 +153,13 @@ impl Server {
/// 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 {
match packet.option(options::SERVER_IDENTIFIER) {
Some(DhcpOption::ServerIdentifier(x)) => {
x == &self.server_ip
},
Some(DhcpOption::ServerIdentifier(x)) => x == &self.server_ip,
_ => false,
}
}
/// Encodes and sends a DHCP packet back to the client.
pub fn send(&self, p: Packet) -> std::io::Result<usize> {
/// Encodes and sends a DHCP packet back to the client.
pub fn send(&self, p: Packet) -> std::io::Result<usize> {
let mut addr = self.src;
if p.broadcast || addr.ip() == IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)) {
addr.set_ip(std::net::IpAddr::V4(self.broadcast_ip));
@@ -170,5 +167,5 @@ pub fn send(&self, p: Packet) -> std::io::Result<usize> {
println!("Sending Response to: {:?}", addr); // Print the address
self.socket.send_to(p.encode(&mut self.out_buf.get()), addr)
}
}
}