1 /*****************************************************************************
2 * bonjour.c: Bonjour services discovery module
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
7 * Authors: Jon Lech Johansen <jon@nanocrew.net>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_playlist.h>
35 #include <vlc_arrays.h>
37 #include <avahi-client/client.h>
39 # include <avahi-client/publish.h>
40 # include <avahi-client/lookup.h>
42 #include <avahi-common/simple-watch.h>
43 #include <avahi-common/malloc.h>
44 #include <avahi-common/error.h>
46 /*****************************************************************************
48 *****************************************************************************/
51 static int Open ( vlc_object_t * );
52 static void Close( vlc_object_t * );
55 set_shortname( "Bonjour" );
56 set_description( N_("Bonjour services") );
57 set_category( CAT_PLAYLIST );
58 set_subcategory( SUBCAT_PLAYLIST_SD );
59 set_capability( "services_discovery", 0 );
60 set_callbacks( Open, Close );
63 /*****************************************************************************
65 *****************************************************************************/
67 struct services_discovery_sys_t
69 AvahiSimplePoll *simple_poll;
71 AvahiServiceBrowser *sb;
72 vlc_dictionary_t services_name_to_input_item;
75 /*****************************************************************************
77 *****************************************************************************/
80 static void Run ( services_discovery_t *p_intf );
82 /*****************************************************************************
84 *****************************************************************************/
85 static void client_callback( AvahiClient *c, AvahiClientState state,
88 services_discovery_t *p_sd = ( services_discovery_t* )userdata;
89 services_discovery_sys_t *p_sys = p_sd->p_sys;
92 if( state == AVAHI_CLIENT_FAILURE &&
93 (avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) )
95 if( state == AVAHI_CLIENT_DISCONNECTED )
98 msg_Err( p_sd, "avahi client disconnected" );
99 avahi_simple_poll_quit( p_sys->simple_poll );
103 /*****************************************************************************
105 *****************************************************************************/
106 static void resolve_callback(
107 AvahiServiceResolver *r,
108 AvahiIfIndex interface,
109 AvahiProtocol protocol,
110 AvahiResolverEvent event,
114 const char *host_name,
115 const AvahiAddress *address,
117 AvahiStringList *txt,
119 AvahiLookupResultFlags flags,
123 services_discovery_t *p_sd = ( services_discovery_t* )userdata;
124 services_discovery_sys_t *p_sys = p_sd->p_sys;
126 VLC_UNUSED(interface); VLC_UNUSED(host_name);
132 if( event == AVAHI_RESOLVER_FAILURE )
134 if( event == AVAHI_RESOLVER_TIMEOUT )
138 "failed to resolve service '%s' of type '%s' in domain '%s'",
139 name, type, domain );
141 else if( event == AVAHI_RESOLVER_FOUND )
144 char *psz_uri = NULL;
145 char *psz_addr = NULL;
146 AvahiStringList *asl = NULL;
147 input_item_t *p_input = NULL;
149 msg_Dbg( p_sd, "service '%s' of type '%s' in domain '%s'",
150 name, type, domain );
152 avahi_address_snprint(a, (sizeof(a)/sizeof(a[0]))-1, address);
153 if( protocol == AVAHI_PROTO_INET6 )
154 if( asprintf( &psz_addr, "[%s]", a ) == -1 )
158 asl = avahi_string_list_find( txt, "path" );
164 if( avahi_string_list_get_pair( asl, &key, &value, &size ) == 0 &&
167 if( asprintf( &psz_uri, "http://%s:%d%s",
168 psz_addr != NULL ? psz_addr : a, port, value ) == -1 )
175 avahi_free( (void *)key );
177 avahi_free( (void *)value );
181 if( asprintf( &psz_uri, "http://%s:%d",
182 psz_addr != NULL ? psz_addr : a, port ) == -1 )
189 if( psz_addr != NULL )
190 free( (void *)psz_addr );
192 if( psz_uri != NULL )
194 p_input = input_ItemNewExt( p_sd, psz_uri, name, 0, NULL, -1 );
195 free( (void *)psz_uri );
197 if( p_input != NULL )
199 vlc_dictionary_insert( &p_sys->services_name_to_input_item,
201 services_discovery_AddItem( p_sd, p_input, NULL /* no category */ );
202 vlc_gc_decref( p_input );
206 avahi_service_resolver_free( r );
209 /*****************************************************************************
211 *****************************************************************************/
212 static void browse_callback(
213 AvahiServiceBrowser *b,
214 AvahiIfIndex interface,
215 AvahiProtocol protocol,
216 AvahiBrowserEvent event,
221 AvahiLookupResultFlags flags,
229 services_discovery_t *p_sd = ( services_discovery_t* )userdata;
230 services_discovery_sys_t *p_sys = p_sd->p_sys;
231 if( event == AVAHI_BROWSER_NEW )
233 if( avahi_service_resolver_new( p_sys->client, interface, protocol,
234 name, type, domain, AVAHI_PROTO_UNSPEC,
238 resolve_callback, userdata ) == NULL )
240 msg_Err( p_sd, "failed to resolve service '%s': %s", name,
241 avahi_strerror( avahi_client_errno( p_sys->client ) ) );
246 /** \todo Store the input id and search it, rather than searching the items */
247 input_item_t *p_item;
248 p_item = vlc_dictionary_value_for_key(
249 &p_sys->services_name_to_input_item,
252 msg_Err( p_sd, "failed to find service '%s' in playlist", name );
255 services_discovery_RemoveItem( p_sd, p_item );
256 vlc_dictionary_remove_value_for_key(
257 &p_sys->services_name_to_input_item,
263 /*****************************************************************************
264 * Open: initialize and create stuff
265 *****************************************************************************/
266 static int Open( vlc_object_t *p_this )
268 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
269 services_discovery_sys_t *p_sys;
272 p_sd->p_sys = p_sys = (services_discovery_sys_t *)malloc(
273 sizeof( services_discovery_sys_t ) );
278 memset( p_sys, 0, sizeof(*p_sys) );
280 vlc_dictionary_init( &p_sys->services_name_to_input_item, 1 );
282 p_sys->simple_poll = avahi_simple_poll_new();
283 if( p_sys->simple_poll == NULL )
285 msg_Err( p_sd, "failed to create avahi simple poll" );
289 p_sys->client = avahi_client_new( avahi_simple_poll_get(p_sys->simple_poll),
293 client_callback, p_sd, &err );
294 if( p_sys->client == NULL )
296 msg_Err( p_sd, "failed to create avahi client: %s",
297 avahi_strerror( err ) );
301 p_sys->sb = avahi_service_browser_new( p_sys->client, AVAHI_IF_UNSPEC,
303 "_vlc-http._tcp", NULL,
307 browse_callback, p_sd );
308 if( p_sys->sb == NULL )
310 msg_Err( p_sd, "failed to create avahi service browser" );
314 services_discovery_SetLocalizedName( p_sd, _("Bonjour") );
321 if( p_sys->sb != NULL )
322 avahi_service_browser_free( p_sys->sb );
323 if( p_sys->client != NULL )
324 avahi_client_free( p_sys->client );
325 if( p_sys->simple_poll != NULL )
326 avahi_simple_poll_free( p_sys->simple_poll );
328 vlc_dictionary_clear( &p_sys->services_name_to_input_item );
334 /*****************************************************************************
336 *****************************************************************************/
337 static void Close( vlc_object_t *p_this )
339 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
340 services_discovery_sys_t *p_sys = p_sd->p_sys;
342 avahi_service_browser_free( p_sys->sb );
343 avahi_client_free( p_sys->client );
344 avahi_simple_poll_free( p_sys->simple_poll );
346 vlc_dictionary_clear( &p_sys->services_name_to_input_item );
350 /*****************************************************************************
352 *****************************************************************************/
353 static void Run( services_discovery_t *p_sd )
355 services_discovery_sys_t *p_sys = p_sd->p_sys;
357 while( !p_sd->b_die )
359 if( avahi_simple_poll_iterate( p_sys->simple_poll, 100 ) != 0 )
361 msg_Err( p_sd, "poll iterate failed" );