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