1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//! Packet wire format parsing and building.
//!
//! The following packet are supported:
//! - `ICMPv4`
//! - `ICMPv6`
//! - `IPv4`
//! - `IPv6`
//! - `UDP`
//! - `TCP`
//! - `ICMP` extensions
//!
//! # Endianness
//!
//! The internal representation is held in network byte order (big-endian) and
//! all accessor methods take and return data in host byte order, converting as
//! necessary for the given architecture.
//!
//! # Example
//!
//! The following example parses an `UDP` packet and asserts its fields:
//!
//! ```rust
//! # fn main() -> anyhow::Result<()> {
//! use trippy_packet::udp::UdpPacket;
//!
//! let buf = hex_literal::hex!("68 bf 81 b6 00 40 ac be");
//! let packet = UdpPacket::new_view(&buf)?;
//! assert_eq!(26815, packet.get_source());
//! assert_eq!(33206, packet.get_destination());
//! assert_eq!(64, packet.get_length());
//! assert_eq!(44222, packet.get_checksum());
//! assert!(packet.payload().is_empty());
//! # Ok(())
//! # }
//! ```
//!
//! The following example builds an `ICMPv4` echo request packet:
//!
//! ```rust
//! # fn main() -> anyhow::Result<()> {
//! use trippy_packet::checksum::icmp_ipv4_checksum;
//! use trippy_packet::icmpv4::echo_request::EchoRequestPacket;
//! use trippy_packet::icmpv4::{IcmpCode, IcmpPacket, IcmpType};
//!
//! let mut buf = [0; IcmpPacket::minimum_packet_size()];
//! let mut icmp = EchoRequestPacket::new(&mut buf)?;
//! icmp.set_icmp_type(IcmpType::EchoRequest);
//! icmp.set_icmp_code(IcmpCode(0));
//! icmp.set_identifier(1234);
//! icmp.set_sequence(10);
//! icmp.set_checksum(icmp_ipv4_checksum(icmp.packet()));
//! assert_eq!(icmp.packet(), &hex_literal::hex!("08 00 f3 23 04 d2 00 0a"));
//! # Ok(())
//! # }
//! ```
/// Packet errors.
/// Functions for calculating network checksums.
/// `ICMPv4` packets.
/// `ICMPv6` packets.
/// `ICMP` extensions.
/// `IPv4` packets.
/// `IPv6` packets.
/// `UDP` packets.
/// `TCP` packets.
/// The IP packet next layer protocol.
/// Format a payload as a hexadecimal string.