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