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 pathformat (
str) – output formatbyteorder (
Literal['little','big']) – output file byte ordernanosecond (
bool) – output nanosecond-resolution file flagbidirectional (
bool) – trace both halves of a conversation as one flowanalyze (
bool) – parse each flow’s application layer, so thatIndex.packetcan be readtimeout (
float|None) – idle timeout in seconds, on the capture’s clock;Nonekeeps every flow untilfinish()
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 atfinish(), the end of the capture, or, given atimeout, 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=Truea flow also carries its application layer:Index.packetholds 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.
Defaults
- __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:
packet (
Packet[_AT]) – a flow packet (trace.udp.packet)output (
bool) – flag if has formatted dumper
- Returns:
If
outputisTrue, returns the initiatedDumperobject, 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.finishdoes, 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:
- 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:
- Returns:
The flow’s index entry, its payloads, if kept, in a
Deferredparsing them withself._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 ...
BUFIDorders the endpoints canonically when tracing bidirectionally, the default, as TCP’s does. Nothing a datagram carries ends its flow:TraceFlow.finishfinalises it at the end of the capture, orTraceFlow.advanceonce it has been idle for the tracer’stimeout, after which a datagram on the same endpoints opens a new flow.- trace.udp.index¶
Data structure for UDP flow tracing (element from
TraceFlow.indextuple) 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 ...
packetis 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]¶
-
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.
- frame: Frame | FrameRecord | dict[str, Any]¶
Extracted frame info – the default engine’s frame, a third-party engine’s
FrameRecord, or thedictPyShark’s adapter reports, which carries no frame record.
- pcapkit.foundation.traceflow.udp.data.BufferID¶
Buffer ID, i.e.
(address, port, address, port), a plaintupleasTCP'sis, for the same reason; seeTraceFlow.make_bufid.
- class pcapkit.foundation.traceflow.udp.data.Buffer[_AT: (IPv4Address, IPv6Address)](fpout, index, label, origin, forward, reverse, payload)[source]¶
-
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;
forwardandreverseare 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 thelabelnames first.
- class pcapkit.foundation.traceflow.udp.data.Index(fpout, index, label, forward, reverse, packet)[source]¶
Bases:
DeferredPacket,InfoData structure for a traced UDP flow, as an index entry.
See also
element from
pcapkit.foundation.traceflow.traceflow.TraceFlow.indextuple
- fpout: str | None¶
Path of the flow’s output file, or
Nonewhen the output format writes no file. Open and finalised flows report the same path.
- forward: tuple[int, ...]¶
Frame index of the packets travelling in the direction that opened the flow, in capture order.
- class pcapkit.foundation.traceflow.udp.data.Deferred(analyze, datagrams)[source]¶
Bases:
objectA 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__’sanalyze, 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.
Type Variables¶
- pcapkit.foundation.traceflow.udp.data._AT: ipaddress.IPv4Address | ipaddress.IPv6Address¶
IP address type.