access.c 19 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*****************************************************************************
 * bdsm/access.c: liBDSM based SMB/CIFS access module
 *****************************************************************************
 * Copyright (C) 2001-2014 VLC authors and VideoLAN
 *
 * Authors: Julien 'Lta' BALLET <contact # lta 'dot' io>
 *
 * 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

Thomas Guillem's avatar
Thomas Guillem committed
30 31 32 33
#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_services_discovery.h>
#include <vlc_url.h>
34 35
#include <vlc_access.h>
#include <vlc_variables.h>
Thomas Guillem's avatar
Thomas Guillem committed
36
#include <vlc_keystore.h>
37
#include <vlc_network.h>
38

39
#include <assert.h>
40
#include <string.h>
41 42 43 44 45 46
#ifdef HAVE_SYS_SOCKET_H
# include <sys/socket.h>
# include <netinet/in.h>
# include <arpa/inet.h>
# include <netdb.h>
#endif
47

Thomas Guillem's avatar
Thomas Guillem committed
48
#include <bdsm/bdsm.h>
49
#include "../smb_common.h"
Thomas Guillem's avatar
Thomas Guillem committed
50

51 52 53
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
Thomas Guillem's avatar
Thomas Guillem committed
54 55 56 57
int bdsm_SdOpen( vlc_object_t * );
void bdsm_SdClose( vlc_object_t * );
int bdsm_sd_probe_Open( vlc_object_t * );

58 59 60
static int Open( vlc_object_t * );
static void Close( vlc_object_t * );

61
VLC_SD_PROBE_HELPER( "dsm", N_("Windows networks"), SD_CAT_LAN )
62 63 64 65 66 67 68 69 70 71

#define BDSM_HELP N_("libdsm's SMB (Windows network shares) input and browser")

vlc_module_begin ()
    set_shortname( "dsm" )
    set_description( N_("libdsm SMB input") )
    set_help(BDSM_HELP)
    set_capability( "access", 20 )
    set_category( CAT_INPUT )
    set_subcategory( SUBCAT_INPUT_ACCESS )
72 73 74
    add_string( "smb-user", NULL, SMB_USER_TEXT, SMB_USER_LONGTEXT, false )
    add_password( "smb-pwd", NULL, SMB_PASS_TEXT, SMB_PASS_LONGTEXT, false )
    add_string( "smb-domain", NULL, SMB_DOMAIN_TEXT, SMB_DOMAIN_LONGTEXT, false )
75 76
    add_shortcut( "smb", "cifs" )
    set_callbacks( Open, Close )
77 78

    add_submodule()
79
        add_shortcut( "dsm-sd" )
80
        set_description( N_("libdsm NETBIOS discovery module") )
81 82 83
        set_category( CAT_PLAYLIST )
        set_subcategory( SUBCAT_PLAYLIST_SD )
        set_capability( "services_discovery", 0 )
84
        set_callbacks( bdsm_SdOpen, bdsm_SdClose )
85 86 87

        VLC_SD_PROBE_SUBMODULE

88 89 90 91 92
vlc_module_end ()

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
93 94 95 96 97 98 99 100
static ssize_t Read( stream_t *, void *, size_t );
static int Seek( stream_t *, uint64_t );
static int Control( stream_t *, int, va_list );
static int BrowserInit( stream_t *p_access );

static int get_address( stream_t *p_access );
static int login( stream_t *p_access );
static bool get_path( stream_t *p_access );
101
static int add_item( stream_t *p_access,  struct vlc_readdir_helper *p_rdh,
102
                     const char *psz_name, int i_type );
Thomas Guillem's avatar
Thomas Guillem committed
103 104 105 106 107 108 109

struct access_sys_t
{
    netbios_ns         *p_ns;               /**< Netbios name service */
    smb_session        *p_session;          /**< bdsm SMB Session object */

    vlc_url_t           url;
110 111 112
    char               *psz_fullpath;
    const char         *psz_share;
    const char         *psz_path;
Thomas Guillem's avatar
Thomas Guillem committed
113 114 115 116 117 118 119

    char                netbios_name[16];
    struct in_addr      addr;

    smb_fd              i_fd;               /**< SMB fd for the file we're reading */
    smb_tid             i_tid;              /**< SMB Tree ID we're connected to */
};
120 121 122 123

/*****************************************************************************
 * Open: Initialize module's data structures and libdsm
 *****************************************************************************/
124
static int Open( vlc_object_t *p_this )
125
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
126
    stream_t     *p_access = (stream_t*)p_this;
