space_packet_parser.generators.udp

UDP packet generator.

This module provides functionality for parsing UDP (User Datagram Protocol) packets from binary data sources. UDP is a connectionless transport protocol defined in RFC 768.

Classes

UDPPacketBytesbytes

Binary representation of a UDP packet with header field accessors.

Functions

create_udp_packetUDPPacketBytes

Factory function to create UDP packets from components.

udp_generatorIterator[UDPPacketBytes]

Generator that yields UDP packets from binary data sources.

Classes

UDPPacketBytes

Binary (bytes) representation of a UDP packet.

Functions

create_udp_packet(→ UDPPacketBytes)

Create a binary UDP packet from input values.

udp_generator(→ collections.abc.Iterator[UDPPacketBytes])

A generator that reads UDP packets from binary data.

Module Contents

class space_packet_parser.generators.udp.UDPPacketBytes

Bases: bytes

Binary (bytes) representation of a UDP packet.

Methods to extract the UDP header fields are added to the raw bytes object. This class follows the same pattern as CCSDSPacketBytes.

UDP Packet Structure (RFC 768): - Source Port: 16 bits (0-65535) - Destination Port: 16 bits (0-65535) - Length: 16 bits (total length in bytes including 8-byte header) - Checksum: 16 bits (optional error checking) - Data: Variable length payload

HEADER_LENGTH_BYTES = 8
__str__() str

Return a human-readable string representation of the UDP packet header.

property source_port: int

UDP source port (16 bits, bytes 0-1).

property dest_port: int

UDP destination port (16 bits, bytes 2-3).

property length: int

UDP packet length in bytes, including 8-byte header (16 bits, bytes 4-5).

property checksum: int

UDP checksum (16 bits, bytes 6-7).

property header_values: tuple[int, Ellipsis]

Convenience property for tuple of header values.

Returns:

Tuple containing (source_port, dest_port, length, checksum).

Return type:

tuple[int, …]

property header: bytes

Convenience property returns the UDP header bytes (first 8 bytes).

Returns:

The 8-byte UDP header.

Return type:

bytes

property data: bytes

Convenience property returns only the UDP payload data (no header).

Returns:

The UDP payload data without the header.

Return type:

bytes

space_packet_parser.generators.udp.create_udp_packet(data: bytes = b'', *, source_port: int = 0, dest_port: int = 0, checksum: int = 0) UDPPacketBytes

Create a binary UDP packet from input values.

Pack the header fields into the proper bit locations and append the data bytes.

Parameters:
  • data (bytes) – Payload data bytes.

  • source_port (int) – UDP source port (16 bits, 0-65535).

  • dest_port (int) – UDP destination port (16 bits, 0-65535).

  • checksum (int) – UDP checksum (16 bits, 0-65535). Use 0 if not computed.

Returns:

Resulting binary UDP packet.

Return type:

UDPPacketBytes

Raises:

ValueError – If any field value is out of range or if the total packet length exceeds 65535 bytes.

Notes

This function is useful for generating test UDP packets for debugging or mocking purposes. The length field is automatically computed as 8 + len(data).

space_packet_parser.generators.udp.udp_generator(binary_data: BinaryIO | socket.socket | bytes, *, buffer_read_size_bytes: int | None = None, show_progress: bool = False) collections.abc.Iterator[UDPPacketBytes]

A generator that reads UDP packets from binary data.

Each iteration yields a UDPPacketBytes object representing a single UDP packet. The generator reads the UDP length field to determine packet boundaries.

Parameters:
  • binary_data (Union[BinaryIO, socket.socket, bytes]) – Binary data source containing UDP packets. Can be a file-like object, socket, or bytes.

  • buffer_read_size_bytes (int, optional) – Number of bytes to read from e.g. a BufferedReader or socket binary data source on each read attempt. If None, defaults to 4096 bytes from a socket, -1 (full read) from a file.

  • show_progress (bool) – Default False. If True, prints a status bar. Note that for socket sources, the percentage will be zero until the generator ends.

Yields:

UDPPacketBytes – The bytes of a single UDP packet.

Notes

This generator assumes: - Binary data contains back-to-back UDP packets with no additional framing - Each packet has a valid length field - No error checking or recovery from malformed packets