Skip to main content

CIDR-Bottle is yet another Patricia Trie implmentation, however this one is specifically designed for parsing and validating CIDRS in routing tables and ROAs.

Project description

CIDR-Bottle

Release Badge Pipeline Badge

CIDR-Bottle is yet another implementation of a Patricia Trie for handling network routing information (such as ROAs & Routing Tables) for reconciliation. However, unlike other implementations it supports both sub-tree checking and longest-prefix matching.

An attractive screenshot of the example code below

CIDR-Bottle was designed specifically to be used for reconciling RIR Allocation & Assignment records with actual BGP Announcements. It isn't designed to be the fastest (it's written in pure python), but it should be the most full-featured implementation. That said, unless you're writing a routing engine in python (in which case I'd be really interested to know why), speed shouldn't be a significant issue.

Usage

Initialisation

By default, a CIDR-Bottle is in IPv4 mode, to use IPv6 mode you must supply an IPv6 CIDR.

The root Bottle does not need to be the entire IP space, it can be any subnet.

from cidr_bottle import Bottle
from ipaddress import IPv4Network, IPv6Network

## Defaults to IPv4
root = Bottle()  # 0.0.0.0/0

## IPv6 mode is initialised by passing an IPv6 CIDR (either as a str or an instance of ipaddress.IPv6Network) 
root6 = Bottle("::/0")  # ::/0

## Supports detached (not starting at either 0.0.0.0/0 or ::/0) roots
detached_root = Bottle("198.51.100.0/24")

Racking a Bottle (Inserting a node)

## Supports insert with str
root.insert("198.51.100.0/24")

## Supports insert with instances of ipaddress.IPv4Network
root.insert(IPv4Network("198.51.100.0/24"))

## Supports insert with instances of ipaddress.IPv6Network
root.insert(IPv6Network("2001:db8::/48"))

## Supports attaching any json serializable objects to nodes **This is important for future planned releases**
root.insert("198.51.100.0/24", {"example": "dict"})
root.insert("198.51.100.0/24", "string example")

## Supports dict-style indexing
root["198.51.100.0/24"] = "string example"

Contains CIDR?

Returns True where there is a covering prefix, otherwise false. NOTE: This means that it returns true 100% of the time when the root is 0.0.0.0/0 or ::/0

if "198.51.100.0/24" in root:
    ## do something
### or
if root.contains("198.51.100.0/24"):
    ## do something

You can enforce exact matches by passing exact=True to the contains method.

if not root.contains("198.51.100.128/25", exact=True):
    ## do something

Drinking a Bottle (Get node)

This will return a matching covering prefix if present. In the case of a detached root, this means that it can return None if no such covering prefix exists. NOTE: This is longest prefix matching

print(root["198.51.100.0/24"])
### or
print(root.get("198.51.100.0/24"))

Similar to the .contains(...) method, you can enforce exact matches by passing exact=True to the get method. This will raise a KeyError if there is no exact match.

print(root.get("198.51.100.128/25"), exact=True)  # will raise a KeyError("no exact match found")

Children / Sub-Tree checking

With CIDR-Bottle you can retrieve all the defined children of a bottle(node).

root.insert("198.51.100.0/25")
root.insert("198.51.100.128/25")
print(root["198.51.100.0/24"].children())

Smashing bottles (Deleting Nodes)

Deleting an edge node removes it completely.

Deleting an intermediate node, converts it into a "passing" node, and does not affect any descendants of that node.

del root["198.51.100.0/24"]
### or
root.delete("198.51.100.0/24")

Installation (from pip):

pip install cidr_bottle

Installation (from source):

git clone https://gitlab.com/geoip.network/cidr_bottle
poetry install

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

CIDR-Bottle-1.0.0a0.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

CIDR_Bottle-1.0.0a0-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file CIDR-Bottle-1.0.0a0.tar.gz.

File metadata

  • Download URL: CIDR-Bottle-1.0.0a0.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.9.12 Linux/5.4.109+

File hashes

Hashes for CIDR-Bottle-1.0.0a0.tar.gz
Algorithm Hash digest
SHA256 93aaa94ac4286ddd18b61aa6529b4db0af8ccc6aabf1541220c57aa70ec3b342
MD5 4f87436b6626ac499ab52c5d784c9980
BLAKE2b-256 41b4befd8c9660dc762b1b36fb92d222810377a7a29a056a29be2c62681964f5

See more details on using hashes here.

File details

Details for the file CIDR_Bottle-1.0.0a0-py3-none-any.whl.

File metadata

  • Download URL: CIDR_Bottle-1.0.0a0-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.9.12 Linux/5.4.109+

File hashes

Hashes for CIDR_Bottle-1.0.0a0-py3-none-any.whl
Algorithm Hash digest
SHA256 21d7d6ae7d86db512d11be2772ce8381fa7fd1103acd9bba48282aff8a4b9f46
MD5 afebb918a3e5a307c2ddfa72a1415639
BLAKE2b-256 354911c70b6966e594f5a2d1b7e15c80da60f50240229b97a487b44f9794d7a2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page