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 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) – ignored with a warning, as an IP conversation has no application layer of its own to analyse

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

Warns:

AttributeWarning – If analyze is True.

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 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.

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 = 'IP'

Protocol name of current flow tracing object.

__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:
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.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.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 IP conversations.

Return type:

tuple[Index, ...]

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 missing

  • format (str) – output format

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

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

  • bidirectional (bool) – whether the two halves of a conversation are one flow

  • analyze (bool) – ignored with a warning, since an IP conversation has no application layer of its own; trace TCP or UDP for one

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

Warns:

AttributeWarning – If analyze is True.

Utilities

_endpoints(packet)[source]

The endpoints of a flow packet: (address,) of its source, then of its destination.

Parameters:

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

Return type:

tuple[tuple[Any, ...], tuple[Any, ...]]

Returns:

The addresses alone, since IP has no ports.

_make_index(buffer)[source]

Report a flow’s buffer as its index entry.

Parameters:

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

Return type:

Index

Returns:

The flow’s index entry.

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 ...

BUFID orders 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.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
 |--> (Info) data ...

Data Structures

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

Bases: Info, Generic[_AT]

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.

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.

timestamp: float

Frame timestamp.

pcapkit.foundation.traceflow.ip.data.BufferID

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

alias of tuple[_AT, _AT]

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

Bases: Info, Generic[_AT]

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; 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]

(address,) 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.

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

Bases: Info

Data structure for a traced IP 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.

Type Variables

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

IP address type.