127
    access_sys_t *p_sys;
Thomas Guillem's avatar
Thomas Guillem committed
128
    smb_stat st;
129 130 131 132 133 134

    /* Init p_access */
    p_sys = p_access->p_sys = (access_sys_t*)calloc( 1, sizeof( access_sys_t ) );
    if( p_access->p_sys == NULL )
        return VLC_ENOMEM;

Thomas Guillem's avatar
Thomas Guillem committed
135 136 137 138 139 140 141 142
    p_sys->p_ns = netbios_ns_new();
    if( p_sys->p_ns == NULL )
        goto error;

    p_sys->p_session = smb_session_new();
    if( p_sys->p_session == NULL )
        goto error;

143 144
    if( vlc_UrlParseFixup( &p_sys->url, p_access->psz_url ) != 0 )
        goto error;
Thomas Guillem's avatar
Thomas Guillem committed
145

Thomas Guillem's avatar
Thomas Guillem committed
146
    if( get_address( p_access ) != VLC_SUCCESS )
147 148
        goto error;

Thomas Guillem's avatar
Thomas Guillem committed
149 150 151 152
    msg_Dbg( p_access, "Session: Host name = %s, ip = %s", p_sys->netbios_name,
             inet_ntoa( p_sys->addr ) );

    /* Now that we have the required data, let's establish a session */
153 154 155
    if( smb_session_connect( p_sys->p_session, p_sys->netbios_name,
                             p_sys->addr.s_addr, SMB_TRANSPORT_TCP )
                             != DSM_SUCCESS )
Thomas Guillem's avatar
Thomas Guillem committed
156 157 158 159 160
    {
        msg_Err( p_access, "Unable to connect/negotiate SMB session");
        goto error;
    }

161 162
    get_path( p_access );

Thomas Guillem's avatar
Thomas Guillem committed
163
    if( login( p_access ) != VLC_SUCCESS )
164
    {
165 166
        msg_Err( p_access, "Unable to open file with path %s (in share %s)",
                 p_sys->psz_path, p_sys->psz_share );
Thomas Guillem's avatar
Thomas Guillem committed
167
        goto error;
168
    }
Thomas Guillem's avatar
Thomas Guillem committed
169

170 171
    /* If there is no shares, browse them */
    if( !p_sys->psz_share )
172
        return BrowserInit( p_access );
Thomas Guillem's avatar
Thomas Guillem committed
173

174 175
    assert(p_sys->i_fd > 0);

176 177 178
    msg_Dbg( p_access, "Path: Share name = %s, path = %s", p_sys->psz_share,
             p_sys->psz_path );

Thomas Guillem's avatar
Thomas Guillem committed
179
    st = smb_stat_fd( p_sys->p_session, p_sys->i_fd );
180 181 182 183 184 185 186
    if( smb_stat_get( st, SMB_STAT_ISDIR ) )
    {
        smb_fclose( p_sys->p_session, p_sys->i_fd );
        return BrowserInit( p_access );
    }

    msg_Dbg( p_access, "Successfully opened smb://%s", p_access->psz_location );
Thomas Guillem's avatar
Thomas Guillem committed
187

188 189 190 191
    ACCESS_SET_CALLBACKS( Read, NULL, Control, Seek );
    return VLC_SUCCESS;

    error:
Thomas Guillem's avatar
Thomas Guillem committed
192
        Close( p_this );
193 194 195 196 197 198
        return VLC_EGENERIC;
}

/*****************************************************************************
 * Close: free unused data structures
 *****************************************************************************/
199
static void Close( vlc_object_t *p_this )
200
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
201
    stream_t     *p_access = (stream_t*)p_this;
202 203
    access_sys_t *p_sys = p_access->p_sys;

Thomas Guillem's avatar
Thomas Guillem committed
204 205 206 207 208 209
    if( p_sys->p_ns )
        netbios_ns_destroy( p_sys->p_ns );
    if( p_sys->i_fd )
        smb_fclose( p_sys->p_session, p_sys->i_fd );
    if( p_sys->p_session )
        smb_session_destroy( p_sys->p_session );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
210
    vlc_UrlClean( &p_sys->url );
211
    free( p_sys->psz_fullpath );
212

213 214 215 216 217 218 219
    free( p_sys );
}

/*****************************************************************************
 * Local functions
 *****************************************************************************/

