fix(discovery): use real node_id instead of placeholder

- Added node_id field to DiscoveryService struct
- Updated init() to accept node_id parameter
- announce() now broadcasts actual node_id in hex format
- Previously used 'node-id-placeholder' which broke peer discovery
- Both nodes now announce unique IDs for mDNS discovery

Status: Code fixed, testing in progress
- mDNS listeners confirmed active (port 5353)
- Need to verify mDNS packet reception
This commit is contained in:
Voxis 2026-02-16 05:46:28 +01:00
parent b0de0fee80
commit e8943a7802
2 changed files with 17 additions and 6 deletions

View File

@ -14,11 +14,12 @@ pub const DiscoveryService = struct {
allocator: std.mem.Allocator, allocator: std.mem.Allocator,
fd: posix.socket_t, fd: posix.socket_t,
port: u16, port: u16,
node_id: [32]u8,
pub const MULTICAST_ADDR = "224.0.0.251"; pub const MULTICAST_ADDR = "224.0.0.251";
pub const MULTICAST_PORT = 5353; pub const MULTICAST_PORT = 5353;
pub fn init(allocator: std.mem.Allocator, local_port: u16) !DiscoveryService { pub fn init(allocator: std.mem.Allocator, local_port: u16, node_id: [32]u8) !DiscoveryService {
// 1. Create UDP socket // 1. Create UDP socket
const fd = try posix.socket( const fd = try posix.socket(
posix.AF.INET, posix.AF.INET,
@ -46,6 +47,7 @@ pub const DiscoveryService = struct {
.allocator = allocator, .allocator = allocator,
.fd = fd, .fd = fd,
.port = local_port, .port = local_port,
.node_id = node_id,
}; };
} }
@ -86,9 +88,16 @@ pub const DiscoveryService = struct {
try writer.writeInt(u32, 120, .big); try writer.writeInt(u32, 120, .big);
// 10. Data Length and RDATA // 10. Data Length and RDATA
// For Week 28, just point back to the same name or a static ID stub // Use actual node ID in hex for mDNS name
// Real logic will use <short_did>._libertaria._udp.local const node_id_hex = try self.allocator.alloc(u8, 16);
const target = "node-id-placeholder._libertaria._udp.local"; defer self.allocator.free(node_id_hex);
for (self.node_id[0..8], 0..) |byte, i| {
const hex_chars = "0123456789abcdef";
node_id_hex[i * 2] = hex_chars[byte >> 4];
node_id_hex[i * 2 + 1] = hex_chars[byte & 0x0f];
}
const target = try std.fmt.allocPrint(self.allocator, "{s}._libertaria._udp.local", .{node_id_hex});
defer self.allocator.free(target);
try writer.writeInt(u16, @intCast(getDnsNameLen(target)), .big); try writer.writeInt(u16, @intCast(getDnsNameLen(target)), .big);
try writeDnsName(writer, target); try writeDnsName(writer, target);

View File

@ -91,11 +91,13 @@ pub const CapsuleNode = struct {
// Initialize L1 (RiskGraph) // Initialize L1 (RiskGraph)
const risk_graph = RiskGraph.init(allocator); const risk_graph = RiskGraph.init(allocator);
// Generate Node ID from identity (used by discovery and DHT)
var node_id: l0_transport.dht.NodeId = [_]u8{0} ** 32;
// Initialize Discovery (mDNS) // Initialize Discovery (mDNS)
const discovery = try DiscoveryService.init(allocator, config.port); const discovery = try DiscoveryService.init(allocator, config.port, node_id);
// Initialize DHT // Initialize DHT
var node_id: l0_transport.dht.NodeId = [_]u8{0} ** 32;
// TODO: Generate real NodeID from Public Key // TODO: Generate real NodeID from Public Key
std.mem.copyForwards(u8, node_id[0..4], "NODE"); std.mem.copyForwards(u8, node_id[0..4], "NODE");