1 /*****************************************************************************
2 * ftp.c: FTP input module
3 *****************************************************************************
4 * Copyright (C) 2001-2006 VLC authors and VideoLAN
5 * Copyright © 2006 Rémi Denis-Courmont
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 /*****************************************************************************
28 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
38 #include <vlc_access.h>
39 #include <vlc_dialog.h>
41 #include <vlc_network.h>
45 #include <vlc_charset.h>
48 # define IPPORT_FTP 21u
52 # define IPPORT_FTPS 990u
55 /*****************************************************************************
57 *****************************************************************************/
58 static int InOpen ( vlc_object_t * );
59 static void InClose( vlc_object_t * );
61 static int OutOpen ( vlc_object_t * );
62 static void OutClose( vlc_object_t * );
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.")
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,
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 )
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 )
102 /*****************************************************************************
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 );
109 static int OutSeek( sout_access_out_t *, off_t );
110 static ssize_t Write( sout_access_out_t *, block_t * );
113 static void FeaturesCheck( void *, const char * );
115 typedef struct ftp_features_t
132 ftp_features_t features;
133 vlc_tls_creds_t *p_creds;
134 enum tls_mode_e tlsmode;
142 char sz_epsv_ip[NI_MAXNUMERICHOST];
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 );
163 va_start( args, fmt );
164 val = vasprintf( &cmd, fmtbuf, args );
166 if( unlikely(val == -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" );
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:
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 )
205 char *resp = net_Gets( obj, sys->cmd.fd, sys->cmd.p_vs );
208 msg_Err( obj, "response failure" );
213 unsigned code = strtoul( resp, &end, 10 );
214 if( (end - resp) != 3 || (*end != '-' && *end != ' ') )
216 msg_Err( obj, "malformatted response" );
219 msg_Dbg( obj, "received response: \"%s\"", resp );
221 if( *end == '-' ) /* Multi-line response */
228 char *line = net_Gets( obj, sys->cmd.fd, sys->cmd.p_vs );
231 msg_Err( obj, "response failure" );
235 done = !strncmp( resp, line, 4 );
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;
274 if ( !strncmp( psz_access, "ftpes", 5 ) )
275 p_sys->tlsmode = EXPLICIT;
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,
290 if( p_sys->cmd.p_tls == NULL )
292 msg_Err( p_access, "cannot establish FTP/TLS session on command channel" );
295 p_sys->cmd.p_vs = &p_sys->cmd.p_tls->sock;
300 static void clearCmdTLS( access_sys_t *p_sys )
302 if ( p_sys->cmd.p_tls ) vlc_tls_SessionDelete( p_sys->cmd.p_tls );
303 if ( p_sys->p_creds ) vlc_tls_Delete( p_sys->p_creds );
304 p_sys->cmd.p_tls = NULL;
305 p_sys->cmd.p_vs = NULL;
306 p_sys->p_creds = NULL;
309 static int Login( vlc_object_t *p_access, access_sys_t *p_sys )
314 /* *** Open a TCP connection with server *** */
315 int fd = p_sys->cmd.fd = net_ConnectTCP( p_access, p_sys->url.psz_host,
319 msg_Err( p_access, "connection failed" );
320 dialog_Fatal( p_access, _("Network interaction failed"), "%s",
321 _("VLC could not connect with the given server.") );
325 if ( p_sys->tlsmode == IMPLICIT ) /* FTPS Mode */
327 if ( createCmdTLS( p_access, p_sys, fd, "ftps") < 0 )
331 while( ftp_RecvCommand( p_access, p_sys, &i_answer, NULL ) == 1 );
333 if( i_answer / 100 != 2 )
335 msg_Err( p_access, "connection rejected" );
336 dialog_Fatal( p_access, _("Network interaction failed"), "%s",
337 _("VLC's connection to the given server was rejected.") );
341 msg_Dbg( p_access, "connection accepted (%d)", i_answer );
343 if( p_sys->url.psz_username && *p_sys->url.psz_username )
344 psz = strdup( p_sys->url.psz_username );
346 psz = var_InheritString( p_access, "ftp-user" );
350 /* Features check first */
351 if( ftp_SendCommand( p_access, p_sys, "FEAT" ) < 0
352 || ftp_RecvAnswer( p_access, p_sys, NULL, NULL,
353 FeaturesCheck, &p_sys->features ) < 0 )
355 msg_Err( p_access, "cannot get server features" );
359 /* Create TLS Session */
360 if( p_sys->tlsmode == EXPLICIT )
362 if ( ! p_sys->features.b_authtls )
364 msg_Err( p_access, "Server does not support TLS" );
368 if( ftp_SendCommand( p_access, p_sys, "AUTH TLS" ) < 0
369 || ftp_RecvCommand( p_access, p_sys, &i_answer, NULL ) < 0
372 msg_Err( p_access, "cannot switch to TLS: server replied with code %d",
377 if ( createCmdTLS( p_access, p_sys, fd, "ftpes") < 0 )
383 if( p_sys->tlsmode != NONE )
385 if( ftp_SendCommand( p_access, p_sys, "PBSZ 0" ) < 0 ||
386 ftp_RecvCommand( p_access, p_sys, &i_answer, NULL ) < 0 ||
389 msg_Err( p_access, "Can't truncate Protection buffer size for TLS" );
394 if( ftp_SendCommand( p_access, p_sys, "PROT P" ) < 0 ||
395 ftp_RecvCommand( p_access, p_sys, &i_answer, NULL ) < 0 ||
398 msg_Err( p_access, "Can't set Data channel protection" );
404 /* Send credentials over channel */
405 if( ftp_SendCommand( p_access, p_sys, "USER %s", psz ) < 0 ||
406 ftp_RecvCommand( p_access, p_sys, &i_answer, NULL ) < 0 )
413 switch( i_answer / 100 )
416 /* X.509 auth successful after AUTH TLS / RFC 2228 sec. 4 */
417 if ( i_answer == 232 )
418 msg_Dbg( p_access, "user accepted and authenticated" );
420 msg_Dbg( p_access, "user accepted" );
423 msg_Dbg( p_access, "password needed" );
424 if( p_sys->url.psz_password && *p_sys->url.psz_password )
425 psz = strdup( p_sys->url.psz_password );
427 psz = var_InheritString( p_access, "ftp-pwd" );
431 if( ftp_SendCommand( p_access, p_sys, "PASS %s", psz ) < 0 ||
432 ftp_RecvCommand( p_access, p_sys, &i_answer, NULL ) < 0 )
439 switch( i_answer / 100 )
442 msg_Dbg( p_access, "password accepted" );
445 msg_Dbg( p_access, "account needed" );
446 psz = var_InheritString( p_access, "ftp-account" );
447 if( ftp_SendCommand( p_access, p_sys, "ACCT %s",
449 ftp_RecvCommand( p_access, p_sys, &i_answer, NULL ) < 0 )
456 if( i_answer / 100 != 2 )
458 msg_Err( p_access, "account rejected" );
459 dialog_Fatal( p_access,
460 _("Network interaction failed"),
461 "%s", _("Your account was rejected.") );
464 msg_Dbg( p_access, "account accepted" );
468 msg_Err( p_access, "password rejected" );
469 dialog_Fatal( p_access, _("Network interaction failed"),
470 "%s", _("Your password was rejected.") );
475 msg_Err( p_access, "user rejected" );
476 dialog_Fatal( p_access, _("Network interaction failed"), "%s",
477 _("Your connection attempt to the server was rejected.") );
484 clearCmdTLS( p_sys );
488 static void FeaturesCheck( void *opaque, const char *feature )
490 ftp_features_t *features = opaque;
492 if( strcasestr( feature, "UTF8" ) != NULL )
493 features->b_unicode = true;
495 if( strcasestr( feature, "AUTH TLS" ) != NULL )
496 features->b_authtls = true;
499 static const char *IsASCII( const char *str )
502 for( const char *p = str; (c = *p) != '\0'; p++ )
508 static int Connect( vlc_object_t *p_access, access_sys_t *p_sys )
510 if( Login( p_access, p_sys ) < 0 )
513 /* Extended passive mode */
514 if( ftp_SendCommand( p_access, p_sys, "EPSV ALL" ) < 0 )
516 msg_Err( p_access, "cannot request extended passive mode" );
520 if( ftp_RecvCommand( p_access, p_sys, NULL, NULL ) == 2 )
522 if( net_GetPeerAddress( p_sys->cmd.fd, p_sys->sz_epsv_ip, NULL ) )
527 /* If ESPV ALL fails, we fallback to PASV.
528 * We have to restart the connection in case there is a NAT that
529 * understands EPSV ALL in the way, and hence won't allow PASV on
530 * the initial connection.
532 msg_Info( p_access, "FTP Extended passive mode disabled" );
533 clearCmdTLS( p_sys );
534 net_Close( p_sys->cmd.fd );
536 if( Login( p_access, p_sys ) )
540 if( (p_sys->features.b_unicode ? IsUTF8 : IsASCII)(p_sys->url.psz_path) == NULL )
542 msg_Err( p_access, "unsupported path: \"%s\"", p_sys->url.psz_path );
546 /* check binary mode support */
547 if( ftp_SendCommand( p_access, p_sys, "TYPE I" ) < 0 ||
548 ftp_RecvCommand( p_access, p_sys, NULL, NULL ) != 2 )
550 msg_Err( p_access, "cannot set binary transfer mode" );
557 clearCmdTLS( p_sys );
558 net_Close( p_sys->cmd.fd );
563 static int parseURL( vlc_url_t *url, const char *path, enum tls_mode_e mode )
568 /* *** Parse URL and get server addr/port and path *** */
569 while( *path == '/' )
572 vlc_UrlParse( url, path, 0 );
574 if( url->psz_host == NULL || *url->psz_host == '\0' )
577 if( url->i_port <= 0 )
579 if( mode == IMPLICIT )
580 url->i_port = IPPORT_FTPS;
582 url->i_port = IPPORT_FTP; /* default port */
585 if( url->psz_path == NULL )
587 /* FTP URLs are relative to user's default directory (RFC1738 §3.2)
588 For absolute path use ftp://foo.bar//usr/local/etc/filename */
589 /* FIXME: we should issue a series of CWD, one per slash */
592 assert( url->psz_path[0] == '/' );
596 char *type = strstr( url->psz_path, ";type=" );
600 if( strchr( "iI", type[6] ) == NULL )
601 return VLC_EGENERIC; /* ASCII and directory not supported */
603 decode_URI( url->psz_path );
608 /****************************************************************************
609 * Open: connect to ftp server and ask for file
610 ****************************************************************************/
611 static int InOpen( vlc_object_t *p_this )
613 access_t *p_access = (access_t*)p_this;
618 STANDARD_READ_ACCESS_INIT
621 p_sys->directory = false;
623 readTLSMode( p_sys, p_access->psz_access );
625 if( parseURL( &p_sys->url, p_access->psz_location, p_sys->tlsmode ) )
628 if( Connect( p_this, p_sys ) )
632 if( p_sys->url.psz_path == NULL )
633 p_sys->directory = true;
635 if( ftp_SendCommand( p_this, p_sys, "SIZE %s", p_sys->url.psz_path ) < 0 )
638 if ( ftp_RecvCommand( p_this, p_sys, NULL, &psz_arg ) == 2 )
640 p_sys->size = atoll( &psz_arg[4] );
642 msg_Dbg( p_access, "file size: %"PRIu64, p_sys->size );
645 if( ftp_SendCommand( p_this, p_sys, "CWD %s", p_sys->url.psz_path ) < 0 )
648 if( ftp_RecvCommand( p_this, p_sys, NULL, NULL ) != 2 )
650 msg_Err( p_this, "file or directory does not exist" );
654 p_sys->directory = true;
656 /* Start the 'stream' */
657 if( ftp_StartStream( p_this, p_sys, 0 ) < 0 )
659 msg_Err( p_this, "cannot retrieve file" );
660 clearCmdTLS( p_sys );
661 net_Close( p_sys->cmd.fd );
668 clearCmdTLS( p_sys );
669 net_Close( p_sys->cmd.fd );
672 vlc_UrlClean( &p_sys->url );
678 static int OutOpen( vlc_object_t *p_this )
680 sout_access_out_t *p_access = (sout_access_out_t *)p_this;
683 p_sys = calloc( 1, sizeof( *p_sys ) );
690 readTLSMode( p_sys, p_access->psz_access );
692 if( parseURL( &p_sys->url, p_access->psz_path, p_sys->tlsmode ) )
694 if( p_sys->url.psz_path == NULL )
696 msg_Err( p_this, "no filename specified" );
700 if( Connect( p_this, p_sys ) )
703 /* Start the 'stream' */
704 if( ftp_StartStream( p_this, p_sys, 0 ) < 0 )
706 msg_Err( p_access, "cannot store file" );
707 clearCmdTLS( p_sys );
708 net_Close( p_sys->cmd.fd );
712 p_access->pf_seek = OutSeek;
713 p_access->pf_write = Write;
714 p_access->p_sys = (void *)p_sys;
719 vlc_UrlClean( &p_sys->url );
725 /*****************************************************************************
726 * Close: free unused data structures
727 *****************************************************************************/
728 static void Close( vlc_object_t *p_access, access_sys_t *p_sys )
730 msg_Dbg( p_access, "stopping stream" );
731 ftp_StopStream( p_access, p_sys );
733 if( ftp_SendCommand( p_access, p_sys, "QUIT" ) < 0 )
735 msg_Warn( p_access, "cannot quit" );
739 ftp_RecvCommand( p_access, p_sys, NULL, NULL );
742 clearCmdTLS( p_sys );
743 net_Close( p_sys->cmd.fd );
746 vlc_UrlClean( &p_sys->url );
750 static void InClose( vlc_object_t *p_this )
752 Close( p_this, ((access_t *)p_this)->p_sys);
756 static void OutClose( vlc_object_t *p_this )
758 Close( p_this, GET_OUT_SYS(p_this));
763 /*****************************************************************************
764 * Seek: try to go at the right place
765 *****************************************************************************/
766 static int _Seek( vlc_object_t *p_access, access_sys_t *p_sys, uint64_t i_pos )
768 msg_Dbg( p_access, "seeking to %"PRIu64, i_pos );
770 ftp_StopStream( (vlc_object_t *)p_access, p_sys );
771 if( ftp_StartStream( (vlc_object_t *)p_access, p_sys, i_pos ) < 0 )
777 static int Seek( access_t *p_access, uint64_t i_pos )
779 int val = _Seek( (vlc_object_t *)p_access, p_access->p_sys, i_pos );
783 p_access->info.b_eof = false;
784 p_access->info.i_pos = i_pos;
790 static int OutSeek( sout_access_out_t *p_access, off_t i_pos )
792 return _Seek( (vlc_object_t *)p_access, GET_OUT_SYS( p_access ), i_pos);
796 /*****************************************************************************
798 *****************************************************************************/
799 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
801 access_sys_t *p_sys = p_access->p_sys;
803 assert( p_sys->data.fd != -1 );
804 assert( !p_sys->out );
806 if( p_access->info.b_eof )
809 if( p_sys->directory )
811 char *psz_line = net_Gets( p_access, p_sys->data.fd, p_sys->data.p_vs );
814 p_access->info.b_eof = true;
819 snprintf( (char*)p_buffer, i_len, "%s://%s:%d/%s/%s\n",
820 ( p_sys->tlsmode == NONE ) ? "ftp" :
821 ( ( p_sys->tlsmode == IMPLICIT ) ? "ftps" : "ftpes" ),
822 p_sys->url.psz_host, p_sys->url.i_port,
823 p_sys->url.psz_path, psz_line );
825 return strlen( (const char *)p_buffer );
830 int i_read = net_Read( p_access, p_sys->data.fd, p_sys->data.p_vs,
831 p_buffer, i_len, false );
833 p_access->info.b_eof = true;
834 else if( i_read > 0 )
835 p_access->info.i_pos += i_read;
841 /*****************************************************************************
843 *****************************************************************************/
845 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
847 access_sys_t *p_sys = GET_OUT_SYS(p_access);
850 assert( p_sys->data.fd != -1 );
852 while( p_buffer != NULL )
854 block_t *p_next = p_buffer->p_next;;
856 i_write += net_Write( p_access, p_sys->data.fd, p_sys->data.p_vs,
857 p_buffer->p_buffer, p_buffer->i_buffer );
858 block_Release( p_buffer );
867 /*****************************************************************************
869 *****************************************************************************/
870 static int Control( access_t *p_access, int i_query, va_list args )
877 case ACCESS_CAN_SEEK:
878 pb_bool = (bool*)va_arg( args, bool* );
879 *pb_bool = !p_access->p_sys->directory;
881 case ACCESS_CAN_FASTSEEK:
882 pb_bool = (bool*)va_arg( args, bool* );
885 case ACCESS_CAN_PAUSE:
886 pb_bool = (bool*)va_arg( args, bool* );
887 *pb_bool = true; /* FIXME */
889 case ACCESS_CAN_CONTROL_PACE:
890 pb_bool = (bool*)va_arg( args, bool* );
891 *pb_bool = true; /* FIXME */
893 case ACCESS_GET_SIZE:
894 *va_arg( args, uint64_t * ) = p_access->p_sys->size;
897 case ACCESS_GET_PTS_DELAY:
898 pi_64 = (int64_t*)va_arg( args, int64_t * );
899 *pi_64 = INT64_C(1000)
900 * var_InheritInteger( p_access, "network-caching" );
903 case ACCESS_SET_PAUSE_STATE:
904 pb_bool = (bool*)va_arg( args, bool* );
906 return Seek( p_access, p_access->info.i_pos );
916 static int ftp_StartStream( vlc_object_t *p_access, access_sys_t *p_sys,
919 char psz_ipv4[16], *psz_ip = p_sys->sz_epsv_ip;
921 char *psz_arg, *psz_parser;
924 assert( p_sys->data.fd == -1 );
926 if( ( ftp_SendCommand( p_access, p_sys, *psz_ip ? "EPSV" : "PASV" ) < 0 )
927 || ( ftp_RecvCommand( p_access, p_sys, &i_answer, &psz_arg ) != 2 ) )
929 msg_Err( p_access, "cannot set passive mode" );
933 psz_parser = strchr( psz_arg, '(' );
934 if( psz_parser == NULL )
937 msg_Err( p_access, "cannot parse passive mode response" );
943 char psz_fmt[7] = "(|||%u";
944 psz_fmt[1] = psz_fmt[2] = psz_fmt[3] = psz_parser[1];
946 if( sscanf( psz_parser, psz_fmt, &i_port ) < 1 )
949 msg_Err( p_access, "cannot parse passive mode response" );
955 unsigned a1, a2, a3, a4, p1, p2;
957 if( ( sscanf( psz_parser, "(%u,%u,%u,%u,%u,%u", &a1, &a2, &a3, &a4,
958 &p1, &p2 ) < 6 ) || ( a1 > 255 ) || ( a2 > 255 )
959 || ( a3 > 255 ) || ( a4 > 255 ) || ( p1 > 255 ) || ( p2 > 255 ) )
962 msg_Err( p_access, "cannot parse passive mode response" );
966 sprintf( psz_ipv4, "%u.%u.%u.%u", a1, a2, a3, a4 );
968 i_port = (p1 << 8) | p2;
972 msg_Dbg( p_access, "ip:%s port:%d", psz_ip, i_port );
974 if( ftp_SendCommand( p_access, p_sys, "TYPE I" ) < 0 ||
975 ftp_RecvCommand( p_access, p_sys, &i_answer, NULL ) != 2 )
977 msg_Err( p_access, "cannot set binary transfer mode" );
983 if( ftp_SendCommand( p_access, p_sys, "REST %"PRIu64, i_start ) < 0 ||
984 ftp_RecvCommand( p_access, p_sys, &i_answer, NULL ) > 3 )
986 msg_Err( p_access, "cannot set restart offset" );
991 msg_Dbg( p_access, "waiting for data connection..." );
992 p_sys->data.fd = net_ConnectTCP( p_access, psz_ip, i_port );
993 if( p_sys->data.fd < 0 )
995 msg_Err( p_access, "failed to connect with server" );
998 msg_Dbg( p_access, "connection with \"%s:%d\" successful",
1001 if( p_sys->directory )
1003 if( ftp_SendCommand( p_access, p_sys, "NLST" ) < 0 ||
1004 ftp_RecvCommand( p_access, p_sys, NULL, &psz_arg ) > 2 )
1006 msg_Err( p_access, "cannot list directory contents" );
1007 return VLC_EGENERIC;
1013 assert( p_sys->url.psz_path );
1014 if( ftp_SendCommand( p_access, p_sys, "%s %s",
1015 p_sys->out ? "STOR" : "RETR",
1016 p_sys->url.psz_path ) < 0
1017 || ftp_RecvCommand( p_access, p_sys, &i_answer, NULL ) > 2 )
1019 msg_Err( p_access, "cannot retrieve file" );
1020 return VLC_EGENERIC;
1024 if( p_sys->tlsmode != NONE )
1026 /* FIXME: Do Reuse TLS Session */
1027 /* TLS/SSL handshake */
1028 p_sys->data.p_tls = vlc_tls_ClientSessionCreate( p_sys->p_creds,
1029 p_sys->data.fd, p_sys->url.psz_host,
1030 ( p_sys->tlsmode == EXPLICIT ) ? "ftpes-data"
1032 if( p_sys->data.p_tls == NULL )
1034 msg_Err( p_access, "cannot establish FTP/TLS session for data" \
1035 ": server not allowing new session ?" );
1036 return VLC_EGENERIC;
1038 p_sys->data.p_vs = &p_sys->data.p_tls->sock;
1041 shutdown( p_sys->data.fd, p_sys->out ? SHUT_RD : SHUT_WR );
1046 static int ftp_StopStream ( vlc_object_t *p_access, access_sys_t *p_sys )
1048 if( ftp_SendCommand( p_access, p_sys, "ABOR" ) < 0 )
1050 msg_Warn( p_access, "cannot abort file" );
1051 if( p_sys->data.fd > 0 )
1053 if ( p_sys->data.p_tls ) vlc_tls_SessionDelete( p_sys->data.p_tls );
1054 net_Close( p_sys->data.fd );
1056 p_sys->data.fd = -1;
1057 p_sys->data.p_tls = NULL;
1058 p_sys->data.p_vs = NULL;
1059 return VLC_EGENERIC;
1062 if( p_sys->data.fd != -1 )
1064 if ( p_sys->data.p_tls ) vlc_tls_SessionDelete( p_sys->data.p_tls );
1065 net_Close( p_sys->data.fd );
1066 p_sys->data.fd = -1;
1067 p_sys->data.p_tls = NULL;
1068 p_sys->data.p_vs = NULL;
1069 /* Read the final response from RETR/STOR, i.e. 426 or 226 */
1070 ftp_RecvCommand( p_access, p_sys, NULL, NULL );
1072 /* Read the response from ABOR, i.e. 226 or 225 */
1073 ftp_RecvCommand( p_access, p_sys, NULL, NULL );