3 * @brief List host supporting NETBIOS on the local network
5 /*****************************************************************************
6 * Copyright © 2014 Authors and the VideoLAN team
8 * Authors: - Julien 'Lta' BALLET <contact # lta 'dot' io>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_atomic.h>
35 #include <vlc_services_discovery.h>
36 #include <bdsm/bdsm.h>
38 #if !defined(BDSM_VERSION_CURRENT) || BDSM_VERSION_CURRENT < 2
39 # error libdsm version current too low: need at least BDSM_VERSION_CURRENT 2
42 #define BROADCAST_TIMEOUT 6 // in seconds
44 int bdsm_SdOpen( vlc_object_t * );
45 void bdsm_SdClose( vlc_object_t * );
46 int bdsm_sd_probe_Open( vlc_object_t * );
50 netbios_ns_entry *p_entry;
54 struct services_discovery_sys_t
57 vlc_array_t *p_entry_item_list;
60 int bdsm_sd_probe_Open (vlc_object_t *p_this)
62 vlc_probe_t *p_probe = (vlc_probe_t *)p_this;
64 vlc_sd_probe_Add( p_probe, "dsm{longname=\"Windows networks\"}",
65 N_( "Windows networks" ), SD_CAT_LAN );
67 return VLC_PROBE_CONTINUE;
70 static void entry_item_append( services_discovery_t *p_sd,
71 netbios_ns_entry *p_entry,
72 input_item_t *p_item )
74 services_discovery_sys_t *p_sys = p_sd->p_sys;
75 struct entry_item *p_entry_item = calloc(1, sizeof(struct entry_item));
79 p_entry_item->p_entry = p_entry;
80 p_entry_item->p_item = p_item;
81 vlc_gc_incref( p_item );
82 vlc_array_append( p_sys->p_entry_item_list, p_entry_item );
83 services_discovery_AddItem( p_sd, p_item, NULL );
86 static void entry_item_remove( services_discovery_t *p_sd,
87 netbios_ns_entry *p_entry )
89 services_discovery_sys_t *p_sys = p_sd->p_sys;
91 for ( int i = 0; i < vlc_array_count( p_sys->p_entry_item_list ); i++ )
93 struct entry_item *p_entry_item;
95 p_entry_item = vlc_array_item_at_index( p_sys->p_entry_item_list, i );
96 if( p_entry_item->p_entry == p_entry )
98 services_discovery_RemoveItem( p_sd, p_entry_item->p_item );
99 vlc_gc_decref( p_entry_item->p_item );
100 vlc_array_remove( p_sys->p_entry_item_list, i );
101 free( p_entry_item );
107 static void netbios_ns_discover_on_entry_added( void *p_opaque,
108 netbios_ns_entry *p_entry )
110 services_discovery_t *p_sd = (services_discovery_t *)p_opaque;
112 char type = netbios_ns_entry_type( p_entry );
114 if( type == NETBIOS_FILESERVER )
116 input_item_t *p_item;
118 const char *name = netbios_ns_entry_name( p_entry );
120 if( asprintf(&psz_mrl, "smb://%s", name) < 0 )
123 p_item = input_item_NewWithType( psz_mrl, name, 0, NULL,
124 0, -1, ITEM_TYPE_NODE );
125 msg_Dbg( p_sd, "Adding item %s", psz_mrl );
128 entry_item_append( p_sd, p_entry, p_item );
129 vlc_gc_decref( p_item );
133 static void netbios_ns_discover_on_entry_removed( void *p_opaque,
134 netbios_ns_entry *p_entry )
136 services_discovery_t *p_sd = (services_discovery_t *)p_opaque;
138 entry_item_remove( p_sd, p_entry );
141 int bdsm_SdOpen (vlc_object_t *p_this)
143 services_discovery_t *p_sd = (services_discovery_t *)p_this;
144 services_discovery_sys_t *p_sys = calloc (1, sizeof (*p_sys));
145 netbios_ns_discover_callbacks callbacks;
151 p_sys->p_entry_item_list = vlc_array_new();
152 if ( p_sys->p_entry_item_list == NULL )
155 p_sys->p_ns = netbios_ns_new();
156 if( p_sys->p_ns == NULL )
159 callbacks.p_opaque = p_sd;
160 callbacks.pf_on_entry_added = netbios_ns_discover_on_entry_added;
161 callbacks.pf_on_entry_removed = netbios_ns_discover_on_entry_removed;
163 if( !netbios_ns_discover_start( p_sys->p_ns, BROADCAST_TIMEOUT,
170 bdsm_SdClose( p_this );
174 void bdsm_SdClose (vlc_object_t *p_this)
176 services_discovery_t *sd = (services_discovery_t *)p_this;
177 services_discovery_sys_t *p_sys = sd->p_sys;
184 netbios_ns_discover_stop( p_sys->p_ns );
185 netbios_ns_destroy( p_sys->p_ns );
188 if( p_sys->p_entry_item_list )
190 for ( int i = 0; i < vlc_array_count( p_sys->p_entry_item_list ); i++ )
192 struct entry_item *p_entry_item;
194 p_entry_item = vlc_array_item_at_index( p_sys->p_entry_item_list,
196 vlc_gc_decref( p_entry_item->p_item );
197 free( p_entry_item );
199 vlc_array_destroy( p_sys->p_entry_item_list );