ftp.c 25.5 KB
Newer Older
1
/*****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
2
 * ftp.c: FTP input module
3
 *****************************************************************************
4
 * Copyright (C) 2001-2006 the VideoLAN team
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
5
 * Copyright © 2006 Rémi Denis-Courmont
6
 * $Id$
7
 *
8
 * Authors: Laurent Aimar <fenrir@via.ecp.fr> - original code
9
 *          Rémi Denis-Courmont <rem # videolan.org> - EPSV support
10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
Antoine Cellerier's avatar
Antoine Cellerier committed
23
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 25 26 27 28
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
29 30 31 32
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

33
#include <vlc_common.h>
34
#include <vlc_plugin.h>
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
35

36
#include <assert.h>
37

Clément Stenac's avatar
Clément Stenac committed
38
#include <vlc_access.h>
39
#include <vlc_dialog.h>
40

Clément Stenac's avatar
Clément Stenac committed
41
#include <vlc_network.h>
Rémi Duraffort's avatar
Rémi Duraffort committed
42
#include <vlc_url.h>
Clément Stenac's avatar
Clément Stenac committed
43
#include <vlc_sout.h>
44
#include <vlc_charset.h>
45

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
46 47 48 49
#ifndef IPPORT_FTP
# define IPPORT_FTP 21u
#endif

50
/*****************************************************************************
51
 * Module descriptor
52
 *****************************************************************************/
53 54 55 56
static int   InOpen ( vlc_object_t * );
static void  InClose( vlc_object_t * );
static int  OutOpen ( vlc_object_t * );
static void OutClose( vlc_object_t * );
57

Christophe Massiot's avatar
Christophe Massiot committed
58
#define CACHING_TEXT N_("Caching value in ms")
59
#define CACHING_LONGTEXT N_( \
60
    "Caching value for FTP streams. This " \
Clément Stenac's avatar
Clément Stenac committed
61
    "value should be set in milliseconds." )
Gildas Bazin's avatar
 
Gildas Bazin committed
62
#define USER_TEXT N_("FTP user name")
Clément Stenac's avatar
Clément Stenac committed
63
#define USER_LONGTEXT N_("User name that will " \
Gildas Bazin's avatar
 
Gildas Bazin committed
64 65
    "be used for the connection.")
#define PASS_TEXT N_("FTP password")
Clément Stenac's avatar
Clément Stenac committed
66
#define PASS_LONGTEXT N_("Password that will be " \
Gildas Bazin's avatar
 
Gildas Bazin committed
67 68
    "used for the connection.")
#define ACCOUNT_TEXT N_("FTP account")
Clément Stenac's avatar
Clément Stenac committed
69
#define ACCOUNT_LONGTEXT N_("Account that will be " \
Gildas Bazin's avatar
 
Gildas Bazin committed
70
    "used for the connection.")
71

72 73 74 75 76 77
vlc_module_begin ()
    set_shortname( "FTP" )
    set_description( N_("FTP input") )
    set_capability( "access", 0 )
    set_category( CAT_INPUT )
    set_subcategory( SUBCAT_INPUT_ACCESS )
Gildas Bazin's avatar
 
Gildas Bazin committed
78
    add_integer( "ftp-caching", 2 * DEFAULT_PTS_DELAY / 1000, NULL,
Rémi Duraffort's avatar
Rémi Duraffort committed
79
                 CACHING_TEXT, CACHING_LONGTEXT, true )
80
        change_safe()
Gildas Bazin's avatar
 
Gildas Bazin committed
81
    add_string( "ftp-user", "anonymous", NULL, USER_TEXT, USER_LONGTEXT,
Rémi Duraffort's avatar
Rémi Duraffort committed
82
                false )
83 84
    add_string( "ftp-pwd", "anonymous@example.com", NULL, PASS_TEXT,
                PASS_LONGTEXT, false )
Gildas Bazin's avatar
 
Gildas Bazin committed
85
    add_string( "ftp-account", "anonymous", NULL, ACCOUNT_TEXT,
Rémi Duraffort's avatar
Rémi Duraffort committed
86
                ACCOUNT_LONGTEXT, false )
87 88
    add_shortcut( "ftp" )
    set_callbacks( InOpen, InClose )
89

90
    add_submodule ()
Rémi Duraffort's avatar
Rémi Duraffort committed
91 92 93 94 95
        set_shortname( "FTP" )
        set_description( N_("FTP upload output") )
        set_capability( "sout access", 0 )
        set_category( CAT_SOUT )
        set_subcategory( SUBCAT_SOUT_ACO )
