ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
client.h
Go to the documentation of this file.
1// ProtoCore v1.0.16 - Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file client.h
6 * @brief The client engine: drive the handshake, authenticate, and request the port forward.
7 */
8
9#ifndef PROTOCORE_CLIENT_CLIENT_H
10#define PROTOCORE_CLIENT_CLIENT_H
11
13
14EMBED_BEGIN_DECLS
15
16/** @brief Lifecycle phase of the forward, for observability. */
17EMBED_ENUM_BYTE(protocore_ssh_client_state,
18 PROTOCORE_SSH_CLIENT_IDLE = 0, ///< not started.
19 PROTOCORE_SSH_CLIENT_CONNECTING, ///< TCP + SSH handshake + auth in progress.
20 PROTOCORE_SSH_CLIENT_UP, ///< authenticated and the remote forward is established.
21 PROTOCORE_SSH_CLIENT_FAILED ///< the last attempt failed (host-key mismatch, auth, or transport).
22);
23
24#if PROTOCORE_ENABLE_SSH_CLIENT
25
26/** @brief How to reach the relay, who to log in as, and what to forward back. */
27typedef struct
28{
29 const char *host; ///< relay hostname or dotted-quad.
30 uint16_t port; ///< relay SSH port (0 => 22).
31 const char *user; ///< SSH username on the relay.
32 const uint8_t *auth_seed; ///< 32-byte ssh-ed25519 private seed the device authenticates with.
33 const uint8_t *host_pin; ///< 32-byte SHA-256 of the relay's host-key blob (K_S); handshake aborts on mismatch.
34 const char *bind_addr; ///< address the relay binds the forward on ("" / null => "" = all, "localhost", ...).
35 uint16_t
36 bind_port; ///< remote port the relay listens on (tcpip-forward); connections accepted there are forwarded back.
37 uint16_t local_port; ///< local TCP port a forwarded connection is bridged to (e.g. 80).
38} protocore_ssh_client_cfg;
39
40/**
41 * @brief Start (or restart) the forward: connect to the relay, handshake, authenticate, and request
42 * the remote forward. Non-blocking after the initial connect; drive it with poll().
43 * @return true if the connection and handshake started; false on bad args or immediate failure.
44 *
45 * @warning Call begin() and poll() from the SAME task, and give that task enough stack for the
46 * negotiated KEX. The handshake's field arithmetic runs in the caller's task: curve25519/ed25519
47 * peak ~10.5 KB, and the mlkem768x25519 hybrid (PROTOCORE_ENABLE_PQC_KEX) adds ML-KEM-768 for ~16 KB total.
48 * The Arduino loop() task's default 8 KB is NOT enough - run the forward from a dedicated task created
49 * with a >= 20480-byte stack (see the example). begin() claims a private scratch arena for the calling
50 * task, so poll() must run in that same task or the packet-decrypt tripwire fires.
51 */
52
53/**
54 * @brief Pump the forward: advance the handshake, service the relay's keepalives, accept
55 * forwarded-tcpip channels and bridge their bytes to/from the local service. Call every loop,
56 * from the same (adequately-stacked) task that called begin() - see the begin() @warning.
57 */
58
59/** @brief Tear the forward down and close the relay connection. */
60
61/**
62 * @brief The client engine's operations, for the layers that frame messages on its connection.
63 *
64 * @var SshClientNs::send frame one payload as a binary packet and write it to the relay
65 * @var SshClientNs::crypto_work the client slot's handshake scratch, or null when the pool is short
66 * @var SshClientNs::state the forward's lifecycle phase
67 */
68/** @brief The message bytes one send frames as a binary packet. */
69typedef struct
70{
71 const uint8_t *payload; ///< the message bytes a send carries
72 size_t len; ///< how many
73} SshClientMsgArgs;
74
75typedef struct
76{
77 const protocore_ssh_client_cfg *cfg; ///< what a begin dials with
78
79 SshClientMsgArgs msg; ///< the message bytes a send carries
80
81 embed_bool ok; ///< a call's true/false outcome
82 uint8_t *work; ///< the crypto scratch a lookup reports
83 protocore_ssh_client_state state_of; ///< the phase a lookup reports
84
85 void (*const send)(const uint8_t *work);
86 void (*const crypto_work)(const uint8_t *work);
87 void (*const state)(const uint8_t *work);
88 void (*const begin)(const uint8_t *work);
89 void (*const poll)(uint8_t *work);
90 void (*const end)(const uint8_t *work);
91} SshClientNs;
92
93/** @brief The one instance, defined in client.c. */
94extern SshClientNs SshClient;
95
96/**
97 * @brief The PROTOCORE_SSH_CLIENT_BORROW bytes this module's state lives in.
98 *
99 * Stated beside the namespace rather than on it: an entry takes a borrow, and this is where
100 * that borrow comes from. Taken once from the end of the pool, which no mark and no release
101 * walks, so the state lasts the life of the program.
102 *
103 * @return the span.
104 */
105uint8_t *protocore_ssh_client_span(void);
106
107#endif // PROTOCORE_ENABLE_SSH_CLIENT
108
109EMBED_END_DECLS
110
111#endif // PROTOCORE_CLIENT_CLIENT_H
EMBED_BEGIN_DECLS EMBED_ENUM_BYTE(protocore_ssh_client_state, PROTOCORE_SSH_CLIENT_IDLE=0, PROTOCORE_SSH_CLIENT_CONNECTING, PROTOCORE_SSH_CLIENT_UP, PROTOCORE_SSH_CLIENT_FAILED)
Lifecycle phase of the forward, for observability.
Root infrastructure: fixed widths, serializers, opcodes and sizes, for every layer above.