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