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