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-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 _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 //Forward declaration of ScpClientContext structure
60 struct _ScpClientContext;
61 #define ScpClientContext struct _ScpClientContext
62 
63 //C++ guard
64 #ifdef __cplusplus
65 extern "C" {
66 #endif
67 
68 
69 /**
70  * @brief SCP client state
71  **/
72 
73 typedef enum
74 {
98 
99 
100 /**
101  * @brief SSH initialization callback function
102  **/
103 
106 
107 
108 /**
109  * @brief SCP client context
110  **/
111 
113 {
114  ScpClientState state; ///<SCP client state
115  NetInterface *interface; ///<Underlying network interface
116  ScpClientSshInitCallback sshInitCallback; ///<SSH initialization callback function
117  systime_t timeout; ///<Timeout value
118  systime_t timestamp; ///<Timestamp to manage timeout
119  char_t buffer[SCP_CLIENT_BUFFER_SIZE]; ///<Memory buffer for input/output operations
120  size_t bufferLen; ///<Length of the buffer, in bytes
121  size_t bufferPos; ///<Current position in the buffer
122  ScpOpcode statusCode; ///<Status code
123  uint64_t fileSize; ///<Size of the file, in bytes
124  uint64_t fileOffset; ///<Offset within the file
125  SshContext sshContext; ///<SSH context
126  SshConnection sshConnection; ///<SSH connection
127  SshChannel sshChannel; ///<SSH channel
128 };
129 
130 
131 //SCP client related functions
133 
135  ScpClientSshInitCallback callback);
136 
138 
140  NetInterface *interface);
141 
143  const IpAddr *serverIpAddr, uint16_t serverPort);
144 
146  const char_t *path, uint_t mode, uint64_t size);
147 
149  const char_t *path, uint64_t *size);
150 
151 error_t scpClientWriteFile(ScpClientContext *context, const void *data,
152  size_t length, size_t *written, uint_t flags);
153 
154 error_t scpClientReadFile(ScpClientContext *context, void *data, size_t size,
155  size_t *received, uint_t flags);
156 
158 
161 
162 void scpClientDeinit(ScpClientContext *context);
163 
164 //C++ guard
165 #ifdef __cplusplus
166 }
167 #endif
168 
169 #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:893
size_t bufferLen
Length of the buffer, in bytes.
Definition: scp_client.h:120
IP network address.
Definition: ip.h:90
@ SCP_CLIENT_STATE_WRITE_INIT
Definition: scp_client.h:82
uint8_t data[]
Definition: ethernet.h:222
error_t scpClientBindToInterface(ScpClientContext *context, NetInterface *interface)
Bind the SCP client to a particular network interface.
Definition: scp_client.c:123
@ SCP_CLIENT_STATE_DISCONNECTED
Definition: scp_client.h:75
@ SCP_CLIENT_STATE_WRITE_ACK
Definition: scp_client.h:84
@ SCP_CLIENT_STATE_READ_DATA
Definition: scp_client.h:91
@ SCP_CLIENT_STATE_CHANNEL_REPLY
Definition: scp_client.h:81
ScpOpcode
SCP directive opcodes.
Definition: scp_common.h:62
@ SCP_CLIENT_STATE_READ_COMMAND
Definition: scp_client.h:89
ScpClientState state
SCP client state.
Definition: scp_client.h:114
@ SCP_CLIENT_STATE_CONNECTED
Definition: scp_client.h:78
@ SCP_CLIENT_STATE_CONNECTING_1
Definition: scp_client.h:76
@ SCP_CLIENT_STATE_WRITE_FIN
Definition: scp_client.h:87
systime_t timestamp
Timestamp to manage timeout.
Definition: scp_client.h:118
ScpOpcode statusCode
Status code.
Definition: scp_client.h:122
error_t scpClientOpenFileForWriting(ScpClientContext *context, const char_t *path, uint_t mode, uint64_t size)
Open a file for writing.
Definition: scp_client.c:239
#define SshContext
Definition: ssh.h:870
error_t scpClientSetTimeout(ScpClientContext *context, systime_t timeout)
Set communication timeout.
Definition: scp_client.c:102
error_t
Error codes.
Definition: error.h:43
SshConnection sshConnection
SSH connection.
Definition: scp_client.h:126
error_t scpClientInit(ScpClientContext *context)
Initialize SCP client context.
Definition: scp_client.c:54
#define ScpClientContext
Definition: scp_client.h:61
@ SCP_CLIENT_STATE_CONNECTING_2
Definition: scp_client.h:77
#define NetInterface
Definition: net.h:36
@ SCP_CLIENT_STATE_READ_ACK
Definition: scp_client.h:90
SCP client context.
Definition: scp_client.h:113
@ SCP_CLIENT_STATE_WRITE_DATA
Definition: scp_client.h:85
uint8_t length
Definition: tcp.h:368
uint64_t fileSize
Size of the file, in bytes.
Definition: scp_client.h:123
error_t scpClientCloseFile(ScpClientContext *context)
Close file.
Definition: scp_client.c:996
@ SCP_CLIENT_STATE_READ_STATUS
Definition: scp_client.h:92
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:771
uint8_t flags
Definition: tcp.h:351
char char_t
Definition: compiler_port.h:48
error_t scpClientRegisterSshInitCallback(ScpClientContext *context, ScpClientSshInitCallback callback)
Register SSH initialization callback function.
Definition: scp_client.c:80
@ SCP_CLIENT_STATE_CHANNEL_OPEN
Definition: scp_client.h:79
SshChannel sshChannel
SSH channel.
Definition: scp_client.h:127
NetInterface * interface
Underlying network interface.
Definition: scp_client.h:115
error_t scpClientDisconnect(ScpClientContext *context)
Gracefully disconnect from the SCP server.
Definition: scp_client.c:1161
@ SCP_CLIENT_STATE_WRITE_STATUS
Definition: scp_client.h:86
@ SCP_CLIENT_STATE_READ_INIT
Definition: scp_client.h:88
error_t(* ScpClientSshInitCallback)(ScpClientContext *context, SshContext *sshContext)
SSH initialization callback function.
Definition: scp_client.h:104
#define SshConnection
Definition: ssh.h:874
void scpClientDeinit(ScpClientContext *context)
Release SCP client context.
Definition: scp_client.c:1281
#define SCP_CLIENT_BUFFER_SIZE
Definition: scp_client.h:54
size_t bufferPos
Current position in the buffer.
Definition: scp_client.h:121
error_t scpClientOpenFileForReading(ScpClientContext *context, const char_t *path, uint64_t *size)
Open a file for reading.
Definition: scp_client.c:504
@ SCP_CLIENT_STATE_CHANNEL_REQUEST
Definition: scp_client.h:80
uint64_t fileOffset
Offset within the file.
Definition: scp_client.h:124
@ SCP_CLIENT_STATE_DISCONNECTING_2
Definition: scp_client.h:96
systime_t timeout
Timeout value.
Definition: scp_client.h:117
unsigned int uint_t
Definition: compiler_port.h:50
error_t scpClientClose(ScpClientContext *context)
Close the connection with the SCP server.
Definition: scp_client.c:1260
SshContext sshContext
SSH context.
Definition: scp_client.h:125
Secure Shell (SSH)
@ SCP_CLIENT_STATE_DISCONNECTING_1
Definition: scp_client.h:95
ScpClientState
SCP client state.
Definition: scp_client.h:74
@ SCP_CLIENT_STATE_WRITE_COMMAND
Definition: scp_client.h:83
@ SCP_CLIENT_STATE_READ_FIN
Definition: scp_client.h:93
ScpClientSshInitCallback sshInitCallback
SSH initialization callback function.
Definition: scp_client.h:116
char_t buffer[SCP_CLIENT_BUFFER_SIZE]
Memory buffer for input/output operations.
Definition: scp_client.h:119
#define SshChannel
Definition: ssh.h:878
@ SCP_CLIENT_STATE_CHANNEL_CLOSE
Definition: scp_client.h:94
error_t scpClientConnect(ScpClientContext *context, const IpAddr *serverIpAddr, uint16_t serverPort)
Establish a connection with the specified SCP server.
Definition: scp_client.c:146