scp_client.h
Go to the documentation of this file.
1 /**
2  * @file scp_client.h
3  * @brief SCP 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.6
29  **/
30 
31 #ifndef _SCP_CLIENT_H
32 #define _SCP_CLIENT_H
33 
34 //Dependencies
35 #include "ssh/ssh.h"
36 #include "scp/scp_common.h"
37 
38 //SCP client support
39 #ifndef SCP_CLIENT_SUPPORT
40  #define SCP_CLIENT_SUPPORT DISABLED
41 #elif (SCP_CLIENT_SUPPORT != ENABLED && SCP_CLIENT_SUPPORT != DISABLED)
42  #error SCP_CLIENT_SUPPORT parameter is not valid
43 #endif
44 
45 //Default timeout
46 #ifndef SCP_CLIENT_DEFAULT_TIMEOUT
47  #define SCP_CLIENT_DEFAULT_TIMEOUT 20000
48 #elif (SCP_CLIENT_DEFAULT_TIMEOUT < 1000)
49  #error SCP_CLIENT_DEFAULT_TIMEOUT parameter is not valid
50 #endif
51 
52 //Size of the buffer for input/output operations
53 #ifndef SCP_CLIENT_BUFFER_SIZE
54  #define SCP_CLIENT_BUFFER_SIZE 512
55 #elif (SCP_CLIENT_BUFFER_SIZE < 256)
56  #error SCP_CLIENT_BUFFER_SIZE parameter is not valid
57 #endif
58 
59 //Application specific context
60 #ifndef SCP_CLIENT_CONTEXT_PRIVATE
61  #define SCP_CLIENT_CONTEXT_PRIVATE
62 #endif
63 
64 //Forward declaration of ScpClientContext structure
65 struct _ScpClientContext;
66 #define ScpClientContext struct _ScpClientContext
67 
68 //C++ guard
69 #ifdef __cplusplus
70 extern "C" {
71 #endif
72 
73 
74 /**
75  * @brief SCP client state
76  **/
77 
78 typedef enum
79 {
103 
104 
105 /**
106  * @brief SSH initialization callback function
107  **/
108 
111 
112 
113 /**
114  * @brief SCP client context
115  **/
116 
118 {
119  ScpClientState state; ///<SCP client state
120  NetContext *netContext; ///<TCP/IP stack context
121  NetInterface *interface; ///<Underlying network interface
122  ScpClientSshInitCallback sshInitCallback; ///<SSH initialization callback function
123  systime_t timeout; ///<Timeout value
124  systime_t timestamp; ///<Timestamp to manage timeout
125  char_t buffer[SCP_CLIENT_BUFFER_SIZE]; ///<Memory buffer for input/output operations
126  size_t bufferLen; ///<Length of the buffer, in bytes
127  size_t bufferPos; ///<Current position in the buffer
128  ScpOpcode statusCode; ///<Status code
129  uint64_t fileSize; ///<Size of the file, in bytes
130  uint64_t fileOffset; ///<Offset within the file
131  uint32_t exitStatus; ///<Exit status of the SCP command
132  SshContext sshContext; ///<SSH context
133  SshConnection sshConnection; ///<SSH connection
134  SshChannel sshChannel; ///<SSH channel
135  SCP_CLIENT_CONTEXT_PRIVATE ///<Application specific context
136 };
137 
138 
139 //SCP client related functions
141 
143  ScpClientSshInitCallback callback);
144 
146 
148  NetInterface *interface);
149 
151  const IpAddr *serverIpAddr, uint16_t serverPort);
152 
154  const char_t *path, uint_t mode, uint64_t size);
155 
157  const char_t *path, uint64_t *size);
158 
159 error_t scpClientWriteFile(ScpClientContext *context, const void *data,
160  size_t length, size_t *written, uint_t flags);
161 
162 error_t scpClientReadFile(ScpClientContext *context, void *data, size_t size,
163  size_t *received, uint_t flags);
164 
166 
169 
170 void scpClientDeinit(ScpClientContext *context);
171 
172 //C++ guard
173 #ifdef __cplusplus
174 }
175 #endif
176 
177 #endif
error_t scpClientReadFile(ScpClientContext *context, void *data, size_t size, size_t *received, uint_t flags)
Read from a remote file.
Definition: scp_client.c:904
#define NetContext
Definition: net.h:36
size_t bufferLen
Length of the buffer, in bytes.
Definition: scp_client.h:126
IP network address.
Definition: ip.h:94
@ SCP_CLIENT_STATE_WRITE_INIT
Definition: scp_client.h:87
uint8_t data[]
Definition: ethernet.h:224
error_t scpClientBindToInterface(ScpClientContext *context, NetInterface *interface)
Bind the SCP client to a particular network interface.
Definition: scp_client.c:128
@ SCP_CLIENT_STATE_DISCONNECTED
Definition: scp_client.h:80
@ SCP_CLIENT_STATE_WRITE_ACK
Definition: scp_client.h:89
@ SCP_CLIENT_STATE_READ_DATA
Definition: scp_client.h:96
@ SCP_CLIENT_STATE_CHANNEL_REPLY
Definition: scp_client.h:86
#define SCP_CLIENT_CONTEXT_PRIVATE
Definition: scp_client.h:61
ScpOpcode
SCP directive opcodes.
Definition: scp_common.h:62
@ SCP_CLIENT_STATE_READ_COMMAND
Definition: scp_client.h:94
ScpClientState state
SCP client state.
Definition: scp_client.h:119
@ SCP_CLIENT_STATE_CONNECTED
Definition: scp_client.h:83
@ SCP_CLIENT_STATE_CONNECTING_1
Definition: scp_client.h:81
@ SCP_CLIENT_STATE_WRITE_FIN
Definition: scp_client.h:92
systime_t timestamp
Timestamp to manage timeout.
Definition: scp_client.h:124
ScpOpcode statusCode
Status code.
Definition: scp_client.h:128
error_t scpClientOpenFileForWriting(ScpClientContext *context, const char_t *path, uint_t mode, uint64_t size)
Open a file for writing.
Definition: scp_client.c:244
#define SshContext
Definition: ssh.h:931
error_t scpClientSetTimeout(ScpClientContext *context, systime_t timeout)
Set communication timeout.
Definition: scp_client.c:107
error_t
Error codes.
Definition: error.h:43
SshConnection sshConnection
SSH connection.
Definition: scp_client.h:133
error_t scpClientInit(ScpClientContext *context)
Initialize SCP client context.
Definition: scp_client.c:54
#define ScpClientContext
Definition: scp_client.h:66
@ SCP_CLIENT_STATE_CONNECTING_2
Definition: scp_client.h:82
#define NetInterface
Definition: net.h:40
uint32_t exitStatus
Exit status of the SCP command.
Definition: scp_client.h:131
@ SCP_CLIENT_STATE_READ_ACK
Definition: scp_client.h:95
SCP client context.
Definition: scp_client.h:118
@ SCP_CLIENT_STATE_WRITE_DATA
Definition: scp_client.h:90
uint8_t length
Definition: tcp.h:375
uint64_t fileSize
Size of the file, in bytes.
Definition: scp_client.h:129
error_t scpClientCloseFile(ScpClientContext *context)
Close file.
Definition: scp_client.c:1007
@ SCP_CLIENT_STATE_READ_STATUS
Definition: scp_client.h:97
Definitions common to SCP client and server.
uint32_t systime_t
System time.
error_t scpClientWriteFile(ScpClientContext *context, const void *data, size_t length, size_t *written, uint_t flags)
Write to a remote file.
Definition: scp_client.c:782
char char_t
Definition: compiler_port.h:55
error_t scpClientRegisterSshInitCallback(ScpClientContext *context, ScpClientSshInitCallback callback)
Register SSH initialization callback function.
Definition: scp_client.c:85
@ SCP_CLIENT_STATE_CHANNEL_OPEN
Definition: scp_client.h:84
SshChannel sshChannel
SSH channel.
Definition: scp_client.h:134
NetContext * netContext
TCP/IP stack context.
Definition: scp_client.h:120
NetInterface * interface
Underlying network interface.
Definition: scp_client.h:121
error_t scpClientDisconnect(ScpClientContext *context)
Gracefully disconnect from the SCP server.
Definition: scp_client.c:1179
@ SCP_CLIENT_STATE_WRITE_STATUS
Definition: scp_client.h:91
@ SCP_CLIENT_STATE_READ_INIT
Definition: scp_client.h:93
error_t(* ScpClientSshInitCallback)(ScpClientContext *context, SshContext *sshContext)
SSH initialization callback function.
Definition: scp_client.h:109
#define SshConnection
Definition: ssh.h:935
void scpClientDeinit(ScpClientContext *context)
Release SCP client context.
Definition: scp_client.c:1299
#define SCP_CLIENT_BUFFER_SIZE
Definition: scp_client.h:54
size_t bufferPos
Current position in the buffer.
Definition: scp_client.h:127
error_t scpClientOpenFileForReading(ScpClientContext *context, const char_t *path, uint64_t *size)
Open a file for reading.
Definition: scp_client.c:512
@ SCP_CLIENT_STATE_CHANNEL_REQUEST
Definition: scp_client.h:85
uint64_t fileOffset
Offset within the file.
Definition: scp_client.h:130
uint8_t flags
Definition: tcp.h:358
@ SCP_CLIENT_STATE_DISCONNECTING_2
Definition: scp_client.h:101
systime_t timeout
Timeout value.
Definition: scp_client.h:123
unsigned int uint_t
Definition: compiler_port.h:57
error_t scpClientClose(ScpClientContext *context)
Close the connection with the SCP server.
Definition: scp_client.c:1278
SshContext sshContext
SSH context.
Definition: scp_client.h:132
Secure Shell (SSH)
@ SCP_CLIENT_STATE_DISCONNECTING_1
Definition: scp_client.h:100
ScpClientState
SCP client state.
Definition: scp_client.h:79
@ SCP_CLIENT_STATE_WRITE_COMMAND
Definition: scp_client.h:88
@ SCP_CLIENT_STATE_READ_FIN
Definition: scp_client.h:98
ScpClientSshInitCallback sshInitCallback
SSH initialization callback function.
Definition: scp_client.h:122
char_t buffer[SCP_CLIENT_BUFFER_SIZE]
Memory buffer for input/output operations.
Definition: scp_client.h:125
#define SshChannel
Definition: ssh.h:939
@ SCP_CLIENT_STATE_CHANNEL_CLOSE
Definition: scp_client.h:99
error_t scpClientConnect(ScpClientContext *context, const IpAddr *serverIpAddr, uint16_t serverPort)
Establish a connection with the specified SCP server.
Definition: scp_client.c:151