]> git.sesse.net Git - vlc/blob - modules/access/ftp.c
Update copyright date
[vlc] / modules / access / ftp.c
1 /*****************************************************************************
2  * ftp.c: FTP input module
3  *****************************************************************************
4  * Copyright (C) 2001-2005 VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28
29 #include <vlc/vlc.h>
30 #include <vlc/input.h>
31
32 #include "network.h"
33
34 /*****************************************************************************
35  * Module descriptor
36  *****************************************************************************/
37 static int     Open ( vlc_object_t * );
38 static void    Close( vlc_object_t * );
39
40 #define CACHING_TEXT N_("Caching value in ms")
41 #define CACHING_LONGTEXT N_( \
42     "Allows you to modify the default caching value for FTP streams. This " \
43     "value should be set in millisecond units." )
44 #define USER_TEXT N_("FTP user name")
45 #define USER_LONGTEXT N_("Allows you to modify the user name that will " \
46     "be used for the connection.")
47 #define PASS_TEXT N_("FTP password")
48 #define PASS_LONGTEXT N_("Allows you to modify the password that will be " \
49     "used for the connection.")
50 #define ACCOUNT_TEXT N_("FTP account")
51 #define ACCOUNT_LONGTEXT N_("Allows you to modify the account that will be " \
52     "used for the connection.")
53
54 vlc_module_begin();
55     set_shortname( "FTP" );
56     set_description( _("FTP input") );
57     set_capability( "access2", 0 );
58     set_category( CAT_INPUT );
59     set_subcategory( SUBCAT_INPUT_ACCESS );
60     add_integer( "ftp-caching", 2 * DEFAULT_PTS_DELAY / 1000, NULL,
61                  CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
62     add_string( "ftp-user", "anonymous", NULL, USER_TEXT, USER_LONGTEXT,
63                 VLC_FALSE );
64     add_string( "ftp-pwd", "anonymous@dummy.org", NULL, PASS_TEXT,
65                 PASS_LONGTEXT, VLC_FALSE );
66     add_string( "ftp-account", "anonymous", NULL, ACCOUNT_TEXT,
67                 ACCOUNT_LONGTEXT, VLC_FALSE );
68     add_shortcut( "ftp" );
69     set_callbacks( Open, Close );
70 vlc_module_end();
71
72 /*****************************************************************************
73  * Local prototypes
74  *****************************************************************************/
75 static int Read( access_t *, uint8_t *, int );
76 static int Seek( access_t *, int64_t );
77 static int Control( access_t *, int, va_list );
78
79 struct access_sys_t
80 {
81     vlc_url_t  url;
82
83     int        fd_cmd;
84     int        fd_data;
85     
86     vlc_bool_t b_epsv;
87 };
88
89 static int  ftp_SendCommand( access_t *, char *, ... );
90 static int  ftp_ReadCommand( access_t *, int *, char ** );
91 static int  ftp_StartStream( access_t *, int64_t );
92 static int  ftp_StopStream ( access_t *);
93
94 /****************************************************************************
95  * Open: connect to ftp server and ask for file
96  ****************************************************************************/
97 static int Open( vlc_object_t *p_this )
98 {
99     access_t     *p_access = (access_t*)p_this;
100     access_sys_t *p_sys;
101     char         *psz;
102
103     int          i_answer;
104     char         *psz_arg;
105
106     /* Init p_access */
107     p_access->pf_read = Read;
108     p_access->pf_block = NULL;
109     p_access->pf_seek = Seek;
110     p_access->pf_control = Control;
111     p_access->info.i_update = 0;
112     p_access->info.i_size = 0;
113     p_access->info.i_pos = 0;
114     p_access->info.b_eof = VLC_FALSE;
115     p_access->info.i_title = 0;
116     p_access->info.i_seekpoint = 0;
117     p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
118     memset( p_sys, 0, sizeof( access_sys_t ) );
119     p_sys->fd_cmd = -1;
120     p_sys->fd_data = -1;
121
122     /* *** Parse URL and get server addr/port and path *** */
123     psz = p_access->psz_path;
124     while( *psz == '/' )
125     {
126         psz++;
127     }
128     vlc_UrlParse( &p_sys->url, psz, 0 );
129
130     if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
131     {
132         msg_Err( p_access, "invalid server name" );
133         goto exit_error;
134     }
135     if( p_sys->url.i_port <= 0 )
136     {
137         p_sys->url.i_port = 21; /* default port */
138     }
139
140     /* *** Open a TCP connection with server *** */
141     msg_Dbg( p_access, "waiting for connection..." );
142     p_sys->fd_cmd = net_OpenTCP( p_access, p_sys->url.psz_host,
143                                  p_sys->url.i_port );
144     if( p_sys->fd_cmd < 0 )
145     {
146         msg_Err( p_access, "failed to connect with server" );
147         goto exit_error;
148     }
149
150     for( ;; )
151     {
152         if( ftp_ReadCommand( p_access, &i_answer, NULL ) != 1 )
153         {
154             break;
155         }
156     }
157     if( i_answer / 100 != 2 )
158     {
159         msg_Err( p_access, "connection rejected" );
160         goto exit_error;
161     }
162
163     msg_Dbg( p_access, "connection accepted (%d)", i_answer );
164
165     psz = var_CreateGetString( p_access, "ftp-user" );
166     if( ftp_SendCommand( p_access, "USER %s", psz ) < 0 ||
167         ftp_ReadCommand( p_access, &i_answer, NULL ) < 0 )
168     {
169         free( psz );
170         goto exit_error;
171     }
172     free( psz );
173
174     switch( i_answer / 100 )
175     {
176         case 2:
177             msg_Dbg( p_access, "user accepted" );
178             break;
179         case 3:
180             msg_Dbg( p_access, "password needed" );
181             psz = var_CreateGetString( p_access, "ftp-pwd" );
182             if( ftp_SendCommand( p_access, "PASS %s", psz ) < 0 ||
183                 ftp_ReadCommand( p_access, &i_answer, NULL ) < 0 )
184             {
185                 free( psz );
186                 goto exit_error;
187             }
188             free( psz );
189
190             switch( i_answer / 100 )
191             {
192                 case 2:
193                     msg_Dbg( p_access, "password accepted" );
194                     break;
195                 case 3:
196                     msg_Dbg( p_access, "account needed" );
197                     psz = var_CreateGetString( p_access, "ftp-account" );
198                     if( ftp_SendCommand( p_access, "ACCT %s",
199                                          psz ) < 0 ||
200                         ftp_ReadCommand( p_access, &i_answer, NULL ) < 0 )
201                     {
202                         free( psz );
203                         goto exit_error;
204                     }
205                     free( psz );
206
207                     if( i_answer / 100 != 2 )
208                     {
209                         msg_Err( p_access, "account rejected" );
210                         goto exit_error;
211                     }
212                     msg_Dbg( p_access, "account accepted" );
213                     break;
214
215                 default:
216                     msg_Err( p_access, "password rejected" );
217                     goto exit_error;
218             }
219             break;
220         default:
221             msg_Err( p_access, "user rejected" );
222             goto exit_error;
223     }
224
225     /* Extended passive mode */
226     if( ftp_SendCommand( p_access, "EPSV ALL" ) < 0 )
227     {
228         msg_Err( p_access, "cannot request extended passive mode" );
229         goto exit_error;
230     }
231     if( ftp_ReadCommand( p_access, &i_answer, NULL ) != 2 )
232     {
233         p_sys->b_epsv = VLC_FALSE;
234         msg_Warn( p_access, "Extended passive mode not supported" );
235     }
236     else
237         p_sys->b_epsv = VLC_TRUE;
238
239     /* binary mode */
240     if( ftp_SendCommand( p_access, "TYPE I" ) < 0 ||
241         ftp_ReadCommand( p_access, &i_answer, NULL ) != 2 )
242     {
243         msg_Err( p_access, "cannot set binary transfer mode" );
244         goto exit_error;
245     }
246
247     /* get size */
248     if( ftp_SendCommand( p_access, "SIZE %s", p_sys->url.psz_path ) < 0 ||
249         ftp_ReadCommand( p_access, &i_answer, &psz_arg ) != 2 )
250     {
251         msg_Err( p_access, "cannot get file size" );
252         goto exit_error;
253     }
254     p_access->info.i_size = atoll( &psz_arg[4] );
255     free( psz_arg );
256     msg_Dbg( p_access, "file size: "I64Fd, p_access->info.i_size );
257
258     /* Start the 'stream' */
259     if( ftp_StartStream( p_access, 0 ) < 0 )
260     {
261         msg_Err( p_access, "cannot retrieve file" );
262         goto exit_error;
263     }
264
265     /* Update default_pts to a suitable value for ftp access */
266     var_Create( p_access, "ftp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
267
268     return VLC_SUCCESS;
269
270 exit_error:
271     if( p_sys->fd_cmd > 0 )
272     {
273         net_Close( p_sys->fd_cmd );
274     }
275     vlc_UrlClean( &p_sys->url );
276     free( p_sys );
277     return VLC_EGENERIC;
278 }
279
280 /*****************************************************************************
281  * Close: free unused data structures
282  *****************************************************************************/
283 static void Close( vlc_object_t *p_this )
284 {
285     access_t      *p_access = (access_t*)p_this;
286     access_sys_t  *p_sys = p_access->p_sys;
287
288     msg_Dbg( p_access, "stopping stream" );
289     ftp_StopStream( p_access );
290
291     if( ftp_SendCommand( p_access, "QUIT" ) < 0 )
292     {
293         msg_Warn( p_access, "cannot quit" );
294     }
295     else
296     {
297         ftp_ReadCommand( p_access, NULL, NULL );
298     }
299     net_Close( p_sys->fd_cmd );
300
301     /* free memory */
302     vlc_UrlClean( &p_sys->url );
303     free( p_sys );
304 }
305
306 /*****************************************************************************
307  * Seek: try to go at the right place
308  *****************************************************************************/
309 static int Seek( access_t *p_access, int64_t i_pos )
310 {
311     if( i_pos < 0 )
312     {
313         return VLC_EGENERIC;
314     }
315     msg_Dbg( p_access, "seeking to "I64Fd, i_pos );
316
317     ftp_StopStream( p_access );
318     if( ftp_StartStream( p_access, i_pos ) < 0 )
319     {
320         p_access->info.b_eof = VLC_TRUE;
321         return VLC_EGENERIC;
322     }
323
324     p_access->info.b_eof = VLC_FALSE;
325     p_access->info.i_pos = i_pos;
326
327     return VLC_SUCCESS;
328 }
329
330 /*****************************************************************************
331  * Read:
332  *****************************************************************************/
333 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
334 {
335     access_sys_t *p_sys = p_access->p_sys;
336     int i_read;
337
338     if( p_access->info.b_eof )
339         return 0;
340
341     i_read = net_Read( p_access, p_sys->fd_data, NULL, p_buffer, i_len,
342                        VLC_FALSE );
343     if( i_read == 0 )
344         p_access->info.b_eof = VLC_TRUE;
345     else if( i_read > 0 )
346         p_access->info.i_pos += i_read;
347
348     return i_read;
349 }
350
351 /*****************************************************************************
352  * Control:
353  *****************************************************************************/
354 static int Control( access_t *p_access, int i_query, va_list args )
355 {
356     vlc_bool_t   *pb_bool;
357     int          *pi_int;
358     int64_t      *pi_64;
359     vlc_value_t  val;
360
361     switch( i_query )
362     {
363         /* */
364         case ACCESS_CAN_SEEK:
365             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
366             *pb_bool = VLC_TRUE;
367             break;
368         case ACCESS_CAN_FASTSEEK:
369             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
370             *pb_bool = VLC_FALSE;
371             break;
372         case ACCESS_CAN_PAUSE:
373             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
374             *pb_bool = VLC_TRUE;    /* FIXME */
375             break;
376         case ACCESS_CAN_CONTROL_PACE:
377             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
378             *pb_bool = VLC_TRUE;    /* FIXME */
379             break;
380
381         /* */
382         case ACCESS_GET_MTU:
383             pi_int = (int*)va_arg( args, int * );
384             *pi_int = 0;
385             break;
386
387         case ACCESS_GET_PTS_DELAY:
388             pi_64 = (int64_t*)va_arg( args, int64_t * );
389             var_Get( p_access, "ftp-caching", &val );
390             *pi_64 = (int64_t)var_GetInteger( p_access, "ftp-caching" ) * I64C(1000);
391             break;
392
393         /* */
394         case ACCESS_SET_PAUSE_STATE:
395             /* Nothing to do */
396             break;
397
398         case ACCESS_GET_TITLE_INFO:
399         case ACCESS_SET_TITLE:
400         case ACCESS_SET_SEEKPOINT:
401         case ACCESS_SET_PRIVATE_ID_STATE:
402             return VLC_EGENERIC;
403
404         default:
405             msg_Warn( p_access, "unimplemented query in control" );
406             return VLC_EGENERIC;
407
408     }
409     return VLC_SUCCESS;
410 }
411
412 /*****************************************************************************
413  * ftp_*:
414  *****************************************************************************/
415 static int ftp_SendCommand( access_t *p_access, char *psz_fmt, ... )
416 {
417     access_sys_t *p_sys = p_access->p_sys;
418     va_list      args;
419     char         *psz_cmd;
420
421     va_start( args, psz_fmt );
422     vasprintf( &psz_cmd, psz_fmt, args );
423     va_end( args );
424
425     msg_Dbg( p_access, "ftp_SendCommand:\"%s\"", psz_cmd);
426     if( net_Printf( VLC_OBJECT(p_access), p_sys->fd_cmd, NULL, "%s\r\n",
427                     psz_cmd ) < 0 )
428     {
429         msg_Err( p_access, "failed to send command" );
430         return VLC_EGENERIC;
431     }
432     return VLC_SUCCESS;
433 }
434
435 /* TODO support this s**t :
436  RFC 959 allows the client to send certain TELNET strings at any moment,
437  even in the middle of a request:
438
439  * \377\377.
440  * \377\376x where x is one byte.
441  * \377\375x where x is one byte. The server is obliged to send \377\374x
442  *                                immediately after reading x.
443  * \377\374x where x is one byte.
444  * \377\373x where x is one byte. The server is obliged to send \377\376x
445  *                                immediately after reading x.
446  * \377x for any other byte x.
447
448  These strings are not part of the requests, except in the case \377\377,
449  where the request contains one \377. */
450 static int ftp_ReadCommand( access_t *p_access,
451                             int *pi_answer, char **ppsz_answer )
452 {
453     access_sys_t *p_sys = p_access->p_sys;
454     char         *psz_line;
455     int          i_answer;
456
457     psz_line = net_Gets( p_access, p_sys->fd_cmd, NULL );
458     msg_Dbg( p_access, "answer=%s", psz_line );
459     if( psz_line == NULL || strlen( psz_line ) < 3 )
460     {
461         msg_Err( p_access, "cannot get answer" );
462         if( psz_line ) free( psz_line );
463         if( pi_answer ) *pi_answer    = 500;
464         if( ppsz_answer ) *ppsz_answer  = NULL;
465         return -1;
466     }
467
468     if( psz_line[3] == '-' )    /* Multiple response */
469     {
470         char end[4];
471
472         memcpy( end, psz_line, 3 );
473         end[3] = ' ';
474
475         for( ;; )
476         {
477             char *psz_tmp = net_Gets( p_access, p_sys->fd_cmd, NULL );
478
479             if( psz_tmp == NULL )   /* Error */
480                 break;
481
482             if( !strncmp( psz_tmp, end, 4 ) )
483             {
484                 free( psz_tmp );
485                 break;
486             }
487             free( psz_tmp );
488         }
489     }
490
491     i_answer = atoi( psz_line );
492
493     if( pi_answer ) *pi_answer = i_answer;
494     if( ppsz_answer )
495     {
496         *ppsz_answer = psz_line;
497     }
498     else
499     {
500         free( psz_line );
501     }
502     return( i_answer / 100 );
503 }
504
505 static int ftp_StartStream( access_t *p_access, off_t i_start )
506 {
507     access_sys_t *p_sys = p_access->p_sys;
508
509     char psz_ipv4[16], *psz_ip;
510     int  i_answer;
511     char *psz_arg, *psz_parser;
512     unsigned  a1, a2, a3, a4, p1, p2;
513     int  i_port;
514
515     if( ( ftp_SendCommand( p_access, p_sys->b_epsv ? "EPSV" : "PASV" ) < 0 )
516      || ( ftp_ReadCommand( p_access, &i_answer, &psz_arg ) != 2 ) )
517     {
518         msg_Err( p_access, "cannot set passive mode" );
519         return VLC_EGENERIC;
520     }
521
522     psz_parser = strchr( psz_arg, '(' );
523     if( psz_parser == NULL )
524     {
525         free( psz_arg );
526         msg_Err( p_access, "cannot parse passive mode response" );
527         return VLC_EGENERIC;
528     }
529
530     if( p_sys->b_epsv )
531     {
532         char psz_fmt[7] = "(|||%u";
533         psz_fmt[1] = psz_fmt[2] = psz_fmt[3] = psz_parser[1];
534
535         if( sscanf( psz_parser, psz_fmt, &i_port ) < 1 )
536         {
537             free( psz_arg );
538             msg_Err( p_access, "cannot parse passive mode response" );
539             return VLC_EGENERIC;
540         }
541         /* FIXME: if psz_ip is a hostname/DNS to a cluster of FTP servers,
542          * this may fail because we'll end up on another server :(
543          * getpeername() + getnameinfo() should be used instead.
544          */
545         psz_ip = p_sys->url.psz_host;
546     }
547     else
548     {
549         if( ( sscanf( psz_parser, "(%u,%u,%u,%u,%u,%u", &a1, &a2, &a3, &a4,
550                       &p1, &p2 ) < 6 ) || ( a1 > 255 ) || ( a2 > 255 )
551          || ( a3 > 255 ) || ( a4 > 255 ) || ( p1 > 255 ) || ( p2 > 255 ) )
552         {
553             free( psz_arg );
554             msg_Err( p_access, "cannot parse passive mode response" );
555             return VLC_EGENERIC;
556         }
557
558         sprintf( psz_ipv4, "%u.%u.%u.%u", a1, a2, a3, a4 );
559         psz_ip = psz_ipv4;
560         i_port = (p1 << 8) | p2;
561     }
562     free( psz_arg );
563
564     msg_Dbg( p_access, "ip:%s port:%d", psz_ip, i_port );
565
566     if( ftp_SendCommand( p_access, "TYPE I" ) < 0 ||
567         ftp_ReadCommand( p_access, &i_answer, NULL ) != 2 )
568     {
569         msg_Err( p_access, "cannot set binary transfer mode" );
570         return VLC_EGENERIC;
571     }
572
573     if( i_start > 0 )
574     {
575         if( ftp_SendCommand( p_access, "REST "I64Fu, i_start ) < 0 ||
576             ftp_ReadCommand( p_access, &i_answer, NULL ) > 3 )
577         {
578             msg_Err( p_access, "cannot set restart point" );
579             return VLC_EGENERIC;
580         }
581     }
582
583     msg_Dbg( p_access, "waiting for data connection..." );
584     p_sys->fd_data = net_OpenTCP( p_access, psz_ip, i_port );
585     if( p_sys->fd_data < 0 )
586     {
587         msg_Err( p_access, "failed to connect with server" );
588         return VLC_EGENERIC;
589     }
590     msg_Dbg( p_access, "connection with \"%s:%d\" successful",
591              psz_ip, i_port );
592
593     /* "1xx" message */
594     if( ftp_SendCommand( p_access, "RETR %s", p_sys->url.psz_path ) < 0 ||
595         ftp_ReadCommand( p_access, &i_answer, NULL ) > 2 )
596     {
597         msg_Err( p_access, "cannot retreive file" );
598         return VLC_EGENERIC;
599     }
600     return VLC_SUCCESS;
601 }
602
603 static int ftp_StopStream ( access_t *p_access )
604 {
605     access_sys_t *p_sys = p_access->p_sys;
606
607     int i_answer;
608
609     if( ftp_SendCommand( p_access, "ABOR" ) < 0 )
610     {
611         msg_Warn( p_access, "cannot abord file" );
612         if(  p_sys->fd_data > 0 )
613             net_Close( p_sys->fd_data );
614         p_sys->fd_data = -1;
615         return VLC_EGENERIC;
616     }
617     if(  p_sys->fd_data > 0 )
618     {
619         net_Close( p_sys->fd_data );
620         p_sys->fd_data = -1;
621         ftp_ReadCommand( p_access, &i_answer, NULL );
622     }
623     ftp_ReadCommand( p_access, &i_answer, NULL );
624
625     return VLC_SUCCESS;
626 }
627