1 /*****************************************************************************
2 * ftp.c: FTP input module
3 *****************************************************************************
4 * Copyright (C) 2001-2005 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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
31 #include <vlc/input.h>
34 #if defined( UNDER_CE )
36 #elif defined( WIN32 )
37 # include <winsock2.h>
39 # include <sys/socket.h>
42 /*****************************************************************************
44 *****************************************************************************/
45 static int Open ( vlc_object_t * );
46 static void Close( vlc_object_t * );
48 #define CACHING_TEXT N_("Caching value in ms")
49 #define CACHING_LONGTEXT N_( \
50 "Allows you to modify the default caching value for FTP streams. This " \
51 "value should be set in millisecond units." )
52 #define USER_TEXT N_("FTP user name")
53 #define USER_LONGTEXT N_("Allows you to modify the user name that will " \
54 "be used for the connection.")
55 #define PASS_TEXT N_("FTP password")
56 #define PASS_LONGTEXT N_("Allows you to modify the password that will be " \
57 "used for the connection.")
58 #define ACCOUNT_TEXT N_("FTP account")
59 #define ACCOUNT_LONGTEXT N_("Allows you to modify the account that will be " \
60 "used for the connection.")
63 set_shortname( "FTP" );
64 set_description( _("FTP input") );
65 set_capability( "access2", 0 );
66 set_category( CAT_INPUT );
67 set_subcategory( SUBCAT_INPUT_ACCESS );
68 add_integer( "ftp-caching", 2 * DEFAULT_PTS_DELAY / 1000, NULL,
69 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
70 add_string( "ftp-user", "anonymous", NULL, USER_TEXT, USER_LONGTEXT,
72 add_string( "ftp-pwd", "anonymous@dummy.org", NULL, PASS_TEXT,
73 PASS_LONGTEXT, VLC_FALSE );
74 add_string( "ftp-account", "anonymous", NULL, ACCOUNT_TEXT,
75 ACCOUNT_LONGTEXT, VLC_FALSE );
76 add_shortcut( "ftp" );
77 set_callbacks( Open, Close );
80 /*****************************************************************************
82 *****************************************************************************/
83 static int Read( access_t *, uint8_t *, int );
84 static int Seek( access_t *, int64_t );
85 static int Control( access_t *, int, va_list );
97 static int ftp_SendCommand( access_t *, char *, ... );
98 static int ftp_ReadCommand( access_t *, int *, char ** );
99 static int ftp_StartStream( access_t *, int64_t );
100 static int ftp_StopStream ( access_t *);
102 static int Connect( access_t *p_access, access_sys_t *p_sys )
107 /* *** Open a TCP connection with server *** */
108 msg_Dbg( p_access, "waiting for connection..." );
109 p_sys->fd_cmd = fd = net_OpenTCP( p_access, p_sys->url.psz_host,
113 msg_Err( p_access, "failed to connect with server" );
119 if( ftp_ReadCommand( p_access, &i_answer, NULL ) != 1 )
124 if( i_answer / 100 != 2 )
126 msg_Err( p_access, "connection rejected" );
130 msg_Dbg( p_access, "connection accepted (%d)", i_answer );
132 psz = var_CreateGetString( p_access, "ftp-user" );
133 if( ftp_SendCommand( p_access, "USER %s", psz ) < 0 ||
134 ftp_ReadCommand( p_access, &i_answer, NULL ) < 0 )
141 switch( i_answer / 100 )
144 msg_Dbg( p_access, "user accepted" );
147 msg_Dbg( p_access, "password needed" );
148 psz = var_CreateGetString( p_access, "ftp-pwd" );
149 if( ftp_SendCommand( p_access, "PASS %s", psz ) < 0 ||
150 ftp_ReadCommand( p_access, &i_answer, NULL ) < 0 )
157 switch( i_answer / 100 )
160 msg_Dbg( p_access, "password accepted" );
163 msg_Dbg( p_access, "account needed" );
164 psz = var_CreateGetString( p_access, "ftp-account" );
165 if( ftp_SendCommand( p_access, "ACCT %s",
167 ftp_ReadCommand( p_access, &i_answer, NULL ) < 0 )
174 if( i_answer / 100 != 2 )
176 msg_Err( p_access, "account rejected" );
179 msg_Dbg( p_access, "account accepted" );
183 msg_Err( p_access, "password rejected" );
188 msg_Err( p_access, "user rejected" );
195 /****************************************************************************
196 * Open: connect to ftp server and ask for file
197 ****************************************************************************/
198 static int Open( vlc_object_t *p_this )
200 access_t *p_access = (access_t*)p_this;
208 p_access->pf_read = Read;
209 p_access->pf_block = NULL;
210 p_access->pf_seek = Seek;
211 p_access->pf_control = Control;
212 p_access->info.i_update = 0;
213 p_access->info.i_size = 0;
214 p_access->info.i_pos = 0;
215 p_access->info.b_eof = VLC_FALSE;
216 p_access->info.i_title = 0;
217 p_access->info.i_seekpoint = 0;
218 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
219 memset( p_sys, 0, sizeof( access_sys_t ) );
222 p_sys->psz_epsv_ip = NULL;
224 /* *** Parse URL and get server addr/port and path *** */
225 psz = p_access->psz_path;
230 vlc_UrlParse( &p_sys->url, psz, 0 );
232 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
234 msg_Err( p_access, "invalid server name" );
237 if( p_sys->url.i_port <= 0 )
239 p_sys->url.i_port = 21; /* default port */
242 if( Connect( p_access, p_sys ) < 0 )
245 /* Extended passive mode */
246 if( ftp_SendCommand( p_access, "EPSV ALL" ) < 0 )
248 msg_Err( p_access, "cannot request extended passive mode" );
252 if( ftp_ReadCommand( p_access, &i_answer, NULL ) == 2 )
254 char hostaddr[NI_MAXNUMERICHOST];
255 struct sockaddr_storage addr;
256 socklen_t len = sizeof (addr);
258 if( getpeername( p_sys->fd_cmd, (struct sockaddr *)&addr, &len ) )
260 msg_Err( p_access, "getpeername failed" );
264 i_answer = vlc_getnameinfo( (struct sockaddr *)&addr, len, hostaddr,
265 sizeof( hostaddr ), NULL, NI_NUMERICHOST );
268 msg_Err( p_access, "getnameinfo failed: %s",
269 vlc_gai_strerror( i_answer ) );
272 p_sys->psz_epsv_ip = strdup( hostaddr );
273 if( p_sys->psz_epsv_ip == NULL )
278 /* If ESPV ALL fails, we fallback to PASV.
279 * We have to restart the connection in case there is a NAT that
280 * understands EPSV ALL in the way, and hence won't allow PASV on
281 * the initial connection.
283 net_Close( p_sys->fd_cmd );
286 if( ( p_sys->fd_cmd = Connect( p_access, p_sys ) ) < 0 )
289 msg_Info( p_access, "FTP Extended passive mode disabled" );
293 if( ftp_SendCommand( p_access, "TYPE I" ) < 0 ||
294 ftp_ReadCommand( p_access, &i_answer, NULL ) != 2 )
296 msg_Err( p_access, "cannot set binary transfer mode" );
301 if( ftp_SendCommand( p_access, "SIZE %s", p_sys->url.psz_path ) < 0 ||
302 ftp_ReadCommand( p_access, &i_answer, &psz_arg ) != 2 )
304 msg_Err( p_access, "cannot get file size" );
307 p_access->info.i_size = atoll( &psz_arg[4] );
309 msg_Dbg( p_access, "file size: "I64Fd, p_access->info.i_size );
311 /* Start the 'stream' */
312 if( ftp_StartStream( p_access, 0 ) < 0 )
314 msg_Err( p_access, "cannot retrieve file" );
318 /* Update default_pts to a suitable value for ftp access */
319 var_Create( p_access, "ftp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
324 if( p_sys->fd_cmd >= 0 )
325 net_Close( p_sys->fd_cmd );
326 vlc_UrlClean( &p_sys->url );
331 /*****************************************************************************
332 * Close: free unused data structures
333 *****************************************************************************/
334 static void Close( vlc_object_t *p_this )
336 access_t *p_access = (access_t*)p_this;
337 access_sys_t *p_sys = p_access->p_sys;
339 msg_Dbg( p_access, "stopping stream" );
340 ftp_StopStream( p_access );
342 if( ftp_SendCommand( p_access, "QUIT" ) < 0 )
344 msg_Warn( p_access, "cannot quit" );
348 ftp_ReadCommand( p_access, NULL, NULL );
350 net_Close( p_sys->fd_cmd );
351 if( p_sys->psz_epsv_ip != NULL )
352 free( p_sys->psz_epsv_ip );
355 vlc_UrlClean( &p_sys->url );
359 /*****************************************************************************
360 * Seek: try to go at the right place
361 *****************************************************************************/
362 static int Seek( access_t *p_access, int64_t i_pos )
368 msg_Dbg( p_access, "seeking to "I64Fd, i_pos );
370 ftp_StopStream( p_access );
371 if( ftp_StartStream( p_access, i_pos ) < 0 )
373 p_access->info.b_eof = VLC_TRUE;
377 p_access->info.b_eof = VLC_FALSE;
378 p_access->info.i_pos = i_pos;
383 /*****************************************************************************
385 *****************************************************************************/
386 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
388 access_sys_t *p_sys = p_access->p_sys;
391 if( p_access->info.b_eof )
394 i_read = net_Read( p_access, p_sys->fd_data, NULL, p_buffer, i_len,
397 p_access->info.b_eof = VLC_TRUE;
398 else if( i_read > 0 )
399 p_access->info.i_pos += i_read;
404 /*****************************************************************************
406 *****************************************************************************/
407 static int Control( access_t *p_access, int i_query, va_list args )
417 case ACCESS_CAN_SEEK:
418 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
421 case ACCESS_CAN_FASTSEEK:
422 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
423 *pb_bool = VLC_FALSE;
425 case ACCESS_CAN_PAUSE:
426 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
427 *pb_bool = VLC_TRUE; /* FIXME */
429 case ACCESS_CAN_CONTROL_PACE:
430 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
431 *pb_bool = VLC_TRUE; /* FIXME */
436 pi_int = (int*)va_arg( args, int * );
440 case ACCESS_GET_PTS_DELAY:
441 pi_64 = (int64_t*)va_arg( args, int64_t * );
442 var_Get( p_access, "ftp-caching", &val );
443 *pi_64 = (int64_t)var_GetInteger( p_access, "ftp-caching" ) * I64C(1000);
447 case ACCESS_SET_PAUSE_STATE:
451 case ACCESS_GET_TITLE_INFO:
452 case ACCESS_SET_TITLE:
453 case ACCESS_SET_SEEKPOINT:
454 case ACCESS_SET_PRIVATE_ID_STATE:
458 msg_Warn( p_access, "unimplemented query in control" );
465 /*****************************************************************************
467 *****************************************************************************/
468 static int ftp_SendCommand( access_t *p_access, char *psz_fmt, ... )
470 access_sys_t *p_sys = p_access->p_sys;
474 va_start( args, psz_fmt );
475 vasprintf( &psz_cmd, psz_fmt, args );
478 msg_Dbg( p_access, "ftp_SendCommand:\"%s\"", psz_cmd);
479 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd_cmd, NULL, "%s\r\n",
482 msg_Err( p_access, "failed to send command" );
488 /* TODO support this s**t :
489 RFC 959 allows the client to send certain TELNET strings at any moment,
490 even in the middle of a request:
493 * \377\376x where x is one byte.
494 * \377\375x where x is one byte. The server is obliged to send \377\374x
495 * immediately after reading x.
496 * \377\374x where x is one byte.
497 * \377\373x where x is one byte. The server is obliged to send \377\376x
498 * immediately after reading x.
499 * \377x for any other byte x.
501 These strings are not part of the requests, except in the case \377\377,
502 where the request contains one \377. */
503 static int ftp_ReadCommand( access_t *p_access,
504 int *pi_answer, char **ppsz_answer )
506 access_sys_t *p_sys = p_access->p_sys;
510 psz_line = net_Gets( p_access, p_sys->fd_cmd, NULL );
511 msg_Dbg( p_access, "answer=%s", psz_line );
512 if( psz_line == NULL || strlen( psz_line ) < 3 )
514 msg_Err( p_access, "cannot get answer" );
515 if( psz_line ) free( psz_line );
516 if( pi_answer ) *pi_answer = 500;
517 if( ppsz_answer ) *ppsz_answer = NULL;
521 if( psz_line[3] == '-' ) /* Multiple response */
525 memcpy( end, psz_line, 3 );
530 char *psz_tmp = net_Gets( p_access, p_sys->fd_cmd, NULL );
532 if( psz_tmp == NULL ) /* Error */
535 if( !strncmp( psz_tmp, end, 4 ) )
544 i_answer = atoi( psz_line );
546 if( pi_answer ) *pi_answer = i_answer;
549 *ppsz_answer = psz_line;
555 return( i_answer / 100 );
558 static int ftp_StartStream( access_t *p_access, off_t i_start )
560 access_sys_t *p_sys = p_access->p_sys;
562 char psz_ipv4[16], *psz_ip;
564 char *psz_arg, *psz_parser;
565 unsigned a1, a2, a3, a4, p1, p2;
568 if( ( ftp_SendCommand( p_access, p_sys->psz_epsv_ip != NULL
569 ? "EPSV" : "PASV" ) < 0 )
570 || ( ftp_ReadCommand( p_access, &i_answer, &psz_arg ) != 2 ) )
572 msg_Err( p_access, "cannot set passive mode" );
576 psz_parser = strchr( psz_arg, '(' );
577 if( psz_parser == NULL )
580 msg_Err( p_access, "cannot parse passive mode response" );
584 psz_ip = p_sys->psz_epsv_ip;
587 char psz_fmt[7] = "(|||%u";
588 psz_fmt[1] = psz_fmt[2] = psz_fmt[3] = psz_parser[1];
590 if( sscanf( psz_parser, psz_fmt, &i_port ) < 1 )
593 msg_Err( p_access, "cannot parse passive mode response" );
599 if( ( sscanf( psz_parser, "(%u,%u,%u,%u,%u,%u", &a1, &a2, &a3, &a4,
600 &p1, &p2 ) < 6 ) || ( a1 > 255 ) || ( a2 > 255 )
601 || ( a3 > 255 ) || ( a4 > 255 ) || ( p1 > 255 ) || ( p2 > 255 ) )
604 msg_Err( p_access, "cannot parse passive mode response" );
608 sprintf( psz_ipv4, "%u.%u.%u.%u", a1, a2, a3, a4 );
610 i_port = (p1 << 8) | p2;
614 msg_Dbg( p_access, "ip:%s port:%d", psz_ip, i_port );
616 if( ftp_SendCommand( p_access, "TYPE I" ) < 0 ||
617 ftp_ReadCommand( p_access, &i_answer, NULL ) != 2 )
619 msg_Err( p_access, "cannot set binary transfer mode" );
625 if( ftp_SendCommand( p_access, "REST "I64Fu, i_start ) < 0 ||
626 ftp_ReadCommand( p_access, &i_answer, NULL ) > 3 )
628 msg_Err( p_access, "cannot set restart point" );
633 msg_Dbg( p_access, "waiting for data connection..." );
634 p_sys->fd_data = net_OpenTCP( p_access, psz_ip, i_port );
635 if( p_sys->fd_data < 0 )
637 msg_Err( p_access, "failed to connect with server" );
640 msg_Dbg( p_access, "connection with \"%s:%d\" successful",
644 if( ftp_SendCommand( p_access, "RETR %s", p_sys->url.psz_path ) < 0 ||
645 ftp_ReadCommand( p_access, &i_answer, NULL ) > 2 )
647 msg_Err( p_access, "cannot retreive file" );
653 static int ftp_StopStream ( access_t *p_access )
655 access_sys_t *p_sys = p_access->p_sys;
659 if( ftp_SendCommand( p_access, "ABOR" ) < 0 )
661 msg_Warn( p_access, "cannot abord file" );
662 if( p_sys->fd_data > 0 )
663 net_Close( p_sys->fd_data );
667 if( p_sys->fd_data > 0 )
669 net_Close( p_sys->fd_data );
671 ftp_ReadCommand( p_access, &i_answer, NULL );
673 ftp_ReadCommand( p_access, &i_answer, NULL );