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
Binary (bytes) representation of a UDP packet. |
Functions
|
Create a binary UDP packet from input values. |
|
A generator that reads UDP packets from binary data. |
Module Contents
- class space_packet_parser.generators.udp.UDPPacketBytes
Bases:
bytesBinary (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
- 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:
- Returns:
Resulting binary UDP packet.
- Return type:
- 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