| 1 |
/* |
| 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 |
*@param fname サーバ上のファイル名 |
| 42 |
*@param cmd 送信するFTPコマンド |
| 43 |
*@retval int エラーコード |
| 44 |
*@note |
| 45 |
*/ |
| 46 |
static int ftp_put_buffer_main( LIBOFTP *ftp, const char *buf, int bufsiz, const char *fname, const char *cmd ) |
| 47 |
{ |
| 48 |
int data_socket; |
| 49 |
const char *p = buf; |
| 50 |
int n, res; |
| 51 |
|
| 52 |
/* |
| 53 |
* 送信準備 |
| 54 |
*/ |
| 55 |
if( ftp->flag_passive ) { |
| 56 |
data_socket = ftp_getready_pasv( ftp, cmd, fname ); |
| 57 |
} else { |
| 58 |
data_socket = ftp_getready_active( ftp, cmd, fname ); |
| 59 |
} |
| 60 |
if( data_socket < 0 ) { |
| 61 |
return data_socket; |
| 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 |
DEBUGPRINT1( "recv error. %s\n", strerror(errno) ); |
| 76 |
copy_strerror(); |
| 77 |
close( data_socket ); |
| 78 |
return LIBOFTP_ERROR_OS; |
| 79 |
} |
| 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 |
res = ftp_receive_response( ftp, ftp->error_message, sizeof(ftp->error_message)-1 ); |
| 93 |
if( res != 226 ) { /* 226: Closing data connection. */ |
| 94 |
DEBUGPRINT1( "got illegal response %d\n", res ); |
| 95 |
return res < 0? res: LIBOFTP_ERROR_PROTOCOL; |
| 96 |
} |
| 97 |
|
| 98 |
return LIBOFTP_NOERROR; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
/***** Global functions *****************************************************/ |
| 104 |
|
| 105 |
/****************************************************************************/ |
| 106 |
/*! バッファからファイル送信 |
| 107 |
* |
| 108 |
*@param ftp LIBOFTPへのポインタ。 |
| 109 |
*@param buf バッファへのポインタ |
| 110 |
*@param bufsiz バッファサイズ |
| 111 |
*@param fname サーバ上のファイル名 |
| 112 |
*@retval int エラーコード |
| 113 |
*@note |
| 114 |
*/ |
| 115 |
int ftp_put_buffer( LIBOFTP *ftp, const char *buf, int bufsiz, const char *fname ) |
| 116 |
{ |
| 117 |
return ftp_put_buffer_main( ftp, buf, bufsiz, fname, "STOR" ); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
/****************************************************************************/ |
| 123 |
/*! バッファからファイル送信 アペンドモード |
| 124 |
* |
| 125 |
*@param ftp LIBOFTPへのポインタ。 |
| 126 |
*@param buf バッファへのポインタ |
| 127 |
*@param bufsiz バッファサイズ |
| 128 |
*@param fname サーバ上のファイル名 |
| 129 |
*@retval int エラーコード |
| 130 |
*@note |
| 131 |
*/ |
| 132 |
int ftp_append_buffer( LIBOFTP *ftp, const char *buf, int bufsiz, const char *fname ) |
| 133 |
{ |
| 134 |
return ftp_put_buffer_main( ftp, buf, bufsiz, fname, "APPE" ); |
| 135 |
} |