Remove legacy parameter from add_string()
[vlc/asuraparaju-public.git] / modules / access / ftp.c
blob7e172dfc550bc02919a77fed65e910e0126fdf78
1 /*****************************************************************************
2 * ftp.c: FTP input module
3 *****************************************************************************
4 * Copyright (C) 2001-2006 the VideoLAN team
5 * Copyright © 2006 Rémi Denis-Courmont
6 * $Id$
8 * Authors: Laurent Aimar <fenrir@via.ecp.fr> - original code
9 * Rémi Denis-Courmont <rem # videolan.org> - EPSV support
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
27 * Preamble
28 *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
36 #include <assert.h>
38 #include <vlc_access.h>
39 #include <vlc_dialog.h>
41 #include <vlc_network.h>
42 #include <vlc_url.h>
43 #include <vlc_sout.h>
44 #include <vlc_charset.h>
46 #ifndef IPPORT_FTP
47 # define IPPORT_FTP 21u
48 #endif
50 /*****************************************************************************
51 * Module descriptor
52 *****************************************************************************/
53 static int InOpen ( vlc_object_t * );
54 static void InClose( vlc_object_t * );
55 static int OutOpen ( vlc_object_t * );
56 static void OutClose( vlc_object_t * );
58 #define CACHING_TEXT N_("Caching value in ms")
59 #define CACHING_LONGTEXT N_( \
60 "Caching value for FTP streams. This " \
61 "value should be set in milliseconds." )
62 #define USER_TEXT N_("FTP user name")
63 #define USER_LONGTEXT N_("User name that will " \
64 "be used for the connection.")
65 #define PASS_TEXT N_("FTP password")
66 #define PASS_LONGTEXT N_("Password that will be " \
67 "used for the connection.")
68 #define ACCOUNT_TEXT N_("FTP account")
69 #define ACCOUNT_LONGTEXT N_("Account that will be " \
70 "used for the connection.")
72 vlc_module_begin ()
73 set_shortname( "FTP" )
74 set_description( N_("FTP input") )
75 set_capability( "access", 0 )
76 set_category( CAT_INPUT )
77 set_subcategory( SUBCAT_INPUT_ACCESS )
78 add_integer( "ftp-caching", 2 * DEFAULT_PTS_DELAY / 1000, NULL,
79 CACHING_TEXT, CACHING_LONGTEXT, true )
80 change_safe()
81 add_string( "ftp-user", "anonymous", USER_TEXT, USER_LONGTEXT,
82 false )
83 add_string( "ftp-pwd", "anonymous@example.com", PASS_TEXT,
84 PASS_LONGTEXT, false )
85 add_string( "ftp-account", "anonymous", ACCOUNT_TEXT,
86 ACCOUNT_LONGTEXT, false )
87 add_shortcut( "ftp" )
88 set_callbacks( InOpen, InClose )
90 add_submodule ()
91 set_shortname( "FTP" )
92 set_description( N_("FTP upload output") )
93 set_capability( "sout access", 0 )
94 set_category( CAT_SOUT )
95 set_subcategory( SUBCAT_SOUT_ACO )
96 add_shortcut( "ftp" )
97 set_callbacks( OutOpen, OutClose )
98 vlc_module_end ()
100 /*****************************************************************************
101 * Local prototypes
102 *****************************************************************************/
103 static ssize_t Read( access_t *, uint8_t *, size_t );
104 static ssize_t Write( sout_access_out_t *, block_t * );
105 static int Seek( access_t *, uint64_t );
106 static int OutSeek( sout_access_out_t *, off_t );
107 static int Control( access_t *, int, va_list );
109 struct access_sys_t
111 vlc_url_t url;
113 int fd_cmd;
114 int fd_data;
116 char sz_epsv_ip[NI_MAXNUMERICHOST];
117 bool out;
118 bool directory;
120 #define GET_OUT_SYS( p_this ) \
121 ((access_sys_t *)(((sout_access_out_t *)(p_this))->p_sys))
123 static int ftp_SendCommand( vlc_object_t *, access_sys_t *, const char *, ... );
124 static int ftp_ReadCommand( vlc_object_t *, access_sys_t *, int *, char ** );
125 static int ftp_StartStream( vlc_object_t *, access_sys_t *, uint64_t );
126 static int ftp_StopStream ( vlc_object_t *, access_sys_t * );
128 static int Login( vlc_object_t *p_access, access_sys_t *p_sys )
130 int i_answer;
131 char *psz;
133 /* *** Open a TCP connection with server *** */
134 int fd = p_sys->fd_cmd = net_ConnectTCP( p_access, p_sys->url.psz_host,
135 p_sys->url.i_port );
136 if( fd == -1 )
138 msg_Err( p_access, "connection failed" );
139 dialog_Fatal( p_access, _("Network interaction failed"), "%s",
140 _("VLC could not connect with the given server.") );
141 return -1;
144 while( ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) == 1 );
146 if( i_answer / 100 != 2 )
148 msg_Err( p_access, "connection rejected" );
149 dialog_Fatal( p_access, _("Network interaction failed"), "%s",
150 _("VLC's connection to the given server was rejected.") );
151 return -1;
154 msg_Dbg( p_access, "connection accepted (%d)", i_answer );
156 if( p_sys->url.psz_username && *p_sys->url.psz_username )
157 psz = strdup( p_sys->url.psz_username );
158 else
159 psz = var_InheritString( p_access, "ftp-user" );
160 if( !psz )
161 return -1;
163 if( ftp_SendCommand( p_access, p_sys, "USER %s", psz ) < 0 ||
164 ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) < 0 )
166 free( psz );
167 return -1;
169 free( psz );
171 switch( i_answer / 100 )
173 case 2:
174 msg_Dbg( p_access, "user accepted" );
175 break;
176 case 3:
177 msg_Dbg( p_access, "password needed" );
178 if( p_sys->url.psz_password && *p_sys->url.psz_password )
179 psz = strdup( p_sys->url.psz_password );
180 else
181 psz = var_InheritString( p_access, "ftp-pwd" );
182 if( !psz )
183 return -1;
185 if( ftp_SendCommand( p_access, p_sys, "PASS %s", psz ) < 0 ||
186 ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) < 0 )
188 free( psz );
189 return -1;
191 free( psz );
193 switch( i_answer / 100 )
195 case 2:
196 msg_Dbg( p_access, "password accepted" );
197 break;
198 case 3:
199 msg_Dbg( p_access, "account needed" );
200 psz = var_InheritString( p_access, "ftp-account" );
201 if( ftp_SendCommand( p_access, p_sys, "ACCT %s",
202 psz ) < 0 ||
203 ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) < 0 )
205 free( psz );
206 return -1;
208 free( psz );
210 if( i_answer / 100 != 2 )
212 msg_Err( p_access, "account rejected" );
213 dialog_Fatal( p_access,
214 _("Network interaction failed"),
215 "%s", _("Your account was rejected.") );
216 return -1;
218 msg_Dbg( p_access, "account accepted" );
219 break;
221 default:
222 msg_Err( p_access, "password rejected" );
223 dialog_Fatal( p_access, _("Network interaction failed"),
224 "%s", _("Your password was rejected.") );
225 return -1;
227 break;
228 default:
229 msg_Err( p_access, "user rejected" );
230 dialog_Fatal( p_access, _("Network interaction failed"), "%s",
231 _("Your connection attempt to the server was rejected.") );
232 return -1;
235 return 0;
238 static int Connect( vlc_object_t *p_access, access_sys_t *p_sys )
240 if( Login( p_access, p_sys ) < 0 )
241 return -1;
243 /* Extended passive mode */
244 if( ftp_SendCommand( p_access, p_sys, "EPSV ALL" ) < 0 )
246 msg_Err( p_access, "cannot request extended passive mode" );
247 net_Close( p_sys->fd_cmd );
248 return -1;
251 if( ftp_ReadCommand( p_access, p_sys, NULL, NULL ) == 2 )
253 if( net_GetPeerAddress( p_sys->fd_cmd, p_sys->sz_epsv_ip, NULL ) )
255 net_Close( p_sys->fd_cmd );
256 return -1;
259 else
261 /* If ESPV ALL fails, we fallback to PASV.
262 * We have to restart the connection in case there is a NAT that
263 * understands EPSV ALL in the way, and hence won't allow PASV on
264 * the initial connection.
266 msg_Info( p_access, "FTP Extended passive mode disabled" );
267 net_Close( p_sys->fd_cmd );
269 if( Login( p_access, p_sys ) )
271 net_Close( p_sys->fd_cmd );
272 return -1;
276 /* check binary mode support */
277 if( ftp_SendCommand( p_access, p_sys, "TYPE I" ) < 0 ||
278 ftp_ReadCommand( p_access, p_sys, NULL, NULL ) != 2 )
280 msg_Err( p_access, "cannot set binary transfer mode" );
281 net_Close( p_sys->fd_cmd );
282 return -1;
285 return 0;
289 static int parseURL( vlc_url_t *url, const char *path )
291 if( path == NULL )
292 return VLC_EGENERIC;
294 /* *** Parse URL and get server addr/port and path *** */
295 while( *path == '/' )
296 path++;
298 vlc_UrlParse( url, path, 0 );
300 if( url->psz_host == NULL || *url->psz_host == '\0' )
301 return VLC_EGENERIC;
303 if( url->i_port <= 0 )
304 url->i_port = IPPORT_FTP; /* default port */
306 if( url->psz_path == NULL )
307 return VLC_SUCCESS;
308 /* FTP URLs are relative to user's default directory (RFC1738 §3.2)
309 For absolute path use ftp://foo.bar//usr/local/etc/filename */
310 /* FIXME: we should issue a series of CWD, one per slash */
311 if( url->psz_path )
313 assert( url->psz_path[0] == '/' );
314 url->psz_path++;
317 char *type = strstr( url->psz_path, ";type=" );
318 if( type )
320 *type = '\0';
321 if( strchr( "iI", type[6] ) == NULL )
322 return VLC_EGENERIC; /* ASCII and directory not supported */
324 decode_URI( url->psz_path );
325 /* FIXME: check for UTF-8 support, otherwise only ASCII is allowed */
326 EnsureUTF8( url->psz_path );
327 return VLC_SUCCESS;
331 /****************************************************************************
332 * Open: connect to ftp server and ask for file
333 ****************************************************************************/
334 static int InOpen( vlc_object_t *p_this )
336 access_t *p_access = (access_t*)p_this;
337 access_sys_t *p_sys;
338 char *psz_arg;
340 /* Init p_access */
341 STANDARD_READ_ACCESS_INIT
342 p_sys->fd_data = -1;
343 p_sys->out = false;
344 p_sys->directory = false;
346 if( parseURL( &p_sys->url, p_access->psz_location ) )
347 goto exit_error;
349 if( Connect( p_this, p_sys ) )
350 goto exit_error;
352 /* get size */
353 if( p_sys->url.psz_path == NULL )
354 p_sys->directory = true;
355 else
356 if( ftp_SendCommand( p_this, p_sys, "SIZE %s", p_sys->url.psz_path ) < 0 )
357 goto error;
358 else
359 if ( ftp_ReadCommand( p_this, p_sys, NULL, &psz_arg ) == 2 )
361 p_access->info.i_size = atoll( &psz_arg[4] );
362 free( psz_arg );
363 msg_Dbg( p_access, "file size: %"PRIu64, p_access->info.i_size );
365 else
366 if( ftp_SendCommand( p_this, p_sys, "CWD %s", p_sys->url.psz_path ) < 0 )
367 goto error;
368 else
369 if( ftp_ReadCommand( p_this, p_sys, NULL, NULL ) != 2 )
371 msg_Err( p_access, "file or directory does not exist" );
372 goto error;
374 else
375 p_sys->directory = true;
377 /* Start the 'stream' */
378 if( ftp_StartStream( p_this, p_sys, 0 ) < 0 )
380 msg_Err( p_access, "cannot retrieve file" );
381 net_Close( p_sys->fd_cmd );
382 goto exit_error;
385 /* Update default_pts to a suitable value for ftp access */
386 var_Create( p_access, "ftp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
388 return VLC_SUCCESS;
390 error:
391 net_Close( p_sys->fd_cmd );
392 exit_error:
393 vlc_UrlClean( &p_sys->url );
394 free( p_sys );
395 return VLC_EGENERIC;
398 static int OutOpen( vlc_object_t *p_this )
400 sout_access_out_t *p_access = (sout_access_out_t *)p_this;
401 access_sys_t *p_sys;
403 p_sys = calloc( 1, sizeof( *p_sys ) );
404 if( !p_sys )
405 return VLC_ENOMEM;
407 /* Init p_access */
408 p_sys->fd_data = -1;
409 p_sys->out = true;
411 if( parseURL( &p_sys->url, p_access->psz_path ) )
412 goto exit_error;
413 if( p_sys->url.psz_path == NULL )
415 msg_Err( p_this, "no filename specified" );
416 goto exit_error;
419 if( Connect( p_this, p_sys ) )
420 goto exit_error;
422 /* Start the 'stream' */
423 if( ftp_StartStream( p_this, p_sys, 0 ) < 0 )
425 msg_Err( p_access, "cannot store file" );
426 net_Close( p_sys->fd_cmd );
427 goto exit_error;
430 p_access->pf_seek = OutSeek;
431 p_access->pf_write = Write;
432 p_access->p_sys = (void *)p_sys;
434 return VLC_SUCCESS;
436 exit_error:
437 vlc_UrlClean( &p_sys->url );
438 free( p_sys );
439 return VLC_EGENERIC;
442 /*****************************************************************************
443 * Close: free unused data structures
444 *****************************************************************************/
445 static void Close( vlc_object_t *p_access, access_sys_t *p_sys )
447 msg_Dbg( p_access, "stopping stream" );
448 ftp_StopStream( p_access, p_sys );
450 if( ftp_SendCommand( p_access, p_sys, "QUIT" ) < 0 )
452 msg_Warn( p_access, "cannot quit" );
454 else
456 ftp_ReadCommand( p_access, p_sys, NULL, NULL );
458 net_Close( p_sys->fd_cmd );
460 /* free memory */
461 vlc_UrlClean( &p_sys->url );
462 free( p_sys );
465 static void InClose( vlc_object_t *p_this )
467 Close( p_this, ((access_t *)p_this)->p_sys);
470 static void OutClose( vlc_object_t *p_this )
472 Close( p_this, GET_OUT_SYS(p_this));
476 /*****************************************************************************
477 * Seek: try to go at the right place
478 *****************************************************************************/
479 static int _Seek( vlc_object_t *p_access, access_sys_t *p_sys, uint64_t i_pos )
481 msg_Dbg( p_access, "seeking to %"PRIu64, i_pos );
483 ftp_StopStream( (vlc_object_t *)p_access, p_sys );
484 if( ftp_StartStream( (vlc_object_t *)p_access, p_sys, i_pos ) < 0 )
485 return VLC_EGENERIC;
487 return VLC_SUCCESS;
490 static int Seek( access_t *p_access, uint64_t i_pos )
492 int val = _Seek( (vlc_object_t *)p_access, p_access->p_sys, i_pos );
493 if( val )
494 return val;
496 p_access->info.b_eof = false;
497 p_access->info.i_pos = i_pos;
499 return VLC_SUCCESS;
502 static int OutSeek( sout_access_out_t *p_access, off_t i_pos )
504 return _Seek( (vlc_object_t *)p_access, GET_OUT_SYS( p_access ), i_pos);
507 /*****************************************************************************
508 * Read:
509 *****************************************************************************/
510 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
512 access_sys_t *p_sys = p_access->p_sys;
514 assert( p_sys->fd_data != -1 );
515 assert( !p_sys->out );
517 if( p_access->info.b_eof )
518 return 0;
520 if( p_sys->directory )
522 char *psz_line = net_Gets( p_access, p_sys->fd_data, NULL );
523 if( !psz_line )
525 p_access->info.b_eof = true;
526 return 0;
528 else
530 snprintf( (char*)p_buffer, i_len, "ftp://%s:%d/%s/%s\n",
531 p_sys->url.psz_host, p_sys->url.i_port,
532 p_sys->url.psz_path, psz_line );
533 free( psz_line );
534 return strlen( (const char *)p_buffer );
537 else
539 int i_read = net_Read( p_access, p_sys->fd_data, NULL,
540 p_buffer, i_len, false );
541 if( i_read == 0 )
542 p_access->info.b_eof = true;
543 else if( i_read > 0 )
544 p_access->info.i_pos += i_read;
546 return i_read;
550 /*****************************************************************************
551 * Write:
552 *****************************************************************************/
553 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
555 access_sys_t *p_sys = GET_OUT_SYS(p_access);
556 size_t i_write = 0;
558 assert( p_sys->fd_data != -1 );
560 while( p_buffer != NULL )
562 block_t *p_next = p_buffer->p_next;;
564 i_write += net_Write( p_access, p_sys->fd_data, NULL,
565 p_buffer->p_buffer, p_buffer->i_buffer );
566 block_Release( p_buffer );
568 p_buffer = p_next;
571 return i_write;
574 /*****************************************************************************
575 * Control:
576 *****************************************************************************/
577 static int Control( access_t *p_access, int i_query, va_list args )
579 bool *pb_bool;
580 int64_t *pi_64;
582 switch( i_query )
584 /* */
585 case ACCESS_CAN_SEEK:
586 pb_bool = (bool*)va_arg( args, bool* );
587 *pb_bool = !p_access->p_sys->directory;
588 break;
589 case ACCESS_CAN_FASTSEEK:
590 pb_bool = (bool*)va_arg( args, bool* );
591 *pb_bool = false;
592 break;
593 case ACCESS_CAN_PAUSE:
594 pb_bool = (bool*)va_arg( args, bool* );
595 *pb_bool = true; /* FIXME */
596 break;
597 case ACCESS_CAN_CONTROL_PACE:
598 pb_bool = (bool*)va_arg( args, bool* );
599 *pb_bool = true; /* FIXME */
600 break;
602 /* */
603 case ACCESS_GET_PTS_DELAY:
604 pi_64 = (int64_t*)va_arg( args, int64_t * );
605 *pi_64 = var_GetInteger( p_access, "ftp-caching" ) * INT64_C(1000);
606 break;
608 /* */
609 case ACCESS_SET_PAUSE_STATE:
610 pb_bool = (bool*)va_arg( args, bool* );
611 if ( !pb_bool )
612 return Seek( p_access, p_access->info.i_pos );
613 break;
615 case ACCESS_GET_TITLE_INFO:
616 case ACCESS_SET_TITLE:
617 case ACCESS_SET_SEEKPOINT:
618 case ACCESS_SET_PRIVATE_ID_STATE:
619 case ACCESS_GET_CONTENT_TYPE:
620 case ACCESS_GET_META:
621 return VLC_EGENERIC;
623 default:
624 msg_Warn( p_access, "unimplemented query in control: %d", i_query);
625 return VLC_EGENERIC;
628 return VLC_SUCCESS;
631 /*****************************************************************************
632 * ftp_*:
633 *****************************************************************************/
634 static int ftp_SendCommand( vlc_object_t *p_access, access_sys_t *p_sys,
635 const char *psz_fmt, ... )
637 va_list args;
638 char *psz_cmd;
640 va_start( args, psz_fmt );
641 if( vasprintf( &psz_cmd, psz_fmt, args ) == -1 )
642 return VLC_EGENERIC;
644 va_end( args );
646 msg_Dbg( p_access, "ftp_SendCommand:\"%s\"", psz_cmd);
648 if( net_Printf( p_access, p_sys->fd_cmd, NULL, "%s\r\n", psz_cmd ) < 0 )
650 msg_Err( p_access, "failed to send command" );
651 return VLC_EGENERIC;
653 return VLC_SUCCESS;
656 /* TODO support this s**t :
657 RFC 959 allows the client to send certain TELNET strings at any moment,
658 even in the middle of a request:
660 * \377\377.
661 * \377\376x where x is one byte.
662 * \377\375x where x is one byte. The server is obliged to send \377\374x
663 * immediately after reading x.
664 * \377\374x where x is one byte.
665 * \377\373x where x is one byte. The server is obliged to send \377\376x
666 * immediately after reading x.
667 * \377x for any other byte x.
669 These strings are not part of the requests, except in the case \377\377,
670 where the request contains one \377. */
671 static int ftp_ReadCommand( vlc_object_t *p_access, access_sys_t *p_sys,
672 int *pi_answer, char **ppsz_answer )
674 char *psz_line;
675 int i_answer;
677 psz_line = net_Gets( p_access, p_sys->fd_cmd, NULL );
678 if( psz_line == NULL || strlen( psz_line ) < 3 )
680 msg_Err( p_access, "cannot get answer" );
681 free( psz_line );
682 if( pi_answer ) *pi_answer = 500;
683 if( ppsz_answer ) *ppsz_answer = NULL;
684 return -1;
686 msg_Dbg( p_access, "answer=%s", psz_line );
688 if( psz_line[3] == '-' ) /* Multiple response */
690 char end[4];
692 memcpy( end, psz_line, 3 );
693 end[3] = ' ';
695 for( ;; )
697 char *psz_tmp = net_Gets( p_access, p_sys->fd_cmd, NULL );
699 if( psz_tmp == NULL ) /* Error */
700 break;
702 if( !strncmp( psz_tmp, end, 4 ) )
704 free( psz_tmp );
705 break;
707 free( psz_tmp );
711 i_answer = atoi( psz_line );
713 if( pi_answer ) *pi_answer = i_answer;
714 if( ppsz_answer )
716 *ppsz_answer = psz_line;
718 else
720 free( psz_line );
722 return( i_answer / 100 );
725 static int ftp_StartStream( vlc_object_t *p_access, access_sys_t *p_sys,
726 uint64_t i_start )
728 char psz_ipv4[16], *psz_ip = p_sys->sz_epsv_ip;
729 int i_answer;
730 char *psz_arg, *psz_parser;
731 int i_port;
733 assert( p_sys->fd_data == -1 );
735 if( ( ftp_SendCommand( p_access, p_sys, *psz_ip ? "EPSV" : "PASV" ) < 0 )
736 || ( ftp_ReadCommand( p_access, p_sys, &i_answer, &psz_arg ) != 2 ) )
738 msg_Err( p_access, "cannot set passive mode" );
739 return VLC_EGENERIC;
742 psz_parser = strchr( psz_arg, '(' );
743 if( psz_parser == NULL )
745 free( psz_arg );
746 msg_Err( p_access, "cannot parse passive mode response" );
747 return VLC_EGENERIC;
750 if( *psz_ip )
752 char psz_fmt[7] = "(|||%u";
753 psz_fmt[1] = psz_fmt[2] = psz_fmt[3] = psz_parser[1];
755 if( sscanf( psz_parser, psz_fmt, &i_port ) < 1 )
757 free( psz_arg );
758 msg_Err( p_access, "cannot parse passive mode response" );
759 return VLC_EGENERIC;
762 else
764 unsigned a1, a2, a3, a4, p1, p2;
766 if( ( sscanf( psz_parser, "(%u,%u,%u,%u,%u,%u", &a1, &a2, &a3, &a4,
767 &p1, &p2 ) < 6 ) || ( a1 > 255 ) || ( a2 > 255 )
768 || ( a3 > 255 ) || ( a4 > 255 ) || ( p1 > 255 ) || ( p2 > 255 ) )
770 free( psz_arg );
771 msg_Err( p_access, "cannot parse passive mode response" );
772 return VLC_EGENERIC;
775 sprintf( psz_ipv4, "%u.%u.%u.%u", a1, a2, a3, a4 );
776 psz_ip = psz_ipv4;
777 i_port = (p1 << 8) | p2;
779 free( psz_arg );
781 msg_Dbg( p_access, "ip:%s port:%d", psz_ip, i_port );
783 if( ftp_SendCommand( p_access, p_sys, "TYPE I" ) < 0 ||
784 ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) != 2 )
786 msg_Err( p_access, "cannot set binary transfer mode" );
787 return VLC_EGENERIC;
790 if( i_start > 0 )
792 if( ftp_SendCommand( p_access, p_sys, "REST %"PRIu64, i_start ) < 0 ||
793 ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) > 3 )
795 msg_Err( p_access, "cannot set restart offset" );
796 return VLC_EGENERIC;
800 msg_Dbg( p_access, "waiting for data connection..." );
801 p_sys->fd_data = net_ConnectTCP( p_access, psz_ip, i_port );
802 if( p_sys->fd_data < 0 )
804 msg_Err( p_access, "failed to connect with server" );
805 return VLC_EGENERIC;
807 msg_Dbg( p_access, "connection with \"%s:%d\" successful",
808 psz_ip, i_port );
810 if( p_sys->directory )
812 if( ftp_SendCommand( p_access, p_sys, "NLST" ) < 0 ||
813 ftp_ReadCommand( p_access, p_sys, NULL, &psz_arg ) > 2 )
815 msg_Err( p_access, "cannot list directory contents" );
816 return VLC_EGENERIC;
819 else
821 /* "1xx" message */
822 assert( p_sys->url.psz_path );
823 if( ftp_SendCommand( p_access, p_sys, "%s %s",
824 p_sys->out ? "STOR" : "RETR",
825 p_sys->url.psz_path ) < 0
826 || ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) > 2 )
828 msg_Err( p_access, "cannot retrieve file" );
829 return VLC_EGENERIC;
833 shutdown( p_sys->fd_data, p_sys->out ? SHUT_RD : SHUT_WR );
835 return VLC_SUCCESS;
838 static int ftp_StopStream ( vlc_object_t *p_access, access_sys_t *p_sys )
840 if( ftp_SendCommand( p_access, p_sys, "ABOR" ) < 0 )
842 msg_Warn( p_access, "cannot abort file" );
843 if( p_sys->fd_data > 0 )
844 net_Close( p_sys->fd_data );
845 p_sys->fd_data = -1;
846 return VLC_EGENERIC;
849 if( p_sys->fd_data != -1 )
851 net_Close( p_sys->fd_data );
852 p_sys->fd_data = -1;
853 /* Read the final response from RETR/STOR, i.e. 426 or 226 */
854 ftp_ReadCommand( p_access, p_sys, NULL, NULL );
856 /* Read the response from ABOR, i.e. 226 or 225 */
857 ftp_ReadCommand( p_access, p_sys, NULL, NULL );
859 return VLC_SUCCESS;