Thomas Guillem's avatar
Thomas Guillem committed
220
/* Returns VLC_EGENERIC if it wasn't able to get an ip address to connect to */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
221
static int get_address( stream_t *p_access )
222
{
Thomas Guillem's avatar
Thomas Guillem committed
223 224
    access_sys_t *p_sys = p_access->p_sys;

225 226
    if( p_sys->url.psz_host != NULL &&
        !inet_pton( AF_INET, p_sys->url.psz_host, &p_sys->addr ) )
227 228 229 230 231
    {
        /* This is not an ip address, let's try netbios/dns resolve */
        struct addrinfo *p_info = NULL;

        /* Is this a netbios name on this LAN ? */
232 233
        if( netbios_ns_resolve( p_sys->p_ns, p_sys->url.psz_host,
                                NETBIOS_FILESERVER,
234
                                &p_sys->addr.s_addr) == 0 )
235 236
        {
            strlcpy( p_sys->netbios_name, p_sys->url.psz_host, 16);
Thomas Guillem's avatar
Thomas Guillem committed
237
            return VLC_SUCCESS;
238 239 240 241 242 243 244 245 246 247
        }
        /* or is it an existing dns name ? */
        else if( getaddrinfo( p_sys->url.psz_host, NULL, NULL, &p_info ) == 0 )
        {
            if( p_info->ai_family == AF_INET )
            {
                struct sockaddr_in *in = (struct sockaddr_in *)p_info->ai_addr;
                p_sys->addr.s_addr = in->sin_addr.s_addr;
            }
            if( p_info->ai_family != AF_INET )
248 249
            {
                freeaddrinfo( p_info );
Thomas Guillem's avatar
Thomas Guillem committed
250
                return VLC_EGENERIC;
251 252
            }
            freeaddrinfo( p_info );
253 254
        }
        else
Thomas Guillem's avatar
Thomas Guillem committed
255
            return VLC_EGENERIC;
256 257 258 259 260 261 262 263 264 265 266 267 268
    }

    /* We have an IP address, let's find the NETBIOS name */
    const char *psz_nbt = netbios_ns_inverse( p_sys->p_ns, p_sys->addr.s_addr );
    if( psz_nbt != NULL )
        strlcpy( p_sys->netbios_name, psz_nbt, 16 );
    else
    {
        msg_Warn( p_access, "Unable to get netbios name of %s",
            p_sys->url.psz_host );
        p_sys->netbios_name[0] = '\0';
    }

Thomas Guillem's avatar
Thomas Guillem committed
269
    return VLC_SUCCESS;
270 271
}

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
272
static int smb_connect( stream_t *p_access, const char *psz_login,
273
                        const char *psz_password, const char *psz_domain)
274
{
Thomas Guillem's avatar
Thomas Guillem committed
275 276
    access_sys_t *p_sys = p_access->p_sys;

277
    smb_session_set_creds( p_sys->p_session, psz_domain,
278
                           psz_login, psz_password );
279
    if( smb_session_login( p_sys->p_session ) == DSM_SUCCESS )
280 281 282 283
    {
        if( p_sys->psz_share )
        {
            /* Connect to the share */
284 285
            if( smb_tree_connect( p_sys->p_session, p_sys->psz_share,
                                  &p_sys->i_tid ) != DSM_SUCCESS )
286
                return VLC_EGENERIC;
287 288

            /* Let's finally ask a handle to the file we wanna read ! */
289 290 291
            return smb_fopen( p_sys->p_session, p_sys->i_tid, p_sys->psz_path,
                              SMB_MOD_RO, &p_sys->i_fd )
                              == DSM_SUCCESS ? VLC_SUCCESS : VLC_EGENERIC;
292
        }
293 294
        else
            return VLC_SUCCESS;
295 296 297
    }
    else
        return VLC_EGENERIC;
298 299
}

300 301
/* Performs login with existing credentials and ask the user for new ones on
   failure */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
