| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
#ifdef HAVE_CONFIG_H |
|---|
| 30 |
# include "config.h" |
|---|
| 31 |
#endif |
|---|
| 32 |
|
|---|
| 33 |
#include <vlc_common.h> |
|---|
| 34 |
#include <vlc_plugin.h> |
|---|
| 35 |
|
|---|
| 36 |
#include <assert.h> |
|---|
| 37 |
|
|---|
| 38 |
#include <vlc_access.h> |
|---|
| 39 |
#include <vlc_interface.h> |
|---|
| 40 |
|
|---|
| 41 |
#include <vlc_network.h> |
|---|
| 42 |
#include "vlc_url.h" |
|---|
| 43 |
#include <vlc_sout.h> |
|---|
| 44 |
|
|---|
| 45 |
#ifndef IPPORT_FTP |
|---|
| 46 |
# define IPPORT_FTP 21u |
|---|
| 47 |
#endif |
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
static int InOpen ( vlc_object_t * ); |
|---|
| 53 |
static void InClose( vlc_object_t * ); |
|---|
| 54 |
static int OutOpen ( vlc_object_t * ); |
|---|
| 55 |
static void OutClose( vlc_object_t * ); |
|---|
| 56 |
|
|---|
| 57 |
#define CACHING_TEXT N_("Caching value in ms") |
|---|
| 58 |
#define CACHING_LONGTEXT N_( \ |
|---|
| 59 |
"Caching value for FTP streams. This " \ |
|---|
| 60 |
"value should be set in milliseconds." ) |
|---|
| 61 |
#define USER_TEXT N_("FTP user name") |
|---|
| 62 |
#define USER_LONGTEXT N_("User name that will " \ |
|---|
| 63 |
"be used for the connection.") |
|---|
| 64 |
#define PASS_TEXT N_("FTP password") |
|---|
| 65 |
#define PASS_LONGTEXT N_("Password that will be " \ |
|---|
| 66 |
"used for the connection.") |
|---|
| 67 |
#define ACCOUNT_TEXT N_("FTP account") |
|---|
| 68 |
#define ACCOUNT_LONGTEXT N_("Account that will be " \ |
|---|
| 69 |
"used for the connection.") |
|---|
| 70 |
|
|---|
| 71 |
vlc_module_begin(); |
|---|
| 72 |
set_shortname( "FTP" ); |
|---|
| 73 |
set_description( N_("FTP input") ); |
|---|
| 74 |
set_capability( "access", 0 ); |
|---|
| 75 |
set_category( CAT_INPUT ); |
|---|
| 76 |
set_subcategory( SUBCAT_INPUT_ACCESS ); |
|---|
| 77 |
add_integer( "ftp-caching", 2 * DEFAULT_PTS_DELAY / 1000, NULL, |
|---|
| 78 |
CACHING_TEXT, CACHING_LONGTEXT, true ); |
|---|
| 79 |
add_string( "ftp-user", "anonymous", NULL, USER_TEXT, USER_LONGTEXT, |
|---|
| 80 |
false ); |
|---|
| 81 |
add_string( "ftp-pwd", "anonymous@example.com", NULL, PASS_TEXT, |
|---|
| 82 |
PASS_LONGTEXT, false ); |
|---|
| 83 |
add_string( "ftp-account", "anonymous", NULL, ACCOUNT_TEXT, |
|---|
| 84 |
ACCOUNT_LONGTEXT, false ); |
|---|
| 85 |
add_shortcut( "ftp" ); |
|---|
| 86 |
set_callbacks( InOpen, InClose ); |
|---|
| 87 |
|
|---|
| 88 |
add_submodule(); |
|---|
| 89 |
set_shortname( "FTP" ); |
|---|
| 90 |
set_description( N_("FTP upload output") ); |
|---|
| 91 |
set_capability( "sout access", 0 ); |
|---|
| 92 |
set_category( CAT_SOUT ); |
|---|
| 93 |
set_subcategory( SUBCAT_SOUT_ACO ); |
|---|
| 94 |
set_callbacks( OutOpen, OutClose ); |
|---|
| 95 |
vlc_module_end(); |
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
static ssize_t Read( access_t *, uint8_t *, size_t ); |
|---|
| 101 |
static ssize_t Write( sout_access_out_t *, block_t * ); |
|---|
| 102 |
static int Seek( access_t *, int64_t ); |
|---|
| 103 |
static int OutSeek( sout_access_out_t *, off_t ); |
|---|
| 104 |
static int Control( access_t *, int, va_list ); |
|---|
| 105 |
|
|---|
| 106 |
struct access_sys_t |
|---|
| 107 |
{ |
|---|
| 108 |
vlc_url_t url; |
|---|
| 109 |
|
|---|
| 110 |
int fd_cmd; |
|---|
| 111 |
int fd_data; |
|---|
| 112 |
|
|---|
| 113 |
char sz_epsv_ip[NI_MAXNUMERICHOST]; |
|---|
| 114 |
bool out; |
|---|
| 115 |
}; |
|---|
| 116 |
#define GET_OUT_SYS( p_this ) \ |
|---|
| 117 |
((access_sys_t *)(((sout_access_out_t *)(p_this))->p_sys)) |
|---|
| 118 |
|
|---|
| 119 |
static int ftp_SendCommand( vlc_object_t *, access_sys_t *, const char *, ... ); |
|---|
| 120 |
static int ftp_ReadCommand( vlc_object_t *, access_sys_t *, int *, char ** ); |
|---|
| 121 |
static int ftp_StartStream( vlc_object_t *, access_sys_t *, int64_t ); |
|---|
| 122 |
static int ftp_StopStream ( vlc_object_t *, access_sys_t * ); |
|---|
| 123 |
|
|---|
| 124 |
static int Login( vlc_object_t *p_access, access_sys_t *p_sys ) |
|---|
| 125 |
{ |
|---|
| 126 |
int i_answer; |
|---|
| 127 |
char *psz; |
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
int fd = p_sys->fd_cmd = net_ConnectTCP( p_access, p_sys->url.psz_host, |
|---|
| 131 |
p_sys->url.i_port ); |
|---|
| 132 |
if( fd == -1 ) |
|---|
| 133 |
{ |
|---|
| 134 |
msg_Err( p_access, "connection failed" ); |
|---|
| 135 |
intf_UserFatal( p_access, false, _("Network interaction failed"), |
|---|
| 136 |
_("VLC could not connect with the given server.") ); |
|---|
| 137 |
return -1; |
|---|
| 138 |
} |
|---|
| 139 |
|
|---|
| 140 |
while( ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) == 1 ); |
|---|
| 141 |
|
|---|
| 142 |
if( i_answer / 100 != 2 ) |
|---|
| 143 |
{ |
|---|
| 144 |
msg_Err( p_access, "connection rejected" ); |
|---|
| 145 |
intf_UserFatal( p_access, false, _("Network interaction failed"), |
|---|
| 146 |
_("VLC's connection to the given server was rejected.") ); |
|---|
| 147 |
return -1; |
|---|
| 148 |
} |
|---|
| 149 |
|
|---|
| 150 |
msg_Dbg( p_access, "connection accepted (%d)", i_answer ); |
|---|
| 151 |
|
|---|
| 152 |
if( p_sys->url.psz_username && *p_sys->url.psz_username ) |
|---|
| 153 |
psz = strdup( p_sys->url.psz_username ); |
|---|
| 154 |
else |
|---|
| 155 |
psz = var_CreateGetString( p_access, "ftp-user" ); |
|---|
| 156 |
|
|---|
| 157 |
if( ftp_SendCommand( p_access, p_sys, "USER %s", psz ) < 0 || |
|---|
| 158 |
ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) < 0 ) |
|---|
| 159 |
{ |
|---|
| 160 |
free( psz ); |
|---|
| 161 |
return -1; |
|---|
| 162 |
} |
|---|
| 163 |
free( psz ); |
|---|
| 164 |
|
|---|
| 165 |
switch( i_answer / 100 ) |
|---|
| 166 |
{ |
|---|
| 167 |
case 2: |
|---|
| 168 |
msg_Dbg( p_access, "user accepted" ); |
|---|
| 169 |
break; |
|---|
| 170 |
case 3: |
|---|
| 171 |
msg_Dbg( p_access, "password needed" ); |
|---|
| 172 |
if( p_sys->url.psz_password && *p_sys->url.psz_password ) |
|---|
| 173 |
psz = strdup( p_sys->url.psz_password ); |
|---|
| 174 |
else |
|---|
| 175 |
psz = var_CreateGetString( p_access, "ftp-pwd" ); |
|---|
| 176 |
|
|---|
| 177 |
if( ftp_SendCommand( p_access, p_sys, "PASS %s", psz ) < 0 || |
|---|
| 178 |
ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) < 0 ) |
|---|
| 179 |
{ |
|---|
| 180 |
free( psz ); |
|---|
| 181 |
return -1; |
|---|
| 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, p_sys, "ACCT %s", |
|---|
| 194 |
psz ) < 0 || |
|---|
| 195 |
ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) < 0 ) |
|---|
| 196 |
{ |
|---|
| 197 |
free( psz ); |
|---|
| 198 |
return -1; |
|---|
| 199 |
} |
|---|
| 200 |
free( psz ); |
|---|
| 201 |
|
|---|
| 202 |
if( i_answer / 100 != 2 ) |
|---|
| 203 |
{ |
|---|
| 204 |
msg_Err( p_access, "account rejected" ); |
|---|
| 205 |
intf_UserFatal( p_access, false, |
|---|
| 206 |
_("Network interaction failed"), |
|---|
| 207 |
_("Your account was rejected.") ); |
|---|
| 208 |
return -1; |
|---|
| 209 |
} |
|---|
| 210 |
msg_Dbg( p_access, "account accepted" ); |
|---|
| 211 |
break; |
|---|
| 212 |
|
|---|
| 213 |
default: |
|---|
| 214 |
msg_Err( p_access, "password rejected" ); |
|---|
| 215 |
intf_UserFatal( p_access, false, |
|---|
| 216 |
_("Network interaction failed"), |
|---|
| 217 |
_("Your password was rejected.") ); |
|---|
| 218 |
return -1; |
|---|
| 219 |
} |
|---|
| 220 |
break; |
|---|
| 221 |
default: |
|---|
| 222 |
msg_Err( p_access, "user rejected" ); |
|---|
| 223 |
intf_UserFatal( p_access, false, |
|---|
| 224 |
_("Network interaction failed"), |
|---|
| 225 |
_("Your connection attempt to the server was rejected.") ); |
|---|
| 226 |
return -1; |
|---|
| 227 |
} |
|---|
| 228 |
|
|---|
| 229 |
return 0; |
|---|
| 230 |
} |
|---|
| 231 |
|
|---|
| 232 |
static int Connect( vlc_object_t *p_access, access_sys_t *p_sys ) |
|---|
| 233 |
{ |
|---|
| 234 |
if( Login( p_access, p_sys ) < 0 ) |
|---|
| 235 |
return -1; |
|---|
| 236 |
|
|---|
| 237 |
|
|---|
| 238 |
if( ftp_SendCommand( p_access, p_sys, "EPSV ALL" ) < 0 ) |
|---|
| 239 |
{ |
|---|
| 240 |
msg_Err( p_access, "cannot request extended passive mode" ); |
|---|
| 241 |
net_Close( p_sys->fd_cmd ); |
|---|
| 242 |
return -1; |
|---|
| 243 |
} |
|---|
| 244 |
|
|---|
| 245 |
if( ftp_ReadCommand( p_access, p_sys, NULL, NULL ) == 2 ) |
|---|
| 246 |
{ |
|---|
| 247 |
if( net_GetPeerAddress( p_sys->fd_cmd, p_sys->sz_epsv_ip, NULL ) ) |
|---|
| 248 |
{ |
|---|
| 249 |
net_Close( p_sys->fd_cmd ); |
|---|
| 250 |
return -1; |
|---|
| 251 |
} |
|---|
| 252 |
} |
|---|
| 253 |
else |
|---|
| 254 |
{ |
|---|
| 255 |
|
|---|
| 256 |
|
|---|
| 257 |
|
|---|
| 258 |
|
|---|
| 259 |
|
|---|
| 260 |
msg_Info( p_access, "FTP Extended passive mode disabled" ); |
|---|
| 261 |
net_Close( p_sys->fd_cmd ); |
|---|
| 262 |
|
|---|
| 263 |
if( Login( p_access, p_sys ) ) |
|---|
| 264 |
{ |
|---|
| 265 |
net_Close( p_sys->fd_cmd ); |
|---|
| 266 |
return -1; |
|---|
| 267 |
} |
|---|
| 268 |
} |
|---|
| 269 |
|
|---|
| 270 |
|
|---|
| 271 |
if( ftp_SendCommand( p_access, p_sys, "TYPE I" ) < 0 || |
|---|
| 272 |
ftp_ReadCommand( p_access, p_sys, NULL, NULL ) != 2 ) |
|---|
| 273 |
{ |
|---|
| 274 |
msg_Err( p_access, "cannot set binary transfer mode" ); |
|---|
| 275 |
net_Close( p_sys->fd_cmd ); |
|---|
| 276 |
return -1; |
|---|
| 277 |
} |
|---|
| 278 |
|
|---|
| 279 |
return 0; |
|---|
| 280 |
} |
|---|
| 281 |
|
|---|
| 282 |
|
|---|
| 283 |
static int parseURL( vlc_url_t *url, const char *path ) |
|---|
| 284 |
{ |
|---|
| 285 |
if( path == NULL ) |
|---|
| 286 |
return -1; |
|---|
| 287 |
|
|---|
| 288 |
|
|---|
| 289 |
while( *path == '/' ) |
|---|
| 290 |
path++; |
|---|
| 291 |
|
|---|
| 292 |
vlc_UrlParse( url, path, 0 ); |
|---|
| 293 |
|
|---|
| 294 |
if( url->psz_host == NULL || *url->psz_host == '\0' ) |
|---|
| 295 |
return -1; |
|---|
| 296 |
|
|---|
| 297 |
if( url->i_port <= 0 ) |
|---|
| 298 |
url->i_port = IPPORT_FTP; |
|---|
| 299 |
|
|---|
| 300 |
|
|---|
| 301 |
|
|---|
| 302 |
|
|---|
| 303 |
if( *url->psz_path == '/' ) |
|---|
| 304 |
url->psz_path++; |
|---|
| 305 |
|
|---|
| 306 |
return 0; |
|---|
| 307 |
} |
|---|
| 308 |
|
|---|
| 309 |
|
|---|
| 310 |
|
|---|
| 311 |
|
|---|
| 312 |
|
|---|
| 313 |
static int InOpen( vlc_object_t *p_this ) |
|---|
| 314 |
{ |
|---|
| 315 |
access_t *p_access = (access_t*)p_this; |
|---|
| 316 |
access_sys_t *p_sys; |
|---|
| 317 |
char *psz_arg; |
|---|
| 318 |
|
|---|
| 319 |
|
|---|
| 320 |
STANDARD_READ_ACCESS_INIT |
|---|
| 321 |
p_sys->fd_data = -1; |
|---|
| 322 |
p_sys->out = false; |
|---|
| 323 |
|
|---|
| 324 |
if( parseURL( &p_sys->url, p_access->psz_path ) ) |
|---|
| 325 |
goto exit_error; |
|---|
| 326 |
|
|---|
| 327 |
if( Connect( p_this, p_sys ) ) |
|---|
| 328 |
goto exit_error; |
|---|
| 329 |
|
|---|
| 330 |
|
|---|
| 331 |
if( ftp_SendCommand( p_this, p_sys, "SIZE %s", p_sys->url.psz_path ) < 0 || |
|---|
| 332 |
ftp_ReadCommand( p_this, p_sys, NULL, &psz_arg ) != 2 ) |
|---|
| 333 |
{ |
|---|
| 334 |
msg_Err( p_access, "cannot get file size" ); |
|---|
| 335 |
net_Close( p_sys->fd_cmd ); |
|---|
| 336 |
goto exit_error; |
|---|
| 337 |
} |
|---|
| 338 |
p_access->info.i_size = atoll( &psz_arg[4] ); |
|---|
| 339 |
free( psz_arg ); |
|---|
| 340 |
msg_Dbg( p_access, "file size: %"PRId64, p_access->info.i_size ); |
|---|
| 341 |
|
|---|
| 342 |
|
|---|
| 343 |
if( ftp_StartStream( p_this, p_sys, 0 ) < 0 ) |
|---|
| 344 |
{ |
|---|
| 345 |
msg_Err( p_access, "cannot retrieve file" ); |
|---|
| 346 |
net_Close( p_sys->fd_cmd ); |
|---|
| 347 |
goto exit_error; |
|---|
| 348 |
} |
|---|
| 349 |
|
|---|
| 350 |
|
|---|
| 351 |
var_Create( p_access, "ftp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT ); |
|---|
| 352 |
|
|---|
| 353 |
return VLC_SUCCESS; |
|---|
| 354 |
|
|---|
| 355 |
exit_error: |
|---|
| 356 |
vlc_UrlClean( &p_sys->url ); |
|---|
| 357 |
free( p_sys ); |
|---|
| 358 |
return VLC_EGENERIC; |
|---|
| 359 |
} |
|---|
| 360 |
|
|---|
| 361 |
static int OutOpen( vlc_object_t *p_this ) |
|---|
| 362 |
{ |
|---|
| 363 |
sout_access_out_t *p_access = (sout_access_out_t *)p_this; |
|---|
| 364 |
access_sys_t *p_sys; |
|---|
| 365 |
|
|---|
| 366 |
p_sys = malloc( sizeof( *p_sys ) ); |
|---|
| 367 |
if( p_sys == NULL ) |
|---|
| 368 |
return VLC_ENOMEM; |
|---|
| 369 |
memset( p_sys, 0, sizeof( *p_sys ) ); |
|---|
| 370 |
|
|---|
| 371 |
|
|---|
| 372 |
p_sys->fd_data = -1; |
|---|
| 373 |
p_sys->out = true; |
|---|
| 374 |
|
|---|
| 375 |
if( parseURL( &p_sys->url, p_access->psz_path ) ) |
|---|
| 376 |
goto exit_error; |
|---|
| 377 |
|
|---|
| 378 |
if( Connect( p_this, p_sys ) ) |
|---|
| 379 |
goto exit_error; |
|---|
| 380 |
|
|---|
| 381 |
|
|---|
| 382 |
if( ftp_StartStream( p_this, p_sys, 0 ) < 0 ) |
|---|
| 383 |
{ |
|---|
| 384 |
msg_Err( p_access, "cannot store file" ); |
|---|
| 385 |
net_Close( p_sys->fd_cmd ); |
|---|
| 386 |
goto exit_error; |
|---|
| 387 |
} |
|---|
| 388 |
|
|---|
| 389 |
p_access->pf_seek = OutSeek; |
|---|
| 390 |
p_access->pf_write = Write; |
|---|
| 391 |
p_access->p_sys = (void *)p_sys; |
|---|
| 392 |
|
|---|
| 393 |
return VLC_SUCCESS; |
|---|
| 394 |
|
|---|
| 395 |
exit_error: |
|---|
| 396 |
vlc_UrlClean( &p_sys->url ); |
|---|
| 397 |
free( p_sys ); |
|---|
| 398 |
return VLC_EGENERIC; |
|---|
| 399 |
} |
|---|
| 400 |
|
|---|
| 401 |
|
|---|
| 402 |
|
|---|
| 403 |
|
|---|
| 404 |
static void Close( vlc_object_t *p_access, access_sys_t *p_sys ) |
|---|
| 405 |
{ |
|---|
| 406 |
msg_Dbg( p_access, "stopping stream" ); |
|---|
| 407 |
ftp_StopStream( p_access, p_sys ); |
|---|
| 408 |
|
|---|
| 409 |
if( ftp_SendCommand( p_access, p_sys, "QUIT" ) < 0 ) |
|---|
| 410 |
{ |
|---|
| 411 |
msg_Warn( p_access, "cannot quit" ); |
|---|
| 412 |
} |
|---|
| 413 |
else |
|---|
| 414 |
{ |
|---|
| 415 |
ftp_ReadCommand( p_access, p_sys, NULL, NULL ); |
|---|
| 416 |
} |
|---|
| 417 |
net_Close( p_sys->fd_cmd ); |
|---|
| 418 |
|
|---|
| 419 |
|
|---|
| 420 |
vlc_UrlClean( &p_sys->url ); |
|---|
| 421 |
free( p_sys ); |
|---|
| 422 |
} |
|---|
| 423 |
|
|---|
| 424 |
static void InClose( vlc_object_t *p_this ) |
|---|
| 425 |
{ |
|---|
| 426 |
Close( p_this, ((access_t *)p_this)->p_sys); |
|---|
| 427 |
} |
|---|
| 428 |
|
|---|
| 429 |
static void OutClose( vlc_object_t *p_this ) |
|---|
| 430 |
{ |
|---|
| 431 |
Close( p_this, GET_OUT_SYS(p_this)); |
|---|
| 432 |
} |
|---|
| 433 |
|
|---|
| 434 |
|
|---|
| 435 |
|
|---|
| 436 |
|
|---|
| 437 |
|
|---|
| 438 |
static int _Seek( vlc_object_t *p_access, access_sys_t *p_sys, int64_t i_pos ) |
|---|
| 439 |
{ |
|---|
| 440 |
if( i_pos < 0 ) |
|---|
| 441 |
return VLC_EGENERIC; |
|---|
| 442 |
|
|---|
| 443 |
msg_Dbg( p_access, "seeking to %"PRId64, i_pos ); |
|---|
| 444 |
|
|---|
| 445 |
ftp_StopStream( (vlc_object_t *)p_access, p_sys ); |
|---|
| 446 |
if( ftp_StartStream( (vlc_object_t *)p_access, p_sys, i_pos ) < 0 ) |
|---|
| 447 |
return VLC_EGENERIC; |
|---|
| 448 |
|
|---|
| 449 |
return VLC_SUCCESS; |
|---|
| 450 |
} |
|---|
| 451 |
|
|---|
| 452 |
static int Seek( access_t *p_access, int64_t i_pos ) |
|---|
| 453 |
{ |
|---|
| 454 |
int val = _Seek( (vlc_object_t *)p_access, p_access->p_sys, i_pos ); |
|---|
| 455 |
if( val ) |
|---|
| 456 |
return val; |
|---|
| 457 |
|
|---|
| 458 |
p_access->info.b_eof = false; |
|---|
| 459 |
p_access->info.i_pos = i_pos; |
|---|
| 460 |
|
|---|
| 461 |
return VLC_SUCCESS; |
|---|
| 462 |
} |
|---|
| 463 |
|
|---|
| 464 |
static int OutSeek( sout_access_out_t *p_access, off_t i_pos ) |
|---|
| 465 |
{ |
|---|
| 466 |
return _Seek( (vlc_object_t *)p_access, GET_OUT_SYS( p_access ), i_pos); |
|---|
| 467 |
} |
|---|
| 468 |
|
|---|
| 469 |
|
|---|
| 470 |
|
|---|
| 471 |
|
|---|
| 472 |
static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len ) |
|---|
| 473 |
{ |
|---|
| 474 |
access_sys_t *p_sys = p_access->p_sys; |
|---|
| 475 |
int i_read; |
|---|
| 476 |
|
|---|
| 477 |
assert( p_sys->fd_data != -1 ); |
|---|
| 478 |
assert( !p_sys->out ); |
|---|
| 479 |
|
|---|
| 480 |
if( p_access->info.b_eof ) |
|---|
| 481 |
return 0; |
|---|
| 482 |
|
|---|
| 483 |
i_read = net_Read( p_access, p_sys->fd_data, NULL, p_buffer, i_len, |
|---|
| 484 |
false ); |
|---|
| 485 |
if( i_read == 0 ) |
|---|
| 486 |
p_access->info.b_eof = true; |
|---|
| 487 |
else if( i_read > 0 ) |
|---|
| 488 |
p_access->info.i_pos += i_read; |
|---|
| 489 |
|
|---|
| 490 |
return i_read; |
|---|
| 491 |
} |
|---|
| 492 |
|
|---|
| 493 |
|
|---|
| 494 |
|
|---|
| 495 |
|
|---|
| 496 |
static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer ) |
|---|
| 497 |
{ |
|---|
| 498 |
access_sys_t *p_sys = GET_OUT_SYS(p_access); |
|---|
| 499 |
size_t i_write = 0; |
|---|
| 500 |
|
|---|
| 501 |
assert( p_sys->fd_data != -1 ); |
|---|
| 502 |
|
|---|
| 503 |
while( p_buffer != NULL ) |
|---|
| 504 |
{ |
|---|
| 505 |
block_t *p_next = p_buffer->p_next;; |
|---|
| 506 |
|
|---|
| 507 |
i_write += net_Write( p_access, p_sys->fd_data, NULL, |
|---|
| 508 |
p_buffer->p_buffer, p_buffer->i_buffer ); |
|---|
| 509 |
block_Release( p_buffer ); |
|---|
| 510 |
|
|---|
| 511 |
p_buffer = p_next; |
|---|
| 512 |
} |
|---|
| 513 |
|
|---|
| 514 |
return i_write; |
|---|
| 515 |
} |
|---|
| 516 |
|
|---|
| 517 |
|
|---|
| 518 |
|
|---|
| 519 |
|
|---|
| 520 |
static int Control( access_t *p_access, int i_query, va_list args ) |
|---|
| 521 |
{ |
|---|
| 522 |
bool *pb_bool; |
|---|
| 523 |
int *pi_int; |
|---|
| 524 |
int64_t *pi_64; |
|---|
| 525 |
vlc_value_t val; |
|---|
| 526 |
|
|---|
| 527 |
switch( i_query ) |
|---|
| 528 |
{ |
|---|
| 529 |
|
|---|
| 530 |
case ACCESS_CAN_SEEK: |
|---|
| 531 |
pb_bool = (bool*)va_arg( args, bool* ); |
|---|
| 532 |
*pb_bool = true; |
|---|
| 533 |
break; |
|---|
| 534 |
case ACCESS_CAN_FASTSEEK: |
|---|
| 535 |
pb_bool = (bool*)va_arg( args, bool* ); |
|---|
| 536 |
*pb_bool = false; |
|---|
| 537 |
break; |
|---|
| 538 |
case ACCESS_CAN_PAUSE: |
|---|
| 539 |
pb_bool = (bool*)va_arg( args, bool* ); |
|---|
| 540 |
*pb_bool = true; |
|---|
| 541 |
break; |
|---|
| 542 |
case ACCESS_CAN_CONTROL_PACE: |
|---|
| 543 |
pb_bool = (bool*)va_arg( args, bool* ); |
|---|
| 544 |
*pb_bool = true; |
|---|
| 545 |
break; |
|---|
| 546 |
|
|---|
| 547 |
|
|---|
| 548 |
case ACCESS_GET_MTU: |
|---|
| 549 |
pi_int = (int*)va_arg( args, int * ); |
|---|
| 550 |
*pi_int = 0; |
|---|
| 551 |
break; |
|---|
| 552 |
|
|---|
| 553 |
case ACCESS_GET_PTS_DELAY: |
|---|
| 554 |
pi_64 = (int64_t*)va_arg( args, int64_t * ); |
|---|
| 555 |
var_Get( p_access, "ftp-caching", &val ); |
|---|
| 556 |
*pi_64 = (int64_t)var_GetInteger( p_access, "ftp-caching" ) * INT64_C(1000); |
|---|
| 557 |
break; |
|---|
| 558 |
|
|---|
| 559 |
|
|---|
| 560 |
case ACCESS_SET_PAUSE_STATE: |
|---|
| 561 |
pb_bool = (bool*)va_arg( args, bool* ); |
|---|
| 562 |
if ( !pb_bool ) |
|---|
| 563 |
return Seek( p_access, p_access->info.i_pos ); |
|---|
| 564 |
break; |
|---|
| 565 |
|
|---|
| 566 |
case ACCESS_GET_TITLE_INFO: |
|---|
| 567 |
case ACCESS_SET_TITLE: |
|---|
| 568 |
case ACCESS_SET_SEEKPOINT: |
|---|
| 569 |
case ACCESS_SET_PRIVATE_ID_STATE: |
|---|
| 570 |
case ACCESS_GET_CONTENT_TYPE: |
|---|
| 571 |
case ACCESS_GET_META: |
|---|
| 572 |
return VLC_EGENERIC; |
|---|
| 573 |
|
|---|
| 574 |
default: |
|---|
| 575 |
msg_Warn( p_access, "unimplemented query in control: %d", i_query); |
|---|
| 576 |
return VLC_EGENERIC; |
|---|
| 577 |
|
|---|
| 578 |
} |
|---|
| 579 |
return VLC_SUCCESS; |
|---|
| 580 |
} |
|---|
| 581 |
|
|---|
| 582 |
|
|---|
| 583 |
|
|---|
| 584 |
|
|---|
| 585 |
static int ftp_SendCommand( vlc_object_t *p_access, access_sys_t *p_sys, |
|---|
| 586 |
const char *psz_fmt, ... ) |
|---|
| 587 |
{ |
|---|
| 588 |
va_list args; |
|---|
| 589 |
char *psz_cmd; |
|---|
| 590 |
|
|---|
| 591 |
va_start( args, psz_fmt ); |
|---|
| 592 |
if( vasprintf( &psz_cmd, psz_fmt, args ) == -1 ) |
|---|
| 593 |
return VLC_EGENERIC; |
|---|
| 594 |
|
|---|
| 595 |
va_end( args ); |
|---|
| 596 |
|
|---|
| 597 |
msg_Dbg( p_access, "ftp_SendCommand:\"%s\"", psz_cmd); |
|---|
| 598 |
|
|---|
| 599 |
if( net_Printf( VLC_OBJECT(p_access), p_sys->fd_cmd, NULL, "%s\r\n", |
|---|
| 600 |
psz_cmd ) < 0 ) |
|---|
| 601 |
{ |
|---|
| 602 |
msg_Err( p_access, "failed to send command" ); |
|---|
| 603 |
return VLC_EGENERIC; |
|---|
| 604 |
} |
|---|
| 605 |
return VLC_SUCCESS; |
|---|
| 606 |
} |
|---|
| 607 |
|
|---|
| 608 |
|
|---|
| 609 |
|
|---|
| 610 |
|
|---|
| 611 |
|
|---|
| 612 |
|
|---|
| 613 |
|
|---|
| 614 |
|
|---|
| 615 |
|
|---|
| 616 |
|
|---|
| 617 |
|
|---|
| 618 |
|
|---|
| 619 |
|
|---|
| 620 |
|
|---|
| 621 |
|
|---|
| 622 |
|
|---|
| 623 |
static int ftp_ReadCommand( vlc_object_t *p_access, access_sys_t *p_sys, |
|---|
| 624 |
int *pi_answer, char **ppsz_answer ) |
|---|
| 625 |
{ |
|---|
| 626 |
char *psz_line; |
|---|
| 627 |
int i_answer; |
|---|
| 628 |
|
|---|
| 629 |
psz_line = net_Gets( p_access, p_sys->fd_cmd, NULL ); |
|---|
| 630 |
if( psz_line == NULL || strlen( psz_line ) < 3 ) |
|---|
| 631 |
{ |
|---|
| 632 |
msg_Err( p_access, "cannot get answer" ); |
|---|
| 633 |
free( psz_line ); |
|---|
| 634 |
if( pi_answer ) *pi_answer = 500; |
|---|
| 635 |
if( ppsz_answer ) *ppsz_answer = NULL; |
|---|
| 636 |
return -1; |
|---|
| 637 |
} |
|---|
| 638 |
msg_Dbg( p_access, "answer=%s", psz_line ); |
|---|
| 639 |
|
|---|
| 640 |
if( psz_line[3] == '-' ) |
|---|
| 641 |
{ |
|---|
| 642 |
char end[4]; |
|---|
| 643 |
|
|---|
| 644 |
memcpy( end, psz_line, 3 ); |
|---|
| 645 |
end[3] = ' '; |
|---|
| 646 |
|
|---|
| 647 |
for( ;; ) |
|---|
| 648 |
{ |
|---|
| 649 |
char *psz_tmp = net_Gets( p_access, p_sys->fd_cmd, NULL ); |
|---|
| 650 |
|
|---|
| 651 |
if( psz_tmp == NULL ) |
|---|
| 652 |
break; |
|---|
| 653 |
|
|---|
| 654 |
if( !strncmp( psz_tmp, end, 4 ) ) |
|---|
| 655 |
{ |
|---|
| 656 |
free( psz_tmp ); |
|---|
| 657 |
break; |
|---|
| 658 |
} |
|---|
| 659 |
free( psz_tmp ); |
|---|
| 660 |
} |
|---|
| 661 |
} |
|---|
| 662 |
|
|---|
| 663 |
i_answer = atoi( psz_line ); |
|---|
| 664 |
|
|---|
| 665 |
if( pi_answer ) *pi_answer = i_answer; |
|---|
| 666 |
if( ppsz_answer ) |
|---|
| 667 |
{ |
|---|
| 668 |
*ppsz_answer = psz_line; |
|---|
| 669 |
} |
|---|
| 670 |
else |
|---|
| 671 |
{ |
|---|
| 672 |
free( psz_line ); |
|---|
| 673 |
} |
|---|
| 674 |
return( i_answer / 100 ); |
|---|
| 675 |
} |
|---|
| 676 |
|
|---|
| 677 |
static int ftp_StartStream( vlc_object_t *p_access, access_sys_t *p_sys, |
|---|
| 678 |
int64_t i_start ) |
|---|
| 679 |
{ |
|---|
| 680 |
char psz_ipv4[16], *psz_ip = p_sys->sz_epsv_ip; |
|---|
| 681 |
int i_answer; |
|---|
| 682 |
char *psz_arg, *psz_parser; |
|---|
| 683 |
int i_port; |
|---|
| 684 |
|
|---|
| 685 |
assert( p_sys->fd_data == -1 ); |
|---|
| 686 |
|
|---|
| 687 |
if( ( ftp_SendCommand( p_access, p_sys, *psz_ip ? "EPSV" : "PASV" ) < 0 ) |
|---|
| 688 |
|| ( ftp_ReadCommand( p_access, p_sys, &i_answer, &psz_arg ) != 2 ) ) |
|---|
| 689 |
{ |
|---|
| 690 |
msg_Err( p_access, "cannot set passive mode" ); |
|---|
| 691 |
return VLC_EGENERIC; |
|---|
| 692 |
} |
|---|
| 693 |
|
|---|
| 694 |
psz_parser = strchr( psz_arg, '(' ); |
|---|
| 695 |
if( psz_parser == NULL ) |
|---|
| 696 |
{ |
|---|
| 697 |
free( psz_arg ); |
|---|
| 698 |
msg_Err( p_access, "cannot parse passive mode response" ); |
|---|
| 699 |
return VLC_EGENERIC; |
|---|
| 700 |
} |
|---|
| 701 |
|
|---|
| 702 |
if( *psz_ip ) |
|---|
| 703 |
{ |
|---|
| 704 |
char psz_fmt[7] = "(|||%u"; |
|---|
| 705 |
psz_fmt[1] = psz_fmt[2] = psz_fmt[3] = psz_parser[1]; |
|---|
| 706 |
|
|---|
| 707 |
if( sscanf( psz_parser, psz_fmt, &i_port ) < 1 ) |
|---|
| 708 |
{ |
|---|
| 709 |
free( psz_arg ); |
|---|
| 710 |
msg_Err( p_access, "cannot parse passive mode response" ); |
|---|
| 711 |
return VLC_EGENERIC; |
|---|
| 712 |
} |
|---|
| 713 |
} |
|---|
| 714 |
else |
|---|
| 715 |
{ |
|---|
| 716 |
unsigned a1, a2, a3, a4, p1, p2; |
|---|
| 717 |
|
|---|
| 718 |
if( ( sscanf( psz_parser, "(%u,%u,%u,%u,%u,%u", &a1, &a2, &a3, &a4, |
|---|
| 719 |
&p1, &p2 ) < 6 ) || ( a1 > 255 ) || ( a2 > 255 ) |
|---|
| 720 |
|| ( a3 > 255 ) || ( a4 > 255 ) || ( p1 > 255 ) || ( p2 > 255 ) ) |
|---|
| 721 |
{ |
|---|
| 722 |
free( psz_arg ); |
|---|
| 723 |
msg_Err( p_access, "cannot parse passive mode response" ); |
|---|
| 724 |
return VLC_EGENERIC; |
|---|
| 725 |
} |
|---|
| 726 |
|
|---|
| 727 |
sprintf( psz_ipv4, "%u.%u.%u.%u", a1, a2, a3, a4 ); |
|---|
| 728 |
psz_ip = psz_ipv4; |
|---|
| 729 |
i_port = (p1 << 8) | p2; |
|---|
| 730 |
} |
|---|
| 731 |
free( psz_arg ); |
|---|
| 732 |
|
|---|
| 733 |
msg_Dbg( p_access, "ip:%s port:%d", psz_ip, i_port ); |
|---|
| 734 |
|
|---|
| 735 |
if( ftp_SendCommand( p_access, p_sys, "TYPE I" ) < 0 || |
|---|
| 736 |
ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) != 2 ) |
|---|
| 737 |
{ |
|---|
| 738 |
msg_Err( p_access, "cannot set binary transfer mode" ); |
|---|
| 739 |
return VLC_EGENERIC; |
|---|
| 740 |
} |
|---|
| 741 |
|
|---|
| 742 |
if( i_start > 0 ) |
|---|
| 743 |
{ |
|---|
| 744 |
if( ftp_SendCommand( p_access, p_sys, "REST %"PRIu64, i_start ) < 0 || |
|---|
| 745 |
ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) > 3 ) |
|---|
| 746 |
{ |
|---|
| 747 |
msg_Err( p_access, "cannot set restart offset" ); |
|---|
| 748 |
return VLC_EGENERIC; |
|---|
| 749 |
} |
|---|
| 750 |
} |
|---|
| 751 |
|
|---|
| 752 |
msg_Dbg( p_access, "waiting for data connection..." ); |
|---|
| 753 |
p_sys->fd_data = net_ConnectTCP( p_access, psz_ip, i_port ); |
|---|
| 754 |
if( p_sys->fd_data < 0 ) |
|---|
| 755 |
{ |
|---|
| 756 |
msg_Err( p_access, "failed to connect with server" ); |
|---|
| 757 |
return VLC_EGENERIC; |
|---|
| 758 |
} |
|---|
| 759 |
msg_Dbg( p_access, "connection with \"%s:%d\" successful", |
|---|
| 760 |
psz_ip, i_port ); |
|---|
| 761 |
|
|---|
| 762 |
|
|---|
| 763 |
if( ftp_SendCommand( p_access, p_sys, "%s %s", |
|---|
| 764 |
p_sys->out ? "STOR" : "RETR", |
|---|
| 765 |
p_sys->url.psz_path ) < 0 || |
|---|
| 766 |
ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) > 2 ) |
|---|
| 767 |
{ |
|---|
| 768 |
msg_Err( p_access, "cannot retrieve file" ); |
|---|
| 769 |
return VLC_EGENERIC; |
|---|
| 770 |
} |
|---|
| 771 |
|
|---|
| 772 |
shutdown( p_sys->fd_data, p_sys->out ? SHUT_RD : SHUT_WR ); |
|---|
| 773 |
|
|---|
| 774 |
return VLC_SUCCESS; |
|---|
| 775 |
} |
|---|
| 776 |
|
|---|
| 777 |
static int ftp_StopStream ( vlc_object_t *p_access, access_sys_t *p_sys ) |
|---|
| 778 |
{ |
|---|
| 779 |
if( ftp_SendCommand( p_access, p_sys, "ABOR" ) < 0 ) |
|---|
| 780 |
{ |
|---|
| 781 |
msg_Warn( p_access, "cannot abort file" ); |
|---|
| 782 |
if( p_sys->fd_data > 0 ) |
|---|
| 783 |
net_Close( p_sys->fd_data ); |
|---|
| 784 |
p_sys->fd_data = -1; |
|---|
| 785 |
return VLC_EGENERIC; |
|---|
| 786 |
} |
|---|
| 787 |
|
|---|
| 788 |
if( p_sys->fd_data != -1 ) |
|---|
| 789 |
{ |
|---|
| 790 |
int i_answer; |
|---|
| 791 |
ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ); |
|---|
| 792 |
if ( i_answer != 227 ) |
|---|
| 793 |
|
|---|
| 794 |
|
|---|
| 795 |
ftp_ReadCommand( p_access, p_sys, NULL, NULL ); |
|---|
| 796 |
|
|---|
| 797 |
net_Close( p_sys->fd_data ); |
|---|
| 798 |
p_sys->fd_data = -1; |
|---|
| 799 |
} |
|---|
| 800 |
|
|---|
| 801 |
return VLC_SUCCESS; |
|---|
| 802 |
} |
|---|