DeterministicESPAsyncWebServer v6.27.1
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
ssh_conn.h
Go to the documentation of this file.
1// Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file ssh_conn.h
6 * @brief Glue between the TCP transport (conn_pool) and the SSH protocol stack.
7 *
8 * Binds a ConnProto::PROTO_SSH TcpConn slot to an SSH session slot, pumps ring-buffer
9 * bytes through the banner exchange and binary-packet layer, and writes the
10 * dispatcher's outbound packets back to the socket. This is the integration
11 * layer the session loop calls for ConnProto::PROTO_SSH connections.
12 */
13
14#ifndef DETERMINISTICESPASYNCWEBSERVER_SSH_CONN_H
15#define DETERMINISTICESPASYNCWEBSERVER_SSH_CONN_H
16
17#include <stddef.h>
18#include <stdint.h>
19
20/**
21 * @brief One-time setup: install the dispatcher's binary-packet emit callback.
22 *
23 * Called from ssh_proto_handler() (the accessor every consumer uses to install SSH),
24 * so registering the handler always wires the emit callback - it can never be
25 * forgotten. Idempotent. Until it runs, the dispatcher's emit callback is null and
26 * every framed SSH packet after the plaintext banner is silently dropped.
27 */
28void ssh_conn_setup();
29
30/** @brief The SSH connection ProtoHandler (accessor; installed by the builtins list, no session dep). */
31struct ProtoHandler;
32const struct ProtoHandler *ssh_proto_handler(void);
33
34/**
35 * @brief Handle a new ConnProto::PROTO_SSH connection on @p conn_slot.
36 *
37 * Allocates an SSH session slot, initializes the transport/packet/channel
38 * state, and sends the server identification banner. If no SSH slot is free
39 * the connection is aborted.
40 */
41void ssh_conn_accept(uint8_t conn_slot);
42
43/**
44 * @brief Drain @p conn_slot's receive ring buffer through the SSH stack.
45 *
46 * Feeds the banner parser until the client identification string completes,
47 * then the binary-packet layer; complete messages are dispatched and any
48 * responses are written to the socket. Closes the connection if the protocol
49 * signals a fatal condition.
50 */
51void ssh_conn_rx(uint8_t conn_slot);
52
53/**
54 * @brief Tear down SSH state for @p conn_slot (disconnect / error).
55 */
56void ssh_conn_close(uint8_t conn_slot);
57
58/**
59 * @brief Send application data to the client over an SSH channel.
60 *
61 * Frames @p data as SSH_MSG_CHANNEL_DATA on channel @p channel, encrypts+MACs it,
62 * and writes it to the socket. @p ssh_slot and @p channel are the values passed to
63 * the data callback registered via ssh_channel_set_data_cb(). A single call sends
64 * at most one channel-data message (bounded by the peer's flow-control window).
65 *
66 * @return Number of bytes sent, or -1 on error (bad slot, channel closed/unknown,
67 * peer window/packet limit, or no active connection).
68 */
69int ssh_conn_send(uint8_t ssh_slot, uint32_t channel, const uint8_t *data, size_t len);
70
71/**
72 * @brief Close an SSH channel from the server side: frame CHANNEL_EOF and
73 * CHANNEL_CLOSE as two binary packets and write them to the socket.
74 *
75 * Used by the port-forwarding owner when the forwarded TCP peer closes.
76 * @return 0 on success, -1 on error (bad slot, channel closed/unknown, no
77 * active connection, or scratch exhausted).
78 */
79int ssh_conn_close_channel(uint8_t ssh_slot, uint32_t channel);
80
81/**
82 * @brief Open a server-initiated "forwarded-tcpip" channel to the client (ssh -R):
83 * build the CHANNEL_OPEN (RFC 4254 §7.2) via the channel codec, frame + send it
84 * on @p ssh_slot's socket, and return the new local channel id. The client's
85 * CHANNEL_OPEN_CONFIRMATION (or FAILURE) later drives the forward-confirm callback.
86 *
87 * @param conn_addr / conn_port the forward's bound address/port (address connected).
88 * @param orig_addr / orig_port the peer that connected to the forwarded port (advisory).
89 * @return the local channel id (>= 0), or -1 (no active connection, channel pool full,
90 * or scratch exhausted). Used by the remote-forward owner.
91 */
92int ssh_conn_open_forwarded(uint8_t ssh_slot, const char *conn_addr, uint16_t conn_port, const char *orig_addr,
93 uint16_t orig_port);
94
95/**
96 * @brief Per-loop poll hook for an SSH connection (registered as the SSH protocol
97 * handler's on_poll). Drives the port-forwarding pump; a no-op when
98 * forwarding is compiled out.
99 */
100void ssh_conn_poll(uint8_t conn_slot);
101
102#endif // DETERMINISTICESPASYNCWEBSERVER_SSH_CONN_H
const struct ProtoHandler * ssh_proto_handler(void)
Definition ssh_conn.cpp:86
int ssh_conn_close_channel(uint8_t ssh_slot, uint32_t channel)
Close an SSH channel from the server side: frame CHANNEL_EOF and CHANNEL_CLOSE as two binary packets ...
Definition ssh_conn.cpp:126
int ssh_conn_open_forwarded(uint8_t ssh_slot, const char *conn_addr, uint16_t conn_port, const char *orig_addr, uint16_t orig_port)
Open a server-initiated "forwarded-tcpip" channel to the client (ssh -R): build the CHANNEL_OPEN (RFC...
Definition ssh_conn.cpp:158
void ssh_conn_rx(uint8_t conn_slot)
Drain conn_slot's receive ring buffer through the SSH stack.
Definition ssh_conn.cpp:273
void ssh_conn_accept(uint8_t conn_slot)
Handle a new ConnProto::PROTO_SSH connection on conn_slot.
Definition ssh_conn.cpp:226
void ssh_conn_poll(uint8_t conn_slot)
Per-loop poll hook for an SSH connection (registered as the SSH protocol handler's on_poll)....
Definition ssh_conn.cpp:188
int ssh_conn_send(uint8_t ssh_slot, uint32_t channel, const uint8_t *data, size_t len)
Send application data to the client over an SSH channel.
Definition ssh_conn.cpp:97
void ssh_conn_setup()
One-time setup: install the dispatcher's binary-packet emit callback.
Definition ssh_conn.cpp:77
void ssh_conn_close(uint8_t conn_slot)
Tear down SSH state for conn_slot (disconnect / error).
Definition ssh_conn.cpp:311
Per-protocol connection event/poll callbacks (Layer 5 dispatch vtable).