[Ttssh2-commit] [4926] SFTPの実装を "sftp.c" に切り出せるようにするため、 "ssh.c" のコードを移動させた。

svnno****@sourc***** svnno****@sourc*****
2012年 5月 3日 (木) 22:09:23 JST


Revision: 4926
          http://sourceforge.jp/projects/ttssh2/svn/view?view=rev&revision=4926
Author:   yutakapon
Date:     2012-05-03 22:09:23 +0900 (Thu, 03 May 2012)
Log Message:
-----------
SFTPの実装を"sftp.c"に切り出せるようにするため、"ssh.c"のコードを移動させた。

Modified Paths:
--------------
    trunk/ttssh2/ttxssh/sftp.c
    trunk/ttssh2/ttxssh/sftp.h
    trunk/ttssh2/ttxssh/ssh.c
    trunk/ttssh2/ttxssh/ssh.h

-------------- next part --------------
Modified: trunk/ttssh2/ttxssh/sftp.c
===================================================================
--- trunk/ttssh2/ttxssh/sftp.c	2012-05-03 10:47:09 UTC (rev 4925)
+++ trunk/ttssh2/ttxssh/sftp.c	2012-05-03 13:09:23 UTC (rev 4926)
@@ -1,29 +1,27 @@
 /*
-Copyright (c) 2008 TeraTerm Project
+Copyright (c) 2008-2012 TeraTerm Project
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without modification,
 are permitted provided that the following conditions are met:
 
-Redistributions of source code must retain the above copyright notice, this list of
-conditions and the following disclaimer.
+  1. Redistributions of source code must retain the above copyright notice,
+     this list of conditions and the following disclaimer.
+  2. Redistributions in binary form must reproduce the above copyright notice,
+     this list of conditions and the following disclaimer in the documentation
+     and/or other materials provided with the distribution.
+  3. The name of the author may not be used to endorse or promote products derived
+     from this software without specific prior written permission.
 
-Redistributions in binary form must reproduce the above copyright notice, this list
-of conditions and the following disclaimer in the documentation and/or other materials
-provided with the distribution.
-
-The name of Robert O'Callahan may not be used to endorse or promote products derived from
-this software without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
-ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
-OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
-THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
-EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
-SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
-HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
-OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
-SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
+INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
+OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
+OF SUCH DAMAGE.
 */
 
 #include "ttxssh.h"
@@ -46,12 +44,67 @@
 #include "ssh.h"
 #include "crypt.h"
 #include "fwd.h"
+#include "ssh.h"
 #include "sftp.h"
 
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <assert.h>
 
+// SFTP\x90\xEA\x97p\x83o\x83b\x83t\x83@\x82\xF0\x8Am\x95ۂ\xB7\x82\xE9\x81BSCP\x82Ƃ͈قȂ\xE8\x81A\x90擪\x82Ɍ㑱\x82̃f\x81[\x83^\x83T\x83C\x83Y\x82𖄂ߍ\x9E\x82ށB
+static void sftp_buffer_alloc(buffer_t **message)
+{
+	buffer_t *msg;
 
+	msg = buffer_init();
+	if (msg == NULL) {
+		goto error;
+	}
+	// Message length(4byte)
+	buffer_put_int(msg, 0); 
 
+	*message = msg;
 
+error:
+	assert(msg != NULL);
+	return;
+}
+
+static void sftp_buffer_free(buffer_t *message)
+{
+	buffer_free(message);
+}
+
+// \x83T\x81[\x83o\x82\xC9SFTP\x83p\x83P\x83b\x83g\x82𑗐M\x82\xB7\x82\xE9\x81B
+static void sftp_send_msg(PTInstVar pvar, Channel_t *c, buffer_t *msg)
+{
+	char *p;
+	int len;
+
+	len = buffer_len(msg);
+	p = buffer_ptr(msg);
+	// \x8Dŏ\x89\x82Ƀ\x81\x83b\x83Z\x81[\x83W\x83T\x83C\x83Y\x82\xF0\x8Ai\x94[\x82\xB7\x82\xE9\x81B
+	set_uint32(p, len - 4);
+	// \x83y\x83C\x83\x8D\x81[\x83h\x82̑\x97\x90M\x81B
+	SSH2_send_channel_data(pvar, c, p, len);
+}
+
+// SFTP\x92ʐM\x8AJ\x8En\x91O\x82̃l\x83S\x83V\x83G\x81[\x83V\x83\x87\x83\x93
+void sftp_do_init(PTInstVar pvar, Channel_t *c)
+{
+	buffer_t *msg;
+
+	c->sftp.state = SFTP_INIT;
+
+	sftp_buffer_alloc(&msg);
+	buffer_put_char(msg, SSH2_FXP_INIT); 
+	buffer_put_int(msg, SSH2_FILEXFER_VERSION);
+	sftp_send_msg(pvar, c, msg);
+	sftp_buffer_free(msg);
+}
+
+void sftp_response(PTInstVar pvar, Channel_t *c, unsigned char *data, unsigned int buflen)
+{
+	OutputDebugPrintf("len %d\n", buflen);
+
+}

