1 /*****************************************************************************
2 * ftp.c: FTP input module
3 *****************************************************************************
4 * Copyright (C) 2001-2006 the VideoLAN team
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
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.
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.
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 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
38 #include <vlc_access.h>
39 #include <vlc_interface.h>
41 #include <vlc_network.h>
46 # define IPPORT_FTP 21u
49 /*****************************************************************************
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 * );
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.")
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,
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 );
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 set_callbacks( OutOpen, OutClose );
97 /*****************************************************************************
99 *****************************************************************************/
100 static ssize_t Read( access_t *, uint8_t *, size_t );
101 static ssize_t Write( sout_access_out_t *, block_t * );
102 static int Seek( access_t *, int64_t );
103 static int OutSeek( sout_access_out_t *, off_t );
104 static int Control( access_t *, int, va_list );
113 char sz_epsv_ip[NI_MAXNUMERICHOST];
116 #define GET_OUT_SYS( p_this ) \
117 ((access_sys_t *)(((sout_access_out_t *)(p_this))->p_sys))
119 static int ftp_SendCommand( vlc_object_t *, access_sys_t *, const char *, ... );
120 static int ftp_ReadCommand( vlc_object_t *, access_sys_t *, int *, char ** );
121 static int ftp_StartStream( vlc_object_t *, access_sys_t *, int64_t );
122 static int ftp_StopStream ( vlc_object_t *, access_sys_t * );
124 static int Login( vlc_object_t *p_access, access_sys_t *p_sys )
129 /* *** Open a TCP connection with server *** */
130 int fd = p_sys->fd_cmd = net_ConnectTCP( p_access, p_sys->url.psz_host,
134 msg_Err( p_access, "connection failed" );
135 intf_UserFatal( p_access, false, _("Network interaction failed"),
136 _("VLC could not connect with the given server.") );
140 while( ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) == 1 );
142 if( i_answer / 100 != 2 )
144 msg_Err( p_access, "connection rejected" );
145 intf_UserFatal( p_access, false, _("Network interaction failed"),
146 _("VLC's connection to the given server was rejected.") );
150 msg_Dbg( p_access, "connection accepted (%d)", i_answer );
152 if( p_sys->url.psz_username && *p_sys->url.psz_username )
153 psz = strdup( p_sys->url.psz_username );
155 psz = var_CreateGetString( p_access, "ftp-user" );
157 if( ftp_SendCommand( p_access, p_sys, "USER %s", psz ) < 0 ||
158 ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) < 0 )
165 switch( i_answer / 100 )
168 msg_Dbg( p_access, "user accepted" );
171 msg_Dbg( p_access, "password needed" );
172 if( p_sys->url.psz_password && *p_sys->url.psz_password )
173 psz = strdup( p_sys->url.psz_password );
175 psz = var_CreateGetString( p_access, "ftp-pwd" );
177 if( ftp_SendCommand( p_access, p_sys, "PASS %s", psz ) < 0 ||
178 ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) < 0 )
185 switch( i_answer / 100 )
188 msg_Dbg( p_access, "password accepted" );
191 msg_Dbg( p_access, "account needed" );
192 psz = var_CreateGetString( p_access, "ftp-account" );
193 if( ftp_SendCommand( p_access, p_sys, "ACCT %s",
195 ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) < 0 )
202 if( i_answer / 100 != 2 )
204 msg_Err( p_access, "account rejected" );
205 intf_UserFatal( p_access, false,
206 _("Network interaction failed"),
207 _("Your account was rejected.") );
210 msg_Dbg( p_access, "account accepted" );
214 msg_Err( p_access, "password rejected" );
215 intf_UserFatal( p_access, false,
216 _("Network interaction failed"),
217 _("Your password was rejected.") );
222 msg_Err( p_access, "user rejected" );
223 intf_UserFatal( p_access, false,
224 _("Network interaction failed"),
225 _("Your connection attempt to the server was rejected.") );
232 static int Connect( vlc_object_t *p_access, access_sys_t *p_sys )
234 if( Login( p_access, p_sys ) < 0 )
237 /* Extended passive mode */
238 if( ftp_SendCommand( p_access, p_sys, "EPSV ALL" ) < 0 )
240 msg_Err( p_access, "cannot request extended passive mode" );
241 net_Close( p_sys->fd_cmd );
245 if( ftp_ReadCommand( p_access, p_sys, NULL, NULL ) == 2 )
247 if( net_GetPeerAddress( p_sys->fd_cmd, p_sys->sz_epsv_ip, NULL ) )
249 net_Close( p_sys->fd_cmd );
255 /* If ESPV ALL fails, we fallback to PASV.
256 * We have to restart the connection in case there is a NAT that
257 * understands EPSV ALL in the way, and hence won't allow PASV on
258 * the initial connection.
260 msg_Info( p_access, "FTP Extended passive mode disabled" );
261 net_Close( p_sys->fd_cmd );
263 if( Login( p_access, p_sys ) )
265 net_Close( p_sys->fd_cmd );
270 /* check binary mode support */
271 if( ftp_SendCommand( p_access, p_sys, "TYPE I" ) < 0 ||
272 ftp_ReadCommand( p_access, p_sys, NULL, NULL ) != 2 )
274 msg_Err( p_access, "cannot set binary transfer mode" );
275 net_Close( p_sys->fd_cmd );
283 static int parseURL( vlc_url_t *url, const char *path )
288 /* *** Parse URL and get server addr/port and path *** */
289 while( *path == '/' )
292 vlc_UrlParse( url, path, 0 );
294 if( url->psz_host == NULL || *url->psz_host == '\0' )
297 if( url->i_port <= 0 )
298 url->i_port = IPPORT_FTP; /* default port */
300 /* FTP URLs are relative to user's default directory (RFC1738)
301 For absolute path use ftp://foo.bar//usr/local/etc/filename */
303 if( *url->psz_path == '/' )
310 /****************************************************************************
311 * Open: connect to ftp server and ask for file
312 ****************************************************************************/
313 static int InOpen( vlc_object_t *p_this )
315 access_t *p_access = (access_t*)p_this;
320 STANDARD_READ_ACCESS_INIT
324 if( parseURL( &p_sys->url, p_access->psz_path ) )
327 if( Connect( p_this, p_sys ) )
331 if( ftp_SendCommand( p_this, p_sys, "SIZE %s", p_sys->url.psz_path ) < 0 ||
332 ftp_ReadCommand( p_this, p_sys, NULL, &psz_arg ) != 2 )
334 msg_Err( p_access, "cannot get file size" );
335 net_Close( p_sys->fd_cmd );
338 p_access->info.i_size = atoll( &psz_arg[4] );
340 msg_Dbg( p_access, "file size: %"PRId64, p_access->info.i_size );
342 /* Start the 'stream' */
343 if( ftp_StartStream( p_this, p_sys, 0 ) < 0 )
345 msg_Err( p_access, "cannot retrieve file" );
346 net_Close( p_sys->fd_cmd );
350 /* Update default_pts to a suitable value for ftp access */
351 var_Create( p_access, "ftp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
356 vlc_UrlClean( &p_sys->url );
361 static int OutOpen( vlc_object_t *p_this )
363 sout_access_out_t *p_access = (sout_access_out_t *)p_this;
366 p_sys = malloc( sizeof( *p_sys ) );
369 memset( p_sys, 0, sizeof( *p_sys ) );
375 if( parseURL( &p_sys->url, p_access->psz_path ) )
378 if( Connect( p_this, p_sys ) )
381 /* Start the 'stream' */
382 if( ftp_StartStream( p_this, p_sys, 0 ) < 0 )
384 msg_Err( p_access, "cannot store file" );
385 net_Close( p_sys->fd_cmd );
389 p_access->pf_seek = OutSeek;
390 p_access->pf_write = Write;
391 p_access->p_sys = (void *)p_sys;
396 vlc_UrlClean( &p_sys->url );
401 /*****************************************************************************
402 * Close: free unused data structures
403 *****************************************************************************/
404 static void Close( vlc_object_t *p_access, access_sys_t *p_sys )
406 msg_Dbg( p_access, "stopping stream" );
407 ftp_StopStream( p_access, p_sys );
409 if( ftp_SendCommand( p_access, p_sys, "QUIT" ) < 0 )
411 msg_Warn( p_access, "cannot quit" );
415 ftp_ReadCommand( p_access, p_sys, NULL, NULL );
417 net_Close( p_sys->fd_cmd );
420 vlc_UrlClean( &p_sys->url );
424 static void InClose( vlc_object_t *p_this )
426 Close( p_this, ((access_t *)p_this)->p_sys);
429 static void OutClose( vlc_object_t *p_this )
431 Close( p_this, GET_OUT_SYS(p_this));
435 /*****************************************************************************
436 * Seek: try to go at the right place
437 *****************************************************************************/
438 static int _Seek( vlc_object_t *p_access, access_sys_t *p_sys, int64_t i_pos )
443 msg_Dbg( p_access, "seeking to %"PRId64, i_pos );
445 ftp_StopStream( (vlc_object_t *)p_access, p_sys );
446 if( ftp_StartStream( (vlc_object_t *)p_access, p_sys, i_pos ) < 0 )
452 static int Seek( access_t *p_access, int64_t i_pos )
454 int val = _Seek( (vlc_object_t *)p_access, p_access->p_sys, i_pos );
458 p_access->info.b_eof = false;
459 p_access->info.i_pos = i_pos;
464 static int OutSeek( sout_access_out_t *p_access, off_t i_pos )
466 return _Seek( (vlc_object_t *)p_access, GET_OUT_SYS( p_access ), i_pos);
469 /*****************************************************************************
471 *****************************************************************************/
472 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
474 access_sys_t *p_sys = p_access->p_sys;
477 assert( p_sys->fd_data != -1 );
478 assert( !p_sys->out );
480 if( p_access->info.b_eof )
483 i_read = net_Read( p_access, p_sys->fd_data, NULL, p_buffer, i_len,
486 p_access->info.b_eof = true;
487 else if( i_read > 0 )
488 p_access->info.i_pos += i_read;
493 /*****************************************************************************
495 *****************************************************************************/
496 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
498 access_sys_t *p_sys = GET_OUT_SYS(p_access);
501 assert( p_sys->fd_data != -1 );
503 while( p_buffer != NULL )
505 block_t *p_next = p_buffer->p_next;;
507 i_write += net_Write( p_access, p_sys->fd_data, NULL,
508 p_buffer->p_buffer, p_buffer->i_buffer );
509 block_Release( p_buffer );
517 /*****************************************************************************
519 *****************************************************************************/
520 static int Control( access_t *p_access, int i_query, va_list args )
530 case ACCESS_CAN_SEEK:
531 pb_bool = (bool*)va_arg( args, bool* );
534 case ACCESS_CAN_FASTSEEK:
535 pb_bool = (bool*)va_arg( args, bool* );
538 case ACCESS_CAN_PAUSE:
539 pb_bool = (bool*)va_arg( args, bool* );
540 *pb_bool = true; /* FIXME */
542 case ACCESS_CAN_CONTROL_PACE:
543 pb_bool = (bool*)va_arg( args, bool* );
544 *pb_bool = true; /* FIXME */
549 pi_int = (int*)va_arg( args, int * );
553 case ACCESS_GET_PTS_DELAY:
554 pi_64 = (int64_t*)va_arg( args, int64_t * );
555 var_Get( p_access, "ftp-caching", &val );
556 *pi_64 = (int64_t)var_GetInteger( p_access, "ftp-caching" ) * INT64_C(1000);
560 case ACCESS_SET_PAUSE_STATE:
561 pb_bool = (bool*)va_arg( args, bool* );
563 return Seek( p_access, p_access->info.i_pos );
566 case ACCESS_GET_TITLE_INFO:
567 case ACCESS_SET_TITLE:
568 case ACCESS_SET_SEEKPOINT:
569 case ACCESS_SET_PRIVATE_ID_STATE:
570 case ACCESS_GET_CONTENT_TYPE:
571 case ACCESS_GET_META:
575 msg_Warn( p_access, "unimplemented query in control: %d", i_query);
582 /*****************************************************************************
584 *****************************************************************************/
585 static int ftp_SendCommand( vlc_object_t *p_access, access_sys_t *p_sys,
586 const char *psz_fmt, ... )
591 va_start( args, psz_fmt );
592 vasprintf( &psz_cmd, psz_fmt, args );
595 msg_Dbg( p_access, "ftp_SendCommand:\"%s\"", psz_cmd);
596 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd_cmd, NULL, "%s\r\n",
599 msg_Err( p_access, "failed to send command" );
605 /* TODO support this s**t :
606 RFC 959 allows the client to send certain TELNET strings at any moment,
607 even in the middle of a request:
610 * \377\376x where x is one byte.
611 * \377\375x where x is one byte. The server is obliged to send \377\374x
612 * immediately after reading x.
613 * \377\374x where x is one byte.
614 * \377\373x where x is one byte. The server is obliged to send \377\376x
615 * immediately after reading x.
616 * \377x for any other byte x.
618 These strings are not part of the requests, except in the case \377\377,
619 where the request contains one \377. */
620 static int ftp_ReadCommand( vlc_object_t *p_access, access_sys_t *p_sys,
621 int *pi_answer, char **ppsz_answer )
626 psz_line = net_Gets( p_access, p_sys->fd_cmd, NULL );
627 if( psz_line == NULL || strlen( psz_line ) < 3 )
629 msg_Err( p_access, "cannot get answer" );
631 if( pi_answer ) *pi_answer = 500;
632 if( ppsz_answer ) *ppsz_answer = NULL;
635 msg_Dbg( p_access, "answer=%s", psz_line );
637 if( psz_line[3] == '-' ) /* Multiple response */
641 memcpy( end, psz_line, 3 );
646 char *psz_tmp = net_Gets( p_access, p_sys->fd_cmd, NULL );
648 if( psz_tmp == NULL ) /* Error */
651 if( !strncmp( psz_tmp, end, 4 ) )
660 i_answer = atoi( psz_line );
662 if( pi_answer ) *pi_answer = i_answer;
665 *ppsz_answer = psz_line;
671 return( i_answer / 100 );
674 static int ftp_StartStream( vlc_object_t *p_access, access_sys_t *p_sys,
677 char psz_ipv4[16], *psz_ip = p_sys->sz_epsv_ip;
679 char *psz_arg, *psz_parser;
682 assert( p_sys->fd_data == -1 );
684 if( ( ftp_SendCommand( p_access, p_sys, *psz_ip ? "EPSV" : "PASV" ) < 0 )
685 || ( ftp_ReadCommand( p_access, p_sys, &i_answer, &psz_arg ) != 2 ) )
687 msg_Err( p_access, "cannot set passive mode" );
691 psz_parser = strchr( psz_arg, '(' );
692 if( psz_parser == NULL )
695 msg_Err( p_access, "cannot parse passive mode response" );
701 char psz_fmt[7] = "(|||%u";
702 psz_fmt[1] = psz_fmt[2] = psz_fmt[3] = psz_parser[1];
704 if( sscanf( psz_parser, psz_fmt, &i_port ) < 1 )
707 msg_Err( p_access, "cannot parse passive mode response" );
713 unsigned a1, a2, a3, a4, p1, p2;
715 if( ( sscanf( psz_parser, "(%u,%u,%u,%u,%u,%u", &a1, &a2, &a3, &a4,
716 &p1, &p2 ) < 6 ) || ( a1 > 255 ) || ( a2 > 255 )
717 || ( a3 > 255 ) || ( a4 > 255 ) || ( p1 > 255 ) || ( p2 > 255 ) )
720 msg_Err( p_access, "cannot parse passive mode response" );
724 sprintf( psz_ipv4, "%u.%u.%u.%u", a1, a2, a3, a4 );
726 i_port = (p1 << 8) | p2;
730 msg_Dbg( p_access, "ip:%s port:%d", psz_ip, i_port );
732 if( ftp_SendCommand( p_access, p_sys, "TYPE I" ) < 0 ||
733 ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) != 2 )
735 msg_Err( p_access, "cannot set binary transfer mode" );
741 if( ftp_SendCommand( p_access, p_sys, "REST %"PRIu64, i_start ) < 0 ||
742 ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) > 3 )
744 msg_Err( p_access, "cannot set restart offset" );
749 msg_Dbg( p_access, "waiting for data connection..." );
750 p_sys->fd_data = net_ConnectTCP( p_access, psz_ip, i_port );
751 if( p_sys->fd_data < 0 )
753 msg_Err( p_access, "failed to connect with server" );
756 msg_Dbg( p_access, "connection with \"%s:%d\" successful",
760 if( ftp_SendCommand( p_access, p_sys, "%s %s",
761 p_sys->out ? "STOR" : "RETR",
762 p_sys->url.psz_path ) < 0 ||
763 ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) > 2 )
765 msg_Err( p_access, "cannot retrieve file" );
769 shutdown( p_sys->fd_data, p_sys->out ? SHUT_RD : SHUT_WR );
774 static int ftp_StopStream ( vlc_object_t *p_access, access_sys_t *p_sys )
776 if( ftp_SendCommand( p_access, p_sys, "ABOR" ) < 0 )
778 msg_Warn( p_access, "cannot abort file" );
779 if( p_sys->fd_data > 0 )
780 net_Close( p_sys->fd_data );
785 if( p_sys->fd_data != -1 )
788 ftp_ReadCommand( p_access, p_sys, &i_answer, NULL );
789 if ( i_answer != 227 )
790 /* If answer is from the previous command,
791 * rathen that succesful ABOR - read next command */
792 ftp_ReadCommand( p_access, p_sys, NULL, NULL );
794 net_Close( p_sys->fd_data );