1 /*****************************************************************************
2 * bonjour.c: Bonjour services discovery module
3 *****************************************************************************
4 * Copyright (C) 2005-2009 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_services_discovery.h>
36 #include <avahi-client/client.h>
37 #include <avahi-client/publish.h>
38 #include <avahi-client/lookup.h>
39 #include <avahi-common/thread-watch.h>
40 #include <avahi-common/malloc.h>
41 #include <avahi-common/error.h>
43 /*****************************************************************************
45 *****************************************************************************/
48 static int Open ( vlc_object_t * );
49 static void Close( vlc_object_t * );
52 set_shortname( "Bonjour" )
53 set_description( N_("Bonjour services") )
54 set_category( CAT_PLAYLIST )
55 set_subcategory( SUBCAT_PLAYLIST_SD )
56 set_capability( "services_discovery", 0 )
57 set_callbacks( Open, Close )
60 /*****************************************************************************
62 *****************************************************************************/
64 struct services_discovery_sys_t
66 AvahiThreadedPoll *poll;
68 AvahiServiceBrowser *sb;
69 vlc_dictionary_t services_name_to_input_item;
72 /*****************************************************************************
74 *****************************************************************************/
76 /*****************************************************************************
78 *****************************************************************************/
79 static void client_callback( AvahiClient *c, AvahiClientState state,
82 services_discovery_t *p_sd = ( services_discovery_t* )userdata;
83 services_discovery_sys_t *p_sys = p_sd->p_sys;
85 if( state == AVAHI_CLIENT_FAILURE &&
86 (avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) )
88 msg_Err( p_sd, "avahi client disconnected" );
89 avahi_threaded_poll_quit( p_sys->poll );
93 /*****************************************************************************
95 *****************************************************************************/
96 static void resolve_callback(
97 AvahiServiceResolver *r,
98 AvahiIfIndex interface,
99 AvahiProtocol protocol,
100 AvahiResolverEvent event,
104 const char *host_name,
105 const AvahiAddress *address,
107 AvahiStringList *txt,
108 AvahiLookupResultFlags flags,
111 services_discovery_t *p_sd = ( services_discovery_t* )userdata;
112 services_discovery_sys_t *p_sys = p_sd->p_sys;
114 VLC_UNUSED(interface); VLC_UNUSED(host_name);
117 if( event == AVAHI_RESOLVER_FAILURE )
120 "failed to resolve service '%s' of type '%s' in domain '%s'",
121 name, type, domain );
123 else if( event == AVAHI_RESOLVER_FOUND )
126 char *psz_uri = NULL;
127 char *psz_addr = NULL;
128 AvahiStringList *asl = NULL;
129 input_item_t *p_input = NULL;
131 msg_Dbg( p_sd, "service '%s' of type '%s' in domain '%s'",
132 name, type, domain );
134 avahi_address_snprint(a, (sizeof(a)/sizeof(a[0]))-1, address);
135 if( protocol == AVAHI_PROTO_INET6 )
136 if( asprintf( &psz_addr, "[%s]", a ) == -1 )
140 asl = avahi_string_list_find( txt, "path" );
146 if( avahi_string_list_get_pair( asl, &key, &value, &size ) == 0 &&
149 if( asprintf( &psz_uri, "http://%s:%d%s",
150 psz_addr != NULL ? psz_addr : a, port, value ) == -1 )
157 avahi_free( (void *)key );
159 avahi_free( (void *)value );
163 if( asprintf( &psz_uri, "http://%s:%d",
164 psz_addr != NULL ? psz_addr : a, port ) == -1 )
173 if( psz_uri != NULL )
175 p_input = input_item_New( p_sd, psz_uri, name );
178 if( p_input != NULL )
180 vlc_dictionary_insert( &p_sys->services_name_to_input_item,
182 services_discovery_AddItem( p_sd, p_input, NULL /* no category */ );
183 vlc_gc_decref( p_input );
187 avahi_service_resolver_free( r );
190 /*****************************************************************************
192 *****************************************************************************/
193 static void browse_callback(
194 AvahiServiceBrowser *b,
195 AvahiIfIndex interface,
196 AvahiProtocol protocol,
197 AvahiBrowserEvent event,
201 AvahiLookupResultFlags flags,
206 services_discovery_t *p_sd = ( services_discovery_t* )userdata;
207 services_discovery_sys_t *p_sys = p_sd->p_sys;
208 if( event == AVAHI_BROWSER_NEW )
210 if( avahi_service_resolver_new( p_sys->client, interface, protocol,
211 name, type, domain, AVAHI_PROTO_UNSPEC,
213 resolve_callback, userdata ) == NULL )
215 msg_Err( p_sd, "failed to resolve service '%s': %s", name,
216 avahi_strerror( avahi_client_errno( p_sys->client ) ) );
221 /** \todo Store the input id and search it, rather than searching the items */
222 input_item_t *p_item;
223 p_item = vlc_dictionary_value_for_key(
224 &p_sys->services_name_to_input_item,
227 msg_Err( p_sd, "failed to find service '%s' in playlist", name );
230 services_discovery_RemoveItem( p_sd, p_item );
231 vlc_dictionary_remove_value_for_key(
232 &p_sys->services_name_to_input_item,
238 /*****************************************************************************
239 * Open: initialize and create stuff
240 *****************************************************************************/
241 static int Open( vlc_object_t *p_this )
243 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
244 services_discovery_sys_t *p_sys;
247 p_sd->p_sys = p_sys = calloc( 1, sizeof( services_discovery_sys_t ) );
251 vlc_dictionary_init( &p_sys->services_name_to_input_item, 1 );
253 p_sys->poll = avahi_threaded_poll_new();
254 if( p_sys->poll == NULL )
256 msg_Err( p_sd, "failed to create Avahi threaded poll" );
260 p_sys->client = avahi_client_new( avahi_threaded_poll_get(p_sys->poll),
261 0, client_callback, p_sd, &err );
262 if( p_sys->client == NULL )
264 msg_Err( p_sd, "failed to create avahi client: %s",
265 avahi_strerror( err ) );
269 p_sys->sb = avahi_service_browser_new( p_sys->client, AVAHI_IF_UNSPEC,
271 "_vlc-http._tcp", NULL,
272 0, browse_callback, p_sd );
273 if( p_sys->sb == NULL )
275 msg_Err( p_sd, "failed to create avahi service browser" );
282 if( p_sys->sb != NULL )
283 avahi_service_browser_free( p_sys->sb );
284 if( p_sys->client != NULL )
285 avahi_client_free( p_sys->client );
286 if( p_sys->poll != NULL )
287 avahi_threaded_poll_free( p_sys->poll );
289 vlc_dictionary_clear( &p_sys->services_name_to_input_item, NULL, NULL );
295 /*****************************************************************************
297 *****************************************************************************/
298 static void Close( vlc_object_t *p_this )
300 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
301 services_discovery_sys_t *p_sys = p_sd->p_sys;
303 avahi_service_browser_free( p_sys->sb );
304 avahi_client_free( p_sys->client );
305 avahi_threaded_poll_free( p_sys->poll );
307 vlc_dictionary_clear( &p_sys->services_name_to_input_item, NULL, NULL );