1 /*****************************************************************************
2 * ftp.c: FTP input module
3 *****************************************************************************
4 * Copyright (C) 2001-2006 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr> - original code
8 * RĂ©mi Denis-Courmont <rem # videolan.org> - EPSV support
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
31 #include <vlc/input.h>
36 /*****************************************************************************
38 *****************************************************************************/
39 static int Open ( vlc_object_t * );
40 static void Close( vlc_object_t * );
42 #define CACHING_TEXT N_("Caching value in ms")
43 #define CACHING_LONGTEXT N_( \
44 "Caching value for FTP streams. This " \
45 "value should be set in milliseconds." )
46 #define USER_TEXT N_("FTP user name")
47 #define USER_LONGTEXT N_("User name that will " \
48 "be used for the connection.")
49 #define PASS_TEXT N_("FTP password")
50 #define PASS_LONGTEXT N_("Password that will be " \
51 "used for the connection.")
52 #define ACCOUNT_TEXT N_("FTP account")
53 #define ACCOUNT_LONGTEXT N_("Account that will be " \
54 "used for the connection.")
57 set_shortname( "FTP" );
58 set_description( _("FTP input") );
59 set_capability( "access2", 0 );
60 set_category( CAT_INPUT );
61 set_subcategory( SUBCAT_INPUT_ACCESS );
62 add_integer( "ftp-caching", 2 * DEFAULT_PTS_DELAY / 1000, NULL,
63 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
64 add_string( "ftp-user", "anonymous", NULL, USER_TEXT, USER_LONGTEXT,
66 add_string( "ftp-pwd", "anonymous@dummy.org", NULL, PASS_TEXT,
67 PASS_LONGTEXT, VLC_FALSE );
68 add_string( "ftp-account", "anonymous", NULL, ACCOUNT_TEXT,
69 ACCOUNT_LONGTEXT, VLC_FALSE );
70 add_shortcut( "ftp" );
71 set_callbacks( Open, Close );
74 /*****************************************************************************
76 *****************************************************************************/
77 static int Read( access_t *, uint8_t *, int );
78 static int Seek( access_t *, int64_t );
79 static int Control( access_t *, int, va_list );
88 char sz_epsv_ip[NI_MAXNUMERICHOST];
91 static int ftp_SendCommand( access_t *, char *, ... );
92 static int ftp_ReadCommand( access_t *, int *, char ** );
93 static int ftp_StartStream( access_t *, int64_t );
94 static int ftp_StopStream ( access_t *);
96 static int Connect( access_t *p_access, access_sys_t *p_sys )
101 /* *** Open a TCP connection with server *** */
102 msg_Dbg( p_access, "waiting for connection..." );
103 p_sys->fd_cmd = fd = net_ConnectTCP( p_access, p_sys->url.psz_host,
107 msg_Err( p_access, "failed to connect with server" );
113 if( ftp_ReadCommand( p_access, &i_answer, NULL ) != 1 )
118 if( i_answer / 100 != 2 )
120 msg_Err( p_access, "connection rejected" );
124 msg_Dbg( p_access, "connection accepted (%d)", i_answer );
126 if( p_sys->url.psz_username && *p_sys->url.psz_username )
127 psz = strdup( p_sys->url.psz_username );
129 psz = var_CreateGetString( p_access, "ftp-user" );
131 if( ftp_SendCommand( p_access, "USER %s", psz ) < 0 ||
132 ftp_ReadCommand( p_access, &i_answer, NULL ) < 0 )
139 switch( i_answer / 100 )
142 msg_Dbg( p_access, "user accepted" );
145 msg_Dbg( p_access, "password needed" );
146 if( p_sys->url.psz_password && *p_sys->url.psz_password )
147 psz = strdup( p_sys->url.psz_password );
149 psz = var_CreateGetString( p_access, "ftp-pwd" );
151 if( ftp_SendCommand( p_access, "PASS %s", psz ) < 0 ||
152 ftp_ReadCommand( p_access, &i_answer, NULL ) < 0 )
159 switch( i_answer / 100 )
162 msg_Dbg( p_access, "password accepted" );
165 msg_Dbg( p_access, "account needed" );
166 psz = var_CreateGetString( p_access, "ftp-account" );
167 if( ftp_SendCommand( p_access, "ACCT %s",
169 ftp_ReadCommand( p_access, &i_answer, NULL ) < 0 )
176 if( i_answer / 100 != 2 )
178 msg_Err( p_access, "account rejected" );
181 msg_Dbg( p_access, "account accepted" );
185 msg_Err( p_access, "password rejected" );
190 msg_Err( p_access, "user rejected" );
197 /****************************************************************************
198 * Open: connect to ftp server and ask for file
199 ****************************************************************************/
200 static int Open( vlc_object_t *p_this )
202 access_t *p_access = (access_t*)p_this;
210 STANDARD_READ_ACCESS_INIT
214 /* *** Parse URL and get server addr/port and path *** */
215 psz = p_access->psz_path;
220 vlc_UrlParse( &p_sys->url, psz, 0 );
222 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
224 msg_Err( p_access, "invalid server name" );
227 if( p_sys->url.i_port <= 0 )
229 p_sys->url.i_port = 21; /* default port */
232 /* FTP URLs are relative to user's default directory (RFC1738)
233 For absolute path use ftp://foo.bar//usr/local/etc/filename */
235 if( *p_sys->url.psz_path == '/' )
236 p_sys->url.psz_path++;
238 if( Connect( p_access, p_sys ) < 0 )
241 /* Extended passive mode */
242 if( ftp_SendCommand( p_access, "EPSV ALL" ) < 0 )
244 msg_Err( p_access, "cannot request extended passive mode" );
248 if( ftp_ReadCommand( p_access, &i_answer, NULL ) == 2 )
250 if( net_GetPeerAddress( p_sys->fd_cmd, p_sys->sz_epsv_ip, NULL ) )
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 net_Close( p_sys->fd_cmd );
262 *p_sys->sz_epsv_ip = '\0';
264 msg_Info( p_access, "FTP Extended passive mode disabled" );
266 if( ( p_sys->fd_cmd = Connect( p_access, p_sys ) ) < 0 )
271 if( ftp_SendCommand( p_access, "TYPE I" ) < 0 ||
272 ftp_ReadCommand( p_access, &i_answer, NULL ) != 2 )
274 msg_Err( p_access, "cannot set binary transfer mode" );
279 if( ftp_SendCommand( p_access, "SIZE %s", p_sys->url.psz_path ) < 0 ||
280 ftp_ReadCommand( p_access, &i_answer, &psz_arg ) != 2 )
282 msg_Err( p_access, "cannot get file size" );
285 p_access->info.i_size = atoll( &psz_arg[4] );
287 msg_Dbg( p_access, "file size: "I64Fd, p_access->info.i_size );
289 /* Start the 'stream' */
290 if( ftp_StartStream( p_access, 0 ) < 0 )
292 msg_Err( p_access, "cannot retrieve file" );
296 /* Update default_pts to a suitable value for ftp access */
297 var_Create( p_access, "ftp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
302 if( p_sys->fd_cmd >= 0 )
303 net_Close( p_sys->fd_cmd );
304 vlc_UrlClean( &p_sys->url );
309 /*****************************************************************************
310 * Close: free unused data structures
311 *****************************************************************************/
312 static void Close( vlc_object_t *p_this )
314 access_t *p_access = (access_t*)p_this;
315 access_sys_t *p_sys = p_access->p_sys;
317 msg_Dbg( p_access, "stopping stream" );
318 ftp_StopStream( p_access );
320 if( ftp_SendCommand( p_access, "QUIT" ) < 0 )
322 msg_Warn( p_access, "cannot quit" );
326 ftp_ReadCommand( p_access, NULL, NULL );
328 net_Close( p_sys->fd_cmd );
331 vlc_UrlClean( &p_sys->url );
335 /*****************************************************************************
336 * Seek: try to go at the right place
337 *****************************************************************************/
338 static int Seek( access_t *p_access, int64_t i_pos )
344 msg_Dbg( p_access, "seeking to "I64Fd, i_pos );
346 ftp_StopStream( p_access );
347 if( ftp_StartStream( p_access, i_pos ) < 0 )
349 p_access->info.b_eof = VLC_TRUE;
353 p_access->info.b_eof = VLC_FALSE;
354 p_access->info.i_pos = i_pos;
359 /*****************************************************************************
361 *****************************************************************************/
362 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
364 access_sys_t *p_sys = p_access->p_sys;
367 if( p_access->info.b_eof )
370 i_read = net_Read( p_access, p_sys->fd_data, NULL, p_buffer, i_len,
373 p_access->info.b_eof = VLC_TRUE;
374 else if( i_read > 0 )
375 p_access->info.i_pos += i_read;
380 /*****************************************************************************
382 *****************************************************************************/
383 static int Control( access_t *p_access, int i_query, va_list args )
393 case ACCESS_CAN_SEEK:
394 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
397 case ACCESS_CAN_FASTSEEK:
398 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
399 *pb_bool = VLC_FALSE;
401 case ACCESS_CAN_PAUSE:
402 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
403 *pb_bool = VLC_TRUE; /* FIXME */
405 case ACCESS_CAN_CONTROL_PACE:
406 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
407 *pb_bool = VLC_TRUE; /* FIXME */
412 pi_int = (int*)va_arg( args, int * );
416 case ACCESS_GET_PTS_DELAY:
417 pi_64 = (int64_t*)va_arg( args, int64_t * );
418 var_Get( p_access, "ftp-caching", &val );
419 *pi_64 = (int64_t)var_GetInteger( p_access, "ftp-caching" ) * I64C(1000);
423 case ACCESS_SET_PAUSE_STATE:
427 case ACCESS_GET_TITLE_INFO:
428 case ACCESS_SET_TITLE:
429 case ACCESS_SET_SEEKPOINT:
430 case ACCESS_SET_PRIVATE_ID_STATE:
434 msg_Warn( p_access, "unimplemented query in control" );
441 /*****************************************************************************
443 *****************************************************************************/
444 static int ftp_SendCommand( access_t *p_access, char *psz_fmt, ... )
446 access_sys_t *p_sys = p_access->p_sys;
450 va_start( args, psz_fmt );
451 vasprintf( &psz_cmd, psz_fmt, args );
454 msg_Dbg( p_access, "ftp_SendCommand:\"%s\"", psz_cmd);
455 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd_cmd, NULL, "%s\r\n",
458 msg_Err( p_access, "failed to send command" );
464 /* TODO support this s**t :
465 RFC 959 allows the client to send certain TELNET strings at any moment,
466 even in the middle of a request:
469 * \377\376x where x is one byte.
470 * \377\375x where x is one byte. The server is obliged to send \377\374x
471 * immediately after reading x.
472 * \377\374x where x is one byte.
473 * \377\373x where x is one byte. The server is obliged to send \377\376x
474 * immediately after reading x.
475 * \377x for any other byte x.
477 These strings are not part of the requests, except in the case \377\377,
478 where the request contains one \377. */
479 static int ftp_ReadCommand( access_t *p_access,
480 int *pi_answer, char **ppsz_answer )
482 access_sys_t *p_sys = p_access->p_sys;
486 psz_line = net_Gets( p_access, p_sys->fd_cmd, NULL );
487 msg_Dbg( p_access, "answer=%s", psz_line );
488 if( psz_line == NULL || strlen( psz_line ) < 3 )
490 msg_Err( p_access, "cannot get answer" );
491 if( psz_line ) free( psz_line );
492 if( pi_answer ) *pi_answer = 500;
493 if( ppsz_answer ) *ppsz_answer = NULL;
497 if( psz_line[3] == '-' ) /* Multiple response */
501 memcpy( end, psz_line, 3 );
506 char *psz_tmp = net_Gets( p_access, p_sys->fd_cmd, NULL );
508 if( psz_tmp == NULL ) /* Error */
511 if( !strncmp( psz_tmp, end, 4 ) )
520 i_answer = atoi( psz_line );
522 if( pi_answer ) *pi_answer = i_answer;
525 *ppsz_answer = psz_line;
531 return( i_answer / 100 );
534 static int ftp_StartStream( access_t *p_access, off_t i_start )
536 access_sys_t *p_sys = p_access->p_sys;
538 char psz_ipv4[16], *psz_ip;
540 char *psz_arg, *psz_parser;
543 psz_ip = p_sys->sz_epsv_ip;
545 if( ( ftp_SendCommand( p_access, *psz_ip ? "EPSV" : "PASV" ) < 0 )
546 || ( ftp_ReadCommand( p_access, &i_answer, &psz_arg ) != 2 ) )
548 msg_Err( p_access, "cannot set passive mode" );
552 psz_parser = strchr( psz_arg, '(' );
553 if( psz_parser == NULL )
556 msg_Err( p_access, "cannot parse passive mode response" );
562 char psz_fmt[7] = "(|||%u";
563 psz_fmt[1] = psz_fmt[2] = psz_fmt[3] = psz_parser[1];
565 if( sscanf( psz_parser, psz_fmt, &i_port ) < 1 )
568 msg_Err( p_access, "cannot parse passive mode response" );
574 unsigned a1, a2, a3, a4, p1, p2;
576 if( ( sscanf( psz_parser, "(%u,%u,%u,%u,%u,%u", &a1, &a2, &a3, &a4,
577 &p1, &p2 ) < 6 ) || ( a1 > 255 ) || ( a2 > 255 )
578 || ( a3 > 255 ) || ( a4 > 255 ) || ( p1 > 255 ) || ( p2 > 255 ) )
581 msg_Err( p_access, "cannot parse passive mode response" );
585 sprintf( psz_ipv4, "%u.%u.%u.%u", a1, a2, a3, a4 );
587 i_port = (p1 << 8) | p2;
591 msg_Dbg( p_access, "ip:%s port:%d", psz_ip, i_port );
593 if( ftp_SendCommand( p_access, "TYPE I" ) < 0 ||
594 ftp_ReadCommand( p_access, &i_answer, NULL ) != 2 )
596 msg_Err( p_access, "cannot set binary transfer mode" );
602 if( ftp_SendCommand( p_access, "REST "I64Fu, i_start ) < 0 ||
603 ftp_ReadCommand( p_access, &i_answer, NULL ) > 3 )
605 msg_Err( p_access, "cannot set restart point" );
610 msg_Dbg( p_access, "waiting for data connection..." );
611 p_sys->fd_data = net_ConnectTCP( p_access, psz_ip, i_port );
612 if( p_sys->fd_data < 0 )
614 msg_Err( p_access, "failed to connect with server" );
617 msg_Dbg( p_access, "connection with \"%s:%d\" successful",
621 if( ftp_SendCommand( p_access, "RETR %s", p_sys->url.psz_path ) < 0 ||
622 ftp_ReadCommand( p_access, &i_answer, NULL ) > 2 )
624 msg_Err( p_access, "cannot retreive file" );
630 static int ftp_StopStream ( access_t *p_access )
632 access_sys_t *p_sys = p_access->p_sys;
636 if( ftp_SendCommand( p_access, "ABOR" ) < 0 )
638 msg_Warn( p_access, "cannot abort file" );
639 if( p_sys->fd_data > 0 )
640 net_Close( p_sys->fd_data );
644 if( p_sys->fd_data > 0 )
646 net_Close( p_sys->fd_data );
648 ftp_ReadCommand( p_access, &i_answer, NULL );
650 ftp_ReadCommand( p_access, &i_answer, NULL );