fix correctness: IPv6 :: parsing, AF_INET6 on macOS, health check units,

DHT heap fallback
This commit is contained in:
2026-02-11 14:20:55 -03:00
parent e45bffbd8b
commit 137469c3e1
5 changed files with 190 additions and 49 deletions

View File

@@ -145,36 +145,42 @@ pub const Ipv6Address = struct {
}
pub fn fromString(str: []const u8) Error!Ipv6Address {
var addr = Ipv6Address{ .bytes = [_]u8{0} ** 16 };
var group_idx: usize = 0;
// Two-pass parsing for :: compression
// Pass 1: parse groups into front/back arrays around ::
var front: [8]u16 = [_]u16{0} ** 8;
var back: [8]u16 = [_]u16{0} ** 8;
var front_count: usize = 0;
var back_count: usize = 0;
var found_double_colon = false;
var current_val: u16 = 0;
var digit_count: usize = 0;
var i: usize = 0;
// Handle leading ::
if (str.len >= 2 and str[0] == ':' and str[1] == ':') {
found_double_colon = true;
i = 2;
}
while (i < str.len) : (i += 1) {
const c = str[i];
if (c == ':') {
if (i + 1 < str.len and str[i + 1] == ':') {
// Handle :: (compressed zeros)
if (group_idx > 0 and digit_count > 0) {
addr.bytes[group_idx * 2] = @intCast(current_val >> 8);
addr.bytes[group_idx * 2 + 1] = @intCast(current_val & 0xFF);
group_idx += 1;
}
// Skip remaining groups for compressed zeros
i += 1;
current_val = 0;
digit_count = 0;
continue;
}
if (group_idx >= 8) return Error.AddressParseError;
addr.bytes[group_idx * 2] = @intCast(current_val >> 8);
addr.bytes[group_idx * 2 + 1] = @intCast(current_val & 0xFF);
group_idx += 1;
// Write current group
const target = if (found_double_colon) &back else &front;
const count = if (found_double_colon) &back_count else &front_count;
if (count.* >= 8) return Error.AddressParseError;
target[count.*] = current_val;
count.* += 1;
current_val = 0;
digit_count = 0;
if (i + 1 < str.len and str[i + 1] == ':') {
if (found_double_colon) return Error.AddressParseError; // only one :: allowed
found_double_colon = true;
i += 1; // skip second colon
}
} else {
const digit = switch (c) {
const digit: u16 = switch (c) {
'0'...'9' => c - '0',
'a'...'f' => c - 'a' + 10,
'A'...'F' => c - 'A' + 10,
@@ -186,10 +192,33 @@ pub const Ipv6Address = struct {
}
}
// Handle last group
if (digit_count > 0 and group_idx < 8) {
addr.bytes[group_idx * 2] = @intCast(current_val >> 8);
addr.bytes[group_idx * 2 + 1] = @intCast(current_val & 0xFF);
// Write final group
if (digit_count > 0) {
const target = if (found_double_colon) &back else &front;
const count = if (found_double_colon) &back_count else &front_count;
if (count.* >= 8) return Error.AddressParseError;
target[count.*] = current_val;
count.* += 1;
}
// Assemble: front groups + zero padding + back groups
const total = front_count + back_count;
if (found_double_colon) {
if (total > 8) return Error.AddressParseError;
} else {
if (total != 8) return Error.AddressParseError;
}
var addr = Ipv6Address{ .bytes = [_]u8{0} ** 16 };
for (front[0..front_count], 0..) |val, g| {
addr.bytes[g * 2] = @intCast(val >> 8);
addr.bytes[g * 2 + 1] = @intCast(val & 0xFF);
}
const back_start = 8 - back_count;
for (back[0..back_count], 0..) |val, g| {
const idx = back_start + g;
addr.bytes[idx * 2] = @intCast(val >> 8);
addr.bytes[idx * 2 + 1] = @intCast(val & 0xFF);
}
return addr;
@@ -627,9 +656,9 @@ pub const Address = struct {
/// Get address family constant for socket operations
pub fn getSocketFamily(self: Address) u16 {
return switch (self.family) {
.ipv4 => 2, // AF_INET
.ipv6 => 10, // AF_INET6
.unix => 1, // AF_UNIX
.ipv4 => posix.AF.INET,
.ipv6 => posix.AF.INET6,
.unix => posix.AF.UNIX,
.custom => 0,
};
}