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.- dd#
The underlying oxidd manager.
- Type:
oxidd.bdd.BDDManager
- gc()#
Collect dead nodes and return how many were removed.
- range_to_prefixes(lo, hi, bits)#
Split an inclusive range into aligned prefix blocks.
- Parameters:
- 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:
- 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.
- addresses(axis, values)#
Build the BDD that is true when the
axisaddress is any ofvalues.- 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
nodeto 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.
- class fireman.bdd.FormattedTraffic(sources, destinations, services)#
Traffic as text.
- 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.
- classmethod encode(icmp_type, icmp_code)#
Encode an optional ICMP type and code as a port-bit range.
- Parameters:
- 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 | codeas 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"]
- class fireman.bdd.TrafficFormatter(bdd)#
Formats BDD traffic sets as text.
Services come out in ascending protocol order, named where
PROTOCOL_NAMESknows 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 justANY.- addresses(ranges)#
Format IPv4 ranges (
srcordst) 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: