17using namespace std::literals;
88 constexpr auto IF_UNSPEC = -1;
141 using Protocol = int;
148 typedef void (*ClientCallback)(Client *, ClientState,
void *userdata);
149 typedef void (*EntryGroupCallback)(EntryGroup *g, EntryGroupState state,
void *userdata);
151 typedef void (*free_fn)(
void *);
153 typedef Client *(*client_new_fn)(
const Poll *poll_api, ClientFlags flags, ClientCallback callback,
void *userdata,
int *error);
154 typedef void (*client_free_fn)(Client *);
155 typedef char *(*alternative_service_name_fn)(
char *);
157 typedef Client *(*entry_group_get_client_fn)(EntryGroup *);
159 typedef EntryGroup *(*entry_group_new_fn)(Client *, EntryGroupCallback,
void *userdata);
160 typedef int (*entry_group_add_service_fn)(
173 typedef int (*entry_group_is_empty_fn)(EntryGroup *);
174 typedef int (*entry_group_reset_fn)(EntryGroup *);
175 typedef int (*entry_group_commit_fn)(EntryGroup *);
177 typedef char *(*strdup_fn)(
const char *);
178 typedef char *(*strerror_fn)(int);
179 typedef int (*client_errno_fn)(Client *);
181 typedef Poll *(*simple_poll_get_fn)(SimplePoll *);
182 typedef int (*simple_poll_loop_fn)(SimplePoll *);
183 typedef void (*simple_poll_quit_fn)(SimplePoll *);
184 typedef SimplePoll *(*simple_poll_new_fn)();
185 typedef void (*simple_poll_free_fn)(SimplePoll *);
188 client_new_fn client_new;
189 client_free_fn client_free;
190 alternative_service_name_fn alternative_service_name;
191 entry_group_get_client_fn entry_group_get_client;
192 entry_group_new_fn entry_group_new;
193 entry_group_add_service_fn entry_group_add_service;
194 entry_group_is_empty_fn entry_group_is_empty;
195 entry_group_reset_fn entry_group_reset;
196 entry_group_commit_fn entry_group_commit;
198 strerror_fn strerror;
199 client_errno_fn client_errno;
200 simple_poll_get_fn simple_poll_get;
201 simple_poll_loop_fn simple_poll_loop;
202 simple_poll_quit_fn simple_poll_quit;
203 simple_poll_new_fn simple_poll_new;
204 simple_poll_free_fn simple_poll_free;
207 static void *handle {
nullptr};
208 static bool funcs_loaded =
false;
215 handle = dyn::handle({
"libavahi-common.so.3",
"libavahi-common.so"});
221 std::vector<std::tuple<dyn::apiproc *, const char *>> funcs {
222 {(dyn::apiproc *) &alternative_service_name,
"avahi_alternative_service_name"},
223 {(dyn::apiproc *) &free,
"avahi_free"},
224 {(dyn::apiproc *) &strdup,
"avahi_strdup"},
225 {(dyn::apiproc *) &strerror,
"avahi_strerror"},
226 {(dyn::apiproc *) &simple_poll_get,
"avahi_simple_poll_get"},
227 {(dyn::apiproc *) &simple_poll_loop,
"avahi_simple_poll_loop"},
228 {(dyn::apiproc *) &simple_poll_quit,
"avahi_simple_poll_quit"},
229 {(dyn::apiproc *) &simple_poll_new,
"avahi_simple_poll_new"},
230 {(dyn::apiproc *) &simple_poll_free,
"avahi_simple_poll_free"},
233 if (dyn::load(handle, funcs)) {
246 static void *handle {
nullptr};
247 static bool funcs_loaded =
false;
254 handle = dyn::handle({
"libavahi-client.so.3",
"libavahi-client.so"});
260 std::vector<std::tuple<dyn::apiproc *, const char *>> funcs {
261 {(dyn::apiproc *) &client_new,
"avahi_client_new"},
262 {(dyn::apiproc *) &client_free,
"avahi_client_free"},
263 {(dyn::apiproc *) &entry_group_get_client,
"avahi_entry_group_get_client"},
264 {(dyn::apiproc *) &entry_group_new,
"avahi_entry_group_new"},
265 {(dyn::apiproc *) &entry_group_add_service,
"avahi_entry_group_add_service"},
266 {(dyn::apiproc *) &entry_group_is_empty,
"avahi_entry_group_is_empty"},
267 {(dyn::apiproc *) &entry_group_reset,
"avahi_entry_group_reset"},
268 {(dyn::apiproc *) &entry_group_commit,
"avahi_entry_group_commit"},
269 {(dyn::apiproc *) &client_errno,
"avahi_client_errno"},
272 if (dyn::load(handle, funcs)) {
281namespace platf::publish {
289 using ptr_t = util::safe_ptr<T, free<T>>;
290 using client_t = util::dyn_safe_ptr<avahi::Client, &avahi::client_free>;
291 using poll_t = util::dyn_safe_ptr<avahi::SimplePoll, &avahi::simple_poll_free>;
293 avahi::EntryGroup *group =
nullptr;
300 void create_services(avahi::Client *c);
302 void entry_group_callback(avahi::EntryGroup *g, avahi::EntryGroupState state,
void *) {
307 BOOST_LOG(info) <<
"Avahi service " << name.get() <<
" successfully established.";
310 name.reset(avahi::alternative_service_name(name.get()));
312 BOOST_LOG(info) <<
"Avahi service name collision, renaming service to " << name.get();
314 create_services(avahi::entry_group_get_client(g));
317 BOOST_LOG(error) <<
"Avahi entry group failure: " << avahi::strerror(avahi::client_errno(avahi::entry_group_get_client(g)));
318 avahi::simple_poll_quit(poll.get());
325 void create_services(avahi::Client *c) {
328 auto fg = util::fail_guard([]() {
329 avahi::simple_poll_quit(poll.get());
333 if (!(group = avahi::entry_group_new(c, entry_group_callback,
nullptr))) {
334 BOOST_LOG(error) <<
"avahi::entry_group_new() failed: "sv << avahi::strerror(avahi::client_errno(c));
339 if (avahi::entry_group_is_empty(group)) {
340 BOOST_LOG(info) <<
"Adding avahi service "sv << name.get();
342 ret = avahi::entry_group_add_service(
346 avahi::PublishFlags(0),
356 if (ret == avahi::ERR_COLLISION) {
358 name.reset(avahi::alternative_service_name(name.get()));
359 BOOST_LOG(info) <<
"Service name collision, renaming service to "sv << name.get();
361 avahi::entry_group_reset(group);
369 BOOST_LOG(error) <<
"Failed to add "sv << SERVICE_TYPE <<
" service: "sv << avahi::strerror(ret);
373 ret = avahi::entry_group_commit(group);
375 BOOST_LOG(error) <<
"Failed to commit entry group: "sv << avahi::strerror(ret);
383 void client_callback(avahi::Client *c, avahi::ClientState state,
void *) {
389 BOOST_LOG(error) <<
"Client failure: "sv << avahi::strerror(avahi::client_errno(c));
390 avahi::simple_poll_quit(poll.get());
395 avahi::entry_group_reset(group);
404 std::thread poll_thread;
406 deinit_t(std::thread poll_thread):
407 poll_thread {std::move(poll_thread)} {
410 ~deinit_t()
override {
411 if (avahi::simple_poll_quit && poll) {
412 avahi::simple_poll_quit(poll.get());
415 if (poll_thread.joinable()) {
421 [[nodiscard]] std::unique_ptr<::platf::deinit_t> start() {
422 if (avahi::init_client()) {
428 poll.reset(avahi::simple_poll_new());
430 BOOST_LOG(error) <<
"Failed to create simple poll object."sv;
435 name.reset(avahi::strdup(instance_name.c_str()));
438 avahi::client_new(avahi::simple_poll_get(poll.get()),
avahi::ClientFlags(0), client_callback,
nullptr, &avhi_error)
442 BOOST_LOG(error) <<
"Failed to create client: "sv << avahi::strerror(avhi_error);
446 return std::make_unique<deinit_t>(std::thread {avahi::simple_poll_loop, poll.get()});
Declarations for common platform specific utilities.
std::string get_host_name()
Returns the current computer name in UTF-8.
Declarations for logging related functions.
Miscellaneous declarations for Linux.
constexpr auto PORT_HTTP
The HTTP port, as a difference from the config port.
std::string mdns_instance_name(const std::string_view &hostname)
Returns a string for use as the instance name for mDNS.
Declarations for networking related functions.
Declarations for the nvhttp (GameStream) server.
@ PROTO_UNSPEC
Unspecified/all protocol(s).
@ SERVER_INVALID
Invalid state (initial).
@ SERVER_FAILURE
Some fatal failure happened, the server is unable to proceed.
@ SERVER_COLLISION
There is a collision with a host RR. All host RRs have been withdrawn, the user should set a new host...
@ SERVER_REGISTERING
Host RRs are being registered.
@ SERVER_RUNNING
All host RRs have been established.
@ CLIENT_S_COLLISION
Server state: COLLISION.
@ CLIENT_FAILURE
Some kind of error happened on the client side.
@ CLIENT_CONNECTING
We're still connecting. This state is only entered when AVAHI_CLIENT_NO_FAIL has been passed to avahi...
@ CLIENT_S_REGISTERING
Server state: REGISTERING.
@ CLIENT_S_RUNNING
Server state: RUNNING.
@ CLIENT_IGNORE_USER_CONFIG
Don't read user configuration.
@ CLIENT_NO_FAIL
Don't fail if the daemon is not available when avahi_client_new() is called, instead enter CLIENT_CON...
PublishFlags
Flags for publishing functions.
@ PUBLISH_NO_REVERSE
For address records: don't create a reverse (PTR) entry.
@ PUBLISH_UPDATE
Update existing records instead of adding new ones.
@ PUBLISH_NO_COOKIE
For service records: do not implicitly add the local service cookie to TXT data.
@ PUBLISH_NO_PROBE
For raw records: Though the RRset is intended to be unique no probes shall be sent.
@ PUBLISH_ALLOW_MULTIPLE
For raw records: Allow multiple local records of this type, even if they are intended to be unique.
@ PUBLISH_UNIQUE
For raw records: The RRset is intended to be unique.
@ PUBLISH_USE_WIDE_AREA
Register the record using wide area DNS (i.e. unicast DNS update).
@ PUBLISH_USE_MULTICAST
Register the record using multicast DNS.
@ PUBLISH_NO_ANNOUNCE
For raw records: Do not announce this RR to other hosts.
@ ENTRY_GROUP_COLLISION
A name collision for one of the entries in the group has been detected, the entries have been withdra...
@ ENTRY_GROUP_UNCOMMITED
The group has not yet been committed, the user must still call avahi_entry_group_commit().
@ ENTRY_GROUP_ESTABLISHED
The entries have successfully been established.
@ ENTRY_GROUP_FAILURE
Some kind of failure happened, the entries have been withdrawn.
@ ENTRY_GROUP_REGISTERING
The entries of the group are currently being registered.
err_e
Error codes used by avahi.
@ ERR_DNS_SERVFAIL
DNS Error: Server Failure.
@ ERR_INVALID_DNS_TYPE
Invalid DNS type.
@ ERR_INVALID_CONFIG
Configuration error.
@ ERR_INVALID_KEY
Invalid key.
@ ERR_INVALID_HOST_NAME
Invalid host name.
@ ERR_NO_DAEMON
Daemon not running.
@ ERR_NOT_FOUND
Not found.
@ ERR_NOT_PERMITTED
Operation not permitted.
@ ERR_INVALID_SERVICE_NAME
Invalid service name.
@ ERR_NOT_SUPPORTED
Not supported.
@ ERR_DNS_NXDOMAIN
DNS Error: No such domain.
@ ERR_INVALID_OBJECT
The object passed to this function was invalid.
@ ERR_NO_NETWORK
No suitable network protocol available.
@ ERR_DISCONNECTED
Daemon connection failed.
@ ERR_IS_PATTERN
RR key is pattern.
@ ERR_ACCESS_DENIED
Access denied.
@ ERR_DNS_NOTAUTH
DNS Error: Not authorized.
@ ERR_TIMEOUT
Timeout reached.
@ ERR_INVALID_RDATA
Invalid RDATA.
@ ERR_INVALID_DOMAIN_NAME
Invalid domain name.
@ ERR_INVALID_INTERFACE
Invalid interface.
@ ERR_INVALID_PORT
Invalid port number.
@ ERR_INVALID_ADDRESS
Invalid address.
@ ERR_NO_MEMORY
Memory exhausted.
@ ERR_INVALID_OPERATION
Invalid operation.
@ ERR_INVALID_SERVICE_SUBTYPE
Invalid service subtype.
@ ERR_BAD_STATE
Object was in a bad state.
@ ERR_INVALID_ARGUMENT
Invalid argument.
@ ERR_DBUS_ERROR
An unexpected D-Bus error occurred.
@ ERR_TOO_MANY_CLIENTS
Too many clients.
@ ERR_TOO_MANY_ENTRIES
Too many entries.
@ ERR_INVALID_PACKET
Invalid packet.
@ ERR_VERSION_MISMATCH
Version mismatch.
@ ERR_INVALID_DNS_ERROR
Invalid DNS return code.
@ ERR_INVALID_TTL
Invalid DNS TTL.
@ ERR_INVALID_DNS_CLASS
Invalid DNS class.
@ ERR_INVALID_SERVICE_TYPE
Invalid service type.
@ ERR_NO_CHANGE
The requested operation is invalid because it is redundant.
@ ERR_DNS_NOTIMP
DNS Error: Not implemented.
@ ERR_FAILURE
Generic error code.
@ ERR_COLLISION
Name collision.
@ ERR_INVALID_FLAGS
Invalid flags.
@ ERR_INVALID_PROTOCOL
Invalid protocol.
@ ERR_DNS_FORMERR
DNS Error: Form error.
@ ERR_INVALID_RECORD
Invalid RR.
@ ERR_DNS_REFUSED
DNS Error: Operation refused.
@ ERR_TOO_MANY_OBJECTS
Too many objects.
Declarations for utility functions.