fix correctness: IPv6 :: parsing, AF_INET6 on macOS, health check units,
DHT heap fallback
This commit is contained in:
@@ -710,9 +710,18 @@ pub const MessageSerializer = struct {
|
||||
fn getPayloadSize(self: *MessageSerializer, msg: message.Message) usize {
|
||||
_ = self;
|
||||
return switch (msg) {
|
||||
.discovery_hello => 2 + 4 + @sizeOf(message.NetworkAddress),
|
||||
.discovery_response => 8 + 4 + @sizeOf(message.NetworkAddress),
|
||||
.discovery_announce => 4 + @sizeOf(message.NetworkAddress),
|
||||
.discovery_hello => |hello| blk: {
|
||||
const base = 2 + 4 + @sizeOf(message.NetworkAddress);
|
||||
break :blk if (hello.isSigned()) base + 64 + 32 + 8 else base;
|
||||
},
|
||||
.discovery_response => |resp| blk: {
|
||||
const base = 8 + 4 + @sizeOf(message.NetworkAddress);
|
||||
break :blk if (resp.isSigned()) base + 64 + 32 + 8 else base;
|
||||
},
|
||||
.discovery_announce => |announce| blk: {
|
||||
const base = 4 + @sizeOf(message.NetworkAddress) + 1; // +1 for TTL
|
||||
break :blk if (announce.isSigned()) base + 64 + 32 + 8 else base;
|
||||
},
|
||||
.topology_query => 8 + 1,
|
||||
.topology_response => |resp| blk: {
|
||||
// Safe addition with overflow check
|
||||
@@ -959,32 +968,67 @@ pub const MessageSerializer = struct {
|
||||
|
||||
switch (msg) {
|
||||
.discovery_hello => |hello| {
|
||||
// SECURITY: Safe bounds check with overflow protection
|
||||
const required = safeAdd(safeAdd(2, 4) orelse return SerializationError.InvalidLength, @sizeOf(message.NetworkAddress)) orelse return SerializationError.InvalidLength;
|
||||
// Base fields: version(2) + capabilities(4) + address
|
||||
const base = safeAdd(safeAdd(2, 4) orelse return SerializationError.InvalidLength, @sizeOf(message.NetworkAddress)) orelse return SerializationError.InvalidLength;
|
||||
const sig_size: usize = if (hello.isSigned()) 64 + 32 + 8 else 0;
|
||||
const required = safeAdd(base, sig_size) orelse return SerializationError.InvalidLength;
|
||||
try checkBounds(buffer, offset, required);
|
||||
@memcpy(buffer[offset..offset + 2], std.mem.asBytes(&hello.version));
|
||||
offset += 2;
|
||||
@memcpy(buffer[offset..offset + 4], std.mem.asBytes(&hello.capabilities));
|
||||
offset += 4;
|
||||
@memcpy(buffer[offset..offset + @sizeOf(message.NetworkAddress)], std.mem.asBytes(&hello.address));
|
||||
offset += @sizeOf(message.NetworkAddress);
|
||||
// Serialize signature fields if signed
|
||||
if (hello.isSigned()) {
|
||||
@memcpy(buffer[offset..][0..64], &hello.signature);
|
||||
offset += 64;
|
||||
@memcpy(buffer[offset..][0..32], &hello.signer_id);
|
||||
offset += 32;
|
||||
std.mem.writeInt(i64, buffer[offset..][0..8], hello.timestamp, .little);
|
||||
}
|
||||
},
|
||||
.discovery_response => |resp| {
|
||||
// SECURITY: Safe bounds check with overflow protection
|
||||
const required = safeAdd(safeAdd(8, 4) orelse return SerializationError.InvalidLength, @sizeOf(message.NetworkAddress)) orelse return SerializationError.InvalidLength;
|
||||
// Base fields: original_id(8) + capabilities(4) + address
|
||||
const base = safeAdd(safeAdd(8, 4) orelse return SerializationError.InvalidLength, @sizeOf(message.NetworkAddress)) orelse return SerializationError.InvalidLength;
|
||||
const sig_size: usize = if (resp.isSigned()) 64 + 32 + 8 else 0;
|
||||
const required = safeAdd(base, sig_size) orelse return SerializationError.InvalidLength;
|
||||
try checkBounds(buffer, offset, required);
|
||||
@memcpy(buffer[offset..offset + 8], std.mem.asBytes(&resp.original_id));
|
||||
offset += 8;
|
||||
@memcpy(buffer[offset..offset + 4], std.mem.asBytes(&resp.capabilities));
|
||||
offset += 4;
|
||||
@memcpy(buffer[offset..offset + @sizeOf(message.NetworkAddress)], std.mem.asBytes(&resp.address));
|
||||
offset += @sizeOf(message.NetworkAddress);
|
||||
// Serialize signature fields if signed
|
||||
if (resp.isSigned()) {
|
||||
@memcpy(buffer[offset..][0..64], &resp.signature);
|
||||
offset += 64;
|
||||
@memcpy(buffer[offset..][0..32], &resp.signer_id);
|
||||
offset += 32;
|
||||
std.mem.writeInt(i64, buffer[offset..][0..8], resp.timestamp, .little);
|
||||
}
|
||||
},
|
||||
.discovery_announce => |announce| {
|
||||
// SECURITY: Safe bounds check with overflow protection
|
||||
const required = safeAdd(4, @sizeOf(message.NetworkAddress)) orelse return SerializationError.InvalidLength;
|
||||
// Base fields: capabilities(4) + address + ttl(1)
|
||||
const base = safeAdd(safeAdd(4, @sizeOf(message.NetworkAddress)) orelse return SerializationError.InvalidLength, 1) orelse return SerializationError.InvalidLength;
|
||||
const sig_size: usize = if (announce.isSigned()) 64 + 32 + 8 else 0;
|
||||
const required = safeAdd(base, sig_size) orelse return SerializationError.InvalidLength;
|
||||
try checkBounds(buffer, offset, required);
|
||||
@memcpy(buffer[offset..offset + 4], std.mem.asBytes(&announce.capabilities));
|
||||
offset += 4;
|
||||
@memcpy(buffer[offset..offset + @sizeOf(message.NetworkAddress)], std.mem.asBytes(&announce.address));
|
||||
offset += @sizeOf(message.NetworkAddress);
|
||||
buffer[offset] = announce.ttl;
|
||||
offset += 1;
|
||||
// Serialize signature fields if signed
|
||||
if (announce.isSigned()) {
|
||||
@memcpy(buffer[offset..][0..64], &announce.signature);
|
||||
offset += 64;
|
||||
@memcpy(buffer[offset..][0..32], &announce.signer_id);
|
||||
offset += 32;
|
||||
std.mem.writeInt(i64, buffer[offset..][0..8], announce.timestamp, .little);
|
||||
}
|
||||
},
|
||||
.topology_query => |query| {
|
||||
// SECURITY: Safe bounds check with overflow protection
|
||||
|
||||
Reference in New Issue
Block a user