1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: ftp.c,v 1.6 2003/02/04 10:07:40 massiot Exp $
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 *****************************************************************************/
28 #include <sys/types.h>
35 #include <vlc/input.h>
37 #ifdef HAVE_SYS_TIME_H
38 # include <sys/time.h>
43 #elif defined( _MSC_VER ) && defined( _WIN32 )
48 # include <winsock2.h>
49 # include <ws2tcpip.h>
51 # define IN_MULTICAST(a) IN_CLASSD(a)
54 # include <sys/socket.h>
55 # include <netinet/in.h>
57 # include <arpa/inet.h>
58 # elif defined( SYS_BEOS )
59 # include <net/netdb.h>
65 /*****************************************************************************
67 *****************************************************************************/
68 static int Open ( vlc_object_t * );
69 static void Close ( vlc_object_t * );
71 static int Read ( input_thread_t * p_input, byte_t * p_buffer,
73 static void Seek ( input_thread_t *, off_t );
74 static int SetProgram ( input_thread_t *, pgrm_descriptor_t * );
77 static ssize_t NetRead ( input_thread_t *, input_socket_t *, byte_t *, size_t );
78 static void NetClose( input_thread_t *, input_socket_t *);
80 static int ftp_SendCommand( input_thread_t *, char *, ... );
81 static int ftp_ReadCommand( input_thread_t *, int *, char ** );
82 static int ftp_StartStream( input_thread_t *, off_t );
83 static int ftp_StopStream ( input_thread_t *);
85 /*****************************************************************************
87 *****************************************************************************/
88 #define CACHING_TEXT N_("caching value in ms")
89 #define CACHING_LONGTEXT N_( \
90 "Allows you to modify the default caching value for ftp streams. This " \
91 "value should be set in miliseconds units." )
94 set_description( _("ftp access module") );
95 set_capability( "access", 0 );
96 add_category_hint( "stream", NULL );
97 add_integer( "ftp-caching", 2 * DEFAULT_PTS_DELAY / 1000, NULL,
98 CACHING_TEXT, CACHING_LONGTEXT );
99 add_string( "ftp-user", "anonymous", NULL, "ftp user name", "ftp user name" );
100 add_string( "ftp-pwd", "anonymous@dummy.org", NULL, "ftp password", "ftp password, be careful with that option..." );
101 add_string( "ftp-account", "anonymous", NULL, "ftp account", "ftp account" );
102 add_shortcut( "ftp" );
103 set_callbacks( Open, Close );
106 /* url: [/]host[:port][/path] */
109 char *psz_server_addr;
121 static void ftp_ParseURL( url_t *, char * );
123 #define FREE( p ) if( p ) free( p )
125 typedef struct access_s
127 input_socket_t socket_cmd;
128 input_socket_t socket_data;
130 url_t url; /* connect to this server */
139 /****************************************************************************
140 ****************************************************************************
141 ******************* *******************
142 ******************* Main functions *******************
143 ******************* *******************
144 ****************************************************************************
145 ****************************************************************************/
147 /****************************************************************************
148 * Open: connect to ftp server and ask for file
149 ****************************************************************************/
150 static int Open( vlc_object_t *p_this )
152 input_thread_t *p_input = (input_thread_t*)p_this;
158 network_socket_t socket_desc;
162 char *psz_user, *psz_pwd, *psz_account;
166 /* *** allocate p_access_data *** */
167 p_input->p_access_data =
168 (void*)p_access = malloc( sizeof( access_t ) );
169 memset( p_access, 0, sizeof( access_t ) );
170 p_url = &p_access->url;
172 /* *** Parse URL and get server addr/port and path *** */
173 ftp_ParseURL( p_url, p_input->psz_name );
175 if( p_url->psz_server_addr == NULL ||
176 !( *p_url->psz_server_addr ) )
178 FREE( p_url->psz_private );
179 msg_Err( p_input, "invalid server name" );
182 if( p_url->i_server_port == 0 )
184 p_url->i_server_port = 21; /* default port */
187 /* 2: look at ip version ipv4/ipv6 */
189 if( config_GetInt( p_input, "ipv4" ) )
191 psz_network = "ipv4";
193 else if( config_GetInt( p_input, "ipv6" ) )
195 psz_network = "ipv6";
198 /* 3: Open a TCP connection with server *** */
199 msg_Dbg( p_input, "waiting for connection..." );
200 socket_desc.i_type = NETWORK_TCP;
201 socket_desc.psz_server_addr = p_url->psz_server_addr;
202 socket_desc.i_server_port = p_url->i_server_port;
203 socket_desc.psz_bind_addr = "";
204 socket_desc.i_bind_port = 0;
205 p_input->p_private = (void*)&socket_desc;
206 if( !( p_network = module_Need( p_input, "network", psz_network ) ) )
208 msg_Err( p_input, "failed to connect with server" );
209 FREE( p_access->url.psz_private );
210 FREE( p_input->p_access_data );
213 module_Unneed( p_input, p_network );
214 p_access->socket_cmd.i_handle = socket_desc.i_handle;
215 p_input->i_mtu = socket_desc.i_mtu;
217 "connection with \"%s:%d\" successful",
218 p_url->psz_server_addr,
219 p_url->i_server_port );
224 if( ftp_ReadCommand( p_input, &i_answer, NULL ) < 0)
226 msg_Err( p_input, "failed to get answer" );
229 if( i_answer / 100 != 1 )
235 if( i_answer / 100 != 2 )
237 msg_Err( p_input, "connection rejected" );
242 msg_Dbg( p_input, "connection accepted (%d)", i_answer );
245 psz_user = config_GetPsz( p_input, "ftp-user" );
246 if( ftp_SendCommand( p_input, "USER %s", psz_user ) < 0 )
253 if( ftp_ReadCommand( p_input, &i_answer, NULL ) < 0)
255 msg_Err( p_input, "failed to get answer" );
258 switch( i_answer / 100 )
261 msg_Dbg( p_input, "user accepted" );
264 msg_Dbg( p_input, "password needed" );
265 psz_pwd = config_GetPsz( p_input, "ftp-pwd" );
266 if( ftp_SendCommand( p_input, "PASS %s", psz_pwd ) < 0 )
272 if( ftp_ReadCommand( p_input, &i_answer, NULL ) < 0)
274 msg_Err( p_input, "failed to get answer" );
277 switch( i_answer / 100 )
280 msg_Dbg( p_input, "password accepted" );
283 msg_Dbg( p_input, "account needed" );
284 psz_account = config_GetPsz( p_input, "ftp-account" );
285 if( ftp_SendCommand( p_input, "ACCT %s", psz_account ) < 0 )
291 if( ftp_ReadCommand( p_input, &i_answer, NULL ) < 0)
293 msg_Err( p_input, "failed to get answer" );
296 if( i_answer / 100 != 2 )
298 msg_Err( p_input, "account rejected" );
303 msg_Dbg( p_input, "account accepted" );
307 msg_Err( p_input, "password rejected" );
312 msg_Err( p_input, "user rejected" );
316 if( ftp_SendCommand( p_input, "TYPE I" ) < 0 )
318 msg_Err( p_input, "cannot set binary transfert mode" );
321 if( ftp_ReadCommand( p_input, &i_answer, NULL ) != 2 )
323 msg_Err( p_input, "cannot set binary transfert mode" );
328 if( ftp_SendCommand( p_input, "SIZE %s", p_url->psz_path ) < 0 )
330 msg_Err( p_input, "cannot get file size" );
333 if( ftp_ReadCommand( p_input, &i_answer, &psz_arg ) != 2 )
335 msg_Err( p_input, "cannot get file size" );
340 p_access->i_filesize = atoll( psz_arg + 4 );
344 char *psz_parser = psz_arg + 4;
346 while( *psz_parser == ' ' ) psz_parser++;
348 while( psz_parser[0] >= '0' && psz_parser[0] <= '9' )
351 i_size += psz_parser[0] - '0';
353 p_access->i_filesize = i_size;
357 msg_Dbg( p_input, "file size: "I64Fd, p_access->i_filesize );
360 if( ftp_StartStream( p_input, 0 ) < 0 )
362 msg_Err( p_input, "cannot retrieve file" );
365 /* *** set exported functions *** */
366 p_input->pf_read = Read;
367 p_input->pf_seek = Seek;
368 p_input->pf_set_program = SetProgram;
369 p_input->pf_set_area = NULL;
371 p_input->p_private = NULL;
373 /* *** finished to set some variable *** */
374 vlc_mutex_lock( &p_input->stream.stream_lock );
375 p_input->stream.b_pace_control = 1;
376 p_input->stream.p_selected_area->i_tell = 0;
377 p_input->stream.b_seekable = 1;
378 p_input->stream.b_connected = 1;
379 p_input->stream.p_selected_area->i_size = p_access->i_filesize;
380 p_input->stream.i_method = INPUT_METHOD_NETWORK;
381 vlc_mutex_unlock( &p_input->stream.stream_lock );
383 /* Update default_pts to a suitable value for ftp access */
384 p_input->i_pts_delay = config_GetInt( p_input, "ftp-caching" ) * 1000;
389 NetClose( p_input, &p_access->socket_cmd );
390 FREE( p_access->url.psz_private );
391 FREE( p_input->p_access_data );
395 /*****************************************************************************
396 * Close: free unused data structures
397 *****************************************************************************/
398 static void Close( vlc_object_t *p_this )
400 input_thread_t *p_input = (input_thread_t *)p_this;
401 access_t *p_access = (access_t*)p_input->p_access_data;
403 msg_Dbg( p_input, "stopping stream" );
404 ftp_StopStream( p_input );
406 if( ftp_SendCommand( p_input, "QUIT" ) < 0 )
408 msg_Err( p_input, "cannot quit" );
412 ftp_ReadCommand( p_input, NULL, NULL );
416 NetClose( p_input, &p_access->socket_cmd );
419 FREE( p_access->url.psz_private );
422 /*****************************************************************************
423 * SetProgram: do nothing
424 *****************************************************************************/
425 static int SetProgram( input_thread_t * p_input,
426 pgrm_descriptor_t * p_program )
431 /*****************************************************************************
432 * Seek: try to go at the right place
433 *****************************************************************************/
434 static void Seek( input_thread_t * p_input, off_t i_pos )
436 //access_t *p_access = (access_t*)p_input->p_access_data;
441 vlc_mutex_lock( &p_input->stream.stream_lock );
443 msg_Dbg( p_input, "seeking to "I64Fd, i_pos );
445 ftp_StopStream( p_input );
446 ftp_StartStream( p_input, i_pos );
448 p_input->stream.p_selected_area->i_tell = i_pos;
449 vlc_mutex_unlock( &p_input->stream.stream_lock );
452 static int Read ( input_thread_t * p_input, byte_t * p_buffer,
455 access_t *p_access = (access_t*)p_input->p_access_data;
458 i_data = NetRead( p_input, &p_access->socket_data, p_buffer, i_len );
463 static int ftp_SendCommand( input_thread_t *p_input, char *psz_fmt, ... )
465 access_t *p_access = (access_t*)p_input->p_access_data;
468 #if !defined(HAVE_VASPRINTF) || defined(SYS_DARWIN)
472 va_start( args, psz_fmt );
474 #if defined(HAVE_VASPRINTF) && !defined(SYS_DARWIN)
475 vasprintf( &psz_buffer, psz_fmt, args );
477 i_size = strlen( psz_fmt ) + 2048;
478 psz_buffer = calloc( i_size, sizeof( char ) );
479 vsnprintf( psz_buffer, i_size, psz_fmt, args );
480 psz_buffer[i_size - 1] = 0;
482 if( !strncmp( psz_buffer, "PASS", 4 ) )
484 msg_Dbg( p_input, "ftp_SendCommand:\"PASS xxx\"" );
488 msg_Dbg( p_input, "ftp_SendCommand:\"%s\"", psz_buffer );
490 psz_buffer = realloc( psz_buffer, strlen( psz_buffer ) + 3 );
491 strcat( psz_buffer, "\r\n" );
492 if( send( p_access->socket_cmd.i_handle,
494 strlen( psz_buffer ),
498 msg_Err( p_input, "failed to send command" );
508 #define BLOCK_SIZE 1024
509 /* TODO support this s**t :
510 RFC 959 allows the client to send certain TELNET strings at any moment,
511 even in the middle of a request:
514 * \377\376x where x is one byte.
515 * \377\375x where x is one byte. The server is obliged to send \377\374x
516 * immediately after reading x.
517 * \377\374x where x is one byte.
518 * \377\373x where x is one byte. The server is obliged to send \377\376x
519 * immediately after reading x.
520 * \377x for any other byte x.
522 These strings are not part of the requests, except in the case \377\377,
523 where the request contains one \377. */
525 static int ftp_ReadCommand( input_thread_t *p_input,
526 int *pi_answer, char **ppsz_answer )
528 access_t *p_access = (access_t*)p_input->p_access_data;
536 i_buffer_size = BLOCK_SIZE + 1;
537 p_buffer = malloc( BLOCK_SIZE + 1);
542 i_read = NetRead( p_input, &p_access->socket_cmd,
543 p_buffer + i_buffer, BLOCK_SIZE );
544 if( i_read <= 0 || p_input->b_die || p_input->b_error )
547 if( pi_answer ) *pi_answer = 500;
548 if( ppsz_answer ) *ppsz_answer = NULL;
556 if( i_read < BLOCK_SIZE )
558 p_buffer[i_buffer] = '\0';
561 i_buffer_size += BLOCK_SIZE;
562 p_buffer = realloc( p_buffer, i_buffer_size );
570 i_answer = atoi( p_buffer );
572 if( pi_answer ) *pi_answer = i_answer;
575 *ppsz_answer = p_buffer;
581 return( i_answer / 100 );
585 if( pi_answer ) *pi_answer = 500;
586 if( ppsz_answer ) *ppsz_answer = NULL;
590 static int ftp_StartStream( input_thread_t *p_input, off_t i_start )
592 access_t *p_access = (access_t*)p_input->p_access_data;
596 char *psz_arg, *psz_parser;
601 network_socket_t socket_desc;
603 if( ftp_SendCommand( p_input, "PASV" ) < 0 )
605 msg_Err( p_input, "cannot set passive transfert mode" );
608 if( ftp_ReadCommand( p_input, &i_answer, &psz_arg ) != 2 )
610 msg_Err( p_input, "cannot set passive transfert mode" );
613 psz_parser = strchr( psz_arg, '(' );
614 if( !psz_parser || sscanf( psz_parser, "(%d,%d,%d,%d,%d,%d", &a1, &a2, &a3, &a4, &p1, &p2 ) < 6 )
617 msg_Err( p_input, "cannot get ip/port for passive transfert mode" );
622 sprintf( psz_ip, "%d.%d.%d.%d", a1, a2, a3, a4 );
623 i_port = p1 * 256 + p2;
624 msg_Dbg( p_input, "ip:%s port:%d", psz_ip, i_port );
626 if( ftp_SendCommand( p_input, "TYPE I" ) < 0 )
628 msg_Err( p_input, "cannot set binary transfert mode" );
631 if( ftp_ReadCommand( p_input, &i_answer, NULL ) != 2 )
633 msg_Err( p_input, "cannot set binary transfert mode" );
640 if( ftp_SendCommand( p_input, "REST "I64Fu, i_start ) < 0 )
642 msg_Err( p_input, "cannot set restart point" );
645 if( ftp_ReadCommand( p_input, &i_answer, NULL ) > 3 )
647 msg_Err( p_input, "cannot set restart point" );
652 msg_Dbg( p_input, "waiting for data connection..." );
653 socket_desc.i_type = NETWORK_TCP;
654 socket_desc.psz_server_addr = psz_ip;
655 socket_desc.i_server_port = i_port;
656 socket_desc.psz_bind_addr = "";
657 socket_desc.i_bind_port = 0;
658 p_input->p_private = (void*)&socket_desc;
659 if( !( p_network = module_Need( p_input, "network", "" ) ) )
661 msg_Err( p_input, "failed to connect with server" );
664 module_Unneed( p_input, p_network );
665 p_access->socket_data.i_handle = socket_desc.i_handle;
666 p_input->i_mtu = socket_desc.i_mtu;
668 "connection with \"%s:%d\" successful",
671 if( ftp_SendCommand( p_input, "RETR %s", p_access->url.psz_path ) < 0 )
673 msg_Err( p_input, "cannot retreive file" );
677 if( ftp_ReadCommand( p_input, &i_answer, NULL ) > 2 )
679 msg_Err( p_input, "cannot retreive file" );
686 static int ftp_StopStream ( input_thread_t *p_input)
688 access_t *p_access = (access_t*)p_input->p_access_data;
692 NetClose( p_input, &p_access->socket_data );
694 if( ftp_SendCommand( p_input, "ABOR" ) < 0 )
696 msg_Err( p_input, "cannot abord file" );
700 ftp_ReadCommand( p_input, &i_answer, NULL );
701 ftp_ReadCommand( p_input, &i_answer, NULL );
707 /****************************************************************************
709 ****************************************************************************/
710 static void ftp_ParseURL( url_t *p_url, char *psz_url )
713 char *psz_server_port;
715 p_url->psz_private = strdup( psz_url );
717 psz_parser = p_url->psz_private;
719 while( *psz_parser == '/' )
723 p_url->psz_server_addr = psz_parser;
725 while( *psz_parser &&
726 *psz_parser != ':' && *psz_parser != '/' && *psz_parser != '@' )
731 if( *psz_parser == ':' )
735 psz_server_port = psz_parser;
737 while( *psz_parser && *psz_parser != '/' )
744 psz_server_port = "";
747 if( *psz_parser == '@' )
754 p_url->psz_bind_addr = psz_parser;
756 while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
761 if( *psz_parser == ':' )
765 psz_bind_port = psz_parser;
767 while( *psz_parser && *psz_parser != '/' )
778 p_url->i_bind_port = strtol( psz_bind_port, &psz_parser, 10 );
782 p_url->i_bind_port = 0;
787 p_url->psz_bind_addr = "";
788 p_url->i_bind_port = 0;
791 if( *psz_parser == '/' )
795 p_url->psz_path = psz_parser;
798 if( *psz_server_port )
800 p_url->i_server_port = strtol( psz_server_port, &psz_parser, 10 );
804 p_url->i_server_port = 0;
808 /*****************************************************************************
809 * Read: read on a file descriptor, checking b_die periodically
810 *****************************************************************************/
811 static ssize_t NetRead( input_thread_t *p_input,
812 input_socket_t *p_socket,
813 byte_t *p_buffer, size_t i_len )
819 struct timeval timeout;
823 /* Initialize file descriptor set */
825 FD_SET( p_socket->i_handle, &fds );
827 /* We'll wait 1 second if nothing happens */
829 timeout.tv_usec = 1000000;
831 /* Find if some data is available */
832 i_ret = select( p_socket->i_handle + 1, &fds,
833 NULL, NULL, &timeout );
835 if( i_ret == -1 && errno != EINTR )
837 msg_Err( p_input, "network select error (%s)", strerror(errno) );
841 ssize_t i_recv = recv( p_socket->i_handle, p_buffer, i_len, 0 );
845 msg_Err( p_input, "recv failed (%s)", strerror(errno) );
856 static void NetClose( input_thread_t *p_input, input_socket_t *p_socket )
858 #if defined( UNDER_CE )
859 CloseHandle( (HANDLE)p_socket->i_handle );
860 #elif defined( WIN32 )
861 closesocket( p_socket->i_handle );
863 close( p_socket->i_handle );