1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: ftp.c,v 1.8 2003/02/20 01:52:45 sigmunau 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>
36 #include <vlc/input.h>
38 #ifdef HAVE_SYS_TIME_H
39 # include <sys/time.h>
44 #elif defined( _MSC_VER ) && defined( _WIN32 )
49 # include <winsock2.h>
50 # include <ws2tcpip.h>
52 # define IN_MULTICAST(a) IN_CLASSD(a)
55 # include <sys/socket.h>
56 # include <netinet/in.h>
58 # include <arpa/inet.h>
59 # elif defined( SYS_BEOS )
60 # include <net/netdb.h>
66 /*****************************************************************************
68 *****************************************************************************/
69 static int Open ( vlc_object_t * );
70 static void Close ( vlc_object_t * );
72 static int Read ( input_thread_t * p_input, byte_t * p_buffer,
74 static void Seek ( input_thread_t *, off_t );
75 static int SetProgram ( input_thread_t *, pgrm_descriptor_t * );
78 static ssize_t NetRead ( input_thread_t *, input_socket_t *, byte_t *, size_t );
79 static void NetClose( input_thread_t *, input_socket_t *);
81 static int ftp_SendCommand( input_thread_t *, char *, ... );
82 static int ftp_ReadCommand( input_thread_t *, int *, char ** );
83 static int ftp_StartStream( input_thread_t *, off_t );
84 static int ftp_StopStream ( input_thread_t *);
86 /*****************************************************************************
88 *****************************************************************************/
89 #define CACHING_TEXT N_("caching value in ms")
90 #define CACHING_LONGTEXT N_( \
91 "Allows you to modify the default caching value for ftp streams. This " \
92 "value should be set in miliseconds units." )
95 set_description( _("ftp access module") );
96 set_capability( "access", 0 );
97 add_category_hint( "stream", NULL, VLC_FALSE );
98 add_integer( "ftp-caching", 2 * DEFAULT_PTS_DELAY / 1000, NULL,
99 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
100 add_string( "ftp-user", "anonymous", NULL, "ftp user name", "ftp user name", VLC_FALSE );
101 add_string( "ftp-pwd", "anonymous@dummy.org", NULL, "ftp password", "ftp password, be careful with that option...", VLC_FALSE );
102 add_string( "ftp-account", "anonymous", NULL, "ftp account", "ftp account", VLC_FALSE );
103 add_shortcut( "ftp" );
104 set_callbacks( Open, Close );
107 /* url: [/]host[:port][/path] */
110 char *psz_server_addr;
122 static void ftp_ParseURL( url_t *, char * );
124 #define FREE( p ) if( p ) free( p )
126 typedef struct access_s
128 input_socket_t socket_cmd;
129 input_socket_t socket_data;
131 url_t url; /* connect to this server */
140 /****************************************************************************
141 ****************************************************************************
142 ******************* *******************
143 ******************* Main functions *******************
144 ******************* *******************
145 ****************************************************************************
146 ****************************************************************************/
148 /****************************************************************************
149 * Open: connect to ftp server and ask for file
150 ****************************************************************************/
151 static int Open( vlc_object_t *p_this )
153 input_thread_t *p_input = (input_thread_t*)p_this;
159 network_socket_t socket_desc;
163 char *psz_user, *psz_pwd, *psz_account;
167 /* *** allocate p_access_data *** */
168 p_input->p_access_data =
169 (void*)p_access = malloc( sizeof( access_t ) );
170 memset( p_access, 0, sizeof( access_t ) );
171 p_url = &p_access->url;
173 /* *** Parse URL and get server addr/port and path *** */
174 ftp_ParseURL( p_url, p_input->psz_name );
176 if( p_url->psz_server_addr == NULL ||
177 !( *p_url->psz_server_addr ) )
179 FREE( p_url->psz_private );
180 msg_Err( p_input, "invalid server name" );
183 if( p_url->i_server_port == 0 )
185 p_url->i_server_port = 21; /* default port */
188 /* 2: look at ip version ipv4/ipv6 */
190 if( config_GetInt( p_input, "ipv4" ) )
192 psz_network = "ipv4";
194 else if( config_GetInt( p_input, "ipv6" ) )
196 psz_network = "ipv6";
199 /* 3: Open a TCP connection with server *** */
200 msg_Dbg( p_input, "waiting for connection..." );
201 socket_desc.i_type = NETWORK_TCP;
202 socket_desc.psz_server_addr = p_url->psz_server_addr;
203 socket_desc.i_server_port = p_url->i_server_port;
204 socket_desc.psz_bind_addr = "";
205 socket_desc.i_bind_port = 0;
206 p_input->p_private = (void*)&socket_desc;
207 if( !( p_network = module_Need( p_input, "network", psz_network ) ) )
209 msg_Err( p_input, "failed to connect with server" );
210 FREE( p_access->url.psz_private );
211 FREE( p_input->p_access_data );
214 module_Unneed( p_input, p_network );
215 p_access->socket_cmd.i_handle = socket_desc.i_handle;
216 p_input->i_mtu = socket_desc.i_mtu;
218 "connection with \"%s:%d\" successful",
219 p_url->psz_server_addr,
220 p_url->i_server_port );
225 if( ftp_ReadCommand( p_input, &i_answer, NULL ) < 0)
227 msg_Err( p_input, "failed to get answer" );
230 if( i_answer / 100 != 1 )
236 if( i_answer / 100 != 2 )
238 msg_Err( p_input, "connection rejected" );
243 msg_Dbg( p_input, "connection accepted (%d)", i_answer );
246 psz_user = config_GetPsz( p_input, "ftp-user" );
247 if( ftp_SendCommand( p_input, "USER %s", psz_user ) < 0 )
254 if( ftp_ReadCommand( p_input, &i_answer, NULL ) < 0)
256 msg_Err( p_input, "failed to get answer" );
259 switch( i_answer / 100 )
262 msg_Dbg( p_input, "user accepted" );
265 msg_Dbg( p_input, "password needed" );
266 psz_pwd = config_GetPsz( p_input, "ftp-pwd" );
267 if( ftp_SendCommand( p_input, "PASS %s", psz_pwd ) < 0 )
273 if( ftp_ReadCommand( p_input, &i_answer, NULL ) < 0)
275 msg_Err( p_input, "failed to get answer" );
278 switch( i_answer / 100 )
281 msg_Dbg( p_input, "password accepted" );
284 msg_Dbg( p_input, "account needed" );
285 psz_account = config_GetPsz( p_input, "ftp-account" );
286 if( ftp_SendCommand( p_input, "ACCT %s", psz_account ) < 0 )
292 if( ftp_ReadCommand( p_input, &i_answer, NULL ) < 0)
294 msg_Err( p_input, "failed to get answer" );
297 if( i_answer / 100 != 2 )
299 msg_Err( p_input, "account rejected" );
304 msg_Dbg( p_input, "account accepted" );
308 msg_Err( p_input, "password rejected" );
313 msg_Err( p_input, "user rejected" );
317 if( ftp_SendCommand( p_input, "TYPE I" ) < 0 )
319 msg_Err( p_input, "cannot set binary transfert mode" );
322 if( ftp_ReadCommand( p_input, &i_answer, NULL ) != 2 )
324 msg_Err( p_input, "cannot set binary transfert mode" );
329 if( ftp_SendCommand( p_input, "SIZE %s", p_url->psz_path ) < 0 )
331 msg_Err( p_input, "cannot get file size" );
334 if( ftp_ReadCommand( p_input, &i_answer, &psz_arg ) != 2 )
336 msg_Err( p_input, "cannot get file size" );
341 p_access->i_filesize = atoll( psz_arg + 4 );
345 char *psz_parser = psz_arg + 4;
347 while( *psz_parser == ' ' ) psz_parser++;
349 while( psz_parser[0] >= '0' && psz_parser[0] <= '9' )
352 i_size += psz_parser[0] - '0';
354 p_access->i_filesize = i_size;
358 msg_Dbg( p_input, "file size: "I64Fd, p_access->i_filesize );
361 if( ftp_StartStream( p_input, 0 ) < 0 )
363 msg_Err( p_input, "cannot retrieve file" );
366 /* *** set exported functions *** */
367 p_input->pf_read = Read;
368 p_input->pf_seek = Seek;
369 p_input->pf_set_program = SetProgram;
370 p_input->pf_set_area = NULL;
372 p_input->p_private = NULL;
374 /* *** finished to set some variable *** */
375 vlc_mutex_lock( &p_input->stream.stream_lock );
376 p_input->stream.b_pace_control = 1;
377 p_input->stream.p_selected_area->i_tell = 0;
378 p_input->stream.b_seekable = 1;
379 p_input->stream.b_connected = 1;
380 p_input->stream.p_selected_area->i_size = p_access->i_filesize;
381 p_input->stream.i_method = INPUT_METHOD_NETWORK;
382 vlc_mutex_unlock( &p_input->stream.stream_lock );
384 /* Update default_pts to a suitable value for ftp access */
385 p_input->i_pts_delay = config_GetInt( p_input, "ftp-caching" ) * 1000;
390 NetClose( p_input, &p_access->socket_cmd );
391 FREE( p_access->url.psz_private );
392 FREE( p_input->p_access_data );
396 /*****************************************************************************
397 * Close: free unused data structures
398 *****************************************************************************/
399 static void Close( vlc_object_t *p_this )
401 input_thread_t *p_input = (input_thread_t *)p_this;
402 access_t *p_access = (access_t*)p_input->p_access_data;
404 msg_Dbg( p_input, "stopping stream" );
405 ftp_StopStream( p_input );
407 if( ftp_SendCommand( p_input, "QUIT" ) < 0 )
409 msg_Err( p_input, "cannot quit" );
413 ftp_ReadCommand( p_input, NULL, NULL );
417 NetClose( p_input, &p_access->socket_cmd );
420 FREE( p_access->url.psz_private );
423 /*****************************************************************************
424 * SetProgram: do nothing
425 *****************************************************************************/
426 static int SetProgram( input_thread_t * p_input,
427 pgrm_descriptor_t * p_program )
432 /*****************************************************************************
433 * Seek: try to go at the right place
434 *****************************************************************************/
435 static void Seek( input_thread_t * p_input, off_t i_pos )
437 //access_t *p_access = (access_t*)p_input->p_access_data;
442 vlc_mutex_lock( &p_input->stream.stream_lock );
444 msg_Dbg( p_input, "seeking to "I64Fd, i_pos );
446 ftp_StopStream( p_input );
447 ftp_StartStream( p_input, i_pos );
449 p_input->stream.p_selected_area->i_tell = i_pos;
450 vlc_mutex_unlock( &p_input->stream.stream_lock );
453 static int Read ( input_thread_t * p_input, byte_t * p_buffer,
456 access_t *p_access = (access_t*)p_input->p_access_data;
459 i_data = NetRead( p_input, &p_access->socket_data, p_buffer, i_len );
464 static int ftp_SendCommand( input_thread_t *p_input, char *psz_fmt, ... )
466 access_t *p_access = (access_t*)p_input->p_access_data;
469 #if !defined(HAVE_VASPRINTF) || defined(SYS_DARWIN)
473 va_start( args, psz_fmt );
475 #if defined(HAVE_VASPRINTF) && !defined(SYS_DARWIN)
476 vasprintf( &psz_buffer, psz_fmt, args );
478 i_size = strlen( psz_fmt ) + 2048;
479 psz_buffer = calloc( i_size, sizeof( char ) );
480 vsnprintf( psz_buffer, i_size, psz_fmt, args );
481 psz_buffer[i_size - 1] = 0;
483 if( !strncmp( psz_buffer, "PASS", 4 ) )
485 msg_Dbg( p_input, "ftp_SendCommand:\"PASS xxx\"" );
489 msg_Dbg( p_input, "ftp_SendCommand:\"%s\"", psz_buffer );
491 psz_buffer = realloc( psz_buffer, strlen( psz_buffer ) + 3 );
492 strcat( psz_buffer, "\r\n" );
493 if( send( p_access->socket_cmd.i_handle,
495 strlen( psz_buffer ),
499 msg_Err( p_input, "failed to send command" );
509 #define BLOCK_SIZE 1024
510 /* TODO support this s**t :
511 RFC 959 allows the client to send certain TELNET strings at any moment,
512 even in the middle of a request:
515 * \377\376x where x is one byte.
516 * \377\375x where x is one byte. The server is obliged to send \377\374x
517 * immediately after reading x.
518 * \377\374x where x is one byte.
519 * \377\373x where x is one byte. The server is obliged to send \377\376x
520 * immediately after reading x.
521 * \377x for any other byte x.
523 These strings are not part of the requests, except in the case \377\377,
524 where the request contains one \377. */
526 static int ftp_ReadCommand( input_thread_t *p_input,
527 int *pi_answer, char **ppsz_answer )
529 access_t *p_access = (access_t*)p_input->p_access_data;
537 i_buffer_size = BLOCK_SIZE + 1;
538 p_buffer = malloc( BLOCK_SIZE + 1);
543 i_read = NetRead( p_input, &p_access->socket_cmd,
544 p_buffer + i_buffer, BLOCK_SIZE );
545 if( i_read <= 0 || p_input->b_die || p_input->b_error )
548 if( pi_answer ) *pi_answer = 500;
549 if( ppsz_answer ) *ppsz_answer = NULL;
557 if( i_read < BLOCK_SIZE )
559 p_buffer[i_buffer] = '\0';
562 i_buffer_size += BLOCK_SIZE;
563 p_buffer = realloc( p_buffer, i_buffer_size );
571 i_answer = atoi( p_buffer );
573 if( pi_answer ) *pi_answer = i_answer;
576 *ppsz_answer = p_buffer;
582 return( i_answer / 100 );
586 if( pi_answer ) *pi_answer = 500;
587 if( ppsz_answer ) *ppsz_answer = NULL;
591 static int ftp_StartStream( input_thread_t *p_input, off_t i_start )
593 access_t *p_access = (access_t*)p_input->p_access_data;
597 char *psz_arg, *psz_parser;
602 network_socket_t socket_desc;
604 if( ftp_SendCommand( p_input, "PASV" ) < 0 )
606 msg_Err( p_input, "cannot set passive transfert mode" );
609 if( ftp_ReadCommand( p_input, &i_answer, &psz_arg ) != 2 )
611 msg_Err( p_input, "cannot set passive transfert mode" );
614 psz_parser = strchr( psz_arg, '(' );
615 if( !psz_parser || sscanf( psz_parser, "(%d,%d,%d,%d,%d,%d", &a1, &a2, &a3, &a4, &p1, &p2 ) < 6 )
618 msg_Err( p_input, "cannot get ip/port for passive transfert mode" );
623 sprintf( psz_ip, "%d.%d.%d.%d", a1, a2, a3, a4 );
624 i_port = p1 * 256 + p2;
625 msg_Dbg( p_input, "ip:%s port:%d", psz_ip, i_port );
627 if( ftp_SendCommand( p_input, "TYPE I" ) < 0 )
629 msg_Err( p_input, "cannot set binary transfert mode" );
632 if( ftp_ReadCommand( p_input, &i_answer, NULL ) != 2 )
634 msg_Err( p_input, "cannot set binary transfert mode" );
641 if( ftp_SendCommand( p_input, "REST "I64Fu, i_start ) < 0 )
643 msg_Err( p_input, "cannot set restart point" );
646 if( ftp_ReadCommand( p_input, &i_answer, NULL ) > 3 )
648 msg_Err( p_input, "cannot set restart point" );
653 msg_Dbg( p_input, "waiting for data connection..." );
654 socket_desc.i_type = NETWORK_TCP;
655 socket_desc.psz_server_addr = psz_ip;
656 socket_desc.i_server_port = i_port;
657 socket_desc.psz_bind_addr = "";
658 socket_desc.i_bind_port = 0;
659 p_input->p_private = (void*)&socket_desc;
660 if( !( p_network = module_Need( p_input, "network", "" ) ) )
662 msg_Err( p_input, "failed to connect with server" );
665 module_Unneed( p_input, p_network );
666 p_access->socket_data.i_handle = socket_desc.i_handle;
667 p_input->i_mtu = socket_desc.i_mtu;
669 "connection with \"%s:%d\" successful",
672 if( ftp_SendCommand( p_input, "RETR %s", p_access->url.psz_path ) < 0 )
674 msg_Err( p_input, "cannot retreive file" );
678 if( ftp_ReadCommand( p_input, &i_answer, NULL ) > 2 )
680 msg_Err( p_input, "cannot retreive file" );
687 static int ftp_StopStream ( input_thread_t *p_input)
689 access_t *p_access = (access_t*)p_input->p_access_data;
693 NetClose( p_input, &p_access->socket_data );
695 if( ftp_SendCommand( p_input, "ABOR" ) < 0 )
697 msg_Err( p_input, "cannot abord file" );
701 ftp_ReadCommand( p_input, &i_answer, NULL );
702 ftp_ReadCommand( p_input, &i_answer, NULL );
708 /****************************************************************************
710 ****************************************************************************/
711 static void ftp_ParseURL( url_t *p_url, char *psz_url )
714 char *psz_server_port;
716 p_url->psz_private = strdup( psz_url );
718 psz_parser = p_url->psz_private;
720 while( *psz_parser == '/' )
724 p_url->psz_server_addr = psz_parser;
726 while( *psz_parser &&
727 *psz_parser != ':' && *psz_parser != '/' && *psz_parser != '@' )
732 if( *psz_parser == ':' )
736 psz_server_port = psz_parser;
738 while( *psz_parser && *psz_parser != '/' )
745 psz_server_port = "";
748 if( *psz_parser == '@' )
755 p_url->psz_bind_addr = psz_parser;
757 while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
762 if( *psz_parser == ':' )
766 psz_bind_port = psz_parser;
768 while( *psz_parser && *psz_parser != '/' )
779 p_url->i_bind_port = strtol( psz_bind_port, &psz_parser, 10 );
783 p_url->i_bind_port = 0;
788 p_url->psz_bind_addr = "";
789 p_url->i_bind_port = 0;
792 if( *psz_parser == '/' )
796 p_url->psz_path = psz_parser;
799 if( *psz_server_port )
801 p_url->i_server_port = strtol( psz_server_port, &psz_parser, 10 );
805 p_url->i_server_port = 0;
809 /*****************************************************************************
810 * Read: read on a file descriptor, checking b_die periodically
811 *****************************************************************************/
812 static ssize_t NetRead( input_thread_t *p_input,
813 input_socket_t *p_socket,
814 byte_t *p_buffer, size_t i_len )
820 struct timeval timeout;
824 /* Initialize file descriptor set */
826 FD_SET( p_socket->i_handle, &fds );
828 /* We'll wait 1 second if nothing happens */
830 timeout.tv_usec = 1000000;
832 /* Find if some data is available */
833 i_ret = select( p_socket->i_handle + 1, &fds,
834 NULL, NULL, &timeout );
836 if( i_ret == -1 && errno != EINTR )
838 msg_Err( p_input, "network select error (%s)", strerror(errno) );
842 ssize_t i_recv = recv( p_socket->i_handle, p_buffer, i_len, 0 );
846 msg_Err( p_input, "recv failed (%s)", strerror(errno) );
857 static void NetClose( input_thread_t *p_input, input_socket_t *p_socket )
859 #if defined( UNDER_CE )
860 CloseHandle( (HANDLE)p_socket->i_handle );
861 #elif defined( WIN32 )
862 closesocket( p_socket->i_handle );
864 close( p_socket->i_handle );