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