Modified: trunk/ttssh2/ttxssh/sftp.h
===================================================================
--- trunk/ttssh2/ttxssh/sftp.h	2012-05-03 10:47:09 UTC (rev 4925)
+++ trunk/ttssh2/ttxssh/sftp.h	2012-05-03 13:09:23 UTC (rev 4926)
@@ -1,29 +1,27 @@
 /*
-Copyright (c) 2008 TeraTerm Project
+Copyright (c) 2008-2012 TeraTerm Project
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without modification,
 are permitted provided that the following conditions are met:
 
-Redistributions of source code must retain the above copyright notice, this list of
-conditions and the following disclaimer.
+  1. Redistributions of source code must retain the above copyright notice,
+     this list of conditions and the following disclaimer.
+  2. Redistributions in binary form must reproduce the above copyright notice,
+     this list of conditions and the following disclaimer in the documentation
+     and/or other materials provided with the distribution.
+  3. The name of the author may not be used to endorse or promote products derived
+     from this software without specific prior written permission.
 
-Redistributions in binary form must reproduce the above copyright notice, this list
-of conditions and the following disclaimer in the documentation and/or other materials
-provided with the distribution.
-
-The name of Robert O'Callahan may not be used to endorse or promote products derived from
-this software without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
-ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
-OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
-THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
-EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
-SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
-HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
-OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
-SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
+INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
+OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
+OF SUCH DAMAGE.
 */
 
 #ifndef __SFTP_H
@@ -34,5 +32,75 @@
 #define SFTP_DEBUG
 #endif
 
+/* version */
+#define	SSH2_FILEXFER_VERSION		3
 
