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