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 *****************************************************************************/
29 #include <vlc_playlist.h>
30 #include <vlc_arrays.h>
32 #include <avahi-client/client.h>
34 # include <avahi-client/publish.h>
35 # include <avahi-client/lookup.h>
37 #include <avahi-common/simple-watch.h>
38 #include <avahi-common/malloc.h>
39 #include <avahi-common/error.h>
41 /*****************************************************************************
43 *****************************************************************************/
46 static int Open ( vlc_object_t * );
47 static void Close( vlc_object_t * );
50 set_shortname( "Bonjour" );
51 set_description( _("Bonjour services") );
52 set_category( CAT_PLAYLIST );
53 set_subcategory( SUBCAT_PLAYLIST_SD );
54 set_capability( "services_discovery", 0 );
55 set_callbacks( Open, Close );
58 /*****************************************************************************
60 *****************************************************************************/
62 struct services_discovery_sys_t
64 AvahiSimplePoll *simple_poll;
66 AvahiServiceBrowser *sb;
67 vlc_dictionary_t services_name_to_input_item;
70 /*****************************************************************************
72 *****************************************************************************/
75 static void Run ( services_discovery_t *p_intf );
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;
87 if( state == AVAHI_CLIENT_FAILURE &&
88 (avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) )
90 if( state == AVAHI_CLIENT_DISCONNECTED )
93 msg_Err( p_sd, "avahi client disconnected" );
94 avahi_simple_poll_quit( p_sys->simple_poll );
98 /*****************************************************************************
100 *****************************************************************************/
101 static void resolve_callback(
102 AvahiServiceResolver *r,
103 AvahiIfIndex interface,
104 AvahiProtocol protocol,
105 AvahiResolverEvent event,
109 const char *host_name,
110 const AvahiAddress *address,
112 AvahiStringList *txt,
114 AvahiLookupResultFlags flags,
118 services_discovery_t *p_sd = ( services_discovery_t* )userdata;
119 services_discovery_sys_t *p_sys = p_sd->p_sys;
122 if( event == AVAHI_RESOLVER_FAILURE )
124 if( event == AVAHI_RESOLVER_TIMEOUT )
128 "failed to resolve service '%s' of type '%s' in domain '%s'",
129 name, type, domain );
131 else if( event == AVAHI_RESOLVER_FOUND )
134 char *psz_uri = NULL;
135 char *psz_addr = NULL;
136 AvahiStringList *asl = NULL;
137 input_item_t *p_input = NULL;
139 msg_Dbg( p_sd, "service '%s' of type '%s' in domain '%s'",
140 name, type, domain );
142 avahi_address_snprint(a, (sizeof(a)/sizeof(a[0]))-1, address);
143 if( protocol == AVAHI_PROTO_INET6 )
144 if( asprintf( &psz_addr, "[%s]", a ) == -1 )
148 asl = avahi_string_list_find( txt, "path" );
154 if( avahi_string_list_get_pair( asl, &key, &value, &size ) == 0 &&
157 if( asprintf( &psz_uri, "http://%s:%d%s",
158 psz_addr != NULL ? psz_addr : a, port, value ) == -1 )
165 avahi_free( (void *)key );
167 avahi_free( (void *)value );
171 if( asprintf( &psz_uri, "http://%s:%d",
172 psz_addr != NULL ? psz_addr : a, port ) == -1 )
179 if( psz_addr != NULL )
180 free( (void *)psz_addr );
182 if( psz_uri != NULL )
184 p_input = input_ItemNewExt( p_sd, psz_uri, name, 0, NULL, -1 );
185 free( (void *)psz_uri );
187 if( p_input != NULL )
189 vlc_dictionary_insert( &p_sys->services_name_to_input_item,
191 services_discovery_AddItem( p_sd, p_input, NULL /* no category */ );
192 vlc_gc_decref( p_input );
196 avahi_service_resolver_free( r );
199 /*****************************************************************************
201 *****************************************************************************/
202 static void browse_callback(
203 AvahiServiceBrowser *b,
204 AvahiIfIndex interface,
205 AvahiProtocol protocol,
206 AvahiBrowserEvent event,
211 AvahiLookupResultFlags flags,
215 services_discovery_t *p_sd = ( services_discovery_t* )userdata;
216 services_discovery_sys_t *p_sys = p_sd->p_sys;
217 if( event == AVAHI_BROWSER_NEW )
219 if( avahi_service_resolver_new( p_sys->client, interface, protocol,
220 name, type, domain, AVAHI_PROTO_UNSPEC,
224 resolve_callback, userdata ) == NULL )
226 msg_Err( p_sd, "failed to resolve service '%s': %s", name,
227 avahi_strerror( avahi_client_errno( p_sys->client ) ) );
232 /** \todo Store the input id and search it, rather than searching the items */
233 input_item_t *p_item;
234 p_item = vlc_dictionary_value_for_key(
235 &p_sys->services_name_to_input_item,
238 msg_Err( p_sd, "failed to find service '%s' in playlist", name );
241 services_discovery_RemoveItem( p_sd, p_item );
242 vlc_dictionary_remove_value_for_key(
243 &p_sys->services_name_to_input_item,
249 /*****************************************************************************
250 * Open: initialize and create stuff
251 *****************************************************************************/
252 static int Open( vlc_object_t *p_this )
254 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
255 services_discovery_sys_t *p_sys;
258 p_sd->p_sys = p_sys = (services_discovery_sys_t *)malloc(
259 sizeof( services_discovery_sys_t ) );
260 if( p_sd->p_sys == NULL )
262 msg_Err( p_sd, "out of memory" );
266 memset( p_sys, 0, sizeof(*p_sys) );
268 vlc_dictionary_init( &p_sys->services_name_to_input_item, 1 );
270 p_sys->simple_poll = avahi_simple_poll_new();
271 if( p_sys->simple_poll == NULL )
273 msg_Err( p_sd, "failed to create avahi simple poll" );
277 p_sys->client = avahi_client_new( avahi_simple_poll_get(p_sys->simple_poll),
281 client_callback, p_sd, &err );
282 if( p_sys->client == NULL )
284 msg_Err( p_sd, "failed to create avahi client: %s",
285 avahi_strerror( err ) );
289 p_sys->sb = avahi_service_browser_new( p_sys->client, AVAHI_IF_UNSPEC,
291 "_vlc-http._tcp", NULL,
295 browse_callback, p_sd );
296 if( p_sys->sb == NULL )
298 msg_Err( p_sd, "failed to create avahi service browser" );
302 services_discovery_SetLocalizedName( p_sd, _("Bonjour") );
309 if( p_sys->sb != NULL )
310 avahi_service_browser_free( p_sys->sb );
311 if( p_sys->client != NULL )
312 avahi_client_free( p_sys->client );
313 if( p_sys->simple_poll != NULL )
314 avahi_simple_poll_free( p_sys->simple_poll );
316 vlc_dictionary_clear( &p_sys->services_name_to_input_item );
322 /*****************************************************************************
324 *****************************************************************************/
325 static void Close( vlc_object_t *p_this )
327 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
328 services_discovery_sys_t *p_sys = p_sd->p_sys;
330 avahi_service_browser_free( p_sys->sb );
331 avahi_client_free( p_sys->client );
332 avahi_simple_poll_free( p_sys->simple_poll );
334 vlc_dictionary_clear( &p_sys->services_name_to_input_item );
338 /*****************************************************************************
340 *****************************************************************************/
341 static void Run( services_discovery_t *p_sd )
343 services_discovery_sys_t *p_sys = p_sd->p_sys;
345 while( !p_sd->b_die )
347 if( avahi_simple_poll_iterate( p_sys->simple_poll, 100 ) != 0 )
349 msg_Err( p_sd, "poll iterate failed" );