shell_client.h
Go to the documentation of this file.
1 /**
2  * @file shell_client.h
3  * @brief SSH secure shell 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 _SHELL_CLIENT_H
32 #define _SHELL_CLIENT_H
33 
34 //Dependencies
35 #include "ssh/ssh.h"
36 
37 //Shell client support
38 #ifndef SHELL_CLIENT_SUPPORT
39  #define SHELL_CLIENT_SUPPORT DISABLED
40 #elif (SHELL_CLIENT_SUPPORT != ENABLED && SHELL_CLIENT_SUPPORT != DISABLED)
41  #error SHELL_CLIENT_SUPPORT parameter is not valid
42 #endif
43 
44 //Default timeout
45 #ifndef SHELL_CLIENT_DEFAULT_TIMEOUT
46  #define SHELL_CLIENT_DEFAULT_TIMEOUT 20000
47 #elif (SHELL_CLIENT_DEFAULT_TIMEOUT < 1000)
48  #error SHELL_CLIENT_DEFAULT_TIMEOUT parameter is not valid
49 #endif
50 
51 //Size of the buffer for input/output operations
52 #ifndef SHELL_CLIENT_BUFFER_SIZE
53  #define SHELL_CLIENT_BUFFER_SIZE 512
54 #elif (SHELL_CLIENT_BUFFER_SIZE < 256)
55  #error SHELL_CLIENT_BUFFER_SIZE parameter is not valid
56 #endif
57 
58 //Application specific context
59 #ifndef SHELL_CLIENT_CONTEXT_PRIVATE
60  #define SHELL_CLIENT_CONTEXT_PRIVATE
61 #endif
62 
63 //Forward declaration of ShellClientContext structure
64 struct _ShellClientContext;
65 #define ShellClientContext struct _ShellClientContext
66 
67 //C++ guard
68 #ifdef __cplusplus
69 extern "C" {
70 #endif
71 
72 
73 /**
74  * @brief Shell client state
75  **/
76 
77 typedef enum
78 {
92 
93 
94 /**
95  * @brief SSH initialization callback function
96  **/
97 
100 
101 
102 /**
103  * @brief Shell client context
104  **/
105 
107 {
108  ShellClientState state; ///<Shell client state
109  NetContext *netContext; ///<TCP/IP stack context
110  NetInterface *interface; ///<Underlying network interface
111  ShellClientSshInitCallback sshInitCallback; ///<SSH initialization callback function
112  systime_t timeout; ///<Timeout value
113  systime_t timestamp; ///<Timestamp to manage timeout
114  char_t buffer[SHELL_CLIENT_BUFFER_SIZE]; ///<Memory buffer for input/output operations
115  size_t bufferLen; ///<Length of the buffer, in bytes
116  size_t bufferPos; ///<Current position in the buffer
117  int32_t exitStatus; ///<Exit status of the command
118  SshContext sshContext; ///<SSH context
119  SshConnection sshConnection; ///<SSH connection
120  SshChannel sshChannel; ///<SSH channel
121  SHELL_CLIENT_CONTEXT_PRIVATE ///<Application specific context
122 };
123 
124 
125 //Shell client related functions
127 
129  ShellClientSshInitCallback callback);
130 
132 
134  NetInterface *interface);
135 
137  const IpAddr *serverIpAddr, uint16_t serverPort);
138 
140  const char_t *command, ...);
141 
143  const char_t *command);
144 
146  size_t length, size_t *written, uint_t flags);
147 
149 
151  size_t size, size_t *received, uint_t flags);
152 
155 
158 
160 
161 //C++ guard
162 #ifdef __cplusplus
163 }
164 #endif
165 
166 #endif
@ SHELL_CLIENT_STATE_CHANNEL_CLOSE
Definition: shell_client.h:88
error_t shellClientCloseStream(ShellClientContext *context)
Close stream.
Definition: shell_client.c:747
@ SHELL_CLIENT_STATE_CHANNEL_REQUEST
Definition: shell_client.h:85
#define NetContext
Definition: net.h:36
SshConnection sshConnection
SSH connection.
Definition: shell_client.h:119
error_t shellClientDisconnect(ShellClientContext *context)
Gracefully disconnect from the SSH server.
Definition: shell_client.c:875
error_t shellClientBindToInterface(ShellClientContext *context, NetInterface *interface)
Bind the shell client to a particular network interface.
Definition: shell_client.c:128
@ SHELL_CLIENT_STATE_CONNECTING_1
Definition: shell_client.h:80
IP network address.
Definition: ip.h:94
@ SHELL_CLIENT_STATE_DISCONNECTED
Definition: shell_client.h:79
NetContext * netContext
TCP/IP stack context.
Definition: shell_client.h:109
@ SHELL_CLIENT_STATE_CHANNEL_INIT
Definition: shell_client.h:83
@ SHELL_CLIENT_STATE_DISCONNECTING_1
Definition: shell_client.h:89
error_t shellClientInit(ShellClientContext *context)
Initialize shell client context.
Definition: shell_client.c:54
uint8_t data[]
Definition: ethernet.h:224
error_t(* ShellClientSshInitCallback)(ShellClientContext *context, SshContext *sshContext)
SSH initialization callback function.
Definition: shell_client.h:98
#define ShellClientContext
Definition: shell_client.h:65
error_t shellClientSetTimeout(ShellClientContext *context, systime_t timeout)
Set communication timeout.
Definition: shell_client.c:107
int32_t exitStatus
Exit status of the command.
Definition: shell_client.h:117
@ SHELL_CLIENT_STATE_CHANNEL_REPLY
Definition: shell_client.h:86
size_t bufferLen
Length of the buffer, in bytes.
Definition: shell_client.h:115
#define SHELL_CLIENT_BUFFER_SIZE
Definition: shell_client.h:53
error_t shellClientConnect(ShellClientContext *context, const IpAddr *serverIpAddr, uint16_t serverPort)
Establish a connection with the specified SSH server.
Definition: shell_client.c:151
#define SshContext
Definition: ssh.h:931
error_t
Error codes.
Definition: error.h:43
error_t shellClientReadStream(ShellClientContext *context, void *data, size_t size, size_t *received, uint_t flags)
Read from stdout stream.
Definition: shell_client.c:660
Shell client context.
Definition: shell_client.h:107
ShellClientState state
Shell client state.
Definition: shell_client.h:108
error_t shellClientRegisterSshInitCallback(ShellClientContext *context, ShellClientSshInitCallback callback)
Register SSH initialization callback function.
Definition: shell_client.c:85
size_t bufferPos
Current position in the buffer.
Definition: shell_client.h:116
#define NetInterface
Definition: net.h:40
@ SHELL_CLIENT_STATE_CHANNEL_DATA
Definition: shell_client.h:87
NetInterface * interface
Underlying network interface.
Definition: shell_client.h:110
@ SHELL_CLIENT_STATE_CONNECTED
Definition: shell_client.h:82
int32_t shellClientGetExitStatus(ShellClientContext *context)
Retrieve exit status.
Definition: shell_client.c:848
uint8_t length
Definition: tcp.h:375
systime_t timeout
Timeout value.
Definition: shell_client.h:112
error_t shellClientExecuteCommand(ShellClientContext *context, const char_t *command)
Execute a command line.
Definition: shell_client.c:322
uint32_t systime_t
System time.
char char_t
Definition: compiler_port.h:55
@ SHELL_CLIENT_STATE_CHANNEL_OPEN
Definition: shell_client.h:84
#define SshConnection
Definition: ssh.h:935
error_t shellClientClose(ShellClientContext *context)
Close the connection with the SSH server.
Definition: shell_client.c:974
#define SHELL_CLIENT_CONTEXT_PRIVATE
Definition: shell_client.h:60
SshChannel sshChannel
SSH channel.
Definition: shell_client.h:120
systime_t timestamp
Timestamp to manage timeout.
Definition: shell_client.h:113
ShellClientSshInitCallback sshInitCallback
SSH initialization callback function.
Definition: shell_client.h:111
error_t shellClientFormatCommand(ShellClientContext *context, const char_t *command,...)
Format a command line.
Definition: shell_client.c:243
SshContext sshContext
SSH context.
Definition: shell_client.h:118
char_t buffer[SHELL_CLIENT_BUFFER_SIZE]
Memory buffer for input/output operations.
Definition: shell_client.h:114
void shellClientDeinit(ShellClientContext *context)
Release shell client context.
Definition: shell_client.c:995
uint8_t flags
Definition: tcp.h:358
ShellClientState
Shell client state.
Definition: shell_client.h:78
unsigned int uint_t
Definition: compiler_port.h:57
error_t shellClientWriteStream(ShellClientContext *context, const void *data, size_t length, size_t *written, uint_t flags)
Write to stdin stream.
Definition: shell_client.c:530
Secure Shell (SSH)
@ SHELL_CLIENT_STATE_DISCONNECTING_2
Definition: shell_client.h:90
#define SshChannel
Definition: ssh.h:939
error_t shellClientFlushStream(ShellClientContext *context)
Flush stdin stream.
Definition: shell_client.c:622
@ SHELL_CLIENT_STATE_CONNECTING_2
Definition: shell_client.h:81