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