comparison stream/stream_ftp.c @ 35339:c7c59b8e3af8

stream ftp: Allocate command buffer on-heap
author al
date Tue, 20 Nov 2012 22:23:38 +0000
parents 4fe8922d6242
children d5476d0811f8
comparison
equal deleted inserted replaced
35338:4fe8922d6242 35339:c7c59b8e3af8
50 50
51 char *cput,*cget; 51 char *cput,*cget;
52 int handle; 52 int handle;
53 int cavail,cleft; 53 int cavail,cleft;
54 char *buf; 54 char *buf;
55 char *cmd_buf;
55 } stream_priv_dflts = { 56 } stream_priv_dflts = {
56 "anonymous","no@spam", 57 "anonymous","no@spam",
57 NULL, 58 NULL,
58 21, 59 21,
59 NULL, 60 NULL,
60 NULL, 61 NULL,
61 NULL, 62 NULL,
62 63
63 -1, 64 -1,
64 0,0, 65 0,0,
65 NULL 66 NULL,
67 NULL,
66 }; 68 };
69
70 #define CMD_BUFSIZE 256
67 71
68 #define BUFSIZE 2048 72 #define BUFSIZE 2048
69 73
70 #define ST_OFF(f) M_ST_OFF(struct stream_priv_s,f) 74 #define ST_OFF(f) M_ST_OFF(struct stream_priv_s,f)
71 /// URL definition 75 /// URL definition
287 } 291 }
288 292
289 static int FtpOpenData(stream_t* s,off_t newpos) { 293 static int FtpOpenData(stream_t* s,off_t newpos) {
290 struct stream_priv_s* p = s->priv; 294 struct stream_priv_s* p = s->priv;
291 int resp; 295 int resp;
292 char str[256],rsp_txt[256]; 296 char rsp_txt[256];
293 297
294 // Open a new connection 298 // Open a new connection
295 s->fd = FtpOpenPort(p); 299 s->fd = FtpOpenPort(p);
296 300
297 if(s->fd < 0) return 0; 301 if(s->fd < 0) return 0;
298 302
299 if(newpos > 0) { 303 if(newpos > 0) {
300 snprintf(str,255,"REST %"PRId64, (int64_t)newpos); 304 snprintf(p->cmd_buf,CMD_BUFSIZE - 1,"REST %"PRId64, (int64_t)newpos);
301 305
302 resp = FtpSendCmd(str,p,rsp_txt); 306 resp = FtpSendCmd(p->cmd_buf,p,rsp_txt);
303 if(resp != 3) { 307 if(resp != 3) {
304 mp_msg(MSGT_OPEN,MSGL_WARN, "[ftp] command '%s' failed: %s\n",str,rsp_txt); 308 mp_msg(MSGT_OPEN,MSGL_WARN, "[ftp] command '%s' failed: %s\n",p->cmd_buf,rsp_txt);
305 newpos = 0; 309 newpos = 0;
306 } 310 }
307 } 311 }
308 312
309 // Get the file 313 // Get the file
310 snprintf(str,255,"RETR %s",p->filename); 314 snprintf(p->cmd_buf,CMD_BUFSIZE - 1,"RETR %s",p->filename);
311 resp = FtpSendCmd(str,p,rsp_txt); 315 resp = FtpSendCmd(p->cmd_buf,p,rsp_txt);
312 316
313 if(resp != 1) { 317 if(resp != 1) {
314 mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] command '%s' failed: %s\n",str,rsp_txt); 318 mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] command '%s' failed: %s\n",p->cmd_buf,rsp_txt);
315 return 0; 319 return 0;
316 } 320 }
317 321
318 s->pos = newpos; 322 s->pos = newpos;
319 return 1; 323 return 1;
404 FtpSendCmd("QUIT", p, NULL); 408 FtpSendCmd("QUIT", p, NULL);
405 closesocket(p->handle); 409 closesocket(p->handle);
406 } 410 }
407 411
408 free(p->buf); 412 free(p->buf);
413 free(p->cmd_buf);
409 414
410 m_struct_free(&stream_opts,p); 415 m_struct_free(&stream_opts,p);
411 } 416 }
412 417
413 418
414 419
415 static int open_f(stream_t *stream,int mode, void* opts, int* file_format) { 420 static int open_f(stream_t *stream,int mode, void* opts, int* file_format) {
416 int resp; 421 int resp;
417 int64_t len = 0; 422 int64_t len = 0;
418 struct stream_priv_s* p = (struct stream_priv_s*)opts; 423 struct stream_priv_s* p = (struct stream_priv_s*)opts;
419 char str[256],rsp_txt[256]; 424 char rsp_txt[256];
420 425
421 if(mode != STREAM_READ) { 426 if(mode != STREAM_READ) {
422 mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] Unknown open mode %d\n",mode); 427 mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] Unknown open mode %d\n",mode);
423 m_struct_free(&stream_opts,opts); 428 m_struct_free(&stream_opts,opts);
424 return STREAM_UNSUPPORTED; 429 return STREAM_UNSUPPORTED;
430 return STREAM_ERROR; 435 return STREAM_ERROR;
431 } 436 }
432 437
433 // Allocate buffers 438 // Allocate buffers
434 p->buf = malloc(BUFSIZE); 439 p->buf = malloc(BUFSIZE);
435 440 p->cmd_buf = malloc(CMD_BUFSIZE);
436 if (!p->buf) { 441
442 if (!p->buf || !p->cmd_buf) {
437 close_f(stream); 443 close_f(stream);
438 m_struct_free(&stream_opts,opts); 444 m_struct_free(&stream_opts,opts);
439 return STREAM_ERROR; 445 return STREAM_ERROR;
440 } 446 }
441 447
456 m_struct_free(&stream_opts,opts); 462 m_struct_free(&stream_opts,opts);
457 return STREAM_ERROR; 463 return STREAM_ERROR;
458 } 464 }
459 465
460 // Login 466 // Login
461 snprintf(str,255,"USER %s",p->user); 467 snprintf(p->cmd_buf,CMD_BUFSIZE - 1,"USER %s",p->user);
462 resp = FtpSendCmd(str,p,rsp_txt); 468 resp = FtpSendCmd(p->cmd_buf,p,rsp_txt);
463 469
464 // password needed 470 // password needed
465 if(resp == 3) { 471 if(resp == 3) {
466 snprintf(str,255,"PASS %s",p->pass); 472 snprintf(p->cmd_buf,CMD_BUFSIZE - 1,"PASS %s",p->pass);
467 resp = FtpSendCmd(str,p,rsp_txt); 473 resp = FtpSendCmd(p->cmd_buf,p,rsp_txt);
468 if(resp != 2) { 474 if(resp != 2) {
469 mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] command '%s' failed: %s\n",str,rsp_txt); 475 mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] command '%s' failed: %s\n",p->cmd_buf,rsp_txt);
470 close_f(stream); 476 close_f(stream);
471 return STREAM_ERROR; 477 return STREAM_ERROR;
472 } 478 }
473 } else if(resp != 2) { 479 } else if(resp != 2) {
474 mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] command '%s' failed: %s\n",str,rsp_txt); 480 mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] command '%s' failed: %s\n",p->cmd_buf,rsp_txt);
475 close_f(stream); 481 close_f(stream);
476 return STREAM_ERROR; 482 return STREAM_ERROR;
477 } 483 }
478 484
479 // Set the transfer type 485 // Set the transfer type
483 close_f(stream); 489 close_f(stream);
484 return STREAM_ERROR; 490 return STREAM_ERROR;
485 } 491 }
486 492
487 // Get the filesize 493 // Get the filesize
488 snprintf(str,255,"SIZE %s",p->filename); 494 snprintf(p->cmd_buf,CMD_BUFSIZE - 1,"SIZE %s",p->filename);
489 resp = FtpSendCmd(str,p,rsp_txt); 495 resp = FtpSendCmd(p->cmd_buf,p,rsp_txt);
490 if(resp != 2) { 496 if(resp != 2) {
491 mp_msg(MSGT_OPEN,MSGL_WARN, "[ftp] command '%s' failed: %s\n",str,rsp_txt); 497 mp_msg(MSGT_OPEN,MSGL_WARN, "[ftp] command '%s' failed: %s\n",p->cmd_buf,rsp_txt);
492 } else { 498 } else {
493 int dummy; 499 int dummy;
494 sscanf(rsp_txt,"%d %"SCNd64,&dummy,&len); 500 sscanf(rsp_txt,"%d %"SCNd64,&dummy,&len);
495 } 501 }
496 502