]> git.sesse.net Git - vlc/blob - modules/access/ftp.c
ca50e0cc1dff9cb0c8b3df45977e876c31af16bb
[vlc] / modules / access / ftp.c
1 /*****************************************************************************
2  * ftp.c: FTP input module
3  *****************************************************************************
4  * Copyright (C) 2001-2006 the VideoLAN team
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  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35
36 #include <assert.h>
37
38 #include <vlc_access.h>
39 #include <vlc_dialog.h>
40
41 #include <vlc_network.h>
42 #include <vlc_url.h>
43 #include <vlc_sout.h>
44 #include <vlc_charset.h>
45
46 #ifndef IPPORT_FTP
47 # define IPPORT_FTP 21u
48 #endif
49
50 /*****************************************************************************
51  * Module descriptor
52  *****************************************************************************/
53 static int   InOpen ( vlc_object_t * );
54 static void  InClose( vlc_object_t * );
55 static int  OutOpen ( vlc_object_t * );
56 static void OutClose( vlc_object_t * );
57
58 #define CACHING_TEXT N_("Caching value in ms")
59 #define CACHING_LONGTEXT N_( \
60     "Caching value for FTP streams. This " \
61     "value should be set in milliseconds." )
62 #define USER_TEXT N_("FTP user name")
63 #define USER_LONGTEXT N_("User name that will " \
64     "be used for the connection.")
65 #define PASS_TEXT N_("FTP password")
66 #define PASS_LONGTEXT N_("Password that will be " \
67     "used for the connection.")
68 #define ACCOUNT_TEXT N_("FTP account")
69 #define ACCOUNT_LONGTEXT N_("Account that will be " \
70     "used for the connection.")
71
72 vlc_module_begin ()
73     set_shortname( "FTP" )
74     set_description( N_("FTP input") )
75     set_capability( "access", 0 )
76     set_category( CAT_INPUT )
77     set_subcategory( SUBCAT_INPUT_ACCESS )
78     add_integer( "ftp-caching", 2 * DEFAULT_PTS_DELAY / 1000, NULL,
79                  CACHING_TEXT, CACHING_LONGTEXT, true )
80         change_safe()
81     add_string( "ftp-user", "anonymous", NULL, USER_TEXT, USER_LONGTEXT,
82                 false )
83     add_string( "ftp-pwd", "anonymous@example.com", NULL, PASS_TEXT,
84                 PASS_LONGTEXT, false )
85     add_string( "ftp-account", "anonymous", NULL, ACCOUNT_TEXT,
86                 ACCOUNT_LONGTEXT, false )
87     add_shortcut( "ftp" )
88     set_callbacks( InOpen, InClose )
89
90     add_submodule ()
91         set_shortname( "FTP" )
92         set_description( N_("FTP upload output") )
93         set_capability( "sout access", 0 )
94         set_category( CAT_SOUT )
95         set_subcategory( SUBCAT_SOUT_ACO )
96         add_shortcut( "ftp" )
97         set_callbacks( OutOpen, OutClose )
98 vlc_module_end ()
99
100 /*****************************************************************************
101  * Local prototypes
102  *****************************************************************************/
103 static ssize_t Read( access_t *, uint8_t *, size_t );
104 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 );
107 static int Control( access_t *, int, va_list );
108
109 struct access_sys_t
110 {
111     vlc_url_t  url;
112
113     int        fd_cmd;
114     int        fd_data;
115
116     char       sz_epsv_ip[NI_MAXNUMERICHOST];
117     bool       out;
118     bool       directory;
119 };
120 #define GET_OUT_SYS( p_this ) \
121     ((access_sys_t *)(((sout_access_out_t *)(p_this))->p_sys))
122
123 static int ftp_SendCommand( vlc_object_t *, access_sys_t *, const char *, ... );
124 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     int fd = p_sys->fd_cmd = net_ConnectTCP( p_access, p_sys->url.psz_host,
135                                              p_sys->url.i_port );
136     if( fd == -1 )
137     {
138         msg_Err( p_access, "connection failed" );
139         dialog_Fatal( p_access, _("Network interaction failed"), "%s",
140                         _("VLC could not connect with the given server.") );
141         return -1;
142     }
143
144     while( ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) == 1 );
145
146     if( i_answer / 100 != 2 )
147     {
148         msg_Err( p_access, "connection rejected" );
149         dialog_Fatal( p_access, _("Network interaction failed"), "%s",
150                         _("VLC's connection to the given server was rejected.") );
151         return -1;
152     }
153
154     msg_Dbg( p_access, "connection accepted (%d)", i_answer );
155
156     if( p_sys->url.psz_username && *p_sys->url.psz_username )
157         psz = strdup( p_sys->url.psz_username );
158     else
159         psz = var_CreateGetString( p_access, "ftp-user" );
160     if( !psz )
161         return -1;
162
163     if( ftp_SendCommand( p_access, p_sys, "USER %s", psz ) < 0 ||
164         ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) < 0 )
165     {
166         free( psz );
167         return -1;
168     }
169     free( psz );
170
171     switch( i_answer / 100 )
172     {
173         case 2:
174             msg_Dbg( p_access, "user accepted" );
175             break;
176         case 3:
177             msg_Dbg( p_access, "password needed" );
178             if( p_sys->url.psz_password && *p_sys->url.psz_password )
179                 psz = strdup( p_sys->url.psz_password );
180             else
181                 psz = var_CreateGetString( p_access, "ftp-pwd" );
182             if( !psz )
183                 return -1;
184
185             if( ftp_SendCommand( p_access, p_sys, "PASS %s", psz ) < 0 ||
186                 ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) < 0 )
187             {
188                 free( psz );
189                 return -1;
190             }
191             free( psz );
192
193             switch( i_answer / 100 )
194             {
195                 case 2:
196                     msg_Dbg( p_access, "password accepted" );
197                     break;
198                 case 3:
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                     if( i_answer / 100 != 2 )
211                     {
212                         msg_Err( p_access, "account rejected" );
213                         dialog_Fatal( p_access,
214                                       _("Network interaction failed"),
215                                       "%s", _("Your account was rejected.") );
216                         return -1;
217                     }
218                     msg_Dbg( p_access, "account accepted" );
219                     break;
220
221                 default:
222                     msg_Err( p_access, "password rejected" );
223                     dialog_Fatal( p_access, _("Network interaction failed"),
224                                   "%s",  _("Your password was rejected.") );
225                     return -1;
226             }
227             break;
228         default:
229             msg_Err( p_access, "user rejected" );
230             dialog_Fatal( p_access, _("Network interaction failed"), "%s",
231                         _("Your connection attempt to the server was rejected.") );
232             return -1;
233     }
234
235     return 0;
236 }
237
238 static int Connect( vlc_object_t *p_access, access_sys_t *p_sys )
239 {
240     if( Login( p_access, p_sys ) < 0 )
241         return -1;
242
243     /* Extended passive mode */
244     if( ftp_SendCommand( p_access, p_sys, "EPSV ALL" ) < 0 )
245     {
246         msg_Err( p_access, "cannot request extended passive mode" );
247         net_Close( p_sys->fd_cmd );
248         return -1;
249     }
250
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             net_Close( p_sys->fd_cmd );
256             return -1;
257         }
258     }
259     else
260     {
261         /* If ESPV ALL fails, we fallback to PASV.
262          * We have to restart the connection in case there is a NAT that
263          * understands EPSV ALL in the way, and hence won't allow PASV on
264          * the initial connection.
265          */
266         msg_Info( p_access, "FTP Extended passive mode disabled" );
267         net_Close( p_sys->fd_cmd );
268
269         if( Login( p_access, p_sys ) )
270         {
271             net_Close( p_sys->fd_cmd );
272             return -1;
273         }
274     }
275
276     /* check binary mode support */
277     if( ftp_SendCommand( p_access, p_sys, "TYPE I" ) < 0 ||
278         ftp_ReadCommand( p_access, p_sys, NULL, NULL ) != 2 )
279     {
280         msg_Err( p_access, "cannot set binary transfer mode" );
281         net_Close( p_sys->fd_cmd );
282         return -1;
283     }
284
285     return 0;
286 }
287
288
289 static int parseURL( vlc_url_t *url, const char *path )
290 {
291     if( path == NULL )
292         return VLC_EGENERIC;
293
294     /* *** Parse URL and get server addr/port and path *** */
295     while( *path == '/' )
296         path++;
297
298     vlc_UrlParse( url, path, 0 );
299
300     if( url->psz_host == NULL || *url->psz_host == '\0' )
301         return VLC_EGENERIC;
302
303     if( url->i_port <= 0 )
304         url->i_port = IPPORT_FTP; /* default port */
305
306     /* FTP URLs are relative to user's default directory (RFC1738 §3.2)
307     For absolute path use ftp://foo.bar//usr/local/etc/filename */
308     /* FIXME: we should issue a series of CWD, one per slash */
309     if( url->psz_path == NULL )
310         return VLC_SUCCESS;
311
312     char *type = strstr( url->psz_path, ";type=" );
313     if( type )
314     {
315         *type = '\0';
316         if( strchr( "iI", type[6] ) == NULL )
317             return VLC_EGENERIC; /* ASCII and directory not supported */
318     }
319     decode_URI( url->psz_path );
320     /* FIXME: check for UTF-8 support, otherwise only ASCII is allowed */
321     EnsureUTF8( url->psz_path );
322     return VLC_SUCCESS;
323 }
324
325
326 /****************************************************************************
327  * Open: connect to ftp server and ask for file
328  ****************************************************************************/
329 static int InOpen( vlc_object_t *p_this )
330 {
331     access_t     *p_access = (access_t*)p_this;
332     access_sys_t *p_sys;
333     char         *psz_arg;
334
335     /* Init p_access */
336     STANDARD_READ_ACCESS_INIT
337     p_sys->fd_data = -1;
338     p_sys->out = false;
339     p_sys->directory = false;
340
341     if( parseURL( &p_sys->url, p_access->psz_location ) )
342         goto exit_error;
343
344     if( Connect( p_this, p_sys ) )
345         goto exit_error;
346
347     /* get size */
348     if( p_sys->url.psz_path == NULL )
349         p_sys->directory = true;
350     else
351     if( ftp_SendCommand( p_this, p_sys, "SIZE %s", p_sys->url.psz_path ) < 0
352      || ftp_ReadCommand( p_this, p_sys, NULL, &psz_arg ) != 2 )
353     {
354         msg_Dbg( p_access, "cannot get file size" );
355         msg_Dbg( p_access, "will try to get directory contents" );
356         if( ftp_SendCommand( p_this, p_sys, "CWD %s", p_sys->url.psz_path ) < 0
357          || ftp_ReadCommand( p_this, p_sys, NULL, &psz_arg ) != 2 )
358         {
359             msg_Err( p_access, "file or directory doesn't exist" );
360             net_Close( p_sys->fd_cmd );
361             goto exit_error;
362         }
363         p_sys->directory = true;
364     }
365     else
366     {
367         p_access->info.i_size = atoll( &psz_arg[4] );
368         free( psz_arg );
369         msg_Dbg( p_access, "file size: %"PRIu64, p_access->info.i_size );
370     }
371
372     /* Start the 'stream' */
373     if( ftp_StartStream( p_this, p_sys, 0 ) < 0 )
374     {
375         msg_Err( p_access, "cannot retrieve file" );
376         net_Close( p_sys->fd_cmd );
377         goto exit_error;
378     }
379
380     /* Update default_pts to a suitable value for ftp access */
381     var_Create( p_access, "ftp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
382
383     return VLC_SUCCESS;
384
385 exit_error:
386     vlc_UrlClean( &p_sys->url );
387     free( p_sys );
388     return VLC_EGENERIC;
389 }
390
391 static int OutOpen( vlc_object_t *p_this )
392 {
393     sout_access_out_t *p_access = (sout_access_out_t *)p_this;
394     access_sys_t      *p_sys;
395
396     p_sys = calloc( 1, sizeof( *p_sys ) );
397     if( !p_sys )
398         return VLC_ENOMEM;
399
400     /* Init p_access */
401     p_sys->fd_data = -1;
402     p_sys->out = true;
403
404     if( parseURL( &p_sys->url, p_access->psz_path ) )
405         goto exit_error;
406     if( p_sys->url.psz_path == NULL )
407     {
408         msg_Err( p_this, "no filename specified" );
409         goto exit_error;
410     }
411
412     if( Connect( p_this, p_sys ) )
413         goto exit_error;
414
415     /* Start the 'stream' */
416     if( ftp_StartStream( p_this, p_sys, 0 ) < 0 )
417     {
418         msg_Err( p_access, "cannot store file" );
419         net_Close( p_sys->fd_cmd );
420         goto exit_error;
421     }
422
423     p_access->pf_seek = OutSeek;
424     p_access->pf_write = Write;
425     p_access->p_sys = (void *)p_sys;
426
427     return VLC_SUCCESS;
428
429 exit_error:
430     vlc_UrlClean( &p_sys->url );
431     free( p_sys );
432     return VLC_EGENERIC;
433 }
434
435 /*****************************************************************************
436  * Close: free unused data structures
437  *****************************************************************************/
438 static void Close( vlc_object_t *p_access, access_sys_t *p_sys )
439 {
440     msg_Dbg( p_access, "stopping stream" );
441     ftp_StopStream( p_access, p_sys );
442
443     if( ftp_SendCommand( p_access, p_sys, "QUIT" ) < 0 )
444     {
445         msg_Warn( p_access, "cannot quit" );
446     }
447     else
448     {
449         ftp_ReadCommand( p_access, p_sys, NULL, NULL );
450     }
451     net_Close( p_sys->fd_cmd );
452
453     /* free memory */
454     vlc_UrlClean( &p_sys->url );
455     free( p_sys );
456 }
457
458 static void InClose( vlc_object_t *p_this )
459 {
460     Close( p_this, ((access_t *)p_this)->p_sys);
461 }
462
463 static void OutClose( vlc_object_t *p_this )
464 {
465     Close( p_this, GET_OUT_SYS(p_this));
466 }
467
468
469 /*****************************************************************************
470  * Seek: try to go at the right place
471  *****************************************************************************/
472 static int _Seek( vlc_object_t *p_access, access_sys_t *p_sys, uint64_t i_pos )
473 {
474     msg_Dbg( p_access, "seeking to %"PRIu64, i_pos );
475
476     ftp_StopStream( (vlc_object_t *)p_access, p_sys );
477     if( ftp_StartStream( (vlc_object_t *)p_access, p_sys, i_pos ) < 0 )
478         return VLC_EGENERIC;
479
480     return VLC_SUCCESS;
481 }
482
483 static int Seek( access_t *p_access, uint64_t i_pos )
484 {
485     int val = _Seek( (vlc_object_t *)p_access, p_access->p_sys, i_pos );
486     if( val )
487         return val;
488
489     p_access->info.b_eof = false;
490     p_access->info.i_pos = i_pos;
491
492     return VLC_SUCCESS;
493 }
494
495 static int OutSeek( sout_access_out_t *p_access, off_t i_pos )
496 {
497     return _Seek( (vlc_object_t *)p_access, GET_OUT_SYS( p_access ), i_pos);
498 }
499
500 /*****************************************************************************
501  * Read:
502  *****************************************************************************/
503 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
504 {
505     access_sys_t *p_sys = p_access->p_sys;
506
507     assert( p_sys->fd_data != -1 );
508     assert( !p_sys->out );
509
510     if( p_access->info.b_eof )
511         return 0;
512
513     if( p_sys->directory )
514     {
515         char *psz_line = net_Gets( p_access, p_sys->fd_data, NULL );
516         if( !psz_line )
517         {
518             p_access->info.b_eof = true;
519             return 0;
520         }
521         else
522         {
523             snprintf( (char*)p_buffer, i_len, "ftp://%s:%d%s/%s\n",
524                       p_sys->url.psz_host, p_sys->url.i_port,
525                       p_sys->url.psz_path, psz_line );
526             free( psz_line );
527             return strlen( (const char *)p_buffer );
528         }
529     }
530     else
531     {
532         int i_read = net_Read( p_access, p_sys->fd_data, NULL,
533                                p_buffer, i_len, false );
534         if( i_read == 0 )
535             p_access->info.b_eof = true;
536         else if( i_read > 0 )
537             p_access->info.i_pos += i_read;
538
539         return i_read;
540     }
541 }
542
543 /*****************************************************************************
544  * Write:
545  *****************************************************************************/
546 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
547 {
548     access_sys_t *p_sys = GET_OUT_SYS(p_access);
549     size_t i_write = 0;
550
551     assert( p_sys->fd_data != -1 );
552
553     while( p_buffer != NULL )
554     {
555         block_t *p_next = p_buffer->p_next;;
556
557         i_write += net_Write( p_access, p_sys->fd_data, NULL,
558                               p_buffer->p_buffer, p_buffer->i_buffer );
559         block_Release( p_buffer );
560
561         p_buffer = p_next;
562     }
563
564     return i_write;
565 }
566
567 /*****************************************************************************
568  * Control:
569  *****************************************************************************/
570 static int Control( access_t *p_access, int i_query, va_list args )
571 {
572     bool    *pb_bool;
573     int64_t *pi_64;
574
575     switch( i_query )
576     {
577         /* */
578         case ACCESS_CAN_SEEK:
579             pb_bool = (bool*)va_arg( args, bool* );
580             *pb_bool = true;
581             break;
582         case ACCESS_CAN_FASTSEEK:
583             pb_bool = (bool*)va_arg( args, bool* );
584             *pb_bool = false;
585             break;
586         case ACCESS_CAN_PAUSE:
587             pb_bool = (bool*)va_arg( args, bool* );
588             *pb_bool = true;    /* FIXME */
589             break;
590         case ACCESS_CAN_CONTROL_PACE:
591             pb_bool = (bool*)va_arg( args, bool* );
592             *pb_bool = true;    /* FIXME */
593             break;
594
595         /* */
596         case ACCESS_GET_PTS_DELAY:
597             pi_64 = (int64_t*)va_arg( args, int64_t * );
598             *pi_64 = var_GetInteger( p_access, "ftp-caching" ) * INT64_C(1000);
599             break;
600
601         /* */
602         case ACCESS_SET_PAUSE_STATE:
603             pb_bool = (bool*)va_arg( args, bool* );
604             if ( !pb_bool )
605               return Seek( p_access, p_access->info.i_pos );
606             break;
607
608         case ACCESS_GET_TITLE_INFO:
609         case ACCESS_SET_TITLE:
610         case ACCESS_SET_SEEKPOINT:
611         case ACCESS_SET_PRIVATE_ID_STATE:
612         case ACCESS_GET_CONTENT_TYPE:
613         case ACCESS_GET_META:
614             return VLC_EGENERIC;
615
616         default:
617             msg_Warn( p_access, "unimplemented query in control: %d", i_query);
618             return VLC_EGENERIC;
619
620     }
621     return VLC_SUCCESS;
622 }
623
624 /*****************************************************************************
625  * ftp_*:
626  *****************************************************************************/
627 static int ftp_SendCommand( vlc_object_t *p_access, access_sys_t *p_sys,
628                             const char *psz_fmt, ... )
629 {
630     va_list      args;
631     char         *psz_cmd;
632
633     va_start( args, psz_fmt );
634     if( vasprintf( &psz_cmd, psz_fmt, args ) == -1 )
635         return VLC_EGENERIC;
636
637     va_end( args );
638
639     msg_Dbg( p_access, "ftp_SendCommand:\"%s\"", psz_cmd);
640
641     if( net_Printf( p_access, p_sys->fd_cmd, NULL, "%s\r\n", psz_cmd ) < 0 )
642     {
643         msg_Err( p_access, "failed to send command" );
644         return VLC_EGENERIC;
645     }
646     return VLC_SUCCESS;
647 }
648
649 /* TODO support this s**t :
650  RFC 959 allows the client to send certain TELNET strings at any moment,
651  even in the middle of a request:
652
653  * \377\377.
654  * \377\376x where x is one byte.
655  * \377\375x where x is one byte. The server is obliged to send \377\374x
656  *                                immediately after reading x.
657  * \377\374x where x is one byte.
658  * \377\373x where x is one byte. The server is obliged to send \377\376x
659  *                                immediately after reading x.
660  * \377x for any other byte x.
661
662  These strings are not part of the requests, except in the case \377\377,
663  where the request contains one \377. */
664 static int ftp_ReadCommand( vlc_object_t *p_access, access_sys_t *p_sys,
665                             int *pi_answer, char **ppsz_answer )
666 {
667     char         *psz_line;
668     int          i_answer;
669
670     psz_line = net_Gets( p_access, p_sys->fd_cmd, NULL );
671     if( psz_line == NULL || strlen( psz_line ) < 3 )
672     {
673         msg_Err( p_access, "cannot get answer" );
674         free( psz_line );
675         if( pi_answer ) *pi_answer    = 500;
676         if( ppsz_answer ) *ppsz_answer  = NULL;
677         return -1;
678     }
679     msg_Dbg( p_access, "answer=%s", psz_line );
680
681     if( psz_line[3] == '-' )    /* Multiple response */
682     {
683         char end[4];
684
685         memcpy( end, psz_line, 3 );
686         end[3] = ' ';
687
688         for( ;; )
689         {
690             char *psz_tmp = net_Gets( p_access, p_sys->fd_cmd, NULL );
691
692             if( psz_tmp == NULL )   /* Error */
693                 break;
694
695             if( !strncmp( psz_tmp, end, 4 ) )
696             {
697                 free( psz_tmp );
698                 break;
699             }
700             free( psz_tmp );
701         }
702     }
703
704     i_answer = atoi( psz_line );
705
706     if( pi_answer ) *pi_answer = i_answer;
707     if( ppsz_answer )
708     {
709         *ppsz_answer = psz_line;
710     }
711     else
712     {
713         free( psz_line );
714     }
715     return( i_answer / 100 );
716 }
717
718 static int ftp_StartStream( vlc_object_t *p_access, access_sys_t *p_sys,
719                             uint64_t i_start )
720 {
721     char psz_ipv4[16], *psz_ip = p_sys->sz_epsv_ip;
722     int  i_answer;
723     char *psz_arg, *psz_parser;
724     int  i_port;
725
726     assert( p_sys->fd_data == -1 );
727
728     if( ( ftp_SendCommand( p_access, p_sys, *psz_ip ? "EPSV" : "PASV" ) < 0 )
729      || ( ftp_ReadCommand( p_access, p_sys, &i_answer, &psz_arg ) != 2 ) )
730     {
731         msg_Err( p_access, "cannot set passive mode" );
732         return VLC_EGENERIC;
733     }
734
735     psz_parser = strchr( psz_arg, '(' );
736     if( psz_parser == NULL )
737     {
738         free( psz_arg );
739         msg_Err( p_access, "cannot parse passive mode response" );
740         return VLC_EGENERIC;
741     }
742
743     if( *psz_ip )
744     {
745         char psz_fmt[7] = "(|||%u";
746         psz_fmt[1] = psz_fmt[2] = psz_fmt[3] = psz_parser[1];
747
748         if( sscanf( psz_parser, psz_fmt, &i_port ) < 1 )
749         {
750             free( psz_arg );
751             msg_Err( p_access, "cannot parse passive mode response" );
752             return VLC_EGENERIC;
753         }
754     }
755     else
756     {
757         unsigned a1, a2, a3, a4, p1, p2;
758
759         if( ( sscanf( psz_parser, "(%u,%u,%u,%u,%u,%u", &a1, &a2, &a3, &a4,
760                       &p1, &p2 ) < 6 ) || ( a1 > 255 ) || ( a2 > 255 )
761          || ( a3 > 255 ) || ( a4 > 255 ) || ( p1 > 255 ) || ( p2 > 255 ) )
762         {
763             free( psz_arg );
764             msg_Err( p_access, "cannot parse passive mode response" );
765             return VLC_EGENERIC;
766         }
767
768         sprintf( psz_ipv4, "%u.%u.%u.%u", a1, a2, a3, a4 );
769         psz_ip = psz_ipv4;
770         i_port = (p1 << 8) | p2;
771     }
772     free( psz_arg );
773
774     msg_Dbg( p_access, "ip:%s port:%d", psz_ip, i_port );
775
776     if( ftp_SendCommand( p_access, p_sys, "TYPE I" ) < 0 ||
777         ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) != 2 )
778     {
779         msg_Err( p_access, "cannot set binary transfer mode" );
780         return VLC_EGENERIC;
781     }
782
783     if( i_start > 0 )
784     {
785         if( ftp_SendCommand( p_access, p_sys, "REST %"PRIu64, i_start ) < 0 ||
786             ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) > 3 )
787         {
788             msg_Err( p_access, "cannot set restart offset" );
789             return VLC_EGENERIC;
790         }
791     }
792
793     msg_Dbg( p_access, "waiting for data connection..." );
794     p_sys->fd_data = net_ConnectTCP( p_access, psz_ip, i_port );
795     if( p_sys->fd_data < 0 )
796     {
797         msg_Err( p_access, "failed to connect with server" );
798         return VLC_EGENERIC;
799     }
800     msg_Dbg( p_access, "connection with \"%s:%d\" successful",
801              psz_ip, i_port );
802
803     if( p_sys->directory )
804     {
805         if( ftp_SendCommand( p_access, p_sys, "NLST" ) < 0 ||
806             ftp_ReadCommand( p_access, p_sys, NULL, &psz_arg ) > 2 )
807         {
808             msg_Err( p_access, "cannot list directory contents" );
809         return VLC_EGENERIC;
810         }
811     }
812     else
813     {
814         /* "1xx" message */
815         assert( p_sys->url.psz_path );
816         if( ftp_SendCommand( p_access, p_sys, "%s %s",
817                              p_sys->out ? "STOR" : "RETR",
818                              p_sys->url.psz_path ) < 0
819          || ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) > 2 )
820         {
821             msg_Err( p_access, "cannot retrieve file" );
822             return VLC_EGENERIC;
823         }
824     }
825
826     shutdown( p_sys->fd_data, p_sys->out ? SHUT_RD : SHUT_WR );
827
828     return VLC_SUCCESS;
829 }
830
831 static int ftp_StopStream ( vlc_object_t *p_access, access_sys_t *p_sys )
832 {
833     if( ftp_SendCommand( p_access, p_sys, "ABOR" ) < 0 )
834     {
835         msg_Warn( p_access, "cannot abort file" );
836         if(  p_sys->fd_data > 0 )
837             net_Close( p_sys->fd_data );
838         p_sys->fd_data = -1;
839         return VLC_EGENERIC;
840     }
841
842     if( p_sys->fd_data != -1 )
843     {
844         net_Close( p_sys->fd_data );
845         p_sys->fd_data = -1;
846         /* Read the final response from RETR/STOR, i.e. 426 or 226 */
847         ftp_ReadCommand( p_access, p_sys, NULL, NULL );
848     }
849     /* Read the response from ABOR, i.e. 226 or 225 */
850     ftp_ReadCommand( p_access, p_sys, NULL, NULL );
851
852     return VLC_SUCCESS;
853 }