// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! Avahi's D-Bus messages, as bodies: what goes into the method calls that //! publish and browse, and what comes out of the signals that answer them. //! No I/O, so every one is tested -- and fuzzed -- without a bus. //! //! The signatures and constants are Avahi 0.8's, from its introspection //! files (`share/dbus-1/interfaces/org.freedesktop.Avahi.*.xml`) and //! `avahi-common/defs.h` and `address.h`. const std = @import("std"); const mdbus = @import("mdbus"); pub const Encoder = mdbus.Connection.Encoder; pub const Decoder = mdbus.wire.Decoder; /// Any interface: `AVAHI_IF_UNSPEC`. pub const if_unspec: i32 = -1; /// An address family, as Avahi numbers them. pub const Protocol = enum(i32) { inet = 0, inet6 = 1, unspec = -1, _, }; /// The daemon's state, as `Server.GetState` and `Server.StateChanged` say it. pub const ServerState = enum(i32) { invalid = 0, /// Its host records are being probed for. registering = 1, /// Its host records are established: services can be published. running = 2, collision = 3, failure = 4, _, }; /// An entry group's state, as `EntryGroup.StateChanged` says it. pub const EntryGroupState = enum(i32) { uncommitted = 0, registering = 1, established = 2, /// A name in the group is taken on the network; its records have been /// withdrawn. collision = 3, failure = 4, _, }; /// `AVAHI_LOOKUP_RESULT_OUR_OWN`: a service this host itself publishes. pub const lookup_result_our_own: u32 = 16; /// `AVAHI_LOOKUP_RESULT_LOCAL`: from this host's own cache or records. pub const lookup_result_local: u32 = 8; /// A service to publish. `name` is the instance's name, which Avahi may /// have to change if another host has it; `host` empty is this host. pub const Service = struct { name: []const u8, /// `_sendspin._tcp`, say. type: []const u8, /// Empty is `local`. domain: []const u8 = "", /// Empty is this host's own name. host: []const u8 = "", port: u16, /// Each a `key=value` string, as RFC 6763 §6 has it. txt: []const []const u8 = &.{}, interface: i32 = if_unspec, protocol: Protocol = .unspec, }; pub const add_service_signature = "iiussssqaay"; /// The body of `EntryGroup.AddService` for `service`, under `name` -- the /// service's own, or the alternative a collision made it take. pub fn encodeAddService(e: *Encoder, service: Service, name: []const u8) !void { try e.int32(service.interface); try e.int32(@intFromEnum(service.protocol)); try e.uint32(0); try e.string(name); try e.string(service.type); try e.string(service.domain); try e.string(service.host); try e.uint16(service.port); try encodeTxt(e, service.txt); } /// A TXT record as Avahi carries it: `aay`, an array of strings of bytes. pub fn encodeTxt(e: *Encoder, txt: []const []const u8) !void { const outer = try e.beginArray('a'); for (txt) |record| { const inner = try e.beginArray('y'); try e.byteSlice(record); try e.endArray(inner); } try e.endArray(outer); } pub const service_browser_new_signature = "iissu"; /// The body of `Server.ServiceBrowserNew`. pub fn encodeServiceBrowserNew(e: *Encoder, protocol: Protocol, service_type: []const u8, domain: []const u8) !void { try e.int32(if_unspec); try e.int32(@intFromEnum(protocol)); try e.string(service_type); try e.string(domain); try e.uint32(0); } pub const service_resolver_new_signature = "iisssiu"; /// The body of `Server.ServiceResolverNew`, for the item a browser found, /// its address in `address_protocol`. pub fn encodeServiceResolverNew(e: *Encoder, item: Item, address_protocol: Protocol) !void { try e.int32(item.interface); try e.int32(@intFromEnum(item.protocol)); try e.string(item.name); try e.string(item.type); try e.string(item.domain); try e.int32(@intFromEnum(address_protocol)); try e.uint32(0); } /// A service instance a browser saw arrive or leave, on one interface in /// one protocol: `ServiceBrowser.ItemNew` and `ItemRemove`. The strings /// borrow the message. pub const Item = struct { interface: i32, protocol: Protocol, name: []const u8, type: []const u8, domain: []const u8, flags: u32, pub const signature = "iisssu"; pub fn decode(d: *Decoder) !Item { const item: Item = .{ .interface = try d.int32(), .protocol = @enumFromInt(try d.int32()), .name = try d.string(), .type = try d.string(), .domain = try d.string(), .flags = try d.uint32(), }; try d.end(); return item; } pub fn ourOwn(self: Item) bool { return self.flags & lookup_result_our_own != 0; } }; /// Most TXT strings a resolved service is read with; more are dropped. pub const max_txt = 32; /// A service instance resolved: `ServiceResolver.Found`. The strings borrow /// the message. pub const Resolved = struct { interface: i32, protocol: Protocol, name: []const u8, type: []const u8, domain: []const u8, /// The host's name, `speaker.local`, say. host: []const u8, address_protocol: Protocol, /// In text, as Avahi gives it: `192.168.1.20`, or `fe80::1`. address: []const u8, port: u16, txt_buf: [max_txt][]const u8 = undefined, txt_len: usize = 0, flags: u32, pub const signature = "iissssisqaayu"; pub fn decode(d: *Decoder) !Resolved { var r: Resolved = .{ .interface = try d.int32(), .protocol = @enumFromInt(try d.int32()), .name = try d.string(), .type = try d.string(), .domain = try d.string(), .host = try d.string(), .address_protocol = @enumFromInt(try d.int32()), .address = try d.string(), .port = try d.uint16(), .flags = undefined, }; const outer = try d.beginArray('a'); while (!try d.arrayFinished(outer)) { const inner = try d.beginArray('y'); const record = try d.byteSlice(inner); if (r.txt_len < max_txt) { r.txt_buf[r.txt_len] = record; r.txt_len += 1; } } try d.endArray(outer); r.flags = try d.uint32(); try d.end(); return r; } /// The TXT strings, in order. pub fn txt(self: *const Resolved) []const []const u8 { return self.txt_buf[0..self.txt_len]; } /// The value of TXT `key`, matched without regard to case as RFC 6763 /// §6.4 has it; empty for a key with no `=`, null for one not there. pub fn txtValue(self: *const Resolved, key: []const u8) ?[]const u8 { return findTxt(self.txt(), key); } pub fn ourOwn(self: *const Resolved) bool { return self.flags & lookup_result_our_own != 0; } }; /// The value of `key` among `txt`, as `Resolved.txtValue` finds it. pub fn findTxt(txt: []const []const u8, key: []const u8) ?[]const u8 { for (txt) |record| { const eq = std.mem.indexOfScalar(u8, record, '=') orelse record.len; if (eq == 0) continue; if (!std.ascii.eqlIgnoreCase(record[0..eq], key)) continue; return if (eq == record.len) "" else record[eq + 1 ..]; } return null; } /// `EntryGroup.StateChanged` and `Server.StateChanged`: a state and, for a /// failure, why. pub const StateChanged = struct { state: i32, @"error": []const u8, pub const signature = "is"; pub fn decode(d: *Decoder) !StateChanged { const s: StateChanged = .{ .state = try d.int32(), .@"error" = try d.string() }; try d.end(); return s; } }; /// `org.freedesktop.DBus.NameOwnerChanged`: the daemon coming and going. pub const NameOwnerChanged = struct { name: []const u8, old_owner: []const u8, new_owner: []const u8, pub const signature = "sss"; pub fn decode(d: *Decoder) !NameOwnerChanged { const n: NameOwnerChanged = .{ .name = try d.string(), .old_owner = try d.string(), .new_owner = try d.string() }; try d.end(); return n; } }; const testing = std.testing; fn decoderOf(e: *const Encoder) Decoder { return .{ .data = e.bytes(), .endian = .little }; } test "a service's AddService body" { var e: Encoder = .init(testing.allocator); defer e.deinit(); try encodeAddService(&e, .{ .name = "Kitchen", .type = "_sendspin._tcp", .port = 8928, .txt = &.{ "name=Kitchen", "path=/sendspin" } }, "Kitchen #2"); var d = decoderOf(&e); try testing.expectEqual(if_unspec, try d.int32()); try testing.expectEqual(@as(i32, -1), try d.int32()); try testing.expectEqual(@as(u32, 0), try d.uint32()); try testing.expectEqualStrings("Kitchen #2", try d.string()); try testing.expectEqualStrings("_sendspin._tcp", try d.string()); try testing.expectEqualStrings("", try d.string()); try testing.expectEqualStrings("", try d.string()); try testing.expectEqual(@as(u16, 8928), try d.uint16()); const outer = try d.beginArray('a'); for ([_][]const u8{ "name=Kitchen", "path=/sendspin" }) |want| { try testing.expect(!try d.arrayFinished(outer)); const inner = try d.beginArray('y'); try testing.expectEqualStrings(want, try d.byteSlice(inner)); } try d.endArray(outer); try d.end(); } test "a resolved service, and its TXT" { var e: Encoder = .init(testing.allocator); defer e.deinit(); try e.int32(2); try e.int32(0); try e.string("Kitchen"); try e.string("_sendspin._tcp"); try e.string("local"); try e.string("kitchen.local"); try e.int32(0); try e.string("192.168.1.20"); try e.uint16(8928); try encodeTxt(&e, &.{ "Name=Kitchen", "path=/sendspin", "flag", "=novalue" }); try e.uint32(lookup_result_local | lookup_result_our_own); var d = decoderOf(&e); const r = try Resolved.decode(&d); try testing.expectEqualStrings("kitchen.local", r.host); try testing.expectEqualStrings("192.168.1.20", r.address); try testing.expectEqual(Protocol.inet, r.address_protocol); try testing.expectEqual(@as(u16, 8928), r.port); try testing.expectEqual(@as(usize, 4), r.txt().len); try testing.expectEqualStrings("Kitchen", r.txtValue("name").?); try testing.expectEqualStrings("/sendspin", r.txtValue("PATH").?); try testing.expectEqualStrings("", r.txtValue("flag").?); try testing.expect(r.txtValue("missing") == null); try testing.expect(r.ourOwn()); } test "an item, a state, an owner change" { var e: Encoder = .init(testing.allocator); defer e.deinit(); try e.int32(3); try e.int32(1); try e.string("Den"); try e.string("_sendspin._tcp"); try e.string("local"); try e.uint32(0); var d = decoderOf(&e); const item = try Item.decode(&d); try testing.expectEqual(Protocol.inet6, item.protocol); try testing.expectEqualStrings("Den", item.name); try testing.expect(!item.ourOwn()); var e2: Encoder = .init(testing.allocator); defer e2.deinit(); try e2.int32(@intFromEnum(EntryGroupState.collision)); try e2.string(""); var d2 = decoderOf(&e2); const s = try StateChanged.decode(&d2); try testing.expectEqual(EntryGroupState.collision, @as(EntryGroupState, @enumFromInt(s.state))); // Trailing bytes are refused: the signature says what is there. try e2.string("extra"); var d3 = decoderOf(&e2); try testing.expectError(error.TrailingData, StateChanged.decode(&d3)); }