Follow UDP Streams

Added in version 1.10.0.

pcapkit.foundation.traceflow.udp.traceflow traces UDP flows from a series of packets.

class pcapkit.foundation.traceflow.udp.traceflow.UDP[_DT, _BT: Info, _IT: Info, _PT: Info](*, fout='./tmp', format='pcap', byteorder=sys.byteorder, nanosecond=False, bidirectional=True, analyze=False, timeout=None)[source]

Bases: TraceFlowBase[tuple[_AT, int, _AT, int], Buffer[_AT], Index, Packet[_AT]], Generic[_AT]

Trace UDP flows.

Parameters:
  • fout (str) – output path

  • format (str) – output format

  • byteorder (Literal['little', 'big']) – output file byte order

  • nanosecond (bool) – output nanosecond-resolution file flag

  • bidirectional (bool) – trace both halves of a conversation as one flow

  • analyze (bool) – parse each flow’s application layer, so that Index.packet can be read

  • timeout (float | None) – idle timeout in seconds, on the capture’s clock; None keeps every flow until finish()

Note

A UDP flow is the datagrams exchanged between two (address, port) endpoints, both directions as one flow by default, as TCP’s is. UDP has no connection (RFC 768), so nothing a datagram carries can end its flow: a flow ends at finish(), the end of the capture, or, given a timeout, once that many seconds of the capture’s clock pass without one of its datagrams (advance()), as an IPFIX idle timeout ends one. A datagram on the same endpoints after that opens a new flow.

Note

With analyze=True a flow also carries its application layer: Index.packet holds each datagram’s payload, parsed on its first read by the ports, as a frame’s UDP layer dispatches it. A datagram is a whole message, so there is nothing to reassemble; one cut short by the snapshot length, or the first fragment of an IP datagram, is parsed as captured.

Added in version 1.10.0.

property name: str

Protocol name of current flow tracing object.

Note

The same value is also available as a class attribute, from the property of the same name on TraceFlowMeta.

property protocol: Type[pcapkit.protocols.protocol.ProtocolBase]

Protocol of current flow tracing object.

Note

The same value is also available as a class attribute, from the property of the same name on TraceFlowMeta.

__callback_fn__: list[Callable[[_IT], None]]

List of callback functions upon a finalised flow.

Defaults

__protocol_name__: str = 'UDP'

Protocol name of current flow tracing object.

__protocol_type__: Type[ProtocolBase] = <class 'pcapkit.protocols.transport.udp.protocol.UDP'>

Protocol of current flow tracing object.

Methods

dump(packet)[source]

Dump frame to output files.

Parameters:

packet (Packet[_AT]) – a flow packet (trace.udp.packet)

The flow’s output file is kept open for its next frame, as _write() describes.

trace(packet, *, output=False)[source]

Trace packets.

Overloads:
Parameters:
Returns:

If output is True, returns the initiated Dumper object, which will dump data to the output file named after the flow label; otherwise, returns the flow label itself.

Notes

The flow label is formatted as following, as _make_label() builds it from the packet that opened the flow:

f'{packet.src}_{packet.srcport}-{packet.dst}_{packet.dstport}-{packet.timestamp}'
finish()[source]

Finalise every flow still being traced.

Idempotent: it drains the buffer, so a second call finds nothing to do. It also closes every output file still kept open, as TraceFlow.finish does, even when a callback raises: the flow it was handed is finalised, and the flows after it stay in the buffer, unfinalised, for a later call.

submit()[source]

Submit traced UDP flows.

Return type:

tuple[Index, ...]

Returns:

Traced UDP flows (trace.udp.index), the open ones first, which have not fired their callbacks; finish() is what does that.

Utilities

_make_index(buffer)[source]

Report a flow’s buffer as its index entry.

Parameters:

buffer (Buffer[_AT]) – a flow buffer (trace.udp.buffer)

Return type:

Index

Returns:

The flow’s index entry, its payloads, if kept, in a Deferred parsing them with self._parse_keywords.

Terminology

trace.udp.packet

Data structure for UDP flow tracing (TraceFlow.dump) is as follows:

trace_dict = dict(
    protocol=data_link,                     # data link type from global header
    index=frame.info.number,                # frame number
    frame=frame.info,                       # extracted frame info
    src=ip.src,                             # source IP
    dst=ip.dst,                             # destination IP
    srcport=udp.srcport,                    # UDP source port
    dstport=udp.dstport,                    # UDP destination port
    timestamp=float(
        frame.info.time_epoch),             # frame timestamp
    payload=bytes(udp.packet.payload),      # raw bytes type payload
)
trace.udp.buffer

Data structure for internal buffering when performing flow tracing algorithms (TraceFlow._buffer) is as follows:

(dict) buffer --> memory buffer for flow tracing
 |--> (tuple) BUFID : (dict)
 |       |--> ip.src      |
 |       |--> udp.srcport |
 |       |--> ip.dst      |
 |       |--> udp.dstport |
 |                        |--> 'fpout' : (dictdumper.dumper.Dumper) output dumper object
 |                        |--> 'index': (list) list of frame index, both directions
 |                        |              |--> (int) frame index
 |                        |--> 'label': (str) flow label generated from the packet
 |                        |                   that opened the flow
 |                        |--> 'origin': (tuple) (address, port) of the endpoint
 |                        |                      that opened the flow
 |                        |--> 'forward': (list) frame index sent by 'origin'
 |                        |--> 'reverse': (list) frame index sent to 'origin'
 |                        |--> 'payload': (Optional[list]) each datagram's port pair
 |                                        and payload, or None when analyze is off
 |--> (tuple) BUFID ...

