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