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