bump product version to 5.0.4.1
[LibreOffice.git] / sd / source / ui / remotecontrol / AvahiNetworkService.cxx
blob895cf7cb3ba3027f9ca0d319652d5409712a2a44
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 */
9 #include <time.h>
10 #include <iostream>
11 #include <limits>
12 #include <new>
13 #include <stdlib.h>
14 #include <assert.h>
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"
32 using namespace sd;
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);
43 group = g;
45 /* Called whenever the entry group state changes */
47 switch (state) {
48 case AVAHI_ENTRY_GROUP_ESTABLISHED :
49 /* The entry group has been established successfully */
50 SAL_INFO( "sdremote.wifi", "Service '" << avahiService->getName() << "' successfully established." );
51 break;
53 case AVAHI_ENTRY_GROUP_COLLISION : {
54 char *n;
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));
65 break;
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);
74 break;
76 case AVAHI_ENTRY_GROUP_UNCOMMITED:
77 case AVAHI_ENTRY_GROUP_REGISTERING:
82 static bool create_services(AvahiClient *c) {
83 assert(c);
85 /* If this is the first time we're called, let's create a new
86 * entry group if necessary */
87 if(!client)
88 return false;
90 if (!group)
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();
94 return false;
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() << "'");
102 char r[128];
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
109 if (ret < 0) {
111 if (ret == AVAHI_ERR_COLLISION){
112 /* A service name collision with a local service happened. Let's
113 * pick a new name */
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();
126 return false;
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();
133 return false;
137 return true; //Services we're already created
140 static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) {
141 assert(c);
143 /* Called whenever the client or server state changes */
145 switch (state) {
146 case AVAHI_CLIENT_S_RUNNING:
147 create_services(c);
148 break;
149 case AVAHI_CLIENT_FAILURE:
150 SAL_WARN("sdremote.wifi", "Client failure: " << avahi_strerror(avahi_client_errno(c)));
151 avahiService->clear();
152 break;
153 case AVAHI_CLIENT_S_COLLISION:
154 case AVAHI_CLIENT_S_REGISTERING:
155 if (group)
156 avahi_entry_group_reset(group);
157 break;
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();
171 int error = 0;
172 avahiService = this;
173 if (!(threaded_poll = avahi_threaded_poll_new())) {
174 SAL_WARN("sdremote.wifi", "avahi_threaded_poll_new '" << avahiService->getName() << "' failed");
175 return;
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");
180 return;
183 if(!create_services(client))
184 return;
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");
189 return;
193 void AvahiNetworkService::clear() {
194 /* Call this when the app shuts down */
195 if(threaded_poll)
196 avahi_threaded_poll_stop(threaded_poll);
197 if(client)
198 avahi_client_free(client);
199 if(threaded_poll)
200 avahi_threaded_poll_free(threaded_poll);