sftp_client.h
Go to the documentation of this file.
1 /**
2  * @file sftp_client.h
3  * @brief SFTP client
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2019-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneSSH Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.4
29  **/
30 
31 #ifndef _SFTP_CLIENT_H
32 #define _SFTP_CLIENT_H
33 
34 //Dependencies
35 #include "ssh/ssh.h"
36 #include "sftp/sftp_common.h"
37 
38 //SFTP client support
39 #ifndef SFTP_CLIENT_SUPPORT
40  #define SFTP_CLIENT_SUPPORT DISABLED
41 #elif (SFTP_CLIENT_SUPPORT != ENABLED && SFTP_CLIENT_SUPPORT != DISABLED)
42  #error SFTP_CLIENT_SUPPORT parameter is not valid
43 #endif
44 
45 //Minimum SFTP protocol version that can be negotiated
46 #ifndef SFTP_CLIENT_MIN_VERSION
47  #define SFTP_CLIENT_MIN_VERSION 1
48 #elif (SFTP_CLIENT_MIN_VERSION < 1)
49  #error SFTP_CLIENT_MIN_VERSION parameter is not valid
50 #endif
51 
52 //Maximum SFTP protocol version that can be negotiated
53 #ifndef SFTP_CLIENT_MAX_VERSION
54  #define SFTP_CLIENT_MAX_VERSION 3
55 #elif (SFTP_CLIENT_MAX_VERSION > 3 || SFTP_CLIENT_MAX_VERSION < SFTP_CLIENT_MIN_VERSION)
56  #error SFTP_CLIENT_MAX_VERSION parameter is not valid
57 #endif
58 
59 //Default timeout
60 #ifndef SFTP_CLIENT_DEFAULT_TIMEOUT
61  #define SFTP_CLIENT_DEFAULT_TIMEOUT 20000
62 #elif (SFTP_CLIENT_DEFAULT_TIMEOUT < 1000)
63  #error SFTP_CLIENT_DEFAULT_TIMEOUT parameter is not valid
64 #endif
65 
66 //Maximum packet size
67 #ifndef SFTP_CLIENT_MAX_PACKET_SIZE
68  #define SFTP_CLIENT_MAX_PACKET_SIZE 32768
69 #elif (SFTP_CLIENT_MAX_PACKET_SIZE < 256)
70  #error SFTP_CLIENT_MAX_PACKET_SIZE parameter is not valid
71 #endif
72 
73 //Size of the buffer for input/output operations
74 #ifndef SFTP_CLIENT_BUFFER_SIZE
75  #define SFTP_CLIENT_BUFFER_SIZE 1024
76 #elif (SFTP_CLIENT_BUFFER_SIZE < 256)
77  #error SFTP_CLIENT_BUFFER_SIZE parameter is not valid
78 #endif
79 
80 //Maximum size of file handle
81 #ifndef SFTP_CLIENT_MAX_HANDLE_SIZE
82  #define SFTP_CLIENT_MAX_HANDLE_SIZE 32
83 #elif (SFTP_CLIENT_MAX_HANDLE_SIZE < 4)
84  #error SFTP_CLIENT_MAX_HANDLE_SIZE parameter is not valid
85 #endif
86 
87 //Maximum length of file names
88 #ifndef SFTP_CLIENT_MAX_FILENAME_LEN
89  #define SFTP_CLIENT_MAX_FILENAME_LEN 64
90 #elif (SFTP_CLIENT_MAX_FILENAME_LEN < 16)
91  #error SFTP_CLIENT_MAX_FILENAME_LEN parameter is not valid
92 #endif
93 
94 //Maximum path length
95 #ifndef SFTP_CLIENT_MAX_PATH_LEN
96  #define SFTP_CLIENT_MAX_PATH_LEN 128
97 #elif (SFTP_CLIENT_MAX_PATH_LEN < 16)
98  #error SFTP_CLIENT_MAX_PATH_LEN parameter is not valid
99 #endif
100 
101 //Forward declaration of SftpClientContext structure
102 struct _SftpClientContext;
103 #define SftpClientContext struct _SftpClientContext
104 
105 //C++ guard
106 #ifdef __cplusplus
107 extern "C" {
108 #endif
109 
110 
111 /**
112  * @brief SFTP client state
113  **/
114 
115 typedef enum
116 {
134 
135 
136 /**
137  * @brief SSH initialization callback function
138  **/
139 
142 
143 
144 /**
145  * @brief SFTP client context
146  **/
147 
149 {
150  SftpVersion version; ///<SFTP protocol version
151  SftpClientState state; ///<SFTP client state
152  NetInterface *interface; ///<Underlying network interface
153  SftpClientSshInitCallback sshInitCallback; ///<SSH initialization callback function
154  systime_t timeout; ///<Timeout value
155  systime_t timestamp; ///<Timestamp to manage timeout
156  uint8_t buffer[SFTP_CLIENT_BUFFER_SIZE]; ///<Memory buffer for input/output operations
157  SftpPacketType requestType; ///<Request type
158  uint32_t requestId; ///<Request identifier
159  size_t requestLen;
160  size_t requestPos;
161  size_t responseLen;
162  size_t responsePos;
164  size_t dataLen; ///<Length of the data payload
165  uint64_t fileOffset; ///<Offset within the file
166  uint32_t statusCode; ///<Status code returned by the server
167  char_t currentDir[SFTP_CLIENT_MAX_PATH_LEN + 1]; ///<Current directory
168  uint8_t handle[SFTP_CLIENT_MAX_HANDLE_SIZE]; ///<File handle (opaque string)
169  size_t handleLen; ///<Length of the file handle, in bytes
170  SshContext sshContext; ///<SSH context
171  SshConnection sshConnection; ///<SSH connection
172  SshChannel sshChannel; ///<SSH channel
173 };
174 
175 
176 /**
177  * @brief Directory entry
178  **/
179 
180 typedef struct
181 {
183  uint32_t type;
184  uint64_t size;
185  uint32_t permissions;
187 } SftpDirEntry;
188 
189 
190 //SFTP client related functions
192 
194  SftpClientSshInitCallback callback);
195 
197 
199  NetInterface *interface);
200 
202  const IpAddr *serverIpAddr, uint16_t serverPort);
203 
205 
207  const char_t *path);
208 
210 
211 error_t sftpClientOpenDir(SftpClientContext *context, const char_t *path);
214 
215 error_t sftpClientCreateDir(SftpClientContext *context, const char_t *path);
216 error_t sftpClientDeleteDir(SftpClientContext *context, const char_t *path);
217 
218 error_t sftpClientOpenFile(SftpClientContext *context, const char_t *path,
219  uint_t mode);
220 
221 error_t sftpClientWriteFile(SftpClientContext *context, const void *data,
222  size_t length, size_t *written, uint_t flags);
223 
224 error_t sftpClientReadFile(SftpClientContext *context, void *data, size_t size,
225  size_t *received, uint_t flags);
226 
228 
229 error_t sftpClientRenameFile(SftpClientContext *context, const char_t *oldPath,
230  const char_t *newPath);
231 
233 
235 
238 
239 void sftpClientDeinit(SftpClientContext *context);
240 
241 //C++ guard
242 #ifdef __cplusplus
243 }
244 #endif
245 
246 #endif
uint8_t buffer[SFTP_CLIENT_BUFFER_SIZE]
Memory buffer for input/output operations.
Definition: sftp_client.h:156
SFTP client context.
Definition: sftp_client.h:149
#define SFTP_CLIENT_BUFFER_SIZE
Definition: sftp_client.h:75
SftpPacketType
SFTP packet types.
Definition: sftp_common.h:134
uint64_t size
Definition: sftp_client.h:184
IP network address.
Definition: ip.h:90
error_t sftpClientReadFile(SftpClientContext *context, void *data, size_t size, size_t *received, uint_t flags)
Read from a remote file.
Definition: sftp_client.c:1086
SftpPacketType requestType
Request type.
Definition: sftp_client.h:157
error_t sftpClientBindToInterface(SftpClientContext *context, NetInterface *interface)
Bind the SFTP client to a particular network interface.
Definition: sftp_client.c:121
uint8_t data[]
Definition: ethernet.h:222
error_t sftpClientOpenFile(SftpClientContext *context, const char_t *path, uint_t mode)
Open a file for reading, writing, or appending.
Definition: sftp_client.c:845
systime_t timestamp
Timestamp to manage timeout.
Definition: sftp_client.h:155
@ SFTP_CLIENT_STATE_RECEIVING_NAME
Definition: sftp_client.h:129
@ SFTP_CLIENT_STATE_CONNECTING
Definition: sftp_client.h:118
SftpClientState state
SFTP client state.
Definition: sftp_client.h:151
size_t handleLen
Length of the file handle, in bytes.
Definition: sftp_client.h:169
char_t name[]
Directory entry.
Definition: sftp_client.h:181
SftpVersion version
SFTP protocol version.
Definition: sftp_client.h:150
SftpClientSshInitCallback sshInitCallback
SSH initialization callback function.
Definition: sftp_client.h:153
error_t sftpClientWriteFile(SftpClientContext *context, const void *data, size_t length, size_t *written, uint_t flags)
Write to a remote file.
Definition: sftp_client.c:912
@ SFTP_CLIENT_STATE_SENDING_DATA
Definition: sftp_client.h:127
#define SFTP_CLIENT_MAX_PATH_LEN
Definition: sftp_client.h:96
uint64_t fileOffset
Offset within the file.
Definition: sftp_client.h:165
SftpStatusCode
Status codes.
Definition: sftp_common.h:170
SshChannel sshChannel
SSH channel.
Definition: sftp_client.h:172
error_t sftpClientChangeWorkingDir(SftpClientContext *context, const char_t *path)
Change working directory.
Definition: sftp_client.c:340
#define SshContext
Definition: ssh.h:870
error_t sftpClientInit(SftpClientContext *context)
Initialize SFTP client context.
Definition: sftp_client.c:52
error_t sftpClientCloseFile(SftpClientContext *context)
Close file.
Definition: sftp_client.c:1237
error_t
Error codes.
Definition: error.h:43
error_t(* SftpClientSshInitCallback)(SftpClientContext *context, SshContext *sshContext)
SSH initialization callback function.
Definition: sftp_client.h:140
@ SFTP_CLIENT_STATE_CHANNEL_REQUEST
Definition: sftp_client.h:122
error_t sftpClientCreateDir(SftpClientContext *context, const char_t *path)
Create a new directory.
Definition: sftp_client.c:724
error_t sftpClientReadDir(SftpClientContext *context, SftpDirEntry *dirEntry)
Read an entry from the directory.
Definition: sftp_client.c:508
@ SFTP_CLIENT_STATE_CONNECTED
Definition: sftp_client.h:119
#define NetInterface
Definition: net.h:36
error_t sftpClientRenameFile(SftpClientContext *context, const char_t *oldPath, const char_t *newPath)
Rename a file.
Definition: sftp_client.c:1299
const char_t * sftpClientGetWorkingDir(SftpClientContext *context)
Get current working directory.
Definition: sftp_client.c:313
@ SFTP_CLIENT_STATE_DISCONNECTING_2
Definition: sftp_client.h:131
Date and time representation.
Definition: date_time.h:47
uint8_t length
Definition: tcp.h:368
@ SFTP_CLIENT_STATE_CHANNEL_REPLY
Definition: sftp_client.h:123
SftpClientState
SFTP client state.
Definition: sftp_client.h:116
#define SFTP_CLIENT_MAX_HANDLE_SIZE
Definition: sftp_client.h:82
systime_t timeout
Timeout value.
Definition: sftp_client.h:154
uint8_t handle[SFTP_CLIENT_MAX_HANDLE_SIZE]
File handle (opaque string)
Definition: sftp_client.h:168
uint32_t systime_t
System time.
error_t sftpClientDisconnect(SftpClientContext *context)
Gracefully disconnect from the SFTP server.
Definition: sftp_client.c:1450
uint8_t flags
Definition: tcp.h:351
char char_t
Definition: compiler_port.h:48
SftpStatusCode sftpClientGetStatusCode(SftpClientContext *context)
Retrieve SFTP status code.
Definition: sftp_client.c:1423
size_t dataLen
Length of the data payload.
Definition: sftp_client.h:164
@ SFTP_CLIENT_STATE_DISCONNECTING_1
Definition: sftp_client.h:130
uint32_t permissions
Definition: sftp_client.h:185
@ SFTP_CLIENT_STATE_RECEIVING_DATA
Definition: sftp_client.h:128
error_t sftpClientChangeToParentDir(SftpClientContext *context)
Change to parent directory.
Definition: sftp_client.c:434
#define SftpClientContext
Definition: sftp_client.h:103
#define SshConnection
Definition: ssh.h:874
@ SFTP_CLIENT_STATE_CHANNEL_DATA
Definition: sftp_client.h:124
char_t currentDir[SFTP_CLIENT_MAX_PATH_LEN+1]
Current directory.
Definition: sftp_client.h:167
@ SFTP_CLIENT_STATE_SENDING_COMMAND_1
Definition: sftp_client.h:125
uint32_t type
Definition: sftp_client.h:183
SshContext sshContext
SSH context.
Definition: sftp_client.h:170
SshConnection sshConnection
SSH connection.
Definition: sftp_client.h:171
@ SFTP_CLIENT_STATE_SENDING_COMMAND_2
Definition: sftp_client.h:126
error_t sftpClientSetTimeout(SftpClientContext *context, systime_t timeout)
Set communication timeout.
Definition: sftp_client.c:100
@ SFTP_CLIENT_STATE_DISCONNECTING_3
Definition: sftp_client.h:132
error_t sftpClientCloseDir(SftpClientContext *context)
Close directory.
Definition: sftp_client.c:663
uint32_t requestId
Request identifier.
Definition: sftp_client.h:158
Definitions common to SFTP client and server.
SftpVersion
SFTP protocol version.
Definition: sftp_common.h:118
NetInterface * interface
Underlying network interface.
Definition: sftp_client.h:152
@ SFTP_CLIENT_STATE_CHANNEL_OPEN
Definition: sftp_client.h:120
uint32_t statusCode
Status code returned by the server.
Definition: sftp_client.h:166
error_t sftpClientOpenDir(SftpClientContext *context, const char_t *path)
Open a directory.
Definition: sftp_client.c:448
error_t sftpClientDeleteFile(SftpClientContext *context, const char_t *path)
Delete a file.
Definition: sftp_client.c:1364
DateTime modified
Definition: sftp_client.h:186
unsigned int uint_t
Definition: compiler_port.h:50
error_t sftpClientDeleteDir(SftpClientContext *context, const char_t *path)
Remove a directory.
Definition: sftp_client.c:784
@ SFTP_CLIENT_STATE_CHANNEL_OPEN_REPLY
Definition: sftp_client.h:121
Secure Shell (SSH)
error_t sftpClientRegisterSshInitCallback(SftpClientContext *context, SftpClientSshInitCallback callback)
Register SSH initialization callback function.
Definition: sftp_client.c:78
#define SFTP_CLIENT_MAX_FILENAME_LEN
Definition: sftp_client.h:89
void sftpClientDeinit(SftpClientContext *context)
Release SFTP client context.
Definition: sftp_client.c:1592
@ SFTP_CLIENT_STATE_DISCONNECTED
Definition: sftp_client.h:117
error_t sftpClientConnect(SftpClientContext *context, const IpAddr *serverIpAddr, uint16_t serverPort)
Establish a connection with the specified SFTP server.
Definition: sftp_client.c:144
#define SshChannel
Definition: ssh.h:878
error_t sftpClientClose(SftpClientContext *context)
Close the connection with the SFTP server.
Definition: sftp_client.c:1571