]> git.sesse.net Git - vlc/blob - modules/services_discovery/bonjour.c
ncurses: removes useless unused parameter warnings, mvhline()->mvwhline()
[vlc] / modules / services_discovery / bonjour.c
1 /*****************************************************************************
2  * bonjour.c: Bonjour services discovery module
3  *****************************************************************************
4  * Copyright (C) 2005 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/vlc.h>
33 #include <vlc_playlist.h>
34 #include <vlc_arrays.h>
35
36 #include <avahi-client/client.h>
37 #ifdef HAVE_AVAHI_06
38 # include <avahi-client/publish.h>
39 # include <avahi-client/lookup.h>
40 #endif
41 #include <avahi-common/simple-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( _("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     AvahiSimplePoll     *simple_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 /* Main functions */
79     static void Run    ( services_discovery_t *p_intf );
80
81 /*****************************************************************************
82  * client_callback
83  *****************************************************************************/
84 static void client_callback( AvahiClient *c, AvahiClientState state,
85                              void * userdata )
86 {
87     services_discovery_t *p_sd = ( services_discovery_t* )userdata;
88     services_discovery_sys_t *p_sys = p_sd->p_sys;
89
90 #ifdef HAVE_AVAHI_06
91     if( state == AVAHI_CLIENT_FAILURE &&
92         (avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) )
93 #else
94     if( state == AVAHI_CLIENT_DISCONNECTED )
95 #endif
96     {
97         msg_Err( p_sd, "avahi client disconnected" );
98         avahi_simple_poll_quit( p_sys->simple_poll );
99     }
100 }
101
102 /*****************************************************************************
103  * resolve_callback
104  *****************************************************************************/
105 static void resolve_callback(
106     AvahiServiceResolver *r,
107     AvahiIfIndex interface,
108     AvahiProtocol protocol,
109     AvahiResolverEvent event,
110     const char *name,
111     const char *type,
112     const char *domain,
113     const char *host_name,
114     const AvahiAddress *address,
115     uint16_t port,
116     AvahiStringList *txt,
117 #ifdef HAVE_AVAHI_06
118     AvahiLookupResultFlags flags,
119 #endif
120     void* userdata )
121 {
122     services_discovery_t *p_sd = ( services_discovery_t* )userdata;
123     services_discovery_sys_t *p_sys = p_sd->p_sys;
124
125 #ifdef HAVE_AVAHI_06
126     if( event == AVAHI_RESOLVER_FAILURE )
127 #else
128     if( event == AVAHI_RESOLVER_TIMEOUT )
129 #endif
130     {
131         msg_Err( p_sd,
132                  "failed to resolve service '%s' of type '%s' in domain '%s'",
133                  name, type, domain );
134     }
135     else if( event == AVAHI_RESOLVER_FOUND )
136     {
137         char a[128];
138         char *psz_uri = NULL;
139         char *psz_addr = NULL;
140         AvahiStringList *asl = NULL;
141         input_item_t *p_input = NULL;
142
143         msg_Dbg( p_sd, "service '%s' of type '%s' in domain '%s'",
144                  name, type, domain );
145
146         avahi_address_snprint(a, (sizeof(a)/sizeof(a[0]))-1, address);
147         if( protocol == AVAHI_PROTO_INET6 )
148             if( asprintf( &psz_addr, "[%s]", a ) == -1 )
149                 return;
150
151         if( txt != NULL )
152             asl = avahi_string_list_find( txt, "path" );
153         if( asl != NULL )
154         {
155             size_t size;
156             char *key = NULL;
157             char *value = NULL;
158             if( avahi_string_list_get_pair( asl, &key, &value, &size ) == 0 &&
159                 value != NULL )
160             {
161                 if( asprintf( &psz_uri, "http://%s:%d%s",
162                           psz_addr != NULL ? psz_addr : a, port, value ) == -1 )
163                 {
164                     free( psz_addr );
165                     return;
166                 }
167             }
168             if( key != NULL )
169                 avahi_free( (void *)key );
170             if( value != NULL )
171                 avahi_free( (void *)value );
172         }
173         else
174         {
175             if( asprintf( &psz_uri, "http://%s:%d",
176                       psz_addr != NULL ? psz_addr : a, port ) == -1 )
177             {
178                 free( psz_addr );
179                 return;
180             }
181         }
182
183         if( psz_addr != NULL )
184             free( (void *)psz_addr );
185
186         if( psz_uri != NULL )
187         {
188             p_input = input_ItemNewExt( p_sd, psz_uri, name, 0, NULL, -1 );
189             free( (void *)psz_uri );
190         }
191         if( p_input != NULL )
192         {
193             vlc_dictionary_insert( &p_sys->services_name_to_input_item,
194                 name, p_input );
195             services_discovery_AddItem( p_sd, p_input, NULL /* no category */ );
196             vlc_gc_decref( p_input );
197        }
198     }
199
200     avahi_service_resolver_free( r );
201 }
202
203 /*****************************************************************************
204  * browser_callback
205  *****************************************************************************/
206 static void browse_callback(
207     AvahiServiceBrowser *b,
208     AvahiIfIndex interface,
209     AvahiProtocol protocol,
210     AvahiBrowserEvent event,
211     const char *name,
212     const char *type,
213     const char *domain,
214 #ifdef HAVE_AVAHI_06
215     AvahiLookupResultFlags flags,
216 #endif
217     void* userdata )
218 {
219     services_discovery_t *p_sd = ( services_discovery_t* )userdata;
220     services_discovery_sys_t *p_sys = p_sd->p_sys;
221     if( event == AVAHI_BROWSER_NEW )
222     {
223         if( avahi_service_resolver_new( p_sys->client, interface, protocol,
224                                         name, type, domain, AVAHI_PROTO_UNSPEC,
225 #ifdef HAVE_AVAHI_06
226                                         0,
227 #endif
228                                         resolve_callback, userdata ) == NULL )
229         {
230             msg_Err( p_sd, "failed to resolve service '%s': %s", name,
231                      avahi_strerror( avahi_client_errno( p_sys->client ) ) );
232         }
233     }
234     else if( name )
235     {
236         /** \todo Store the input id and search it, rather than searching the items */
237         input_item_t *p_item;
238         p_item = vlc_dictionary_value_for_key(
239                         &p_sys->services_name_to_input_item,
240                         name );
241         if( !p_item )
242             msg_Err( p_sd, "failed to find service '%s' in playlist", name );
243         else
244         {
245             services_discovery_RemoveItem( p_sd, p_item );
246             vlc_dictionary_remove_value_for_key(
247                         &p_sys->services_name_to_input_item,
248                         name );
249         }
250     }
251 }
252
253 /*****************************************************************************
254  * Open: initialize and create stuff
255  *****************************************************************************/
256 static int Open( vlc_object_t *p_this )
257 {
258     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
259     services_discovery_sys_t *p_sys;
260     int err;
261
262     p_sd->p_sys = p_sys = (services_discovery_sys_t *)malloc(
263         sizeof( services_discovery_sys_t ) );
264     if( p_sd->p_sys == NULL )
265     {
266         msg_Err( p_sd, "out of memory" );
267         return VLC_EGENERIC;
268     }
269
270     memset( p_sys, 0, sizeof(*p_sys) );
271
272     vlc_dictionary_init( &p_sys->services_name_to_input_item, 1 );
273
274     p_sys->simple_poll = avahi_simple_poll_new();
275     if( p_sys->simple_poll == NULL )
276     {
277         msg_Err( p_sd, "failed to create avahi simple poll" );
278         goto error;
279     }
280
281     p_sys->client = avahi_client_new( avahi_simple_poll_get(p_sys->simple_poll),
282 #ifdef HAVE_AVAHI_06
283                                       0,
284 #endif
285                                       client_callback, p_sd, &err );
286     if( p_sys->client == NULL )
287     {
288         msg_Err( p_sd, "failed to create avahi client: %s",
289                  avahi_strerror( err ) );
290         goto error;
291     }
292
293     p_sys->sb = avahi_service_browser_new( p_sys->client, AVAHI_IF_UNSPEC,
294                                            AVAHI_PROTO_UNSPEC,
295                                            "_vlc-http._tcp", NULL,
296 #ifdef HAVE_AVAHI_06
297                                            0,
298 #endif
299                                            browse_callback, p_sd );
300     if( p_sys->sb == NULL )
301     {
302         msg_Err( p_sd, "failed to create avahi service browser" );
303         goto error;
304     }
305
306     services_discovery_SetLocalizedName( p_sd, _("Bonjour") );
307
308     p_sd->pf_run = Run;
309
310     return VLC_SUCCESS;
311
312 error:
313     if( p_sys->sb != NULL )
314         avahi_service_browser_free( p_sys->sb );
315     if( p_sys->client != NULL )
316         avahi_client_free( p_sys->client );
317     if( p_sys->simple_poll != NULL )
318         avahi_simple_poll_free( p_sys->simple_poll );
319
320     vlc_dictionary_clear( &p_sys->services_name_to_input_item );
321     free( p_sys );
322
323     return VLC_EGENERIC;
324 }
325
326 /*****************************************************************************
327  * Close: cleanup
328  *****************************************************************************/
329 static void Close( vlc_object_t *p_this )
330 {
331     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
332     services_discovery_sys_t *p_sys = p_sd->p_sys;
333
334     avahi_service_browser_free( p_sys->sb );
335     avahi_client_free( p_sys->client );
336     avahi_simple_poll_free( p_sys->simple_poll );
337
338     vlc_dictionary_clear( &p_sys->services_name_to_input_item );
339     free( p_sys );
340 }
341
342 /*****************************************************************************
343  * Run: main thread
344  *****************************************************************************/
345 static void Run( services_discovery_t *p_sd )
346 {
347     services_discovery_sys_t *p_sys = p_sd->p_sys;
348
349     while( !p_sd->b_die )
350     {
351         if( avahi_simple_poll_iterate( p_sys->simple_poll, 100 ) != 0 )
352         {
353             msg_Err( p_sd, "poll iterate failed" );
354             break;
355         }
356     }
357 }