Skip to main content

pdu/
icmp.rs

1/*
2   Copyright (c) 2019 Alex Forster <alex@alexforster.com>
3
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7
8       http://www.apache.org/licenses/LICENSE-2.0
9
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15
16   SPDX-License-Identifier: Apache-2.0
17*/
18
19use core::convert::TryInto;
20
21use crate::{util, Error, Result};
22
23/// Represents an ICMP payload
24#[derive(Debug, Copy, Clone)]
25pub struct IcmpPdu<'a> {
26    buffer: &'a [u8],
27}
28
29/// Contains the inner payload of an [`IcmpPdu`]
30#[derive(Debug, Copy, Clone)]
31pub enum Icmp<'a> {
32    Raw(&'a [u8]),
33}
34
35impl<'a> IcmpPdu<'a> {
36    /// Constructs a [`IcmpPdu`] backed by the provided `buffer`
37    pub fn new(buffer: &'a [u8]) -> Result<Self> {
38        if buffer.len() < 8 {
39            return Err(Error::Truncated);
40        }
41        Ok(IcmpPdu { buffer })
42    }
43
44    /// Returns a reference to the entire underlying buffer that was provided during construction
45    pub fn buffer(&'a self) -> &'a [u8] {
46        self.buffer
47    }
48
49    /// Consumes this object and returns a reference to the entire underlying buffer that was provided during
50    /// construction
51    pub fn into_buffer(self) -> &'a [u8] {
52        self.buffer
53    }
54
55    /// Returns the slice of the underlying buffer that contains the header part of this PDU
56    pub fn as_bytes(&'a self) -> &'a [u8] {
57        self.clone().into_bytes()
58    }
59
60    /// Consumes this object and returns the slice of the underlying buffer that contains the header part of this PDU
61    pub fn into_bytes(self) -> &'a [u8] {
62        &self.buffer[0..8]
63    }
64
65    /// Returns an object representing the inner payload of this PDU
66    pub fn inner(&'a self) -> Result<Icmp<'a>> {
67        self.clone().into_inner()
68    }
69
70    /// Consumes this object and returns an object representing the inner payload of this PDU
71    pub fn into_inner(self) -> Result<Icmp<'a>> {
72        let rest = &self.buffer[4..];
73        Ok(Icmp::Raw(rest))
74    }
75
76    pub fn message_type(&'a self) -> u8 {
77        self.buffer[0]
78    }
79
80    pub fn message_code(&'a self) -> u8 {
81        self.buffer[1]
82    }
83
84    pub fn checksum(&'a self) -> u16 {
85        u16::from_be_bytes(self.buffer[2..=3].try_into().unwrap())
86    }
87
88    pub fn computed_checksum(&'a self, ip: &crate::Ip) -> u16 {
89        match ip {
90            crate::Ip::Ipv4(_) => util::checksum(&[&self.buffer[0..=1], &self.buffer[4..]]),
91            crate::Ip::Ipv6(ipv6) => util::checksum(&[
92                &ipv6.source_address().as_ref(),
93                &ipv6.destination_address().as_ref(),
94                &(ipv6.payload_length() as u32).to_be_bytes().as_ref(),
95                &[0x0, 0x0, 0x0, ipv6.computed_protocol()].as_ref(),
96                &self.buffer[0..=1],
97                &self.buffer[4..],
98            ]),
99        }
100    }
101
102    #[deprecated(since = "1.3.0", note = "use IcmpPdu::inner()")]
103    pub fn message(&'a self) -> &'a [u8] {
104        &self.buffer[4..]
105    }
106
107    pub fn computed_data_offset(&'a self) -> usize {
108        4
109    }
110}