bump product version to 6.3.0.0.beta1
[LibreOffice.git] / sd / source / ui / remotecontrol / AvahiNetworkService.cxx
blob72f27e98a2009ce9a2fefcd9fd5d4705d72da133
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
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/.
8 */
10 #include <config_dbus.h>
12 #include <time.h>
13 #include <iostream>
14 #include <limits>
15 #include <new>
16 #include <stdlib.h>
17 #include <assert.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>
29 #if ENABLE_DBUS
30 #include <dbus/dbus.h>
31 #endif
33 #include <sal/log.hxx>
35 #include "AvahiNetworkService.hxx"
36 #include "ZeroconfService.hxx"
38 using namespace sd;
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);
49 group = g;
51 /* Called whenever the entry group state changes */
53 switch (state) {
54 case AVAHI_ENTRY_GROUP_ESTABLISHED :
55 /* The entry group has been established successfully */
56 SAL_INFO( "sdremote.wifi", "Service '" << avahiService->getName() << "' successfully established." );
57 break;
59 case AVAHI_ENTRY_GROUP_COLLISION : {
60 char *n;
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));
71 break;
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);
80 break;
82 case AVAHI_ENTRY_GROUP_UNCOMMITED:
83 case AVAHI_ENTRY_GROUP_REGISTERING:
88 static bool create_services(AvahiClient *c) {
89 assert(c);
91 /* If this is the first time we're called, let's create a new
92 * entry group if necessary */
93 if(!client)
94 return false;
96 if (!group)
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();
100 return false;
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() << "'");
108 char r[128];
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
115 if (ret < 0) {
117 if (ret == AVAHI_ERR_COLLISION){
118 /* A service name collision with a local service happened. Let's
119 * pick a new name */
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();
132 return false;
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();
139 return false;
143 return true; //Services we're already created
146 static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) {
147 assert(c);
149 /* Called whenever the client or server state changes */
151 switch (state) {
152 case AVAHI_CLIENT_S_RUNNING:
153 create_services(c);
154 break;
155 case AVAHI_CLIENT_FAILURE:
156 SAL_WARN("sdremote.wifi", "Client failure: " << avahi_strerror(avahi_client_errno(c)));
157 avahiService->clear();
158 break;
159 case AVAHI_CLIENT_S_COLLISION:
160 case AVAHI_CLIENT_S_REGISTERING:
161 if (group)
162 avahi_entry_group_reset(group);
163 break;
164 case AVAHI_CLIENT_CONNECTING:
169 void AvahiNetworkService::setup() {
170 #if ENABLE_DBUS
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();
181 #endif
183 int error = 0;
184 avahiService = this;
185 if (!(threaded_poll = avahi_threaded_poll_new())) {
186 SAL_WARN("sdremote.wifi", "avahi_threaded_poll_new '" << avahiService->getName() << "' failed");
187 return;
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");
192 return;
195 if(!create_services(client))
196 return;
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");
201 return;
205 void AvahiNetworkService::clear() {
206 /* Call this when the app shuts down */
207 if(threaded_poll)
208 avahi_threaded_poll_stop(threaded_poll);
209 if(client)
210 avahi_client_free(client);
211 if(threaded_poll)
212 avahi_threaded_poll_free(threaded_poll);