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