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