96
        add_shortcut( "ftp" )
Rémi Duraffort's avatar
Rémi Duraffort committed
97
        set_callbacks( OutOpen, OutClose )
98
vlc_module_end ()
99

100 101 102
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
103 104
static ssize_t Read( access_t *, uint8_t *, size_t );
static ssize_t Write( sout_access_out_t *, block_t * );
105
static int Seek( access_t *, uint64_t );
106
static int OutSeek( sout_access_out_t *, off_t );
Laurent Aimar's avatar
Laurent Aimar committed
107
static int Control( access_t *, int, va_list );
108

109
struct access_sys_t
110
{
111
    vlc_url_t  url;
112

113 114
    int        fd_cmd;
    int        fd_data;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
115

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
116
    char       sz_epsv_ip[NI_MAXNUMERICHOST];
117
    bool       out;
118
    bool       directory;
119
};
120 121
#define GET_OUT_SYS( p_this ) \
    ((access_sys_t *)(((sout_access_out_t *)(p_this))->p_sys))
122

123 124
static int ftp_SendCommand( vlc_object_t *, access_sys_t *, const char *, ... );
static int ftp_ReadCommand( vlc_object_t *, access_sys_t *, int *, char ** );
125
static int ftp_StartStream( vlc_object_t *, access_sys_t *, uint64_t );
126
static int ftp_StopStream ( vlc_object_t *, access_sys_t * );
127

128
static int Login( vlc_object_t *p_access, access_sys_t *p_sys )
129
{
130
    int i_answer;
131
    char *psz;
132

133
    /* *** Open a TCP connection with server *** */
134 135
    int fd = p_sys->fd_cmd = net_ConnectTCP( p_access, p_sys->url.psz_host,
                                             p_sys->url.i_port );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
136
    if( fd == -1 )
137
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
138
        msg_Err( p_access, "connection failed" );
Rémi Denis-Courmont's avatar
fmt  
Rémi Denis-Courmont committed
139
        dialog_Fatal( p_access, _("Network interaction failed"), "%s",
140
                        _("VLC could not connect with the given server.") );
141
        return -1;
142
    }
Laurent Aimar's avatar
Laurent Aimar committed
143

144
    while( ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) == 1 );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
145

146 147
    if( i_answer / 100 != 2 )
    {
Laurent Aimar's avatar
Laurent Aimar committed
148
        msg_Err( p_access, "connection rejected" );
Rémi Denis-Courmont's avatar
fmt  
Rémi Denis-Courmont committed
149
        dialog_Fatal( p_access, _("Network interaction failed"), "%s",
150
                        _("VLC's connection to the given server was rejected.") );
151
        return -1;
152 153
    }

Laurent Aimar's avatar
Laurent Aimar committed
154
    msg_Dbg( p_access, "connection accepted (%d)", i_answer );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
155

156 157 158 159
    if( p_sys->url.psz_username && *p_sys->url.psz_username )
        psz = strdup( p_sys->url.psz_username );
    else
        psz = var_CreateGetString( p_access, "ftp-user" );
160 161
    if( !psz )
        return -1;
162

163 164
    if( ftp_SendCommand( p_access, p_sys, "USER %s", psz ) < 0 ||
        ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) < 0 )
165
    {
166
        free( psz );
167
        return -1;
168
    }
169
    free( psz );
170

171 172 173
    switch( i_answer / 100 )
    {
        case 2:
Laurent Aimar's avatar
Laurent Aimar committed
174
            msg_Dbg( p_access, "user accepted" );
175 176
            break;
        case 3:
Laurent Aimar's avatar
Laurent Aimar committed
177
            msg_Dbg( p_access, "password needed" );
178 179
            if( p_sys->url.psz_password && *p_sys->url.psz_password )
                psz = strdup( p_sys->url.psz_password );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
180
            else
181
                psz = var_CreateGetString( p_access, "ftp-pwd" );
182 183
            if( !psz )
                return -1;
184

185 186
            if( ftp_SendCommand( p_access, p_sys, "PASS %s", psz ) < 0 ||
                ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) < 0 )
187
            {
188
                free( psz );
189
                return -1;
190
            }
191
            free( psz );
192

