Fix a bunch of warnings and errors
[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 #include <stdlib.h>                                      /* malloc(), free() */
28
29 #include <vlc/vlc.h>
30 #include <vlc/intf.h>
31
32 #include <avahi-client/client.h>
33 #ifdef HAVE_AVAHI_06
34 # include <avahi-client/publish.h>
35 # include <avahi-client/lookup.h>
36 #endif
37 #include <avahi-common/simple-watch.h>
38 #include <avahi-common/malloc.h>
39 #include <avahi-common/error.h>
40
41 /*****************************************************************************
42  * Module descriptor
43  *****************************************************************************/
44
45 /* Callbacks */
46     static int  Open ( vlc_object_t * );
47     static void Close( vlc_object_t * );
48
49 vlc_module_begin();
50     set_shortname( "Bonjour" );
51     set_description( _("Bonjour services") );
52     set_category( CAT_PLAYLIST );
53     set_subcategory( SUBCAT_PLAYLIST_SD );
54     set_capability( "services_discovery", 0 );
55     set_callbacks( Open, Close );
56 vlc_module_end();
57
58 /*****************************************************************************
59  * Local structures
60  *****************************************************************************/
61
62 struct services_discovery_sys_t
63 {
64     /* playlist node */
65     playlist_item_t     *p_node_cat, *p_node_one;
66     playlist_t          *p_playlist;
67
68     AvahiSimplePoll     *simple_poll;
69     AvahiClient         *client;
70     AvahiServiceBrowser *sb;
71 };
72
73 /*****************************************************************************
74  * Local prototypes
75  *****************************************************************************/
76
77 /* Main functions */
78     static void Run    ( services_discovery_t *p_intf );
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 #ifdef HAVE_AVAHI_06
90     if( state == AVAHI_CLIENT_FAILURE &&
91         (avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) )
92 #else
93     if( state == AVAHI_CLIENT_DISCONNECTED )
94 #endif
95     {
96         msg_Err( p_sd, "avahi client disconnected" );
97         avahi_simple_poll_quit( p_sys->simple_poll );
98     }
99 }
100
101 /*****************************************************************************
102  * resolve_callback
103  *****************************************************************************/
104 static void resolve_callback(
105     AvahiServiceResolver *r,
106     AvahiIfIndex interface,
107     AvahiProtocol protocol,
108     AvahiResolverEvent event,
109     const char *name,
110     const char *type,
111     const char *domain,
112     const char *host_name,
113     const AvahiAddress *address,
114     uint16_t port,
115     AvahiStringList *txt,
116 #ifdef HAVE_AVAHI_06
117     AvahiLookupResultFlags flags,
118 #endif
119     void* userdata )
120 {
121     services_discovery_t *p_sd = ( services_discovery_t* )userdata;
122     services_discovery_sys_t *p_sys = p_sd->p_sys;
123
124 #ifdef HAVE_AVAHI_06
125     if( event == AVAHI_RESOLVER_FAILURE )
126 #else
127     if( event == AVAHI_RESOLVER_TIMEOUT )
128 #endif
129     {
130         msg_Err( p_sd,
131                  "failed to resolve service '%s' of type '%s' in domain '%s'",
132                  name, type, domain );
133     }
134     else if( event == AVAHI_RESOLVER_FOUND )
135     {
136         char a[128];
137         char *psz_uri = NULL;
138         char *psz_addr = NULL;
139         AvahiStringList *asl = NULL;
140         input_item_t *p_input = NULL;
141
142         msg_Dbg( p_sd, "service '%s' of type '%s' in domain '%s'",
143                  name, type, domain );
144
145         avahi_address_snprint(a, (sizeof(a)/sizeof(a[0]))-1, address);
146         if( protocol == AVAHI_PROTO_INET6 )
147             asprintf( &psz_addr, "[%s]", a );
148
149         if( txt != NULL )
150             asl = avahi_string_list_find( txt, "path" );
151         if( asl != NULL )
152         {
153             size_t size;
154             char *key = NULL;
155             char *value = NULL;
156             if( avahi_string_list_get_pair( asl, &key, &value, &size ) == 0 &&
157                 value != NULL )
158             {
159                 asprintf( &psz_uri, "http://%s:%d%s",
160                           psz_addr != NULL ? psz_addr : a, port, value );
161             }
162             if( key != NULL )
163                 avahi_free( (void *)key );
164             if( value != NULL )
165                 avahi_free( (void *)value );
166         }
167         else
168         {
169             asprintf( &psz_uri, "http://%s:%d",
170                       psz_addr != NULL ? psz_addr : a, port );
171         }
172
173         if( psz_addr != NULL )
174             free( (void *)psz_addr );
175
176         if( psz_uri != NULL )
177         {
178             p_input = input_ItemNewExt( p_sd, psz_uri, name, 0, NULL, -1 );
179             free( (void *)psz_uri );
180         }
181         if( p_input != NULL )
182         {
183             playlist_item_t *p_item;
184             p_item = playlist_NodeAddInput( p_sys->p_playlist, p_input,
185                                             p_sys->p_node_cat,
186                                             PLAYLIST_APPEND, PLAYLIST_END );
187             p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
188             p_item->i_flags &= ~PLAYLIST_SAVE_FLAG;
189             p_item = playlist_NodeAddInput( p_sys->p_playlist, p_input,
190                                             p_sys->p_node_one,
191                                             PLAYLIST_APPEND, PLAYLIST_END );
192             p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
193             p_item->i_flags &= ~PLAYLIST_SAVE_FLAG;
194        }
195     }
196
197     avahi_service_resolver_free( r );
198 }
199
200 /*****************************************************************************
201  * browser_callback
202  *****************************************************************************/
203 static void browse_callback(
204     AvahiServiceBrowser *b,
205     AvahiIfIndex interface,
206     AvahiProtocol protocol,
207     AvahiBrowserEvent event,
208     const char *name,
209     const char *type,
210     const char *domain,
211 #ifdef HAVE_AVAHI_06
212     AvahiLookupResultFlags flags,
213 #endif
214     void* userdata )
215 {
216     services_discovery_t *p_sd = ( services_discovery_t* )userdata;
217     services_discovery_sys_t *p_sys = p_sd->p_sys;
218
219     if( event == AVAHI_BROWSER_NEW )
220     {
221         if( avahi_service_resolver_new( p_sys->client, interface, protocol,
222                                         name, type, domain, AVAHI_PROTO_UNSPEC,
223 #ifdef HAVE_AVAHI_06
224                                         0,
225 #endif
226                                         resolve_callback, userdata ) == NULL )
227         {
228             msg_Err( p_sd, "failed to resolve service '%s': %s", name,
229                      avahi_strerror( avahi_client_errno( p_sys->client ) ) );
230         }
231     }
232     else
233     {
234         /** \todo Store the input id and search it, rather than searching the items */
235         playlist_item_t *p_item;
236         p_item = playlist_ChildSearchName( p_sys->p_node_cat, name );
237         if( p_item == NULL )
238             msg_Err( p_sd, "failed to find service '%s' in playlist", name );
239         else
240         {
241             playlist_LockDeleteAllFromInput( p_sys->p_playlist,
242                                               p_item->p_input->i_id );
243         }
244     }
245 }
246
247 /*****************************************************************************
248  * Open: initialize and create stuff
249  *****************************************************************************/
250 static int Open( vlc_object_t *p_this )
251 {
252     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
253     services_discovery_sys_t *p_sys;
254     playlist_view_t *p_view;
255     vlc_value_t val;
256     int err;
257
258     p_sd->p_sys = p_sys = (services_discovery_sys_t *)malloc(
259         sizeof( services_discovery_sys_t ) );
260     if( p_sd->p_sys == NULL )
261     {
262         msg_Err( p_sd, "out of memory" );
263         return VLC_EGENERIC;
264     }
265
266     memset( p_sys, 0, sizeof(*p_sys) );
267
268     p_sys->simple_poll = avahi_simple_poll_new();
269     if( p_sys->simple_poll == NULL )
270     {
271         msg_Err( p_sd, "failed to create avahi simple poll" );
272         goto error;
273     }
274
275     p_sys->client = avahi_client_new( avahi_simple_poll_get(p_sys->simple_poll),
276 #ifdef HAVE_AVAHI_06
277                                       0,
278 #endif
279                                       client_callback, p_sd, &err );
280     if( p_sys->client == NULL )
281     {
282         msg_Err( p_sd, "failed to create avahi client: %s",
283                  avahi_strerror( err ) );
284         goto error;
285     }
286
287     p_sys->sb = avahi_service_browser_new( p_sys->client, AVAHI_IF_UNSPEC,
288                                            AVAHI_PROTO_UNSPEC,
289                                            "_vlc-http._tcp", NULL,
290 #ifdef HAVE_AVAHI_06
291                                            0,
292 #endif
293                                            browse_callback, p_sd );
294     if( p_sys->sb == NULL )
295     {
296         msg_Err( p_sd, "failed to create avahi service browser" );
297         goto error;
298     }
299
300     /* Create our playlist node */
301     p_sys->p_playlist = (playlist_t *)vlc_object_find( p_sd,
302                                                        VLC_OBJECT_PLAYLIST,
303                                                        FIND_ANYWHERE );
304     if( !p_sys->p_playlist )
305     {
306         msg_Warn( p_sd, "unable to find playlist, cancelling");
307         goto error;
308     }
309
310     playlist_NodesCreateForSD( p_sys->p_playlist, _("Bonjour"),
311                               &p_sys->p_node_cat,&p_sys->p_node_one );
312     p_sd->pf_run = Run;
313
314     return VLC_SUCCESS;
315
316 error:
317     if( p_sys->p_playlist != NULL )
318         vlc_object_release( p_sys->p_playlist );
319     if( p_sys->sb != NULL )
320         avahi_service_browser_free( p_sys->sb );
321     if( p_sys->client != NULL )
322         avahi_client_free( p_sys->client );
323     if( p_sys->simple_poll != NULL )
324         avahi_simple_poll_free( p_sys->simple_poll );
325
326     free( (void *)p_sys );
327
328     return VLC_EGENERIC;
329 }
330
331 /*****************************************************************************
332  * Close: cleanup
333  *****************************************************************************/
334 static void Close( vlc_object_t *p_this )
335 {
336     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
337     services_discovery_sys_t *p_sys = p_sd->p_sys;
338
339     avahi_service_browser_free( p_sys->sb );
340     avahi_client_free( p_sys->client );
341     avahi_simple_poll_free( p_sys->simple_poll );
342
343     playlist_NodeDelete( p_sys->p_playlist, p_sys->p_node_one, VLC_TRUE, VLC_TRUE );
344     playlist_NodeDelete( p_sys->p_playlist, p_sys->p_node_cat, VLC_TRUE, VLC_TRUE );
345     vlc_object_release( p_sys->p_playlist );
346
347     free( p_sys );
348 }
349
350 /*****************************************************************************
351  * Run: main thread
352  *****************************************************************************/
353 static void Run( services_discovery_t *p_sd )
354 {
355     services_discovery_sys_t *p_sys = p_sd->p_sys;
356
357     while( !p_sd->b_die )
358     {
359         if( avahi_simple_poll_iterate( p_sys->simple_poll, 100 ) != 0 )
360         {
361             msg_Err( p_sd, "poll iterate failed" );
362             break;
363         }
364     }
365 }