+/* client to server */
+#define SSH2_FXP_INIT			1
+#define SSH2_FXP_OPEN			3
+#define SSH2_FXP_CLOSE			4
+#define SSH2_FXP_READ			5
+#define SSH2_FXP_WRITE			6
+#define SSH2_FXP_LSTAT			7
+#define SSH2_FXP_STAT_VERSION_0		7
+#define SSH2_FXP_FSTAT			8
+#define SSH2_FXP_SETSTAT		9
+#define SSH2_FXP_FSETSTAT		10
+#define SSH2_FXP_OPENDIR		11
+#define SSH2_FXP_READDIR		12
+#define SSH2_FXP_REMOVE			13
+#define SSH2_FXP_MKDIR			14
+#define SSH2_FXP_RMDIR			15
+#define SSH2_FXP_REALPATH		16
+#define SSH2_FXP_STAT			17
+#define SSH2_FXP_RENAME			18
+#define SSH2_FXP_READLINK		19
+#define SSH2_FXP_SYMLINK		20
+
+/* server to client */
+#define SSH2_FXP_VERSION		2
+#define SSH2_FXP_STATUS			101
+#define SSH2_FXP_HANDLE			102
+#define SSH2_FXP_DATA			103
+#define SSH2_FXP_NAME			104
+#define SSH2_FXP_ATTRS			105
+
+#define SSH2_FXP_EXTENDED		200
+#define SSH2_FXP_EXTENDED_REPLY		201
+
+/* attributes */
+#define SSH2_FILEXFER_ATTR_SIZE		0x00000001
+#define SSH2_FILEXFER_ATTR_UIDGID	0x00000002
+#define SSH2_FILEXFER_ATTR_PERMISSIONS	0x00000004
+#define SSH2_FILEXFER_ATTR_ACMODTIME	0x00000008
+#define SSH2_FILEXFER_ATTR_EXTENDED	0x80000000
+
+/* portable open modes */
+#define SSH2_FXF_READ			0x00000001
+#define SSH2_FXF_WRITE			0x00000002
+#define SSH2_FXF_APPEND			0x00000004
+#define SSH2_FXF_CREAT			0x00000008
+#define SSH2_FXF_TRUNC			0x00000010
+#define SSH2_FXF_EXCL			0x00000020
+
+/* statv****@opens***** f_flag flags */
+#define SSH2_FXE_STATVFS_ST_RDONLY	0x00000001
+#define SSH2_FXE_STATVFS_ST_NOSUID	0x00000002
+
+/* status messages */
+#define SSH2_FX_OK			0
+#define SSH2_FX_EOF			1
+#define SSH2_FX_NO_SUCH_FILE		2
+#define SSH2_FX_PERMISSION_DENIED	3
+#define SSH2_FX_FAILURE			4
+#define SSH2_FX_BAD_MESSAGE		5
+#define SSH2_FX_NO_CONNECTION		6
+#define SSH2_FX_CONNECTION_LOST		7
+#define SSH2_FX_OP_UNSUPPORTED		8
+#define SSH2_FX_MAX			8
+
+
+void sftp_do_init(PTInstVar pvar, Channel_t *c);
+void sftp_response(PTInstVar pvar, Channel_t *c, unsigned char *data, unsigned int buflen);
+
 #endif

Modified: trunk/ttssh2/ttxssh/ssh.c
===================================================================
--- trunk/ttssh2/ttxssh/ssh.c	2012-05-03 10:47:09 UTC (rev 4925)
+++ trunk/ttssh2/ttxssh/ssh.c	2012-05-03 13:09:23 UTC (rev 4926)
@@ -76,50 +76,7 @@
 // channel data structure
 #define CHANNEL_MAX 100
 
-enum scp_state {
-	SCP_INIT, SCP_TIMESTAMP, SCP_FILEINFO, SCP_DATA, SCP_CLOSING,
-};
 
-typedef struct bufchain {
-	buffer_t *msg;
-	struct bufchain *next;
-} bufchain_t;
-
-typedef struct scp {
-	enum scp_dir dir;              // transfer direction
-	enum scp_state state;          // SCP state 
-	char localfile[MAX_PATH];      // local filename
-	char localfilefull[MAX_PATH];  // local filename fullpath
-	char remotefile[MAX_PATH];     // remote filename
-	FILE *localfp;                 // file pointer for local file
-	struct __stat64 filestat;      // file status information
-	HWND progress_window;
-	HANDLE thread;
-	unsigned int thread_id;
-	PTInstVar pvar;
-	// for receiving file
-	long long filetotalsize;
-	long long filercvsize;
-} scp_t;
-
-typedef struct channel {
-	int used;
-	int self_id;
-	int remote_id;
-	unsigned int local_window;
-	unsigned int local_window_max;
-	unsigned int local_consumed;
-	unsigned int local_maxpacket;
-	unsigned int remote_window;
-	unsigned int remote_maxpacket;
-	enum channel_type type;
-	int local_num;
-	bufchain_t *bufchain;
-	scp_t scp;
-	buffer_t *agent_msg;
-	int agent_request_len;
-} Channel_t;
-
 static Channel_t channels[CHANNEL_MAX];
 
 static char ssh_ttymodes[] = "\x01\x03\x02\x1c\x03\x08\x04\x15\x05\x04";