193 194 195
            switch( i_answer / 100 )
            {
                case 2:
Laurent Aimar's avatar
Laurent Aimar committed
196
                    msg_Dbg( p_access, "password accepted" );
197 198
                    break;
                case 3:
Laurent Aimar's avatar
Laurent Aimar committed
199
                    msg_Dbg( p_access, "account needed" );
200
                    psz = var_CreateGetString( p_access, "ftp-account" );
201
                    if( ftp_SendCommand( p_access, p_sys, "ACCT %s",
202
                                         psz ) < 0 ||
203
                        ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) < 0 )
204
                    {
205
                        free( psz );
206
                        return -1;
207
                    }
208
                    free( psz );
209

210 211
                    if( i_answer / 100 != 2 )
                    {
Laurent Aimar's avatar
Laurent Aimar committed
212
                        msg_Err( p_access, "account rejected" );
213
                        dialog_Fatal( p_access,
Rémi Denis-Courmont's avatar
fmt  
Rémi Denis-Courmont committed
214 215
                                      _("Network interaction failed"),
                                      "%s", _("Your account was rejected.") );
216
                        return -1;
217
                    }
Laurent Aimar's avatar
Laurent Aimar committed
218
                    msg_Dbg( p_access, "account accepted" );
219
                    break;
220

221
                default:
Laurent Aimar's avatar
Laurent Aimar committed
222
                    msg_Err( p_access, "password rejected" );
223
                    dialog_Fatal( p_access, _("Network interaction failed"),
Rémi Denis-Courmont's avatar
fmt  
Rémi Denis-Courmont committed
224
                                  "%s",  _("Your password was rejected.") );
225
                    return -1;
226 227 228
            }
            break;
        default:
Laurent Aimar's avatar
Laurent Aimar committed
229
            msg_Err( p_access, "user rejected" );
Rémi Denis-Courmont's avatar
fmt  
Rémi Denis-Courmont committed
230
            dialog_Fatal( p_access, _("Network interaction failed"), "%s",
231
                        _("Your connection attempt to the server was rejected.") );
232
            return -1;
233 234
    }

235 236 237
    return 0;
}

238
static int Connect( vlc_object_t *p_access, access_sys_t *p_sys )
239
{
240 241
    if( Login( p_access, p_sys ) < 0 )
        return -1;
242

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
243
    /* Extended passive mode */
244
    if( ftp_SendCommand( p_access, p_sys, "EPSV ALL" ) < 0 )
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
245 246
    {
        msg_Err( p_access, "cannot request extended passive mode" );
247
        net_Close( p_sys->fd_cmd );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
248 249 250
        return -1;
    }

251
    if( ftp_ReadCommand( p_access, p_sys, NULL, NULL ) == 2 )
252
    {
253
        if( net_GetPeerAddress( p_sys->fd_cmd, p_sys->sz_epsv_ip, NULL ) )
254 255 256 257
        {
            net_Close( p_sys->fd_cmd );
            return -1;
        }
258
    }
259 260 261 262 263 264 265
    else
    {
        /* If ESPV ALL fails, we fallback to PASV.
         * We have to restart the connection in case there is a NAT that
         * understands EPSV ALL in the way, and hence won't allow PASV on
         * the initial connection.
         */
266
        msg_Info( p_access, "FTP Extended passive mode disabled" );
267
        net_Close( p_sys->fd_cmd );
268

Rémi Denis-Courmont's avatar
Oups  
Rémi Denis-Courmont committed
269
        if( Login( p_access, p_sys ) )
270 271 272 273
        {
            net_Close( p_sys->fd_cmd );
            return -1;
        }
274
    }
275

276 277 278
    /* check binary mode support */
    if( ftp_SendCommand( p_access, p_sys, "TYPE I" ) < 0 ||
        ftp_ReadCommand( p_access, p_sys, NULL, NULL ) != 2 )
279
    {
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
280
        msg_Err( p_access, "cannot set binary transfer mode" );
281 282
        net_Close( p_sys->fd_cmd );
        return -1;
283 284
    }

285 286 287 288 289 290 291
    return 0;
}