BUFID orders the endpoints canonically when tracing bidirectionally, the default, as TCP’s does. Nothing a datagram carries ends its flow: TraceFlow.finish finalises it at the end of the capture, or TraceFlow.advance once it has been idle for the tracer’s timeout, after which a datagram on the same endpoints opens a new flow.

trace.udp.index

Data structure for UDP flow tracing (element from TraceFlow.index tuple) is as follows:

(tuple) index
 |--> (Info) data
 |     |--> 'fpout' : (Optional[str]) output filename, or None when
 |     |                              the format writes no file
 |     |--> 'index': (tuple) tuple of frame index, both directions,
 |     |                     in capture order
 |     |              |--> (int) frame index
 |     |--> 'label': (str) flow label generated from the packet that
 |     |                   opened the flow
 |     |--> 'forward': (tuple) frame index in the direction that
 |     |                       opened the flow
 |     |--> 'reverse': (tuple) frame index the other way; empty when
 |     |                      tracing unidirectionally
 |     |--> 'packet': (Optional[tuple]) each datagram's application
 |                    layer, one per frame of 'index', or None when
 |                    analyze is off
 |--> (Info) data ...

packet is parsed on its first read, each payload by its ports.

Data Structures

class pcapkit.foundation.traceflow.udp.data.Packet[_AT: (IPv4Address, IPv6Address)](protocol, index, frame, src, dst, srcport, dstport, timestamp, payload)[source]

Bases: Info, Generic[_AT]

Data structure for a packet submitted to UDP flow tracing.

protocol: LinkType

Data link type – the PCAP global header’s, or that of the PCAP-NG interface the packet was captured on.

index: int

Frame number.

frame: Frame | FrameRecord | dict[str, Any]

Extracted frame info – the default engine’s frame, a third-party engine’s FrameRecord, or the dict PyShark’s adapter reports, which carries no frame record.

src: _AT

Source IP.

dst: _AT

Destination IP.

srcport: int

UDP source port.

dstport: int

UDP destination port.

timestamp: float

Frame timestamp.

payload: bytes

Raw bytes type UDP payload, i.e. the application-layer octets the datagram carries, as captured.

pcapkit.foundation.traceflow.udp.data.BufferID

Buffer ID, i.e. (address, port, address, port), a plain tuple as TCP's is, for the same reason; see TraceFlow.make_bufid.

alias of tuple[_AT, int, _AT, int]

class pcapkit.foundation.traceflow.udp.data.Buffer[_AT: (IPv4Address, IPv6Address)](fpout, index, label, origin, forward, reverse, payload)[source]

Bases: Info, Generic[_AT]

Data structure for a UDP flow tracing buffer.

fpout: dictdumper.dumper.Dumper

Output dumper object.

index: list[int]

List of frame index, both directions, in capture order. This is the authoritative ordering; forward and reverse are subsequences of it.

label: str

Flow label, built from the packet that opened the flow, cf. TraceFlow._make_label.

origin: tuple[_AT, int]

(address, port) of the endpoint whose packet opened this flow, which defines what “forward” means for it and which the label names first.

forward: list[int]

List of frame index sent by origin, in capture order.

reverse: list[int]

List of frame index sent to origin, in capture order. Always empty when tracing unidirectionally.

payload: list[tuple[tuple[int, int], bytes]] | None

Each datagram’s (source, destination) port pair and payload, in capture order, or None when the tracer was not asked to analyse the application layer.

class pcapkit.foundation.traceflow.udp.data.Index(fpout, index, label, forward, reverse, packet)[source]

Bases: DeferredPacket, Info

Data structure for a traced UDP flow, as an index entry.

fpout: str | None

Path of the flow’s output file, or None when the output format writes no file. Open and finalised flows report the same path.

index: tuple[int, ...]

Tuple of frame index, both directions, in capture order.

label: str

Flow label, built from the packet that opened the flow.

forward: tuple[int, ...]

Frame index of the packets travelling in the direction that opened the flow, in capture order.

reverse: tuple[int, ...]

Frame index of the packets travelling the other way, in capture order. Empty when tracing unidirectionally.

packet: tuple[ProtocolBase, ...] | None

Each datagram’s payload parsed as its application layer, one per frame of index and in its order, or None when the tracer was not asked for it (analyze=False, the default). Parsed on the first read.

class pcapkit.foundation.traceflow.udp.data.Deferred(analyze, datagrams)[source]

Bases: object

A postponed parse of a traced UDP flow’s application layer.

A UDP datagram is a whole message (RFC 768), so a flow’s application layer is its datagrams’ payloads, each parsed alone, and nothing is reassembled. This holds them until somebody reads Index.packet.

Parameters:
  • analyze (Callable[[tuple[int, int], bytes], ProtocolBase]) – The parse of one payload, called with its (source, destination) port pair and its octets: the tracer’s __protocol_type__’s analyze, bound to the stops and context the extraction’s engine parses its frames with.

  • datagrams (tuple[tuple[tuple[int, int], bytes], ...]) – Each datagram’s port pair and payload, in capture order.

__call__()[source]

Run the postponed parse.

Return type:

tuple[ProtocolBase, ...]

Returns:

Each datagram’s payload parsed as its application layer, looked up by its ports, in capture order.

Type Variables

pcapkit.foundation.traceflow.udp.data._AT: ipaddress.IPv4Address | ipaddress.IPv6Address

IP address type.