@@ -159,7 +116,6 @@
 void SSH2_dispatch_add_range_message(unsigned char begin, unsigned char end);
 int dh_pub_is_valid(DH *dh, BIGNUM *dh_pub);
 static void start_ssh_heartbeat_thread(PTInstVar pvar);
-static void SSH2_send_channel_data(PTInstVar pvar, Channel_t *c, unsigned char FAR * buf, unsigned int buflen);
 void ssh2_channel_send_close(PTInstVar pvar, Channel_t *c);
 static BOOL SSH_agent_response(PTInstVar pvar, Channel_t *c, int local_channel_num, unsigned char *data, unsigned int buflen);
 
@@ -709,12 +665,6 @@
 	}
 }
 
-#define get_payload_uint32(pvar, offset) get_uint32_MSBfirst((pvar)->ssh_state.payload + (offset))
-#define get_uint32(buf) get_uint32_MSBfirst((buf))
-#define set_uint32(buf, v) set_uint32_MSBfirst((buf), (v))
-#define get_mpint_len(pvar, offset) ((get_ushort16_MSBfirst((pvar)->ssh_state.payload + (offset)) + 7) >> 3)
-#define get_ushort16(buf) get_ushort16_MSBfirst((buf))
-
 #define do_crc(buf, len) (~(uint32)crc32(0xFFFFFFFF, (buf), (len)))
 
 /* Decrypt the payload, checksum it, eat the padding, get the packet type
@@ -846,7 +796,7 @@
    or for the packet type byte).
    Returns a pointer to the payload data area, a region of length 'len',
    to be filled by the caller. */
-static unsigned char FAR *begin_send_packet(PTInstVar pvar, int type, int len)
+unsigned char FAR *begin_send_packet(PTInstVar pvar, int type, int len)
 {
 	unsigned char FAR *buf;
 
@@ -872,7 +822,6 @@
 	return buf + 1;
 }
 
-#define finish_send_packet(pvar) finish_send_packet_special((pvar), 0)
 
 // \x91\x97\x90M\x83\x8A\x83g\x83\x89\x83C\x8A֐\x94\x82̒lj\xC1
 //
@@ -973,7 +922,7 @@
 
 /* if skip_compress is true, then the data has already been compressed
    into outbuf + 12 */
-static void finish_send_packet_special(PTInstVar pvar, int skip_compress)
+void finish_send_packet_special(PTInstVar pvar, int skip_compress)
 {
 	unsigned int len = pvar->ssh_state.outgoing_packet_len;
 	unsigned char FAR *data;
@@ -3269,7 +3218,7 @@
 
 }
 
-static void SSH2_send_channel_data(PTInstVar pvar, Channel_t *c, unsigned char FAR * buf, unsigned int buflen)
+void SSH2_send_channel_data(PTInstVar pvar, Channel_t *c, unsigned char FAR * buf, unsigned int buflen)
 {
 	buffer_t *msg;
 	unsigned char *outmsg;
@@ -7303,6 +7252,12 @@
 			char ch = '\0';
 			SSH2_send_channel_data(pvar, c, &ch, 1);
 		}
+
+	} else if (c->type == TYPE_SFTP) {
+		// SFTP\x83Z\x83b\x83V\x83\x87\x83\x93\x82\xF0\x8AJ\x8En\x82\xB7\x82邽\x82߂̃l\x83S\x83V\x83G\x81[\x83V\x83\x87\x83\x93\x82\xF0\x8Ds\x82\xA4\x81B
+		// (2012.5.3 yutaka)
+		sftp_do_init(pvar, c);
+
 	}
 
 	return TRUE;
