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_access.h>
34 #include <vlc_interface.h>
36 #include <vlc_network.h>
41 # define IPPORT_FTP 21u
44 /*****************************************************************************
46 *****************************************************************************/
47 static int InOpen ( vlc_object_t * );
48 static void InClose( vlc_object_t * );
49 static int OutOpen ( vlc_object_t * );
50 static void OutClose( vlc_object_t * );
52 #define CACHING_TEXT N_("Caching value in ms")
53 #define CACHING_LONGTEXT N_( \
54 "Caching value for FTP streams. This " \
55 "value should be set in milliseconds." )
56 #define USER_TEXT N_("FTP user name")
57 #define USER_LONGTEXT N_("User name that will " \
58 "be used for the connection.")
59 #define PASS_TEXT N_("FTP password")
60 #define PASS_LONGTEXT N_("Password that will be " \
61 "used for the connection.")
62 #define ACCOUNT_TEXT N_("FTP account")
63 #define ACCOUNT_LONGTEXT N_("Account that will be " \
64 "used for the connection.")
67 set_shortname( "FTP" );
68 set_description( _("FTP input") );
69 set_capability( "access2", 0 );
70 set_category( CAT_INPUT );
71 set_subcategory( SUBCAT_INPUT_ACCESS );
72 add_integer( "ftp-caching", 2 * DEFAULT_PTS_DELAY / 1000, NULL,
73 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
74 add_string( "ftp-user", "anonymous", NULL, USER_TEXT, USER_LONGTEXT,
76 add_string( "ftp-pwd", "anonymous@example.com", NULL, PASS_TEXT,
77 PASS_LONGTEXT, VLC_FALSE );
78 add_string( "ftp-account", "anonymous", NULL, ACCOUNT_TEXT,
79 ACCOUNT_LONGTEXT, VLC_FALSE );
80 add_shortcut( "ftp" );
81 set_callbacks( InOpen, InClose );
84 set_shortname( "FTP" );
85 set_description( _("FTP upload output") );
86 set_capability( "sout access", 0 );
87 set_category( CAT_SOUT );
88 set_subcategory( SUBCAT_SOUT_ACO );
89 set_callbacks( OutOpen, OutClose );
92 /*****************************************************************************
94 *****************************************************************************/
95 static ssize_t Read( access_t *, uint8_t *, size_t );
96 static ssize_t Write( sout_access_out_t *, block_t * );
97 static int Seek( access_t *, int64_t );
98 static int OutSeek( sout_access_out_t *, int64_t );
99 static int Control( access_t *, int, va_list );
108 char sz_epsv_ip[NI_MAXNUMERICHOST];
110 #define GET_OUT_SYS( p_this ) \
111 ((access_sys_t *)(((sout_access_out_t *)(p_this))->p_sys))
113 static int ftp_SendCommand( vlc_object_t *, access_sys_t *, const char *, ... );
114 static int ftp_ReadCommand( vlc_object_t *, access_sys_t *, int *, char ** );
115 static int ftp_StartStream( vlc_object_t *, access_sys_t *, int64_t );
116 static int ftp_StopStream ( vlc_object_t *, access_sys_t * );
118 static int Login( vlc_object_t *p_access, access_sys_t *p_sys )
123 /* *** Open a TCP connection with server *** */
124 int fd = p_sys->fd_cmd = net_ConnectTCP( p_access, p_sys->url.psz_host,
128 msg_Err( p_access, "connection failed" );
129 intf_UserFatal( p_access, VLC_FALSE, _("Network interaction failed"),
130 _("VLC could not connect with the given server.") );
134 while( ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) == 1 );
136 if( i_answer / 100 != 2 )
138 msg_Err( p_access, "connection rejected" );
139 intf_UserFatal( p_access, VLC_FALSE, _("Network interaction failed"),
140 _("VLC's connection to the given server was rejected.") );
144 msg_Dbg( p_access, "connection accepted (%d)", i_answer );
146 if( p_sys->url.psz_username && *p_sys->url.psz_username )
147 psz = strdup( p_sys->url.psz_username );
149 psz = var_CreateGetString( p_access, "ftp-user" );
151 if( ftp_SendCommand( p_access, p_sys, "USER %s", psz ) < 0 ||
152 ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) < 0 )
159 switch( i_answer / 100 )
162 msg_Dbg( p_access, "user accepted" );
165 msg_Dbg( p_access, "password needed" );
166 if( p_sys->url.psz_password && *p_sys->url.psz_password )
167 psz = strdup( p_sys->url.psz_password );
169 psz = var_CreateGetString( p_access, "ftp-pwd" );
171 if( ftp_SendCommand( p_access, p_sys, "PASS %s", psz ) < 0 ||
172 ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) < 0 )
179 switch( i_answer / 100 )
182 msg_Dbg( p_access, "password accepted" );
185 msg_Dbg( p_access, "account needed" );
186 psz = var_CreateGetString( p_access, "ftp-account" );
187 if( ftp_SendCommand( p_access, p_sys, "ACCT %s",
189 ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) < 0 )
196 if( i_answer / 100 != 2 )
198 msg_Err( p_access, "account rejected" );
199 intf_UserFatal( p_access, VLC_FALSE,
200 _("Network interaction failed"),
201 _("Your account was rejected.") );
204 msg_Dbg( p_access, "account accepted" );
208 msg_Err( p_access, "password rejected" );
209 intf_UserFatal( p_access, VLC_FALSE,
210 _("Network interaction failed"),
211 _("Your password was rejected.") );
216 msg_Err( p_access, "user rejected" );
217 intf_UserFatal( p_access, VLC_FALSE,
218 _("Network interaction failed"),
219 _("Your connection attempt to the server was rejected.") );
226 static int Connect( vlc_object_t *p_access, access_sys_t *p_sys )
228 if( Login( p_access, p_sys ) < 0 )
231 /* Extended passive mode */
232 if( ftp_SendCommand( p_access, p_sys, "EPSV ALL" ) < 0 )
234 msg_Err( p_access, "cannot request extended passive mode" );
235 net_Close( p_sys->fd_cmd );
239 if( ftp_ReadCommand( p_access, p_sys, NULL, NULL ) == 2 )
241 if( net_GetPeerAddress( p_sys->fd_cmd, p_sys->sz_epsv_ip, NULL ) )
243 net_Close( p_sys->fd_cmd );
249 /* If ESPV ALL fails, we fallback to PASV.
250 * We have to restart the connection in case there is a NAT that
251 * understands EPSV ALL in the way, and hence won't allow PASV on
252 * the initial connection.
254 msg_Info( p_access, "FTP Extended passive mode disabled" );
255 net_Close( p_sys->fd_cmd );
257 if( Login( p_access, p_sys ) )
259 net_Close( p_sys->fd_cmd );
264 /* check binary mode support */
265 if( ftp_SendCommand( p_access, p_sys, "TYPE I" ) < 0 ||
266 ftp_ReadCommand( p_access, p_sys, NULL, NULL ) != 2 )
268 msg_Err( p_access, "cannot set binary transfer mode" );
269 net_Close( p_sys->fd_cmd );
277 static int parseURL( vlc_url_t *url, const char *path )
282 /* *** Parse URL and get server addr/port and path *** */
283 while( *path == '/' )
286 vlc_UrlParse( url, path, 0 );
288 if( url->psz_host == NULL || *url->psz_host == '\0' )
291 if( url->i_port <= 0 )
292 url->i_port = IPPORT_FTP; /* default port */
294 /* FTP URLs are relative to user's default directory (RFC1738)
295 For absolute path use ftp://foo.bar//usr/local/etc/filename */
297 if( *url->psz_path == '/' )
304 /****************************************************************************
305 * Open: connect to ftp server and ask for file
306 ****************************************************************************/
307 static int InOpen( vlc_object_t *p_this )
309 access_t *p_access = (access_t*)p_this;
314 STANDARD_READ_ACCESS_INIT
317 if( parseURL( &p_sys->url, p_access->psz_path ) )
320 if( Connect( p_this, p_sys ) )
324 if( ftp_SendCommand( p_this, p_sys, "SIZE %s", p_sys->url.psz_path ) < 0 ||
325 ftp_ReadCommand( p_this, p_sys, NULL, &psz_arg ) != 2 )
327 msg_Err( p_access, "cannot get file size" );
328 net_Close( p_sys->fd_cmd );
331 p_access->info.i_size = atoll( &psz_arg[4] );
333 msg_Dbg( p_access, "file size: "I64Fd, p_access->info.i_size );
335 /* Start the 'stream' */
336 if( ftp_StartStream( p_this, p_sys, 0 ) < 0 )
338 msg_Err( p_access, "cannot retrieve file" );
339 net_Close( p_sys->fd_cmd );
343 /* Update default_pts to a suitable value for ftp access */
344 var_Create( p_access, "ftp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
349 vlc_UrlClean( &p_sys->url );
354 static int OutOpen( vlc_object_t *p_this )
356 sout_access_out_t *p_access = (sout_access_out_t *)p_this;
359 p_sys = malloc( sizeof( *p_sys ) );
362 memset( p_sys, 0, sizeof( *p_sys ) );
367 if( parseURL( &p_sys->url, p_access->psz_path ) )
370 if( Connect( p_this, p_sys ) )
373 /* Start the 'stream' */
374 if( ftp_StartStream( p_this, p_sys, 0 ) < 0 )
376 msg_Err( p_access, "cannot store file" );
377 net_Close( p_sys->fd_cmd );
381 p_access->pf_seek = OutSeek;
382 p_access->pf_write = Write;
383 p_access->p_sys = (void *)p_sys;
388 vlc_UrlClean( &p_sys->url );
393 /*****************************************************************************
394 * Close: free unused data structures
395 *****************************************************************************/
396 static void Close( vlc_object_t *p_access, access_sys_t *p_sys )
398 msg_Dbg( p_access, "stopping stream" );
399 ftp_StopStream( p_access, p_sys );
401 if( ftp_SendCommand( p_access, p_sys, "QUIT" ) < 0 )
403 msg_Warn( p_access, "cannot quit" );
407 ftp_ReadCommand( p_access, p_sys, NULL, NULL );
409 net_Close( p_sys->fd_cmd );
412 vlc_UrlClean( &p_sys->url );
416 static void InClose( vlc_object_t *p_this )
418 Close( p_this, ((access_t *)p_this)->p_sys);
421 static void OutClose( vlc_object_t *p_this )
423 Close( p_this, GET_OUT_SYS(p_this));
427 /*****************************************************************************
428 * Seek: try to go at the right place
429 *****************************************************************************/
430 static int _Seek( vlc_object_t *p_access, access_sys_t *p_sys, int64_t i_pos )
435 msg_Dbg( p_access, "seeking to "I64Fd, i_pos );
437 ftp_StopStream( (vlc_object_t *)p_access, p_sys );
438 if( ftp_StartStream( (vlc_object_t *)p_access, p_sys, i_pos ) < 0 )
444 static int Seek( access_t *p_access, int64_t i_pos )
446 int val = _Seek( (vlc_object_t *)p_access, p_access->p_sys, i_pos );
450 p_access->info.b_eof = VLC_FALSE;
451 p_access->info.i_pos = i_pos;
456 static int OutSeek( sout_access_out_t *p_access, off_t i_pos )
458 return _Seek( (vlc_object_t *)p_access, GET_OUT_SYS( p_access ), i_pos);
461 /*****************************************************************************
463 *****************************************************************************/
464 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
466 access_sys_t *p_sys = p_access->p_sys;
469 assert( p_sys->fd_data != -1 );
470 assert( p_access->i_object_type == VLC_OBJECT_ACCESS );
472 if( p_access->info.b_eof )
475 i_read = net_Read( p_access, p_sys->fd_data, NULL, p_buffer, i_len,
478 p_access->info.b_eof = VLC_TRUE;
479 else if( i_read > 0 )
480 p_access->info.i_pos += i_read;
485 /*****************************************************************************
487 *****************************************************************************/
488 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
490 access_sys_t *p_sys = GET_OUT_SYS(p_access);
493 assert( p_sys->fd_data != -1 );
495 while( p_buffer != NULL )
497 block_t *p_next = p_buffer->p_next;;
499 i_write += net_Write( p_access, p_sys->fd_data, NULL,
500 p_buffer->p_buffer, p_buffer->i_buffer );
501 block_Release( p_buffer );
509 /*****************************************************************************
511 *****************************************************************************/
512 static int Control( access_t *p_access, int i_query, va_list args )
522 case ACCESS_CAN_SEEK:
523 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
526 case ACCESS_CAN_FASTSEEK:
527 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
528 *pb_bool = VLC_FALSE;
530 case ACCESS_CAN_PAUSE:
531 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
532 *pb_bool = VLC_TRUE; /* FIXME */
534 case ACCESS_CAN_CONTROL_PACE:
535 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
536 *pb_bool = VLC_TRUE; /* FIXME */
541 pi_int = (int*)va_arg( args, int * );
545 case ACCESS_GET_PTS_DELAY:
546 pi_64 = (int64_t*)va_arg( args, int64_t * );
547 var_Get( p_access, "ftp-caching", &val );
548 *pi_64 = (int64_t)var_GetInteger( p_access, "ftp-caching" ) * I64C(1000);
552 case ACCESS_SET_PAUSE_STATE:
553 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
555 return Seek( p_access, p_access->info.i_pos );
558 case ACCESS_GET_TITLE_INFO:
559 case ACCESS_SET_TITLE:
560 case ACCESS_SET_SEEKPOINT:
561 case ACCESS_SET_PRIVATE_ID_STATE:
562 case ACCESS_GET_CONTENT_TYPE:
566 msg_Warn( p_access, "unimplemented query in control" );
573 /*****************************************************************************
575 *****************************************************************************/
576 static int ftp_SendCommand( vlc_object_t *p_access, access_sys_t *p_sys,
577 const char *psz_fmt, ... )
582 va_start( args, psz_fmt );
583 vasprintf( &psz_cmd, psz_fmt, args );
586 msg_Dbg( p_access, "ftp_SendCommand:\"%s\"", psz_cmd);
587 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd_cmd, NULL, "%s\r\n",
590 msg_Err( p_access, "failed to send command" );
596 /* TODO support this s**t :
597 RFC 959 allows the client to send certain TELNET strings at any moment,
598 even in the middle of a request:
601 * \377\376x where x is one byte.
602 * \377\375x where x is one byte. The server is obliged to send \377\374x
603 * immediately after reading x.
604 * \377\374x where x is one byte.
605 * \377\373x where x is one byte. The server is obliged to send \377\376x
606 * immediately after reading x.
607 * \377x for any other byte x.
609 These strings are not part of the requests, except in the case \377\377,
610 where the request contains one \377. */
611 static int ftp_ReadCommand( vlc_object_t *p_access, access_sys_t *p_sys,
612 int *pi_answer, char **ppsz_answer )
617 psz_line = net_Gets( p_access, p_sys->fd_cmd, NULL );
618 if( psz_line == NULL || strlen( psz_line ) < 3 )
620 msg_Err( p_access, "cannot get answer" );
621 if( psz_line ) free( psz_line );
622 if( pi_answer ) *pi_answer = 500;
623 if( ppsz_answer ) *ppsz_answer = NULL;
626 msg_Dbg( p_access, "answer=%s", psz_line );
628 if( psz_line[3] == '-' ) /* Multiple response */
632 memcpy( end, psz_line, 3 );
637 char *psz_tmp = net_Gets( p_access, p_sys->fd_cmd, NULL );
639 if( psz_tmp == NULL ) /* Error */
642 if( !strncmp( psz_tmp, end, 4 ) )
651 i_answer = atoi( psz_line );
653 if( pi_answer ) *pi_answer = i_answer;
656 *ppsz_answer = psz_line;
662 return( i_answer / 100 );
665 static int ftp_StartStream( vlc_object_t *p_access, access_sys_t *p_sys,
668 char psz_ipv4[16], *psz_ip = p_sys->sz_epsv_ip;
670 char *psz_arg, *psz_parser;
673 assert( p_sys->fd_data == -1 );
675 if( ( ftp_SendCommand( p_access, p_sys, *psz_ip ? "EPSV" : "PASV" ) < 0 )
676 || ( ftp_ReadCommand( p_access, p_sys, &i_answer, &psz_arg ) != 2 ) )
678 msg_Err( p_access, "cannot set passive mode" );
682 psz_parser = strchr( psz_arg, '(' );
683 if( psz_parser == NULL )
686 msg_Err( p_access, "cannot parse passive mode response" );
692 char psz_fmt[7] = "(|||%u";
693 psz_fmt[1] = psz_fmt[2] = psz_fmt[3] = psz_parser[1];
695 if( sscanf( psz_parser, psz_fmt, &i_port ) < 1 )
698 msg_Err( p_access, "cannot parse passive mode response" );
704 unsigned a1, a2, a3, a4, p1, p2;
706 if( ( sscanf( psz_parser, "(%u,%u,%u,%u,%u,%u", &a1, &a2, &a3, &a4,
707 &p1, &p2 ) < 6 ) || ( a1 > 255 ) || ( a2 > 255 )
708 || ( a3 > 255 ) || ( a4 > 255 ) || ( p1 > 255 ) || ( p2 > 255 ) )
711 msg_Err( p_access, "cannot parse passive mode response" );
715 sprintf( psz_ipv4, "%u.%u.%u.%u", a1, a2, a3, a4 );
717 i_port = (p1 << 8) | p2;
721 msg_Dbg( p_access, "ip:%s port:%d", psz_ip, i_port );
723 if( ftp_SendCommand( p_access, p_sys, "TYPE I" ) < 0 ||
724 ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) != 2 )
726 msg_Err( p_access, "cannot set binary transfer mode" );
732 if( ftp_SendCommand( p_access, p_sys, "REST "I64Fu, i_start ) < 0 ||
733 ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) > 3 )
735 msg_Err( p_access, "cannot set restart offset" );
740 msg_Dbg( p_access, "waiting for data connection..." );
741 p_sys->fd_data = net_ConnectTCP( p_access, psz_ip, i_port );
742 if( p_sys->fd_data < 0 )
744 msg_Err( p_access, "failed to connect with server" );
747 msg_Dbg( p_access, "connection with \"%s:%d\" successful",
751 if( ftp_SendCommand( p_access, p_sys, "%s %s",
752 (p_access->i_object_type == VLC_OBJECT_ACCESS)
754 p_sys->url.psz_path ) < 0 ||
755 ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) > 2 )
757 msg_Err( p_access, "cannot retrieve file" );
761 shutdown( p_sys->fd_data,
762 ( p_access->i_object_type == VLC_OBJECT_ACCESS ) );
767 static int ftp_StopStream ( vlc_object_t *p_access, access_sys_t *p_sys )
769 if( ftp_SendCommand( p_access, p_sys, "ABOR" ) < 0 )
771 msg_Warn( p_access, "cannot abort file" );
772 if( p_sys->fd_data > 0 )
773 net_Close( p_sys->fd_data );
778 if( p_sys->fd_data != -1 )
780 net_Close( p_sys->fd_data );
782 ftp_ReadCommand( p_access, p_sys, NULL, NULL );
784 ftp_ReadCommand( p_access, p_sys, NULL, NULL );