pkts/
lib.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2//
3// Copyright (c) 2024 Nathaniel Bennett <me[at]nathanielbennett[dotcom]>
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10
11//! `pkts` - A library for creating, decoding and modifying packet layers.
12//!
13
14#![forbid(unsafe_code)]
15#![cfg_attr(not(feature = "std"), no_std)]
16// Show required OS/features on docs.rs.
17#![cfg_attr(docsrs, feature(doc_auto_cfg))]
18
19#[cfg(all(not(feature = "std"), feature = "alloc"))]
20extern crate alloc;
21
22pub mod dev_prelude;
23pub mod error;
24pub mod layers;
25#[doc(hidden)]
26pub mod prelude;
27pub mod sequence;
28pub mod session;
29pub mod utils;
30pub mod writer;
31
32#[cfg(feature = "alloc")]
33mod private {
34    pub trait Sealed {}
35}
36
37#[cfg(test)]
38#[cfg(feature = "alloc")]
39mod tests {
40    use crate::layers::udp::*;
41    use crate::sequence::ipv4::*;
42    use crate::sequence::LayeredSequence;
43    use pkts_common::BufferMut;
44
45    #[test]
46    fn udp_builder() {
47        let payload = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05];
48
49        let mut buffer = [0u8; 128];
50
51        let udp_builder = UdpBuilder::new(&mut buffer)
52            .sport(65321)
53            .dport(443)
54            .chksum(0)
55            .payload_raw(&payload);
56
57        let _buf: BufferMut<'_> = match udp_builder.build() {
58            Ok(buf) => buf,
59            Err(e) => panic!("{:?}", e),
60        };
61    }
62
63    #[test]
64    fn udp_builder_2() {
65        let inner_payload = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05];
66        let mut buffer = [0u8; 100];
67
68        let udp_builder = UdpBuilder::new(&mut buffer)
69            .sport(65321)
70            .dport(443)
71            .chksum(0)
72            .payload(|b| {
73                UdpBuilder::from_buffer(b)
74                    .sport(2452)
75                    .dport(80)
76                    .chksum(0)
77                    .payload_raw(&inner_payload)
78                    .build()
79            });
80
81        let _udp_packet = udp_builder.build().unwrap();
82    }
83
84    #[test]
85    fn multi_layer_sequence() {
86        let ip1 = Ipv4Sequence::new();
87
88        let mut _layered_seq = LayeredSequence::new(ip1, false)
89            .add(Ipv4Sequence::new(), true)
90            .add(Ipv4Sequence::new(), false)
91            .add(Ipv4Sequence::new(), true);
92    }
93}