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