|
1 |
| -//! Low-level packet access and construction. |
2 |
| -//! |
3 |
| -//! The `wire` module deals with the packet *representation*. It provides two levels |
4 |
| -//! of functionality. |
5 |
| -//! |
6 |
| -//! * First, it provides functions to extract fields from sequences of octets, |
7 |
| -//! and to insert fields into sequences of octets. This happens `Packet` family of |
8 |
| -//! structures, e.g. [EthernetFrame] or [Ipv4Packet]. |
9 |
| -//! * Second, in cases where the space of valid field values is much smaller than the space |
10 |
| -//! of possible field values, it provides a compact, high-level representation |
11 |
| -//! of packet data that can be parsed from and emitted into a sequence of octets. |
12 |
| -//! This happens through the `Repr` family of structs and enums, e.g. [ArpRepr] or [Ipv4Repr]. |
13 |
| -// https://github.com/rust-lang/rust/issues/38739 |
14 |
| -//! </ul> |
15 |
| -//! |
16 |
| -//! [EthernetFrame]: struct.EthernetFrame.html |
17 |
| -//! [Ipv4Packet]: struct.Ipv4Packet.html |
18 |
| -//! [ArpRepr]: enum.ArpRepr.html |
19 |
| -//! [Ipv4Repr]: struct.Ipv4Repr.html |
20 |
| -//! |
21 |
| -//! The functions in the `wire` module are designed for use together with `-Cpanic=abort`. |
22 |
| -//! |
23 |
| -//! The `Packet` family of data structures guarantees that, if the `Packet::check_len()` method |
24 |
| -//! returned `Ok(())`, then no accessor or setter method will panic; however, the guarantee |
25 |
| -//! provided by `Packet::check_len()` may no longer hold after changing certain fields, |
26 |
| -//! which are listed in the documentation for the specific packet. |
27 |
| -//! |
28 |
| -//! The `Packet::new_checked` method is a shorthand for a combination of `Packet::new` and |
29 |
| -//! `Packet::check_len`. |
30 |
| -//! When parsing untrusted input, it is *necessary* to use `Packet::new_checked()`; |
31 |
| -//! so long as the buffer is not modified, no accessor will fail. |
32 |
| -//! When emitting output, though, it is *incorrect* to use `Packet::new_checked()`; |
33 |
| -//! the length check is likely to succeed on a zeroed buffer, but fail on a buffer |
34 |
| -//! filled with data from a previous packet, such as when reusing buffers, resulting |
35 |
| -//! in nondeterministic panics with some network devices but not others. |
36 |
| -//! The buffer length for emission is not calculated by the `Packet` layer. |
37 |
| -//! |
38 |
| -//! In the `Repr` family of data structures, the `Repr::parse()` method never panics |
39 |
| -//! as long as `Packet::new_checked()` (or `Packet::check_len()`) has succeeded, and |
40 |
| -//! the `Repr::emit()` method never panics as long as the underlying buffer is exactly |
41 |
| -//! `Repr::buffer_len()` octets long. |
42 |
| -//! |
43 |
| -//! # Examples |
44 |
| -//! |
45 |
| -//! To emit an IP packet header into an octet buffer, and then parse it back: |
46 |
| -//! |
47 |
| -/*! |
| 1 | +/*! Low-level packet access and construction. |
| 2 | +
|
| 3 | +The `wire` module deals with the packet *representation*. It provides two levels |
| 4 | +of functionality. |
| 5 | +
|
| 6 | + * First, it provides functions to extract fields from sequences of octets, |
| 7 | + and to insert fields into sequences of octets. This happens `Packet` family of |
| 8 | + structures, e.g. [EthernetFrame] or [Ipv4Packet]. |
| 9 | + * Second, in cases where the space of valid field values is much smaller than the space |
| 10 | + of possible field values, it provides a compact, high-level representation |
| 11 | + of packet data that can be parsed from and emitted into a sequence of octets. |
| 12 | + This happens through the `Repr` family of structs and enums, e.g. [ArpRepr] or [Ipv4Repr]. |
| 13 | +
|
| 14 | +[EthernetFrame]: struct.EthernetFrame.html |
| 15 | +[Ipv4Packet]: struct.Ipv4Packet.html |
| 16 | +[ArpRepr]: enum.ArpRepr.html |
| 17 | +[Ipv4Repr]: struct.Ipv4Repr.html |
| 18 | +
|
| 19 | +The functions in the `wire` module are designed for use together with `-Cpanic=abort`. |
| 20 | +
|
| 21 | +The `Packet` family of data structures guarantees that, if the `Packet::check_len()` method |
| 22 | +returned `Ok(())`, then no accessor or setter method will panic; however, the guarantee |
| 23 | +provided by `Packet::check_len()` may no longer hold after changing certain fields, |
| 24 | +which are listed in the documentation for the specific packet. |
| 25 | +
|
| 26 | +The `Packet::new_checked` method is a shorthand for a combination of `Packet::new` and |
| 27 | +`Packet::check_len`. |
| 28 | +When parsing untrusted input, it is *necessary* to use `Packet::new_checked()`; |
| 29 | +so long as the buffer is not modified, no accessor will fail. |
| 30 | +When emitting output, though, it is *incorrect* to use `Packet::new_checked()`; |
| 31 | +the length check is likely to succeed on a zeroed buffer, but fail on a buffer |
| 32 | +filled with data from a previous packet, such as when reusing buffers, resulting |
| 33 | +in nondeterministic panics with some network devices but not others. |
| 34 | +The buffer length for emission is not calculated by the `Packet` layer. |
| 35 | +
|
| 36 | +In the `Repr` family of data structures, the `Repr::parse()` method never panics |
| 37 | +as long as `Packet::new_checked()` (or `Packet::check_len()`) has succeeded, and |
| 38 | +the `Repr::emit()` method never panics as long as the underlying buffer is exactly |
| 39 | +`Repr::buffer_len()` octets long. |
| 40 | +
|
| 41 | +# Examples |
| 42 | +
|
| 43 | +To emit an IP packet header into an octet buffer, and then parse it back: |
| 44 | +
|
48 | 45 | ```rust
|
49 | 46 | # #[cfg(feature = "proto-ipv4")]
|
50 | 47 | # {
|
|
0 commit comments