A little more detail regarding using my github copies of the code with where it's...
[vlc/adversarial.git] / modules / access / ftp.c
blobaa42a31dd6b9d9adb436cfa48c404cb858920825
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$
8 * Authors: Laurent Aimar <fenrir@via.ecp.fr> - original code
9 * Rémi Denis-Courmont <rem # videolan.org> - EPSV support
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.
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.
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 *****************************************************************************/
26 /*****************************************************************************
27 * Preamble
28 *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
36 #include <assert.h>
38 #include <vlc_access.h>
39 #include <vlc_dialog.h>
41 #include <vlc_network.h>
42 #include <vlc_url.h>
43 #include <vlc_tls.h>
44 #include <vlc_sout.h>
45 #include <vlc_charset.h>
47 #ifndef IPPORT_FTP
48 # define IPPORT_FTP 21u
49 #endif
51 #ifndef IPPORT_FTPS
52 # define IPPORT_FTPS 990u
53 #endif
55 /*****************************************************************************
56 * Module descriptor
57 *****************************************************************************/
58 static int InOpen ( vlc_object_t * );
59 static void InClose( vlc_object_t * );
60 #ifdef ENABLE_SOUT
61 static int OutOpen ( vlc_object_t * );
62 static void OutClose( vlc_object_t * );
63 #endif
65 #define USER_TEXT N_("FTP user name")
66 #define USER_LONGTEXT N_("User name that will " \
67 "be used for the connection.")
68 #define PASS_TEXT N_("FTP password")
69 #define PASS_LONGTEXT N_("Password that will be " \
70 "used for the connection.")
71 #define ACCOUNT_TEXT N_("FTP account")
72 #define ACCOUNT_LONGTEXT N_("Account that will be " \
73 "used for the connection.")
75 vlc_module_begin ()
76 set_shortname( "FTP" )
77 set_description( N_("FTP input") )
78 set_capability( "access", 0 )
79 set_category( CAT_INPUT )
80 set_subcategory( SUBCAT_INPUT_ACCESS )
81 add_string( "ftp-user", "anonymous", USER_TEXT, USER_LONGTEXT,
82 false )
83 add_string( "ftp-pwd", "anonymous@example.com", PASS_TEXT,
84 PASS_LONGTEXT, false )
85 add_string( "ftp-account", "anonymous", ACCOUNT_TEXT,
86 ACCOUNT_LONGTEXT, false )
87 add_shortcut( "ftp", "ftps", "ftpes" )
88 set_callbacks( InOpen, InClose )
90 #ifdef ENABLE_SOUT
91 add_submodule ()
92 set_shortname( "FTP" )
93 set_description( N_("FTP upload output") )
94 set_capability( "sout access", 0 )
95 set_category( CAT_SOUT )
96 set_subcategory( SUBCAT_SOUT_ACO )
97 add_shortcut( "ftp", "ftps", "ftpes" )
98 set_callbacks( OutOpen, OutClose )
99 #endif
100 vlc_module_end ()
102 /*****************************************************************************
103 * Local prototypes
104 *****************************************************************************/
105 static ssize_t Read( access_t *, uint8_t *, size_t );
106 static int Seek( access_t *, uint64_t );
107 static int Control( access_t *, int, va_list );
108 #ifdef ENABLE_SOUT
109 static int OutSeek( sout_access_out_t *, off_t );
110 static ssize_t Write( sout_access_out_t *, block_t * );
111 #endif
113 static void FeaturesCheck( void *, const char * );
115 typedef struct ftp_features_t
117 bool b_unicode;
118 bool b_authtls;
119 } ftp_features_t;
121 enum tls_mode_e
123 NONE = 0,
124 IMPLICIT,/* ftps */
125 EXPLICIT /* ftpes */
128 struct access_sys_t
130 vlc_url_t url;
132 ftp_features_t features;
133 vlc_tls_creds_t *p_creds;
134 enum tls_mode_e tlsmode;
135 struct
137 vlc_tls_t *p_tls;
138 v_socket_t *p_vs;
139 int fd;
140 } cmd, data;
142 char sz_epsv_ip[NI_MAXNUMERICHOST];
143 bool out;
144 bool directory;
145 uint64_t size;
147 #define GET_OUT_SYS( p_this ) \
148 ((access_sys_t *)(((sout_access_out_t *)(p_this))->p_sys))
150 static int ftp_SendCommand( vlc_object_t *obj, access_sys_t *sys,
151 const char *fmt, ... )
153 size_t fmtlen = strlen( fmt );
154 char fmtbuf[fmtlen + 3];
156 memcpy( fmtbuf, fmt, fmtlen );
157 memcpy( fmtbuf + fmtlen, "\r\n", 3 );
159 va_list args;
160 char *cmd;
161 int val;
163 va_start( args, fmt );
164 val = vasprintf( &cmd, fmtbuf, args );
165 va_end( args );
166 if( unlikely(val == -1) )
167 return -1;
169 msg_Dbg( obj, "sending request: \"%.*s\" (%d bytes)", val - 2, cmd, val );
170 if( net_Write( obj, sys->cmd.fd, sys->cmd.p_vs, cmd, val ) != val )
172 msg_Err( obj, "request failure" );
173 val = -1;
175 else
176 val = 0;
177 free( cmd );
178 return val;
181 /* TODO support this s**t :
182 RFC 959 allows the client to send certain TELNET strings at any moment,
183 even in the middle of a request:
185 * \377\377.
186 * \377\376x where x is one byte.
187 * \377\375x where x is one byte. The server is obliged to send \377\374x
188 * immediately after reading x.
189 * \377\374x where x is one byte.
190 * \377\373x where x is one byte. The server is obliged to send \377\376x
191 * immediately after reading x.
192 * \377x for any other byte x.
194 These strings are not part of the requests, except in the case \377\377,
195 where the request contains one \377. */
196 static int ftp_RecvAnswer( vlc_object_t *obj, access_sys_t *sys,
197 int *restrict codep, char **restrict strp,
198 void (*cb)(void *, const char *), void *opaque )
200 if( codep != NULL )
201 *codep = 500;
202 if( strp != NULL )
203 *strp = NULL;
205 char *resp = net_Gets( obj, sys->cmd.fd, sys->cmd.p_vs );
206 if( resp == NULL )
208 msg_Err( obj, "response failure" );
209 goto error;
212 char *end;
213 unsigned code = strtoul( resp, &end, 10 );
214 if( (end - resp) != 3 || (*end != '-' && *end != ' ') )
216 msg_Err( obj, "malformatted response" );
217 goto error;
219 msg_Dbg( obj, "received response: \"%s\"", resp );
221 if( *end == '-' ) /* Multi-line response */
223 bool done;
225 *end = ' ';
228 char *line = net_Gets( obj, sys->cmd.fd, sys->cmd.p_vs );
229 if( line == NULL )
231 msg_Err( obj, "response failure" );
232 goto error;
235 done = !strncmp( resp, line, 4 );
236 if( !done )
237 cb( opaque, line );
238 free( line );
240 while( !done );
243 if( codep != NULL )
244 *codep = code;
245 if( strp != NULL )
246 *strp = resp;
247 else
248 free( resp );
249 return code / 100;
250 error:
251 free( resp );
252 return -1;
255 static void DummyLine( void *data, const char *str )
257 (void) data; (void) str;
260 static int ftp_RecvCommand( vlc_object_t *obj, access_sys_t *sys,
261 int *restrict codep, char **restrict strp )
263 return ftp_RecvAnswer( obj, sys, codep, strp, DummyLine, NULL );
266 static int ftp_StartStream( vlc_object_t *, access_sys_t *, uint64_t );
267 static int ftp_StopStream ( vlc_object_t *, access_sys_t * );
269 static void readTLSMode( access_sys_t *p_sys, const char * psz_access )
271 if ( !strncmp( psz_access, "ftps", 4 ) )
272 p_sys->tlsmode = IMPLICIT;
273 else
274 if ( !strncmp( psz_access, "ftpes", 5 ) )
275 p_sys->tlsmode = EXPLICIT;
276 else
277 p_sys->tlsmode = NONE;
280 static int createCmdTLS( vlc_object_t *p_access, access_sys_t *p_sys, int fd,
281 const char *psz_session_name )
283 p_sys->p_creds = vlc_tls_ClientCreate( p_access );
284 if( p_sys->p_creds == NULL ) return -1;
286 /* TLS/SSL handshake */
287 p_sys->cmd.p_tls = vlc_tls_ClientSessionCreate( p_sys->p_creds, fd,
288 p_sys->url.psz_host,
289 psz_session_name,
290 NULL, NULL );
291 if( p_sys->cmd.p_tls == NULL )
293 msg_Err( p_access, "cannot establish FTP/TLS session on command channel" );
294 return -1;
296 p_sys->cmd.p_vs = &p_sys->cmd.p_tls->sock;
298 return 0;
301 static void clearCmdTLS( access_sys_t *p_sys )
303 if ( p_sys->cmd.p_tls ) vlc_tls_SessionDelete( p_sys->cmd.p_tls );
304 if ( p_sys->p_creds ) vlc_tls_Delete( p_sys->p_creds );
305 p_sys->cmd.p_tls = NULL;
306 p_sys->cmd.p_vs = NULL;
307 p_sys->p_creds = NULL;
310 static int Login( vlc_object_t *p_access, access_sys_t *p_sys )
312 int i_answer;
313 char *psz;
315 /* *** Open a TCP connection with server *** */
316 int fd = p_sys->cmd.fd = net_ConnectTCP( p_access, p_sys->url.psz_host,
317 p_sys->url.i_port );
318 if( fd == -1 )
320 msg_Err( p_access, "connection failed" );
321 dialog_Fatal( p_access, _("Network interaction failed"), "%s",
322 _("VLC could not connect with the given server.") );
323 return -1;
326 if ( p_sys->tlsmode == IMPLICIT ) /* FTPS Mode */
328 if ( createCmdTLS( p_access, p_sys, fd, "ftps") < 0 )
329 goto error;
332 while( ftp_RecvCommand( p_access, p_sys, &i_answer, NULL ) == 1 );
334 if( i_answer / 100 != 2 )
336 msg_Err( p_access, "connection rejected" );
337 dialog_Fatal( p_access, _("Network interaction failed"), "%s",
338 _("VLC's connection to the given server was rejected.") );
339 return -1;
342 msg_Dbg( p_access, "connection accepted (%d)", i_answer );
344 if( p_sys->url.psz_username && *p_sys->url.psz_username )
345 psz = strdup( p_sys->url.psz_username );
346 else
347 psz = var_InheritString( p_access, "ftp-user" );
348 if( !psz )
349 return -1;
351 /* Features check first */
352 if( ftp_SendCommand( p_access, p_sys, "FEAT" ) < 0
353 || ftp_RecvAnswer( p_access, p_sys, NULL, NULL,
354 FeaturesCheck, &p_sys->features ) < 0 )
356 msg_Err( p_access, "cannot get server features" );
357 return -1;
360 /* Create TLS Session */
361 if( p_sys->tlsmode == EXPLICIT )
363 if ( ! p_sys->features.b_authtls )
365 msg_Err( p_access, "Server does not support TLS" );
366 return -1;
369 if( ftp_SendCommand( p_access, p_sys, "AUTH TLS" ) < 0
370 || ftp_RecvCommand( p_access, p_sys, &i_answer, NULL ) < 0
371 || i_answer != 234 )
373 msg_Err( p_access, "cannot switch to TLS: server replied with code %d",
374 i_answer );
375 return -1;
378 if ( createCmdTLS( p_access, p_sys, fd, "ftpes") < 0 )
380 goto error;
384 if( p_sys->tlsmode != NONE )
386 if( ftp_SendCommand( p_access, p_sys, "PBSZ 0" ) < 0 ||
387 ftp_RecvCommand( p_access, p_sys, &i_answer, NULL ) < 0 ||
388 i_answer != 200 )
390 msg_Err( p_access, "Can't truncate Protection buffer size for TLS" );
391 free( psz );
392 goto error;
395 if( ftp_SendCommand( p_access, p_sys, "PROT P" ) < 0 ||
396 ftp_RecvCommand( p_access, p_sys, &i_answer, NULL ) < 0 ||
397 i_answer != 200 )
399 msg_Err( p_access, "Can't set Data channel protection" );
400 free( psz );
401 goto error;
405 /* Send credentials over channel */
406 if( ftp_SendCommand( p_access, p_sys, "USER %s", psz ) < 0 ||
407 ftp_RecvCommand( p_access, p_sys, &i_answer, NULL ) < 0 )
409 free( psz );
410 goto error;
412 free( psz );
414 switch( i_answer / 100 )
416 case 2:
417 /* X.509 auth successful after AUTH TLS / RFC 2228 sec. 4 */
418 if ( i_answer == 232 )
419 msg_Dbg( p_access, "user accepted and authenticated" );
420 else
421 msg_Dbg( p_access, "user accepted" );
422 break;
423 case 3:
424 msg_Dbg( p_access, "password needed" );
425 if( p_sys->url.psz_password && *p_sys->url.psz_password )
426 psz = strdup( p_sys->url.psz_password );
427 else
428 psz = var_InheritString( p_access, "ftp-pwd" );
429 if( !psz )
430 goto error;
432 if( ftp_SendCommand( p_access, p_sys, "PASS %s", psz ) < 0 ||
433 ftp_RecvCommand( p_access, p_sys, &i_answer, NULL ) < 0 )
435 free( psz );
436 goto error;
438 free( psz );
440 switch( i_answer / 100 )
442 case 2:
443 msg_Dbg( p_access, "password accepted" );
444 break;
445 case 3:
446 msg_Dbg( p_access, "account needed" );
447 psz = var_InheritString( p_access, "ftp-account" );
448 if( ftp_SendCommand( p_access, p_sys, "ACCT %s",
449 psz ) < 0 ||
450 ftp_RecvCommand( p_access, p_sys, &i_answer, NULL ) < 0 )
452 free( psz );
453 goto error;
455 free( psz );
457 if( i_answer / 100 != 2 )
459 msg_Err( p_access, "account rejected" );
460 dialog_Fatal( p_access,
461 _("Network interaction failed"),
462 "%s", _("Your account was rejected.") );
463 goto error;
465 msg_Dbg( p_access, "account accepted" );
466 break;
468 default:
469 msg_Err( p_access, "password rejected" );
470 dialog_Fatal( p_access, _("Network interaction failed"),
471 "%s", _("Your password was rejected.") );
472 goto error;
474 break;
475 default:
476 msg_Err( p_access, "user rejected" );
477 dialog_Fatal( p_access, _("Network interaction failed"), "%s",
478 _("Your connection attempt to the server was rejected.") );
479 goto error;
482 return 0;
484 error:
485 clearCmdTLS( p_sys );
486 return -1;
489 static void FeaturesCheck( void *opaque, const char *feature )
491 ftp_features_t *features = opaque;
493 if( strcasestr( feature, "UTF8" ) != NULL )
494 features->b_unicode = true;
495 else
496 if( strcasestr( feature, "AUTH TLS" ) != NULL )
497 features->b_authtls = true;
500 static const char *IsASCII( const char *str )
502 int8_t c;
503 for( const char *p = str; (c = *p) != '\0'; p++ )
504 if( c < 0 )
505 return NULL;
506 return str;
509 static int Connect( vlc_object_t *p_access, access_sys_t *p_sys )
511 if( Login( p_access, p_sys ) < 0 )
512 return -1;
514 /* Extended passive mode */
515 if( ftp_SendCommand( p_access, p_sys, "EPSV ALL" ) < 0 )
517 msg_Err( p_access, "cannot request extended passive mode" );
518 goto error;
521 if( ftp_RecvCommand( p_access, p_sys, NULL, NULL ) == 2 )
523 if( net_GetPeerAddress( p_sys->cmd.fd, p_sys->sz_epsv_ip, NULL ) )
524 goto error;
526 else
528 /* If ESPV ALL fails, we fallback to PASV.
529 * We have to restart the connection in case there is a NAT that
530 * understands EPSV ALL in the way, and hence won't allow PASV on
531 * the initial connection.
533 msg_Info( p_access, "FTP Extended passive mode disabled" );
534 clearCmdTLS( p_sys );
535 net_Close( p_sys->cmd.fd );
537 if( Login( p_access, p_sys ) )
538 goto error;
541 if( (p_sys->features.b_unicode ? IsUTF8 : IsASCII)(p_sys->url.psz_path) == NULL )
543 msg_Err( p_access, "unsupported path: \"%s\"", p_sys->url.psz_path );
544 goto error;
547 /* check binary mode support */
548 if( ftp_SendCommand( p_access, p_sys, "TYPE I" ) < 0 ||
549 ftp_RecvCommand( p_access, p_sys, NULL, NULL ) != 2 )
551 msg_Err( p_access, "cannot set binary transfer mode" );
552 goto error;
555 return 0;
557 error:
558 clearCmdTLS( p_sys );
559 net_Close( p_sys->cmd.fd );
560 return -1;
564 static int parseURL( vlc_url_t *url, const char *path, enum tls_mode_e mode )
566 if( path == NULL )
567 return VLC_EGENERIC;
569 /* *** Parse URL and get server addr/port and path *** */
570 while( *path == '/' )
571 path++;
573 vlc_UrlParse( url, path, 0 );
575 if( url->psz_host == NULL || *url->psz_host == '\0' )
576 return VLC_EGENERIC;
578 if( url->i_port <= 0 )
580 if( mode == IMPLICIT )
581 url->i_port = IPPORT_FTPS;
582 else
583 url->i_port = IPPORT_FTP; /* default port */
586 if( url->psz_path == NULL )
587 return VLC_SUCCESS;
588 /* FTP URLs are relative to user's default directory (RFC1738 §3.2)
589 For absolute path use ftp://foo.bar//usr/local/etc/filename */
590 /* FIXME: we should issue a series of CWD, one per slash */
591 if( url->psz_path )
593 assert( url->psz_path[0] == '/' );
594 url->psz_path++;
597 char *type = strstr( url->psz_path, ";type=" );
598 if( type )
600 *type = '\0';
601 if( strchr( "iI", type[6] ) == NULL )
602 return VLC_EGENERIC; /* ASCII and directory not supported */
604 decode_URI( url->psz_path );
605 return VLC_SUCCESS;
609 /****************************************************************************
610 * Open: connect to ftp server and ask for file
611 ****************************************************************************/
612 static int InOpen( vlc_object_t *p_this )
614 access_t *p_access = (access_t*)p_this;
615 access_sys_t *p_sys;
616 char *psz_arg;
618 /* Init p_access */
619 STANDARD_READ_ACCESS_INIT
620 p_sys->data.fd = -1;
621 p_sys->out = false;
622 p_sys->directory = false;
623 p_sys->size = 0;
624 readTLSMode( p_sys, p_access->psz_access );
626 if( parseURL( &p_sys->url, p_access->psz_location, p_sys->tlsmode ) )
627 goto exit_error;
629 if( Connect( p_this, p_sys ) )
630 goto exit_error;
632 /* get size */
633 if( p_sys->url.psz_path == NULL )
634 p_sys->directory = true;
635 else
636 if( ftp_SendCommand( p_this, p_sys, "SIZE %s", p_sys->url.psz_path ) < 0 )
637 goto error;
638 else
639 if ( ftp_RecvCommand( p_this, p_sys, NULL, &psz_arg ) == 2 )
641 p_sys->size = atoll( &psz_arg[4] );
642 free( psz_arg );
643 msg_Dbg( p_access, "file size: %"PRIu64, p_sys->size );
645 else
646 if( ftp_SendCommand( p_this, p_sys, "CWD %s", p_sys->url.psz_path ) < 0 )
647 goto error;
648 else
649 if( ftp_RecvCommand( p_this, p_sys, NULL, NULL ) != 2 )
651 msg_Err( p_this, "file or directory does not exist" );
652 goto error;
654 else
655 p_sys->directory = true;
657 /* Start the 'stream' */
658 if( ftp_StartStream( p_this, p_sys, 0 ) < 0 )
660 msg_Err( p_this, "cannot retrieve file" );
661 clearCmdTLS( p_sys );
662 net_Close( p_sys->cmd.fd );
663 goto exit_error;
666 return VLC_SUCCESS;
668 error:
669 clearCmdTLS( p_sys );
670 net_Close( p_sys->cmd.fd );
672 exit_error:
673 vlc_UrlClean( &p_sys->url );
674 free( p_sys );
675 return VLC_EGENERIC;
678 #ifdef ENABLE_SOUT
679 static int OutOpen( vlc_object_t *p_this )
681 sout_access_out_t *p_access = (sout_access_out_t *)p_this;
682 access_sys_t *p_sys;
684 p_sys = calloc( 1, sizeof( *p_sys ) );
685 if( !p_sys )
686 return VLC_ENOMEM;
688 /* Init p_access */
689 p_sys->data.fd = -1;
690 p_sys->out = true;
691 readTLSMode( p_sys, p_access->psz_access );
693 if( parseURL( &p_sys->url, p_access->psz_path, p_sys->tlsmode ) )
694 goto exit_error;
695 if( p_sys->url.psz_path == NULL )
697 msg_Err( p_this, "no filename specified" );
698 goto exit_error;
701 if( Connect( p_this, p_sys ) )
702 goto exit_error;
704 /* Start the 'stream' */
705 if( ftp_StartStream( p_this, p_sys, 0 ) < 0 )
707 msg_Err( p_access, "cannot store file" );
708 clearCmdTLS( p_sys );
709 net_Close( p_sys->cmd.fd );
710 goto exit_error;
713 p_access->pf_seek = OutSeek;
714 p_access->pf_write = Write;
715 p_access->p_sys = (void *)p_sys;
717 return VLC_SUCCESS;
719 exit_error:
720 vlc_UrlClean( &p_sys->url );
721 free( p_sys );
722 return VLC_EGENERIC;
724 #endif
726 /*****************************************************************************
727 * Close: free unused data structures
728 *****************************************************************************/
729 static void Close( vlc_object_t *p_access, access_sys_t *p_sys )
731 msg_Dbg( p_access, "stopping stream" );
732 ftp_StopStream( p_access, p_sys );
734 if( ftp_SendCommand( p_access, p_sys, "QUIT" ) < 0 )
736 msg_Warn( p_access, "cannot quit" );
738 else
740 ftp_RecvCommand( p_access, p_sys, NULL, NULL );
743 clearCmdTLS( p_sys );
744 net_Close( p_sys->cmd.fd );
746 /* free memory */
747 vlc_UrlClean( &p_sys->url );
748 free( p_sys );
751 static void InClose( vlc_object_t *p_this )
753 Close( p_this, ((access_t *)p_this)->p_sys);
756 #ifdef ENABLE_SOUT
757 static void OutClose( vlc_object_t *p_this )
759 Close( p_this, GET_OUT_SYS(p_this));
761 #endif
764 /*****************************************************************************
765 * Seek: try to go at the right place
766 *****************************************************************************/
767 static int _Seek( vlc_object_t *p_access, access_sys_t *p_sys, uint64_t i_pos )
769 msg_Dbg( p_access, "seeking to %"PRIu64, i_pos );
771 ftp_StopStream( (vlc_object_t *)p_access, p_sys );
772 if( ftp_StartStream( (vlc_object_t *)p_access, p_sys, i_pos ) < 0 )
773 return VLC_EGENERIC;
775 return VLC_SUCCESS;
778 static int Seek( access_t *p_access, uint64_t i_pos )
780 int val = _Seek( (vlc_object_t *)p_access, p_access->p_sys, i_pos );
781 if( val )
782 return val;
784 p_access->info.b_eof = false;
785 p_access->info.i_pos = i_pos;
787 return VLC_SUCCESS;
790 #ifdef ENABLE_SOUT
791 static int OutSeek( sout_access_out_t *p_access, off_t i_pos )
793 return _Seek( (vlc_object_t *)p_access, GET_OUT_SYS( p_access ), i_pos);
795 #endif
797 /*****************************************************************************
798 * Read:
799 *****************************************************************************/
800 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
802 access_sys_t *p_sys = p_access->p_sys;
804 assert( p_sys->data.fd != -1 );
805 assert( !p_sys->out );
807 if( p_access->info.b_eof )
808 return 0;
810 if( p_sys->directory )
812 char *psz_line = net_Gets( p_access, p_sys->data.fd, p_sys->data.p_vs );
813 if( !psz_line )
815 p_access->info.b_eof = true;
816 return 0;
818 else
820 snprintf( (char*)p_buffer, i_len, "%s://%s:%d/%s/%s\n",
821 ( p_sys->tlsmode == NONE ) ? "ftp" :
822 ( ( p_sys->tlsmode == IMPLICIT ) ? "ftps" : "ftpes" ),
823 p_sys->url.psz_host, p_sys->url.i_port,
824 p_sys->url.psz_path, psz_line );
825 free( psz_line );
826 return strlen( (const char *)p_buffer );
829 else
831 int i_read = net_Read( p_access, p_sys->data.fd, p_sys->data.p_vs,
832 p_buffer, i_len, false );
833 if( i_read == 0 )
834 p_access->info.b_eof = true;
835 else if( i_read > 0 )
836 p_access->info.i_pos += i_read;
838 return i_read;
842 /*****************************************************************************
843 * Write:
844 *****************************************************************************/
845 #ifdef ENABLE_SOUT
846 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
848 access_sys_t *p_sys = GET_OUT_SYS(p_access);
849 size_t i_write = 0;
851 assert( p_sys->data.fd != -1 );
853 while( p_buffer != NULL )
855 block_t *p_next = p_buffer->p_next;;
857 i_write += net_Write( p_access, p_sys->data.fd, p_sys->data.p_vs,
858 p_buffer->p_buffer, p_buffer->i_buffer );
859 block_Release( p_buffer );
861 p_buffer = p_next;
864 return i_write;
866 #endif
868 /*****************************************************************************
869 * Control:
870 *****************************************************************************/
871 static int Control( access_t *p_access, int i_query, va_list args )
873 bool *pb_bool;
874 int64_t *pi_64;
876 switch( i_query )
878 case ACCESS_CAN_SEEK:
879 pb_bool = (bool*)va_arg( args, bool* );
880 *pb_bool = !p_access->p_sys->directory;
881 break;
882 case ACCESS_CAN_FASTSEEK:
883 pb_bool = (bool*)va_arg( args, bool* );
884 *pb_bool = false;
885 break;
886 case ACCESS_CAN_PAUSE:
887 pb_bool = (bool*)va_arg( args, bool* );
888 *pb_bool = true; /* FIXME */
889 break;
890 case ACCESS_CAN_CONTROL_PACE:
891 pb_bool = (bool*)va_arg( args, bool* );
892 *pb_bool = true; /* FIXME */
893 break;
894 case ACCESS_GET_SIZE:
895 *va_arg( args, uint64_t * ) = p_access->p_sys->size;
896 break;
898 case ACCESS_GET_PTS_DELAY:
899 pi_64 = (int64_t*)va_arg( args, int64_t * );
900 *pi_64 = INT64_C(1000)
901 * var_InheritInteger( p_access, "network-caching" );
902 break;
904 case ACCESS_SET_PAUSE_STATE:
905 pb_bool = (bool*)va_arg( args, bool* );
906 if ( !pb_bool )
907 return Seek( p_access, p_access->info.i_pos );
908 break;
910 default:
911 return VLC_EGENERIC;
914 return VLC_SUCCESS;
917 static int ftp_StartStream( vlc_object_t *p_access, access_sys_t *p_sys,
918 uint64_t i_start )
920 char psz_ipv4[16], *psz_ip = p_sys->sz_epsv_ip;
921 int i_answer;
922 char *psz_arg, *psz_parser;
923 int i_port;
925 assert( p_sys->data.fd == -1 );
927 if( ( ftp_SendCommand( p_access, p_sys, *psz_ip ? "EPSV" : "PASV" ) < 0 )
928 || ( ftp_RecvCommand( p_access, p_sys, &i_answer, &psz_arg ) != 2 ) )
930 msg_Err( p_access, "cannot set passive mode" );
931 return VLC_EGENERIC;
934 psz_parser = strchr( psz_arg, '(' );
935 if( psz_parser == NULL )
937 free( psz_arg );
938 msg_Err( p_access, "cannot parse passive mode response" );
939 return VLC_EGENERIC;
942 if( *psz_ip )
944 char psz_fmt[7] = "(|||%u";
945 psz_fmt[1] = psz_fmt[2] = psz_fmt[3] = psz_parser[1];
947 if( sscanf( psz_parser, psz_fmt, &i_port ) < 1 )
949 free( psz_arg );
950 msg_Err( p_access, "cannot parse passive mode response" );
951 return VLC_EGENERIC;
954 else
956 unsigned a1, a2, a3, a4, p1, p2;
958 if( ( sscanf( psz_parser, "(%u,%u,%u,%u,%u,%u", &a1, &a2, &a3, &a4,
959 &p1, &p2 ) < 6 ) || ( a1 > 255 ) || ( a2 > 255 )
960 || ( a3 > 255 ) || ( a4 > 255 ) || ( p1 > 255 ) || ( p2 > 255 ) )
962 free( psz_arg );
963 msg_Err( p_access, "cannot parse passive mode response" );
964 return VLC_EGENERIC;
967 sprintf( psz_ipv4, "%u.%u.%u.%u", a1, a2, a3, a4 );
968 psz_ip = psz_ipv4;
969 i_port = (p1 << 8) | p2;
971 free( psz_arg );
973 msg_Dbg( p_access, "ip:%s port:%d", psz_ip, i_port );
975 if( ftp_SendCommand( p_access, p_sys, "TYPE I" ) < 0 ||
976 ftp_RecvCommand( p_access, p_sys, &i_answer, NULL ) != 2 )
978 msg_Err( p_access, "cannot set binary transfer mode" );
979 return VLC_EGENERIC;
982 if( i_start > 0 )
984 if( ftp_SendCommand( p_access, p_sys, "REST %"PRIu64, i_start ) < 0 ||
985 ftp_RecvCommand( p_access, p_sys, &i_answer, NULL ) > 3 )
987 msg_Err( p_access, "cannot set restart offset" );
988 return VLC_EGENERIC;
992 msg_Dbg( p_access, "waiting for data connection..." );
993 p_sys->data.fd = net_ConnectTCP( p_access, psz_ip, i_port );
994 if( p_sys->data.fd < 0 )
996 msg_Err( p_access, "failed to connect with server" );
997 return VLC_EGENERIC;
999 msg_Dbg( p_access, "connection with \"%s:%d\" successful",
1000 psz_ip, i_port );
1002 if( p_sys->directory )
1004 if( ftp_SendCommand( p_access, p_sys, "NLST" ) < 0 ||
1005 ftp_RecvCommand( p_access, p_sys, NULL, &psz_arg ) > 2 )
1007 msg_Err( p_access, "cannot list directory contents" );
1008 return VLC_EGENERIC;
1011 else
1013 /* "1xx" message */
1014 assert( p_sys->url.psz_path );
1015 if( ftp_SendCommand( p_access, p_sys, "%s %s",
1016 p_sys->out ? "STOR" : "RETR",
1017 p_sys->url.psz_path ) < 0
1018 || ftp_RecvCommand( p_access, p_sys, &i_answer, NULL ) > 2 )
1020 msg_Err( p_access, "cannot retrieve file" );
1021 return VLC_EGENERIC;
1025 if( p_sys->tlsmode != NONE )
1027 /* FIXME: Do Reuse TLS Session */
1028 /* TLS/SSL handshake */
1029 p_sys->data.p_tls = vlc_tls_ClientSessionCreate( p_sys->p_creds,
1030 p_sys->data.fd, p_sys->url.psz_host,
1031 ( p_sys->tlsmode == EXPLICIT ) ? "ftpes-data"
1032 : "ftps-data",
1033 NULL, NULL );
1034 if( p_sys->data.p_tls == NULL )
1036 msg_Err( p_access, "cannot establish FTP/TLS session for data" \
1037 ": server not allowing new session ?" );
1038 return VLC_EGENERIC;
1040 p_sys->data.p_vs = &p_sys->data.p_tls->sock;
1042 else
1043 shutdown( p_sys->data.fd, p_sys->out ? SHUT_RD : SHUT_WR );
1045 return VLC_SUCCESS;
1048 static int ftp_StopStream ( vlc_object_t *p_access, access_sys_t *p_sys )
1050 if( ftp_SendCommand( p_access, p_sys, "ABOR" ) < 0 )
1052 msg_Warn( p_access, "cannot abort file" );
1053 if( p_sys->data.fd > 0 )
1055 if ( p_sys->data.p_tls ) vlc_tls_SessionDelete( p_sys->data.p_tls );
1056 net_Close( p_sys->data.fd );
1058 p_sys->data.fd = -1;
1059 p_sys->data.p_tls = NULL;
1060 p_sys->data.p_vs = NULL;
1061 return VLC_EGENERIC;
1064 if( p_sys->data.fd != -1 )
1066 if ( p_sys->data.p_tls ) vlc_tls_SessionDelete( p_sys->data.p_tls );
1067 net_Close( p_sys->data.fd );
1068 p_sys->data.fd = -1;
1069 p_sys->data.p_tls = NULL;
1070 p_sys->data.p_vs = NULL;
1071 /* Read the final response from RETR/STOR, i.e. 426 or 226 */
1072 ftp_RecvCommand( p_access, p_sys, NULL, NULL );
1074 /* Read the response from ABOR, i.e. 226 or 225 */
1075 ftp_RecvCommand( p_access, p_sys, NULL, NULL );
1077 return VLC_SUCCESS;