Converted globalhotkeys to XCB.
[vlc.git] / modules / services_discovery / bonjour.c
1 /*****************************************************************************
2  * bonjour.c: Bonjour services discovery module
3  *****************************************************************************
4  * Copyright (C) 2005-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Jon Lech Johansen <jon@nanocrew.net>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Includes
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_playlist.h>
35 #include <vlc_arrays.h>
36 #include <vlc_services_discovery.h>
37
38 #include <avahi-client/client.h>
39 #include <avahi-client/publish.h>
40 #include <avahi-client/lookup.h>
41 #include <avahi-common/thread-watch.h>
42 #include <avahi-common/malloc.h>
43 #include <avahi-common/error.h>
44
45 /*****************************************************************************
46  * Module descriptor
47  *****************************************************************************/
48
49 /* Callbacks */
50     static int  Open ( vlc_object_t * );
51     static void Close( vlc_object_t * );
52
53 vlc_module_begin ()
54     set_shortname( "Bonjour" )
55     set_description( N_("Bonjour services") )
56     set_category( CAT_PLAYLIST )
57     set_subcategory( SUBCAT_PLAYLIST_SD )
58     set_capability( "services_discovery", 0 )
59     set_callbacks( Open, Close )
60 vlc_module_end ()
61
62 /*****************************************************************************
63  * Local structures
64  *****************************************************************************/
65
66 struct services_discovery_sys_t
67 {
68     AvahiThreadedPoll   *poll;
69     AvahiClient         *client;
70     AvahiServiceBrowser *sb;
71     vlc_dictionary_t    services_name_to_input_item;
72 };
73
74 /*****************************************************************************
75  * Local prototypes
76  *****************************************************************************/
77
78 /*****************************************************************************
79  * client_callback
80  *****************************************************************************/
81 static void client_callback( AvahiClient *c, AvahiClientState state,
82                              void * userdata )
83 {
84     services_discovery_t *p_sd = ( services_discovery_t* )userdata;
85     services_discovery_sys_t *p_sys = p_sd->p_sys;
86
87     if( state == AVAHI_CLIENT_FAILURE &&
88         (avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) )
89     {
90         msg_Err( p_sd, "avahi client disconnected" );
91         avahi_threaded_poll_quit( p_sys->poll );
92     }
93 }
94
95 /*****************************************************************************
96  * resolve_callback
97  *****************************************************************************/
98 static void resolve_callback(
99     AvahiServiceResolver *r,
100     AvahiIfIndex interface,
101     AvahiProtocol protocol,
102     AvahiResolverEvent event,
103     const char *name,
104     const char *type,
105     const char *domain,
106     const char *host_name,
107     const AvahiAddress *address,
108     uint16_t port,
109     AvahiStringList *txt,
110     AvahiLookupResultFlags flags,
111     void* userdata )
112 {
113     services_discovery_t *p_sd = ( services_discovery_t* )userdata;
114     services_discovery_sys_t *p_sys = p_sd->p_sys;
115     
116     VLC_UNUSED(interface); VLC_UNUSED(host_name);
117     VLC_UNUSED(flags);
118
119     if( event == AVAHI_RESOLVER_FAILURE )
120     {
121         msg_Err( p_sd,
122                  "failed to resolve service '%s' of type '%s' in domain '%s'",
123                  name, type, domain );
124     }
125     else if( event == AVAHI_RESOLVER_FOUND )
126     {
127         char a[128];
128         char *psz_uri = NULL;
129         char *psz_addr = NULL;
130         AvahiStringList *asl = NULL;
131         input_item_t *p_input = NULL;
132
133         msg_Dbg( p_sd, "service '%s' of type '%s' in domain '%s'",
134                  name, type, domain );
135
136         avahi_address_snprint(a, (sizeof(a)/sizeof(a[0]))-1, address);
137         if( protocol == AVAHI_PROTO_INET6 )
138             if( asprintf( &psz_addr, "[%s]", a ) == -1 )
139                 return;
140
141         if( txt != NULL )
142             asl = avahi_string_list_find( txt, "path" );
143         if( asl != NULL )
144         {
145             size_t size;
146             char *key = NULL;
147             char *value = NULL;
148             if( avahi_string_list_get_pair( asl, &key, &value, &size ) == 0 &&
149                 value != NULL )
150             {
151                 if( asprintf( &psz_uri, "http://%s:%d%s",
152                           psz_addr != NULL ? psz_addr : a, port, value ) == -1 )
153                 {
154                     free( psz_addr );
155                     return;
156                 }
157             }
158             if( key != NULL )
159                 avahi_free( (void *)key );
160             if( value != NULL )
161                 avahi_free( (void *)value );
162         }
163         else
164         {
165             if( asprintf( &psz_uri, "http://%s:%d",
166                       psz_addr != NULL ? psz_addr : a, port ) == -1 )
167             {
168                 free( psz_addr );
169                 return;
170             }
171         }
172
173         if( psz_addr != NULL )
174             free( (void *)psz_addr );
175
176         if( psz_uri != NULL )
177         {
178             p_input = input_item_New( p_sd, psz_uri, name );
179             free( (void *)psz_uri );
180         }
181         if( p_input != NULL )
182         {
183             vlc_dictionary_insert( &p_sys->services_name_to_input_item,
184                 name, p_input );
185             services_discovery_AddItem( p_sd, p_input, NULL /* no category */ );
186             vlc_gc_decref( p_input );
187        }
188     }
189
190     avahi_service_resolver_free( r );
191 }
192
193 /*****************************************************************************
194  * browser_callback
195  *****************************************************************************/
196 static void browse_callback(
197     AvahiServiceBrowser *b,
198     AvahiIfIndex interface,
199     AvahiProtocol protocol,
200     AvahiBrowserEvent event,
201     const char *name,
202     const char *type,
203     const char *domain,
204     AvahiLookupResultFlags flags,
205     void* userdata )
206 {
207     VLC_UNUSED(b);
208     VLC_UNUSED(flags);
209     services_discovery_t *p_sd = ( services_discovery_t* )userdata;
210     services_discovery_sys_t *p_sys = p_sd->p_sys;
211     if( event == AVAHI_BROWSER_NEW )
212     {
213         if( avahi_service_resolver_new( p_sys->client, interface, protocol,
214                                         name, type, domain, AVAHI_PROTO_UNSPEC,
215                                         0,
216                                         resolve_callback, userdata ) == NULL )
217         {
218             msg_Err( p_sd, "failed to resolve service '%s': %s", name,
219                      avahi_strerror( avahi_client_errno( p_sys->client ) ) );
220         }
221     }
222     else if( name )
223     {
224         /** \todo Store the input id and search it, rather than searching the items */
225         input_item_t *p_item;
226         p_item = vlc_dictionary_value_for_key(
227                         &p_sys->services_name_to_input_item,
228                         name );
229         if( !p_item )
230             msg_Err( p_sd, "failed to find service '%s' in playlist", name );
231         else
232         {
233             services_discovery_RemoveItem( p_sd, p_item );
234             vlc_dictionary_remove_value_for_key(
235                         &p_sys->services_name_to_input_item,
236                         name, NULL, NULL );
237         }
238     }
239 }
240
241 /*****************************************************************************
242  * Open: initialize and create stuff
243  *****************************************************************************/
244 static int Open( vlc_object_t *p_this )
245 {
246     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
247     services_discovery_sys_t *p_sys;
248     int err;
249
250     p_sd->p_sys = p_sys = (services_discovery_sys_t *)malloc(
251         sizeof( services_discovery_sys_t ) );
252
253     if( !p_sys )
254         return VLC_ENOMEM;
255
256     memset( p_sys, 0, sizeof(*p_sys) );
257
258     vlc_dictionary_init( &p_sys->services_name_to_input_item, 1 );
259
260     p_sys->poll = avahi_threaded_poll_new();
261     if( p_sys->poll == NULL )
262     {
263         msg_Err( p_sd, "failed to create Avahi threaded poll" );
264         goto error;
265     }
266
267     p_sys->client = avahi_client_new( avahi_threaded_poll_get(p_sys->poll),
268                                       0, client_callback, p_sd, &err );
269     if( p_sys->client == NULL )
270     {
271         msg_Err( p_sd, "failed to create avahi client: %s",
272                  avahi_strerror( err ) );
273         goto error;
274     }
275
276     p_sys->sb = avahi_service_browser_new( p_sys->client, AVAHI_IF_UNSPEC,
277                                            AVAHI_PROTO_UNSPEC,
278                                            "_vlc-http._tcp", NULL,
279                                            0, browse_callback, p_sd );
280     if( p_sys->sb == NULL )
281     {
282         msg_Err( p_sd, "failed to create avahi service browser" );
283         goto error;
284     }
285
286     return VLC_SUCCESS;
287
288 error:
289     if( p_sys->sb != NULL )
290         avahi_service_browser_free( p_sys->sb );
291     if( p_sys->client != NULL )
292         avahi_client_free( p_sys->client );
293     if( p_sys->poll != NULL )
294         avahi_threaded_poll_free( p_sys->poll );
295
296     vlc_dictionary_clear( &p_sys->services_name_to_input_item, NULL, NULL );
297     free( p_sys );
298
299     return VLC_EGENERIC;
300 }
301
302 /*****************************************************************************
303  * Close: cleanup
304  *****************************************************************************/
305 static void Close( vlc_object_t *p_this )
306 {
307     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
308     services_discovery_sys_t *p_sys = p_sd->p_sys;
309
310     avahi_service_browser_free( p_sys->sb );
311     avahi_client_free( p_sys->client );
312     avahi_threaded_poll_free( p_sys->poll );
313
314     vlc_dictionary_clear( &p_sys->services_name_to_input_item, NULL, NULL );
315     free( p_sys );
316 }