Skip to main content

dvb_ule/
lib.rs

1//! ULE — Unidirectional Lightweight Encapsulation (RFC 4326) + Extension
2//! Headers (RFC 5163): IP over MPEG-2 Transport Streams.
3//!
4//! ULE encapsulates a network-layer PDU (an IP datagram, Ethernet frame, etc.)
5//! into a **SubNetwork Data Unit** ([`Sndu`]) and maps it into the payload of
6//! MPEG-2 TS packets on a single PID (RFC 4326 §3, §4).
7//!
8//! This crate implements:
9//!
10//! - [`Sndu`] — the SNDU wire structure (RFC 4326 §4, Figure 1): the `D` bit +
11//!   15-bit `Length` + 16-bit `Type`, an optional 6-byte Destination NPA
12//!   address (present iff `D = 0`), the PDU, and the 4-byte CRC-32 trailer.
13//!   `Length` and the CRC are **recomputed on serialize** from the typed
14//!   fields — there is no raw passthrough.
15//! - [`TypeField`] — the §4.4 split at `0x0600`: a Next-Header (`H-LEN`/
16//!   `H-Type`) below, an EtherType at or above.
17//! - [`ExtensionHeader`] / [`PayloadChain`] — the chained extension-header
18//!   model (RFC 4326 §5, RFC 5163 §3): Optional headers (`H-LEN = 1..=5`,
19//!   total `2·H-LEN` bytes) and a terminating EtherType or Mandatory header
20//!   (Test-SNDU 0x00, Bridged-Frame 0x01, TS-Concat 0x02, PDU-Concat 0x03).
21//! - [`UleReceiver`] — TS-packet de-fragmentation/reassembly (RFC 4326 §6, §7):
22//!   PUSI + 1-byte Payload Pointer handling, fragmentation across packets,
23//!   packing of multiple SNDUs per packet, and End-Indicator / 0xFF padding.
24//!
25//! The CRC-32 is the MPEG-2 / DSM-CC CRC (poly `0x04C11DB7`, init
26//! `0xFFFFFFFF`, MSB-first, no reflection, no final XOR — RFC 4326 §4.6),
27//! reused from [`dvb_common::crc32_mpeg2`]; this is verified byte-exact against
28//! RFC 4326 Appendix B's worked example in the crate's fixture test.
29//!
30//! `#![no_std]` + `alloc`; depends only on `dvb-common`.
31//!
32//! # Examples
33//!
34//! Build an IPv4 SNDU (L2 filtering, `D = 0`) from typed fields and round-trip
35//! it:
36//!
37//! ```
38//! use dvb_ule::{Sndu, TypeField};
39//!
40//! let pdu = [0x45u8, 0x00, 0x00, 0x14]; // start of an IPv4 header
41//! let sndu = Sndu::new(
42//!     TypeField::EtherType(0x0800),
43//!     Some([0x00, 0x01, 0x02, 0x03, 0x04, 0x05]),
44//!     &pdu,
45//! );
46//! let mut buf = vec![0u8; sndu.serialized_len()];
47//! sndu.serialize_into(&mut buf).unwrap();
48//! assert_eq!(Sndu::parse(&buf).unwrap(), sndu);
49//! ```
50#![no_std]
51#![cfg_attr(docsrs, feature(doc_cfg))]
52#![warn(missing_docs)]
53// Runnable examples, embedded so they render on docs.rs and stay in sync with
54// the actual `examples/*.rs` files (shown, not compiled).
55#![doc = "\n## Runnable examples\n"]
56#![doc = "Run with `cargo run -p dvb-ule --example <name>`.\n"]
57#![doc = "\n### `build_sndu`\n\n```rust,ignore"]
58#![doc = include_str!("../examples/build_sndu.rs")]
59#![doc = "```\n\n### `receive_sndu`\n\n```rust,ignore"]
60#![doc = include_str!("../examples/receive_sndu.rs")]
61#![doc = "```"]
62
63extern crate alloc;
64
65mod error;
66mod ext_header;
67mod sndu;
68mod ts;
69mod type_field;
70
71pub use error::{Error, Result};
72pub use ext_header::{
73    ExtensionHeader, MandatoryHType, OptionalHType, PayloadChain, H_TYPE_BRIDGED_FRAME,
74    H_TYPE_EXT_PADDING, H_TYPE_PDU_CONCAT, H_TYPE_TEST_SNDU, H_TYPE_TIMESTAMP, H_TYPE_TS_CONCAT,
75};
76pub use sndu::{
77    is_end_indicator, Sndu, BASE_HEADER_LEN, CRC_LEN, END_INDICATOR, END_INDICATOR_LENGTH, NPA_LEN,
78    PADDING_BYTE,
79};
80pub use ts::{UleReceiver, TS_PAYLOAD_LEN};
81pub use type_field::{TypeField, ETHERTYPE_BOUNDARY, ETHERTYPE_IPV4, ETHERTYPE_IPV6};