static int parseURL( vlc_url_t *url, const char *path )
{
    if( path == NULL )
292
        return VLC_EGENERIC;
293 294 295 296 297 298 299 300

    /* *** Parse URL and get server addr/port and path *** */
    while( *path == '/' )
        path++;

    vlc_UrlParse( url, path, 0 );

    if( url->psz_host == NULL || *url->psz_host == '\0' )
301
        return VLC_EGENERIC;
302 303 304 305

    if( url->i_port <= 0 )
        url->i_port = IPPORT_FTP; /* default port */

306 307
    if( url->psz_path == NULL )
        return VLC_SUCCESS;
308
    /* FTP URLs are relative to user's default directory (RFC1738 §3.2)
309
    For absolute path use ftp://foo.bar//usr/local/etc/filename */
310
    /* FIXME: we should issue a series of CWD, one per slash */
311 312 313 314 315
    if( url->psz_path )
    {
        assert( url->psz_path[0] == '/' );
        url->psz_path++;
    }
316 317 318 319 320 321 322 323

    char *type = strstr( url->psz_path, ";type=" );
    if( type )
    {
        *type = '\0';
        if( strchr( "iI", type[6] ) == NULL )
            return VLC_EGENERIC; /* ASCII and directory not supported */
    }
324
    decode_URI( url->psz_path );
325 326
    /* FIXME: check for UTF-8 support, otherwise only ASCII is allowed */
    EnsureUTF8( url->psz_path );
327
    return VLC_SUCCESS;
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
}


/****************************************************************************
 * Open: connect to ftp server and ask for file
 ****************************************************************************/
