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