Keely has opened PR #3 for IP address types in swift-networking-types and invited feedback ahead of Monday’s Networking Workgroup meeting. I’m bringing my detailed response here because the representation choices connect directly to requirements we have been discussing in this thread.
The numeric initializers and accessors are useful, and keeping storage private preserves implementation flexibility. I would recommend UInt32 and UInt128 as the backing storage for IPv4 and IPv6 address bits, with InlineArray providing an explicit octet boundary.
The reason is the range of networking operations these shared types need to support. Endpoint interoperability is one important consumer. Routing, allocation, policy, and network-data processing also need a foundation whose representation makes their operations straightforward.
As discussed earlier regarding canonical prefixes, an address with prefix context, such as 192.0.2.129/24, preserves an individual address. Its canonical network, 192.0.2.0/24, identifies address space. That distinction matters when allocating a block, comparing routing information, or applying policy. A prefix length also has uses independently of an address, including prefix-length bounds in policy. For example, a ROA carries prefixes and an optional maximum length. The prefix identifies covered address space; the maximum length limits the specificity of the routes it can authorize. Those are distinct values with a relationship the consumer must preserve.
My concrete-type sketch in post #31 was intended to accommodate the direction of the NWG implementation: separate IPv4/IPv6 types, validated family-specific prefix lengths, mixed-family enums, numeric storage, and network-order octets. It explicitly allows network and endpoint types to follow progressively. We can establish that direction without requiring every operation in the first release.
Scale is part of this requirement. I process approximately 189,000 RPKI records from one Regional Internet Registry every 15 minutes. That is a concrete recurring workload in which networking values are data being processed. Workloads in this space give us reason to evaluate repeated masking, comparison, ordering, prefix-length checks, and containment alongside parsing and socket conversion. The record count establishes the use case; performance conclusions still require measurement.
A few representative consumers illustrate the shared operations:
- Routing and policy analysis: canonical prefix identity, containment, and selection of more-specific prefixes.
- IPAM and configuration: allocation boundaries, subdivision, overlap detection, and checking that a requested block fits inside a parent.
- Firewall and admission policy: address membership, range comparison, and exact coverage, while preserving the policy associated with that coverage.
- Telemetry and inventory: classifying flow addresses against prefixes and matching observations to address ranges.
DNS, registry lookups, and reachability tools consume these values too. Their queries, observations, and decisions belong in higher layers. The currency layer supplies the address-space operations those layers compose.
In swift-cidr, the address bits are stored as UInt32 for IPv4 and UInt128 for IPv6. These retain ordinary numeric semantics: 127.0.0.1 has the value 0x7F00_0001 on either a little-endian or big-endian machine. Its layout in memory is a separate question. Masking, ordering, arithmetic, and leading/trailing bit counts can therefore operate directly on the stored values. I discussed that distinction in post #33, with a longer explanation in #34.
Integer fields already form part of the containing value, so the choice is about which operations the representation supports directly and how byte-oriented consumers obtain their data. The PR already makes numeric values accessible, and byte-backed storage can support these operations. My recommendation concerns which representation should make the recurring work direct. Both choices deserve evaluation against representative workloads.
Family relationships remain relevant with concrete types: an IPv4 address pairs with an IPv4 prefix length and produces an IPv4 network. The same relationship holds for IPv6. swift-cidr will retain its generic design for its algorithms and downstream composition. Those examples explain the relationships; concrete types can express them in the NWG API without adopting that generic design.
InlineArray has a specific role in swift-cidr. The public .octets projections and octet initializers exchange InlineArray<4, UInt8> or InlineArray<16, UInt8> values in network byte order. They provide owned, fixed-size bytes for protocol and interoperability boundaries without text conversion. They preserve the address bits, including host bits, but carry neither prefix context nor scope.
For example, using swift-cidr’s concrete aliases with Swift 6.2+ on Linux or targeting Apple OS 26+:
import CIDR
if let length = IPv4PrefixLength(24) {
let address = IPv4Address(address: 0xC000_0281, prefixLength: length)
let network = address.network
let octets: InlineArray<4, UInt8> = address.octets
print(address) // 192.0.2.129/24
print(network) // 192.0.2.0/24
print(octets[0], octets[1], octets[2], octets[3]) // 192 0 2 129
}
The network projection explicitly clears host bits; the octet projection preserves the original address. Reconstructing its full address-with-context value requires supplying the prefix length separately.
The core CIDR module has no dependencies. CIDRNIO is a separate adapter module for SwiftNIO, including SocketAddress conversions. Those conversions copy octets into socket address fields on Linux and Apple OS 26+, with integer-based fallbacks on earlier supported Apple releases. Its ByteBuffer operations remain integer-based. The separate CIDRPOSIX module supplies POSIX family constants and address-payload extraction. This keeps socket interoperability available without making socket structure the organizing principle of the core model.
That separation opens more of the Swift on Server space: services processing routing, addressing, policy, and operational data alongside services establishing connections. I would like us to evaluate this foundation against recurring prefix-processing workloads as well as socket interoperability, using integer backing and explicit byte boundaries as the starting recommendation.