static int InOpen( vlc_object_t *p_this )
{
    access_t     *p_access = (access_t*)p_this;
    access_sys_t *p_sys;
    char         *psz_arg;

    /* Init p_access */
    STANDARD_READ_ACCESS_INIT
    p_sys->fd_data = -1;
343
    p_sys->out = false;
344
    p_sys->directory = false;
345

346
    if( parseURL( &p_sys->url, p_access->psz_location ) )
347 348 349 350 351
        goto exit_error;

    if( Connect( p_this, p_sys ) )
        goto exit_error;

352
    /* get size */
353 354 355 356
    if( p_sys->url.psz_path == NULL )
        p_sys->directory = true;
    else
    if( ftp_SendCommand( p_this, p_sys, "SIZE %s", p_sys->url.psz_path ) < 0
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
357
     || ftp_ReadCommand( p_this, p_sys, NULL, &psz_arg ) != 2 )
358
    {
359 360
        msg_Dbg( p_access, "cannot get file size" );
        msg_Dbg( p_access, "will try to get directory contents" );
361 362
        if( ftp_SendCommand( p_this, p_sys, "CWD %s", p_sys->url.psz_path ) < 0
         || ftp_ReadCommand( p_this, p_sys, NULL, &psz_arg ) != 2 )
363 364 365 366 367 368 369 370 371 372 373
        {
            msg_Err( p_access, "file or directory doesn't exist" );
            net_Close( p_sys->fd_cmd );
            goto exit_error;
        }
        p_sys->directory = true;
    }
    else
    {
        p_access->info.i_size = atoll( &psz_arg[4] );
        free( psz_arg );
374
        msg_Dbg( p_access, "file size: %"PRIu64, p_access->info.i_size );
375 376
    }

377
    /* Start the 'stream' */
378
    if( ftp_StartStream( p_this, p_sys, 0 ) < 0 )
379
    {
Laurent Aimar's avatar
Laurent Aimar committed
380
        msg_Err( p_access, "cannot retrieve file" );
381
        net_Close( p_sys->fd_cmd );
382 383 384 385
        goto exit_error;
    }

    /* Update default_pts to a suitable value for ftp access */
Laurent Aimar's avatar
Laurent Aimar committed
386
    var_Create( p_access, "ftp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
387

388
    return VLC_SUCCESS;
389 390

exit_error:
391 392 393
    vlc_UrlClean( &p_sys->url );
    free( p_sys );
    return VLC_EGENERIC;
394 395
}

396 397 398 399 400
static int OutOpen( vlc_object_t *p_this )
{
    sout_access_out_t *p_access = (sout_access_out_t *)p_this;
    access_sys_t      *p_sys;

401 402
    p_sys = calloc( 1, sizeof( *p_sys ) );
    if( !p_sys )
403 404 405 406
        return VLC_ENOMEM;

    /* Init p_access */
    p_sys->fd_data = -1;
407
    p_sys->out = true;
408

409
    if( parseURL( &p_sys->url, p_access->psz_path ) )
410
        goto exit_error;
411 412 413 414 415
    if( p_sys->url.psz_path == NULL )
    {
        msg_Err( p_this, "no filename specified" );
        goto exit_error;
    }
416 417 418 419 420 421 422 423 424 425 426 427 428 429

    if( Connect( p_this, p_sys ) )
        goto exit_error;

    /* Start the 'stream' */
    if( ftp_StartStream( p_this, p_sys, 0 ) < 0 )
    {
        msg_Err( p_access, "cannot store file" );
        net_Close( p_sys->fd_cmd );
        goto exit_error;
    }

    p_access->pf_seek = OutSeek;
    p_access->pf_write = Write;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
430
    p_access->p_sys = (void *)p_sys;
431 432 433 434

    return VLC_SUCCESS;

exit_error:
Rémi Denis-Courmont's avatar
Oups  
Rémi Denis-Courmont committed
435
    vlc_UrlClean( &p_sys->url );
436 437 438 439
    free( p_sys );
    return VLC_EGENERIC;
}

440 441 442
/*****************************************************************************
 * Close: free unused data structures
 *****************************************************************************/
443
static void Close( vlc_object_t *p_access, access_sys_t *p_sys )
444
{
Laurent Aimar's avatar
Laurent Aimar committed
445
    msg_Dbg( p_access, "stopping stream" );
446
    ftp_StopStream( p_access, p_sys );
447

448
    if( ftp_SendCommand( p_access, p_sys, "QUIT" ) < 0 )
449
    {
Laurent Aimar's avatar
Laurent Aimar committed
450
        msg_Warn( p_access, "cannot quit" );
451 452 453
    }
    else
    {
454
        ftp_ReadCommand( p_access, p_sys, NULL, NULL );
455
    }
456
    net_Close( p_sys->fd_cmd );
457 458

    /* free memory */
459 460
    vlc_UrlClean( &p_sys->url );
    free( p_sys );
461 462
}

463 464 465 466 467 468 469 470 471 472 473
static void InClose( vlc_object_t *p_this )
{
    Close( p_this, ((access_t *)p_this)->p_sys);
}

static void OutClose( vlc_object_t *p_this )
{
    Close( p_this, GET_OUT_SYS(p_this));
}


474 475 476
/*****************************************************************************
 * Seek: try to go at the right place
 *****************************************************************************/
477
static int _Seek( vlc_object_t *p_access, access_sys_t *p_sys, uint64_t i_pos )
478
{
479
    msg_Dbg( p_access, "seeking to %"PRIu64, i_pos );
480

481 482
    ftp_StopStream( (vlc_object_t *)p_access, p_sys );
    if( ftp_StartStream( (vlc_object_t *)p_access, p_sys, i_pos ) < 0 )
Laurent Aimar's avatar
Laurent Aimar committed
483
        return VLC_EGENERIC;
484 485 486 487

    return VLC_SUCCESS;
}

488
static int Seek( access_t *p_access, uint64_t i_pos )
489 490 491 492
{
    int val = _Seek( (vlc_object_t *)p_access, p_access->p_sys, i_pos );
    if( val )
        return val;
493

494
    p_access->info.b_eof = false;
495
    p_access->info.i_pos = i_pos;
496

Laurent Aimar's avatar
Laurent Aimar committed
497
    return VLC_SUCCESS;
498 499
}

500 501 502 503 504
static int OutSeek( sout_access_out_t *p_access, off_t i_pos )
{
    return _Seek( (vlc_object_t *)p_access, GET_OUT_SYS( p_access ), i_pos);
}

505 506 507
/*****************************************************************************
 * Read:
 *****************************************************************************/
508
static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
509
{
Laurent Aimar's avatar
Laurent Aimar committed
510 511
    access_sys_t *p_sys = p_access->p_sys;

512
    assert( p_sys->fd_data != -1 );
513
    assert( !p_sys->out );
514

515
    if( p_access->info.b_eof )
Laurent Aimar's avatar
Laurent Aimar committed
516 517
        return 0;

518 519 520 521 522 523 524 525 526 527
    if( p_sys->directory )
    {
        char *psz_line = net_Gets( p_access, p_sys->fd_data, NULL );
        if( !psz_line )
        {
            p_access->info.b_eof = true;
            return 0;
        }
        else
        {
528
            snprintf( (char*)p_buffer, i_len, "ftp://%s:%d/%s/%s\n",
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
                      p_sys->url.psz_host, p_sys->url.i_port,
                      p_sys->url.psz_path, psz_line );
            free( psz_line );
            return strlen( (const char *)p_buffer );
        }
    }
    else
    {
        int i_read = net_Read( p_access, p_sys->fd_data, NULL,
                               p_buffer, i_len, false );
        if( i_read == 0 )
            p_access->info.b_eof = true;
        else if( i_read > 0 )
            p_access->info.i_pos += i_read;

        return i_read;
    }
Laurent Aimar's avatar
Laurent Aimar committed
546 547
}

548 549 550
/*****************************************************************************
 * Write:
 *****************************************************************************/
551
static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
{
    access_sys_t *p_sys = GET_OUT_SYS(p_access);
    size_t i_write = 0;

    assert( p_sys->fd_data != -1 );

    while( p_buffer != NULL )
    {
        block_t *p_next = p_buffer->p_next;;

        i_write += net_Write( p_access, p_sys->fd_data, NULL,
                              p_buffer->p_buffer, p_buffer->i_buffer );
        block_Release( p_buffer );

        p_buffer = p_next;
    }

    return i_write;
}

Laurent Aimar's avatar
Laurent Aimar committed
572 573 574 575 576
/*****************************************************************************
 * Control:
 *****************************************************************************/
static int Control( access_t *p_access, int i_query, va_list args )
{
577 578
    bool    *pb_bool;
    int64_t *pi_64;
Laurent Aimar's avatar
Laurent Aimar committed
579 580 581 582 583

    switch( i_query )
    {
        /* */
        case ACCESS_CAN_SEEK:
584 585
            pb_bool = (bool*)va_arg( args, bool* );
            *pb_bool = true;
Laurent Aimar's avatar
Laurent Aimar committed
586 587
            break;
        case ACCESS_CAN_FASTSEEK:
588 589
            pb_bool = (bool*)va_arg( args, bool* );
            *pb_bool = false;
Laurent Aimar's avatar
Laurent Aimar committed
590 591
            break;
        case ACCESS_CAN_PAUSE:
592 593
            pb_bool = (bool*)va_arg( args, bool* );
            *pb_bool = true;    /* FIXME */
Laurent Aimar's avatar
Laurent Aimar committed
594 595
            break;
        case ACCESS_CAN_CONTROL_PACE:
596 597
            pb_bool = (bool*)va_arg( args, bool* );
            *pb_bool = true;    /* FIXME */
Laurent Aimar's avatar
Laurent Aimar committed
598 599 600 601 602
            break;

        /* */
        case ACCESS_GET_PTS_DELAY:
            pi_64 = (int64_t*)va_arg( args, int64_t * );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
603
            *pi_64 = var_GetInteger( p_access, "ftp-caching" ) * INT64_C(1000);
Laurent Aimar's avatar
Laurent Aimar committed
604
            break;
605

Laurent Aimar's avatar
Laurent Aimar committed
606 607
        /* */
        case ACCESS_SET_PAUSE_STATE:
608
            pb_bool = (bool*)va_arg( args, bool* );
609 610
            if ( !pb_bool )
              return Seek( p_access, p_access->info.i_pos );
Laurent Aimar's avatar
Laurent Aimar committed
611 612
            break;

613 614 615
        case ACCESS_GET_TITLE_INFO:
        case ACCESS_SET_TITLE:
        case ACCESS_SET_SEEKPOINT:
616
        case ACCESS_SET_PRIVATE_ID_STATE:
617
        case ACCESS_GET_CONTENT_TYPE:
618
        case ACCESS_GET_META:
619 620
            return VLC_EGENERIC;

Laurent Aimar's avatar
Laurent Aimar committed
621
        default:
622
            msg_Warn( p_access, "unimplemented query in control: %d", i_query);
Laurent Aimar's avatar
Laurent Aimar committed
623 624 625 626
            return VLC_EGENERIC;

    }
    return VLC_SUCCESS;
627 628
}

629 630 631
/*****************************************************************************
 * ftp_*:
 *****************************************************************************/
632 633
static int ftp_SendCommand( vlc_object_t *p_access, access_sys_t *p_sys,
                            const char *psz_fmt, ... )
634
{
635 636
    va_list      args;
    char         *psz_cmd;
637 638

    va_start( args, psz_fmt );
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
639 640 641
    if( vasprintf( &psz_cmd, psz_fmt, args ) == -1 )
        return VLC_EGENERIC;

642
    va_end( args );
643

Laurent Aimar's avatar
Laurent Aimar committed
644
    msg_Dbg( p_access, "ftp_SendCommand:\"%s\"", psz_cmd);
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
645

646
    if( net_Printf( p_access, p_sys->fd_cmd, NULL, "%s\r\n", psz_cmd ) < 0 )
647
    {
Laurent Aimar's avatar
Laurent Aimar committed
648
        msg_Err( p_access, "failed to send command" );
649
        return VLC_EGENERIC;
650
    }
651
    return VLC_SUCCESS;
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
}

/* TODO support this s**t :
 RFC 959 allows the client to send certain TELNET strings at any moment,
 even in the middle of a request:

 * \377\377.
 * \377\376x where x is one byte.
 * \377\375x where x is one byte. The server is obliged to send \377\374x
 *                                immediately after reading x.
 * \377\374x where x is one byte.
 * \377\373x where x is one byte. The server is obliged to send \377\376x
 *                                immediately after reading x.
 * \377x for any other byte x.

 These strings are not part of the requests, except in the case \377\377,
 where the request contains one \377. */
669
static int ftp_ReadCommand( vlc_object_t *p_access, access_sys_t *p_sys,
Gildas Bazin's avatar
 
Gildas Bazin committed
670
                            int *pi_answer, char **ppsz_answer )
671
{
672 673 674
    char         *psz_line;
    int          i_answer;

675
    psz_line = net_Gets( p_access, p_sys->fd_cmd, NULL );
676 677
    if( psz_line == NULL || strlen( psz_line ) < 3 )
    {
Laurent Aimar's avatar
Laurent Aimar committed
678
        msg_Err( p_access, "cannot get answer" );
679
        free( psz_line );
680 681 682
        if( pi_answer ) *pi_answer    = 500;
        if( ppsz_answer ) *ppsz_answer  = NULL;
        return -1;
683
    }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
684
    msg_Dbg( p_access, "answer=%s", psz_line );
685

686 687 688 689 690 691 692 693 694
    if( psz_line[3] == '-' )    /* Multiple response */
    {
        char end[4];

        memcpy( end, psz_line, 3 );
        end[3] = ' ';

        for( ;; )
        {
695
            char *psz_tmp = net_Gets( p_access, p_sys->fd_cmd, NULL );
696 697 698 699 700 701 702 703 704 705 706 707 708

            if( psz_tmp == NULL )   /* Error */
                break;

            if( !strncmp( psz_tmp, end, 4 ) )
            {
                free( psz_tmp );
                break;
            }
            free( psz_tmp );
        }
    }

709
    i_answer = atoi( psz_line );
710 711 712 713

    if( pi_answer ) *pi_answer = i_answer;
    if( ppsz_answer )
    {
714
        *ppsz_answer = psz_line;
715 716 717
    }
    else
    {
718
        free( psz_line );
719 720 721 722
    }
    return( i_answer / 100 );
}

723
static int ftp_StartStream( vlc_object_t *p_access, access_sys_t *p_sys,
724
                            uint64_t i_start )
725
{
726
    char psz_ipv4[16], *psz_ip = p_sys->sz_epsv_ip;
727 728 729 730
    int  i_answer;
    char *psz_arg, *psz_parser;
    int  i_port;

731
    assert( p_sys->fd_data == -1 );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
732

733 734
    if( ( ftp_SendCommand( p_access, p_sys, *psz_ip ? "EPSV" : "PASV" ) < 0 )
     || ( ftp_ReadCommand( p_access, p_sys, &i_answer, &psz_arg ) != 2 ) )
735
    {
736
        msg_Err( p_access, "cannot set passive mode" );
737
        return VLC_EGENERIC;
738
    }
739

740
    psz_parser = strchr( psz_arg, '(' );
741
    if( psz_parser == NULL )
742
    {
743
        free( psz_arg );
744
        msg_Err( p_access, "cannot parse passive mode response" );
745
        return VLC_EGENERIC;
746
    }
747

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
748
    if( *psz_ip )
749 750 751 752 753 754 755 756 757 758 759 760 761
    {
        char psz_fmt[7] = "(|||%u";
        psz_fmt[1] = psz_fmt[2] = psz_fmt[3] = psz_parser[1];

        if( sscanf( psz_parser, psz_fmt, &i_port ) < 1 )
        {
            free( psz_arg );
            msg_Err( p_access, "cannot parse passive mode response" );
            return VLC_EGENERIC;
        }
    }
    else
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
762
        unsigned a1, a2, a3, a4, p1, p2;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
763

764 765 766 767 768 769 770 771 772 773 774 775 776
        if( ( sscanf( psz_parser, "(%u,%u,%u,%u,%u,%u", &a1, &a2, &a3, &a4,
                      &p1, &p2 ) < 6 ) || ( a1 > 255 ) || ( a2 > 255 )
         || ( a3 > 255 ) || ( a4 > 255 ) || ( p1 > 255 ) || ( p2 > 255 ) )
        {
            free( psz_arg );
            msg_Err( p_access, "cannot parse passive mode response" );
            return VLC_EGENERIC;
        }

        sprintf( psz_ipv4, "%u.%u.%u.%u", a1, a2, a3, a4 );
        psz_ip = psz_ipv4;
        i_port = (p1 << 8) | p2;
    }
777
    free( psz_arg );
778

Laurent Aimar's avatar
Laurent Aimar committed
779
    msg_Dbg( p_access, "ip:%s port:%d", psz_ip, i_port );
780

781 782
    if( ftp_SendCommand( p_access, p_sys, "TYPE I" ) < 0 ||
        ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) != 2 )
