Skip to main content

sniffnet_packet_parser/
icmp_type.rs

1use std::fmt::{Display, Formatter};
2
3use etherparse::{Icmpv4Type, Icmpv6Type};
4
5#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
6/// The ICMP message type.
7pub enum IcmpType {
8    V4(IcmpTypeV4),
9    V6(IcmpTypeV6),
10}
11
12impl Display for IcmpType {
13    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
14        write!(
15            f,
16            "{}",
17            match self {
18                IcmpType::V4(icmpv4_type) => {
19                    icmpv4_type.to_string()
20                }
21                IcmpType::V6(icmpv6_type) => {
22                    icmpv6_type.to_string()
23                }
24            }
25        )
26    }
27}
28
29#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
30#[allow(clippy::module_name_repetitions)]
31/// The `ICMPv4` message type.
32pub enum IcmpTypeV4 {
33    EchoReply,
34    DestinationUnreachable,
35    SourceQuench,
36    Redirect,
37    AlternateHostAddress,
38    Echo,
39    RouterAdvertisement,
40    RouterSolicitation,
41    TimeExceeded,
42    ParameterProblem,
43    Timestamp,
44    TimestampReply,
45    InformationRequest,
46    InformationReply,
47    AddressMaskRequest,
48    AddressMaskReply,
49    Traceroute,
50    DatagramConversionError,
51    MobileHostRedirect,
52    IPv6WhereAreYou,
53    IPv6IAmHere,
54    MobileRegistrationRequest,
55    MobileRegistrationReply,
56    DomainNameRequest,
57    DomainNameReply,
58    Skip,
59    Photuris,
60    ExtendedEchoRequest,
61    ExtendedEchoReply,
62    Unknown,
63}
64
65impl IcmpTypeV4 {
66    #[must_use]
67    pub(crate) fn from_etherparse(icmpv4type: &Icmpv4Type) -> IcmpType {
68        IcmpType::V4(match icmpv4type {
69            Icmpv4Type::EchoReply(_) => Self::EchoReply,
70            Icmpv4Type::DestinationUnreachable(_) => Self::DestinationUnreachable,
71            Icmpv4Type::Redirect(_) => Self::Redirect,
72            Icmpv4Type::EchoRequest(_) => Self::Echo,
73            Icmpv4Type::TimeExceeded(_) => Self::TimeExceeded,
74            Icmpv4Type::ParameterProblem(_) => Self::ParameterProblem,
75            Icmpv4Type::TimestampRequest(_) => Self::Timestamp,
76            Icmpv4Type::TimestampReply(_) => Self::TimestampReply,
77            Icmpv4Type::Unknown { type_u8: id, .. } => match id {
78                4 => Self::SourceQuench,
79                6 => Self::AlternateHostAddress,
80                9 => Self::RouterAdvertisement,
81                10 => Self::RouterSolicitation,
82                15 => Self::InformationRequest,
83                16 => Self::InformationReply,
84                17 => Self::AddressMaskRequest,
85                18 => Self::AddressMaskReply,
86                30 => Self::Traceroute,
87                31 => Self::DatagramConversionError,
88                32 => Self::MobileHostRedirect,
89                33 => Self::IPv6WhereAreYou,
90                34 => Self::IPv6IAmHere,
91                35 => Self::MobileRegistrationRequest,
92                36 => Self::MobileRegistrationReply,
93                37 => Self::DomainNameRequest,
94                38 => Self::DomainNameReply,
95                39 => Self::Skip,
96                40 => Self::Photuris,
97                42 => Self::ExtendedEchoRequest,
98                43 => Self::ExtendedEchoReply,
99                _ => Self::Unknown,
100            },
101        })
102    }
103}
104
105impl Display for IcmpTypeV4 {
106    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
107        write!(
108            f,
109            "{}",
110            match self {
111                IcmpTypeV4::EchoReply => "Echo Reply",
112                IcmpTypeV4::DestinationUnreachable => "Destination Unreachable",
113                IcmpTypeV4::SourceQuench => "Source Quench",
114                IcmpTypeV4::Redirect => "Redirect",
115                IcmpTypeV4::AlternateHostAddress => "Alternate Host Address",
116                IcmpTypeV4::Echo => "Echo",
117                IcmpTypeV4::RouterAdvertisement => "Router Advertisement",
118                IcmpTypeV4::RouterSolicitation => "Router Solicitation",
119                IcmpTypeV4::TimeExceeded => "Time Exceeded",
120                IcmpTypeV4::ParameterProblem => "Parameter Problem",
121                IcmpTypeV4::Timestamp => "Timestamp",
122                IcmpTypeV4::TimestampReply => "Timestamp Reply",
123                IcmpTypeV4::InformationRequest => "Information Request",
124                IcmpTypeV4::InformationReply => "Information Reply",
125                IcmpTypeV4::AddressMaskRequest => "Address Mask Request",
126                IcmpTypeV4::AddressMaskReply => "Address Mask Reply",
127                IcmpTypeV4::Traceroute => "Traceroute",
128                IcmpTypeV4::DatagramConversionError => "Datagram Conversion Error",
129                IcmpTypeV4::MobileHostRedirect => "Mobile Host Redirect",
130                IcmpTypeV4::IPv6WhereAreYou => "IPv6 Where-Are-You",
131                IcmpTypeV4::IPv6IAmHere => "IPv6 I-Am-Here",
132                IcmpTypeV4::MobileRegistrationRequest => "Mobile Registration Request",
133                IcmpTypeV4::MobileRegistrationReply => "Mobile Registration Reply",
134                IcmpTypeV4::DomainNameRequest => "Domain Name Request",
135                IcmpTypeV4::DomainNameReply => "Domain Name Reply",
136                IcmpTypeV4::Skip => "SKIP",
137                IcmpTypeV4::Photuris => "Photuris",
138                IcmpTypeV4::ExtendedEchoRequest => "Extended Echo Request",
139                IcmpTypeV4::ExtendedEchoReply => "Extended Echo Reply",
140                IcmpTypeV4::Unknown => "?",
141            }
142        )
143    }
144}
145
146#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
147#[allow(clippy::module_name_repetitions)]
148/// The `ICMPv6` message type.
149pub enum IcmpTypeV6 {
150    DestinationUnreachable,
151    PacketTooBig,
152    TimeExceeded,
153    ParameterProblem,
154    EchoRequest,
155    EchoReply,
156    MulticastListenerQuery,
157    MulticastListenerReport,
158    MulticastListenerDone,
159    RouterSolicitation,
160    RouterAdvertisement,
161    NeighborSolicitation,
162    NeighborAdvertisement,
163    RedirectMessage,
164    RouterRenumbering,
165    ICMPNodeInformationQuery,
166    ICMPNodeInformationResponse,
167    InverseNeighborDiscoverySolicitationMessage,
168    InverseNeighborDiscoveryAdvertisementMessage,
169    Version2MulticastListenerReport,
170    HomeAgentAddressDiscoveryRequestMessage,
171    HomeAgentAddressDiscoveryReplyMessage,
172    MobilePrefixSolicitation,
173    MobilePrefixAdvertisement,
174    CertificationPathSolicitationMessage,
175    CertificationPathAdvertisementMessage,
176    MulticastRouterAdvertisement,
177    MulticastRouterSolicitation,
178    MulticastRouterTermination,
179    FMIPv6Messages,
180    RPLControlMessage,
181    ILNPv6LocatorUpdateMessage,
182    DuplicateAddressRequest,
183    DuplicateAddressConfirmation,
184    MPLControlMessage,
185    ExtendedEchoRequest,
186    ExtendedEchoReply,
187    Unknown,
188}
189
190impl IcmpTypeV6 {
191    #[must_use]
192    pub(crate) fn from_etherparse(icmpv6type: &Icmpv6Type) -> IcmpType {
193        IcmpType::V6(match icmpv6type {
194            Icmpv6Type::DestinationUnreachable(_) => Self::DestinationUnreachable,
195            Icmpv6Type::PacketTooBig { .. } => Self::PacketTooBig,
196            Icmpv6Type::TimeExceeded(_) => Self::TimeExceeded,
197            Icmpv6Type::ParameterProblem(_) => Self::ParameterProblem,
198            Icmpv6Type::EchoRequest(_) => Self::EchoRequest,
199            Icmpv6Type::EchoReply(_) => Self::EchoReply,
200            Icmpv6Type::RouterSolicitation => Self::RouterSolicitation,
201            Icmpv6Type::RouterAdvertisement(_) => Self::RouterAdvertisement,
202            Icmpv6Type::NeighborSolicitation => Self::NeighborSolicitation,
203            Icmpv6Type::NeighborAdvertisement(_) => Self::NeighborAdvertisement,
204            Icmpv6Type::Redirect => Self::RedirectMessage,
205            Icmpv6Type::Unknown { type_u8: id, .. } => match id {
206                130 => Self::MulticastListenerQuery,
207                131 => Self::MulticastListenerReport,
208                132 => Self::MulticastListenerDone,
209                138 => Self::RouterRenumbering,
210                139 => Self::ICMPNodeInformationQuery,
211                140 => Self::ICMPNodeInformationResponse,
212                141 => Self::InverseNeighborDiscoverySolicitationMessage,
213                142 => Self::InverseNeighborDiscoveryAdvertisementMessage,
214                143 => Self::Version2MulticastListenerReport,
215                144 => Self::HomeAgentAddressDiscoveryRequestMessage,
216                145 => Self::HomeAgentAddressDiscoveryReplyMessage,
217                146 => Self::MobilePrefixSolicitation,
218                147 => Self::MobilePrefixAdvertisement,
219                148 => Self::CertificationPathSolicitationMessage,
220                149 => Self::CertificationPathAdvertisementMessage,
221                151 => Self::MulticastRouterAdvertisement,
222                152 => Self::MulticastRouterSolicitation,
223                153 => Self::MulticastRouterTermination,
224                154 => Self::FMIPv6Messages,
225                155 => Self::RPLControlMessage,
226                156 => Self::ILNPv6LocatorUpdateMessage,
227                157 => Self::DuplicateAddressRequest,
228                158 => Self::DuplicateAddressConfirmation,
229                159 => Self::MPLControlMessage,
230                160 => Self::ExtendedEchoRequest,
231                161 => Self::ExtendedEchoReply,
232                _ => Self::Unknown,
233            },
234        })
235    }
236}
237
238impl Display for IcmpTypeV6 {
239    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
240        write!(
241            f,
242            "{}",
243            match self {
244                IcmpTypeV6::DestinationUnreachable => "Destination Unreachable",
245                IcmpTypeV6::PacketTooBig => "Packet Too Big",
246                IcmpTypeV6::TimeExceeded => "Time Exceeded",
247                IcmpTypeV6::ParameterProblem => "Parameter Problem",
248                IcmpTypeV6::EchoRequest => "Echo Request",
249                IcmpTypeV6::EchoReply => "Echo Reply",
250                IcmpTypeV6::MulticastListenerQuery => "Multicast Listener Query",
251                IcmpTypeV6::MulticastListenerReport => "Multicast Listener Report",
252                IcmpTypeV6::MulticastListenerDone => "Multicast Listener Done",
253                IcmpTypeV6::RouterSolicitation => "Router Solicitation",
254                IcmpTypeV6::RouterAdvertisement => "Router Advertisement",
255                IcmpTypeV6::NeighborSolicitation => "Neighbor Solicitation",
256                IcmpTypeV6::NeighborAdvertisement => "Neighbor Advertisement",
257                IcmpTypeV6::RedirectMessage => "Redirect Message",
258                IcmpTypeV6::RouterRenumbering => "Router Renumbering",
259                IcmpTypeV6::ICMPNodeInformationQuery => "ICMP Node Information Query",
260                IcmpTypeV6::ICMPNodeInformationResponse => "ICMP Node Information Response",
261                IcmpTypeV6::InverseNeighborDiscoverySolicitationMessage =>
262                    "Inverse Neighbor Discovery Solicitation Message",
263                IcmpTypeV6::InverseNeighborDiscoveryAdvertisementMessage =>
264                    "Inverse Neighbor Discovery Advertisement Message",
265                IcmpTypeV6::Version2MulticastListenerReport =>
266                    "Version 2 Multicast Listener Report",
267                IcmpTypeV6::HomeAgentAddressDiscoveryRequestMessage =>
268                    "Home Agent Address Discovery Request Message",
269                IcmpTypeV6::HomeAgentAddressDiscoveryReplyMessage =>
270                    "Home Agent Address Discovery Reply Message",
271                IcmpTypeV6::MobilePrefixSolicitation => "Mobile Prefix Solicitation",
272                IcmpTypeV6::MobilePrefixAdvertisement => "Mobile Prefix Advertisement",
273                IcmpTypeV6::CertificationPathSolicitationMessage =>
274                    "Certification Path Solicitation Message",
275                IcmpTypeV6::CertificationPathAdvertisementMessage =>
276                    "Certification Path Advertisement Message",
277                IcmpTypeV6::MulticastRouterAdvertisement => "Multicast Router Advertisement",
278                IcmpTypeV6::MulticastRouterSolicitation => "Multicast Router Solicitation",
279                IcmpTypeV6::MulticastRouterTermination => "Multicast Router Termination",
280                IcmpTypeV6::FMIPv6Messages => "FMIPv6 Messages",
281                IcmpTypeV6::RPLControlMessage => "RPL Control Message",
282                IcmpTypeV6::ILNPv6LocatorUpdateMessage => "ILNPv6 Locator Update Message",
283                IcmpTypeV6::DuplicateAddressRequest => "Duplicate Address Request",
284                IcmpTypeV6::DuplicateAddressConfirmation => "Duplicate Address Confirmation",
285                IcmpTypeV6::MPLControlMessage => "MPL Control Message",
286                IcmpTypeV6::ExtendedEchoRequest => "Extended Echo Request",
287                IcmpTypeV6::ExtendedEchoReply => "Extended Echo Reply",
288                IcmpTypeV6::Unknown => "?",
289            }
290        )
291    }
292}