Skip to content

Commit

Permalink
chore(wire): add check_len to all parse methods
Browse files Browse the repository at this point in the history
This commit adds a call to `check_len` to all parse methods in the
`wire` module. This ensures that no accessor methods are valid,
helping the compiler in optimizing.
  • Loading branch information
thvdveld committed Jan 3, 2024
1 parent 3f2a219 commit abef4e9
Show file tree
Hide file tree
Showing 21 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/wire/arp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,8 @@ impl Repr {
/// Parse an Address Resolution Protocol packet and return a high-level representation,
/// or return `Err(Error)` if the packet is not recognized.
pub fn parse<T: AsRef<[u8]>>(packet: &Packet<T>) -> Result<Repr> {
packet.check_len()?;

match (
packet.hardware_type(),
packet.protocol_type(),
Expand Down
1 change: 1 addition & 0 deletions src/wire/dhcpv4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,7 @@ impl<'a> Repr<'a> {
where
T: AsRef<[u8]> + ?Sized,
{
packet.check_len()?;
let transaction_id = packet.transaction_id();
let client_hardware_address = packet.client_hardware_address();
let client_ip = packet.client_ip();
Expand Down
1 change: 1 addition & 0 deletions src/wire/ethernet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ impl Repr {

/// Emit a high-level representation into an Ethernet II frame.
pub fn emit<T: AsRef<[u8]> + AsMut<[u8]>>(&self, frame: &mut Frame<T>) {
assert!(frame.buffer.as_ref().len() >= self.buffer_len());
frame.set_src_addr(self.src_addr);
frame.set_dst_addr(self.dst_addr);
frame.set_ethertype(self.ethertype);
Expand Down
2 changes: 2 additions & 0 deletions src/wire/icmpv4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,8 @@ impl<'a> Repr<'a> {
where
T: AsRef<[u8]> + ?Sized,
{
packet.check_len()?;

// Valid checksum is expected.
if checksum_caps.icmpv4.rx() && !packet.verify_checksum() {
return Err(Error);
Expand Down
2 changes: 2 additions & 0 deletions src/wire/icmpv6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,8 @@ impl<'a> Repr<'a> {
where
T: AsRef<[u8]> + ?Sized,
{
packet.check_len()?;

fn create_packet_from_payload<'a, T>(packet: &Packet<&'a T>) -> Result<(&'a [u8], Ipv6Repr)>
where
T: AsRef<[u8]> + ?Sized,
Expand Down
2 changes: 2 additions & 0 deletions src/wire/igmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ impl Repr {
where
T: AsRef<[u8]> + ?Sized,
{
packet.check_len()?;

// Check if the address is 0.0.0.0 or multicast
let addr = packet.group_addr();
if !addr.is_unspecified() && !addr.is_multicast() {
Expand Down
1 change: 1 addition & 0 deletions src/wire/ipsec_ah.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ pub struct Repr<'a> {
impl<'a> Repr<'a> {
/// Parse an IPSec Authentication Header packet and return a high-level representation.
pub fn parse<T: AsRef<[u8]> + ?Sized>(packet: &Packet<&'a T>) -> Result<Repr<'a>> {
packet.check_len()?;
Ok(Repr {
next_header: packet.next_header(),
security_parameters_index: packet.security_parameters_index(),
Expand Down
1 change: 1 addition & 0 deletions src/wire/ipsec_esp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ pub struct Repr {
impl Repr {
/// Parse an IPSec Encapsulating Security Payload packet and return a high-level representation.
pub fn parse<T: AsRef<[u8]>>(packet: &Packet<T>) -> Result<Repr> {
packet.check_len()?;
Ok(Repr {
security_parameters_index: packet.security_parameters_index(),
sequence_number: packet.sequence_number(),
Expand Down
1 change: 1 addition & 0 deletions src/wire/ipv4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,7 @@ impl Repr {
packet: &Packet<&T>,
checksum_caps: &ChecksumCapabilities,
) -> Result<Repr> {
packet.check_len()?;
// Version 4 is expected.
if packet.version() != 4 {
return Err(Error);
Expand Down
1 change: 1 addition & 0 deletions src/wire/ipv6ext_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ impl<'a> Repr<'a> {
where
T: AsRef<[u8]> + ?Sized,
{
header.check_len()?;
Ok(Self {
next_header: header.next_header(),
length: header.header_len(),
Expand Down
1 change: 1 addition & 0 deletions src/wire/ipv6fragment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ impl Repr {
where
T: AsRef<[u8]> + ?Sized,
{
header.check_len()?;
Ok(Repr {
frag_offset: header.frag_offset(),
more_frags: header.more_frags(),
Expand Down
2 changes: 2 additions & 0 deletions src/wire/ipv6hbh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ impl<'a> Repr<'a> {
where
T: AsRef<[u8]> + ?Sized,
{
header.check_len()?;

let mut options = Vec::new();

let iter = Ipv6OptionsIterator::new(header.options());
Expand Down
1 change: 1 addition & 0 deletions src/wire/ipv6option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ impl<'a> Repr<'a> {
where
T: AsRef<[u8]> + ?Sized,
{
opt.check_len()?;
match opt.option_type() {
Type::Pad1 => Ok(Repr::Pad1),
Type::PadN => Ok(Repr::PadN(opt.data_len())),
Expand Down
1 change: 1 addition & 0 deletions src/wire/ipv6routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ impl<'a> Repr<'a> {
where
T: AsRef<[u8]> + ?Sized,
{
header.check_len()?;
match header.routing_type() {
Type::Type2 => Ok(Repr::Type2 {
segments_left: header.segments_left(),
Expand Down
1 change: 1 addition & 0 deletions src/wire/mld.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ impl<'a> Repr<'a> {
where
T: AsRef<[u8]> + ?Sized,
{
packet.check_len()?;
match packet.msg_type() {
Message::MldQuery => Ok(Repr::Query {
max_resp_code: packet.max_resp_code(),
Expand Down
2 changes: 2 additions & 0 deletions src/wire/ndisc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ impl<'a> Repr<'a> {
where
T: AsRef<[u8]> + ?Sized,
{
packet.check_len()?;

let (mut src_ll_addr, mut mtu, mut prefix_info, mut target_ll_addr, mut redirected_hdr) =
(None, None, None, None, None);

Expand Down
2 changes: 2 additions & 0 deletions src/wire/ndiscoption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,8 @@ impl<'a> Repr<'a> {
where
T: AsRef<[u8]> + ?Sized,
{
opt.check_len()?;

match opt.option_type() {
Type::SourceLinkLayerAddr => {
if opt.data_len() >= 1 {
Expand Down
2 changes: 2 additions & 0 deletions src/wire/rpl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,8 @@ impl<'p> Repr<'p> {
}

pub fn parse<T: AsRef<[u8]> + ?Sized>(packet: &Packet<&'p T>) -> Result<Self> {
packet.check_len()?;

let options = packet.options()?;
match RplControlMessage::from(packet.msg_code()) {
RplControlMessage::DodagInformationSolicitation => {
Expand Down
1 change: 1 addition & 0 deletions src/wire/sixlowpan/frag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ impl defmt::Format for Repr {
impl Repr {
/// Parse a 6LoWPAN Fragment header.
pub fn parse<T: AsRef<[u8]>>(packet: &Packet<T>) -> Result<Self> {
packet.check_len()?;
let size = packet.datagram_size();
let tag = packet.datagram_tag();

Expand Down
2 changes: 2 additions & 0 deletions src/wire/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,8 @@ impl<'a> Repr<'a> {
where
T: AsRef<[u8]> + ?Sized,
{
packet.check_len()?;

// Source and destination ports must be present.
if packet.src_port() == 0 {
return Err(Error);
Expand Down
2 changes: 2 additions & 0 deletions src/wire/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ impl Repr {
where
T: AsRef<[u8]> + ?Sized,
{
packet.check_len()?;

// Destination port cannot be omitted (but source port can be).
if packet.dst_port() == 0 {
return Err(Error);
Expand Down

0 comments on commit abef4e9

Please sign in to comment.