[vlc-devel] [PATCH 2/3] Add liBDSM based SMB host service discovery

Julien 'Lta' BALLET elthariel at gmail.com
Wed Jun 18 19:11:51 CEST 2014


From: Julien 'Lta' BALLET <contact at lta.io>

---
 modules/services_discovery/Makefile.am |   7 ++
 modules/services_discovery/bdsm.c      | 131 +++++++++++++++++++++++++++++++++
 2 files changed, 138 insertions(+)
 create mode 100644 modules/services_discovery/bdsm.c

diff --git a/modules/services_discovery/Makefile.am b/modules/services_discovery/Makefile.am
index 11fca94..2cbc92a 100644
--- a/modules/services_discovery/Makefile.am
+++ b/modules/services_discovery/Makefile.am
@@ -68,3 +68,10 @@ libos2drive_plugin_la_SOURCES = services_discovery/os2drive.c
 if HAVE_OS2
 sd_LTLIBRARIES += libos2drive_plugin.la
 endif
+
+libdsm_plugin_la_SOURCES = services_discovery/bdsm.c
+libdsm_plugin_la_CFLAGS = $(AM_CFLAGS) $(BDSM_CFLAGS)
+libdsm_plugin_la_LIBADD = $(BDSM_LIBS)
+libdsm_plugin_la_LDFLAGS = $(AM_LDFLAGS) -rpath '$(sddir)'
+sd_LTLIBRARIES += $(LTLIBdsm)
+EXTRA_LTLIBRARIES += libdsm_plugin.la
diff --git a/modules/services_discovery/bdsm.c b/modules/services_discovery/bdsm.c
new file mode 100644
index 0000000..c13434e
--- /dev/null
+++ b/modules/services_discovery/bdsm.c
@@ -0,0 +1,131 @@
+/**
+ * @file bdsm.c
+ * @brief List host supporting NETBIOS on the local network
+ */
+/*****************************************************************************
+ * Copyright © 2014 Authors and the VideoLAN team
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+/*****************************************************************************
+ * Preamble
+ *****************************************************************************/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <bdsm/netbios_ns.h>
+
+#include <vlc_common.h>
+#include <vlc_services_discovery.h>
+#include <vlc_plugin.h>
+
+static int Open( vlc_object_t * );
+static void Close( vlc_object_t * );
+static int vlc_sd_probe_Open( vlc_object_t * );
+
+vlc_module_begin()
+    set_shortname( N_( "Windows networks" ) )
+    set_description( N_( "Netbios speaking host on the Local network" ) )
+    set_category( CAT_PLAYLIST )
+    set_subcategory( SUBCAT_PLAYLIST_SD )
+    set_capability( "services_discovery", 0 )
+    set_callbacks( Open, Close )
+    add_shortcut( "smb", "bdsm" )
+
+    VLC_SD_PROBE_SUBMODULE
+
+vlc_module_end()
+
+struct services_discovery_sys_t
+{
+    netbios_ns      *ns;
+};
+
+static int vlc_sd_probe_Open (vlc_object_t *p_this)
+{
+    vlc_probe_t *p_probe = (vlc_probe_t *)p_this;
+
+    vlc_sd_probe_Add( p_probe, "dsm{longname=\"Windows networks\"}",
+                      N_( "Windows networks" ), SD_CAT_LAN );
+
+    return VLC_PROBE_CONTINUE;
+}
+
+static int Open (vlc_object_t *p_this)
+{
+    services_discovery_t *p_sd = (services_discovery_t *)p_this;
+    services_discovery_sys_t *p_sys = malloc (sizeof (*p_sys));
+
+    if( p_sys == NULL )
+        return VLC_ENOMEM;
+    p_sd->p_sys = p_sys;
+
+    /* Let's create a NETBIOS name service object */
+    p_sys->ns = netbios_ns_new();
+    if( p_sys->ns == NULL )
+        goto error;
+
+    if( !netbios_ns_discover( p_sys->ns ) )
+        goto error;
+
+    for( size_t i = 0; i < netbios_ns_entry_count( p_sys->ns ); i++ )
+    {
+        netbios_ns_entry *p_entry = netbios_ns_entry_at( p_sys->ns, i );
+
+        if( p_entry->type == 0x20 )
+        {
+            input_item_t *p_item;
+            char *psz_mrl;
+
+            /* XXX: Remove trailing whitespaces */
+            if( asprintf(&psz_mrl, "smb://%#s/", p_entry->name) < 0 )
+                goto error;
+
+            p_item = input_item_NewWithType( psz_mrl, p_entry->name, 0, NULL,
+                                             0, -1, ITEM_TYPE_NODE );
+            msg_Dbg( p_sd, "Adding item %s", psz_mrl );
+
+            services_discovery_AddItem( p_sd, p_item, NULL );
+
+            free( psz_mrl );
+        }
+    }
+
+    return VLC_SUCCESS;
+
+    error:
+        if( p_sys->ns != NULL )
+            netbios_ns_destroy( p_sys->ns );
+        free( p_sys );
+        p_sd->p_sys = NULL;
+
+        return VLC_EGENERIC;
+}
+
+static void Close (vlc_object_t *p_this)
+{
+    services_discovery_t *sd = (services_discovery_t *)p_this;
+    services_discovery_sys_t *p_sys = sd->p_sys;
+
+    if( p_sys == NULL )
+        return;
+
+    if( p_sys->ns != NULL )
+        netbios_ns_destroy( p_sys->ns );
+}
+
-- 
2.0.0




More information about the vlc-devel mailing list