]> git.sesse.net Git - vlc/blob - modules/access/dsm/sd.c
dsm: change item b_net and i_type
[vlc] / modules / access / dsm / sd.c
1 /**
2  * @file bdsm/sd.c
3  * @brief List host supporting NETBIOS on the local network
4  */
5 /*****************************************************************************
6  * Copyright © 2014 Authors and the VideoLAN team
7  *
8  * Authors: - Julien 'Lta' BALLET <contact # lta 'dot' io>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_atomic.h>
35 #include <vlc_services_discovery.h>
36 #include <bdsm/bdsm.h>
37
38 #if !defined(BDSM_VERSION_CURRENT) || BDSM_VERSION_CURRENT < 2
39 # error libdsm version current too low: need at least BDSM_VERSION_CURRENT 2
40 #endif
41
42 #define BROADCAST_TIMEOUT 6 // in seconds
43
44 int bdsm_SdOpen( vlc_object_t * );
45 void bdsm_SdClose( vlc_object_t * );
46 int bdsm_sd_probe_Open( vlc_object_t * );
47
48 struct entry_item
49 {
50     netbios_ns_entry *p_entry;
51     input_item_t *p_item;
52 };
53
54 struct services_discovery_sys_t
55 {
56     netbios_ns      *p_ns;
57     vlc_array_t     *p_entry_item_list;
58 };
59
60 int bdsm_sd_probe_Open (vlc_object_t *p_this)
61 {
62     vlc_probe_t *p_probe = (vlc_probe_t *)p_this;
63
64     vlc_sd_probe_Add( p_probe, "dsm{longname=\"Windows networks\"}",
65                       N_( "Windows networks" ), SD_CAT_LAN );
66
67     return VLC_PROBE_CONTINUE;
68 }
69
70 static void entry_item_append( services_discovery_t *p_sd,
71                                netbios_ns_entry *p_entry,
72                                input_item_t *p_item )
73 {
74     services_discovery_sys_t *p_sys = p_sd->p_sys;
75     struct entry_item *p_entry_item = calloc(1, sizeof(struct entry_item));
76
77     if( !p_entry_item )
78         return;
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 );
84 }
85
86 static void entry_item_remove( services_discovery_t *p_sd,
87                                netbios_ns_entry *p_entry )
88 {
89     services_discovery_sys_t *p_sys = p_sd->p_sys;
90
91     for ( int i = 0; i < vlc_array_count( p_sys->p_entry_item_list ); i++ )
92     {
93         struct entry_item *p_entry_item;
94
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  )
97         {
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 );
102             break;
103         }
104     }
105 }
106
107 static void netbios_ns_discover_on_entry_added( void *p_opaque,
108                                                 netbios_ns_entry *p_entry )
109 {
110     services_discovery_t *p_sd = (services_discovery_t *)p_opaque;
111
112     char type = netbios_ns_entry_type( p_entry );
113
114     if( type == NETBIOS_FILESERVER )
115     {
116         input_item_t *p_item;
117         char *psz_mrl;
118         const char *name = netbios_ns_entry_name( p_entry );
119
120         if( asprintf(&psz_mrl, "smb://%s", name) < 0 )
121             return;
122
123         p_item = input_item_NewWithTypeExt( psz_mrl, name, 0, NULL,
124                                             0, -1, ITEM_TYPE_NODE, 1 );
125         msg_Dbg( p_sd, "Adding item %s", psz_mrl );
126         free(psz_mrl);
127
128         entry_item_append( p_sd, p_entry, p_item );
129         vlc_gc_decref( p_item );
130     }
131 }
132
133 static void netbios_ns_discover_on_entry_removed( void *p_opaque,
134                                                   netbios_ns_entry *p_entry )
135 {
136     services_discovery_t *p_sd = (services_discovery_t *)p_opaque;
137
138     entry_item_remove( p_sd, p_entry );
139 }
140
141 int bdsm_SdOpen (vlc_object_t *p_this)
142 {
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;
146
147     if( p_sys == NULL )
148         return VLC_ENOMEM;
149
150     p_sd->p_sys = p_sys;
151     p_sys->p_entry_item_list = vlc_array_new();
152     if ( p_sys->p_entry_item_list == NULL )
153         return VLC_ENOMEM;
154
155     p_sys->p_ns = netbios_ns_new();
156     if( p_sys->p_ns == NULL )
157         goto error;
158
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;
162
163     if( !netbios_ns_discover_start( p_sys->p_ns, BROADCAST_TIMEOUT,
164                                     &callbacks) )
165         goto error;
166
167     return VLC_SUCCESS;
168
169     error:
170         bdsm_SdClose( p_this );
171         return VLC_EGENERIC;
172 }
173
174 void bdsm_SdClose (vlc_object_t *p_this)
175 {
176     services_discovery_t *sd = (services_discovery_t *)p_this;
177     services_discovery_sys_t *p_sys = sd->p_sys;
178
179     if( p_sys == NULL )
180         return;
181
182     if( p_sys->p_ns )
183     {
184         netbios_ns_discover_stop( p_sys->p_ns );
185         netbios_ns_destroy( p_sys->p_ns );
186     }
187
188     if( p_sys->p_entry_item_list )
189     {
190         for ( int i = 0; i < vlc_array_count( p_sys->p_entry_item_list ); i++ )
191         {
192             struct entry_item *p_entry_item;
193
194             p_entry_item = vlc_array_item_at_index( p_sys->p_entry_item_list,
195                                                     i );
196             vlc_gc_decref( p_entry_item->p_item );
197             free( p_entry_item );
198         }
199         vlc_array_destroy( p_sys->p_entry_item_list );
200     }
201
202     free( p_sys );
203 }