services_discovery/: fix warnings
[vlc.git] / 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     VLC_UNUSED(interface); VLC_UNUSED(host_name);
126 #ifdef HAVE_AVAHI_06
127     VLC_UNUSED(flags);
128 #endif
129
130 #ifdef HAVE_AVAHI_06
131     if( event == AVAHI_RESOLVER_FAILURE )
132 #else
133     if( event == AVAHI_RESOLVER_TIMEOUT )
134 #endif
135     {
136         msg_Err( p_sd,
137                  "failed to resolve service '%s' of type '%s' in domain '%s'",
138                  name, type, domain );
139     }
140     else if( event == AVAHI_RESOLVER_FOUND )
141     {
142         char a[128];
143         char *psz_uri = NULL;
144         char *psz_addr = NULL;
145         AvahiStringList *asl = NULL;
146         input_item_t *p_input = NULL;
147
148         msg_Dbg( p_sd, "service '%s' of type '%s' in domain '%s'",
149                  name, type, domain );
150
151         avahi_address_snprint(a, (sizeof(a)/sizeof(a[0]))-1, address);
152         if( protocol == AVAHI_PROTO_INET6 )
153             if( asprintf( &psz_addr, "[%s]", a ) == -1 )
154                 return;
155
156         if( txt != NULL )
157             asl = avahi_string_list_find( txt, "path" );
158         if( asl != NULL )
159         {
160             size_t size;
161             char *key = NULL;
162             char *value = NULL;
163             if( avahi_string_list_get_pair( asl, &key, &value, &size ) == 0 &&
164                 value != NULL )
165             {
166                 if( asprintf( &psz_uri, "http://%s:%d%s",
167                           psz_addr != NULL ? psz_addr : a, port, value ) == -1 )
168                 {
169                     free( psz_addr );
170                     return;
171                 }
172             }
173             if( key != NULL )
174                 avahi_free( (void *)key );
175             if( value != NULL )
176                 avahi_free( (void *)value );
177         }
178         else
179         {
180             if( asprintf( &psz_uri, "http://%s:%d",
181                       psz_addr != NULL ? psz_addr : a, port ) == -1 )
182             {
183                 free( psz_addr );
184                 return;
185             }
186         }
187
188         if( psz_addr != NULL )
189             free( (void *)psz_addr );
190
191         if( psz_uri != NULL )
192         {
193             p_input = input_ItemNewExt( p_sd, psz_uri, name, 0, NULL, -1 );
194             free( (void *)psz_uri );
195         }
196         if( p_input != NULL )
197         {
198             vlc_dictionary_insert( &p_sys->services_name_to_input_item,
199                 name, p_input );
200             services_discovery_AddItem( p_sd, p_input, NULL /* no category */ );
201             vlc_gc_decref( p_input );
202        }
203     }
204
205     avahi_service_resolver_free( r );
206 }
207
208 /*****************************************************************************
209  * browser_callback
210  *****************************************************************************/
211 static void browse_callback(
212     AvahiServiceBrowser *b,
213     AvahiIfIndex interface,
214     AvahiProtocol protocol,
215     AvahiBrowserEvent event,
216     const char *name,
217     const char *type,
218     const char *domain,
219 #ifdef HAVE_AVAHI_06
220     AvahiLookupResultFlags flags,
221 #endif
222     void* userdata )
223 {
224     VLC_UNUSED(b);
225 #ifdef HAVE_AVAHI_06
226     VLC_UNUSED(flags);
227 #endif
228     services_discovery_t *p_sd = ( services_discovery_t* )userdata;
229     services_discovery_sys_t *p_sys = p_sd->p_sys;
230     if( event == AVAHI_BROWSER_NEW )
231     {
232         if( avahi_service_resolver_new( p_sys->client, interface, protocol,
233                                         name, type, domain, AVAHI_PROTO_UNSPEC,
234 #ifdef HAVE_AVAHI_06
235                                         0,
236 #endif
237                                         resolve_callback, userdata ) == NULL )
238         {
239             msg_Err( p_sd, "failed to resolve service '%s': %s", name,
240                      avahi_strerror( avahi_client_errno( p_sys->client ) ) );
241         }
242     }
243     else if( name )
244     {
245         /** \todo Store the input id and search it, rather than searching the items */
246         input_item_t *p_item;
247         p_item = vlc_dictionary_value_for_key(
248                         &p_sys->services_name_to_input_item,
249                         name );
250         if( !p_item )
251             msg_Err( p_sd, "failed to find service '%s' in playlist", name );
252         else
253         {
254             services_discovery_RemoveItem( p_sd, p_item );
255             vlc_dictionary_remove_value_for_key(
256                         &p_sys->services_name_to_input_item,
257                         name );
258         }
259     }
260 }
261
262 /*****************************************************************************
263  * Open: initialize and create stuff
264  *****************************************************************************/
265 static int Open( vlc_object_t *p_this )
266 {
267     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
268     services_discovery_sys_t *p_sys;
269     int err;
270
271     p_sd->p_sys = p_sys = (services_discovery_sys_t *)malloc(
272         sizeof( services_discovery_sys_t ) );
273     if( p_sd->p_sys == NULL )
274     {
275         msg_Err( p_sd, "out of memory" );
276         return VLC_EGENERIC;
277     }
278
279     memset( p_sys, 0, sizeof(*p_sys) );
280
281     vlc_dictionary_init( &p_sys->services_name_to_input_item, 1 );
282
283     p_sys->simple_poll = avahi_simple_poll_new();
284     if( p_sys->simple_poll == NULL )
285     {
286         msg_Err( p_sd, "failed to create avahi simple poll" );
287         goto error;
288     }
289
290     p_sys->client = avahi_client_new( avahi_simple_poll_get(p_sys->simple_poll),
291 #ifdef HAVE_AVAHI_06
292                                       0,
293 #endif
294                                       client_callback, p_sd, &err );
295     if( p_sys->client == NULL )
296     {
297         msg_Err( p_sd, "failed to create avahi client: %s",
298                  avahi_strerror( err ) );
299         goto error;
300     }
301
302     p_sys->sb = avahi_service_browser_new( p_sys->client, AVAHI_IF_UNSPEC,
303                                            AVAHI_PROTO_UNSPEC,
304                                            "_vlc-http._tcp", NULL,
305 #ifdef HAVE_AVAHI_06
306                                            0,
307 #endif
308                                            browse_callback, p_sd );
309     if( p_sys->sb == NULL )
310     {
311         msg_Err( p_sd, "failed to create avahi service browser" );
312         goto error;
313     }
314
315     services_discovery_SetLocalizedName( p_sd, _("Bonjour") );
316
317     p_sd->pf_run = Run;
318
319     return VLC_SUCCESS;
320
321 error:
322     if( p_sys->sb != NULL )
323         avahi_service_browser_free( p_sys->sb );
324     if( p_sys->client != NULL )
325         avahi_client_free( p_sys->client );
326     if( p_sys->simple_poll != NULL )
327         avahi_simple_poll_free( p_sys->simple_poll );
328
329     vlc_dictionary_clear( &p_sys->services_name_to_input_item );
330     free( p_sys );
331
332     return VLC_EGENERIC;
333 }
334
335 /*****************************************************************************
336  * Close: cleanup
337  *****************************************************************************/
338 static void Close( vlc_object_t *p_this )
339 {
340     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
341     services_discovery_sys_t *p_sys = p_sd->p_sys;
342
343     avahi_service_browser_free( p_sys->sb );
344     avahi_client_free( p_sys->client );
345     avahi_simple_poll_free( p_sys->simple_poll );
346
347     vlc_dictionary_clear( &p_sys->services_name_to_input_item );
348     free( p_sys );
349 }
350
351 /*****************************************************************************
352  * Run: main thread
353  *****************************************************************************/
354 static void Run( services_discovery_t *p_sd )
355 {
356     services_discovery_sys_t *p_sys = p_sd->p_sys;
357
358     while( !p_sd->b_die )
359     {
360         if( avahi_simple_poll_iterate( p_sys->simple_poll, 100 ) != 0 )
361         {
362             msg_Err( p_sd, "poll iterate failed" );
363             break;
364         }
365     }
366 }