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 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
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
65 playlist_item_t *p_node_cat, *p_node_one;
66 playlist_t *p_playlist;
68 AvahiSimplePoll *simple_poll;
70 AvahiServiceBrowser *sb;
73 /*****************************************************************************
75 *****************************************************************************/
78 static void Run ( services_discovery_t *p_intf );
80 /*****************************************************************************
82 *****************************************************************************/
83 static void client_callback( AvahiClient *c, AvahiClientState state,
86 services_discovery_t *p_sd = ( services_discovery_t* )userdata;
87 services_discovery_sys_t *p_sys = p_sd->p_sys;
90 if( state == AVAHI_CLIENT_FAILURE &&
91 (avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) )
93 if( state == AVAHI_CLIENT_DISCONNECTED )
96 msg_Err( p_sd, "avahi client disconnected" );
97 avahi_simple_poll_quit( p_sys->simple_poll );
101 /*****************************************************************************
103 *****************************************************************************/
104 static void resolve_callback(
105 AvahiServiceResolver *r,
106 AvahiIfIndex interface,
107 AvahiProtocol protocol,
108 AvahiResolverEvent event,
112 const char *host_name,
113 const AvahiAddress *address,
115 AvahiStringList *txt,
117 AvahiLookupResultFlags flags,
121 services_discovery_t *p_sd = ( services_discovery_t* )userdata;
122 services_discovery_sys_t *p_sys = p_sd->p_sys;
125 if( event == AVAHI_RESOLVER_FAILURE )
127 if( event == AVAHI_RESOLVER_TIMEOUT )
131 "failed to resolve service '%s' of type '%s' in domain '%s'",
132 name, type, domain );
134 else if( event == AVAHI_RESOLVER_FOUND )
137 char *psz_uri = NULL;
138 char *psz_addr = NULL;
139 AvahiStringList *asl = NULL;
140 playlist_item_t *p_item = NULL;
142 msg_Dbg( p_sd, "service '%s' of type '%s' in domain '%s'",
143 name, type, domain );
145 avahi_address_snprint(a, (sizeof(a)/sizeof(a[0]))-1, address);
146 if( protocol == AVAHI_PROTO_INET6 )
147 asprintf( &psz_addr, "[%s]", a );
150 asl = avahi_string_list_find( txt, "path" );
156 if( avahi_string_list_get_pair( asl, &key, &value, &size ) == 0 &&
159 asprintf( &psz_uri, "http://%s:%d%s",
160 psz_addr != NULL ? psz_addr : a, port, value );
163 avahi_free( (void *)key );
165 avahi_free( (void *)value );
169 asprintf( &psz_uri, "http://%s:%d",
170 psz_addr != NULL ? psz_addr : a, port );
173 if( psz_addr != NULL )
174 free( (void *)psz_addr );
176 if( psz_uri != NULL )
178 p_item = playlist_ItemNew( p_sd, psz_uri, name );
179 free( (void *)psz_uri );
183 p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
185 playlist_NodeAddItem( p_sys->p_playlist, p_item,
186 VIEW_CATEGORY, p_sys->p_node,
187 PLAYLIST_APPEND, PLAYLIST_END );
191 avahi_service_resolver_free( r );
194 /*****************************************************************************
196 *****************************************************************************/
197 static void browse_callback(
198 AvahiServiceBrowser *b,
199 AvahiIfIndex interface,
200 AvahiProtocol protocol,
201 AvahiBrowserEvent event,
206 AvahiLookupResultFlags flags,
210 services_discovery_t *p_sd = ( services_discovery_t* )userdata;
211 services_discovery_sys_t *p_sys = p_sd->p_sys;
213 if( event == AVAHI_BROWSER_NEW )
215 if( avahi_service_resolver_new( p_sys->client, interface, protocol,
216 name, type, domain, AVAHI_PROTO_UNSPEC,
220 resolve_callback, userdata ) == NULL )
222 msg_Err( p_sd, "failed to resolve service '%s': %s", name,
223 avahi_strerror( avahi_client_errno( p_sys->client ) ) );
228 playlist_item_t *p_item;
230 p_item = playlist_ChildSearchName( p_sys->p_node, name );
233 msg_Err( p_sd, "failed to find service '%s' in playlist", name );
237 playlist_Delete( p_sys->p_playlist, p_item->input.i_id );
242 /*****************************************************************************
243 * Open: initialize and create stuff
244 *****************************************************************************/
245 static int Open( vlc_object_t *p_this )
247 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
248 services_discovery_sys_t *p_sys;
249 playlist_view_t *p_view;
253 p_sd->p_sys = p_sys = (services_discovery_sys_t *)malloc(
254 sizeof( services_discovery_sys_t ) );
255 if( p_sd->p_sys == NULL )
257 msg_Err( p_sd, "out of memory" );
261 memset( p_sys, 0, sizeof(*p_sys) );
263 p_sys->simple_poll = avahi_simple_poll_new();
264 if( p_sys->simple_poll == NULL )
266 msg_Err( p_sd, "failed to create avahi simple poll" );
270 p_sys->client = avahi_client_new( avahi_simple_poll_get(p_sys->simple_poll),
274 client_callback, p_sd, &err );
275 if( p_sys->client == NULL )
277 msg_Err( p_sd, "failed to create avahi client: %s",
278 avahi_strerror( err ) );
282 p_sys->sb = avahi_service_browser_new( p_sys->client, AVAHI_IF_UNSPEC,
284 "_vlc-http._tcp", NULL,
288 browse_callback, p_sd );
289 if( p_sys->sb == NULL )
291 msg_Err( p_sd, "failed to create avahi service browser" );
295 /* Create our playlist node */
296 p_sys->p_playlist = (playlist_t *)vlc_object_find( p_sd,
299 if( !p_sys->p_playlist )
301 msg_Warn( p_sd, "unable to find playlist, cancelling");
305 playlist_NodesCreateForSD( p_playlist, _("Bonjour"), &p_sys->p_node_cat,
306 &p_sys->p_node_one );
312 if( p_sys->p_playlist != NULL )
313 vlc_object_release( p_sys->p_playlist );
314 if( p_sys->sb != NULL )
315 avahi_service_browser_free( p_sys->sb );
316 if( p_sys->client != NULL )
317 avahi_client_free( p_sys->client );
318 if( p_sys->simple_poll != NULL )
319 avahi_simple_poll_free( p_sys->simple_poll );
321 free( (void *)p_sys );
326 /*****************************************************************************
328 *****************************************************************************/
329 static void Close( vlc_object_t *p_this )
331 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
332 services_discovery_sys_t *p_sys = p_sd->p_sys;
334 avahi_service_browser_free( p_sys->sb );
335 avahi_client_free( p_sys->client );
336 avahi_simple_poll_free( p_sys->simple_poll );
338 playlist_NodeDelete( p_sys->p_playlist, p_sys->p_node, VLC_TRUE, VLC_TRUE );
339 vlc_object_release( p_sys->p_playlist );
344 /*****************************************************************************
346 *****************************************************************************/
347 static void Run( services_discovery_t *p_sd )
349 services_discovery_sys_t *p_sys = p_sd->p_sys;
351 while( !p_sd->b_die )
353 if( avahi_simple_poll_iterate( p_sys->simple_poll, 100 ) != 0 )
355 msg_Err( p_sd, "poll iterate failed" );