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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
|
/*****************************************************************
* gmerlin-avdecoder - a general purpose multimedia decoding library
*
* Copyright (c) 2001 - 2012 Members of the Gmerlin project
* gmerlin-general@lists.sourceforge.net
* http://gmerlin.sourceforge.net
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* *****************************************************************/
#include <unistd.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <avdec_private.h>
#define LOG_DOMAIN "in_ftp"
typedef struct
{
int control_fd;
int data_fd;
int64_t bytes_read;
} ftp_priv_t;
static int get_server_answer(const bgav_options_t * opt,
int fd, char ** server_msg,
int * server_msg_alloc,int connect_timeout)
{
char status[5];
char status_neu[5];
status[4] = '\0';
status_neu[4] = '\0';
if(!bgav_read_line_fd(opt, fd, server_msg, server_msg_alloc, connect_timeout))
{
return 0;
}
strncpy(status, *server_msg, 4);
if(status[3]=='-')
{
status[3] = ' ';
}
else
{
return atoi(*server_msg);
}
status_neu[0]='\0';
while(strncmp(status, status_neu, 4)!= 0)
{
if(!bgav_read_line_fd(opt, fd, server_msg, server_msg_alloc, connect_timeout))
return 0;
strncpy(status_neu, *server_msg, 4);
}
return atoi(*server_msg);
}
static char * parse_address(const char * server_msg, int * port)
{
int ip[4];
int po[2];
const char * pos;
char * rest;
pos = strrchr(server_msg, ')');
while((*pos != '(') && (pos != server_msg))
pos--;
if(*pos != '(')
return NULL;
pos++;
ip[0] = strtol(pos, &rest, 10);
if((pos == rest) || (*rest != ','))
return NULL;
pos = rest+1;
ip[1] = strtol(pos, &rest, 10);
if((pos == rest) || (*rest != ','))
return NULL;
pos = rest+1;
ip[2] = strtol(pos, &rest, 10);
if((pos == rest) || (*rest != ','))
return NULL;
pos = rest+1;
ip[3] = strtol(pos, &rest, 10);
if((pos == rest) || (*rest != ','))
return NULL;
pos = rest+1;
po[0] = strtol(pos, &rest, 10);
if((pos == rest) || (*rest != ','))
return NULL;
pos = rest+1;
po[1] = strtol(pos, &rest, 10);
if((pos == rest) || (*rest != ')'))
return NULL;
pos = rest+1;
*port = (po[0] << 8) | po[1];
return bgav_sprintf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
}
/* Open Funktion: Gibt 1 bei Erfolg zurueck */
/*
* 150 About to open data connection
* 200 Binaer mode
* 213 Size of file
* 220 Connection ready
* 221 Quit connection
* 226 File send ok
* 227 Pasv mode
* 230 Login successful
* 250 Directory successfully changed
* 331 Login password
*/
#define FREE(ptr) if(ptr) { free(ptr); ptr = NULL; }
static int open_ftp(bgav_input_context_t * ctx, const char * url, char ** r)
{
int port = -1;
int server_msg_alloc = 0;
int data_port = -1;
char * data_ip = NULL;
char * server_msg = NULL;
char * server_cmd = NULL;
char * host = NULL;
char * path = NULL;
char *file_name;
char * pos;
char * user = NULL;
char * pass = NULL;
ftp_priv_t * p;
int ret = 0;
if(!bgav_url_split(url,
NULL, /* Protocol */
&user, /* User */
&pass, /* Pass */
&host,
&port,
&path))
{
gavl_log(GAVL_LOG_ERROR, LOG_DOMAIN, "Unvalid URL");
goto fail;
}
if(port == -1)
{
port = 21;
}
p = calloc(1, sizeof(*p));
ctx->priv = p;
/* Connect */
if((p->control_fd = bgav_tcp_connect(ctx->opt, host, port))== -1)
goto fail;
if(get_server_answer(ctx->opt,
p->control_fd, &server_msg, &server_msg_alloc,
ctx->opt->connect_timeout) != 220)
{
gavl_log(GAVL_LOG_ERROR, LOG_DOMAIN, "Could not read answer");
goto fail;
}
/* done */
/* Check out how to log in */
if(!user || !pass)
{
if(ctx->opt->ftp_anonymous)
{
user = gavl_strdup("ftp");
if(ctx->opt->ftp_anonymous_password)
pass = gavl_strdup(ctx->opt->ftp_anonymous_password);
else
pass = gavl_strdup("gates@nanosoft.com");
}
else /* Get user/pass with callback */
{
if(!ctx->opt->user_pass_callback ||
!ctx->opt->user_pass_callback(ctx->opt->user_pass_callback_data,
host, &user, &pass))
goto fail;
}
}
/* Server login */
server_cmd = bgav_sprintf("USER %s\r\n", user);
if(!bgav_tcp_send(ctx->opt, p->control_fd, (uint8_t*)server_cmd, strlen(server_cmd)))
goto fail;
FREE(server_cmd);
if(get_server_answer(ctx->opt,
p->control_fd, &server_msg, &server_msg_alloc,
ctx->opt->connect_timeout) != 331)
{
gavl_log(GAVL_LOG_ERROR, LOG_DOMAIN,
"Could not read answer");
goto fail;
}
server_cmd = bgav_sprintf("PASS %s\r\n", pass);
if(!bgav_tcp_send(ctx->opt, p->control_fd, (uint8_t*)server_cmd, strlen(server_cmd)))
goto fail;
FREE(server_cmd);
if(get_server_answer(ctx->opt,
p->control_fd, &server_msg, &server_msg_alloc,
ctx->opt->connect_timeout) != 230)
{
gavl_log(GAVL_LOG_ERROR, LOG_DOMAIN, "Could not read answer");
goto fail;
}
/* done */
/* parse file_name and directory */
file_name = strrchr(path, '/');
if(!file_name)
goto fail;
*file_name = '\0';
file_name ++;
/* done */
/* Change Directory */
server_cmd = bgav_sprintf("CWD %s\r\n",path);
if(!bgav_tcp_send(ctx->opt, p->control_fd, (uint8_t*)server_cmd, strlen(server_cmd)))
goto fail;
FREE(server_cmd);
if(get_server_answer(ctx->opt,
p->control_fd, &server_msg, &server_msg_alloc,
ctx->opt->connect_timeout) != 250)
{
gavl_log(GAVL_LOG_ERROR, LOG_DOMAIN, "Could not read answer");
goto fail;
}
/* done */
/* Find size of File */
server_cmd = bgav_sprintf("SIZE %s\r\n",file_name);
if(!bgav_tcp_send(ctx->opt,
p->control_fd, (uint8_t*)server_cmd, strlen(server_cmd)))
goto fail;
FREE(server_cmd);
if(get_server_answer(ctx->opt,
p->control_fd, &server_msg, &server_msg_alloc,
ctx->opt->connect_timeout) != 213)
{
gavl_log(GAVL_LOG_ERROR, LOG_DOMAIN, "Could not read answer");
goto fail;
}
pos = server_msg;
while(!isspace(*pos) && (*pos != '\0'))
pos++;
if(*pos == '\0')
{
gavl_log(GAVL_LOG_ERROR, LOG_DOMAIN, "Invalid server answer");
goto fail;
}
ctx->total_bytes = strtoll(pos, NULL, 10);
/* done */
/* Set Binaer */
server_cmd = bgav_sprintf("TYPE I\r\n");
if(!bgav_tcp_send(ctx->opt,
p->control_fd, (uint8_t*)server_cmd, strlen(server_cmd)))
goto fail;
FREE(server_cmd);
if(get_server_answer(ctx->opt,
p->control_fd, &server_msg, &server_msg_alloc,
ctx->opt->connect_timeout) != 200)
{
gavl_log(GAVL_LOG_ERROR, LOG_DOMAIN, "Could not read answer");
goto fail;
}
/* done */
/* Set PASV */
server_cmd = bgav_sprintf("PASV\r\n");
if(!bgav_tcp_send(ctx->opt, p->control_fd, (uint8_t*)server_cmd, strlen(server_cmd)))
goto fail;
FREE(server_cmd);
if(get_server_answer(ctx->opt,
p->control_fd, &server_msg, &server_msg_alloc,
ctx->opt->connect_timeout) != 227)
{
gavl_log(GAVL_LOG_ERROR, LOG_DOMAIN, "Could not read answer");
goto fail;
}
data_ip = parse_address(server_msg, &data_port);
/* done */
/* Connect */
if((p->data_fd = bgav_tcp_connect(ctx->opt, data_ip, data_port))== -1)
goto fail;
/* done */
/* open data connection */
server_cmd = bgav_sprintf("RETR %s\r\n", file_name);
if(!bgav_tcp_send(ctx->opt, p->control_fd, (uint8_t*)server_cmd, strlen(server_cmd)))
goto fail;
FREE(server_cmd);
if(get_server_answer(ctx->opt,
p->control_fd, &server_msg, &server_msg_alloc,
ctx->opt->connect_timeout) != 150)
{
gavl_log(GAVL_LOG_ERROR, LOG_DOMAIN, "Could not read answer");
goto fail;
}
/* done */
// ctx->flags |= BGAV_INPUT_DO_BUFFER;
ctx->url = gavl_strdup(url);
ret = 1;
fail:
FREE(server_cmd);
FREE(host);
FREE(path);
FREE(user);
FREE(pass);
FREE(data_ip);
FREE(server_msg);
return ret;
}
/* Lese funktionen: Geben die Zahl der gelesenen BYTES zurueck */
static int do_read(bgav_input_context_t * ctx,
uint8_t * buffer, int len, int timeout)
{
int len_read;
ftp_priv_t * p;
p = ctx->priv;
if(len + p->bytes_read > ctx->total_bytes)
len = ctx->total_bytes - p->bytes_read;
if(!len)
{
return 0;
}
len_read = bgav_read_data_fd(ctx->opt,
p->data_fd, buffer, len, timeout);
p->bytes_read += len_read;
return len_read;
}
static int read_ftp(bgav_input_context_t * ctx,
uint8_t * buffer, int len)
{
return do_read(ctx, buffer, len, ctx->opt->read_timeout);
}
/* Close: Zumachen */
static void close_ftp(bgav_input_context_t * ctx)
{
char * server_cmd;
ftp_priv_t * p;
p = ctx->priv;
server_cmd = bgav_sprintf("QUIT\r\n");
bgav_tcp_send(ctx->opt, p->control_fd, (uint8_t*)server_cmd,
strlen(server_cmd));
free(server_cmd);
if(p->control_fd >= 0)
closesocket(p->control_fd);
if(p->data_fd >= 0)
closesocket(p->data_fd);
free(p);
}
const bgav_input_t bgav_input_ftp =
{
.name = "ftp",
.open = open_ftp,
.read = read_ftp,
.close = close_ftp
};
|