UDP - User Datagram Protocol

pcapkit.protocols.transport.udp contains UDP only, which implements the extractor for User Datagram Protocol (UDP) [*], whose structure is described as below:

Octets

Bits

Name

Description

0

0

udp.srcport

Source Port

2

16

udp.dstport

Destination Port

4

32

udp.len

Length (header included)

6

48

udp.checksum

Checksum

class pcapkit.protocols.transport.udp.UDP(file=None, length=None, **kwargs)[source]

Bases: Transport[UDP, UDP]

This class implements User Datagram Protocol.

This class supports parsing of the following protocols, which are registered in the self.__proto__ attribute:

Note

The HTTP ports resolve to pcapkit.protocols.application.http.HTTP, which identifies the version from the payload and delegates. TCP.__proto__ binds the same class for its HTTP ports.

Changed in version 1.5.0: make() takes total_length, which a from_data() rebuild passes so that a truncated capture keeps its declared Length (#1155), and trailer, so that the octets past the Length field rebuild as captured (#1461).

property name: Literal['User Datagram Protocol']

Name of current protocol.

property length: Literal[8]

Header length of current protocol.

property src: AppType

Source port.

property dst: AppType

Destination port.

read(length=None, **kwargs)[source]

Read User Datagram Protocol (UDP).

Structure of UDP header [RFC 768]:

 0      7 8     15 16    23 24    31
+--------+--------+--------+--------+
|     Source      |   Destination   |
|      Port       |      Port       |
+--------+--------+--------+--------+
|                 |                 |
|     Length      |    Checksum     |
+--------+--------+--------+--------+
|
|          data octets ...
+---------------- ...
Parameters:
  • length (int | None) – Length of packet data.

  • **kwargs (Any) – Arbitrary keyword arguments.

Return type:

UDP

Returns:

Parsed packet data.

make(srcport=0, dstport=0, checksum=b'\x00\x00', total_length=None, payload=b'', trailer=b'', **kwargs)[source]

Make (construct) packet data.

Parameters:
  • srcport (AppType | int) – Source port.

  • dstport (AppType | int) – Destination port.

  • checksum (bytes) – Checksum.

  • total_length (int | None) – Length of the datagram, header included; computed from the header and the payload when omitted.

  • payload (bytes | Schema | ProtocolBase) – Payload data.

  • trailer (bytes) – Octets after the datagram, outside the Length field; written as is and not counted in a computed total_length.

  • **kwargs (Any) – Arbitrary keyword arguments.

Return type:

UDP

Returns:

Constructed packet data.

Changed in version 1.5.0: An out-of-range srcport or dstport raises ValueError rather than being accepted (#758). total_length sits before payload (#1155), so a caller passing payload positionally sets total_length instead.

classmethod _make_data(data)[source]

Create key-value pairs from data for protocol construction.

Parameters:

data (UDP) – protocol data

Return type:

dict[str, Any]

Returns:

Key-value pairs for protocol construction.

__proto__: DefaultDict[int, ModuleDescriptor[ProtocolBase] | Type[ProtocolBase]]

Protocol index mapping for decoding next layer, see self._decode_next_layer & self._import_next_layer.

classmethod __index__()[source]

Numeral registry index of the protocol.

Return type:

TransType

Returns:

Numeral registry index of the protocol in IANA.

Header Schemas

class pcapkit.protocols.schema.transport.udp.UDP(srcport=NO_VALUE, dstport=NO_VALUE, len=NO_VALUE, checksum=NO_VALUE, payload=NO_VALUE, trailer=NO_VALUE, *, __packet__=None, **kwargs)[source]

Bases: Schema

Header schema for UDP packet.

srcport: AppType = <PortEnumField srcport>

Source port.

dstport: AppType = <PortEnumField dstport>

Destination port.

len: int = <UInt16Field len>

Length of UDP packet.

checksum: bytes = <BytesField checksum>

Checksum of UDP packet.

payload: bytes = <PayloadField payload>

Payload. A Length below the header’s own 8 octets leaves the length negative, which takes the rest of the datagram.

trailer: bytes = <PayloadField trailer>

Octets captured past the Length field, see IPv4.trailer.

Data Models

class pcapkit.protocols.data.transport.udp.UDP(srcport, dstport, len, checksum)[source]

Bases: Protocol

Data model for UDP protocol.

srcport: AppType

Source port.

dstport: AppType

Destination port.

len: int

Length, header included.

checksum: bytes

Checksum.

trailer: bytes

Octets captured past the Length field, present only when there are any.

Footnotes