Skip to main content

PyTCP-net_proto

The network-protocol packet parse / assemble / validate layer of the PyTCP TCP/IP stack — extracted as its own distribution and usable on its own.

from net_proto.protocols.udp.udp__assembler import UdpAssembler
from net_proto.protocols.udp.udp__parser import UdpParser

# Assemble (TX): serialise header + payload into a list of buffers.
datagram = UdpAssembler(udp__sport=12345, udp__dport=53, udp__payload=b"query")
buffers: list[bytes] = []
datagram.assemble(buffers)

# Parse (RX): raises UdpIntegrityError / UdpSanityError on bad wire input.
parsed = UdpParser(packet_rx)

Why

Strict, RFC-grounded, fully-typed wire-format codecs for the common Internet protocols, with a single clean validation-error tree and no runtime dependencies beyond the address library it is built on.

Protocol coverage

Each protocol is a parser / assembler pair over a frozen header dataclass, with integrity + sanity validation and typed wire enums.

Family Governing RFC(s)
Ethernet II RFC 894 / 7042 (EtherType)
IEEE 802.3 + LLC / SNAP IEEE 802.3 / 802.2 / RFC 1042
ARP RFC 826
IPv4 (+ options: LSRR/SSRR, RR, Timestamp, Router-Alert, CIPSO) RFC 791 / 1108 / 2113
IPv6 RFC 8200
IPv6 Hop-by-Hop / Destination Options (PadN, Jumbo, RouterAlert, Tunnel-Limit, CALIPSO) RFC 8200 / 2675 / 2711
IPv6 Routing / Fragment extension headers RFC 8200 / 5095
ICMPv4 RFC 792 / 1122
ICMPv6 RFC 4443
ICMPv6 Neighbor Discovery (+ options) RFC 4861 / 8106
ICMPv6 MLDv2 (+ MLDv1 compatibility) RFC 3810 / 2710
IGMP (host membership: IGMPv1/v2/v3 + source-specific) RFC 1112 / 2236 / 3376
TCP (+ options: MSS, WScale, SACK, Timestamps, AccECN, Fast-Open) RFC 9293 / 2018 / 7323 / 9768 / 7413
UDP RFC 768
DHCPv4 (+ options) RFC 2131 / 2132
DHCPv6 (+ options) RFC 8415
DNS (A / AAAA query + response, name compression) RFC 1035

The six-file pattern

Every protocol under protocols/<proto>/ follows the same layout (see .claude/rules/net_proto.md):

  • <proto>__header.py — the frozen *Header dataclass (@dataclass(frozen=True, kw_only=True, slots=True)) + the *HeaderProperties read-mixin + the RFC ASCII diagram + struct constants.
  • <proto>__base.py* base composing header (+ options + payload) with the shared dunders (__len__ / __str__ / __repr__ / __buffer__).
  • <proto>__parser.py — the three-phase RX pipeline: _validate_integrity()_parse()_validate_sanity().
  • <proto>__assembler.py — the keyword-only TX constructor + assemble(buffers, /) with checksum injection.
  • <proto>__errors.py — the *IntegrityError / *SanityError pair.
  • <proto>__enums.py + options/ — protocol enums and TLV options where the protocol has them.

Validation-error model

One two-axis tree, rendered with a canonical category + protocol prefix so tests and logs match exactly:

PacketIntegrityError → "[INTEGRITY ERROR][<PROTO>] ..."   (structural / wire-shape)
PacketSanityError    → "[SANITY ERROR][<PROTO>] ..."      (logical invariant)

Integrity vs sanity: integrity checks run on the raw frame before fields are trusted (length bounds, checksum, header shape); sanity checks run on already-parsed fields (a port of 0, a reserved-bit violation). Both raise typed *Errors — never assert — so they survive python -O (assertions stripped), because they defend against hostile wire input. Conversely, *Header.__post_init__ and *Assembler.__init__ use assert (programmer-error guards, OK to strip under -O); any wire-reachable bound an assert guards is mirrored as a typed raise in _validate_integrity. This wire-input vs programmer-input discipline (rule §9.2) is AST-clean across the package.

Typed wire enums

Protocol codepoints are ProtoEnumByte / ProtoEnumWord subclasses (EtherType, IpProto, Icmp6Type, ArpOperation, …), never bare ints. Unknown wire codepoints are materialised natively via the stdlib enum.Enum._missing_ hook (an UNKNOWN_<value> identity-stable pseudo-member) — no third-party aenum dependency.

Install

pip install PyTCP-net_proto

Depends only on PyTCP-net_addr (the address value-type library) — no other runtime dependencies. Fully typed (ships py.typed, PEP 561); strict-mypy clean.

Requirements

Python 3.14+ (PEP 695 generics on the assembler stacking, modern typing throughout).

Current state (3.0.8)

  • ~270 source modules; 6024 unit tests, ~99% source coverage (the remaining lines are protocol dunders, from_buffer unpacking, and a few integrity-rejection branches with no dedicated rejection test — test-completeness, not defects).
  • Per-RFC adherence records live in docs/rfc/ (the wire-format header / parser / assembler / options rows are net_proto's surface). The parser RFC-adherence pass and the assembler audit pass are both CLOSED; follow-up audits A–L are complete (see docs/refactor/net_proto_remaining_audits.md).

Changelog

See CHANGELOG.md.

License

GPL-3.0-or-later. Part of the PyTCP project by Sebastian Majewski.

Download files

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

Source Distribution

pytcp_net_proto-3.0.9.tar.gz (201.4 kB view details)

Uploaded Source

Built Distribution

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

pytcp_net_proto-3.0.9-py3-none-any.whl (499.0 kB view details)

Uploaded Python 3

File details

Details for the file pytcp_net_proto-3.0.9.tar.gz.

File metadata

  • Download URL: pytcp_net_proto-3.0.9.tar.gz
  • Upload date:
  • Size: 201.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for pytcp_net_proto-3.0.9.tar.gz
Algorithm Hash digest
SHA256 39a390c9ef8c64398805c542980ebffba792d5a6446974da131ab75b4aad1bc2
MD5 147ff1424a6c9401380156eb2da41b12
BLAKE2b-256 26b124df88c24411d3336b3eb06e8a45fa015ba6c7d1afdb48a207c15c1fbb29

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytcp_net_proto-3.0.9.tar.gz:

Publisher: publish.yml on ccie18643/PyTCP

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

File details

Details for the file pytcp_net_proto-3.0.9-py3-none-any.whl.

File metadata

  • Download URL: pytcp_net_proto-3.0.9-py3-none-any.whl
  • Upload date:
  • Size: 499.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for pytcp_net_proto-3.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 5814bdf76ce48f399a6b28b46be2d8c64e2b62ccd2ef4488c406b47f1e4b014d
MD5 af2a5dd52bd036a4c028fafb8d8c9cc0
BLAKE2b-256 e7bc89c4a6e6e2f670337a84d76bb67cb2e376be7b9154142a5de584c7d88e87

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytcp_net_proto-3.0.9-py3-none-any.whl:

Publisher: publish.yml on ccie18643/PyTCP

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

Release history Release notifications | RSS feed

This release

3.0.9 This release

2 files

3.0.8

2 files

3.0.7

2 files

3.0.6

2 files

3.0.5

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page