Skip to main content

Fast RFC-strict IPv4/IPv6 address arithmetic, CIDR manipulation, bulk prefix operations, and LPM indexing

Project description

libcidr

libcidr is a C11 library and CPython extension for strict IPv4/IPv6 address arithmetic, CIDR manipulation, bulk prefix operations, longest-prefix-match indexing, and IANA special-purpose address classification. It does not do routing protocol logic, network I/O, DNS resolution, or operational routability inference beyond the documented classification flags. Designed for Python users hitting pure-Python performance limits; see bench/BASELINES.md for measured comparisons against ipaddress, netaddr, and pytricia.

Build Requirements

  • libc
  • Clang or GCC
  • CPython 3.11 or newer for the Python binding

Build And Install

C Library

make
make install

This builds libcidr.a and installs:

  • libcidr.a into $(PREFIX)/lib
  • libcidr.h into $(PREFIX)/include

Python Binding

If installing from PyPI:

pip install libcidr

If building from source:

make python-ext
make python-install

Minimal C Example

#include <stdio.h>

#include <libcidr.h>

int
main(void)
{
	cidr_addr_t addr;
	cidr_prefix_t prefix;
	cidr_class_t cls;
	bool contains;
	cidr_err_t rc;

	rc = cidr_addr_parse("192.168.1.10", &addr);
	if (rc != CIDR_OK)
		return 1;

	rc = cidr_addr_classify(&addr, &cls);
	if (rc != CIDR_OK)
		return 1;

	rc = cidr_prefix_parse("192.168.1.0/24", &prefix);
	if (rc != CIDR_OK)
		return 1;

	rc = cidr_prefix_contains(&prefix, &addr, &contains);
	if (rc != CIDR_OK)
		return 1;

	printf("private=%s contains=%s\n",
	       (cls & CIDR_CLASS_PRIVATE) ? "yes" : "no",
	       contains ? "yes" : "no");
	return 0;
}

Build example:

cc -std=c11 -I/usr/local/include example.c -L/usr/local/lib -lcidr -o example
./example
# output: private=yes contains=yes

Minimal Python Example

import libcidr

addrs = [
    libcidr.IPv4Address("10.1.2.3"),
    libcidr.IPv4Address("203.0.113.10"),
]
prefixes = [
    libcidr.IPv4Network("10.0.0.0/8"),
    libcidr.IPv4Network("192.168.0.0/16"),
]

print(libcidr.bulk_contains(addrs, prefixes))
# [0, -1]

Known Limitations

  • No network I/O
  • No DNS resolution
  • CIDR_CLASS_GLOBAL means no special-purpose block matched; it does not imply real-world routability

Design Rationale

See ARCHITECTURE.md for the full design rationale and internal specification.

License

ISC License. See LICENSE.

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

libcidr-1.0.0.tar.gz (67.5 kB view details)

Uploaded Source

Built Distributions

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

libcidr-1.0.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (119.7 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

libcidr-1.0.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (122.2 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ ARM64

File details

Details for the file libcidr-1.0.0.tar.gz.

File metadata

  • Download URL: libcidr-1.0.0.tar.gz
  • Upload date:
  • Size: 67.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for libcidr-1.0.0.tar.gz
Algorithm Hash digest
SHA256 b8f3a8621c540f29399035648e544e0b01e066e782bce44e46e4357d8ad09630
MD5 db2dc5622e7bf6d1294623197e15bfd2
BLAKE2b-256 be97040fb48ee2a9e9bf84c0d20c9a8dc733c231cefce2cd6b437d09d81dbc8a

See more details on using hashes here.

Provenance

The following attestation bundles were made for libcidr-1.0.0.tar.gz:

Publisher: release.yml on pawelzelawski/libcidr

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file libcidr-1.0.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for libcidr-1.0.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9a7623adbfe7fc7f4a522f36c244db5fbe89ad312e6c7f50648250b2eec93a8e
MD5 c2b3d4071169a804c7b376158cc35a6a
BLAKE2b-256 edc3c881809df06d9ad5a4c8c3ab7cb9fa99097f6bb20f37d6cf79538c779c68

See more details on using hashes here.

Provenance

The following attestation bundles were made for libcidr-1.0.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on pawelzelawski/libcidr

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file libcidr-1.0.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for libcidr-1.0.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 2e6ed84d3957f78f7a2b9772684dd2af8a71785348b28d4381d022b6c1ab3913
MD5 3419c6007741d9840407e2c314a2b4ba
BLAKE2b-256 42fedfcb0d89bc205c204c5407002c645422b61ba489c65a32fdea1b7a8fa622

See more details on using hashes here.

Provenance

The following attestation bundles were made for libcidr-1.0.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: release.yml on pawelzelawski/libcidr

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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