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>
36 #include <vlc_services_discovery.h>
38 #include <avahi-client/client.h>
39 #include <avahi-client/publish.h>
40 #include <avahi-client/lookup.h>
41 #include <avahi-common/thread-watch.h>
42 #include <avahi-common/malloc.h>
43 #include <avahi-common/error.h>
45 /*****************************************************************************
47 *****************************************************************************/
50 static int Open ( vlc_object_t * );
51 static void Close( vlc_object_t * );
54 set_shortname( "Bonjour" )
55 set_description( N_("Bonjour services") )
56 set_category( CAT_PLAYLIST )
57 set_subcategory( SUBCAT_PLAYLIST_SD )
58 set_capability( "services_discovery", 0 )
59 set_callbacks( Open, Close )
62 /*****************************************************************************
64 *****************************************************************************/
66 struct services_discovery_sys_t
68 AvahiThreadedPoll *poll;
70 AvahiServiceBrowser *sb;
71 vlc_dictionary_t services_name_to_input_item;
74 /*****************************************************************************
76 *****************************************************************************/
78 /*****************************************************************************
80 *****************************************************************************/
81 static void client_callback( AvahiClient *c, AvahiClientState state,
84 services_discovery_t *p_sd = ( services_discovery_t* )userdata;
85 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 msg_Err( p_sd, "avahi client disconnected" );
91 avahi_threaded_poll_quit( p_sys->poll );
95 /*****************************************************************************
97 *****************************************************************************/
98 static void resolve_callback(
99 AvahiServiceResolver *r,
100 AvahiIfIndex interface,
101 AvahiProtocol protocol,
102 AvahiResolverEvent event,
106 const char *host_name,
107 const AvahiAddress *address,
109 AvahiStringList *txt,
110 AvahiLookupResultFlags flags,
113 services_discovery_t *p_sd = ( services_discovery_t* )userdata;
114 services_discovery_sys_t *p_sys = p_sd->p_sys;
116 VLC_UNUSED(interface); VLC_UNUSED(host_name);
119 if( event == AVAHI_RESOLVER_FAILURE )
122 "failed to resolve service '%s' of type '%s' in domain '%s'",
123 name, type, domain );
125 else if( event == AVAHI_RESOLVER_FOUND )
128 char *psz_uri = NULL;
129 char *psz_addr = NULL;
130 AvahiStringList *asl = NULL;
131 input_item_t *p_input = NULL;
133 msg_Dbg( p_sd, "service '%s' of type '%s' in domain '%s'",
134 name, type, domain );
136 avahi_address_snprint(a, (sizeof(a)/sizeof(a[0]))-1, address);
137 if( protocol == AVAHI_PROTO_INET6 )
138 if( asprintf( &psz_addr, "[%s]", a ) == -1 )
142 asl = avahi_string_list_find( txt, "path" );
148 if( avahi_string_list_get_pair( asl, &key, &value, &size ) == 0 &&
151 if( asprintf( &psz_uri, "http://%s:%d%s",
152 psz_addr != NULL ? psz_addr : a, port, value ) == -1 )
159 avahi_free( (void *)key );
161 avahi_free( (void *)value );
165 if( asprintf( &psz_uri, "http://%s:%d",
166 psz_addr != NULL ? psz_addr : a, port ) == -1 )
173 if( psz_addr != NULL )
174 free( (void *)psz_addr );
176 if( psz_uri != NULL )
178 p_input = input_item_NewExt( p_sd, psz_uri, name, 0, NULL, 0, -1 );
179 free( (void *)psz_uri );
181 if( p_input != NULL )
183 vlc_dictionary_insert( &p_sys->services_name_to_input_item,
185 services_discovery_AddItem( p_sd, p_input, NULL /* no category */ );
186 vlc_gc_decref( p_input );
190 avahi_service_resolver_free( r );
193 /*****************************************************************************
195 *****************************************************************************/
196 static void browse_callback(
197 AvahiServiceBrowser *b,
198 AvahiIfIndex interface,
199 AvahiProtocol protocol,
200 AvahiBrowserEvent event,
204 AvahiLookupResultFlags flags,
209 services_discovery_t *p_sd = ( services_discovery_t* )userdata;
210 services_discovery_sys_t *p_sys = p_sd->p_sys;
211 if( event == AVAHI_BROWSER_NEW )
213 if( avahi_service_resolver_new( p_sys->client, interface, protocol,
214 name, type, domain, AVAHI_PROTO_UNSPEC,
216 resolve_callback, userdata ) == NULL )
218 msg_Err( p_sd, "failed to resolve service '%s': %s", name,
219 avahi_strerror( avahi_client_errno( p_sys->client ) ) );
224 /** \todo Store the input id and search it, rather than searching the items */
225 input_item_t *p_item;
226 p_item = vlc_dictionary_value_for_key(
227 &p_sys->services_name_to_input_item,
230 msg_Err( p_sd, "failed to find service '%s' in playlist", name );
233 services_discovery_RemoveItem( p_sd, p_item );
234 vlc_dictionary_remove_value_for_key(
235 &p_sys->services_name_to_input_item,
241 /*****************************************************************************
242 * Open: initialize and create stuff
243 *****************************************************************************/
244 static int Open( vlc_object_t *p_this )
246 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
247 services_discovery_sys_t *p_sys;
250 p_sd->p_sys = p_sys = (services_discovery_sys_t *)malloc(
251 sizeof( services_discovery_sys_t ) );
256 memset( p_sys, 0, sizeof(*p_sys) );
258 vlc_dictionary_init( &p_sys->services_name_to_input_item, 1 );
260 p_sys->poll = avahi_threaded_poll_new();
261 if( p_sys->poll == NULL )
263 msg_Err( p_sd, "failed to create Avahi threaded poll" );
267 p_sys->client = avahi_client_new( avahi_threaded_poll_get(p_sys->poll),
268 0, client_callback, p_sd, &err );
269 if( p_sys->client == NULL )
271 msg_Err( p_sd, "failed to create avahi client: %s",
272 avahi_strerror( err ) );
276 p_sys->sb = avahi_service_browser_new( p_sys->client, AVAHI_IF_UNSPEC,
278 "_vlc-http._tcp", NULL,
279 0, browse_callback, p_sd );
280 if( p_sys->sb == NULL )
282 msg_Err( p_sd, "failed to create avahi service browser" );
289 if( p_sys->sb != NULL )
290 avahi_service_browser_free( p_sys->sb );
291 if( p_sys->client != NULL )
292 avahi_client_free( p_sys->client );
293 if( p_sys->poll != NULL )
294 avahi_threaded_poll_free( p_sys->poll );
296 vlc_dictionary_clear( &p_sys->services_name_to_input_item, NULL, NULL );
302 /*****************************************************************************
304 *****************************************************************************/
305 static void Close( vlc_object_t *p_this )
307 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
308 services_discovery_sys_t *p_sys = p_sd->p_sys;
310 avahi_service_browser_free( p_sys->sb );
311 avahi_client_free( p_sys->client );
312 avahi_threaded_poll_free( p_sys->poll );
314 vlc_dictionary_clear( &p_sys->services_name_to_input_item, NULL, NULL );