ssh_server_misc.c
Go to the documentation of this file.
1 /**
2  * @file ssh_server_misc.c
3  * @brief Helper functions for SSH server
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.0
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL SSH_TRACE_LEVEL
33 
34 //Dependencies
35 #include "ssh/ssh.h"
36 #include "ssh/ssh_server.h"
37 #include "ssh/ssh_server_misc.h"
38 #include "ssh/ssh_transport.h"
39 #include "ssh/ssh_channel.h"
40 #include "ssh/ssh_misc.h"
41 #include "debug.h"
42 
43 //Check SSH stack configuration
44 #if (SSH_SUPPORT == ENABLED && SSH_SERVER_SUPPORT == ENABLED)
45 
46 
47 /**
48  * @brief Handle periodic operations
49  * @param[in] context Pointer to the SSH server context
50  **/
51 
53 {
54  error_t error;
55  uint_t i;
57  SshConnection *connection;
58 
59  //Get current time
61 
62  //Loop through the connection table
63  for(i = 0; i < context->sshContext.numConnections; i++)
64  {
65  //Point to the current entry
66  connection = &context->sshContext.connections[i];
67 
68  //Active connection?
69  if(connection->state != SSH_CONN_STATE_CLOSED)
70  {
71  //Check idle connection timeout (a value of zero means no timeout)
72  if(context->timeout != 0)
73  {
74  //Disconnect inactive client after idle timeout
75  if(timeCompare(time, connection->timestamp + context->timeout) >= 0)
76  {
77  //Debug message
78  TRACE_INFO("SSH server: Closing inactive connection...\r\n");
79 
80  //Send an SSH_MSG_DISCONNECT message
82  "Session idle timeout");
83 
84  //Failed to send message?
85  if(error)
86  {
87  //Close the SSH connection
88  sshCloseConnection(connection);
89  }
90  }
91  }
92  }
93  }
94 }
95 
96 
97 /**
98  * @brief Accept connection request
99  * @param[in] context Pointer to the SSH server context
100  **/
101 
103 {
104  Socket *socket;
105  IpAddr clientIpAddr;
106  uint16_t clientPort;
107  SshConnection *connection;
108 
109  //Accept incoming connection
110  socket = socketAccept(context->socket, &clientIpAddr, &clientPort);
111 
112  //Make sure the socket handle is valid
113  if(socket != NULL)
114  {
115  //Allocate a new SSH connection
116  connection = sshOpenConnection(&context->sshContext, socket);
117 
118  //If the connection table runs out of space, then the client's connection
119  //request is rejected
120  if(connection != NULL)
121  {
122  //Debug message
123  TRACE_INFO("SSH server: Connection established with client %s port %"
124  PRIu16 "...\r\n", ipAddrToString(&clientIpAddr, NULL), clientPort);
125 
126  //Force the socket to operate in non-blocking mode
128  }
129  else
130  {
131  //Debug message
132  TRACE_INFO("SSH Server: Connection refused with client %s port %"
133  PRIu16 "...\r\n", ipAddrToString(&clientIpAddr, NULL), clientPort);
134 
135  //The SSH server cannot accept the incoming connection request
137  }
138  }
139 }
140 
141 #endif
int_t socket(int_t family, int_t type, int_t protocol)
Create a socket that is bound to a specific transport service provider.
Definition: bsd_socket.c:65
unsigned int uint_t
Definition: compiler_port.h:50
Debugging facilities.
#define TRACE_INFO(...)
Definition: debug.h:95
uint32_t time
error_t
Error codes.
Definition: error.h:43
char_t * ipAddrToString(const IpAddr *ipAddr, char_t *str)
Convert a binary IP address to a string representation.
Definition: ip.c:838
#define timeCompare(t1, t2)
Definition: os_port.h:40
systime_t osGetSystemTime(void)
Retrieve system time.
uint32_t systime_t
System time.
Socket * socketAccept(Socket *socket, IpAddr *clientIpAddr, uint16_t *clientPort)
Permit an incoming connection attempt on a socket.
Definition: socket.c:912
error_t socketSetTimeout(Socket *socket, systime_t timeout)
Set timeout value for blocking operations.
Definition: socket.c:148
void socketClose(Socket *socket)
Close an existing socket.
Definition: socket.c:1517
#define Socket
Definition: socket.h:36
Secure Shell (SSH)
@ SSH_CONN_STATE_CLOSED
Definition: ssh.h:1042
#define SshConnection
Definition: ssh.h:883
@ SSH_DISCONNECT_BY_APPLICATION
Definition: ssh.h:1015
SSH channel management.
SshConnection * sshOpenConnection(SshContext *context, Socket *socket)
Open a new SSH connection.
Definition: ssh_misc.c:66
void sshCloseConnection(SshConnection *connection)
Close SSH connection.
Definition: ssh_misc.c:172
SSH helper functions.
SSH server.
void sshServerTick(SshServerContext *context)
Handle periodic operations.
void sshServerAcceptConnection(SshServerContext *context)
Accept connection request.
Helper functions for SSH server.
error_t sshSendDisconnect(SshConnection *connection, uint32_t reasonCode, const char_t *description)
Send SSH_MSG_DISCONNECT message.
SSH transport layer protocol.
IP network address.
Definition: ip.h:79
SSH server context.
Definition: ssh_server.h:115
SshContext sshContext
SSH context.
Definition: ssh_server.h:124
systime_t timeout
Idle connection timeout.
Definition: ssh_server.h:123
Socket * socket
Listening socket.
Definition: ssh_server.h:121