302
static int login( stream_t *p_access )
303
{
Thomas Guillem's avatar
Thomas Guillem committed
304
    int i_ret = VLC_EGENERIC;
Thomas Guillem's avatar
Thomas Guillem committed
305
    access_sys_t *p_sys = p_access->p_sys;
Thomas Guillem's avatar
Thomas Guillem committed
306 307 308
    vlc_credential credential;
    char *psz_var_domain;
    const char *psz_login, *psz_password, *psz_domain;
309
    bool b_guest = false;
Thomas Guillem's avatar
Thomas Guillem committed
310

311
    vlc_credential_init( &credential, &p_sys->url );
Thomas Guillem's avatar
Thomas Guillem committed
312
    psz_var_domain = var_InheritString( p_access, "smb-domain" );
313
    credential.psz_realm = psz_var_domain ? psz_var_domain : NULL;
Thomas Guillem's avatar
Thomas Guillem committed
314 315 316 317 318 319 320

    vlc_credential_get( &credential, p_access, "smb-user", "smb-pwd",
                        NULL, NULL );

    if( !credential.psz_username )
    {
        psz_login = "Guest";
321
        psz_password = "";
322
        b_guest = true;
Thomas Guillem's avatar
Thomas Guillem committed
323 324 325 326 327 328
    }
    else
    {
        psz_login = credential.psz_username;
        psz_password = credential.psz_password;
    }
329
    psz_domain = credential.psz_realm ? credential.psz_realm : p_sys->netbios_name;
Thomas Guillem's avatar
Thomas Guillem committed
330

331
    /* Try to authenticate on the remote machine */
Thomas Guillem's avatar
Thomas Guillem committed
332 333
    if( smb_connect( p_access, psz_login, psz_password, psz_domain )
                     != VLC_SUCCESS )
334
    {
335
        while( vlc_credential_get( &credential, p_access, "smb-user", "smb-pwd",
336 337
                                   SMB_LOGIN_DIALOG_TITLE,
                                   SMB_LOGIN_DIALOG_TEXT, p_sys->netbios_name ) )
338
        {
339
            b_guest = false;
Thomas Guillem's avatar
Thomas Guillem committed
340 341
            psz_login = credential.psz_username;
            psz_password = credential.psz_password;
342 343
            psz_domain = credential.psz_realm ? credential.psz_realm
                                              : p_sys->netbios_name;
Thomas Guillem's avatar
Thomas Guillem committed
344 345 346
            if( smb_connect( p_access, psz_login, psz_password, psz_domain )
                             == VLC_SUCCESS )
                goto success;
347 348
        }

349
        msg_Err( p_access, "Unable to login" );
Thomas Guillem's avatar
Thomas Guillem committed
350
        goto error;
351
    }
352
    else if( smb_session_is_guest( p_sys->p_session ) == 1 )
353
    {
354
        msg_Warn( p_access, "Login failure but you were logged in as a Guest");
355 356
        b_guest = true;
    }
357

Thomas Guillem's avatar
Thomas Guillem committed
358
success:
359
    msg_Warn( p_access, "Creds: username = '%s', domain = '%s'",
Thomas Guillem's avatar
Thomas Guillem committed
360
             psz_login, psz_domain );
361
    if( !b_guest )
362
        vlc_credential_store( &credential, p_access );
363

Thomas Guillem's avatar
Thomas Guillem committed
364 365 366 367 368
    i_ret = VLC_SUCCESS;
error:
    vlc_credential_clean( &credential );
    free( psz_var_domain );
    return i_ret;
369 370
}

371
static void backslash_path( char *psz_path )
372
{
373
    char *iter = psz_path;
374 375 376 377 378 379 380 381 382 383 384

    /* Let's switch the path delimiters from / to \ */
    while( *iter != '\0' )
    {
        if( *iter == '/' )
            *iter = '\\';
        iter++;
    }
}

/* Get the share and filepath from uri (also replace all / by \ in url.psz_path) */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
385
static bool get_path( stream_t *p_access )
386
{
Thomas Guillem's avatar
Thomas Guillem committed
387
    access_sys_t *p_sys = p_access->p_sys;
388 389 390 391 392
    char *iter;

    if( p_sys->url.psz_path == NULL )
        return false;

393 394 395 396 397
    p_sys->psz_fullpath = vlc_uri_decode_duplicate( p_sys->url.psz_path );
    if( p_sys->psz_fullpath == NULL )
        return false;

    backslash_path( p_sys->psz_fullpath );
398 399

    /* Is path longer than just "/" ? */
400
    if( strlen( p_sys->psz_fullpath ) > 1 )
401
    {
402
        iter = p_sys->psz_fullpath;
403 404
        while( *iter == '\\' ) iter++; /* Handle smb://Host/////Share/ */

405
        p_sys->psz_share = iter;
406 407 408 409 410 411 412 413 414 415 416 417
    }
    else
    {
        msg_Dbg( p_access, "no share, nor file path provided, will switch to browser");
        return false;
    }

    iter = strchr( p_sys->psz_share, '\\' );
    if( iter == NULL || strlen(iter + 1) == 0 )
    {
        if( iter != NULL ) /* Remove the trailing \ */
            *iter = '\0';
418
        p_sys->psz_path = "";
419 420 421 422 423

        msg_Dbg( p_access, "no file path provided, will switch to browser ");
        return true;
    }

424
    p_sys->psz_path = iter + 1; /* Skip the first \ */
425 426 427 428 429 430 431 432
    *iter = '\0';

    return true;
}