783
    {
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
784
        msg_Err( p_access, "cannot set binary transfer mode" );
785
        return VLC_EGENERIC;
786 787 788 789
    }

    if( i_start > 0 )
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
790
        if( ftp_SendCommand( p_access, p_sys, "REST %"PRIu64, i_start ) < 0 ||
791
            ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) > 3 )
792
        {
793
            msg_Err( p_access, "cannot set restart offset" );
794
            return VLC_EGENERIC;
795 796 797
        }
    }

Laurent Aimar's avatar
Laurent Aimar committed
798
    msg_Dbg( p_access, "waiting for data connection..." );
799
    p_sys->fd_data = net_ConnectTCP( p_access, psz_ip, i_port );
800
    if( p_sys->fd_data < 0 )
801
    {
Laurent Aimar's avatar
Laurent Aimar committed
802
        msg_Err( p_access, "failed to connect with server" );
803
        return VLC_EGENERIC;
804
    }
Laurent Aimar's avatar
Laurent Aimar committed
805
    msg_Dbg( p_access, "connection with \"%s:%d\" successful",
806 807
             psz_ip, i_port );

808
    if( p_sys->directory )
809
    {
810 811 812 813
        if( ftp_SendCommand( p_access, p_sys, "NLST" ) < 0 ||
            ftp_ReadCommand( p_access, p_sys, NULL, &psz_arg ) > 2 )
        {
            msg_Err( p_access, "cannot list directory contents" );
814
        return VLC_EGENERIC;
815 816 817 818 819
        }
    }
    else
    {
        /* "1xx" message */
820
        assert( p_sys->url.psz_path );
821 822
        if( ftp_SendCommand( p_access, p_sys, "%s %s",
                             p_sys->out ? "STOR" : "RETR",
823
                             p_sys->url.psz_path ) < 0
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
824
         || ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) > 2 )
