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