b4dfac3b3265ea0473677c67b84f59732e5fed24
[vlc.git] / modules / services_discovery / bonjour.c
1 /*****************************************************************************
2  * bonjour.c: Bonjour services discovery module
3  *****************************************************************************
4  * Copyright (C) 2005-2009 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_services_discovery.h>
35
36 #include <avahi-client/client.h>
37 #include <avahi-client/publish.h>
38 #include <avahi-client/lookup.h>
39 #include <avahi-common/thread-watch.h>
40 #include <avahi-common/malloc.h>
41 #include <avahi-common/error.h>
42
43 /*****************************************************************************
44  * Module descriptor
45  *****************************************************************************/
46
47 /* Callbacks */
48     static int  Open ( vlc_object_t * );
49     static void Close( vlc_object_t * );
50
51 vlc_module_begin ()
52     set_shortname( "Bonjour" )
53     set_description( N_("Bonjour services") )
54     set_category( CAT_PLAYLIST )
55     set_subcategory( SUBCAT_PLAYLIST_SD )
56     set_capability( "services_discovery", 0 )
57     set_callbacks( Open, Close )
58 vlc_module_end ()
59
60 /*****************************************************************************
61  * Local structures
62  *****************************************************************************/
63
64 struct services_discovery_sys_t
65 {
66     AvahiThreadedPoll   *poll;
67     AvahiClient         *client;
68     AvahiServiceBrowser *sb;
69     vlc_dictionary_t    services_name_to_input_item;
70 };
71
72 /*****************************************************************************
73  * Local prototypes
74  *****************************************************************************/
75
76 /*****************************************************************************
77  * client_callback
78  *****************************************************************************/
79 static void client_callback( AvahiClient *c, AvahiClientState state,
80                              void * userdata )
81 {
82     services_discovery_t *p_sd = ( services_discovery_t* )userdata;
83     services_discovery_sys_t *p_sys = p_sd->p_sys;
84
85     if( state == AVAHI_CLIENT_FAILURE &&
86         (avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) )
87     {
88         msg_Err( p_sd, "avahi client disconnected" );
89         avahi_threaded_poll_quit( p_sys->poll );
90     }
91 }
92
93 /*****************************************************************************
94  * resolve_callback
95  *****************************************************************************/
96 static void resolve_callback(
97     AvahiServiceResolver *r,
98     AvahiIfIndex interface,
99     AvahiProtocol protocol,
100     AvahiResolverEvent event,
101     const char *name,
102     const char *type,
103     const char *domain,
104     const char *host_name,
105     const AvahiAddress *address,
106     uint16_t port,
107     AvahiStringList *txt,
108     AvahiLookupResultFlags flags,
109     void* userdata )
110 {
111     services_discovery_t *p_sd = ( services_discovery_t* )userdata;
112     services_discovery_sys_t *p_sys = p_sd->p_sys;
113     
114     VLC_UNUSED(interface); VLC_UNUSED(host_name);
115     VLC_UNUSED(flags);
116
117     if( event == AVAHI_RESOLVER_FAILURE )
118     {
119         msg_Err( p_sd,
120                  "failed to resolve service '%s' of type '%s' in domain '%s'",
121                  name, type, domain );
122     }
123     else if( event == AVAHI_RESOLVER_FOUND )
124     {
125         char a[128];
126         char *psz_uri = NULL;
127         char *psz_addr = NULL;
128         AvahiStringList *asl = NULL;
129         input_item_t *p_input = NULL;
130
131         msg_Dbg( p_sd, "service '%s' of type '%s' in domain '%s'",
132                  name, type, domain );
133
134         avahi_address_snprint(a, (sizeof(a)/sizeof(a[0]))-1, address);
135         if( protocol == AVAHI_PROTO_INET6 )
136             if( asprintf( &psz_addr, "[%s]", a ) == -1 )
137                 return;
138
139         if( txt != NULL )
140             asl = avahi_string_list_find( txt, "path" );
141         if( asl != NULL )
142         {
143             size_t size;
144             char *key = NULL;
145             char *value = NULL;
146             if( avahi_string_list_get_pair( asl, &key, &value, &size ) == 0 &&
147                 value != NULL )
148             {
149                 if( asprintf( &psz_uri, "http://%s:%d%s",
150                           psz_addr != NULL ? psz_addr : a, port, value ) == -1 )
151                 {
152                     free( psz_addr );
153                     return;
154                 }
155             }
156             if( key != NULL )
157                 avahi_free( (void *)key );
158             if( value != NULL )
159                 avahi_free( (void *)value );
160         }
161         else
162         {
163             if( asprintf( &psz_uri, "http://%s:%d",
164                       psz_addr != NULL ? psz_addr : a, port ) == -1 )
165             {
166                 free( psz_addr );
167                 return;
168             }
169         }
170
171         free( psz_addr );
172
173         if( psz_uri != NULL )
174         {
175             p_input = input_item_New( p_sd, psz_uri, name );
176             free( psz_uri );
177         }
178         if( p_input != NULL )
179         {
180             vlc_dictionary_insert( &p_sys->services_name_to_input_item,
181                 name, p_input );
182             services_discovery_AddItem( p_sd, p_input, NULL /* no category */ );
183             vlc_gc_decref( p_input );
184        }
185     }
186
187     avahi_service_resolver_free( r );
188 }
189
190 /*****************************************************************************
191  * browser_callback
192  *****************************************************************************/
193 static void browse_callback(
194     AvahiServiceBrowser *b,
195     AvahiIfIndex interface,
196     AvahiProtocol protocol,
197     AvahiBrowserEvent event,
198     const char *name,
199     const char *type,
200     const char *domain,
201     AvahiLookupResultFlags flags,
202     void* userdata )
203 {
204     VLC_UNUSED(b);
205     VLC_UNUSED(flags);
206     services_discovery_t *p_sd = ( services_discovery_t* )userdata;
207     services_discovery_sys_t *p_sys = p_sd->p_sys;
208     if( event == AVAHI_BROWSER_NEW )
209     {
210         if( avahi_service_resolver_new( p_sys->client, interface, protocol,
211                                         name, type, domain, AVAHI_PROTO_UNSPEC,
212                                         0,
213                                         resolve_callback, userdata ) == NULL )
214         {
215             msg_Err( p_sd, "failed to resolve service '%s': %s", name,
216                      avahi_strerror( avahi_client_errno( p_sys->client ) ) );
217         }
218     }
219     else if( name )
220     {
221         /** \todo Store the input id and search it, rather than searching the items */
222         input_item_t *p_item;
223         p_item = vlc_dictionary_value_for_key(
224                         &p_sys->services_name_to_input_item,
225                         name );
226         if( !p_item )
227             msg_Err( p_sd, "failed to find service '%s' in playlist", name );
228         else
229         {
230             services_discovery_RemoveItem( p_sd, p_item );
231             vlc_dictionary_remove_value_for_key(
232                         &p_sys->services_name_to_input_item,
233                         name, NULL, NULL );
234         }
235     }
236 }
237
238 /*****************************************************************************
239  * Open: initialize and create stuff
240  *****************************************************************************/
241 static int Open( vlc_object_t *p_this )
242 {
243     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
244     services_discovery_sys_t *p_sys;
245     int err;
246
247     p_sd->p_sys = p_sys = calloc( 1, sizeof( services_discovery_sys_t ) );
248     if( !p_sys )
249         return VLC_ENOMEM;
250
251     vlc_dictionary_init( &p_sys->services_name_to_input_item, 1 );
252
253     p_sys->poll = avahi_threaded_poll_new();
254     if( p_sys->poll == NULL )
255     {
256         msg_Err( p_sd, "failed to create Avahi threaded poll" );
257         goto error;
258     }
259
260     p_sys->client = avahi_client_new( avahi_threaded_poll_get(p_sys->poll),
261                                       0, client_callback, p_sd, &err );
262     if( p_sys->client == NULL )
263     {
264         msg_Err( p_sd, "failed to create avahi client: %s",
265                  avahi_strerror( err ) );
266         goto error;
267     }
268
269     p_sys->sb = avahi_service_browser_new( p_sys->client, AVAHI_IF_UNSPEC,
270                                            AVAHI_PROTO_UNSPEC,
271                                            "_vlc-http._tcp", NULL,
272                                            0, browse_callback, p_sd );
273     if( p_sys->sb == NULL )
274     {
275         msg_Err( p_sd, "failed to create avahi service browser" );
276         goto error;
277     }
278
279     return VLC_SUCCESS;
280
281 error:
282     if( p_sys->sb != NULL )
283         avahi_service_browser_free( p_sys->sb );
284     if( p_sys->client != NULL )
285         avahi_client_free( p_sys->client );
286     if( p_sys->poll != NULL )
287         avahi_threaded_poll_free( p_sys->poll );
288
289     vlc_dictionary_clear( &p_sys->services_name_to_input_item, NULL, NULL );
290     free( p_sys );
291
292     return VLC_EGENERIC;
293 }
294
295 /*****************************************************************************
296  * Close: cleanup
297  *****************************************************************************/
298 static void Close( vlc_object_t *p_this )
299 {
300     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
301     services_discovery_sys_t *p_sys = p_sd->p_sys;
302
303     avahi_service_browser_free( p_sys->sb );
304     avahi_client_free( p_sys->client );
305     avahi_threaded_poll_free( p_sys->poll );
306
307     vlc_dictionary_clear( &p_sys->services_name_to_input_item, NULL, NULL );
308     free( p_sys );
309 }