/*****************************************************************************
 * Seek: try to go at the right place
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
433
static int Seek( stream_t *p_access, uint64_t i_pos )
434 435 436 437 438 439 440 441
{
    access_sys_t *p_sys = p_access->p_sys;

    if( i_pos >= INT64_MAX )
        return VLC_EGENERIC;

    msg_Dbg( p_access, "seeking to %"PRId64, i_pos );

442 443
    if (smb_fseek(p_sys->p_session, p_sys->i_fd, i_pos, SMB_SEEK_SET) == -1)
        return VLC_EGENERIC;
444 445 446 447 448 449 450

    return VLC_SUCCESS;
}

/*****************************************************************************
 * Read:
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
451
static ssize_t Read( stream_t *p_access, void *p_buffer, size_t i_len )
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
{
    access_sys_t *p_sys = p_access->p_sys;
    int i_read;

    i_read = smb_fread( p_sys->p_session, p_sys->i_fd, p_buffer, i_len );
    if( i_read < 0 )
    {
        msg_Err( p_access, "read failed" );
        return -1;
    }

    return i_read;
}

/*****************************************************************************
 * Control:
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
469
static int Control( stream_t *p_access, int i_query, va_list args )
470
{
471
    access_sys_t *p_sys = p_access->p_sys;
472

473 474
    switch( i_query )
    {
475 476 477
    case STREAM_CAN_SEEK:
    case STREAM_CAN_PAUSE:
    case STREAM_CAN_CONTROL_PACE:
478 479 480
        *va_arg( args, bool* ) = true;
        break;

481
    case STREAM_CAN_FASTSEEK:
482 483 484
        *va_arg( args, bool* ) = false;
        break;

485
    case STREAM_GET_SIZE:
486
    {
487
        smb_stat st = smb_stat_fd( p_sys->p_session, p_sys->i_fd );
488 489 490
        *va_arg( args, uint64_t * ) = smb_stat_get( st, SMB_STAT_SIZE );
        break;
    }
491
    case STREAM_GET_PTS_DELAY:
492 493 494 495
        *va_arg( args, int64_t * ) = INT64_C(1000)
            * var_InheritInteger( p_access, "network-caching" );
        break;

496
    case STREAM_SET_PAUSE_STATE:
497 498 499 500 501 502 503 504 505
        /* Nothing to do */
        break;

    default:
        return VLC_EGENERIC;
    }

    return VLC_SUCCESS;
}
Thomas Guillem's avatar
Thomas Guillem committed
506

507
static int add_item( stream_t *p_access, struct vlc_readdir_helper *p_rdh,
508
                     const char *psz_name, int i_type )
