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>
17 #include <avahi-client/client.h>
18 #include <avahi-client/publish.h>
20 #include <avahi-common/alternative.h>
21 #include <avahi-common/error.h>
22 #include <avahi-common/thread-watch.h>
23 #include <comphelper/random.hxx>
26 #include <dbus/dbus.h>
29 #include <sal/log.hxx>
31 #include "AvahiNetworkService.hxx"
32 #include "ZeroconfService.hxx"
36 static AvahiClient
*client
= nullptr;
37 static AvahiThreadedPoll
*threaded_poll
= nullptr;
38 static AvahiEntryGroup
*group
= nullptr;
39 static AvahiNetworkService
*avahiService
= nullptr;
41 static bool create_services(AvahiClient
*c
);
43 static void entry_group_callback(AvahiEntryGroup
*g
, AvahiEntryGroupState state
, AVAHI_GCC_UNUSED
void *userdata
) {
44 assert(g
== group
|| group
== nullptr);
47 /* Called whenever the entry group state changes */
50 case AVAHI_ENTRY_GROUP_ESTABLISHED
:
51 /* The entry group has been established successfully */
52 SAL_INFO( "sdremote.wifi", "Service '" << avahiService
->getName() << "' successfully established." );
55 case AVAHI_ENTRY_GROUP_COLLISION
: {
58 /* A service name collision with a remote service
59 * happened. Let's pick a new name */
60 n
= avahi_alternative_service_name(avahiService
->getName().c_str());
61 avahiService
->setName(n
);
63 SAL_INFO( "sdremote.wifi", "Service name collision, renaming service to '" << avahiService
->getName() << "'");
65 /* And recreate the services */
66 create_services(avahi_entry_group_get_client(g
));
70 case AVAHI_ENTRY_GROUP_FAILURE
:
72 SAL_WARN("sdremote.wifi", "Entry group failure: " << avahi_strerror(avahi_client_errno(avahi_entry_group_get_client(g
))));
74 /* Some kind of failure happened while we were registering our services */
75 avahi_threaded_poll_quit(threaded_poll
);
78 case AVAHI_ENTRY_GROUP_UNCOMMITED
:
79 case AVAHI_ENTRY_GROUP_REGISTERING
:
84 static bool create_services(AvahiClient
*c
) {
87 /* If this is the first time we're called, let's create a new
88 * entry group if necessary */
93 if (!(group
= avahi_entry_group_new(c
, entry_group_callback
, nullptr))) {
94 SAL_WARN("sdremote.wifi", "avahi_entry_group_new() failed: " << avahi_strerror(avahi_client_errno(c
)));
95 avahiService
->clear();
99 /* If the group is empty (either because it was just created, or
100 * because it was reset previously, add our entries. */
102 if (avahi_entry_group_is_empty(group
)) {
103 SAL_INFO("sdremote.wifi", "Adding service '" << avahiService
->getName() << "'");
105 int nRandom
= comphelper::rng::uniform_int_distribution(0, std::numeric_limits
<int>::max());
106 snprintf(r
, sizeof(r
), "random=%i", nRandom
);
107 int ret
= avahi_entry_group_add_service(
108 group
, AVAHI_IF_UNSPEC
, AVAHI_PROTO_UNSPEC
, static_cast<AvahiPublishFlags
>(0),
109 avahiService
->getName().c_str(), kREG_TYPE
, nullptr, nullptr, 1599, "local", r
, nullptr
113 if (ret
== AVAHI_ERR_COLLISION
){
114 /* A service name collision with a local service happened. Let's
116 char *n
= avahi_alternative_service_name(avahiService
->getName().c_str());
117 avahiService
->setName(n
);
119 SAL_WARN("sdremote.wifi", "Service name collision, renaming service to '" << avahiService
->getName() << "'");
121 avahi_entry_group_reset(group
);
123 return create_services(c
);
126 SAL_WARN("sdremote.wifi", "Failed to add _impressremote._tcp service: " << avahi_strerror(ret
));
127 avahiService
->clear();
131 /* Tell the server to register the service */
132 if ((ret
= avahi_entry_group_commit(group
)) < 0) {
133 SAL_WARN("sdremote.wifi", "Failed to commit entry group: " << avahi_strerror(ret
));
134 avahiService
->clear();
139 return true; //Services we're already created
142 static void client_callback(AvahiClient
*c
, AvahiClientState state
, AVAHI_GCC_UNUSED
void * userdata
) {
145 /* Called whenever the client or server state changes */
148 case AVAHI_CLIENT_S_RUNNING
:
151 case AVAHI_CLIENT_FAILURE
:
152 SAL_WARN("sdremote.wifi", "Client failure: " << avahi_strerror(avahi_client_errno(c
)));
153 avahiService
->clear();
155 case AVAHI_CLIENT_S_COLLISION
:
156 case AVAHI_CLIENT_S_REGISTERING
:
158 avahi_entry_group_reset(group
);
160 case AVAHI_CLIENT_CONNECTING
:
165 void AvahiNetworkService::setup() {
167 // Sure, without ENABLE_DBUS it probably makes no sense to try to use this Avahi stuff either,
168 // but this is just a stop-gap measure to get this to even compile for now with the probably
169 // pointless combination of configurable options --enable-avahi --enable-dbus --disable-gui.
171 // Avahi internally uses D-Bus, which requires the following in order to be
172 // thread-safe (and we potentially access D-Bus from different threads in
173 // different places of the code base):
174 if (!dbus_threads_init_default()) {
175 throw std::bad_alloc();
181 if (!(threaded_poll
= avahi_threaded_poll_new())) {
182 SAL_WARN("sdremote.wifi", "avahi_threaded_poll_new '" << avahiService
->getName() << "' failed");
186 if (!(client
= avahi_client_new(avahi_threaded_poll_get(threaded_poll
), static_cast<AvahiClientFlags
>(0), client_callback
, nullptr, &error
))) {
187 SAL_WARN("sdremote.wifi", "avahi_client_new failed");
191 if(!create_services(client
))
194 /* Finally, start the event loop thread */
195 if (avahi_threaded_poll_start(threaded_poll
) < 0) {
196 SAL_WARN("sdremote.wifi", "avahi_threaded_poll_start failed");
201 void AvahiNetworkService::clear() {
202 /* Call this when the app shuts down */
204 avahi_threaded_poll_stop(threaded_poll
);
206 avahi_client_free(client
);
208 avahi_threaded_poll_free(threaded_poll
);