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