extend dhcp sample packet and separate file and server_name parsing in preparation for overloaded options parsing
This commit is contained in:
parent
e0cc79198f
commit
0bf69237b9
Binary file not shown.
68
src/lib.rs
68
src/lib.rs
@ -86,17 +86,15 @@ fn parse_client_hardware_address(i: &[u8], hardware_address_length: u8) -> IResu
|
||||
}
|
||||
|
||||
fn parse_server_name(i: &[u8]) -> IResult<&[u8], &str> {
|
||||
let (i, val) = take(64usize)(i)?;
|
||||
let nullbyte_position = val.iter().position(|v| *v == b'\0').unwrap_or(64);
|
||||
let val = unsafe { std::str::from_utf8_unchecked(&val[..nullbyte_position]) };
|
||||
let nullbyte_position = i.iter().position(|v| *v == b'\0').unwrap_or(64);
|
||||
let val = unsafe { std::str::from_utf8_unchecked(&i[..nullbyte_position]) };
|
||||
|
||||
Ok((i, val))
|
||||
}
|
||||
|
||||
fn parse_file(i: &[u8]) -> IResult<&[u8], &Path> {
|
||||
let (i, val) = take(128usize)(i)?;
|
||||
let nullbyte_position = val.iter().position(|v| *v == b'\0').unwrap_or(128);
|
||||
let val = unsafe { std::str::from_utf8_unchecked(&val[..nullbyte_position]) };
|
||||
let nullbyte_position = i.iter().position(|v| *v == b'\0').unwrap_or(128);
|
||||
let val = unsafe { std::str::from_utf8_unchecked(&i[..nullbyte_position]) };
|
||||
|
||||
Ok((i, Path::new(val)))
|
||||
}
|
||||
@ -114,10 +112,11 @@ fn parse_dhcp_packet(packet: &[u8]) -> IResult<&[u8], DhcpMessage> {
|
||||
let (packet, gateway_address) = parse_ipv4_address(packet)?;
|
||||
let (packet, client_hardware_address) =
|
||||
parse_client_hardware_address(packet, hardware_address_length)?;
|
||||
let potential_options = packet;
|
||||
|
||||
let (packet, server_name) = parse_server_name(packet)?;
|
||||
let (packet, file_name) = parse_file(packet)?;
|
||||
let (packet, server_name_bytes) = take(64usize)(packet)?;
|
||||
let (packet, file_name_bytes) = take(128usize)(packet)?;
|
||||
|
||||
let (_, server_name) = parse_server_name(server_name_bytes)?;
|
||||
let (_, file_name) = parse_file(file_name_bytes)?;
|
||||
|
||||
Ok((
|
||||
packet,
|
||||
@ -141,30 +140,39 @@ fn parse_dhcp_packet(packet: &[u8]) -> IResult<&[u8], DhcpMessage> {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
static DHCP_PACKET: &'static [u8] = include_bytes!("../dhcp_discover.bin");
|
||||
static DHCP_PACKET2: &'static [u8] = &[
|
||||
0x01, 0x01, 0x06, 0x05, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x7f, 0x00, 0x00,
|
||||
0x01, 0x7f, 0x00, 0x00, 0x01, 0x7f, 0x00, 0x00, 0x01, 0x54, 0x05, 0xdb, 0xa6, 0x96, 0xd0,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //CHADDR padding
|
||||
0x49, 0x20, 0x62, 0x69, 0x6D, 0x73, 0x20, 0x31, 0x20, 0x44, 0x48, 0x43, 0x50, 0x20, 0x53,
|
||||
0x65, 0x72, 0x76, 0x65, 0x72, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, // server name with padding
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // file with padding
|
||||
#[rustfmt::skip]
|
||||
static DHCP_PACKET: &'static [u8] = &[
|
||||
0x01, // op
|
||||
0x01, // htype
|
||||
0x06, // hlen
|
||||
0x05, // hops
|
||||
0x00, 0x00, 0x00, 0x01, // xid
|
||||
0x00, 0x02, // secs
|
||||
0x00, 0x01, // flags
|
||||
0x7f, 0x00, 0x00, 0x01, // ciaddr
|
||||
0x7f, 0x00, 0x00, 0x01, // siaddr
|
||||
0x7f, 0x00, 0x00, 0x01, // giaddr
|
||||
// chaddr
|
||||
0x54, 0x05, 0xdb, 0xa6, 0x96, 0xd0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
// server name
|
||||
0x49, 0x20, 0x62, 0x69, 0x6D, 0x73, 0x20, 0x31, 0x20, 0x44, 0x48, 0x43, 0x50, 0x20, 0x53, 0x65,
|
||||
0x72, 0x76, 0x65, 0x72, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
// file
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
];
|
||||
|
||||
#[test]
|
||||
fn parses() {
|
||||
let res = super::parse_dhcp_packet(DHCP_PACKET2).unwrap();
|
||||
let res = super::parse_dhcp_packet(DHCP_PACKET).unwrap();
|
||||
dbg!(res);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user