| 1 |
hirohitohigashi |
17 |
/* |
| 2 |
|
|
liboftp: this is an FTP library to simplify the work to a Developer |
| 3 |
|
|
who want to work with FTP servers (RFC 959). |
| 4 |
|
|
|
| 5 |
|
|
Copyright (c) 2009 hirohito higashi. All rights reserved. |
| 6 |
|
|
This file is distributed under BSD license. |
| 7 |
|
|
*/ |
| 8 |
|
|
|
| 9 |
|
|
|
| 10 |
|
|
/***** Feature test switches ************************************************/ |
| 11 |
|
|
/***** System headers *******************************************************/ |
| 12 |
|
|
#include <unistd.h> |
| 13 |
|
|
#include <sys/types.h> |
| 14 |
|
|
#include <sys/socket.h> |
| 15 |
|
|
|
| 16 |
|
|
|
| 17 |
|
|
/***** Local headers ********************************************************/ |
| 18 |
|
|
#include "liboftp.h" |
| 19 |
|
|
#include "sub.h" |
| 20 |
|
|
|
| 21 |
|
|
|
| 22 |
|
|
/***** Constat values *******************************************************/ |
| 23 |
|
|
#define TRANSFER_SEGMENT_SIZE 4096 /* 一度のrecv()で転送するバイト数 */ |
| 24 |
|
|
|
| 25 |
|
|
|
| 26 |
|
|
/***** Macros ***************************************************************/ |
| 27 |
|
|
/***** Typedefs *************************************************************/ |
| 28 |
|
|
/***** Function prototypes **************************************************/ |
| 29 |
|
|
/***** Local variables ******************************************************/ |
| 30 |
|
|
/***** Global variables *****************************************************/ |
| 31 |
|
|
/***** Signal catching functions ********************************************/ |
| 32 |
|
|
/***** Local functions ******************************************************/ |
| 33 |
|
|
/***** Global functions *****************************************************/ |
| 34 |
|
|
|
| 35 |
|
|
|
| 36 |
|
|
/****************************************************************************/ |
| 37 |
|
|
/*! バッファからファイル送信 |
| 38 |
|
|
* |
| 39 |
|
|
*@param ftp LIBOFTPへのポインタ。 |
| 40 |
|
|
*@param fname サーバ上のファイル名 |
| 41 |
|
|
*@param buf バッファへのポインタ |
| 42 |
|
|
*@param bufsiz バッファサイズ |
| 43 |
|
|
*@retval int 0: no error. -1: OS level error. -2: ftp protocol error. -3: buffer too small. |
| 44 |
|
|
*@note |
| 45 |
|
|
*/ |
| 46 |
|
|
int ftp_put_buffer( LIBOFTP *ftp, const char *fname, char *buf, int bufsiz ) |
| 47 |
|
|
{ |
| 48 |
|
|
int data_socket; |
| 49 |
|
|
char *p = buf; |
| 50 |
|
|
int n; |
| 51 |
|
|
|
| 52 |
|
|
/* |
| 53 |
|
|
* 送信準備 |
| 54 |
|
|
*/ |
| 55 |
|
|
if( ftp->flag_passive ) { |
| 56 |
|
|
data_socket = ftp_getready_pasv( ftp, fname, "STOR" ); |
| 57 |
|
|
if( data_socket < 0 ) { |
| 58 |
|
|
return -1; /* XXX: mixed os level error and ftp protocol error. */ |
| 59 |
|
|
} |
| 60 |
|
|
} else { |
| 61 |
|
|
return -2; /* XXX: active mode not ready yet. */ |
| 62 |
|
|
} |
| 63 |
|
|
|
| 64 |
|
|
/* |
| 65 |
|
|
* タイムアウトが意図通りに働くように、分割してsendする。 |
| 66 |
|
|
*/ |
| 67 |
|
|
while( 1 ) { |
| 68 |
|
|
int len = bufsiz; |
| 69 |
|
|
if( len > TRANSFER_SEGMENT_SIZE ) { |
| 70 |
|
|
len = TRANSFER_SEGMENT_SIZE; |
| 71 |
|
|
} |
| 72 |
|
|
n = sendn( data_socket, p, len, 0 ); |
| 73 |
|
|
DEBUGPRINT1( "SEND: n=%d\n", n ); |
| 74 |
|
|
if( n < 0 ) { |
| 75 |
|
|
close( data_socket ); |
| 76 |
|
|
return -1; |
| 77 |
|
|
} |
| 78 |
|
|
if( n == 0 ) { |
| 79 |
|
|
break; |
| 80 |
|
|
} |
| 81 |
|
|
p += n; |
| 82 |
|
|
bufsiz -= n; |
| 83 |
|
|
if( bufsiz <= 0 ) { |
| 84 |
|
|
break; |
| 85 |
|
|
} |
| 86 |
|
|
} |
| 87 |
|
|
close( data_socket ); |
| 88 |
|
|
|
| 89 |
|
|
/* |
| 90 |
|
|
* receive response. |
| 91 |
|
|
*/ |
| 92 |
|
|
if( ftp_receive_response( ftp, 0, 0 ) != 226 ) { /* 226: Closing data connection. */ |
| 93 |
|
|
return -2; |
| 94 |
|
|
} |
| 95 |
|
|
|
| 96 |
|
|
return 0; |
| 97 |
|
|
} |