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