Follow IP Conversations¶
Added in version 1.10.0.
pcapkit.foundation.traceflow.ip.traceflow traces IP conversations from a
series of packets.
- class pcapkit.foundation.traceflow.ip.traceflow.IP[_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,_AT],Buffer[_AT],Index,Packet[_AT]],Generic[_AT]Trace IP conversations.
- 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) – ignored with a warning, as an IP conversation has no application layer of its own to analysetimeout (
float|None) – idle timeout in seconds, on the capture’s clock;Nonekeeps every flow untilfinish()
- Warns:
AttributeWarning– IfanalyzeisTrue.
Note
An IP conversation is the datagrams exchanged between two addresses, of whatever protocol they carry, both directions as one flow by default. A frame belongs to the conversation of its outermost IPv4 or IPv6 header, the one its link carries: in an IP-in-IP tunnel (RFC 2003, RFC 2473, RFC 4213) that is the tunnel’s, where TCP and UDP flows take the innermost header’s endpoints. Every fragment of a datagram carries that header, so each joins the conversation.
IP has no connection to end, so a flow ends at
finish(), 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.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.internet.ip.protocol.IP'>¶
Protocol of current flow tracing object.
Methods
- dump(packet)[source]¶
Dump frame to output files.
- Parameters:
packet (
Packet[_AT]) – a flow packet (trace.ip.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.ip.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.dst}-{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 IP conversations.
- Return type:
- Returns:
Traced IP conversations (trace.ip.index), the open ones first, which have not fired their callbacks;
finish()is what does that.
Data Models
- __init__(*, fout='./tmp', format='pcap', byteorder=sys.byteorder, nanosecond=False, bidirectional=True, analyze=False, timeout=None)[source]¶
Initialise instance.
- Parameters:
fout (
str) – output root path, made if missingformat (
str) – output formatbyteorder (
Literal['little','big']) – output file byte ordernanosecond (
bool) – output nanosecond-resolution file flagbidirectional (
bool) – whether the two halves of a conversation are one flowanalyze (
bool) – ignored with a warning, since an IP conversation has no application layer of its own; trace TCP or UDP for onetimeout (
float|None) – idle timeout in seconds, on the capture’s clock;Nonekeeps every flow untilfinish()
- Warns:
AttributeWarning– IfanalyzeisTrue.
Utilities
Terminology¶
- trace.ip.packet¶
Data structure for IP 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, outermost header dst=ip.dst, # destination IP, outermost header timestamp=float( frame.info.time_epoch), # frame timestamp )
- trace.ip.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 | | |--> ip.dst | | |--> '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,) of the endpoint that | | opened the flow | |--> 'forward': (list) frame index sent by 'origin' | |--> 'reverse': (list) frame index sent to 'origin' |--> (tuple) BUFID ...
BUFIDorders the two addresses canonically when tracing bidirectionally, the default. A flow ends as a UDP one does.- trace.ip.index¶
Data structure for IP 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 |--> (Info) data ...
Data Structures¶
- class pcapkit.foundation.traceflow.ip.data.Packet[_AT: (IPv4Address, IPv6Address)](protocol, index, frame, src, dst, timestamp)[source]¶
-
Data structure for a packet submitted to IP 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.ip.data.BufferID¶
Buffer ID, i.e.
(address, address), a plaintupleasTCP'sis, for the same reason; seeTraceFlow.make_bufid.
- class pcapkit.foundation.traceflow.ip.data.Buffer[_AT: (IPv4Address, IPv6Address)](fpout, index, label, origin, forward, reverse)[source]¶
-
Data structure for an IP 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.
- class pcapkit.foundation.traceflow.ip.data.Index(fpout, index, label, forward, reverse)[source]¶
Bases:
InfoData structure for a traced IP 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.
Type Variables¶
- pcapkit.foundation.traceflow.ip.data._AT: ipaddress.IPv4Address | ipaddress.IPv6Address¶
IP address type.