1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: ftp.c,v 1.5 2002/12/31 01:54:35 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>
39 #elif defined( _MSC_VER ) && defined( _WIN32 )
44 # include <winsock2.h>
45 # include <ws2tcpip.h>
47 # define IN_MULTICAST(a) IN_CLASSD(a)
50 # include <sys/socket.h>
51 # include <netinet/in.h>
53 # include <arpa/inet.h>
54 # elif defined( SYS_BEOS )
55 # include <net/netdb.h>
61 /*****************************************************************************
63 *****************************************************************************/
64 static int Open ( vlc_object_t * );
65 static void Close ( vlc_object_t * );
67 static int Read ( input_thread_t * p_input, byte_t * p_buffer,
69 static void Seek ( input_thread_t *, off_t );
70 static int SetProgram ( input_thread_t *, pgrm_descriptor_t * );
73 static ssize_t NetRead ( input_thread_t *, input_socket_t *, byte_t *, size_t );
74 static void NetClose( input_thread_t *, input_socket_t *);
76 static int ftp_SendCommand( input_thread_t *, char *, ... );
77 static int ftp_ReadCommand( input_thread_t *, int *, char ** );
78 static int ftp_StartStream( input_thread_t *, off_t );
79 static int ftp_StopStream ( input_thread_t *);
81 /*****************************************************************************
83 *****************************************************************************/
84 #define CACHING_TEXT N_("caching value in ms")
85 #define CACHING_LONGTEXT N_( \
86 "Allows you to modify the default caching value for ftp streams. This " \
87 "value should be set in miliseconds units." )
90 set_description( _("ftp access module") );
91 set_capability( "access", 0 );
92 add_category_hint( "stream", NULL );
93 add_integer( "ftp-caching", 2 * DEFAULT_PTS_DELAY / 1000, NULL,
94 CACHING_TEXT, CACHING_LONGTEXT );
95 add_string( "ftp-user", "anonymous", NULL, "ftp user name", "ftp user name" );
96 add_string( "ftp-pwd", "anonymous@dummy.org", NULL, "ftp password", "ftp password, be careful with that option..." );
97 add_string( "ftp-account", "anonymous", NULL, "ftp account", "ftp account" );
98 add_shortcut( "ftp" );
99 set_callbacks( Open, Close );
102 /* url: [/]host[:port][/path] */
105 char *psz_server_addr;
117 static void ftp_ParseURL( url_t *, char * );
119 #define FREE( p ) if( p ) free( p )
121 typedef struct access_s
123 input_socket_t socket_cmd;
124 input_socket_t socket_data;
126 url_t url; /* connect to this server */
135 /****************************************************************************
136 ****************************************************************************
137 ******************* *******************
138 ******************* Main functions *******************
139 ******************* *******************
140 ****************************************************************************
141 ****************************************************************************/
143 /****************************************************************************
144 * Open: connect to ftp server and ask for file
145 ****************************************************************************/
146 static int Open( vlc_object_t *p_this )
148 input_thread_t *p_input = (input_thread_t*)p_this;
154 network_socket_t socket_desc;
158 char *psz_user, *psz_pwd, *psz_account;
162 /* *** allocate p_access_data *** */
163 p_input->p_access_data =
164 (void*)p_access = malloc( sizeof( access_t ) );
165 memset( p_access, 0, sizeof( access_t ) );
166 p_url = &p_access->url;
168 /* *** Parse URL and get server addr/port and path *** */
169 ftp_ParseURL( p_url, p_input->psz_name );
171 if( p_url->psz_server_addr == NULL ||
172 !( *p_url->psz_server_addr ) )
174 FREE( p_url->psz_private );
175 msg_Err( p_input, "invalid server name" );
178 if( p_url->i_server_port == 0 )
180 p_url->i_server_port = 21; /* default port */
183 /* 2: look at ip version ipv4/ipv6 */
185 if( config_GetInt( p_input, "ipv4" ) )
187 psz_network = "ipv4";
189 else if( config_GetInt( p_input, "ipv6" ) )
191 psz_network = "ipv6";
194 /* 3: Open a TCP connection with server *** */
195 msg_Dbg( p_input, "waiting for connection..." );
196 socket_desc.i_type = NETWORK_TCP;
197 socket_desc.psz_server_addr = p_url->psz_server_addr;
198 socket_desc.i_server_port = p_url->i_server_port;
199 socket_desc.psz_bind_addr = "";
200 socket_desc.i_bind_port = 0;
201 p_input->p_private = (void*)&socket_desc;
202 if( !( p_network = module_Need( p_input, "network", psz_network ) ) )
204 msg_Err( p_input, "failed to connect with server" );
205 FREE( p_access->url.psz_private );
206 FREE( p_input->p_access_data );
209 module_Unneed( p_input, p_network );
210 p_access->socket_cmd.i_handle = socket_desc.i_handle;
211 p_input->i_mtu = socket_desc.i_mtu;
213 "connection with \"%s:%d\" successful",
214 p_url->psz_server_addr,
215 p_url->i_server_port );
220 if( ftp_ReadCommand( p_input, &i_answer, NULL ) < 0)
222 msg_Err( p_input, "failed to get answer" );
225 if( i_answer / 100 != 1 )
231 if( i_answer / 100 != 2 )
233 msg_Err( p_input, "connection rejected" );
238 msg_Dbg( p_input, "connection accepted (%d)", i_answer );
241 psz_user = config_GetPsz( p_input, "ftp-user" );
242 if( ftp_SendCommand( p_input, "USER %s", psz_user ) < 0 )
249 if( ftp_ReadCommand( p_input, &i_answer, NULL ) < 0)
251 msg_Err( p_input, "failed to get answer" );
254 switch( i_answer / 100 )
257 msg_Dbg( p_input, "user accepted" );
260 msg_Dbg( p_input, "password needed" );
261 psz_pwd = config_GetPsz( p_input, "ftp-pwd" );
262 if( ftp_SendCommand( p_input, "PASS %s", psz_pwd ) < 0 )
268 if( ftp_ReadCommand( p_input, &i_answer, NULL ) < 0)
270 msg_Err( p_input, "failed to get answer" );
273 switch( i_answer / 100 )
276 msg_Dbg( p_input, "password accepted" );
279 msg_Dbg( p_input, "account needed" );
280 psz_account = config_GetPsz( p_input, "ftp-account" );
281 if( ftp_SendCommand( p_input, "ACCT %s", psz_account ) < 0 )
287 if( ftp_ReadCommand( p_input, &i_answer, NULL ) < 0)
289 msg_Err( p_input, "failed to get answer" );
292 if( i_answer / 100 != 2 )
294 msg_Err( p_input, "account rejected" );
299 msg_Dbg( p_input, "account accepted" );
303 msg_Err( p_input, "password rejected" );
308 msg_Err( p_input, "user rejected" );
312 if( ftp_SendCommand( p_input, "TYPE I" ) < 0 )
314 msg_Err( p_input, "cannot set binary transfert mode" );
317 if( ftp_ReadCommand( p_input, &i_answer, NULL ) != 2 )
319 msg_Err( p_input, "cannot set binary transfert mode" );
324 if( ftp_SendCommand( p_input, "SIZE %s", p_url->psz_path ) < 0 )
326 msg_Err( p_input, "cannot get file size" );
329 if( ftp_ReadCommand( p_input, &i_answer, &psz_arg ) != 2 )
331 msg_Err( p_input, "cannot get file size" );
336 p_access->i_filesize = atoll( psz_arg + 4 );
340 char *psz_parser = psz_arg + 4;
342 while( *psz_parser == ' ' ) psz_parser++;
344 while( psz_parser[0] >= '0' && psz_parser[0] <= '9' )
347 i_size += psz_parser[0] - '0';
349 p_access->i_filesize = i_size;
353 msg_Dbg( p_input, "file size: "I64Fd, p_access->i_filesize );
356 if( ftp_StartStream( p_input, 0 ) < 0 )
358 msg_Err( p_input, "cannot retrieve file" );
361 /* *** set exported functions *** */
362 p_input->pf_read = Read;
363 p_input->pf_seek = Seek;
364 p_input->pf_set_program = SetProgram;
365 p_input->pf_set_area = NULL;
367 p_input->p_private = NULL;
369 /* *** finished to set some variable *** */
370 vlc_mutex_lock( &p_input->stream.stream_lock );
371 p_input->stream.b_pace_control = 1;
372 p_input->stream.p_selected_area->i_tell = 0;
373 p_input->stream.b_seekable = 1;
374 p_input->stream.b_connected = 1;
375 p_input->stream.p_selected_area->i_size = p_access->i_filesize;
376 p_input->stream.i_method = INPUT_METHOD_NETWORK;
377 vlc_mutex_unlock( &p_input->stream.stream_lock );
379 /* Update default_pts to a suitable value for ftp access */
380 p_input->i_pts_delay = config_GetInt( p_input, "ftp-caching" ) * 1000;
385 NetClose( p_input, &p_access->socket_cmd );
386 FREE( p_access->url.psz_private );
387 FREE( p_input->p_access_data );
391 /*****************************************************************************
392 * Close: free unused data structures
393 *****************************************************************************/
394 static void Close( vlc_object_t *p_this )
396 input_thread_t *p_input = (input_thread_t *)p_this;
397 access_t *p_access = (access_t*)p_input->p_access_data;
399 msg_Dbg( p_input, "stopping stream" );
400 ftp_StopStream( p_input );
402 if( ftp_SendCommand( p_input, "QUIT" ) < 0 )
404 msg_Err( p_input, "cannot quit" );
408 ftp_ReadCommand( p_input, NULL, NULL );
412 NetClose( p_input, &p_access->socket_cmd );
415 FREE( p_access->url.psz_private );
418 /*****************************************************************************
419 * SetProgram: do nothing
420 *****************************************************************************/
421 static int SetProgram( input_thread_t * p_input,
422 pgrm_descriptor_t * p_program )
427 /*****************************************************************************
428 * Seek: try to go at the right place
429 *****************************************************************************/
430 static void Seek( input_thread_t * p_input, off_t i_pos )
432 //access_t *p_access = (access_t*)p_input->p_access_data;
437 vlc_mutex_lock( &p_input->stream.stream_lock );
439 msg_Dbg( p_input, "seeking to "I64Fd, i_pos );
441 ftp_StopStream( p_input );
442 ftp_StartStream( p_input, i_pos );
444 p_input->stream.p_selected_area->i_tell = i_pos;
445 vlc_mutex_unlock( &p_input->stream.stream_lock );
448 static int Read ( input_thread_t * p_input, byte_t * p_buffer,
451 access_t *p_access = (access_t*)p_input->p_access_data;
454 i_data = NetRead( p_input, &p_access->socket_data, p_buffer, i_len );
459 static int ftp_SendCommand( input_thread_t *p_input, char *psz_fmt, ... )
461 access_t *p_access = (access_t*)p_input->p_access_data;
464 #if !defined(HAVE_VASPRINTF) || defined(SYS_DARWIN)
468 va_start( args, psz_fmt );
470 #if defined(HAVE_VASPRINTF) && !defined(SYS_DARWIN)
471 vasprintf( &psz_buffer, psz_fmt, args );
473 i_size = strlen( psz_fmt ) + 2048;
474 psz_buffer = calloc( i_size, sizeof( char ) );
475 vsnprintf( psz_buffer, i_size, psz_fmt, args );
476 psz_buffer[i_size - 1] = 0;
478 if( !strncmp( psz_buffer, "PASS", 4 ) )
480 msg_Dbg( p_input, "ftp_SendCommand:\"PASS xxx\"" );
484 msg_Dbg( p_input, "ftp_SendCommand:\"%s\"", psz_buffer );
486 psz_buffer = realloc( psz_buffer, strlen( psz_buffer ) + 3 );
487 strcat( psz_buffer, "\r\n" );
488 if( send( p_access->socket_cmd.i_handle,
490 strlen( psz_buffer ),
494 msg_Err( p_input, "failed to send command" );
504 #define BLOCK_SIZE 1024
505 /* TODO support this s**t :
506 RFC 959 allows the client to send certain TELNET strings at any moment,
507 even in the middle of a request:
510 * \377\376x where x is one byte.
511 * \377\375x where x is one byte. The server is obliged to send \377\374x
512 * immediately after reading x.
513 * \377\374x where x is one byte.
514 * \377\373x where x is one byte. The server is obliged to send \377\376x
515 * immediately after reading x.
516 * \377x for any other byte x.
518 These strings are not part of the requests, except in the case \377\377,
519 where the request contains one \377. */
521 static int ftp_ReadCommand( input_thread_t *p_input,
522 int *pi_answer, char **ppsz_answer )
524 access_t *p_access = (access_t*)p_input->p_access_data;
532 i_buffer_size = BLOCK_SIZE + 1;
533 p_buffer = malloc( BLOCK_SIZE + 1);
538 i_read = NetRead( p_input, &p_access->socket_cmd,
539 p_buffer + i_buffer, BLOCK_SIZE );
540 if( i_read <= 0 || p_input->b_die || p_input->b_error )
543 if( pi_answer ) *pi_answer = 500;
544 if( ppsz_answer ) *ppsz_answer = NULL;
552 if( i_read < BLOCK_SIZE )
554 p_buffer[i_buffer] = '\0';
557 i_buffer_size += BLOCK_SIZE;
558 p_buffer = realloc( p_buffer, i_buffer_size );
566 i_answer = atoi( p_buffer );
568 if( pi_answer ) *pi_answer = i_answer;
571 *ppsz_answer = p_buffer;
577 return( i_answer / 100 );
581 if( pi_answer ) *pi_answer = 500;
582 if( ppsz_answer ) *ppsz_answer = NULL;
586 static int ftp_StartStream( input_thread_t *p_input, off_t i_start )
588 access_t *p_access = (access_t*)p_input->p_access_data;
592 char *psz_arg, *psz_parser;
597 network_socket_t socket_desc;
599 if( ftp_SendCommand( p_input, "PASV" ) < 0 )
601 msg_Err( p_input, "cannot set passive transfert mode" );
604 if( ftp_ReadCommand( p_input, &i_answer, &psz_arg ) != 2 )
606 msg_Err( p_input, "cannot set passive transfert mode" );
609 psz_parser = strchr( psz_arg, '(' );
610 if( !psz_parser || sscanf( psz_parser, "(%d,%d,%d,%d,%d,%d", &a1, &a2, &a3, &a4, &p1, &p2 ) < 6 )
613 msg_Err( p_input, "cannot get ip/port for passive transfert mode" );
618 sprintf( psz_ip, "%d.%d.%d.%d", a1, a2, a3, a4 );
619 i_port = p1 * 256 + p2;
620 msg_Dbg( p_input, "ip:%s port:%d", psz_ip, i_port );
622 if( ftp_SendCommand( p_input, "TYPE I" ) < 0 )
624 msg_Err( p_input, "cannot set binary transfert mode" );
627 if( ftp_ReadCommand( p_input, &i_answer, NULL ) != 2 )
629 msg_Err( p_input, "cannot set binary transfert mode" );
636 if( ftp_SendCommand( p_input, "REST "I64Fu, i_start ) < 0 )
638 msg_Err( p_input, "cannot set restart point" );
641 if( ftp_ReadCommand( p_input, &i_answer, NULL ) > 3 )
643 msg_Err( p_input, "cannot set restart point" );
648 msg_Dbg( p_input, "waiting for data connection..." );
649 socket_desc.i_type = NETWORK_TCP;
650 socket_desc.psz_server_addr = psz_ip;
651 socket_desc.i_server_port = i_port;
652 socket_desc.psz_bind_addr = "";
653 socket_desc.i_bind_port = 0;
654 p_input->p_private = (void*)&socket_desc;
655 if( !( p_network = module_Need( p_input, "network", "" ) ) )
657 msg_Err( p_input, "failed to connect with server" );
660 module_Unneed( p_input, p_network );
661 p_access->socket_data.i_handle = socket_desc.i_handle;
662 p_input->i_mtu = socket_desc.i_mtu;
664 "connection with \"%s:%d\" successful",
667 if( ftp_SendCommand( p_input, "RETR %s", p_access->url.psz_path ) < 0 )
669 msg_Err( p_input, "cannot retreive file" );
673 if( ftp_ReadCommand( p_input, &i_answer, NULL ) > 2 )
675 msg_Err( p_input, "cannot retreive file" );
682 static int ftp_StopStream ( input_thread_t *p_input)
684 access_t *p_access = (access_t*)p_input->p_access_data;
688 NetClose( p_input, &p_access->socket_data );
690 if( ftp_SendCommand( p_input, "ABOR" ) < 0 )
692 msg_Err( p_input, "cannot abord file" );
696 ftp_ReadCommand( p_input, &i_answer, NULL );
697 ftp_ReadCommand( p_input, &i_answer, NULL );
703 /****************************************************************************
705 ****************************************************************************/
706 static void ftp_ParseURL( url_t *p_url, char *psz_url )
709 char *psz_server_port;
711 p_url->psz_private = strdup( psz_url );
713 psz_parser = p_url->psz_private;
715 while( *psz_parser == '/' )
719 p_url->psz_server_addr = psz_parser;
721 while( *psz_parser &&
722 *psz_parser != ':' && *psz_parser != '/' && *psz_parser != '@' )
727 if( *psz_parser == ':' )
731 psz_server_port = psz_parser;
733 while( *psz_parser && *psz_parser != '/' )
740 psz_server_port = "";
743 if( *psz_parser == '@' )
750 p_url->psz_bind_addr = psz_parser;
752 while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
757 if( *psz_parser == ':' )
761 psz_bind_port = psz_parser;
763 while( *psz_parser && *psz_parser != '/' )
774 p_url->i_bind_port = strtol( psz_bind_port, &psz_parser, 10 );
778 p_url->i_bind_port = 0;
783 p_url->psz_bind_addr = "";
784 p_url->i_bind_port = 0;
787 if( *psz_parser == '/' )
791 p_url->psz_path = psz_parser;
794 if( *psz_server_port )
796 p_url->i_server_port = strtol( psz_server_port, &psz_parser, 10 );
800 p_url->i_server_port = 0;
804 /*****************************************************************************
805 * Read: read on a file descriptor, checking b_die periodically
806 *****************************************************************************/
807 static ssize_t NetRead( input_thread_t *p_input,
808 input_socket_t *p_socket,
809 byte_t *p_buffer, size_t i_len )
815 struct timeval timeout;
819 /* Initialize file descriptor set */
821 FD_SET( p_socket->i_handle, &fds );
823 /* We'll wait 1 second if nothing happens */
825 timeout.tv_usec = 1000000;
827 /* Find if some data is available */
828 i_ret = select( p_socket->i_handle + 1, &fds,
829 NULL, NULL, &timeout );
831 if( i_ret == -1 && errno != EINTR )
833 msg_Err( p_input, "network select error (%s)", strerror(errno) );
837 ssize_t i_recv = recv( p_socket->i_handle, p_buffer, i_len, 0 );
841 msg_Err( p_input, "recv failed (%s)", strerror(errno) );
852 static void NetClose( input_thread_t *p_input, input_socket_t *p_socket )
854 #if defined( UNDER_CE )
855 CloseHandle( (HANDLE)p_socket->i_handle );
856 #elif defined( WIN32 )
857 closesocket( p_socket->i_handle );
859 close( p_socket->i_handle );