1 #ifndef __LIBSSH2_SFTP_H
2 #define __LIBSSH2_SFTP_H
3 /*
4 * Copyright (C) 2010 - 2012 by Daniel Stenberg
5 * Author: Daniel Stenberg <daniel@haxx.se>
6 *
7 * Redistribution and use in source and binary forms,
8 * with or without modification, are permitted provided
9 * that the following conditions are met:
10 *
11 * Redistributions of source code must retain the above
12 * copyright notice, this list of conditions and the
13 * following disclaimer.
14 *
15 * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials
18 * provided with the distribution.
19 *
20 * Neither the name of the copyright holder nor the names
21 * of any other contributors may be used to endorse or
22 * promote products derived from this software without
23 * specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
26 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
27 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
30 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
32 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
33 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
35 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
38 * OF SUCH DAMAGE.
39 *
40 */
41
42 /*
43 * MAX_SFTP_OUTGOING_SIZE MUST not be larger than 32500 or so. This is the
44 * amount of data sent in each FXP_WRITE packet
45 */
46 #define MAX_SFTP_OUTGOING_SIZE 30000
47
48 /* MAX_SFTP_READ_SIZE is how much data is asked for at max in each FXP_READ
49 * packets.
50 */
51 #define MAX_SFTP_READ_SIZE 30000
52
53 struct sftp_pipeline_chunk {
54 struct list_node node;
55 libssh2_uint64_t offset; /* READ: offset at which to start reading
56 WRITE: not used */
57 size_t len; /* WRITE: size of the data to write
58 READ: how many bytes that was asked for */
59 size_t sent;
60 ssize_t lefttosend; /* if 0, the entire packet has been sent off */
61 uint32_t request_id;
62 unsigned char packet[1]; /* data */
63 };
64
65 struct sftp_zombie_requests {
66 struct list_node node;
67 uint32_t request_id;
68 };
69
70 #ifndef MIN
71 #define MIN(x,y) ((x)<(y)?(x):(y))
72 #endif
73
74 struct _LIBSSH2_SFTP_PACKET
75 {
76 struct list_node node; /* linked list header */
77 uint32_t request_id;
78 unsigned char *data;
79 size_t data_len; /* payload size */
80 };
81
82 typedef struct _LIBSSH2_SFTP_PACKET LIBSSH2_SFTP_PACKET;
83
84 #define SFTP_HANDLE_MAXLEN 256 /* according to spec! */
85
86 struct _LIBSSH2_SFTP_HANDLE
87 {
88 struct list_node node;
89
90 LIBSSH2_SFTP *sftp;
91
92 char handle[SFTP_HANDLE_MAXLEN];
93 size_t handle_len;
94
95 enum {
96 LIBSSH2_SFTP_HANDLE_FILE,
97 LIBSSH2_SFTP_HANDLE_DIR
98 } handle_type;
99
100 union _libssh2_sftp_handle_data
101 {
102 struct _libssh2_sftp_handle_file_data
103 {
sftp_attrsize(unsigned long flags)104 libssh2_uint64_t offset;
105 libssh2_uint64_t offset_sent;
106 size_t acked; /* container for acked data that hasn't been
107 returned to caller yet, used for sftp_write */
108
109 /* 'data' is used by sftp_read() and is allocated data that has
110 been received already from the server but wasn't returned to
111 the caller yet. It is of size 'data_len' and 'data_left is the
112 number of bytes not yet returned, counted from the end of the
113 buffer. */
114 unsigned char *data;
115 size_t data_len;
_libssh2_store_u64(unsigned char ** ptr,libssh2_uint64_t value)116 size_t data_left;
117
118 char eof; /* we have read to the end */
119 } file;
120 struct _libssh2_sftp_handle_dir_data
121 {
122 uint32_t names_left;
123 void *names_packet;
124 char *next_name;
125 size_t names_packet_len;
126 } dir;
127 } u;
128
129 /* State variables used in libssh2_sftp_close_handle() */
130 libssh2_nonblocking_states close_state;
131 uint32_t close_request_id;
132 unsigned char *close_packet;
133
134 /* list of outstanding packets sent to server */
135 struct list_head packet_list;
136
137 };
138
139 struct _LIBSSH2_SFTP
find_zombie_request(LIBSSH2_SFTP * sftp,uint32_t request_id)140 {
141 LIBSSH2_CHANNEL *channel;
142
143 uint32_t request_id, version;
144
145 struct list_head packets;
146
147 /* List of FXP_READ responses to ignore because EOF already received. */
148 struct list_head zombie_requests;
149
150 /* a list of _LIBSSH2_SFTP_HANDLE structs */
151 struct list_head sftp_handles;
152
153 uint32_t last_errno;
154
155 /* Holder for partial packet, use in libssh2_sftp_packet_read() */
156 unsigned char partial_size[4]; /* buffer for size field */
157 size_t partial_size_len; /* size field length */
158 unsigned char *partial_packet; /* The data */
159 uint32_t partial_len; /* Desired number of bytes */
160 size_t partial_received; /* Bytes received so far */
161
162 /* Time that libssh2_sftp_packet_requirev() started reading */
163 time_t requirev_start;
164
165 /* State variables used in libssh2_sftp_open_ex() */
166 libssh2_nonblocking_states open_state;
167 unsigned char *open_packet;
168 uint32_t open_packet_len; /* 32 bit on the wire */
169 size_t open_packet_sent;
170 uint32_t open_request_id;
171
172 /* State variable used in sftp_read() */
173 libssh2_nonblocking_states read_state;
174
175 /* State variable used in sftp_packet_read() */
176 libssh2_nonblocking_states packet_state;
177
178 /* State variable used in sftp_write() */
179 libssh2_nonblocking_states write_state;
180
181 /* State variables used in sftp_fsync() */
182 libssh2_nonblocking_states fsync_state;
183 unsigned char *fsync_packet;
184 uint32_t fsync_request_id;
185
186 /* State variables used in libssh2_sftp_readdir() */
187 libssh2_nonblocking_states readdir_state;
188 unsigned char *readdir_packet;
189 uint32_t readdir_request_id;
190
191 /* State variables used in libssh2_sftp_fstat_ex() */
192 libssh2_nonblocking_states fstat_state;
193 unsigned char *fstat_packet;
194 uint32_t fstat_request_id;
195
196 /* State variables used in libssh2_sftp_unlink_ex() */
197 libssh2_nonblocking_states unlink_state;
198 unsigned char *unlink_packet;
199 uint32_t unlink_request_id;
200
201 /* State variables used in libssh2_sftp_rename_ex() */
202 libssh2_nonblocking_states rename_state;
203 unsigned char *rename_packet;
204 unsigned char *rename_s;
205 uint32_t rename_request_id;
206
207 /* State variables used in libssh2_sftp_fstatvfs() */
208 libssh2_nonblocking_states fstatvfs_state;
209 unsigned char *fstatvfs_packet;
210 uint32_t fstatvfs_request_id;
211
212 /* State variables used in libssh2_sftp_statvfs() */
213 libssh2_nonblocking_states statvfs_state;
214 unsigned char *statvfs_packet;
215 uint32_t statvfs_request_id;
216
217 /* State variables used in libssh2_sftp_mkdir() */
218 libssh2_nonblocking_states mkdir_state;
219 unsigned char *mkdir_packet;
220 uint32_t mkdir_request_id;
221
222 /* State variables used in libssh2_sftp_rmdir() */
223 libssh2_nonblocking_states rmdir_state;
224 unsigned char *rmdir_packet;
225 uint32_t rmdir_request_id;
226
227 /* State variables used in libssh2_sftp_stat() */
228 libssh2_nonblocking_states stat_state;
229 unsigned char *stat_packet;
230 uint32_t stat_request_id;
231
232 /* State variables used in libssh2_sftp_symlink() */
233 libssh2_nonblocking_states symlink_state;
234 unsigned char *symlink_packet;
235 uint32_t symlink_request_id;
236 };
237
238 #endif /* __LIBSSH2_SFTP_H */
239