Thomas Guillem's avatar
Thomas Guillem committed
509
{
510
    char         *psz_uri;
Thomas Guillem's avatar
Thomas Guillem committed
511 512
    int           i_ret;

Thomas Guillem's avatar
Thomas Guillem committed
513 514
    char *psz_encoded_name = vlc_uri_encode( psz_name );
    if( psz_encoded_name == NULL )
515
        return VLC_ENOMEM;
516 517 518 519 520
    const char *psz_sep = p_access->psz_location[0] != '\0'
        && p_access->psz_location[strlen(p_access->psz_location) -1] != '/'
        ? "/" : "";
    i_ret = asprintf( &psz_uri, "smb://%s%s%s", p_access->psz_location,
                      psz_sep, psz_encoded_name );
Thomas Guillem's avatar
Thomas Guillem committed
521
    free( psz_encoded_name );
Thomas Guillem's avatar
Thomas Guillem committed
522
    if( i_ret == -1 )
523
        return VLC_ENOMEM;
Thomas Guillem's avatar
Thomas Guillem committed
524

525
    return vlc_readdir_helper_additem( p_rdh, psz_uri, NULL, psz_name, i_type,
526
                                       ITEM_NET );
Thomas Guillem's avatar
Thomas Guillem committed
527 528
}

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
529
static int BrowseShare( stream_t *p_access, input_item_node_t *p_node )
Thomas Guillem's avatar
Thomas Guillem committed
530 531
{
    access_sys_t *p_sys = p_access->p_sys;
532
    smb_share_list  shares;
Thomas Guillem's avatar
Thomas Guillem committed
533
    const char     *psz_name;
534 535
    size_t          share_count;
    int             i_ret = VLC_SUCCESS;
Thomas Guillem's avatar
Thomas Guillem committed
536

537 538 539 540
    if( smb_share_get_list( p_sys->p_session, &shares, &share_count )
        != DSM_SUCCESS )
        return VLC_EGENERIC;

541 542
    struct vlc_readdir_helper rdh;
    vlc_readdir_helper_init( &rdh, p_access, p_node );
543 544

    for( size_t i = 0; i < share_count && i_ret == VLC_SUCCESS; i++ )
Thomas Guillem's avatar
Thomas Guillem committed
545
    {
546
        psz_name = smb_share_list_at( shares, i );
Thomas Guillem's avatar
Thomas Guillem committed
547 548 549 550

        if( psz_name[strlen( psz_name ) - 1] == '$')
            continue;

551
        i_ret = add_item( p_access, &rdh, psz_name, ITEM_TYPE_DIRECTORY );
Thomas Guillem's avatar
Thomas Guillem committed
552
    }
553

554
    vlc_readdir_helper_finish( &rdh, i_ret == VLC_SUCCESS );
555 556 557

    smb_share_list_destroy( shares );
    return i_ret;
Thomas Guillem's avatar
Thomas Guillem committed
558 559
}

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
560
static int BrowseDirectory( stream_t *p_access, input_item_node_t *p_node )
Thomas Guillem's avatar
Thomas Guillem committed
561 562
{
    access_sys_t *p_sys = p_access->p_sys;
563
    smb_stat_list   files;
Thomas Guillem's avatar
Thomas Guillem committed
564 565 566
    smb_stat        st;
    char           *psz_query;
    const char     *psz_name;
567 568
    size_t          files_count;
    int             i_ret = VLC_SUCCESS;
Thomas Guillem's avatar
Thomas Guillem committed
569

570
    if( p_sys->psz_path != NULL )
Thomas Guillem's avatar
Thomas Guillem committed
571
    {
572 573 574 575
        if( asprintf( &psz_query, "%s\\*", p_sys->psz_path ) == -1 )
            return VLC_ENOMEM;
        files = smb_find( p_sys->p_session, p_sys->i_tid, psz_query );
        free( psz_query );
Thomas Guillem's avatar
Thomas Guillem committed
576
    }
577 578 579 580 581 582
    else
        files = smb_find( p_sys->p_session, p_sys->i_tid, "\\*" );

    if( files == NULL )
        return VLC_EGENERIC;

583 584
    struct vlc_readdir_helper rdh;
    vlc_readdir_helper_init( &rdh, p_access, p_node );
Thomas Guillem's avatar
Thomas Guillem committed
585

586 587
    files_count = smb_stat_list_count( files );
    for( size_t i = 0; i < files_count && i_ret == VLC_SUCCESS; i++ )
Thomas Guillem's avatar
Thomas Guillem committed
588
    {
589 590
        int i_type;

591
        st = smb_stat_list_at( files, i );
Thomas Guillem's avatar
Thomas Guillem committed
592

Thomas Guillem's avatar
Thomas Guillem committed
593
        if( st == NULL )
594 595 596
        {
            continue;
        }
Thomas Guillem's avatar
Thomas Guillem committed
597 598 599

        psz_name = smb_stat_name( st );

600 601
        i_type = smb_stat_get( st, SMB_STAT_ISDIR ) ?
                 ITEM_TYPE_DIRECTORY : ITEM_TYPE_FILE;
602
        i_ret = add_item( p_access, &rdh, psz_name, i_type );
Thomas Guillem's avatar
Thomas Guillem committed
603
    }
604

605
    vlc_readdir_helper_finish( &rdh, i_ret == VLC_SUCCESS );
606 607 608

    smb_stat_list_destroy( files );
    return i_ret;
Thomas Guillem's avatar
Thomas Guillem committed
609 610
}

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
611
static int BrowserInit( stream_t *p_access )
Thomas Guillem's avatar
Thomas Guillem committed
612 613 614 615 616 617 618
{
    access_sys_t *p_sys = p_access->p_sys;

    if( p_sys->psz_share == NULL )
        p_access->pf_readdir = BrowseShare;
    else
        p_access->pf_readdir = BrowseDirectory;
619
    p_access->pf_control = access_vaDirectoryControlHelper;
Thomas Guillem's avatar
Thomas Guillem committed
620 621 622

    return VLC_SUCCESS;
}