diff --git a/capsule-core/src/discovery.zig b/capsule-core/src/discovery.zig index 5d06760..b52d3c0 100644 --- a/capsule-core/src/discovery.zig +++ b/capsule-core/src/discovery.zig @@ -14,11 +14,12 @@ pub const DiscoveryService = struct { allocator: std.mem.Allocator, fd: posix.socket_t, port: u16, + node_id: [32]u8, pub const MULTICAST_ADDR = "224.0.0.251"; 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 const fd = try posix.socket( posix.AF.INET, @@ -46,6 +47,7 @@ pub const DiscoveryService = struct { .allocator = allocator, .fd = fd, .port = local_port, + .node_id = node_id, }; } @@ -86,9 +88,16 @@ pub const DiscoveryService = struct { try writer.writeInt(u32, 120, .big); // 10. Data Length and RDATA - // For Week 28, just point back to the same name or a static ID stub - // Real logic will use ._libertaria._udp.local - const target = "node-id-placeholder._libertaria._udp.local"; + // Use actual node ID in hex for mDNS name + const node_id_hex = try self.allocator.alloc(u8, 16); + 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 writeDnsName(writer, target); diff --git a/capsule-core/src/node.zig b/capsule-core/src/node.zig index 93fd214..189d4a1 100644 --- a/capsule-core/src/node.zig +++ b/capsule-core/src/node.zig @@ -91,11 +91,13 @@ pub const CapsuleNode = struct { // Initialize L1 (RiskGraph) 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) - const discovery = try DiscoveryService.init(allocator, config.port); + const discovery = try DiscoveryService.init(allocator, config.port, node_id); // Initialize DHT - var node_id: l0_transport.dht.NodeId = [_]u8{0} ** 32; // TODO: Generate real NodeID from Public Key std.mem.copyForwards(u8, node_id[0..4], "NODE");