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