@@ -8126,6 +8081,7 @@
 		SSH2_scp_response(pvar, c, data, str_len);
 
 	} else if (c->type == TYPE_SFTP) {  // SFTP
+		sftp_response(pvar, c, data, str_len);
 
 	} else if (c->type == TYPE_AGENT) {  // agent forward
 		if (!SSH_agent_response(pvar, c, 0, data, str_len)) {

Modified: trunk/ttssh2/ttxssh/ssh.h
===================================================================
--- trunk/ttssh2/ttxssh/ssh.h	2012-05-03 10:47:09 UTC (rev 4925)
+++ trunk/ttssh2/ttxssh/ssh.h	2012-05-03 13:09:23 UTC (rev 4926)
@@ -39,6 +39,8 @@
 
 #include "buffer.h"
 #include "config.h"
+#include <sys/types.h>
+#include <sys/stat.h>
 
 #define DEBUG_PRINT_TO_FILE(base, msg, len) { \
 	static int count = 0; \
@@ -629,4 +631,70 @@
 void SSH2_update_hmac_myproposal(PTInstVar pvar);
 int SSH_notify_break_signal(PTInstVar pvar);
 
+///
+enum scp_state {
+	SCP_INIT, SCP_TIMESTAMP, SCP_FILEINFO, SCP_DATA, SCP_CLOSING,
+};
+
+typedef struct bufchain {
+	buffer_t *msg;
+	struct bufchain *next;
+} bufchain_t;
+
+typedef struct scp {
+	enum scp_dir dir;              // transfer direction
+	enum scp_state state;          // SCP state 
+	char localfile[MAX_PATH];      // local filename
+	char localfilefull[MAX_PATH];  // local filename fullpath
+	char remotefile[MAX_PATH];     // remote filename
+	FILE *localfp;                 // file pointer for local file
+	struct __stat64 filestat;      // file status information
+	HWND progress_window;
+	HANDLE thread;
+	unsigned int thread_id;
+	PTInstVar pvar;
+	// for receiving file
+	long long filetotalsize;
+	long long filercvsize;
+} scp_t;
+
+enum sftp_state {
+	SFTP_INIT, 
+};
+
+typedef struct sftp {
+	enum sftp_state state;
+} sftp_t;
+
+typedef struct channel {
+	int used;
+	int self_id;
+	int remote_id;
+	unsigned int local_window;
+	unsigned int local_window_max;
+	unsigned int local_consumed;
+	unsigned int local_maxpacket;
+	unsigned int remote_window;
+	unsigned int remote_maxpacket;
+	enum channel_type type;
+	int local_num;
+	bufchain_t *bufchain;
+	scp_t scp;
+	buffer_t *agent_msg;
+	int agent_request_len;
+	sftp_t sftp;
+} Channel_t;
+
+unsigned char FAR *begin_send_packet(PTInstVar pvar, int type, int len);
+void finish_send_packet_special(PTInstVar pvar, int skip_compress);
+void SSH2_send_channel_data(PTInstVar pvar, Channel_t *c, unsigned char FAR * buf, unsigned int buflen);
+
+#define finish_send_packet(pvar) finish_send_packet_special((pvar), 0)
+#define get_payload_uint32(pvar, offset) get_uint32_MSBfirst((pvar)->ssh_state.payload + (offset))
+#define get_uint32(buf) get_uint32_MSBfirst((buf))
+#define set_uint32(buf, v) set_uint32_MSBfirst((buf), (v))
+#define get_mpint_len(pvar, offset) ((get_ushort16_MSBfirst((pvar)->ssh_state.payload + (offset)) + 7) >> 3)
+#define get_ushort16(buf) get_ushort16_MSBfirst((buf))
+///
+
 #endif



Ttssh2-commit メーリングリストの案内