fireman.bdd#

Binary decision diagrams (BDDs) for traffic sets.

class fireman.bdd.BDD(*, nodes=67108864, cache=1048576, threads=1)#

An oxidd BDD manager with named variables for each traffic axis.

Also encodes integer ranges into BDDs and decodes paths back into ranges.

Each axis gets one variable per bit, named {axis}_{bit} with bit 0 the least significant. The most significant bit is created first so it sits at the top of the diagram, which lets ranges with a common prefix share nodes. All BDDs that are combined must come from the same instance.

nodes#

Capacity for inner nodes.

Type:

int

cache#

Capacity of the apply cache.

Type:

int

threads#

Number of worker threads.

Type:

int

src#

Source address variables, indexed by bit.

Type:

list[oxidd.bdd.BDDFunction]

dst#

Destination address variables, indexed by bit.

Type:

list[oxidd.bdd.BDDFunction]

port#

Port variables, indexed by bit.

Type:

list[oxidd.bdd.BDDFunction]

protocol#

Protocol variables, indexed by bit.

Type:

list[oxidd.bdd.BDDFunction]

dd#

The underlying oxidd manager.

Type:

oxidd.bdd.BDDManager

gc()#

Collect dead nodes and return how many were removed.

property access_functions: dict[str, list[BDDFunction]]#

Variables of each axis, indexed by bit.

property axis_bits: dict[str, int]#

Width of each axis in bits.

range_to_prefixes(lo, hi, bits)#

Split an inclusive range into aligned prefix blocks.

Parameters:
  • lo (int) – First value of the range.

  • hi (int) – Last value of the range.

  • bits (int) – Width of the axis in bits.

Returns:

(start, prefix_len) pairs that together cover exactly [lo, hi].

Raises:

ValueError – If the range is empty or does not fit in bits.

Return type:

list[tuple[int, int]]

cube(*axes)#

Conjunction of every variable of axes, as quantifiers expect.

Parameters:

axes (str) – Axis names, e.g. "src" or "port".

Returns:

The variable set as one BDD.

Return type:

BDDFunction

range(axis, lo, hi)#

Build the BDD for an inclusive range on one axis.

Parameters:
  • axis (str) – Axis name, e.g. "src" or "port".

  • lo (int) – First value of the range.

  • hi (int) – Last value of the range.

Returns:

BDD that is true exactly for values in [lo, hi] on axis.

Return type:

BDDFunction

addresses(axis, values)#

Build the BDD that is true when the axis address is any of values.

Parameters:
  • axis (Literal['src', 'dst']) – "src" or "dst".

  • values (Iterable[IPv4Address | IPv4Network]) – Addresses and networks.

Returns:

The union of the address ranges.

Return type:

BDDFunction

full_range(axis)#

Return the range covering every value of an axis.

Parameters:

axis (str) – Axis name, e.g. "src" or "port".

Returns:

(0, max_value) for the axis width.

Return type:

RangeT

paths(node)#

Yield every path from node to the true end node.

Parameters:

node (BDDFunction) – BDD to walk.

Yields:

One path per route to true. Variables not in a path are free on it.

ranges(axis, path)#

Convert one path into inclusive ranges on one axis.

Parameters:
  • axis (str) – Axis name, e.g. "src" or "port".

  • path (BDDPathT) – One path from paths().

Returns:

(start, end) ranges covering every value of axis on path.

Return type:

list[RangeT]

class fireman.bdd.FormattedTraffic(sources, destinations, services)#

Traffic as text.

sources#

Source networks in CIDR form, e.g. "10.0.0.0/24".

Type:

tuple[str, …]

destinations#

Destination networks in CIDR form, e.g. "192.168.1.0/30".

Type:

tuple[str, …]

services#

Protocol names (numbers if unnamed), optionally with ports or ICMP types and codes, e.g. "tcp", "tcp 80" or "icmp type 3 code 3".

Type:

tuple[str, …]

class fireman.bdd.IcmpCodec#

Encodes ICMP types and codes into port-bit values and formats them back.

CODE_BITS: ClassVar[int]#

ICMP has no ports, so type and code live in the port bits as type << CODE_BITS | code.

CODE_MASK: ClassVar[int]#

Mask for the code part, 255.

classmethod encode(icmp_type, icmp_code)#

Encode an optional ICMP type and code as a port-bit range.

Parameters:
  • icmp_type (int | None) – ICMP type, or None for every type.

  • icmp_code (int | None) – ICMP code, or None for every code of the type.

Returns:

Inclusive port-bit range; the whole port axis when there is no type.

Raises:

ValueError – If a code is given without a type.

Return type:

RangeT

classmethod format(ranges)#

Format port-bit ranges holding ICMP type << CODE_BITS | code as types and codes.

Each range is split into a partial first type, a run of whole types and a partial last type. Whole types read "type 4" or "type 4-8", partial ones "type 3 code 3" or "type 3 code 2-255".

Examples

  • [(2048, 2303)] -> ["type 8"]

  • [(771, 771)] -> ["type 3 code 3"]

  • [(768, 1023)] -> ["type 3"]

  • [(770, 1024)] -> ["type 3 code 2-255", "type 4 code 0"]

  • [(0, 65535)] -> ["type 0-255"]

  • [(770, 2303)] -> ["type 3 code 2-255", "type 4-8"]

Parameters:

ranges (Iterable[RangeT]) – Inclusive (start, end) ranges of port-bit values.

Returns:

One string per whole-type run or partial type, in order.

Return type:

list[str]

class fireman.bdd.TrafficFormatter(bdd)#

Formats BDD traffic sets as text.

Services come out in ascending protocol order, named where PROTOCOL_NAMES knows the number. A protocol that matches every port is shown alone ("tcp", "gre"), and neighbouring ones that are neither ICMP nor port-based collapse into numeric ranges like "50-51". Otherwise ICMP reads "icmp type 8 code 0" and port protocols "tcp 80" or "tcp 80-443". If everything matches, the services are just ANY.

bdd#

Manager the formatted nodes and paths come from.

Type:

BDD

addresses(ranges)#

Format IPv4 ranges (src or dst) as the fewest CIDR networks.

set(node)#

Format a whole BDD set as a short list of rows.

Shared BDD nodes split one set into many paths, so paths are merged: ranges are joined, paths with the same sources and destinations become one row, then rows with the same services are merged (see _merge_rows()).

Parameters:

node (BDDFunction) – BDD of the traffic set.

Returns:

One row per distinct source and destination group, sorted. Empty if the set is empty.

Return type:

list[FormattedTraffic]