1 /*****************************************************************************
2 * ftp.c: FTP input module
3 *****************************************************************************
4 * Copyright (C) 2001-2005 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
31 #include <vlc/input.h>
35 /*****************************************************************************
37 *****************************************************************************/
38 static int Open ( vlc_object_t * );
39 static void Close( vlc_object_t * );
41 #define CACHING_TEXT N_("Caching value in ms")
42 #define CACHING_LONGTEXT N_( \
43 "Allows you to modify the default caching value for FTP streams. This " \
44 "value should be set in millisecond units." )
45 #define USER_TEXT N_("FTP user name")
46 #define USER_LONGTEXT N_("Allows you to modify the user name that will " \
47 "be used for the connection.")
48 #define PASS_TEXT N_("FTP password")
49 #define PASS_LONGTEXT N_("Allows you to modify the password that will be " \
50 "used for the connection.")
51 #define ACCOUNT_TEXT N_("FTP account")
52 #define ACCOUNT_LONGTEXT N_("Allows you to modify the account that will be " \
53 "used for the connection.")
56 set_shortname( "FTP" );
57 set_description( _("FTP input") );
58 set_capability( "access2", 0 );
59 set_category( CAT_INPUT );
60 set_subcategory( SUBCAT_INPUT_ACCESS );
61 add_integer( "ftp-caching", 2 * DEFAULT_PTS_DELAY / 1000, NULL,
62 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
63 add_string( "ftp-user", "anonymous", NULL, USER_TEXT, USER_LONGTEXT,
65 add_string( "ftp-pwd", "anonymous@dummy.org", NULL, PASS_TEXT,
66 PASS_LONGTEXT, VLC_FALSE );
67 add_string( "ftp-account", "anonymous", NULL, ACCOUNT_TEXT,
68 ACCOUNT_LONGTEXT, VLC_FALSE );
69 add_shortcut( "ftp" );
70 set_callbacks( Open, Close );
73 /*****************************************************************************
75 *****************************************************************************/
76 static int Read( access_t *, uint8_t *, int );
77 static int Seek( access_t *, int64_t );
78 static int Control( access_t *, int, va_list );
90 static int ftp_SendCommand( access_t *, char *, ... );
91 static int ftp_ReadCommand( access_t *, int *, char ** );
92 static int ftp_StartStream( access_t *, int64_t );
93 static int ftp_StopStream ( access_t *);
95 /****************************************************************************
96 * Open: connect to ftp server and ask for file
97 ****************************************************************************/
98 static int Open( vlc_object_t *p_this )
100 access_t *p_access = (access_t*)p_this;
108 p_access->pf_read = Read;
109 p_access->pf_block = NULL;
110 p_access->pf_seek = Seek;
111 p_access->pf_control = Control;
112 p_access->info.i_update = 0;
113 p_access->info.i_size = 0;
114 p_access->info.i_pos = 0;
115 p_access->info.b_eof = VLC_FALSE;
116 p_access->info.i_title = 0;
117 p_access->info.i_seekpoint = 0;
118 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
119 memset( p_sys, 0, sizeof( access_sys_t ) );
122 p_sys->psz_epsv_ip = NULL;
124 /* *** Parse URL and get server addr/port and path *** */
125 psz = p_access->psz_path;
130 vlc_UrlParse( &p_sys->url, psz, 0 );
132 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
134 msg_Err( p_access, "invalid server name" );
137 if( p_sys->url.i_port <= 0 )
139 p_sys->url.i_port = 21; /* default port */
142 /* *** Open a TCP connection with server *** */
143 msg_Dbg( p_access, "waiting for connection..." );
144 p_sys->fd_cmd = net_OpenTCP( p_access, p_sys->url.psz_host,
146 if( p_sys->fd_cmd < 0 )
148 msg_Err( p_access, "failed to connect with server" );
154 if( ftp_ReadCommand( p_access, &i_answer, NULL ) != 1 )
159 if( i_answer / 100 != 2 )
161 msg_Err( p_access, "connection rejected" );
165 msg_Dbg( p_access, "connection accepted (%d)", i_answer );
167 psz = var_CreateGetString( p_access, "ftp-user" );
168 if( ftp_SendCommand( p_access, "USER %s", psz ) < 0 ||
169 ftp_ReadCommand( p_access, &i_answer, NULL ) < 0 )
176 switch( i_answer / 100 )
179 msg_Dbg( p_access, "user accepted" );
182 msg_Dbg( p_access, "password needed" );
183 psz = var_CreateGetString( p_access, "ftp-pwd" );
184 if( ftp_SendCommand( p_access, "PASS %s", psz ) < 0 ||
185 ftp_ReadCommand( p_access, &i_answer, NULL ) < 0 )
192 switch( i_answer / 100 )
195 msg_Dbg( p_access, "password accepted" );
198 msg_Dbg( p_access, "account needed" );
199 psz = var_CreateGetString( p_access, "ftp-account" );
200 if( ftp_SendCommand( p_access, "ACCT %s",
202 ftp_ReadCommand( p_access, &i_answer, NULL ) < 0 )
209 if( i_answer / 100 != 2 )
211 msg_Err( p_access, "account rejected" );
214 msg_Dbg( p_access, "account accepted" );
218 msg_Err( p_access, "password rejected" );
223 msg_Err( p_access, "user rejected" );
227 /* Extended passive mode */
228 if( ftp_SendCommand( p_access, "EPSV ALL" ) < 0 )
230 msg_Err( p_access, "cannot request extended passive mode" );
234 if( ftp_ReadCommand( p_access, &i_answer, NULL ) == 2 )
236 char hostaddr[NI_MAXHOST];
237 struct sockaddr_storage addr;
238 socklen_t len = sizeof (addr);
240 if( getpeername( p_sys->fd_cmd, (struct sockaddr *)&addr, &len ) )
242 msg_Err( p_access, "getpeername failed" );
246 i_answer = vlc_getnameinfo( p_this, (struct sockaddr *)&addr, len,
247 hostaddr, sizeof( hostaddr ), NULL, 0,
251 msg_Err( p_access, "getnameinfo failed: %s",
252 vlc_gai_strerror( i_answer ) );
255 p_sys->psz_epsv_ip = strdup( hostaddr );
257 if( p_sys->psz_epsv_ip == NULL )
258 msg_Info( p_access, "FTP Extended passive mode disabled" );
261 if( ftp_SendCommand( p_access, "TYPE I" ) < 0 ||
262 ftp_ReadCommand( p_access, &i_answer, NULL ) != 2 )
264 msg_Err( p_access, "cannot set binary transfer mode" );
269 if( ftp_SendCommand( p_access, "SIZE %s", p_sys->url.psz_path ) < 0 ||
270 ftp_ReadCommand( p_access, &i_answer, &psz_arg ) != 2 )
272 msg_Err( p_access, "cannot get file size" );
275 p_access->info.i_size = atoll( &psz_arg[4] );
277 msg_Dbg( p_access, "file size: "I64Fd, p_access->info.i_size );
279 /* Start the 'stream' */
280 if( ftp_StartStream( p_access, 0 ) < 0 )
282 msg_Err( p_access, "cannot retrieve file" );
286 /* Update default_pts to a suitable value for ftp access */
287 var_Create( p_access, "ftp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
292 if( p_sys->fd_cmd > 0 )
294 net_Close( p_sys->fd_cmd );
296 vlc_UrlClean( &p_sys->url );
301 /*****************************************************************************
302 * Close: free unused data structures
303 *****************************************************************************/
304 static void Close( vlc_object_t *p_this )
306 access_t *p_access = (access_t*)p_this;
307 access_sys_t *p_sys = p_access->p_sys;
309 msg_Dbg( p_access, "stopping stream" );
310 ftp_StopStream( p_access );
312 if( ftp_SendCommand( p_access, "QUIT" ) < 0 )
314 msg_Warn( p_access, "cannot quit" );
318 ftp_ReadCommand( p_access, NULL, NULL );
320 net_Close( p_sys->fd_cmd );
321 if( p_sys->psz_epsv_ip != NULL )
322 free( p_sys->psz_epsv_ip );
325 vlc_UrlClean( &p_sys->url );
329 /*****************************************************************************
330 * Seek: try to go at the right place
331 *****************************************************************************/
332 static int Seek( access_t *p_access, int64_t i_pos )
338 msg_Dbg( p_access, "seeking to "I64Fd, i_pos );
340 ftp_StopStream( p_access );
341 if( ftp_StartStream( p_access, i_pos ) < 0 )
343 p_access->info.b_eof = VLC_TRUE;
347 p_access->info.b_eof = VLC_FALSE;
348 p_access->info.i_pos = i_pos;
353 /*****************************************************************************
355 *****************************************************************************/
356 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
358 access_sys_t *p_sys = p_access->p_sys;
361 if( p_access->info.b_eof )
364 i_read = net_Read( p_access, p_sys->fd_data, NULL, p_buffer, i_len,
367 p_access->info.b_eof = VLC_TRUE;
368 else if( i_read > 0 )
369 p_access->info.i_pos += i_read;
374 /*****************************************************************************
376 *****************************************************************************/
377 static int Control( access_t *p_access, int i_query, va_list args )
387 case ACCESS_CAN_SEEK:
388 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
391 case ACCESS_CAN_FASTSEEK:
392 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
393 *pb_bool = VLC_FALSE;
395 case ACCESS_CAN_PAUSE:
396 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
397 *pb_bool = VLC_TRUE; /* FIXME */
399 case ACCESS_CAN_CONTROL_PACE:
400 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
401 *pb_bool = VLC_TRUE; /* FIXME */
406 pi_int = (int*)va_arg( args, int * );
410 case ACCESS_GET_PTS_DELAY:
411 pi_64 = (int64_t*)va_arg( args, int64_t * );
412 var_Get( p_access, "ftp-caching", &val );
413 *pi_64 = (int64_t)var_GetInteger( p_access, "ftp-caching" ) * I64C(1000);
417 case ACCESS_SET_PAUSE_STATE:
421 case ACCESS_GET_TITLE_INFO:
422 case ACCESS_SET_TITLE:
423 case ACCESS_SET_SEEKPOINT:
424 case ACCESS_SET_PRIVATE_ID_STATE:
428 msg_Warn( p_access, "unimplemented query in control" );
435 /*****************************************************************************
437 *****************************************************************************/
438 static int ftp_SendCommand( access_t *p_access, char *psz_fmt, ... )
440 access_sys_t *p_sys = p_access->p_sys;
444 va_start( args, psz_fmt );
445 vasprintf( &psz_cmd, psz_fmt, args );
448 msg_Dbg( p_access, "ftp_SendCommand:\"%s\"", psz_cmd);
449 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd_cmd, NULL, "%s\r\n",
452 msg_Err( p_access, "failed to send command" );
458 /* TODO support this s**t :
459 RFC 959 allows the client to send certain TELNET strings at any moment,
460 even in the middle of a request:
463 * \377\376x where x is one byte.
464 * \377\375x where x is one byte. The server is obliged to send \377\374x
465 * immediately after reading x.
466 * \377\374x where x is one byte.
467 * \377\373x where x is one byte. The server is obliged to send \377\376x
468 * immediately after reading x.
469 * \377x for any other byte x.
471 These strings are not part of the requests, except in the case \377\377,
472 where the request contains one \377. */
473 static int ftp_ReadCommand( access_t *p_access,
474 int *pi_answer, char **ppsz_answer )
476 access_sys_t *p_sys = p_access->p_sys;
480 psz_line = net_Gets( p_access, p_sys->fd_cmd, NULL );
481 msg_Dbg( p_access, "answer=%s", psz_line );
482 if( psz_line == NULL || strlen( psz_line ) < 3 )
484 msg_Err( p_access, "cannot get answer" );
485 if( psz_line ) free( psz_line );
486 if( pi_answer ) *pi_answer = 500;
487 if( ppsz_answer ) *ppsz_answer = NULL;
491 if( psz_line[3] == '-' ) /* Multiple response */
495 memcpy( end, psz_line, 3 );
500 char *psz_tmp = net_Gets( p_access, p_sys->fd_cmd, NULL );
502 if( psz_tmp == NULL ) /* Error */
505 if( !strncmp( psz_tmp, end, 4 ) )
514 i_answer = atoi( psz_line );
516 if( pi_answer ) *pi_answer = i_answer;
519 *ppsz_answer = psz_line;
525 return( i_answer / 100 );
528 static int ftp_StartStream( access_t *p_access, off_t i_start )
530 access_sys_t *p_sys = p_access->p_sys;
532 char psz_ipv4[16], *psz_ip;
534 char *psz_arg, *psz_parser;
535 unsigned a1, a2, a3, a4, p1, p2;
538 if( ( ftp_SendCommand( p_access, p_sys->psz_epsv_ip != NULL
539 ? "EPSV" : "PASV" ) < 0 )
540 || ( ftp_ReadCommand( p_access, &i_answer, &psz_arg ) != 2 ) )
542 msg_Err( p_access, "cannot set passive mode" );
546 psz_parser = strchr( psz_arg, '(' );
547 if( psz_parser == NULL )
550 msg_Err( p_access, "cannot parse passive mode response" );
554 psz_ip = p_sys->psz_epsv_ip;
557 char psz_fmt[7] = "(|||%u";
558 psz_fmt[1] = psz_fmt[2] = psz_fmt[3] = psz_parser[1];
560 if( sscanf( psz_parser, psz_fmt, &i_port ) < 1 )
563 msg_Err( p_access, "cannot parse passive mode response" );
569 if( ( sscanf( psz_parser, "(%u,%u,%u,%u,%u,%u", &a1, &a2, &a3, &a4,
570 &p1, &p2 ) < 6 ) || ( a1 > 255 ) || ( a2 > 255 )
571 || ( a3 > 255 ) || ( a4 > 255 ) || ( p1 > 255 ) || ( p2 > 255 ) )
574 msg_Err( p_access, "cannot parse passive mode response" );
578 sprintf( psz_ipv4, "%u.%u.%u.%u", a1, a2, a3, a4 );
580 i_port = (p1 << 8) | p2;
584 msg_Dbg( p_access, "ip:%s port:%d", psz_ip, i_port );
586 if( ftp_SendCommand( p_access, "TYPE I" ) < 0 ||
587 ftp_ReadCommand( p_access, &i_answer, NULL ) != 2 )
589 msg_Err( p_access, "cannot set binary transfer mode" );
595 if( ftp_SendCommand( p_access, "REST "I64Fu, i_start ) < 0 ||
596 ftp_ReadCommand( p_access, &i_answer, NULL ) > 3 )
598 msg_Err( p_access, "cannot set restart point" );
603 msg_Dbg( p_access, "waiting for data connection..." );
604 p_sys->fd_data = net_OpenTCP( p_access, psz_ip, i_port );
605 if( p_sys->fd_data < 0 )
607 msg_Err( p_access, "failed to connect with server" );
610 msg_Dbg( p_access, "connection with \"%s:%d\" successful",
614 if( ftp_SendCommand( p_access, "RETR %s", p_sys->url.psz_path ) < 0 ||
615 ftp_ReadCommand( p_access, &i_answer, NULL ) > 2 )
617 msg_Err( p_access, "cannot retreive file" );
623 static int ftp_StopStream ( access_t *p_access )
625 access_sys_t *p_sys = p_access->p_sys;
629 if( ftp_SendCommand( p_access, "ABOR" ) < 0 )
631 msg_Warn( p_access, "cannot abord file" );
632 if( p_sys->fd_data > 0 )
633 net_Close( p_sys->fd_data );
637 if( p_sys->fd_data > 0 )
639 net_Close( p_sys->fd_data );
641 ftp_ReadCommand( p_access, &i_answer, NULL );
643 ftp_ReadCommand( p_access, &i_answer, NULL );