| 26 |
|
|
| 27 |
|
|
| 28 |
/***** Constat values *******************************************************/ |
/***** Constat values *******************************************************/ |
| 29 |
|
#define TRANSFER_SEGMENT_SIZE 4096 /* 一度のrecv()で転送するバイト数 */ |
| 30 |
|
|
| 31 |
|
|
| 32 |
/***** Macros ***************************************************************/ |
/***** Macros ***************************************************************/ |
| 33 |
/***** Typedefs *************************************************************/ |
/***** Typedefs *************************************************************/ |
| 34 |
/***** Function prototypes **************************************************/ |
/***** Function prototypes **************************************************/ |
| 302 |
snprintf( str1, sizeof(str1), "%s\r\n", cmd ); |
snprintf( str1, sizeof(str1), "%s\r\n", cmd ); |
| 303 |
} |
} |
| 304 |
if( ftp_send_command( ftp, str1 ) < 0 ) { |
if( ftp_send_command( ftp, str1 ) < 0 ) { |
| 305 |
DEBUGPRINT1( "getready_active: %s command sending error.\n", cmd ); |
DEBUGPRINT1( "getready_active: command sending error. %s", str1 ); |
| 306 |
close( sock ); |
close( sock ); |
| 307 |
return LIBOFTP_ERROR_OS; |
return LIBOFTP_ERROR_OS; |
| 308 |
} |
} |
| 428 |
snprintf( str1, sizeof(str1), "%s\r\n", cmd ); |
snprintf( str1, sizeof(str1), "%s\r\n", cmd ); |
| 429 |
} |
} |
| 430 |
if( ftp_send_command( ftp, str1 ) < 0 ) { |
if( ftp_send_command( ftp, str1 ) < 0 ) { |
| 431 |
DEBUGPRINT1( "getready_pasv: command sending error. %s\n", str1 ); |
DEBUGPRINT1( "getready_pasv: command sending error. %s", str1 ); |
| 432 |
return LIBOFTP_ERROR_OS; |
return LIBOFTP_ERROR_OS; |
| 433 |
} |
} |
| 434 |
|
|
| 474 |
return LIBOFTP_ERROR_PROTOCOL; |
return LIBOFTP_ERROR_PROTOCOL; |
| 475 |
} |
} |
| 476 |
} |
} |
| 477 |
|
|
| 478 |
|
|
| 479 |
|
|
| 480 |
|
/****************************************************************************/ |
| 481 |
|
/*! バッファへリスト取得 |
| 482 |
|
* |
| 483 |
|
*@param ftp LIBOFTPへのポインタ。 |
| 484 |
|
*@param cmd コマンド (RETR|LIST|NLST) |
| 485 |
|
*@param fname ファイル名または、グロブ。 |
| 486 |
|
*@param buf バッファへのポインタ |
| 487 |
|
*@param bufsiz バッファサイズ |
| 488 |
|
*@retval int 取得したバイト数 マイナス値ならエラーコード |
| 489 |
|
*@note |
| 490 |
|
*/ |
| 491 |
|
int ftp_get_buffer_main( LIBOFTP *ftp, const char *cmd, const char *fname, char *buf, int bufsiz ) |
| 492 |
|
{ |
| 493 |
|
int data_socket; |
| 494 |
|
char *p = buf; |
| 495 |
|
int res; |
| 496 |
|
|
| 497 |
|
/* |
| 498 |
|
* 受信準備 |
| 499 |
|
*/ |
| 500 |
|
if( ftp->flag_passive ) { |
| 501 |
|
data_socket = ftp_getready_pasv( ftp, cmd, fname ); |
| 502 |
|
} else { |
| 503 |
|
data_socket = ftp_getready_active( ftp, cmd, fname ); |
| 504 |
|
} |
| 505 |
|
if( data_socket < 0 ) { |
| 506 |
|
return data_socket; |
| 507 |
|
} |
| 508 |
|
|
| 509 |
|
/* |
| 510 |
|
* タイムアウトが意図通りに働くように、分割してrecvする。 |
| 511 |
|
*/ |
| 512 |
|
while( 1 ) { |
| 513 |
|
int n; |
| 514 |
|
int len = bufsiz; |
| 515 |
|
if( len > TRANSFER_SEGMENT_SIZE ) { |
| 516 |
|
len = TRANSFER_SEGMENT_SIZE; |
| 517 |
|
} |
| 518 |
|
n = recv( data_socket, p, len, 0 ); |
| 519 |
|
DEBUGPRINT1( "RECV: n=%d\n", n ); |
| 520 |
|
if( n < 0 ) { |
| 521 |
|
int ret; |
| 522 |
|
DEBUGPRINT1( "recv error. %s\n", strerror(errno) ); |
| 523 |
|
if( errno == EAGAIN ) { |
| 524 |
|
ret = LIBOFTP_ERROR_TIMEOUT; |
| 525 |
|
} else { |
| 526 |
|
ret = LIBOFTP_ERROR_OS; |
| 527 |
|
copy_strerror(); |
| 528 |
|
} |
| 529 |
|
close( data_socket ); |
| 530 |
|
return ret; |
| 531 |
|
} |
| 532 |
|
if( n == 0 ) { |
| 533 |
|
break; |
| 534 |
|
} |
| 535 |
|
p += n; |
| 536 |
|
bufsiz -= n; |
| 537 |
|
if( bufsiz <= 0 ) { |
| 538 |
|
break; /* buffer too small */ |
| 539 |
|
} |
| 540 |
|
} |
| 541 |
|
|
| 542 |
|
if( bufsiz > 0 ) { /* バッファ不足だったか? */ |
| 543 |
|
/* |
| 544 |
|
* 不足していない |
| 545 |
|
*/ |
| 546 |
|
close( data_socket ); |
| 547 |
|
|
| 548 |
|
/* receive response. */ |
| 549 |
|
res = ftp_receive_response( ftp, ftp->error_message, sizeof(ftp->error_message)-1 ); |
| 550 |
|
if( res != 226 ) { /* 226: Closing data connection. */ |
| 551 |
|
DEBUGPRINT1( "got illegal response %d\n", res ); |
| 552 |
|
return res < 0? res: LIBOFTP_ERROR_PROTOCOL; |
| 553 |
|
} |
| 554 |
|
|
| 555 |
|
return p - buf; /* return with transfered bytes */ |
| 556 |
|
|
| 557 |
|
} else { |
| 558 |
|
/* |
| 559 |
|
* バッファ不足時 |
| 560 |
|
*/ |
| 561 |
|
DEBUGPRINT1( "buffer too small. %s\n", "" ); |
| 562 |
|
strncpy( ftp->error_message, "buffer too small", sizeof( ftp->error_message ) - 1 ); |
| 563 |
|
|
| 564 |
|
if( ftp_send_command( ftp, "ABOR\r\n" ) < 0 ) { |
| 565 |
|
DEBUGPRINT1( "command sending error. %s\n", "ABOR" ); |
| 566 |
|
close( data_socket ); |
| 567 |
|
return LIBOFTP_ERROR_BUFFER; |
| 568 |
|
} |
| 569 |
|
|
| 570 |
|
res = ftp_receive_response( ftp, 0, 0 ); |
| 571 |
|
if( res == 426 ) { /* 426: Connection closed; transfer aborted. */ |
| 572 |
|
res = ftp_receive_response( ftp, 0, 0 ); |
| 573 |
|
} |
| 574 |
|
close( data_socket ); |
| 575 |
|
if( res != 226 ) { /* 226: Closing data connection. */ |
| 576 |
|
DEBUGPRINT1( "got illegal response %d\n", res ); |
| 577 |
|
return res < 0? res: LIBOFTP_ERROR_PROTOCOL; |
| 578 |
|
} |
| 579 |
|
|
| 580 |
|
return LIBOFTP_ERROR_BUFFER; |
| 581 |
|
} |
| 582 |
|
} |