| 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 |
|
|
|
| 34 |
|
|
|
| 35 |
|
|
/****************************************************************************/ |
| 36 |
|
|
/*! バッファからファイル送信 |
| 37 |
|
|
* |
| 38 |
|
|
*@param ftp LIBOFTPへのポインタ。 |
| 39 |
|
|
*@param buf バッファへのポインタ |
| 40 |
|
|
*@param bufsiz バッファサイズ |
| 41 |
hirohitohigashi |
27 |
*@param fname サーバ上のファイル名 |
| 42 |
hirohitohigashi |
28 |
*@param cmd 送信するFTPコマンド |
| 43 |
hirohitohigashi |
17 |
*@retval int 0: no error. -1: OS level error. -2: ftp protocol error. -3: buffer too small. |
| 44 |
|
|
*@note |
| 45 |
|
|
*/ |
| 46 |
hirohitohigashi |
28 |
static int ftp_put_buffer_main( LIBOFTP *ftp, char *buf, int bufsiz, const char *fname, const char *cmd ) |
| 47 |
hirohitohigashi |
17 |
{ |
| 48 |
|
|
int data_socket; |
| 49 |
|
|
char *p = buf; |
| 50 |
|
|
int n; |
| 51 |
|
|
|
| 52 |
|
|
/* |
| 53 |
|
|
* 送信準備 |
| 54 |
|
|
*/ |
| 55 |
|
|
if( ftp->flag_passive ) { |
| 56 |
hirohitohigashi |
28 |
data_socket = ftp_getready_pasv( ftp, cmd, fname ); |
| 57 |
hirohitohigashi |
17 |
} else { |
| 58 |
hirohitohigashi |
28 |
data_socket = ftp_getready_active( ftp, cmd, fname ); |
| 59 |
hirohitohigashi |
17 |
} |
| 60 |
hirohitohigashi |
20 |
if( data_socket < 0 ) { |
| 61 |
|
|
return data_socket; |
| 62 |
|
|
} |
| 63 |
hirohitohigashi |
17 |
|
| 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 |
hirohitohigashi |
28 |
|
| 79 |
hirohitohigashi |
17 |
p += n; |
| 80 |
|
|
bufsiz -= n; |
| 81 |
|
|
if( bufsiz <= 0 ) { |
| 82 |
|
|
break; |
| 83 |
|
|
} |
| 84 |
|
|
} |
| 85 |
|
|
close( data_socket ); |
| 86 |
|
|
|
| 87 |
|
|
/* |
| 88 |
|
|
* receive response. |
| 89 |
|
|
*/ |
| 90 |
|
|
if( ftp_receive_response( ftp, 0, 0 ) != 226 ) { /* 226: Closing data connection. */ |
| 91 |
|
|
return -2; |
| 92 |
|
|
} |
| 93 |
|
|
|
| 94 |
|
|
return 0; |
| 95 |
|
|
} |
| 96 |
hirohitohigashi |
28 |
|
| 97 |
|
|
|
| 98 |
|
|
|
| 99 |
|
|
/***** Global functions *****************************************************/ |
| 100 |
|
|
|
| 101 |
|
|
/****************************************************************************/ |
| 102 |
|
|
/*! バッファからファイル送信 |
| 103 |
|
|
* |
| 104 |
|
|
*@param ftp LIBOFTPへのポインタ。 |
| 105 |
|
|
*@param buf バッファへのポインタ |
| 106 |
|
|
*@param bufsiz バッファサイズ |
| 107 |
|
|
*@param fname サーバ上のファイル名 |
| 108 |
|
|
*@retval int 0: no error. -1: OS level error. -2: ftp protocol error. -3: buffer too small. |
| 109 |
|
|
*@note |
| 110 |
|
|
*/ |
| 111 |
|
|
int ftp_put_buffer( LIBOFTP *ftp, char *buf, int bufsiz, const char *fname ) |
| 112 |
|
|
{ |
| 113 |
|
|
return ftp_put_buffer_main( ftp, buf, bufsiz, fname, "STOR" ); |
| 114 |
|
|
} |
| 115 |
|
|
|
| 116 |
|
|
|
| 117 |
|
|
|
| 118 |
|
|
/****************************************************************************/ |
| 119 |
|
|
/*! バッファからファイル送信 アペンドモード |
| 120 |
|
|
* |
| 121 |
|
|
*@param ftp LIBOFTPへのポインタ。 |
| 122 |
|
|
*@param buf バッファへのポインタ |
| 123 |
|
|
*@param bufsiz バッファサイズ |
| 124 |
|
|
*@param fname サーバ上のファイル名 |
| 125 |
|
|
*@retval int 0: no error. -1: OS level error. -2: ftp protocol error. -3: buffer too small. |
| 126 |
|
|
*@note |
| 127 |
|
|
*/ |
| 128 |
|
|
int ftp_append_buffer( LIBOFTP *ftp, char *buf, int bufsiz, const char *fname ) |
| 129 |
|
|
{ |
| 130 |
|
|
return ftp_put_buffer_main( ftp, buf, bufsiz, fname, "APPE" ); |
| 131 |
|
|
} |