A library to get unique smallest routable entries from a set of IP prefixes.
Project description
This library is inspired by an issue opened by Thomas Mangin on the repository of [InvalidRoutesReporter](https://github.com/pierky/invalidroutesreporter): https://github.com/pierky/invalidroutesreporter/issues/1
Given a set (or stream) of IP prefixes, this library calculates the unique smallest routable entries (SREs) covered by them.
De-duplication of overlapping prefixes is performed. SREs are calculated on the basis of a target prefix length that can be set on input.
Prefixes can be added and removed as they come (for example from a BGP session) and the resultant set of unique SREs is computed accordingly.
Example:
` >>> from pierky.usres_monitor import UniqueSmallestRoutableEntriesMonitor >>> monitor = UniqueSmallestRoutableEntriesMonitor(target_prefix_len4=24) >>> monitor.add_net("192.168.0.0/16") >>> ["first: {first_ip}, last: {last_ip}, cnt: {cnt}".format(**prefix) for prefix in monitor.get_prefixes(4)] ['first: 192.168.0.0, last: 192.168.255.0, cnt: 256'] >>> monitor.get_count(4) 256 `
So, 192.168.0.0/16 contains 256 /24 subnets.
Now, add a prefix that is already covered by the previous one:
` >>> monitor.add_net("192.168.0.0/21") >>> ["first: {first_ip}, last: {last_ip}, cnt: {cnt}".format(**prefix) for prefix in monitor.get_prefixes(4)] ['first: 192.168.0.0, last: 192.168.255.0, cnt: 256'] `
Nothing changed. Add a prefix that covers the 2 already added before:
` >>> monitor.add_net("192.0.0.0/8") >>> ["first: {first_ip}, last: {last_ip}, cnt: {cnt}".format(**prefix) for prefix in monitor.get_prefixes(4)] ['first: 192.0.0.0, last: 192.255.255.0, cnt: 65536'] `
Here it is, 192.0.0.0/8 covers both 192.168.0.0/16 and 192.168.0.0/21 and contains 65536 /24 subnets.
Now remove the two larger prefixes:
` >>> monitor.del_net("192.0.0.0/8") >>> monitor.del_net("192.168.0.0/16") >>> ["first: {first_ip}, last: {last_ip}, cnt: {cnt}".format(**prefix) for prefix in monitor.get_prefixes(4)] ['first: 192.168.0.0, last: 192.168.7.0, cnt: 8'] `
Only 192.168.0.0/21 remains, with its 8 /24 subnets.
Add now a second prefix:
` >>> monitor.add_net("192.168.8.0/21") >>> ["first: {first_ip}, last: {last_ip}, cnt: {cnt}".format(**prefix) for prefix in monitor.get_prefixes(4)] ['first: 192.168.0.0, last: 192.168.7.0, cnt: 8', 'first: 192.168.8.0, last: 192.168.15.0, cnt: 8'] >>> monitor.get_count(4) 16 `
Two prefixes are printed, each one covering 8 SREs, for a total of 16 SREs.
Both IPv4 and IPv6 can be used, also simultaneously with the same monitor object:
` >>> from pierky.usres_monitor import UniqueSmallestRoutableEntriesMonitor >>> monitor = UniqueSmallestRoutableEntriesMonitor(target_prefix_len4=24, target_prefix_len6=56) >>> monitor.add_net("192.168.0.0/16") >>> monitor.add_net("10.0.0.0/8") >>> monitor.add_net("2001:db8:aaaa::/48") >>> ["first: {first_ip}, last: {last_ip}, cnt: {cnt}".format(**prefix) for prefix in monitor.get_prefixes(4)] ['first: 192.168.0.0, last: 192.168.255.0, cnt: 256', 'first: 10.0.0.0, last: 10.255.255.0, cnt: 65536'] >>> ["first: {first_ip}, last: {last_ip}, cnt: {cnt}".format(**prefix) for prefix in monitor.get_prefixes(6)] ['first: 2001:db8:aaaa::, last: 2001:db8:aaaa:ff00::, cnt: 256'] >>> monitor.get_count(4) 65792 >>> monitor.get_count(6) 256 `
Installation
` pip install usresmonitor `
Optionally, the [apsw](https://github.com/rogerbinns/apsw) SQLite library can be installed; in that case, it will be preferred during the setup of the backend database used by USREsMonitor.
Future work
Add some examples of how this library can be used (ExaBGP integration).
Status
First-release, looking for testers and reviewers.
Bug? Issues?
But also suggestions? New ideas?
Please create an issue on GitHub or drop me a message.
Change log
v0.1.1
Fix: better exception handling of duplicate prefixes
v0.1.0
First release.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file usresmonitor-0.1.1.tar.gz
.
File metadata
- Download URL: usresmonitor-0.1.1.tar.gz
- Upload date:
- Size: 21.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c6159380c687b9cd69fc31b9552ea36f0c0207370f07601ebafd25adcb75461f |
|
MD5 | 5a08b82dc7b17e3f92981e58457f627e |
|
BLAKE2b-256 | c31dcefd3bf3b974aecb7de076a754db8e5241c2d48b7bbb21a536a2865034d1 |