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