]> git.sesse.net Git - vlc/blob - modules/access/dsm/sd.c
2d8334b27326543253dcd2742916e31e4bdd5caa
[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 int bdsm_SdOpen( vlc_object_t * );
39 void bdsm_SdClose( vlc_object_t * );
40 int bdsm_sd_probe_Open( vlc_object_t * );
41
42 struct services_discovery_sys_t
43 {
44     netbios_ns      *p_ns;
45     vlc_thread_t    thread;
46
47     atomic_bool     stop;
48 };
49
50 int bdsm_sd_probe_Open (vlc_object_t *p_this)
51 {
52     vlc_probe_t *p_probe = (vlc_probe_t *)p_this;
53
54     vlc_sd_probe_Add( p_probe, "dsm{longname=\"Windows networks\"}",
55                       N_( "Windows networks" ), SD_CAT_LAN );
56
57     return VLC_PROBE_CONTINUE;
58 }
59
60 static void *Run( void *data )
61 {
62     services_discovery_t *p_sd = data;
63     services_discovery_sys_t *p_sys = p_sd->p_sys;
64
65     if( !netbios_ns_discover( p_sys->p_ns ) )
66         return NULL;
67
68     if (atomic_load(&p_sys->stop))
69         return NULL;
70
71     for( ssize_t i = 0; i < netbios_ns_entry_count( p_sys->p_ns ); i++ )
72     {
73         netbios_ns_entry *p_entry = netbios_ns_entry_at( p_sys->p_ns, i );
74         char type = netbios_ns_entry_type( p_entry );
75
76         if( type == 0x20 )
77         {
78             input_item_t *p_item;
79             char *psz_mrl;
80             const char *name = netbios_ns_entry_name( p_entry );
81
82             if( asprintf(&psz_mrl, "smb://%s", name) < 0 )
83                 return NULL;
84
85             p_item = input_item_NewWithType( psz_mrl, name, 0, NULL,
86                                              0, -1, ITEM_TYPE_NODE );
87             msg_Dbg( p_sd, "Adding item %s", psz_mrl );
88
89             services_discovery_AddItem( p_sd, p_item, NULL );
90
91         }
92     }
93     return NULL;
94 }
95
96 int bdsm_SdOpen (vlc_object_t *p_this)
97 {
98     services_discovery_t *p_sd = (services_discovery_t *)p_this;
99     services_discovery_sys_t *p_sys = calloc (1, sizeof (*p_sys));
100
101     if( p_sys == NULL )
102         return VLC_ENOMEM;
103
104     p_sd->p_sys = p_sys;
105
106     p_sys->p_ns = netbios_ns_new();
107     if( p_sys->p_ns == NULL )
108         goto error;
109
110     atomic_store(&p_sys->stop, false);
111
112     if( vlc_clone( &p_sys->thread, Run, p_sd, VLC_THREAD_PRIORITY_LOW ) )
113     {
114         p_sys->thread = 0;
115         goto error;
116     }
117
118     return VLC_SUCCESS;
119
120     error:
121         bdsm_SdClose( p_this );
122         return VLC_EGENERIC;
123 }
124
125 void bdsm_SdClose (vlc_object_t *p_this)
126 {
127     services_discovery_t *sd = (services_discovery_t *)p_this;
128     services_discovery_sys_t *p_sys = sd->p_sys;
129
130     if( p_sys == NULL )
131         return;
132
133     if( p_sys->thread ) {
134         atomic_store(&p_sys->stop, true);
135
136         if( p_sys->p_ns )
137             netbios_ns_abort( p_sys->p_ns );
138         vlc_join( p_sys->thread, NULL );
139     }
140     if( p_sys->p_ns )
141         netbios_ns_destroy( p_sys->p_ns );
142
143     free( p_sys );
144 }
145