Repository navigation
Use std.posix.sockaddr types for IP addresses - #110
Conversation
Backport changes from zig-0.16 branch to use std.posix.sockaddr.in and std.posix.sockaddr.in6 directly instead of std.net.Ip4Address and std.net.Ip6Address. This reduces dependency on std.net types and provides a foundation for removing std.net dependency entirely in Zig 0.15. Changes: - Add getSockAddrLen() helper function - Update IpAddress union to use std.posix.sockaddr types - Implement custom initIp4/initIp6 functions - Implement pure Zig IPv4/IPv6 parsers (cross-platform) - Custom format() for IPv4, IPv6 with :: compression support - Update all socket operations (bind, connect, sendTo, accept) - Add format validation to tests to catch byte-order bugs
|
Caution Review failedThe pull request is closed. WalkthroughReplaced higher-level IP/Address types with POSIX sockaddr-based storage, added getSockAddrLen and addressToStorage helpers, implemented raw IPv4/IPv6 parsing/formatting and conditional Unix-socket handling, and updated connect/bind/send/receive flows and vendor backends to use sockaddr.storage. Changes
Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes
Possibly related PRs
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
📜 Recent review detailsConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro 📒 Files selected for processing (2)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (2)
src/io/net.zig (2)
142-160: Minor redundancy in parseIpAndPort.Lines 144-145 call
indexOffor"["twice. The second call is redundant since you're already inside the block where it returned non-null. Consider simplifying:- if (std.mem.indexOf(u8, name, "[")) |_| { - const start = std.mem.indexOf(u8, name, "[") orelse return error.InvalidFormat; + if (std.mem.indexOf(u8, name, "[")) |start| { const end = std.mem.indexOf(u8, name, "]") orelse return error.InvalidFormat;
804-853: Inconsistent use of has_unix_sockets constant.Line 13 defines
has_unix_sockets = std.net.has_unix_sockets, and line 829 uses it. However, lines 804 and 853 still referencestd.net.has_unix_socketsdirectly. For consistency, use the local constant:- if (!std.net.has_unix_sockets) unreachable; + if (!has_unix_sockets) unreachable;
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/io/net.zig(11 hunks)src/io/test/net.zig(2 hunks)
🔇 Additional comments (12)
src/io/test/net.zig (3)
27-30: Good test coverage for byte-order validation.The format validation correctly verifies the round-trip behavior (parse → format → string comparison), which will catch byte-order issues in the port and address conversions. Buffer size of 32 bytes is adequate for IPv4 addresses with ports.
37-40: IPv6 format validation looks good.Testing the formatted output catches potential issues with byte ordering and ensures the IPv6 compression logic produces correct results. Buffer size of 64 bytes is appropriate for IPv6 addresses with brackets and port notation.
58-68: Comprehensive parseIpAndPort validation.Testing both IPv4 and IPv6 address:port formats ensures the parsing logic correctly handles both cases. The format checks validate that the output matches the expected canonical form, catching any byte-order or formatting bugs.
src/io/net.zig (9)
33-39: initIp4 correctly handles byte order and alignment.The port conversion to big-endian (line 36) is correct for network byte order. The alignment handling at line 37 properly deals with the [4]u8 → u32 conversion using
align(1)to indicate the pointer may be unaligned.
63-73: parseIp4 implementation looks solid.Edge cases are properly handled:
- Bounds checking prevents buffer overflow (line 68)
- parseInt catches non-numeric input and values > 255 (line 69)
- Final validation ensures exactly 4 octets (line 71)
75-132: IPv6 parser is complex but correct.The implementation properly handles IPv6 abbreviations using a two-buffer approach. Key validations:
- Prevents multiple
::abbreviations (line 88)- Overflow checks for hex values (lines 105-110)
- Bounds checking throughout
- Correct reassembly of abbreviated addresses (line 127)
The complexity is inherent to IPv6 parsing, and the logic appears sound.
182-263: IPv6 formatting with compression is well-implemented.The formatter correctly:
- Detects and formats IPv4-mapped IPv6 addresses (lines 192-202)
- Finds the longest run of zeros for
::compression (lines 211-230)- Only compresses runs of 2+ zeros per RFC 5952 (line 232)
- Properly emits the compressed notation (lines 238-259)
The logic for skipping the compressed range (line 248) works correctly with the loop increment.
164-180: Port byte-order conversions are correct.getPort and setPort properly convert between native and network byte order (big-endian) for both IPv4 and IPv6. This matches the initialization in initIp4 (line 36) and initIp6 (line 56).
285-308: Conditional Unix socket support is consistently guarded.The
unfield having typevoidwhenhas_unix_socketsis false is an unconventional pattern. All access points are properly guarded withif (!has_unix_sockets) unreachable;checks (lines 290, 303), which prevents accessing the void field at runtime. This works but is somewhat fragile compared to compile-time guarantees.
552-567: Socket sendTo operations correctly use address lengths.The memcpy approach at line 564 properly copies only the required bytes for the address family (determined by getSockAddrLen). This avoids including padding or uninitialized data.
983-995: netConnect properly updated for new Address type.The signature change from
std.net.AddresstoAddressis consistent with the refactor goals. The memcpy at line 992 correctly copies only the required address bytes using getSockAddrLen.
17-26: Build verification confirms the code compiles cleanly on WASI. Thestd.posix.sockaddr.unreference doesn't cause compilation issues, so the current implementation is fine as-is. The platform guards already preventAF.UNIXaddresses from being created when unsupported, making the unreachable branch theoretically safe even without explicit conditionals.The suggested refactor was optional; the code works.
Update libxev to use posix.sockaddr.storage instead of std.net.Address for TCP connect operations across all backends (io_uring, epoll, kqueue, iocp). This reduces dependency on std.net types and aligns with the approach used in Zig 0.16. Changes: - Add getSockAddrLen() and addressToStorage() helpers in posix.zig - Update connect operation struct in all backends - Convert std.net.Address to sockaddr.storage in tcp.zig
Update remaining test cases that were using std.net.Address directly for connect operations to use addressToStorage() helper.
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (1)
vendor/libxev/src/backend/io_uring.zig (1)
434-438: Consider extracting the repeated cast.The
@ptrCast(&v.addr)appears twice in the connect preparation. You could store it in a local variable to reduce duplication:-.connect => |*v| sqe.prep_connect( - v.socket, - @ptrCast(&v.addr), - posix_utils.getSockAddrLen(@ptrCast(&v.addr)), -), +.connect => |*v| { + const addr_ptr = @ptrCast(&v.addr); + sqe.prep_connect( + v.socket, + addr_ptr, + posix_utils.getSockAddrLen(addr_ptr), + ); +},
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (6)
vendor/libxev/src/backend/epoll.zig(3 hunks)vendor/libxev/src/backend/io_uring.zig(4 hunks)vendor/libxev/src/backend/iocp.zig(3 hunks)vendor/libxev/src/backend/kqueue.zig(2 hunks)vendor/libxev/src/posix.zig(1 hunks)vendor/libxev/src/watcher/tcp.zig(2 hunks)
🧰 Additional context used
🧠 Learnings (2)
📚 Learning: 2025-10-21T10:00:56.962Z
Learnt from: lalinsky
Repo: lalinsky/zio PR: 62
File: src/utils/queue.zig:141-147
Timestamp: 2025-10-21T10:00:56.962Z
Learning: In work-stealing queues implemented in src/utils/queue.zig, stealers only modify the victim's head position, never the tail. The tail is exclusively owned by the owner thread, so no atomic acquire operations are needed when the owner reads its own tail.
Applied to files:
vendor/libxev/src/backend/iocp.zig
📚 Learning: 2025-10-10T05:59:17.087Z
Learnt from: lalinsky
Repo: lalinsky/zio PR: 41
File: src/udp.zig:79-82
Timestamp: 2025-10-10T05:59:17.087Z
Learning: In UDP socket operations (src/udp.zig), error.Canceled from result_data.result represents OS/IO-level cancellation (e.g., io_uring), not user-requested cancellation. User cancellation is handled by waitForXevCompletion() via try. OS-level error.Canceled should be mapped to error.Unexpected as it represents an unexpected system event.
Applied to files:
vendor/libxev/src/backend/io_uring.zig
🪛 GitHub Actions: CI
vendor/libxev/src/backend/kqueue.zig
[error] 2443-2443: Command failed: cd vendor/libxev && zig build test | Type mismatch: expected type 'c.sockaddr__struct_2232.storage', found 'net.Address' at .addr assignment (src/backend/kqueue.zig:2443).
🔇 Additional comments (3)
vendor/libxev/src/backend/io_uring.zig (3)
6-6: Import added for POSIX address utilities.Clean addition. The
posix_utilsmodule provides the address conversion and length calculation functions needed for the sockaddr migration.
1015-1018: Type migration from std.net.Address to posix.sockaddr.storage.This change removes the high-level abstraction in favor of direct POSIX types. Note that unlike the
acceptoperation which initializesaddr = undefined, theconnectoperation correctly requires the caller to provide a valid address, since connect needs a destination.
1584-1604: Address conversion in test works correctly.The
addressToStoragehelper cleanly converts thestd.net.Addressto the newposix.sockaddr.storageformat. The test successfully establishes a connection (verified at line 1609), confirming the conversion preserves the address information correctly.
Backport changes from zig-0.16 branch to use std.posix.sockaddr.in and std.posix.sockaddr.in6 directly instead of std.net.Ip4Address and std.net.Ip6Address.
This reduces dependency on std.net types and provides a foundation for removing std.net dependency entirely in Zig 0.15.
Changes: