Something went wrong. Try again.
Avahi for Zig: publish and browse mDNS services through the system's Avahi daemon
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! Publish a service, or browse for services, through Avahi.//!//! ```console//! $ zavahi publish Kitchen _sendspin._tcp 8928 name=Kitchen path=/sendspin//! published _sendspin._tcp as Kitchen//! $ zavahi browse _sendspin._tcp 5//! found Kitchen kitchen.local 192.168.1.20 8928 name=Kitchen path=/sendspin//! $ zavahi state//! running//! ```//!//! Each runs until interrupted, or for the seconds given last. Found and lost//! services are printed a line each, once for each interface and protocol//! they are seen on.
const std = @import("std");const Io = std.Io;const avahi = @import("avahi");
pub fn main(init: std.process.Init) !u8 { const io = init.io; const gpa = init.gpa; var args = try init.minimal.args.iterateAllocator(gpa); defer args.deinit(); _ = args.skip(); var list: std.ArrayList([]const u8) = .empty; defer list.deinit(gpa); while (args.next()) |a| try list.append(gpa, a); const argv = list.items; if (argv.len == 0) return usage();
const client = avahi.Client.connect(io, gpa, init.minimal.environ) catch |err| { std.debug.print("cannot reach the Avahi daemon: {t}\n", .{err}); return 1; }; defer client.deinit();
if (std.mem.eql(u8, argv[0], "state")) { std.debug.print("{t}\n", .{try client.serverState()}); return 0; }
var running = try io.concurrent(avahi.Client.run, .{client}); defer { client.stop(); running.await(io) catch {}; }
var seconds: ?u32 = null; if (std.mem.eql(u8, argv[0], "publish")) { if (argv.len < 4) return usage(); const port = std.fmt.parseInt(u16, argv[3], 10) catch return usage(); var txt = argv[4..]; if (txt.len > 0) if (std.fmt.parseInt(u32, txt[txt.len - 1], 10)) |s| { seconds = s; txt = txt[0 .. txt.len - 1]; } else |_| {}; const group = client.publish(.{ .name = argv[1], .type = argv[2], .port = port, .txt = txt }) catch |err| { std.debug.print("cannot publish: {t}\n", .{err}); return 1; }; // Say once it is out there, as whatever name it had to take. var said = false; var waited: u32 = 0; while (seconds == null or waited < seconds.? * 10) : (waited += 1) { if (!said and group.currentState() == .established) { std.debug.print("published {s} as {s}\n", .{ argv[2], group.name }); said = true; } try io.sleep(.fromMilliseconds(100), .awake); } return 0; }
if (std.mem.eql(u8, argv[0], "browse")) { if (argv.len < 2) return usage(); if (argv.len > 2) seconds = std.fmt.parseInt(u32, argv[2], 10) catch return usage(); var printer: Printer = .{}; _ = client.browse(argv[1], .{}, printer.handler()) catch |err| { std.debug.print("cannot browse: {t}\n", .{err}); return 1; }; if (seconds) |s| try io.sleep(.fromSeconds(s), .awake) else while (true) try io.sleep(.fromSeconds(3600), .awake); return 0; } return usage();}
fn usage() u8 { std.debug.print( \\usage: zavahi publish NAME TYPE PORT [KEY=VALUE ...] [SECONDS] \\ zavahi browse TYPE [SECONDS] \\ zavahi state \\ , .{}); return 2;}
const Printer = struct { fn handler(self: *Printer) avahi.Client.Handler { return .{ .context = self, .found = found, .lost = lost }; }
fn found(_: *anyopaque, s: *const avahi.Resolved) void { var buf: [1024]u8 = undefined; var w: Io.Writer = .fixed(&buf); w.print("found {s} {s} {s} {d}", .{ s.name, s.host, s.address, s.port }) catch {}; for (s.txt()) |t| w.print(" {s}", .{t}) catch {}; std.debug.print("{s}\n", .{w.buffered()}); }
fn lost(_: *anyopaque, item: *const avahi.Item) void { std.debug.print("lost {s}\n", .{item.name}); }};