1 /*****************************************************************************
2 * ftp.c: FTP input module
3 *****************************************************************************
4 * Copyright (C) 2001-2004 VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
30 #include <vlc/input.h>
34 /*****************************************************************************
36 *****************************************************************************/
37 static int Open ( vlc_object_t * );
38 static void Close( vlc_object_t * );
40 #define CACHING_TEXT N_("Caching value in ms")
41 #define CACHING_LONGTEXT N_( \
42 "Allows you to modify the default caching value for FTP streams. This " \
43 "value should be set in millisecond units." )
44 #define USER_TEXT N_("FTP user name")
45 #define USER_LONGTEXT N_("Allows you to modify the user name that will " \
46 "be used for the connection.")
47 #define PASS_TEXT N_("FTP password")
48 #define PASS_LONGTEXT N_("Allows you to modify the password that will be " \
49 "used for the connection.")
50 #define ACCOUNT_TEXT N_("FTP account")
51 #define ACCOUNT_LONGTEXT N_("Allows you to modify the account that will be " \
52 "used for the connection.")
55 set_description( _("FTP input") );
56 set_capability( "access2", 0 );
57 add_integer( "ftp-caching", 2 * DEFAULT_PTS_DELAY / 1000, NULL,
58 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
59 add_string( "ftp-user", "anonymous", NULL, USER_TEXT, USER_LONGTEXT,
61 add_string( "ftp-pwd", "anonymous@dummy.org", NULL, PASS_TEXT,
62 PASS_LONGTEXT, VLC_FALSE );
63 add_string( "ftp-account", "anonymous", NULL, ACCOUNT_TEXT,
64 ACCOUNT_LONGTEXT, VLC_FALSE );
65 add_shortcut( "ftp" );
66 set_callbacks( Open, Close );
69 /*****************************************************************************
71 *****************************************************************************/
72 static int Read( access_t *, uint8_t *, int );
73 static int Seek( access_t *, int64_t );
74 static int Control( access_t *, int, va_list );
84 static int ftp_SendCommand( access_t *, char *, ... );
85 static int ftp_ReadCommand( access_t *, int *, char ** );
86 static int ftp_StartStream( access_t *, int64_t );
87 static int ftp_StopStream ( access_t *);
89 /****************************************************************************
90 * Open: connect to ftp server and ask for file
91 ****************************************************************************/
92 static int Open( vlc_object_t *p_this )
94 access_t *p_access = (access_t*)p_this;
102 p_access->pf_read = Read;
103 p_access->pf_block = NULL;
104 p_access->pf_seek = Seek;
105 p_access->pf_control = Control;
106 p_access->info.i_update = 0;
107 p_access->info.i_size = 0;
108 p_access->info.i_pos = 0;
109 p_access->info.b_eof = VLC_FALSE;
110 p_access->info.i_title = 0;
111 p_access->info.i_seekpoint = 0;
112 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
113 memset( p_sys, 0, sizeof( access_sys_t ) );
117 /* *** Parse URL and get server addr/port and path *** */
118 psz = p_access->psz_path;
123 vlc_UrlParse( &p_sys->url, psz, 0 );
125 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
127 msg_Err( p_access, "invalid server name" );
130 if( p_sys->url.i_port <= 0 )
132 p_sys->url.i_port = 21; /* default port */
135 /* *** Open a TCP connection with server *** */
136 msg_Dbg( p_access, "waiting for connection..." );
137 p_sys->fd_cmd = net_OpenTCP( p_access, p_sys->url.psz_host,
139 if( p_sys->fd_cmd < 0 )
141 msg_Err( p_access, "failed to connect with server" );
147 if( ftp_ReadCommand( p_access, &i_answer, NULL ) != 1 )
152 if( i_answer / 100 != 2 )
154 msg_Err( p_access, "connection rejected" );
158 msg_Dbg( p_access, "connection accepted (%d)", i_answer );
160 psz = var_CreateGetString( p_access, "ftp-user" );
161 if( ftp_SendCommand( p_access, "USER %s", psz ) < 0 ||
162 ftp_ReadCommand( p_access, &i_answer, NULL ) < 0 )
169 switch( i_answer / 100 )
172 msg_Dbg( p_access, "user accepted" );
175 msg_Dbg( p_access, "password needed" );
176 psz = var_CreateGetString( p_access, "ftp-pwd" );
177 if( ftp_SendCommand( p_access, "PASS %s", psz ) < 0 ||
178 ftp_ReadCommand( p_access, &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, "ACCT %s",
195 ftp_ReadCommand( p_access, &i_answer, NULL ) < 0 )
202 if( i_answer / 100 != 2 )
204 msg_Err( p_access, "account rejected" );
207 msg_Dbg( p_access, "account accepted" );
211 msg_Err( p_access, "password rejected" );
216 msg_Err( p_access, "user rejected" );
221 if( ftp_SendCommand( p_access, "TYPE I" ) < 0 ||
222 ftp_ReadCommand( p_access, &i_answer, NULL ) != 2 )
224 msg_Err( p_access, "cannot set binary transfer mode" );
229 if( ftp_SendCommand( p_access, "SIZE %s", p_sys->url.psz_path ) < 0 ||
230 ftp_ReadCommand( p_access, &i_answer, &psz_arg ) != 2 )
232 msg_Err( p_access, "cannot get file size" );
235 p_access->info.i_size = atoll( &psz_arg[4] );
237 msg_Dbg( p_access, "file size: "I64Fd, p_access->info.i_size );
239 /* Start the 'stream' */
240 if( ftp_StartStream( p_access, 0 ) < 0 )
242 msg_Err( p_access, "cannot retrieve file" );
246 /* Update default_pts to a suitable value for ftp access */
247 var_Create( p_access, "ftp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
252 if( p_sys->fd_cmd > 0 )
254 net_Close( p_sys->fd_cmd );
256 vlc_UrlClean( &p_sys->url );
261 /*****************************************************************************
262 * Close: free unused data structures
263 *****************************************************************************/
264 static void Close( vlc_object_t *p_this )
266 access_t *p_access = (access_t*)p_this;
267 access_sys_t *p_sys = p_access->p_sys;
269 msg_Dbg( p_access, "stopping stream" );
270 ftp_StopStream( p_access );
272 if( ftp_SendCommand( p_access, "QUIT" ) < 0 )
274 msg_Warn( p_access, "cannot quit" );
278 ftp_ReadCommand( p_access, NULL, NULL );
280 net_Close( p_sys->fd_cmd );
283 vlc_UrlClean( &p_sys->url );
287 /*****************************************************************************
288 * Seek: try to go at the right place
289 *****************************************************************************/
290 static int Seek( access_t *p_access, int64_t i_pos )
296 msg_Dbg( p_access, "seeking to "I64Fd, i_pos );
298 ftp_StopStream( p_access );
299 if( ftp_StartStream( p_access, i_pos ) < 0 )
301 p_access->info.b_eof = VLC_TRUE;
305 p_access->info.b_eof = VLC_FALSE;
306 p_access->info.i_pos = i_pos;
311 /*****************************************************************************
313 *****************************************************************************/
314 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
316 access_sys_t *p_sys = p_access->p_sys;
319 if( p_access->info.b_eof )
322 i_read = net_Read( p_access, p_sys->fd_data, NULL, p_buffer, i_len,
325 p_access->info.b_eof = VLC_TRUE;
326 else if( i_read > 0 )
327 p_access->info.i_pos += i_read;
332 /*****************************************************************************
334 *****************************************************************************/
335 static int Control( access_t *p_access, int i_query, va_list args )
345 case ACCESS_CAN_SEEK:
346 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
349 case ACCESS_CAN_FASTSEEK:
350 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
351 *pb_bool = VLC_FALSE;
353 case ACCESS_CAN_PAUSE:
354 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
355 *pb_bool = VLC_TRUE; /* FIXME */
357 case ACCESS_CAN_CONTROL_PACE:
358 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
359 *pb_bool = VLC_TRUE; /* FIXME */
364 pi_int = (int*)va_arg( args, int * );
368 case ACCESS_GET_PTS_DELAY:
369 pi_64 = (int64_t*)va_arg( args, int64_t * );
370 var_Get( p_access, "ftp-caching", &val );
371 *pi_64 = (int64_t)var_GetInteger( p_access, "ftp-caching" ) * I64C(1000);
375 case ACCESS_SET_PAUSE_STATE:
379 case ACCESS_GET_TITLE_INFO:
380 case ACCESS_SET_TITLE:
381 case ACCESS_SET_SEEKPOINT:
382 case ACCESS_SET_PRIVATE_ID_STATE:
386 msg_Warn( p_access, "unimplemented query in control" );
393 /*****************************************************************************
395 *****************************************************************************/
396 static int ftp_SendCommand( access_t *p_access, char *psz_fmt, ... )
398 access_sys_t *p_sys = p_access->p_sys;
403 va_start( args, psz_fmt );
404 vasprintf( &psz_cmd, psz_fmt, args );
407 msg_Dbg( p_access, "ftp_SendCommand:\"%s\"", psz_cmd);
408 if( ( i_ret = net_Printf( VLC_OBJECT(p_access), p_sys->fd_cmd, NULL,
409 "%s", psz_cmd ) ) > 0 )
411 i_ret = net_Printf( VLC_OBJECT(p_access), p_sys->fd_cmd, NULL,
417 msg_Err( p_access, "failed to send command" );
423 /* TODO support this s**t :
424 RFC 959 allows the client to send certain TELNET strings at any moment,
425 even in the middle of a request:
428 * \377\376x where x is one byte.
429 * \377\375x where x is one byte. The server is obliged to send \377\374x
430 * immediately after reading x.
431 * \377\374x where x is one byte.
432 * \377\373x where x is one byte. The server is obliged to send \377\376x
433 * immediately after reading x.
434 * \377x for any other byte x.
436 These strings are not part of the requests, except in the case \377\377,
437 where the request contains one \377. */
438 static int ftp_ReadCommand( access_t *p_access,
439 int *pi_answer, char **ppsz_answer )
441 access_sys_t *p_sys = p_access->p_sys;
445 psz_line = net_Gets( p_access, p_sys->fd_cmd, NULL );
446 msg_Dbg( p_access, "answer=%s", psz_line );
447 if( psz_line == NULL || strlen( psz_line ) < 3 )
449 msg_Err( p_access, "cannot get answer" );
450 if( psz_line ) free( psz_line );
451 if( pi_answer ) *pi_answer = 500;
452 if( ppsz_answer ) *ppsz_answer = NULL;
456 if( psz_line[3] == '-' ) /* Multiple response */
460 memcpy( end, psz_line, 3 );
465 char *psz_tmp = net_Gets( p_access, p_sys->fd_cmd, NULL );
467 if( psz_tmp == NULL ) /* Error */
470 if( !strncmp( psz_tmp, end, 4 ) )
479 i_answer = atoi( psz_line );
481 if( pi_answer ) *pi_answer = i_answer;
484 *ppsz_answer = psz_line;
490 return( i_answer / 100 );
493 static int ftp_StartStream( access_t *p_access, off_t i_start )
495 access_sys_t *p_sys = p_access->p_sys;
499 char *psz_arg, *psz_parser;
504 if( ftp_SendCommand( p_access, "PASV" ) < 0 ||
505 ftp_ReadCommand( p_access, &i_answer, &psz_arg ) != 2 )
507 msg_Err( p_access, "cannot set passive transfer mode" );
511 psz_parser = strchr( psz_arg, '(' );
513 sscanf( psz_parser, "(%d,%d,%d,%d,%d,%d", &a1, &a2, &a3,
514 &a4, &p1, &p2 ) < 6 )
517 msg_Err( p_access, "cannot get ip/port for passive transfer mode" );
522 sprintf( psz_ip, "%d.%d.%d.%d", a1, a2, a3, a4 );
523 i_port = p1 * 256 + p2;
524 msg_Dbg( p_access, "ip:%s port:%d", psz_ip, i_port );
526 if( ftp_SendCommand( p_access, "TYPE I" ) < 0 ||
527 ftp_ReadCommand( p_access, &i_answer, NULL ) != 2 )
529 msg_Err( p_access, "cannot set binary transfer mode" );
535 if( ftp_SendCommand( p_access, "REST "I64Fu, i_start ) < 0 ||
536 ftp_ReadCommand( p_access, &i_answer, NULL ) > 3 )
538 msg_Err( p_access, "cannot set restart point" );
543 msg_Dbg( p_access, "waiting for data connection..." );
544 p_sys->fd_data = net_OpenTCP( p_access, psz_ip, i_port );
545 if( p_sys->fd_data < 0 )
547 msg_Err( p_access, "failed to connect with server" );
550 msg_Dbg( p_access, "connection with \"%s:%d\" successful",
554 if( ftp_SendCommand( p_access, "RETR %s", p_sys->url.psz_path ) < 0 ||
555 ftp_ReadCommand( p_access, &i_answer, NULL ) > 2 )
557 msg_Err( p_access, "cannot retreive file" );
563 static int ftp_StopStream ( access_t *p_access )
565 access_sys_t *p_sys = p_access->p_sys;
569 if( ftp_SendCommand( p_access, "ABOR" ) < 0 )
571 msg_Warn( p_access, "cannot abord file" );
572 if( p_sys->fd_data > 0 )
573 net_Close( p_sys->fd_data );
577 if( p_sys->fd_data > 0 )
579 net_Close( p_sys->fd_data );
581 ftp_ReadCommand( p_access, &i_answer, NULL );
583 ftp_ReadCommand( p_access, &i_answer, NULL );