1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: ftp.c,v 1.3 2002/12/18 14:17:09 sam 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.p_selected_area->i_size = p_access->i_filesize;
375 p_input->stream.i_method = INPUT_METHOD_NETWORK;
376 vlc_mutex_unlock( &p_input->stream.stream_lock );
378 /* Update default_pts to a suitable value for ftp access */
379 p_input->i_pts_delay = config_GetInt( p_input, "ftp-caching" ) * 1000;
384 NetClose( p_input, &p_access->socket_cmd );
385 FREE( p_access->url.psz_private );
386 FREE( p_input->p_access_data );
390 /*****************************************************************************
391 * Close: free unused data structures
392 *****************************************************************************/
393 static void Close( vlc_object_t *p_this )
395 input_thread_t *p_input = (input_thread_t *)p_this;
396 access_t *p_access = (access_t*)p_input->p_access_data;
398 msg_Dbg( p_input, "stopping stream" );
399 ftp_StopStream( p_input );
401 if( ftp_SendCommand( p_input, "QUIT" ) < 0 )
403 msg_Err( p_input, "cannot quit" );
407 ftp_ReadCommand( p_input, NULL, NULL );
411 NetClose( p_input, &p_access->socket_cmd );
414 FREE( p_access->url.psz_private );
417 /*****************************************************************************
418 * SetProgram: do nothing
419 *****************************************************************************/
420 static int SetProgram( input_thread_t * p_input,
421 pgrm_descriptor_t * p_program )
426 /*****************************************************************************
427 * Seek: try to go at the right place
428 *****************************************************************************/
429 static void Seek( input_thread_t * p_input, off_t i_pos )
431 //access_t *p_access = (access_t*)p_input->p_access_data;
436 vlc_mutex_lock( &p_input->stream.stream_lock );
438 msg_Dbg( p_input, "seeking to "I64Fd, i_pos );
440 ftp_StopStream( p_input );
441 ftp_StartStream( p_input, i_pos );
443 p_input->stream.p_selected_area->i_tell = i_pos;
444 vlc_mutex_unlock( &p_input->stream.stream_lock );
447 static int Read ( input_thread_t * p_input, byte_t * p_buffer,
450 access_t *p_access = (access_t*)p_input->p_access_data;
453 i_data = NetRead( p_input, &p_access->socket_data, p_buffer, i_len );
458 static int ftp_SendCommand( input_thread_t *p_input, char *psz_fmt, ... )
460 access_t *p_access = (access_t*)p_input->p_access_data;
463 #if !defined(HAVE_VASPRINTF) || defined(SYS_DARWIN)
467 va_start( args, psz_fmt );
469 #if defined(HAVE_VASPRINTF) && !defined(SYS_DARWIN)
470 vasprintf( &psz_buffer, psz_fmt, args );
472 i_size = strlen( psz_fmt ) + 2048;
473 psz_buffer = calloc( i_size, sizeof( char ) );
474 vsnprintf( psz_buffer, i_size, psz_fmt, args );
475 psz_buffer[i_size - 1] = 0;
477 if( !strncmp( psz_buffer, "PASS", 4 ) )
479 msg_Dbg( p_input, "ftp_SendCommand:\"PASS xxx\"" );
483 msg_Dbg( p_input, "ftp_SendCommand:\"%s\"", psz_buffer );
485 psz_buffer = realloc( psz_buffer, strlen( psz_buffer ) + 3 );
486 strcat( psz_buffer, "\r\n" );
487 if( send( p_access->socket_cmd.i_handle,
489 strlen( psz_buffer ),
493 msg_Err( p_input, "failed to send command" );
503 #define BLOCK_SIZE 1024
504 /* TODO support this s**t :
505 RFC 959 allows the client to send certain TELNET strings at any moment,
506 even in the middle of a request:
509 * \377\376x where x is one byte.
510 * \377\375x where x is one byte. The server is obliged to send \377\374x
511 * immediately after reading x.
512 * \377\374x where x is one byte.
513 * \377\373x where x is one byte. The server is obliged to send \377\376x
514 * immediately after reading x.
515 * \377x for any other byte x.
517 These strings are not part of the requests, except in the case \377\377,
518 where the request contains one \377. */
520 static int ftp_ReadCommand( input_thread_t *p_input,
521 int *pi_answer, char **ppsz_answer )
523 access_t *p_access = (access_t*)p_input->p_access_data;
531 i_buffer_size = BLOCK_SIZE + 1;
532 p_buffer = malloc( BLOCK_SIZE + 1);
537 i_read = NetRead( p_input, &p_access->socket_cmd,
538 p_buffer + i_buffer, BLOCK_SIZE );
539 if( i_read <= 0 || p_input->b_die || p_input->b_error )
542 if( pi_answer ) *pi_answer = 500;
543 if( ppsz_answer ) *ppsz_answer = NULL;
551 if( i_read < BLOCK_SIZE )
553 p_buffer[i_buffer] = '\0';
556 i_buffer_size += BLOCK_SIZE;
557 p_buffer = realloc( p_buffer, i_buffer_size );
565 i_answer = atoi( p_buffer );
567 if( pi_answer ) *pi_answer = i_answer;
570 *ppsz_answer = p_buffer;
576 return( i_answer / 100 );
580 if( pi_answer ) *pi_answer = 500;
581 if( ppsz_answer ) *ppsz_answer = NULL;
585 static int ftp_StartStream( input_thread_t *p_input, off_t i_start )
587 access_t *p_access = (access_t*)p_input->p_access_data;
591 char *psz_arg, *psz_parser;
596 network_socket_t socket_desc;
598 if( ftp_SendCommand( p_input, "PASV" ) < 0 )
600 msg_Err( p_input, "cannot set passive transfert mode" );
603 if( ftp_ReadCommand( p_input, &i_answer, &psz_arg ) != 2 )
605 msg_Err( p_input, "cannot set passive transfert mode" );
608 psz_parser = strchr( psz_arg, '(' );
609 if( !psz_parser || sscanf( psz_parser, "(%d,%d,%d,%d,%d,%d", &a1, &a2, &a3, &a4, &p1, &p2 ) < 6 )
612 msg_Err( p_input, "cannot get ip/port for passive transfert mode" );
617 sprintf( psz_ip, "%d.%d.%d.%d", a1, a2, a3, a4 );
618 i_port = p1 * 256 + p2;
619 msg_Dbg( p_input, "ip:%s port:%d", psz_ip, i_port );
621 if( ftp_SendCommand( p_input, "TYPE I" ) < 0 )
623 msg_Err( p_input, "cannot set binary transfert mode" );
626 if( ftp_ReadCommand( p_input, &i_answer, NULL ) != 2 )
628 msg_Err( p_input, "cannot set binary transfert mode" );
635 if( ftp_SendCommand( p_input, "REST "I64Fu, i_start ) < 0 )
637 msg_Err( p_input, "cannot set restart point" );
640 if( ftp_ReadCommand( p_input, &i_answer, NULL ) > 3 )
642 msg_Err( p_input, "cannot set restart point" );
647 msg_Dbg( p_input, "waiting for data connection..." );
648 socket_desc.i_type = NETWORK_TCP;
649 socket_desc.psz_server_addr = psz_ip;
650 socket_desc.i_server_port = i_port;
651 socket_desc.psz_bind_addr = "";
652 socket_desc.i_bind_port = 0;
653 p_input->p_private = (void*)&socket_desc;
654 if( !( p_network = module_Need( p_input, "network", "" ) ) )
656 msg_Err( p_input, "failed to connect with server" );
659 module_Unneed( p_input, p_network );
660 p_access->socket_data.i_handle = socket_desc.i_handle;
661 p_input->i_mtu = socket_desc.i_mtu;
663 "connection with \"%s:%d\" successful",
666 if( ftp_SendCommand( p_input, "RETR %s", p_access->url.psz_path ) < 0 )
668 msg_Err( p_input, "cannot retreive file" );
672 if( ftp_ReadCommand( p_input, &i_answer, NULL ) > 2 )
674 msg_Err( p_input, "cannot retreive file" );
681 static int ftp_StopStream ( input_thread_t *p_input)
683 access_t *p_access = (access_t*)p_input->p_access_data;
687 NetClose( p_input, &p_access->socket_data );
689 if( ftp_SendCommand( p_input, "ABOR" ) < 0 )
691 msg_Err( p_input, "cannot abord file" );
695 ftp_ReadCommand( p_input, &i_answer, NULL );
696 ftp_ReadCommand( p_input, &i_answer, NULL );
702 /****************************************************************************
704 ****************************************************************************/
705 static void ftp_ParseURL( url_t *p_url, char *psz_url )
708 char *psz_server_port;
710 p_url->psz_private = strdup( psz_url );
712 psz_parser = p_url->psz_private;
714 while( *psz_parser == '/' )
718 p_url->psz_server_addr = psz_parser;
720 while( *psz_parser &&
721 *psz_parser != ':' && *psz_parser != '/' && *psz_parser != '@' )
726 if( *psz_parser == ':' )
730 psz_server_port = psz_parser;
732 while( *psz_parser && *psz_parser != '/' )
739 psz_server_port = "";
742 if( *psz_parser == '@' )
749 p_url->psz_bind_addr = psz_parser;
751 while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
756 if( *psz_parser == ':' )
760 psz_bind_port = psz_parser;
762 while( *psz_parser && *psz_parser != '/' )
773 p_url->i_bind_port = strtol( psz_bind_port, &psz_parser, 10 );
777 p_url->i_bind_port = 0;
782 p_url->psz_bind_addr = "";
783 p_url->i_bind_port = 0;
786 if( *psz_parser == '/' )
790 p_url->psz_path = psz_parser;
793 if( *psz_server_port )
795 p_url->i_server_port = strtol( psz_server_port, &psz_parser, 10 );
799 p_url->i_server_port = 0;
803 /*****************************************************************************
804 * Read: read on a file descriptor, checking b_die periodically
805 *****************************************************************************/
806 static ssize_t NetRead( input_thread_t *p_input,
807 input_socket_t *p_socket,
808 byte_t *p_buffer, size_t i_len )
814 struct timeval timeout;
818 /* Initialize file descriptor set */
820 FD_SET( p_socket->i_handle, &fds );
822 /* We'll wait 1 second if nothing happens */
824 timeout.tv_usec = 1000000;
826 /* Find if some data is available */
827 i_ret = select( p_socket->i_handle + 1, &fds,
828 NULL, NULL, &timeout );
830 if( i_ret == -1 && errno != EINTR )
832 msg_Err( p_input, "network select error (%s)", strerror(errno) );
836 ssize_t i_recv = recv( p_socket->i_handle, p_buffer, i_len, 0 );
840 msg_Err( p_input, "recv failed (%s)", strerror(errno) );
851 static void NetClose( input_thread_t *p_input, input_socket_t *p_socket )
853 #if defined( UNDER_CE )
854 CloseHandle( (HANDLE)p_socket->i_handle );
855 #elif defined( WIN32 )
856 closesocket( p_socket->i_handle );
858 close( p_socket->i_handle );