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.4
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
SSH channel management.
IP network address.
Definition: ip.h:90
SshConnection * sshOpenConnection(SshContext *context, Socket *socket)
Open a new SSH connection.
Definition: ssh_misc.c:66
void socketClose(Socket *socket)
Close an existing socket.
Definition: socket.c:2062
char_t * ipAddrToString(const IpAddr *ipAddr, char_t *str)
Convert a binary IP address to a string representation.
Definition: ip.c:805
SSH transport layer protocol.
#define timeCompare(t1, t2)
Definition: os_port.h:40
void sshServerTick(SshServerContext *context)
Handle periodic operations.
@ SSH_DISCONNECT_BY_APPLICATION
Definition: ssh.h:1006
systime_t timeout
Idle connection timeout.
Definition: ssh_server.h:123
error_t
Error codes.
Definition: error.h:43
void sshServerAcceptConnection(SshServerContext *context)
Accept connection request.
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
SSH server context.
Definition: ssh_server.h:115
void sshCloseConnection(SshConnection *connection)
Close SSH connection.
Definition: ssh_misc.c:172
error_t sshSendDisconnect(SshConnection *connection, uint32_t reasonCode, const char_t *description)
Send SSH_MSG_DISCONNECT message.
#define TRACE_INFO(...)
Definition: debug.h:95
@ SSH_CONN_STATE_CLOSED
Definition: ssh.h:1033
Socket * socketAccept(Socket *socket, IpAddr *clientIpAddr, uint16_t *clientPort)
Permit an incoming connection attempt on a socket.
Definition: socket.c:1451
Helper functions for SSH server.
uint32_t systime_t
System time.
uint32_t time
#define SshConnection
Definition: ssh.h:874
SSH server.
#define Socket
Definition: socket.h:36
SSH helper functions.
SshContext sshContext
SSH context.
Definition: ssh_server.h:124
unsigned int uint_t
Definition: compiler_port.h:50
Secure Shell (SSH)
error_t socketSetTimeout(Socket *socket, systime_t timeout)
Set timeout value for blocking operations.
Definition: socket.c:148
Socket * socket
Listening socket.
Definition: ssh_server.h:121
Debugging facilities.
systime_t osGetSystemTime(void)
Retrieve system time.