825 826 827 828
        {
            msg_Err( p_access, "cannot retrieve file" );
            return VLC_EGENERIC;
        }
829
    }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
830

831
    shutdown( p_sys->fd_data, p_sys->out ? SHUT_RD : SHUT_WR );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
832

833
    return VLC_SUCCESS;
834 835
}

836
static int ftp_StopStream ( vlc_object_t *p_access, access_sys_t *p_sys )
837
{
838
    if( ftp_SendCommand( p_access, p_sys, "ABOR" ) < 0 )
839
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
840
        msg_Warn( p_access, "cannot abort file" );
Laurent Aimar's avatar
Laurent Aimar committed
841 842 843
        if(  p_sys->fd_data > 0 )
            net_Close( p_sys->fd_data );
        p_sys->fd_data = -1;
844
        return VLC_EGENERIC;
845
    }
846 847

    if( p_sys->fd_data != -1 )
Laurent Aimar's avatar
Laurent Aimar committed
848 849 850
    {
        net_Close( p_sys->fd_data );
        p_sys->fd_data = -1;
851 852
        /* Read the final response from RETR/STOR, i.e. 426 or 226 */
        ftp_ReadCommand( p_access, p_sys, NULL, NULL );
Laurent Aimar's avatar
Laurent Aimar committed
853
    }
854 855
    /* Read the response from ABOR, i.e. 226 or 225 */
    ftp_ReadCommand( p_access, p_sys, NULL, NULL );
856

857
    return VLC_SUCCESS;
858
}