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_shortname( "FTP" );
56 set_description( _("FTP input") );
57 set_capability( "access2", 0 );
58 set_category( CAT_INPUT );
59 set_subcategory( SUBCAT_INPUT_ACCESS );
60 add_integer( "ftp-caching", 2 * DEFAULT_PTS_DELAY / 1000, NULL,
61 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
62 add_string( "ftp-user", "anonymous", NULL, USER_TEXT, USER_LONGTEXT,
64 add_string( "ftp-pwd", "anonymous@dummy.org", NULL, PASS_TEXT,
65 PASS_LONGTEXT, VLC_FALSE );
66 add_string( "ftp-account", "anonymous", NULL, ACCOUNT_TEXT,
67 ACCOUNT_LONGTEXT, VLC_FALSE );
68 add_shortcut( "ftp" );
69 set_callbacks( Open, Close );
72 /*****************************************************************************
74 *****************************************************************************/
75 static int Read( access_t *, uint8_t *, int );
76 static int Seek( access_t *, int64_t );
77 static int Control( access_t *, int, va_list );
89 static int ftp_SendCommand( access_t *, char *, ... );
90 static int ftp_ReadCommand( access_t *, int *, char ** );
91 static int ftp_StartStream( access_t *, int64_t );
92 static int ftp_StopStream ( access_t *);
94 /****************************************************************************
95 * Open: connect to ftp server and ask for file
96 ****************************************************************************/
97 static int Open( vlc_object_t *p_this )
99 access_t *p_access = (access_t*)p_this;
107 p_access->pf_read = Read;
108 p_access->pf_block = NULL;
109 p_access->pf_seek = Seek;
110 p_access->pf_control = Control;
111 p_access->info.i_update = 0;
112 p_access->info.i_size = 0;
113 p_access->info.i_pos = 0;
114 p_access->info.b_eof = VLC_FALSE;
115 p_access->info.i_title = 0;
116 p_access->info.i_seekpoint = 0;
117 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
118 memset( p_sys, 0, sizeof( access_sys_t ) );
122 /* *** Parse URL and get server addr/port and path *** */
123 psz = p_access->psz_path;
128 vlc_UrlParse( &p_sys->url, psz, 0 );
130 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
132 msg_Err( p_access, "invalid server name" );
135 if( p_sys->url.i_port <= 0 )
137 p_sys->url.i_port = 21; /* default port */
140 /* *** Open a TCP connection with server *** */
141 msg_Dbg( p_access, "waiting for connection..." );
142 p_sys->fd_cmd = net_OpenTCP( p_access, p_sys->url.psz_host,
144 if( p_sys->fd_cmd < 0 )
146 msg_Err( p_access, "failed to connect with server" );
152 if( ftp_ReadCommand( p_access, &i_answer, NULL ) != 1 )
157 if( i_answer / 100 != 2 )
159 msg_Err( p_access, "connection rejected" );
163 msg_Dbg( p_access, "connection accepted (%d)", i_answer );
165 psz = var_CreateGetString( p_access, "ftp-user" );
166 if( ftp_SendCommand( p_access, "USER %s", psz ) < 0 ||
167 ftp_ReadCommand( p_access, &i_answer, NULL ) < 0 )
174 switch( i_answer / 100 )
177 msg_Dbg( p_access, "user accepted" );
180 msg_Dbg( p_access, "password needed" );
181 psz = var_CreateGetString( p_access, "ftp-pwd" );
182 if( ftp_SendCommand( p_access, "PASS %s", psz ) < 0 ||
183 ftp_ReadCommand( p_access, &i_answer, NULL ) < 0 )
190 switch( i_answer / 100 )
193 msg_Dbg( p_access, "password accepted" );
196 msg_Dbg( p_access, "account needed" );
197 psz = var_CreateGetString( p_access, "ftp-account" );
198 if( ftp_SendCommand( p_access, "ACCT %s",
200 ftp_ReadCommand( p_access, &i_answer, NULL ) < 0 )
207 if( i_answer / 100 != 2 )
209 msg_Err( p_access, "account rejected" );
212 msg_Dbg( p_access, "account accepted" );
216 msg_Err( p_access, "password rejected" );
221 msg_Err( p_access, "user rejected" );
225 /* Extended passive mode */
226 if( ftp_SendCommand( p_access, "EPSV ALL" ) < 0 )
228 msg_Err( p_access, "cannot request extended passive mode" );
231 if( ftp_ReadCommand( p_access, &i_answer, NULL ) != 2 )
233 p_sys->b_epsv = VLC_FALSE;
234 msg_Warn( p_access, "Extended passive mode not supported" );
237 p_sys->b_epsv = VLC_TRUE;
240 if( ftp_SendCommand( p_access, "TYPE I" ) < 0 ||
241 ftp_ReadCommand( p_access, &i_answer, NULL ) != 2 )
243 msg_Err( p_access, "cannot set binary transfer mode" );
248 if( ftp_SendCommand( p_access, "SIZE %s", p_sys->url.psz_path ) < 0 ||
249 ftp_ReadCommand( p_access, &i_answer, &psz_arg ) != 2 )
251 msg_Err( p_access, "cannot get file size" );
254 p_access->info.i_size = atoll( &psz_arg[4] );
256 msg_Dbg( p_access, "file size: "I64Fd, p_access->info.i_size );
258 /* Start the 'stream' */
259 if( ftp_StartStream( p_access, 0 ) < 0 )
261 msg_Err( p_access, "cannot retrieve file" );
265 /* Update default_pts to a suitable value for ftp access */
266 var_Create( p_access, "ftp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
271 if( p_sys->fd_cmd > 0 )
273 net_Close( p_sys->fd_cmd );
275 vlc_UrlClean( &p_sys->url );
280 /*****************************************************************************
281 * Close: free unused data structures
282 *****************************************************************************/
283 static void Close( vlc_object_t *p_this )
285 access_t *p_access = (access_t*)p_this;
286 access_sys_t *p_sys = p_access->p_sys;
288 msg_Dbg( p_access, "stopping stream" );
289 ftp_StopStream( p_access );
291 if( ftp_SendCommand( p_access, "QUIT" ) < 0 )
293 msg_Warn( p_access, "cannot quit" );
297 ftp_ReadCommand( p_access, NULL, NULL );
299 net_Close( p_sys->fd_cmd );
302 vlc_UrlClean( &p_sys->url );
306 /*****************************************************************************
307 * Seek: try to go at the right place
308 *****************************************************************************/
309 static int Seek( access_t *p_access, int64_t i_pos )
315 msg_Dbg( p_access, "seeking to "I64Fd, i_pos );
317 ftp_StopStream( p_access );
318 if( ftp_StartStream( p_access, i_pos ) < 0 )
320 p_access->info.b_eof = VLC_TRUE;
324 p_access->info.b_eof = VLC_FALSE;
325 p_access->info.i_pos = i_pos;
330 /*****************************************************************************
332 *****************************************************************************/
333 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
335 access_sys_t *p_sys = p_access->p_sys;
338 if( p_access->info.b_eof )
341 i_read = net_Read( p_access, p_sys->fd_data, NULL, p_buffer, i_len,
344 p_access->info.b_eof = VLC_TRUE;
345 else if( i_read > 0 )
346 p_access->info.i_pos += i_read;
351 /*****************************************************************************
353 *****************************************************************************/
354 static int Control( access_t *p_access, int i_query, va_list args )
364 case ACCESS_CAN_SEEK:
365 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
368 case ACCESS_CAN_FASTSEEK:
369 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
370 *pb_bool = VLC_FALSE;
372 case ACCESS_CAN_PAUSE:
373 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
374 *pb_bool = VLC_TRUE; /* FIXME */
376 case ACCESS_CAN_CONTROL_PACE:
377 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
378 *pb_bool = VLC_TRUE; /* FIXME */
383 pi_int = (int*)va_arg( args, int * );
387 case ACCESS_GET_PTS_DELAY:
388 pi_64 = (int64_t*)va_arg( args, int64_t * );
389 var_Get( p_access, "ftp-caching", &val );
390 *pi_64 = (int64_t)var_GetInteger( p_access, "ftp-caching" ) * I64C(1000);
394 case ACCESS_SET_PAUSE_STATE:
398 case ACCESS_GET_TITLE_INFO:
399 case ACCESS_SET_TITLE:
400 case ACCESS_SET_SEEKPOINT:
401 case ACCESS_SET_PRIVATE_ID_STATE:
405 msg_Warn( p_access, "unimplemented query in control" );
412 /*****************************************************************************
414 *****************************************************************************/
415 static int ftp_SendCommand( access_t *p_access, char *psz_fmt, ... )
417 access_sys_t *p_sys = p_access->p_sys;
421 va_start( args, psz_fmt );
422 vasprintf( &psz_cmd, psz_fmt, args );
425 msg_Dbg( p_access, "ftp_SendCommand:\"%s\"", psz_cmd);
426 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd_cmd, NULL, "%s\r\n",
429 msg_Err( p_access, "failed to send command" );
435 /* TODO support this s**t :
436 RFC 959 allows the client to send certain TELNET strings at any moment,
437 even in the middle of a request:
440 * \377\376x where x is one byte.
441 * \377\375x where x is one byte. The server is obliged to send \377\374x
442 * immediately after reading x.
443 * \377\374x where x is one byte.
444 * \377\373x where x is one byte. The server is obliged to send \377\376x
445 * immediately after reading x.
446 * \377x for any other byte x.
448 These strings are not part of the requests, except in the case \377\377,
449 where the request contains one \377. */
450 static int ftp_ReadCommand( access_t *p_access,
451 int *pi_answer, char **ppsz_answer )
453 access_sys_t *p_sys = p_access->p_sys;
457 psz_line = net_Gets( p_access, p_sys->fd_cmd, NULL );
458 msg_Dbg( p_access, "answer=%s", psz_line );
459 if( psz_line == NULL || strlen( psz_line ) < 3 )
461 msg_Err( p_access, "cannot get answer" );
462 if( psz_line ) free( psz_line );
463 if( pi_answer ) *pi_answer = 500;
464 if( ppsz_answer ) *ppsz_answer = NULL;
468 if( psz_line[3] == '-' ) /* Multiple response */
472 memcpy( end, psz_line, 3 );
477 char *psz_tmp = net_Gets( p_access, p_sys->fd_cmd, NULL );
479 if( psz_tmp == NULL ) /* Error */
482 if( !strncmp( psz_tmp, end, 4 ) )
491 i_answer = atoi( psz_line );
493 if( pi_answer ) *pi_answer = i_answer;
496 *ppsz_answer = psz_line;
502 return( i_answer / 100 );
505 static int ftp_StartStream( access_t *p_access, off_t i_start )
507 access_sys_t *p_sys = p_access->p_sys;
509 char psz_ipv4[16], *psz_ip;
511 char *psz_arg, *psz_parser;
512 unsigned a1, a2, a3, a4, p1, p2;
515 if( ( ftp_SendCommand( p_access, p_sys->b_epsv ? "EPSV" : "PASV" ) < 0 )
516 || ( ftp_ReadCommand( p_access, &i_answer, &psz_arg ) != 2 ) )
518 msg_Err( p_access, "cannot set passive mode" );
522 psz_parser = strchr( psz_arg, '(' );
523 if( psz_parser == NULL )
526 msg_Err( p_access, "cannot parse passive mode response" );
532 char psz_fmt[7] = "(|||%u";
533 psz_fmt[1] = psz_fmt[2] = psz_fmt[3] = psz_parser[1];
535 if( sscanf( psz_parser, psz_fmt, &i_port ) < 1 )
538 msg_Err( p_access, "cannot parse passive mode response" );
541 /* FIXME: if psz_ip is a hostname/DNS to a cluster of FTP servers,
542 * this may fail because we'll end up on another server :(
543 * getpeername() + getnameinfo() should be used instead.
545 psz_ip = p_sys->url.psz_host;
549 if( ( sscanf( psz_parser, "(%u,%u,%u,%u,%u,%u", &a1, &a2, &a3, &a4,
550 &p1, &p2 ) < 6 ) || ( a1 > 255 ) || ( a2 > 255 )
551 || ( a3 > 255 ) || ( a4 > 255 ) || ( p1 > 255 ) || ( p2 > 255 ) )
554 msg_Err( p_access, "cannot parse passive mode response" );
558 sprintf( psz_ipv4, "%u.%u.%u.%u", a1, a2, a3, a4 );
560 i_port = (p1 << 8) | p2;
564 msg_Dbg( p_access, "ip:%s port:%d", psz_ip, i_port );
566 if( ftp_SendCommand( p_access, "TYPE I" ) < 0 ||
567 ftp_ReadCommand( p_access, &i_answer, NULL ) != 2 )
569 msg_Err( p_access, "cannot set binary transfer mode" );
575 if( ftp_SendCommand( p_access, "REST "I64Fu, i_start ) < 0 ||
576 ftp_ReadCommand( p_access, &i_answer, NULL ) > 3 )
578 msg_Err( p_access, "cannot set restart point" );
583 msg_Dbg( p_access, "waiting for data connection..." );
584 p_sys->fd_data = net_OpenTCP( p_access, psz_ip, i_port );
585 if( p_sys->fd_data < 0 )
587 msg_Err( p_access, "failed to connect with server" );
590 msg_Dbg( p_access, "connection with \"%s:%d\" successful",
594 if( ftp_SendCommand( p_access, "RETR %s", p_sys->url.psz_path ) < 0 ||
595 ftp_ReadCommand( p_access, &i_answer, NULL ) > 2 )
597 msg_Err( p_access, "cannot retreive file" );
603 static int ftp_StopStream ( access_t *p_access )
605 access_sys_t *p_sys = p_access->p_sys;
609 if( ftp_SendCommand( p_access, "ABOR" ) < 0 )
611 msg_Warn( p_access, "cannot abord file" );
612 if( p_sys->fd_data > 0 )
613 net_Close( p_sys->fd_data );
617 if( p_sys->fd_data > 0 )
619 net_Close( p_sys->fd_data );
621 ftp_ReadCommand( p_access, &i_answer, NULL );
623 ftp_ReadCommand( p_access, &i_answer, NULL );