1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
/*
* Remmina - The GTK+ Remote Desktop Client
* Copyright (C) 2009-2010 Vic Lee
* Copyright (C) 2014-2015 Antenore Gatta, Fabio Castelli, Giovanni Panozzo
* Copyright (C) 2016-2023 Antenore Gatta, Giovanni Panozzo
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
* You must obey the GNU General Public License in all respects
* for all of the code used other than OpenSSL. * If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. * If you
* do not wish to do so, delete this exception statement from your
* version. * If you delete this exception statement from all source
* files in the program, then also delete it here.
*
*/
#include "config.h"
#include "remmina_avahi.h"
#include "remmina/remmina_trace_calls.h"
#ifdef HAVE_LIBAVAHI_CLIENT
#include <avahi-client/client.h>
#include <avahi-client/lookup.h>
#include <avahi-common/simple-watch.h>
#include <avahi-common/malloc.h>
#include <avahi-common/error.h>
struct _RemminaAvahiPriv {
AvahiSimplePoll* simple_poll;
AvahiClient* client;
AvahiServiceBrowser* sb;
guint iterate_handler;
gboolean has_event;
};
static void
remmina_avahi_resolve_callback(
AvahiServiceResolver* r,
AVAHI_GCC_UNUSED AvahiIfIndex interface,
AVAHI_GCC_UNUSED AvahiProtocol protocol,
AvahiResolverEvent event,
const char* name,
const char* type,
const char* domain,
const char* host_name,
const AvahiAddress* address,
uint16_t port,
AvahiStringList* txt,
AvahiLookupResultFlags flags,
AVAHI_GCC_UNUSED void* userdata)
{
TRACE_CALL(__func__);
gchar* key;
gchar* value;
RemminaAvahi* ga = (RemminaAvahi*)userdata;
assert(r);
ga->priv->has_event = TRUE;
switch (event) {
case AVAHI_RESOLVER_FAILURE:
g_print("(remmina-applet avahi-resolver) Failed to resolve service '%s' of type '%s' in domain '%s': %s\n",
name, type, domain, avahi_strerror(avahi_client_errno(avahi_service_resolver_get_client(r))));
break;
case AVAHI_RESOLVER_FOUND:
key = g_strdup_printf("%s,%s,%s", name, type, domain);
if (g_hash_table_lookup(ga->discovered_services, key)) {
g_free(key);
break;
}
value = g_strdup_printf("[%s]:%i", host_name, port);
g_hash_table_insert(ga->discovered_services, key, value);
/* key and value will be freed with g_free when the has table is freed */
g_print("(remmina-applet avahi-resolver) Added service '%s'\n", value);
break;
}
avahi_service_resolver_free(r);
}
static void
remmina_avahi_browse_callback(
AvahiServiceBrowser* b,
AvahiIfIndex interface,
AvahiProtocol protocol,
AvahiBrowserEvent event,
const char* name,
const char* type,
const char* domain,
AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
void* userdata)
{
TRACE_CALL(__func__);
gchar* key;
RemminaAvahi* ga = (RemminaAvahi*)userdata;
assert(b);
ga->priv->has_event = TRUE;
switch (event) {
case AVAHI_BROWSER_FAILURE:
g_print("(remmina-applet avahi-browser) %s\n",
avahi_strerror(avahi_client_errno(avahi_service_browser_get_client(b))));
return;
case AVAHI_BROWSER_NEW:
key = g_strdup_printf("%s,%s,%s", name, type, domain);
if (g_hash_table_lookup(ga->discovered_services, key)) {
g_free(key);
break;
}
g_free(key);
g_print("(remmina-applet avahi-browser) Found service '%s' of type '%s' in domain '%s'\n", name, type, domain);
if (!(avahi_service_resolver_new(ga->priv->client, interface, protocol, name, type, domain,
AVAHI_PROTO_UNSPEC, 0, remmina_avahi_resolve_callback, ga))) {
g_print("(remmina-applet avahi-browser) Failed to resolve service '%s': %s\n",
name, avahi_strerror(avahi_client_errno(ga->priv->client)));
}
break;
case AVAHI_BROWSER_REMOVE:
g_print("(remmina-applet avahi-browser) Removed service '%s' of type '%s' in domain '%s'\n", name, type, domain);
key = g_strdup_printf("%s,%s,%s", name, type, domain);
g_hash_table_remove(ga->discovered_services, key);
g_free(key);
break;
case AVAHI_BROWSER_ALL_FOR_NOW:
case AVAHI_BROWSER_CACHE_EXHAUSTED:
break;
}
}
static void remmina_avahi_client_callback(AvahiClient* c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata)
{
TRACE_CALL(__func__);
RemminaAvahi* ga = (RemminaAvahi*)userdata;
ga->priv->has_event = TRUE;
if (state == AVAHI_CLIENT_FAILURE) {
g_print("(remmina-applet avahi) Server connection failure: %s\n", avahi_strerror(avahi_client_errno(c)));
}
}
static gboolean remmina_avahi_iterate(RemminaAvahi* ga)
{
TRACE_CALL(__func__);
while (TRUE) {
/* Call the iteration until no further events */
ga->priv->has_event = FALSE;
avahi_simple_poll_iterate(ga->priv->simple_poll, 0);
if (!ga->priv->has_event)
break;
}
return TRUE;
}
RemminaAvahi* remmina_avahi_new(void)
{
TRACE_CALL(__func__);
RemminaAvahi* ga;
ga = g_new(RemminaAvahi, 1);
ga->discovered_services = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
ga->started = FALSE;
ga->priv = g_new(RemminaAvahiPriv, 1);
ga->priv->simple_poll = NULL;
ga->priv->client = NULL;
ga->priv->sb = NULL;
ga->priv->iterate_handler = 0;
ga->priv->has_event = FALSE;
return ga;
}
void remmina_avahi_start(RemminaAvahi* ga)
{
TRACE_CALL(__func__);
int error;
if (ga->started)
return;
ga->started = TRUE;
ga->priv->simple_poll = avahi_simple_poll_new();
if (!ga->priv->simple_poll) {
g_print("Failed to create simple poll object.\n");
return;
}
ga->priv->client = avahi_client_new(avahi_simple_poll_get(ga->priv->simple_poll), 0, remmina_avahi_client_callback, ga,
&error);
if (!ga->priv->client) {
g_print("Failed to create client: %s\n", avahi_strerror(error));
return;
}
/** @todo Customize the default domain here */
ga->priv->sb = avahi_service_browser_new(ga->priv->client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_rfb._tcp", NULL, 0,
remmina_avahi_browse_callback, ga);
if (!ga->priv->sb) {
g_print("Failed to create service browser: %s\n", avahi_strerror(avahi_client_errno(ga->priv->client)));
return;
}
ga->priv->iterate_handler = g_timeout_add(5000, (GSourceFunc)remmina_avahi_iterate, ga);
}
void remmina_avahi_stop(RemminaAvahi* ga)
{
TRACE_CALL(__func__);
g_hash_table_remove_all(ga->discovered_services);
if (ga->priv->iterate_handler) {
g_source_remove(ga->priv->iterate_handler);
ga->priv->iterate_handler = 0;
}
if (ga->priv->sb) {
avahi_service_browser_free(ga->priv->sb);
ga->priv->sb = NULL;
}
if (ga->priv->client) {
avahi_client_free(ga->priv->client);
ga->priv->client = NULL;
}
if (ga->priv->simple_poll) {
avahi_simple_poll_free(ga->priv->simple_poll);
ga->priv->simple_poll = NULL;
}
ga->started = FALSE;
}
void remmina_avahi_free(RemminaAvahi* ga)
{
TRACE_CALL(__func__);
if (ga == NULL)
return;
remmina_avahi_stop(ga);
g_free(ga->priv);
g_hash_table_destroy(ga->discovered_services);
g_free(ga);
}
#else
RemminaAvahi* remmina_avahi_new(void)
{
TRACE_CALL(__func__);
return NULL;
}
void remmina_avahi_start(RemminaAvahi* ga)
{
TRACE_CALL(__func__);
}
void remmina_avahi_stop(RemminaAvahi* ga)
{
TRACE_CALL(__func__);
}
void remmina_avahi_free(RemminaAvahi* ga)
{
TRACE_CALL(__func__);
}
#endif
|