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