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>
32 #include <vlc_interaction.h>
37 /*****************************************************************************
39 *****************************************************************************/
40 static int Open ( vlc_object_t * );
41 static void Close( vlc_object_t * );
43 #define CACHING_TEXT N_("Caching value in ms")
44 #define CACHING_LONGTEXT N_( \
45 "Caching value for FTP streams. This " \
46 "value should be set in milliseconds." )
47 #define USER_TEXT N_("FTP user name")
48 #define USER_LONGTEXT N_("User name that will " \
49 "be used for the connection.")
50 #define PASS_TEXT N_("FTP password")
51 #define PASS_LONGTEXT N_("Password that will be " \
52 "used for the connection.")
53 #define ACCOUNT_TEXT N_("FTP account")
54 #define ACCOUNT_LONGTEXT N_("Account that will be " \
55 "used for the connection.")
58 set_shortname( "FTP" );
59 set_description( _("FTP input") );
60 set_capability( "access2", 0 );
61 set_category( CAT_INPUT );
62 set_subcategory( SUBCAT_INPUT_ACCESS );
63 add_integer( "ftp-caching", 2 * DEFAULT_PTS_DELAY / 1000, NULL,
64 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
65 add_string( "ftp-user", "anonymous", NULL, USER_TEXT, USER_LONGTEXT,
67 add_string( "ftp-pwd", "anonymous@dummy.org", NULL, PASS_TEXT,
68 PASS_LONGTEXT, VLC_FALSE );
69 add_string( "ftp-account", "anonymous", NULL, ACCOUNT_TEXT,
70 ACCOUNT_LONGTEXT, VLC_FALSE );
71 add_shortcut( "ftp" );
72 set_callbacks( Open, Close );
75 /*****************************************************************************
77 *****************************************************************************/
78 static int Read( access_t *, uint8_t *, int );
79 static int Seek( access_t *, int64_t );
80 static int Control( access_t *, int, va_list );
89 char sz_epsv_ip[NI_MAXNUMERICHOST];
92 static int ftp_SendCommand( access_t *, char *, ... );
93 static int ftp_ReadCommand( access_t *, int *, char ** );
94 static int ftp_StartStream( access_t *, int64_t );
95 static int ftp_StopStream ( access_t *);
97 static int Connect( access_t *p_access, access_sys_t *p_sys )
102 /* *** Open a TCP connection with server *** */
103 msg_Dbg( p_access, "waiting for connection..." );
104 p_sys->fd_cmd = fd = net_ConnectTCP( p_access, p_sys->url.psz_host,
108 msg_Err( p_access, "failed to connect with server" );
109 intf_UserFatal( p_access, VLC_FALSE, _("Network interaction failed"),
110 _("VLC could not connect with the given server.") );
116 if( ftp_ReadCommand( p_access, &i_answer, NULL ) != 1 )
121 if( i_answer / 100 != 2 )
123 msg_Err( p_access, "connection rejected" );
124 intf_UserFatal( p_access, VLC_FALSE, _("Network interaction failed"),
125 _("VLC's connection to the given server was rejected.") );
129 msg_Dbg( p_access, "connection accepted (%d)", i_answer );
131 if( p_sys->url.psz_username && *p_sys->url.psz_username )
132 psz = strdup( p_sys->url.psz_username );
134 psz = var_CreateGetString( p_access, "ftp-user" );
136 if( ftp_SendCommand( p_access, "USER %s", psz ) < 0 ||
137 ftp_ReadCommand( p_access, &i_answer, NULL ) < 0 )
144 switch( i_answer / 100 )
147 msg_Dbg( p_access, "user accepted" );
150 msg_Dbg( p_access, "password needed" );
151 if( p_sys->url.psz_password && *p_sys->url.psz_password )
152 psz = strdup( p_sys->url.psz_password );
154 psz = var_CreateGetString( p_access, "ftp-pwd" );
156 if( ftp_SendCommand( p_access, "PASS %s", psz ) < 0 ||
157 ftp_ReadCommand( p_access, &i_answer, NULL ) < 0 )
164 switch( i_answer / 100 )
167 msg_Dbg( p_access, "password accepted" );
170 msg_Dbg( p_access, "account needed" );
171 psz = var_CreateGetString( p_access, "ftp-account" );
172 if( ftp_SendCommand( p_access, "ACCT %s",
174 ftp_ReadCommand( p_access, &i_answer, NULL ) < 0 )
181 if( i_answer / 100 != 2 )
183 msg_Err( p_access, "account rejected" );
184 intf_UserFatal( p_access, VLC_FALSE,
185 _("Network interaction failed"),
186 _("Your account was rejected.") );
189 msg_Dbg( p_access, "account accepted" );
193 msg_Err( p_access, "password rejected" );
194 intf_UserFatal( p_access, VLC_FALSE,
195 _("Network interaction failed"),
196 _("Your password was rejected.") );
201 msg_Err( p_access, "user rejected" );
202 intf_UserFatal( p_access, VLC_FALSE,
203 _("Network interaction failed"),
204 _("Your connection attemp to the server was rejected.") );
211 /****************************************************************************
212 * Open: connect to ftp server and ask for file
213 ****************************************************************************/
214 static int Open( vlc_object_t *p_this )
216 access_t *p_access = (access_t*)p_this;
224 STANDARD_READ_ACCESS_INIT
228 /* *** Parse URL and get server addr/port and path *** */
229 psz = p_access->psz_path;
234 vlc_UrlParse( &p_sys->url, psz, 0 );
236 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
238 msg_Err( p_access, "invalid server name" );
241 if( p_sys->url.i_port <= 0 )
243 p_sys->url.i_port = 21; /* default port */
246 /* FTP URLs are relative to user's default directory (RFC1738)
247 For absolute path use ftp://foo.bar//usr/local/etc/filename */
249 if( *p_sys->url.psz_path == '/' )
250 p_sys->url.psz_path++;
252 if( Connect( p_access, p_sys ) < 0 )
255 /* Extended passive mode */
256 if( ftp_SendCommand( p_access, "EPSV ALL" ) < 0 )
258 msg_Err( p_access, "cannot request extended passive mode" );
262 if( ftp_ReadCommand( p_access, &i_answer, NULL ) == 2 )
264 if( net_GetPeerAddress( p_sys->fd_cmd, p_sys->sz_epsv_ip, NULL ) )
269 /* If ESPV ALL fails, we fallback to PASV.
270 * We have to restart the connection in case there is a NAT that
271 * understands EPSV ALL in the way, and hence won't allow PASV on
272 * the initial connection.
274 net_Close( p_sys->fd_cmd );
276 *p_sys->sz_epsv_ip = '\0';
278 msg_Info( p_access, "FTP Extended passive mode disabled" );
280 if( ( p_sys->fd_cmd = Connect( p_access, p_sys ) ) < 0 )
285 if( ftp_SendCommand( p_access, "TYPE I" ) < 0 ||
286 ftp_ReadCommand( p_access, &i_answer, NULL ) != 2 )
288 msg_Err( p_access, "cannot set binary transfer mode" );
293 if( ftp_SendCommand( p_access, "SIZE %s", p_sys->url.psz_path ) < 0 ||
294 ftp_ReadCommand( p_access, &i_answer, &psz_arg ) != 2 )
296 msg_Err( p_access, "cannot get file size" );
299 p_access->info.i_size = atoll( &psz_arg[4] );
301 msg_Dbg( p_access, "file size: "I64Fd, p_access->info.i_size );
303 /* Start the 'stream' */
304 if( ftp_StartStream( p_access, 0 ) < 0 )
306 msg_Err( p_access, "cannot retrieve file" );
310 /* Update default_pts to a suitable value for ftp access */
311 var_Create( p_access, "ftp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
316 if( p_sys->fd_cmd >= 0 )
317 net_Close( p_sys->fd_cmd );
318 vlc_UrlClean( &p_sys->url );
323 /*****************************************************************************
324 * Close: free unused data structures
325 *****************************************************************************/
326 static void Close( vlc_object_t *p_this )
328 access_t *p_access = (access_t*)p_this;
329 access_sys_t *p_sys = p_access->p_sys;
331 msg_Dbg( p_access, "stopping stream" );
332 ftp_StopStream( p_access );
334 if( ftp_SendCommand( p_access, "QUIT" ) < 0 )
336 msg_Warn( p_access, "cannot quit" );
340 ftp_ReadCommand( p_access, NULL, NULL );
342 net_Close( p_sys->fd_cmd );
345 vlc_UrlClean( &p_sys->url );
349 /*****************************************************************************
350 * Seek: try to go at the right place
351 *****************************************************************************/
352 static int Seek( access_t *p_access, int64_t i_pos )
358 msg_Dbg( p_access, "seeking to "I64Fd, i_pos );
360 ftp_StopStream( p_access );
361 if( ftp_StartStream( p_access, i_pos ) < 0 )
363 p_access->info.b_eof = VLC_TRUE;
367 p_access->info.b_eof = VLC_FALSE;
368 p_access->info.i_pos = i_pos;
373 /*****************************************************************************
375 *****************************************************************************/
376 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
378 access_sys_t *p_sys = p_access->p_sys;
381 if( p_access->info.b_eof )
384 i_read = net_Read( p_access, p_sys->fd_data, NULL, p_buffer, i_len,
387 p_access->info.b_eof = VLC_TRUE;
388 else if( i_read > 0 )
389 p_access->info.i_pos += i_read;
394 /*****************************************************************************
396 *****************************************************************************/
397 static int Control( access_t *p_access, int i_query, va_list args )
407 case ACCESS_CAN_SEEK:
408 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
411 case ACCESS_CAN_FASTSEEK:
412 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
413 *pb_bool = VLC_FALSE;
415 case ACCESS_CAN_PAUSE:
416 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
417 *pb_bool = VLC_TRUE; /* FIXME */
419 case ACCESS_CAN_CONTROL_PACE:
420 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
421 *pb_bool = VLC_TRUE; /* FIXME */
426 pi_int = (int*)va_arg( args, int * );
430 case ACCESS_GET_PTS_DELAY:
431 pi_64 = (int64_t*)va_arg( args, int64_t * );
432 var_Get( p_access, "ftp-caching", &val );
433 *pi_64 = (int64_t)var_GetInteger( p_access, "ftp-caching" ) * I64C(1000);
437 case ACCESS_SET_PAUSE_STATE:
441 case ACCESS_GET_TITLE_INFO:
442 case ACCESS_SET_TITLE:
443 case ACCESS_SET_SEEKPOINT:
444 case ACCESS_SET_PRIVATE_ID_STATE:
448 msg_Warn( p_access, "unimplemented query in control" );
455 /*****************************************************************************
457 *****************************************************************************/
458 static int ftp_SendCommand( access_t *p_access, char *psz_fmt, ... )
460 access_sys_t *p_sys = p_access->p_sys;
464 va_start( args, psz_fmt );
465 vasprintf( &psz_cmd, psz_fmt, args );
468 msg_Dbg( p_access, "ftp_SendCommand:\"%s\"", psz_cmd);
469 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd_cmd, NULL, "%s\r\n",
472 msg_Err( p_access, "failed to send command" );
478 /* TODO support this s**t :
479 RFC 959 allows the client to send certain TELNET strings at any moment,
480 even in the middle of a request:
483 * \377\376x where x is one byte.
484 * \377\375x where x is one byte. The server is obliged to send \377\374x
485 * immediately after reading x.
486 * \377\374x where x is one byte.
487 * \377\373x where x is one byte. The server is obliged to send \377\376x
488 * immediately after reading x.
489 * \377x for any other byte x.
491 These strings are not part of the requests, except in the case \377\377,
492 where the request contains one \377. */
493 static int ftp_ReadCommand( access_t *p_access,
494 int *pi_answer, char **ppsz_answer )
496 access_sys_t *p_sys = p_access->p_sys;
500 psz_line = net_Gets( p_access, p_sys->fd_cmd, NULL );
501 msg_Dbg( p_access, "answer=%s", psz_line );
502 if( psz_line == NULL || strlen( psz_line ) < 3 )
504 msg_Err( p_access, "cannot get answer" );
505 if( psz_line ) free( psz_line );
506 if( pi_answer ) *pi_answer = 500;
507 if( ppsz_answer ) *ppsz_answer = NULL;
511 if( psz_line[3] == '-' ) /* Multiple response */
515 memcpy( end, psz_line, 3 );
520 char *psz_tmp = net_Gets( p_access, p_sys->fd_cmd, NULL );
522 if( psz_tmp == NULL ) /* Error */
525 if( !strncmp( psz_tmp, end, 4 ) )
534 i_answer = atoi( psz_line );
536 if( pi_answer ) *pi_answer = i_answer;
539 *ppsz_answer = psz_line;
545 return( i_answer / 100 );
548 static int ftp_StartStream( access_t *p_access, off_t i_start )
550 access_sys_t *p_sys = p_access->p_sys;
552 char psz_ipv4[16], *psz_ip;
554 char *psz_arg, *psz_parser;
557 psz_ip = p_sys->sz_epsv_ip;
559 if( ( ftp_SendCommand( p_access, *psz_ip ? "EPSV" : "PASV" ) < 0 )
560 || ( ftp_ReadCommand( p_access, &i_answer, &psz_arg ) != 2 ) )
562 msg_Err( p_access, "cannot set passive mode" );
566 psz_parser = strchr( psz_arg, '(' );
567 if( psz_parser == NULL )
570 msg_Err( p_access, "cannot parse passive mode response" );
576 char psz_fmt[7] = "(|||%u";
577 psz_fmt[1] = psz_fmt[2] = psz_fmt[3] = psz_parser[1];
579 if( sscanf( psz_parser, psz_fmt, &i_port ) < 1 )
582 msg_Err( p_access, "cannot parse passive mode response" );
588 unsigned a1, a2, a3, a4, p1, p2;
590 if( ( sscanf( psz_parser, "(%u,%u,%u,%u,%u,%u", &a1, &a2, &a3, &a4,
591 &p1, &p2 ) < 6 ) || ( a1 > 255 ) || ( a2 > 255 )
592 || ( a3 > 255 ) || ( a4 > 255 ) || ( p1 > 255 ) || ( p2 > 255 ) )
595 msg_Err( p_access, "cannot parse passive mode response" );
599 sprintf( psz_ipv4, "%u.%u.%u.%u", a1, a2, a3, a4 );
601 i_port = (p1 << 8) | p2;
605 msg_Dbg( p_access, "ip:%s port:%d", psz_ip, i_port );
607 if( ftp_SendCommand( p_access, "TYPE I" ) < 0 ||
608 ftp_ReadCommand( p_access, &i_answer, NULL ) != 2 )
610 msg_Err( p_access, "cannot set binary transfer mode" );
616 if( ftp_SendCommand( p_access, "REST "I64Fu, i_start ) < 0 ||
617 ftp_ReadCommand( p_access, &i_answer, NULL ) > 3 )
619 msg_Err( p_access, "cannot set restart point" );
624 msg_Dbg( p_access, "waiting for data connection..." );
625 p_sys->fd_data = net_ConnectTCP( p_access, psz_ip, i_port );
626 if( p_sys->fd_data < 0 )
628 msg_Err( p_access, "failed to connect with server" );
631 msg_Dbg( p_access, "connection with \"%s:%d\" successful",
635 if( ftp_SendCommand( p_access, "RETR %s", p_sys->url.psz_path ) < 0 ||
636 ftp_ReadCommand( p_access, &i_answer, NULL ) > 2 )
638 msg_Err( p_access, "cannot retreive file" );
644 static int ftp_StopStream ( access_t *p_access )
646 access_sys_t *p_sys = p_access->p_sys;
650 if( ftp_SendCommand( p_access, "ABOR" ) < 0 )
652 msg_Warn( p_access, "cannot abort file" );
653 if( p_sys->fd_data > 0 )
654 net_Close( p_sys->fd_data );
658 if( p_sys->fd_data > 0 )
660 net_Close( p_sys->fd_data );
662 ftp_ReadCommand( p_access, &i_answer, NULL );
664 ftp_ReadCommand( p_access, &i_answer, NULL );