LibreOffice Module sd (master) 1
AvahiNetworkService.cxx
Go to the documentation of this file.
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.
4 *
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
10#include <config_dbus.h>
11
12#include <iostream>
13#include <limits>
14#include <new>
15#include <assert.h>
16
17#include <avahi-client/client.h>
18#include <avahi-client/publish.h>
19
20#include <avahi-common/alternative.h>
21#include <avahi-common/error.h>
22#include <avahi-common/thread-watch.h>
23#include <comphelper/random.hxx>
24
25#if ENABLE_DBUS
26#include <dbus/dbus.h>
27#endif
28
29#include <sal/log.hxx>
30
32#include "ZeroconfService.hxx"
33
34using namespace sd;
35
36static AvahiClient *client = nullptr;
37static AvahiThreadedPoll *threaded_poll = nullptr;
38static AvahiEntryGroup *group = nullptr;
40
41static bool create_services(AvahiClient *c);
42
43static void entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, AVAHI_GCC_UNUSED void *userdata) {
44 assert(g == group || group == nullptr);
45 group = g;
46
47 /* Called whenever the entry group state changes */
48
49 switch (state) {
50 case AVAHI_ENTRY_GROUP_ESTABLISHED :
51 /* The entry group has been established successfully */
52 SAL_INFO( "sdremote.wifi", "Service '" << avahiService->getName() << "' successfully established." );
53 break;
54
55 case AVAHI_ENTRY_GROUP_COLLISION : {
56 char *n;
57
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());
62
63 SAL_INFO( "sdremote.wifi", "Service name collision, renaming service to '" << avahiService->getName() << "'");
64
65 /* And recreate the services */
66 create_services(avahi_entry_group_get_client(g));
67 break;
68 }
69
70 case AVAHI_ENTRY_GROUP_FAILURE :
71
72 SAL_WARN("sdremote.wifi", "Entry group failure: " << avahi_strerror(avahi_client_errno(avahi_entry_group_get_client(g))));
73
74 /* Some kind of failure happened while we were registering our services */
75 avahi_threaded_poll_quit(threaded_poll);
76 break;
77
78 case AVAHI_ENTRY_GROUP_UNCOMMITED:
79 case AVAHI_ENTRY_GROUP_REGISTERING:
80 ;
81 }
82}
83
84static bool create_services(AvahiClient *c) {
85 assert(c);
86
87 /* If this is the first time we're called, let's create a new
88 * entry group if necessary */
89 if(!client)
90 return false;
91
92 if (!group)
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)));
96 return false;
97 }
98
99 /* If the group is empty (either because it was just created, or
100 * because it was reset previously, add our entries. */
101
102 if (avahi_entry_group_is_empty(group)) {
103 SAL_INFO("sdremote.wifi", "Adding service '" << avahiService->getName() << "'");
104 char r[128];
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
110 );
111 if (ret < 0) {
112
113 if (ret == AVAHI_ERR_COLLISION){
114 /* A service name collision with a local service happened. Let's
115 * pick a new name */
116 char *n = avahi_alternative_service_name(avahiService->getName().c_str());
118
119 SAL_WARN("sdremote.wifi", "Service name collision, renaming service to '" << avahiService->getName() << "'");
120
121 avahi_entry_group_reset(group);
122
123 return create_services(c);
124 }
125
126 SAL_WARN("sdremote.wifi", "Failed to add _impressremote._tcp service: " << avahi_strerror(ret));
128 return false;
129 }
130
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));
135 return false;
136 }
137 }
138
139 return true; //Services we're already created
140}
141
142static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) {
143 assert(c);
144
145 /* Called whenever the client or server state changes */
146
147 switch (state) {
148 case AVAHI_CLIENT_S_RUNNING:
150 break;
151 case AVAHI_CLIENT_FAILURE:
152 SAL_WARN("sdremote.wifi", "Client failure: " << avahi_strerror(avahi_client_errno(c)));
154 break;
155 case AVAHI_CLIENT_S_COLLISION:
156 case AVAHI_CLIENT_S_REGISTERING:
157 if (group)
158 avahi_entry_group_reset(group);
159 break;
160 case AVAHI_CLIENT_CONNECTING:
161 ;
162 }
163}
164
165void AvahiNetworkService::setup() {
166#if ENABLE_DBUS
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.
170
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();
176 }
177#endif
178
179 int error = 0;
180 avahiService = this;
181 if (!(threaded_poll = avahi_threaded_poll_new())) {
182 SAL_WARN("sdremote.wifi", "avahi_threaded_poll_new '" << avahiService->getName() << "' failed");
183 return;
184 }
185
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");
188 return;
189 }
190
192 return;
193
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");
197 return;
198 }
199}
200
202 /* Call this when the app shuts down */
203 if(threaded_poll)
204 avahi_threaded_poll_stop(threaded_poll);
205 if(client)
206 avahi_client_free(client);
207 if(threaded_poll)
208 avahi_threaded_poll_free(threaded_poll);
209}
static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void *userdata)
static AvahiEntryGroup * group
static AvahiNetworkService * avahiService
static bool create_services(AvahiClient *c)
static AvahiClient * client
static void entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, AVAHI_GCC_UNUSED void *userdata)
static AvahiThreadedPoll * threaded_poll
#define kREG_TYPE
const std::string & getName() const
void setName(const char *n)
sal_Int64 n
#define SAL_WARN(area, stream)
#define SAL_INFO(area, stream)
int uniform_int_distribution(int a, int b)