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-2026 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.6.0
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  NetContext *netContext; ///<TCP/IP stack context
153  NetInterface *interface; ///<Underlying network interface
154  SftpClientSshInitCallback sshInitCallback; ///<SSH initialization callback function
155  systime_t timeout; ///<Timeout value
156  systime_t timestamp; ///<Timestamp to manage timeout
157  uint8_t buffer[SFTP_CLIENT_BUFFER_SIZE]; ///<Memory buffer for input/output operations
158  SftpPacketType requestType; ///<Request type
159  uint32_t requestId; ///<Request identifier
160  size_t requestLen;
161  size_t requestPos;
162  size_t responseLen;
163  size_t responsePos;
165  size_t dataLen; ///<Length of the data payload
166  uint64_t fileOffset; ///<Offset within the file
167  uint32_t statusCode; ///<Status code returned by the server
168  char_t currentDir[SFTP_CLIENT_MAX_PATH_LEN + 1]; ///<Current directory
169  uint8_t handle[SFTP_CLIENT_MAX_HANDLE_SIZE]; ///<File handle (opaque string)
170  size_t handleLen; ///<Length of the file handle, in bytes
171  SshContext sshContext; ///<SSH context
172  SshConnection sshConnection; ///<SSH connection
173  SshChannel sshChannel; ///<SSH channel
174 };
175 
176 
177 /**
178  * @brief Directory entry
179  **/
180 
181 typedef struct
182 {
184  uint32_t type;
185  uint64_t size;
186  uint32_t permissions;
188 } SftpDirEntry;
189 
190 
191 //SFTP client related functions
193 
195  SftpClientSshInitCallback callback);
196 
198 
200  NetInterface *interface);
201 
203  const IpAddr *serverIpAddr, uint16_t serverPort);
204 
206 
208  const char_t *path);
209 
211 
212 error_t sftpClientOpenDir(SftpClientContext *context, const char_t *path);
215 
216 error_t sftpClientCreateDir(SftpClientContext *context, const char_t *path);
217 error_t sftpClientDeleteDir(SftpClientContext *context, const char_t *path);
218 
219 error_t sftpClientOpenFile(SftpClientContext *context, const char_t *path,
220  uint_t mode);
221 
222 error_t sftpClientWriteFile(SftpClientContext *context, const void *data,
223  size_t length, size_t *written, uint_t flags);
224 
225 error_t sftpClientReadFile(SftpClientContext *context, void *data, size_t size,
226  size_t *received, uint_t flags);
227 
229 
230 error_t sftpClientRenameFile(SftpClientContext *context, const char_t *oldPath,
231  const char_t *newPath);
232 
234 
236 
239 
240 void sftpClientDeinit(SftpClientContext *context);
241 
242 //C++ guard
243 #ifdef __cplusplus
244 }
245 #endif
246 
247 #endif
uint8_t buffer[SFTP_CLIENT_BUFFER_SIZE]
Memory buffer for input/output operations.
Definition: sftp_client.h:157
#define NetContext
Definition: net.h:36
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:185
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:1089
SftpPacketType requestType
Request type.
Definition: sftp_client.h:158
error_t sftpClientBindToInterface(SftpClientContext *context, NetInterface *interface)
Bind the SFTP client to a particular network interface.
Definition: sftp_client.c:124
uint8_t data[]
Definition: ethernet.h:224
error_t sftpClientOpenFile(SftpClientContext *context, const char_t *path, uint_t mode)
Open a file for reading, writing, or appending.
Definition: sftp_client.c:848
systime_t timestamp
Timestamp to manage timeout.
Definition: sftp_client.h:156
@ 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:170
char_t name[]
Directory entry.
Definition: sftp_client.h:182
SftpVersion version
SFTP protocol version.
Definition: sftp_client.h:150
SftpClientSshInitCallback sshInitCallback
SSH initialization callback function.
Definition: sftp_client.h:154
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:915
@ 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:166
SftpStatusCode
Status codes.
Definition: sftp_common.h:170
SshChannel sshChannel
SSH channel.
Definition: sftp_client.h:173
error_t sftpClientChangeWorkingDir(SftpClientContext *context, const char_t *path)
Change working directory.
Definition: sftp_client.c:343
#define SshContext
Definition: ssh.h:892
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:1240
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:727
error_t sftpClientReadDir(SftpClientContext *context, SftpDirEntry *dirEntry)
Read an entry from the directory.
Definition: sftp_client.c:511
@ SFTP_CLIENT_STATE_CONNECTED
Definition: sftp_client.h:119
#define NetInterface
Definition: net.h:40
error_t sftpClientRenameFile(SftpClientContext *context, const char_t *oldPath, const char_t *newPath)
Rename a file.
Definition: sftp_client.c:1302
NetContext * netContext
TCP/IP stack context.
Definition: sftp_client.h:152
const char_t * sftpClientGetWorkingDir(SftpClientContext *context)
Get current working directory.
Definition: sftp_client.c:316
@ SFTP_CLIENT_STATE_DISCONNECTING_2
Definition: sftp_client.h:131
Date and time representation.
Definition: date_time.h:54
uint8_t length
Definition: tcp.h:375
@ 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:155
uint8_t handle[SFTP_CLIENT_MAX_HANDLE_SIZE]
File handle (opaque string)
Definition: sftp_client.h:169
uint32_t systime_t
System time.
error_t sftpClientDisconnect(SftpClientContext *context)
Gracefully disconnect from the SFTP server.
Definition: sftp_client.c:1453
char char_t
Definition: compiler_port.h:55
SftpStatusCode sftpClientGetStatusCode(SftpClientContext *context)
Retrieve SFTP status code.
Definition: sftp_client.c:1426
size_t dataLen
Length of the data payload.
Definition: sftp_client.h:165
@ SFTP_CLIENT_STATE_DISCONNECTING_1
Definition: sftp_client.h:130
uint32_t permissions
Definition: sftp_client.h:186
@ SFTP_CLIENT_STATE_RECEIVING_DATA
Definition: sftp_client.h:128
error_t sftpClientChangeToParentDir(SftpClientContext *context)
Change to parent directory.
Definition: sftp_client.c:437
#define SftpClientContext
Definition: sftp_client.h:103
#define SshConnection
Definition: ssh.h:896
@ 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:168
@ SFTP_CLIENT_STATE_SENDING_COMMAND_1
Definition: sftp_client.h:125
uint32_t type
Definition: sftp_client.h:184
SshContext sshContext
SSH context.
Definition: sftp_client.h:171
SshConnection sshConnection
SSH connection.
Definition: sftp_client.h:172
@ 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:103
@ SFTP_CLIENT_STATE_DISCONNECTING_3
Definition: sftp_client.h:132
error_t sftpClientCloseDir(SftpClientContext *context)
Close directory.
Definition: sftp_client.c:666
uint32_t requestId
Request identifier.
Definition: sftp_client.h:159
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:153
@ SFTP_CLIENT_STATE_CHANNEL_OPEN
Definition: sftp_client.h:120
uint32_t statusCode
Status code returned by the server.
Definition: sftp_client.h:167
error_t sftpClientOpenDir(SftpClientContext *context, const char_t *path)
Open a directory.
Definition: sftp_client.c:451
error_t sftpClientDeleteFile(SftpClientContext *context, const char_t *path)
Delete a file.
Definition: sftp_client.c:1367
uint8_t flags
Definition: tcp.h:358
DateTime modified
Definition: sftp_client.h:187
unsigned int uint_t
Definition: compiler_port.h:57
error_t sftpClientDeleteDir(SftpClientContext *context, const char_t *path)
Remove a directory.
Definition: sftp_client.c:787
@ 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:81
#define SFTP_CLIENT_MAX_FILENAME_LEN
Definition: sftp_client.h:89
void sftpClientDeinit(SftpClientContext *context)
Release SFTP client context.
Definition: sftp_client.c:1595
@ 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:147
#define SshChannel
Definition: ssh.h:900
error_t sftpClientClose(SftpClientContext *context)
Close the connection with the SFTP server.
Definition: sftp_client.c:1574