diff --git a/src/packet.rs b/src/packet.rs index 8984204..2d41431 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -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> { - if input.len() < 1 { + if input.is_empty() { return Err(CustomErr::InvalidHlen); } @@ -184,16 +184,11 @@ fn decode(input: &[u8]) -> IResult<&[u8], Packet> { let mut options = Vec::new(); let mut rest = input; - loop { - match decode_option(rest) { - Ok((new_rest, option)) => { - rest = new_rest; - options.push(option); - if rest.starts_with(&[END]) { - break; - } - } - Err(_) => break, + while let Ok((new_rest, option)) = decode_option(rest) { + rest = new_rest; + options.push(option); + if rest.starts_with(&[END]) { + break; } } @@ -226,12 +221,7 @@ impl Packet { /// Extracts requested option payload from packet if available pub fn option(&self, code: u8) -> Option<&DhcpOption> { - for option in &self.options { - if option.code() == code { - return Some(&option); - } - } - None + self.options.iter().find(|&option| option.code() == code) } /// Convenience function for extracting a packet's message type. diff --git a/src/server.rs b/src/server.rs index ee13473..c1d4802 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1,3 +1,5 @@ +//! This is a convenience module that simplifies the writing of a DHCP server service. + use std::cell::Cell; use std::net::{IpAddr, Ipv4Addr, SocketAddr, UdpSocket}; @@ -5,8 +7,6 @@ use crate::options; use crate::options::{DhcpOption, MessageType}; use crate::packet::*; -///! This is a convenience module that simplifies the writing of a DHCP server service. - pub struct Server { out_buf: Cell<[u8; 1500]>, socket: UdpSocket, @@ -22,12 +22,12 @@ pub trait Handler { pub fn filter_options_by_req(opts: &mut Vec, req_params: &[u8]) { let mut pos = 0; let h = &[ - options::DHCP_MESSAGE_TYPE as u8, - options::SERVER_IDENTIFIER as u8, - options::SUBNET_MASK as u8, - options::IP_ADDRESS_LEASE_TIME as u8, - options::DOMAIN_NAME_SERVER as u8, - options::ROUTER as u8, + options::DHCP_MESSAGE_TYPE, + options::SERVER_IDENTIFIER, + options::SUBNET_MASK, + options::IP_ADDRESS_LEASE_TIME, + options::DOMAIN_NAME_SERVER, + options::ROUTER, ] as &[u8]; // Process options from req_params @@ -132,7 +132,7 @@ impl Server { if let Some(DhcpOption::ParameterRequestList(prl)) = req_packet.option(options::PARAMETER_REQUEST_LIST) { - filter_options_by_req(&mut opts, &prl); + filter_options_by_req(&mut opts, prl); } self.send(Packet {