Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

map the subnet code to SubnetOption #15

Merged
merged 2 commits into from
May 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/options.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use num_traits::FromPrimitive;
use std::net::Ipv4Addr;

#[derive(PartialEq, Clone)]
#[derive(PartialEq, Clone, Debug)]
pub struct RawDhcpOption {
pub code: u8,
pub data: Vec<u8>,
}

#[derive(PartialEq)]
#[derive(PartialEq, Debug)]
pub enum DhcpOption {
DhcpMessageType(MessageType),
ServerIdentifier(Ipv4Addr),
Expand Down Expand Up @@ -318,7 +318,7 @@ pub fn title(code: u8) -> Option<&'static str> {
/// > This option is used to convey the type of the DHCP message. The code for this option is 53,
/// > and its length is 1.
///
#[derive(Primitive, Copy, Clone, PartialEq)]
#[derive(Primitive, Copy, Clone, PartialEq, Debug)]
pub enum MessageType {
/// Client broadcast to locate available servers.
Discover = 1,
Expand Down
2 changes: 2 additions & 0 deletions src/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ impl<I> nom::error::ParseError<I> for Err<I> {
type IResult<I, O> = nom::IResult<I, O, Err<I>>;

/// DHCP Packet Structure
#[derive(Debug)]
pub struct Packet {
pub reply: bool, // false = request, true = reply
pub hops: u8,
Expand Down Expand Up @@ -80,6 +81,7 @@ pub fn decode_option(input: &[u8]) -> IResult<&[u8], DhcpOption> {
ROUTER => DhcpOption::Router(many0(decode_ipv4)(data)?.1),
DOMAIN_NAME_SERVER => DhcpOption::DomainNameServer(many0(decode_ipv4)(data)?.1),
IP_ADDRESS_LEASE_TIME => DhcpOption::IpAddressLeaseTime(be_u32(data)?.1),
SUBNET_MASK => DhcpOption::SubnetMask(decode_ipv4(data)?.1),
MESSAGE => DhcpOption::Message(match std::str::from_utf8(data) {
Ok(s) => s.to_string(),
Err(_) => return Err(nom::Err::Error(Err::NonUtf8String)),
Expand Down
4 changes: 3 additions & 1 deletion src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ 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,
}
}
Expand Down