1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
16 #include <avahi-client/client.h>
17 #include <avahi-client/publish.h>
19 #include <avahi-common/alternative.h>
20 #include <avahi-common/malloc.h>
21 #include <avahi-common/error.h>
22 #include <avahi-common/timeval.h>
23 #include <avahi-common/thread-watch.h>
24 #include <comphelper/random.hxx>
25 #include <dbus/dbus.h>
27 #include <sal/log.hxx>
29 #include "AvahiNetworkService.hxx"
30 #include "ZeroconfService.hxx"
34 static AvahiClient
*client
= NULL
;
35 static AvahiThreadedPoll
*threaded_poll
= NULL
;
36 static AvahiEntryGroup
*group
= NULL
;
37 static AvahiNetworkService
*avahiService
= NULL
;
39 static bool create_services(AvahiClient
*c
);
41 static void entry_group_callback(AvahiEntryGroup
*g
, AvahiEntryGroupState state
, AVAHI_GCC_UNUSED
void *userdata
) {
42 assert(g
== group
|| group
== NULL
);
45 /* Called whenever the entry group state changes */
48 case AVAHI_ENTRY_GROUP_ESTABLISHED
:
49 /* The entry group has been established successfully */
50 SAL_INFO( "sdremote.wifi", "Service '" << avahiService
->getName() << "' successfully established." );
53 case AVAHI_ENTRY_GROUP_COLLISION
: {
56 /* A service name collision with a remote service
57 * happened. Let's pick a new name */
58 n
= avahi_alternative_service_name(avahiService
->getName().c_str());
59 avahiService
->setName(n
);
61 SAL_INFO( "sdremote.wifi", "Service name collision, renaming service to '" << avahiService
->getName() << "'");
63 /* And recreate the services */
64 create_services(avahi_entry_group_get_client(g
));
68 case AVAHI_ENTRY_GROUP_FAILURE
:
70 SAL_WARN("sdremote.wifi", "Entry group failure: " << avahi_strerror(avahi_client_errno(avahi_entry_group_get_client(g
))));
72 /* Some kind of failure happened while we were registering our services */
73 avahi_threaded_poll_quit(threaded_poll
);
76 case AVAHI_ENTRY_GROUP_UNCOMMITED
:
77 case AVAHI_ENTRY_GROUP_REGISTERING
:
82 static bool create_services(AvahiClient
*c
) {
85 /* If this is the first time we're called, let's create a new
86 * entry group if necessary */
91 if (!(group
= avahi_entry_group_new(c
, entry_group_callback
, NULL
))) {
92 SAL_WARN("sdremote.wifi", "avahi_entry_group_new() failed: " << avahi_strerror(avahi_client_errno(c
)));
93 avahiService
->clear();
97 /* If the group is empty (either because it was just created, or
98 * because it was reset previously, add our entries. */
100 if (avahi_entry_group_is_empty(group
)) {
101 SAL_INFO("sdremote.wifi", "Adding service '" << avahiService
->getName() << "'");
103 int nRandom
= comphelper::rng::uniform_int_distribution(0, std::numeric_limits
<int>::max());
104 snprintf(r
, sizeof(r
), "random=%i", nRandom
);
105 int ret
= avahi_entry_group_add_service(
106 group
, AVAHI_IF_UNSPEC
, AVAHI_PROTO_UNSPEC
, static_cast<AvahiPublishFlags
>(0),
107 avahiService
->getName().c_str(), kREG_TYPE
, NULL
, NULL
, 1599, "local", r
, NULL
111 if (ret
== AVAHI_ERR_COLLISION
){
112 /* A service name collision with a local service happened. Let's
114 char *n
= avahi_alternative_service_name(avahiService
->getName().c_str());
115 avahiService
->setName(n
);
117 SAL_WARN("sdremote.wifi", "Service name collision, renaming service to '" << avahiService
->getName() << "'");
119 avahi_entry_group_reset(group
);
121 return create_services(c
);
124 SAL_WARN("sdremote.wifi", "Failed to add _impressremote._tcp service: " << avahi_strerror(ret
));
125 avahiService
->clear();
129 /* Tell the server to register the service */
130 if ((ret
= avahi_entry_group_commit(group
)) < 0) {
131 SAL_WARN("sdremote.wifi", "Failed to commit entry group: " << avahi_strerror(ret
));
132 avahiService
->clear();
137 return true; //Services we're already created
140 static void client_callback(AvahiClient
*c
, AvahiClientState state
, AVAHI_GCC_UNUSED
void * userdata
) {
143 /* Called whenever the client or server state changes */
146 case AVAHI_CLIENT_S_RUNNING
:
149 case AVAHI_CLIENT_FAILURE
:
150 SAL_WARN("sdremote.wifi", "Client failure: " << avahi_strerror(avahi_client_errno(c
)));
151 avahiService
->clear();
153 case AVAHI_CLIENT_S_COLLISION
:
154 case AVAHI_CLIENT_S_REGISTERING
:
156 avahi_entry_group_reset(group
);
158 case AVAHI_CLIENT_CONNECTING
:
163 void AvahiNetworkService::setup() {
164 // Avahi internally uses D-Bus, which requires the following in order to be
165 // thread-safe (and we potentially access D-Bus from different threads in
166 // different places of the code base):
167 if (!dbus_threads_init_default()) {
168 throw std::bad_alloc();
173 if (!(threaded_poll
= avahi_threaded_poll_new())) {
174 SAL_WARN("sdremote.wifi", "avahi_threaded_poll_new '" << avahiService
->getName() << "' failed");
178 if (!(client
= avahi_client_new(avahi_threaded_poll_get(threaded_poll
), static_cast<AvahiClientFlags
>(0), client_callback
, NULL
, &error
))) {
179 SAL_WARN("sdremote.wifi", "avahi_client_new failed");
183 if(!create_services(client
))
186 /* Finally, start the event loop thread */
187 if (avahi_threaded_poll_start(threaded_poll
) < 0) {
188 SAL_WARN("sdremote.wifi", "avahi_threaded_poll_start failed");
193 void AvahiNetworkService::clear() {
194 /* Call this when the app shuts down */
196 avahi_threaded_poll_stop(threaded_poll
);
198 avahi_client_free(client
);
200 avahi_threaded_poll_free(threaded_poll
);