77bb9f37119862b295ba48c7df9adc41b00dd238
[vlc.git] / modules / services_discovery / avahi.c
1 /*****************************************************************************
2  * avahi.c: Bonjour services discovery module
3  *****************************************************************************
4  * Copyright (C) 2005-2009, 2016 VideoLAN and VLC authors
5  *
6  * Authors: Jon Lech Johansen <jon@nanocrew.net>
7  *          Jean-Baptiste Kempf <jb@videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Includes
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #define VLC_MODULE_LICENSE VLC_LICENSE_GPL_2_PLUS
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_services_discovery.h>
36 #include <vlc_renderer_discovery.h>
37
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>
44
45 /*****************************************************************************
46  * Module descriptor
47  *****************************************************************************/
48
49 /* Callbacks */
50 static int  OpenSD ( vlc_object_t * );
51 static void CloseSD( vlc_object_t * );
52 static int  OpenRD ( vlc_object_t * );
53 static void CloseRD( vlc_object_t * );
54
55 VLC_SD_PROBE_HELPER("avahi", N_("Zeroconf network services"), SD_CAT_LAN)
56 VLC_RD_PROBE_HELPER( "avahi_renderer", "Avahi Zeroconf renderer Discovery" )
57
58 vlc_module_begin ()
59     set_shortname( "Avahi" )
60     set_description( N_("Zeroconf services") )
61     set_category( CAT_PLAYLIST )
62     set_subcategory( SUBCAT_PLAYLIST_SD )
63     set_capability( "services_discovery", 0 )
64     set_callbacks( OpenSD, CloseSD )
65     add_shortcut( "mdns", "avahi" )
66
67     VLC_SD_PROBE_SUBMODULE
68     add_submodule() \
69         set_description( N_( "Avahi Renderer Discovery" ) )
70         set_category( CAT_SOUT )
71         set_subcategory( SUBCAT_SOUT_RENDERER )
72         set_capability( "renderer_discovery", 0 )
73         set_callbacks( OpenRD, CloseRD )
74         add_shortcut( "mdns_renderer", "avahi_renderer" )
75         VLC_RD_PROBE_SUBMODULE
76 vlc_module_end ()
77
78 /*****************************************************************************
79  * Local structures
80  *****************************************************************************/
81
82 typedef struct
83 {
84     AvahiThreadedPoll   *poll;
85     AvahiClient         *client;
86     vlc_dictionary_t    services_name_to_input_item;
87     vlc_object_t        *parent;
88     bool                renderer;
89 } discovery_sys_t;
90
91 static const struct
92 {
93     const char *psz_protocol;
94     const char *psz_service_name;
95     bool        b_renderer;
96 } protocols[] = {
97     { "ftp", "_ftp._tcp", false },
98     { "smb", "_smb._tcp", false },
99     { "nfs", "_nfs._tcp", false },
100     { "sftp", "_sftp-ssh._tcp", false },
101     { "rtsp", "_rtsp._tcp", false },
102     { "chromecast", "_googlecast._tcp", true },
103 };
104 #define NB_PROTOCOLS (sizeof(protocols) / sizeof(*protocols))
105
106 static char* get_string_list_value( AvahiStringList* txt, const char* key )
107 {
108     AvahiStringList *asl = avahi_string_list_find( txt, key );
109     if( asl == NULL )
110         return NULL;
111     char* res = NULL;
112     char *sl_key = NULL;
113     char *sl_value = NULL;
114     if( avahi_string_list_get_pair( asl, &sl_key, &sl_value, NULL ) == 0 &&
115         sl_value != NULL )
116     {
117         res = strdup( sl_value );
118     }
119
120     if( sl_key != NULL )
121         avahi_free( (void *)sl_key );
122     if( sl_value != NULL )
123         avahi_free( (void *)sl_value );
124     return res;
125 }
126
127 /*****************************************************************************
128  * helpers
129  *****************************************************************************/
130 static void add_renderer( const char *psz_protocol, const char *psz_name,
131                           const char *psz_addr, uint16_t i_port,
132                           AvahiStringList *txt, discovery_sys_t *p_sys )
133 {
134     vlc_renderer_discovery_t *p_rd = ( vlc_renderer_discovery_t* )(p_sys->parent);
135     AvahiStringList *asl = NULL;
136     char *friendly_name = NULL;
137     char *icon_uri = NULL;
138     char *uri = NULL;
139     char *model = NULL;
140     const char *demux = NULL;
141     const char *extra_uri = NULL;
142     int renderer_flags = 0;
143
144     if( !strcmp( "chromecast", psz_protocol ) ) {
145         /* Capabilities */
146         asl = avahi_string_list_find( txt, "ca" );
147         if( asl != NULL ) {
148             char *key = NULL;
149             char *value = NULL;
150             if( avahi_string_list_get_pair( asl, &key, &value, NULL ) == 0 &&
151                 value != NULL )
152             {
153                 int ca = atoi( value );
154
155                 if( ( ca & 0x01 ) != 0 )
156                     renderer_flags |= VLC_RENDERER_CAN_VIDEO;
157                 if( ( ca & 0x04 ) != 0 )
158                     renderer_flags |= VLC_RENDERER_CAN_AUDIO;
159             }
160
161             if( key != NULL )
162                 avahi_free( (void *)key );
163             if( value != NULL )
164                 avahi_free( (void *)value );
165         }
166
167         /* Friendly name */
168         friendly_name = get_string_list_value( txt, "fn" );
169
170         /* Icon */
171         char* icon_raw = get_string_list_value( txt, "ic" );
172         if( icon_raw != NULL ) {
173             if( asprintf( &icon_uri, "http://%s:8008%s", psz_addr, icon_raw) < 0 )
174                 icon_uri = NULL;
175             free( icon_raw );
176         }
177
178         model = get_string_list_value( txt, "md" );
179
180         if( asprintf( &uri, "%s://%s:%u", psz_protocol, psz_addr, i_port ) < 0 )
181             goto error;
182
183         extra_uri = renderer_flags & VLC_RENDERER_CAN_VIDEO ? NULL : "no-video";
184         demux = "cc_demux";
185     }
186
187     if ( friendly_name && model ) {
188         char* combined;
189         if ( asprintf( &combined, "%s (%s)", friendly_name, model ) == -1 )
190             combined = NULL;
191         if ( combined != NULL ) {
192             free(friendly_name);
193             friendly_name = combined;
194         }
195     }
196
197     vlc_renderer_item_t *p_renderer_item =
198         vlc_renderer_item_new( psz_protocol, friendly_name ? friendly_name : psz_name, uri, extra_uri,
199                                demux, icon_uri, renderer_flags );
200     if( p_renderer_item == NULL )
201         goto error;
202
203     vlc_dictionary_insert( &p_sys->services_name_to_input_item,
204         psz_name, p_renderer_item);
205     vlc_rd_add_item( p_rd, p_renderer_item );
206
207 error:
208     free( friendly_name );
209     free( icon_uri );
210     free( uri );
211     free( model );
212 }
213
214 /*****************************************************************************
215  * client_callback
216  *****************************************************************************/
217 static void client_callback( AvahiClient *c, AvahiClientState state,
218                              void * userdata )
219 {
220     discovery_sys_t *p_sys = userdata;
221
222     if( state == AVAHI_CLIENT_FAILURE &&
223         (avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) )
224     {
225         msg_Err( p_sys->parent, "avahi client disconnected" );
226         avahi_threaded_poll_quit( p_sys->poll );
227     }
228 }
229
230 /*****************************************************************************
231  * resolve_callback
232  *****************************************************************************/
233 static void resolve_callback(
234     AvahiServiceResolver *r,
235     AvahiIfIndex interface,
236     AvahiProtocol protocol,
237     AvahiResolverEvent event,
238     const char *name,
239     const char *type,
240     const char *domain,
241     const char *host_name,
242     const AvahiAddress *address,
243     uint16_t port,
244     AvahiStringList *txt,
245     AvahiLookupResultFlags flags,
246     void* userdata )
247 {
248     discovery_sys_t *p_sys = userdata;
249
250     VLC_UNUSED(interface); VLC_UNUSED(host_name);
251     VLC_UNUSED(flags);
252
253     if( event == AVAHI_RESOLVER_FAILURE )
254     {
255         msg_Err( p_sys->parent,
256                  "failed to resolve service '%s' of type '%s' in domain '%s'",
257                  name, type, domain );
258     }
259     else if( event == AVAHI_RESOLVER_FOUND )
260     {
261         char a[128];
262         char *psz_uri = NULL;
263         char *psz_addr = NULL;
264         AvahiStringList *asl = NULL;
265         input_item_t *p_input = NULL;
266
267         msg_Info( p_sys->parent, "service '%s' of type '%s' in domain '%s' port %i",
268                   name, type, domain, port );
269
270         avahi_address_snprint(a, (sizeof(a)/sizeof(a[0]))-1, address);
271         if( protocol == AVAHI_PROTO_INET6 )
272             if( asprintf( &psz_addr, "[%s]", a ) == -1 )
273             {
274                 avahi_service_resolver_free( r );
275                 return;
276             }
277
278         const char *psz_protocol = NULL;
279         bool is_renderer = false;
280         for( unsigned int i = 0; i < NB_PROTOCOLS; i++ )
281         {
282             if( !strcmp(type, protocols[i].psz_service_name) )
283             {
284                 psz_protocol = protocols[i].psz_protocol;
285                 is_renderer = protocols[i].b_renderer;
286                 break;
287             }
288         }
289         if( psz_protocol == NULL )
290         {
291             free( psz_addr );
292             avahi_service_resolver_free( r );
293             return;
294         }
295
296         if( txt != NULL && is_renderer )
297         {
298             const char* addr_v4v6 = psz_addr != NULL ? psz_addr : a;
299             add_renderer( psz_protocol, name, addr_v4v6, port, txt, p_sys );
300             free( psz_addr );
301             avahi_service_resolver_free( r );
302             return;
303         }
304
305         if( txt != NULL )
306             asl = avahi_string_list_find( txt, "path" );
307         if( asl != NULL )
308         {
309             size_t size;
310             char *key = NULL;
311             char *value = NULL;
312             if( avahi_string_list_get_pair( asl, &key, &value, &size ) == 0 &&
313                 value != NULL )
314             {
315                 if( asprintf( &psz_uri, "%s://%s:%d%s",
316                           psz_protocol,
317                           psz_addr != NULL ? psz_addr : a,
318                           port, value ) == -1 )
319                 {
320                     free( psz_addr );
321                     avahi_service_resolver_free( r );
322                     return;
323                 }
324             }
325             if( key != NULL )
326                 avahi_free( (void *)key );
327             if( value != NULL )
328                 avahi_free( (void *)value );
329         }
330         else
331         {
332             if( asprintf( &psz_uri, "%s://%s:%d",
333                       psz_protocol,
334                       psz_addr != NULL ? psz_addr : a, port ) == -1 )
335             {
336                 free( psz_addr );
337                 avahi_service_resolver_free( r );
338                 return;
339             }
340         }
341
342         free( psz_addr );
343
344         if( psz_uri != NULL )
345         {
346             p_input = input_item_NewDirectory( psz_uri, name, ITEM_NET );
347             free( psz_uri );
348         }
349         if( p_input != NULL )
350         {
351             services_discovery_t *p_sd = ( services_discovery_t* )(p_sys->parent);
352             vlc_dictionary_insert( &p_sys->services_name_to_input_item,
353                 name, p_input );
354             services_discovery_AddItem( p_sd, p_input );
355        }
356     }
357
358     avahi_service_resolver_free( r );
359 }
360
361 /*****************************************************************************
362  * browser_callback
363  *****************************************************************************/
364 static void browse_callback(
365     AvahiServiceBrowser *b,
366     AvahiIfIndex interface,
367     AvahiProtocol protocol,
368     AvahiBrowserEvent event,
369     const char *name,
370     const char *type,
371     const char *domain,
372     AvahiLookupResultFlags flags,
373     void* userdata )
374 {
375     VLC_UNUSED(b);
376     VLC_UNUSED(flags);
377     discovery_sys_t *p_sys = userdata;
378     if( event == AVAHI_BROWSER_NEW )
379     {
380         if( avahi_service_resolver_new( p_sys->client, interface, protocol,
381                                         name, type, domain, AVAHI_PROTO_UNSPEC,
382                                         0,
383                                         resolve_callback, userdata ) == NULL )
384         {
385             msg_Err( p_sys->parent, "failed to resolve service '%s': %s", name,
386                      avahi_strerror( avahi_client_errno( p_sys->client ) ) );
387         }
388     }
389     else if( event == AVAHI_BROWSER_REMOVE && name )
390     {
391         /** \todo Store the input id and search it, rather than searching the items */
392         void *p_item;
393         p_item = vlc_dictionary_value_for_key(
394                         &p_sys->services_name_to_input_item,
395                         name );
396         if( !p_item )
397             msg_Err( p_sys->parent, "failed to find service '%s' in playlist", name );
398         else
399         {
400             if( p_sys->renderer )
401             {
402                 vlc_renderer_discovery_t *p_rd = ( vlc_renderer_discovery_t* )(p_sys->parent);
403                 vlc_rd_remove_item( p_rd, p_item );
404                 vlc_renderer_item_release( p_item );
405             }
406             else
407             {
408                 services_discovery_t *p_sd = ( services_discovery_t* )(p_sys->parent);
409                 services_discovery_RemoveItem( p_sd, p_item );
410                 input_item_Release( p_item );
411             }
412             vlc_dictionary_remove_value_for_key(
413                         &p_sys->services_name_to_input_item,
414                         name, NULL, NULL );
415         }
416     }
417 }
418
419 static void clear_input_item( void* p_item, void* p_obj )
420 {
421     VLC_UNUSED( p_obj );
422     input_item_Release( p_item );
423 }
424
425 static void clear_renderer_item( void* p_item, void* p_obj )
426 {
427     VLC_UNUSED( p_obj );
428     vlc_renderer_item_release( p_item );
429 }
430
431 /*****************************************************************************
432  * Open: initialize and create stuff
433  *****************************************************************************/
434 static int OpenCommon( discovery_sys_t *p_sys )
435 {
436     int err;
437
438     vlc_dictionary_init( &p_sys->services_name_to_input_item, 1 );
439
440     p_sys->poll = avahi_threaded_poll_new();
441     if( p_sys->poll == NULL )
442     {
443         msg_Err( p_sys->parent, "failed to create Avahi threaded poll" );
444         goto error;
445     }
446
447     p_sys->client = avahi_client_new( avahi_threaded_poll_get(p_sys->poll),
448                                       0, client_callback, p_sys, &err );
449     if( p_sys->client == NULL )
450     {
451         msg_Err( p_sys->parent, "failed to create avahi client: %s",
452                  avahi_strerror( err ) );
453         goto error;
454     }
455
456     for( unsigned i = 0; i < NB_PROTOCOLS; i++ )
457     {
458         if( protocols[i].b_renderer != p_sys->renderer )
459             continue;
460
461         AvahiServiceBrowser *sb;
462         sb = avahi_service_browser_new( p_sys->client, AVAHI_IF_UNSPEC,
463                 AVAHI_PROTO_UNSPEC,
464                 protocols[i].psz_service_name, NULL,
465                 0, browse_callback, p_sys );
466         if( sb == NULL )
467         {
468             msg_Err( p_sys->parent, "failed to create avahi service browser %s", avahi_strerror( avahi_client_errno(p_sys->client) ) );
469             goto error;
470         }
471     }
472
473     avahi_threaded_poll_start( p_sys->poll );
474
475     return VLC_SUCCESS;
476
477 error:
478     if( p_sys->client != NULL )
479         avahi_client_free( p_sys->client );
480     if( p_sys->poll != NULL )
481         avahi_threaded_poll_free( p_sys->poll );
482
483     return VLC_EGENERIC;
484 }
485
486 static int OpenSD( vlc_object_t *p_this )
487 {
488     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
489     p_sd->description = _("Zeroconf network services");
490
491     discovery_sys_t *p_sys = p_sd->p_sys = calloc( 1, sizeof( discovery_sys_t ) );
492     if( !p_sd->p_sys )
493         return VLC_ENOMEM;
494     p_sys->parent = p_this;
495     p_sys->renderer = false;
496
497     int ret = OpenCommon( p_sys );
498     if( ret != VLC_SUCCESS )
499     {
500         vlc_dictionary_clear( &p_sys->services_name_to_input_item,
501                               clear_input_item, NULL );
502         free( p_sys );
503     }
504     return ret;
505 }
506
507 static int OpenRD( vlc_object_t *p_this )
508 {
509     vlc_renderer_discovery_t *p_rd = (vlc_renderer_discovery_t *)p_this;
510
511     discovery_sys_t *p_sys = p_rd->p_sys = calloc( 1, sizeof( discovery_sys_t ) );
512     if( !p_rd->p_sys )
513         return VLC_ENOMEM;
514     p_sys->parent = p_this;
515     p_sys->renderer = true;
516
517     int ret = OpenCommon( p_sys );
518     if( ret != VLC_SUCCESS )
519     {
520         vlc_dictionary_clear( &p_sys->services_name_to_input_item,
521                               clear_renderer_item, NULL );
522         free( p_sys );
523     }
524     return ret;
525 }
526
527 /*****************************************************************************
528  * Close: cleanup
529  *****************************************************************************/
530 static void CloseCommon( discovery_sys_t *p_sys )
531 {
532     avahi_threaded_poll_stop( p_sys->poll );
533
534     avahi_client_free( p_sys->client );
535     avahi_threaded_poll_free( p_sys->poll );
536
537 }
538
539 static void CloseSD( vlc_object_t *p_this )
540 {
541     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
542     discovery_sys_t *p_sys = p_sd->p_sys;
543     CloseCommon( p_sys );
544     vlc_dictionary_clear( &p_sys->services_name_to_input_item,
545                           clear_input_item, NULL );
546     free( p_sys );
547 }
548
549 static void CloseRD( vlc_object_t *p_this )
550 {
551     vlc_renderer_discovery_t *p_rd = (vlc_renderer_discovery_t *)p_this;
552     discovery_sys_t *p_sys = p_rd->p_sys;
553     CloseCommon( p_sys );
554     vlc_dictionary_clear( &p_sys->services_name_to_input_item,
555                           clear_renderer_item, NULL );
556     free( p_sys );
557 }