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