1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 #include <config_dbus.h>
19 #include <avahi-client/client.h>
20 #include <avahi-client/publish.h>
22 #include <avahi-common/alternative.h>
23 #include <avahi-common/malloc.h>
24 #include <avahi-common/error.h>
25 #include <avahi-common/timeval.h>
26 #include <avahi-common/thread-watch.h>
27 #include <comphelper/random.hxx>
30 #include <dbus/dbus.h>
33 #include <sal/log.hxx>
35 #include "AvahiNetworkService.hxx"
36 #include "ZeroconfService.hxx"
40 static AvahiClient
*client
= nullptr;
41 static AvahiThreadedPoll
*threaded_poll
= nullptr;
42 static AvahiEntryGroup
*group
= nullptr;
43 static AvahiNetworkService
*avahiService
= nullptr;
45 static bool create_services(AvahiClient
*c
);
47 static void entry_group_callback(AvahiEntryGroup
*g
, AvahiEntryGroupState state
, AVAHI_GCC_UNUSED
void *userdata
) {
48 assert(g
== group
|| group
== nullptr);
51 /* Called whenever the entry group state changes */
54 case AVAHI_ENTRY_GROUP_ESTABLISHED
:
55 /* The entry group has been established successfully */
56 SAL_INFO( "sdremote.wifi", "Service '" << avahiService
->getName() << "' successfully established." );
59 case AVAHI_ENTRY_GROUP_COLLISION
: {
62 /* A service name collision with a remote service
63 * happened. Let's pick a new name */
64 n
= avahi_alternative_service_name(avahiService
->getName().c_str());
65 avahiService
->setName(n
);
67 SAL_INFO( "sdremote.wifi", "Service name collision, renaming service to '" << avahiService
->getName() << "'");
69 /* And recreate the services */
70 create_services(avahi_entry_group_get_client(g
));
74 case AVAHI_ENTRY_GROUP_FAILURE
:
76 SAL_WARN("sdremote.wifi", "Entry group failure: " << avahi_strerror(avahi_client_errno(avahi_entry_group_get_client(g
))));
78 /* Some kind of failure happened while we were registering our services */
79 avahi_threaded_poll_quit(threaded_poll
);
82 case AVAHI_ENTRY_GROUP_UNCOMMITED
:
83 case AVAHI_ENTRY_GROUP_REGISTERING
:
88 static bool create_services(AvahiClient
*c
) {
91 /* If this is the first time we're called, let's create a new
92 * entry group if necessary */
97 if (!(group
= avahi_entry_group_new(c
, entry_group_callback
, nullptr))) {
98 SAL_WARN("sdremote.wifi", "avahi_entry_group_new() failed: " << avahi_strerror(avahi_client_errno(c
)));
99 avahiService
->clear();
103 /* If the group is empty (either because it was just created, or
104 * because it was reset previously, add our entries. */
106 if (avahi_entry_group_is_empty(group
)) {
107 SAL_INFO("sdremote.wifi", "Adding service '" << avahiService
->getName() << "'");
109 int nRandom
= comphelper::rng::uniform_int_distribution(0, std::numeric_limits
<int>::max());
110 snprintf(r
, sizeof(r
), "random=%i", nRandom
);
111 int ret
= avahi_entry_group_add_service(
112 group
, AVAHI_IF_UNSPEC
, AVAHI_PROTO_UNSPEC
, static_cast<AvahiPublishFlags
>(0),
113 avahiService
->getName().c_str(), kREG_TYPE
, nullptr, nullptr, 1599, "local", r
, nullptr
117 if (ret
== AVAHI_ERR_COLLISION
){
118 /* A service name collision with a local service happened. Let's
120 char *n
= avahi_alternative_service_name(avahiService
->getName().c_str());
121 avahiService
->setName(n
);
123 SAL_WARN("sdremote.wifi", "Service name collision, renaming service to '" << avahiService
->getName() << "'");
125 avahi_entry_group_reset(group
);
127 return create_services(c
);
130 SAL_WARN("sdremote.wifi", "Failed to add _impressremote._tcp service: " << avahi_strerror(ret
));
131 avahiService
->clear();
135 /* Tell the server to register the service */
136 if ((ret
= avahi_entry_group_commit(group
)) < 0) {
137 SAL_WARN("sdremote.wifi", "Failed to commit entry group: " << avahi_strerror(ret
));
138 avahiService
->clear();
143 return true; //Services we're already created
146 static void client_callback(AvahiClient
*c
, AvahiClientState state
, AVAHI_GCC_UNUSED
void * userdata
) {
149 /* Called whenever the client or server state changes */
152 case AVAHI_CLIENT_S_RUNNING
:
155 case AVAHI_CLIENT_FAILURE
:
156 SAL_WARN("sdremote.wifi", "Client failure: " << avahi_strerror(avahi_client_errno(c
)));
157 avahiService
->clear();
159 case AVAHI_CLIENT_S_COLLISION
:
160 case AVAHI_CLIENT_S_REGISTERING
:
162 avahi_entry_group_reset(group
);
164 case AVAHI_CLIENT_CONNECTING
:
169 void AvahiNetworkService::setup() {
171 // Sure, without ENABLE_DBUS it probably makes no sense to try to use this Avahi stuff either,
172 // but this is just a stop-gap measure to get this to even compile for now with the probably
173 // pointless combination of configurable options --enable-avahi --enable-dbus --disable-gui.
175 // Avahi internally uses D-Bus, which requires the following in order to be
176 // thread-safe (and we potentially access D-Bus from different threads in
177 // different places of the code base):
178 if (!dbus_threads_init_default()) {
179 throw std::bad_alloc();
185 if (!(threaded_poll
= avahi_threaded_poll_new())) {
186 SAL_WARN("sdremote.wifi", "avahi_threaded_poll_new '" << avahiService
->getName() << "' failed");
190 if (!(client
= avahi_client_new(avahi_threaded_poll_get(threaded_poll
), static_cast<AvahiClientFlags
>(0), client_callback
, nullptr, &error
))) {
191 SAL_WARN("sdremote.wifi", "avahi_client_new failed");
195 if(!create_services(client
))
198 /* Finally, start the event loop thread */
199 if (avahi_threaded_poll_start(threaded_poll
) < 0) {
200 SAL_WARN("sdremote.wifi", "avahi_threaded_poll_start failed");
205 void AvahiNetworkService::clear() {
206 /* Call this when the app shuts down */
208 avahi_threaded_poll_stop(threaded_poll
);
210 avahi_client_free(client
);
212 avahi_threaded_poll_free(threaded_poll
);