1 /*****************************************************************************
2 * bonjour.c: Bonjour services discovery module
3 *****************************************************************************
4 * Copyright (C) 2005-2008 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>
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>
44 /*****************************************************************************
46 *****************************************************************************/
49 static int Open ( vlc_object_t * );
50 static void Close( vlc_object_t * );
53 set_shortname( "Bonjour" )
54 set_description( N_("Bonjour services") )
55 set_category( CAT_PLAYLIST )
56 set_subcategory( SUBCAT_PLAYLIST_SD )
57 set_capability( "services_discovery", 0 )
58 set_callbacks( Open, Close )
61 /*****************************************************************************
63 *****************************************************************************/
65 struct services_discovery_sys_t
67 AvahiThreadedPoll *poll;
69 AvahiServiceBrowser *sb;
70 vlc_dictionary_t services_name_to_input_item;
73 /*****************************************************************************
75 *****************************************************************************/
77 /*****************************************************************************
79 *****************************************************************************/
80 static void client_callback( AvahiClient *c, AvahiClientState state,
83 services_discovery_t *p_sd = ( services_discovery_t* )userdata;
84 services_discovery_sys_t *p_sys = p_sd->p_sys;
86 if( state == AVAHI_CLIENT_FAILURE &&
87 (avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) )
89 msg_Err( p_sd, "avahi client disconnected" );
90 avahi_threaded_poll_quit( p_sys->poll );
94 /*****************************************************************************
96 *****************************************************************************/
97 static void resolve_callback(
98 AvahiServiceResolver *r,
99 AvahiIfIndex interface,
100 AvahiProtocol protocol,
101 AvahiResolverEvent event,
105 const char *host_name,
106 const AvahiAddress *address,
108 AvahiStringList *txt,
109 AvahiLookupResultFlags flags,
112 services_discovery_t *p_sd = ( services_discovery_t* )userdata;
113 services_discovery_sys_t *p_sys = p_sd->p_sys;
115 VLC_UNUSED(interface); VLC_UNUSED(host_name);
118 if( event == AVAHI_RESOLVER_FAILURE )
121 "failed to resolve service '%s' of type '%s' in domain '%s'",
122 name, type, domain );
124 else if( event == AVAHI_RESOLVER_FOUND )
127 char *psz_uri = NULL;
128 char *psz_addr = NULL;
129 AvahiStringList *asl = NULL;
130 input_item_t *p_input = NULL;
132 msg_Dbg( p_sd, "service '%s' of type '%s' in domain '%s'",
133 name, type, domain );
135 avahi_address_snprint(a, (sizeof(a)/sizeof(a[0]))-1, address);
136 if( protocol == AVAHI_PROTO_INET6 )
137 if( asprintf( &psz_addr, "[%s]", a ) == -1 )
141 asl = avahi_string_list_find( txt, "path" );
147 if( avahi_string_list_get_pair( asl, &key, &value, &size ) == 0 &&
150 if( asprintf( &psz_uri, "http://%s:%d%s",
151 psz_addr != NULL ? psz_addr : a, port, value ) == -1 )
158 avahi_free( (void *)key );
160 avahi_free( (void *)value );
164 if( asprintf( &psz_uri, "http://%s:%d",
165 psz_addr != NULL ? psz_addr : a, port ) == -1 )
172 if( psz_addr != NULL )
173 free( (void *)psz_addr );
175 if( psz_uri != NULL )
177 p_input = input_item_NewExt( p_sd, psz_uri, name, 0, NULL, -1 );
178 free( (void *)psz_uri );
180 if( p_input != NULL )
182 vlc_dictionary_insert( &p_sys->services_name_to_input_item,
184 services_discovery_AddItem( p_sd, p_input, NULL /* no category */ );
185 vlc_gc_decref( p_input );
189 avahi_service_resolver_free( r );
192 /*****************************************************************************
194 *****************************************************************************/
195 static void browse_callback(
196 AvahiServiceBrowser *b,
197 AvahiIfIndex interface,
198 AvahiProtocol protocol,
199 AvahiBrowserEvent event,
203 AvahiLookupResultFlags flags,
208 services_discovery_t *p_sd = ( services_discovery_t* )userdata;
209 services_discovery_sys_t *p_sys = p_sd->p_sys;
210 if( event == AVAHI_BROWSER_NEW )
212 if( avahi_service_resolver_new( p_sys->client, interface, protocol,
213 name, type, domain, AVAHI_PROTO_UNSPEC,
215 resolve_callback, userdata ) == NULL )
217 msg_Err( p_sd, "failed to resolve service '%s': %s", name,
218 avahi_strerror( avahi_client_errno( p_sys->client ) ) );
223 /** \todo Store the input id and search it, rather than searching the items */
224 input_item_t *p_item;
225 p_item = vlc_dictionary_value_for_key(
226 &p_sys->services_name_to_input_item,
229 msg_Err( p_sd, "failed to find service '%s' in playlist", name );
232 services_discovery_RemoveItem( p_sd, p_item );
233 vlc_dictionary_remove_value_for_key(
234 &p_sys->services_name_to_input_item,
240 /*****************************************************************************
241 * Open: initialize and create stuff
242 *****************************************************************************/
243 static int Open( vlc_object_t *p_this )
245 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
246 services_discovery_sys_t *p_sys;
249 p_sd->p_sys = p_sys = (services_discovery_sys_t *)malloc(
250 sizeof( services_discovery_sys_t ) );
255 memset( p_sys, 0, sizeof(*p_sys) );
257 vlc_dictionary_init( &p_sys->services_name_to_input_item, 1 );
259 p_sys->poll = avahi_threaded_poll_new();
260 if( p_sys->poll == NULL )
262 msg_Err( p_sd, "failed to create Avahi threaded poll" );
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 )
270 msg_Err( p_sd, "failed to create avahi client: %s",
271 avahi_strerror( err ) );
275 p_sys->sb = avahi_service_browser_new( p_sys->client, AVAHI_IF_UNSPEC,
277 "_vlc-http._tcp", NULL,
278 0, browse_callback, p_sd );
279 if( p_sys->sb == NULL )
281 msg_Err( p_sd, "failed to create avahi service browser" );
285 services_discovery_SetLocalizedName( p_sd, _("Bonjour") );
290 if( p_sys->sb != NULL )
291 avahi_service_browser_free( p_sys->sb );
292 if( p_sys->client != NULL )
293 avahi_client_free( p_sys->client );
294 if( p_sys->poll != NULL )
295 avahi_threaded_poll_free( p_sys->poll );
297 vlc_dictionary_clear( &p_sys->services_name_to_input_item, NULL, NULL );
303 /*****************************************************************************
305 *****************************************************************************/
306 static void Close( vlc_object_t *p_this )
308 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
309 services_discovery_sys_t *p_sys = p_sd->p_sys;
311 avahi_service_browser_free( p_sys->sb );
312 avahi_client_free( p_sys->client );
313 avahi_threaded_poll_free( p_sys->poll );
315 vlc_dictionary_clear( &p_sys->services_name_to_input_item, NULL, NULL );