~ubuntu-branches/ubuntu/raring/remmina/raring

« back to all changes in this revision

Viewing changes to src/remminaavahi.c

  • Committer: Package Import Robot
  • Author(s): Luca Falavigna
  • Date: 2012-02-11 17:28:48 UTC
  • mfrom: (1.2.8)
  • Revision ID: package-import@ubuntu.com-20120211172848-rh3ffi7075qyobuq
Tags: 1.0.0-1
* New upstream release.
  - Compatible with FreeRDP 1.0 (Closes: #658363).
  - Ported to GTK3, this also fixes an incompatibility with
    GTK2 and recent Avahi with GTK3 support, which lead to
    crashes when scanning network services (Closes: #626499).
* debian/patches/libvncserver.patch:
  - Do not use convenience copy of libvncserver.
* debian/patches/g_thread_init.patch:
  - Do not use deprecated g_thread_init function.
* debian/patches/REMMINA_COMMAND_NONE.patch:
  - Removed, obsoleted by GApplication port.
* debian/clean:
  - Remove spurious files created at build-time.
* debian/compat:
  - Bump compatibility level to 9.
* debian/control:
  - Refresh build-dependencies to match new structure.
  - Drop remmina-dev package, no longer used.
  - Build packages once provided by remmina-plugins.
  - Provide remmina-common package.
  - Provide remmina-plugin-gnome package.
* debian/copyright:
  - Refresh copyright information.
* debian/docs:
  - Documentation is no longer accurate, do not ship it anymore.
* debian/remmina-dev.install:
  - Drop remmina-dev package, no longer used.
* debian/remmina-plugin-telepathy.install:
  - Adjust location for Remmina.client.
  - Disable D-BUS support for now.
* debian/rules:
  - Compile with -DWITH_APPINDICATOR=OFF.
  - Do not treat plugins as shared libraries.
* debian/watch:
  - Adjust watch file to match new download location.

Show diffs side-by-side

added added

removed removed

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