ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
network.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 network.h
6 * @brief Decoupled stream I/O: ring buffer in, framed bytes out, one SSH slot per socket.
7 */
8
9#ifndef PROTOCORE_NETWORK_NETWORK_H
10#define PROTOCORE_NETWORK_NETWORK_H
11
13
14EMBED_BEGIN_DECLS
15
16struct ProtoHandler;
17
18/**
19 * @brief Which transport a slot's byte stream belongs to.
20 *
21 * RFC 4253 sec 4: "SSH works over any 8-bit clean, binary-transparent transport", and sec 4 gives
22 * the two ends different ones - the listening role's stream is a socket this end accepted, the
23 * initiating role's is one it dialled. They are separate pools with separate handle spaces, so the
24 * handle alone does not say which array it indexes.
25 */
26EMBED_ENUM_BYTE(SshStreamKind,
27 SSH_STREAM_ACCEPTED = 0, ///< inbound: the handle indexes conn_pool[]
28 SSH_STREAM_DIALED ///< outbound: the handle indexes the client transport's own pool
29);
30
31/** @brief RFC 4254 sec 5: which pool a handle indexes, and the channel it carries. */
32typedef struct
33{
34 SshStreamKind kind; ///< which pool that handle indexes
35 uint32_t channel; ///< the channel a bridge call names
37
38/** @brief The message bytes an emit or a write carries. */
39typedef struct
40{
41 const uint8_t *payload; ///< the message bytes an emit or a write carries
42 size_t len; ///< how many
43 size_t plen; ///< the payload length already built in the region
45
46/** @brief RFC 4254 sec 7.2: where a bridged channel dials, and the client slot it takes. */
47typedef struct
48{
49 const char *host; ///< the destination a channel dials
50 uint16_t port; ///< its port
51 uint32_t timeout_ms; ///< what that open is given
52 int cid; ///< the client slot a channel adopts, or the one a lookup names
54
55/** @brief Where a channel read writes. */
56typedef struct
57{
58 uint8_t *out; ///< where a channel read writes
59 size_t cap; ///< how much room it has; a region lookup reports its own here
61
62/**
63 * @brief The binding between an SSH slot and the byte stream underneath it.
64 *
65 * RFC 4253 sec 4: "SSH works over any 8-bit clean, binary-transparent transport." Nothing here is
66 * one of the three components of RFC 4251 sec 1. This is the stream they run on, and the roles that
67 * drive them (server sec 4.1, client sec 4) live above it.
68 *
69 * A caller sets the members a call takes, invokes it through ::SshNetwork, and reads the outcome off
70 * the same handle.
71 *
72 * @var SshNetworkNs::claim bind an SSH slot to a stream: the handle and which pool it indexes
73 * @var SshNetworkNs::release mark an SSH slot unowned
74 * @var SshNetworkNs::slot_free lowest unowned SSH slot, or 0xFF when the pool is full
75 * @var SshNetworkNs::owns true when @c ssh_slot is bound to @c conn_slot
76 * @var SshNetworkNs::tx_drain put the packet the codec flagged on the wire, as the window allows
77 * @var SshNetworkNs::emit frame one SSH message and hand it to the slot's worker
78 * @var SshNetworkNs::write_msg frame one built SSH message and write it now
79 * @var SshNetworkNs::payload_region the span a message may be built in for a frame without a copy
80 * @var SshNetworkNs::write_msg_at frame what is already in that span and write it
81 *
82 * @var SshNetworkNs::ssh_slot the SSH slot a call acts on
83 * @var SshNetworkNs::conn_slot the stream slot it is bound to
84 * @var SshNetworkNs::handle the stream handle a claim binds
85 * @var SshNetworkNs::stream which pool that handle indexes, and the channel it carries
86 * @var SshNetworkNs::msg the message bytes an emit or a write carries
87 * @var SshNetworkNs::dial where a bridged channel dials
88 * @var SshNetworkNs::read_args where a channel read writes
89 * @var SshNetworkNs::ok a call's true/false outcome
90 * @var SshNetworkNs::i32 a call's signed outcome
91 * @var SshNetworkNs::u8 the lowest unowned slot, or 0xFF when the pool is full
92 * @var SshNetworkNs::n a byte count a call reports
93 * @var SshNetworkNs::region the span a message may be built in for a frame without a copy
94 */
95
96typedef struct
97{
98 uint8_t ssh_slot; ///< the SSH slot a call acts on
99 uint8_t conn_slot; ///< the stream slot it is bound to
100 int handle; ///< the stream handle a claim binds
101 SshStreamRef stream; ///< which pool that handle indexes, and the channel it carries
102 SshNetMsgArgs msg; ///< the message bytes an emit or a write carries
103 SshChanDialArgs dial; ///< where a bridged channel dials
104 SshChanReadArgs read_args; ///< where a channel read writes
105 embed_bool ok;
106 int i32;
107 uint8_t u8;
108 size_t n;
109 uint8_t *region;
110#if PROTOCORE_ENABLE_TCP_CLIENT
111 // Bridging a channel to a socket of our own needs the client half of the transport, so these
112 // exist exactly when TcpClient does.
113#endif // PROTOCORE_ENABLE_TCP_CLIENT
115
116/** @brief The operands and the outcome. */
118
119/** @brief The entries. */
120typedef struct
121{
122 void (*const claim)(const uint8_t *work);
123 void (*const release)(const uint8_t *work);
124 void (*const slot_free)(const uint8_t *work);
125 void (*const owns)(const uint8_t *work);
126 void (*const tx_drain)(const uint8_t *work);
127 void (*const emit)(const uint8_t *work);
128 void (*const write_msg)(const uint8_t *work);
129 void (*const payload_region)(const uint8_t *work);
130 void (*const write_msg_at)(const uint8_t *work);
131 void (*const chan_open)(uint8_t *work);
132 void (*const chan_adopt)(uint8_t *work);
133 void (*const chan_by_cid)(uint8_t *work);
134 void (*const chan_write)(uint8_t *work);
135 void (*const chan_read)(uint8_t *work);
136 void (*const chan_avail)(uint8_t *work);
137 void (*const chan_drained)(uint8_t *work);
138 void (*const chan_close)(uint8_t *work);
139 void (*const chan_close_all)(uint8_t *work);
141
142// What the table binds, defined once in the .c and taking one parameter each: everything
143// else an entry needs is an operand in SshNetworkV or a region of the borrow at a fixed offset.
144void protocore_ssh_network_claim(const uint8_t *work);
145void protocore_ssh_network_release(const uint8_t *work);
146void protocore_ssh_network_slot_free(const uint8_t *work);
147void protocore_ssh_network_owns(const uint8_t *work);
148void protocore_ssh_network_tx_drain(const uint8_t *work);
149void protocore_ssh_network_emit(const uint8_t *work);
150void protocore_ssh_network_write_msg(const uint8_t *work);
151void protocore_ssh_network_payload_region(const uint8_t *work);
152void protocore_ssh_network_write_msg_at(const uint8_t *work);
162
163// `static const`, initialised HERE rather than `extern` against a definition in the .c: a
164// const object whose initializer every translation unit can see is a COMPILE-TIME FACT, so
165// `SshNetwork.claim(work)` resolves to a named function and becomes a DIRECT call. An extern table
166// leaves the call indirect and the symbol live at every level, -O2 -flto included.
167static const SshNetworkNs SshNetwork __attribute__((unused)) = {
175 .payload_region = protocore_ssh_network_payload_region,
185 .chan_close_all = protocore_ssh_network_chan_close_all,
186};
187
188/**
189 * @brief The PROTOCORE_SSH_NETWORK_BORROW bytes this module's state lives in.
190 *
191 * Stated beside the namespace rather than on it: an entry takes a borrow, and this is where
192 * that borrow comes from. Taken once from the end of the pool, which no mark and no release
193 * walks, so the state lasts the life of the program.
194 *
195 * @return the span.
196 */
198
199/** @brief Put the server identification string on the wire, raw, before any binary packet. */
200void ssh_net_version_exchange_send(uint8_t i, uint8_t conn_slot);
201
202EMBED_END_DECLS
203
204#endif // PROTOCORE_NETWORK_NETWORK_H
Root infrastructure: fixed widths, serializers, opcodes and sizes, for every layer above.
void protocore_ssh_network_owns(const uint8_t *work)
void protocore_ssh_network_chan_avail(uint8_t *work)
void protocore_ssh_network_chan_by_cid(uint8_t *work)
void protocore_ssh_network_chan_write(uint8_t *work)
void protocore_ssh_network_claim(const uint8_t *work)
SshNetworkVars SshNetworkV
The operands and the outcome.
uint8_t * protocore_ssh_network_span(void)
The PROTOCORE_SSH_NETWORK_BORROW bytes this module's state lives in.
void protocore_ssh_network_chan_drained(uint8_t *work)
void protocore_ssh_network_chan_close_all(uint8_t *work)
void protocore_ssh_network_chan_close(uint8_t *work)
void protocore_ssh_network_write_msg(const uint8_t *work)
void ssh_net_version_exchange_send(uint8_t i, uint8_t conn_slot)
Put the server identification string on the wire, raw, before any binary packet.
void protocore_ssh_network_chan_open(uint8_t *work)
void protocore_ssh_network_release(const uint8_t *work)
void protocore_ssh_network_chan_read(uint8_t *work)
void protocore_ssh_network_chan_adopt(uint8_t *work)
EMBED_ENUM_BYTE(SshStreamKind, SSH_STREAM_ACCEPTED=0, SSH_STREAM_DIALED)
Which transport a slot's byte stream belongs to.
void protocore_ssh_network_payload_region(const uint8_t *work)
void protocore_ssh_network_emit(const uint8_t *work)
void protocore_ssh_network_tx_drain(const uint8_t *work)
void protocore_ssh_network_slot_free(const uint8_t *work)
void protocore_ssh_network_write_msg_at(const uint8_t *work)
Per-protocol connection event/poll callbacks (the server's dispatch vtable).
RFC 4254 sec 7.2: where a bridged channel dials, and the client slot it takes.
Definition network.h:48
uint32_t timeout_ms
what that open is given
Definition network.h:51
const char * host
the destination a channel dials
Definition network.h:49
uint16_t port
its port
Definition network.h:50
int cid
the client slot a channel adopts, or the one a lookup names
Definition network.h:52
Where a channel read writes.
Definition network.h:57
size_t cap
how much room it has; a region lookup reports its own here
Definition network.h:59
uint8_t * out
where a channel read writes
Definition network.h:58
The message bytes an emit or a write carries.
Definition network.h:40
size_t len
how many
Definition network.h:42
const uint8_t * payload
the message bytes an emit or a write carries
Definition network.h:41
size_t plen
the payload length already built in the region
Definition network.h:43
The entries.
Definition network.h:121
void(*const chan_open)(uint8_t *work)
Definition network.h:131
void(*const write_msg_at)(const uint8_t *work)
Definition network.h:130
void(*const chan_close)(uint8_t *work)
Definition network.h:138
void(*const chan_close_all)(uint8_t *work)
Definition network.h:139
void(*const chan_read)(uint8_t *work)
Definition network.h:135
void(*const slot_free)(const uint8_t *work)
Definition network.h:124
void(*const chan_write)(uint8_t *work)
Definition network.h:134
void(*const chan_by_cid)(uint8_t *work)
Definition network.h:133
void(*const claim)(const uint8_t *work)
Definition network.h:122
void(*const emit)(const uint8_t *work)
Definition network.h:127
void(*const owns)(const uint8_t *work)
Definition network.h:125
void(*const chan_drained)(uint8_t *work)
Definition network.h:137
void(*const tx_drain)(const uint8_t *work)
Definition network.h:126
void(*const write_msg)(const uint8_t *work)
Definition network.h:128
void(*const chan_avail)(uint8_t *work)
Definition network.h:136
void(*const release)(const uint8_t *work)
Definition network.h:123
void(*const chan_adopt)(uint8_t *work)
Definition network.h:132
void(*const payload_region)(const uint8_t *work)
Definition network.h:129
SshChanDialArgs dial
where a bridged channel dials
Definition network.h:103
SshStreamRef stream
which pool that handle indexes, and the channel it carries
Definition network.h:101
uint8_t * region
Definition network.h:109
uint8_t conn_slot
the stream slot it is bound to
Definition network.h:99
uint8_t u8
Definition network.h:107
embed_bool ok
Definition network.h:105
int handle
the stream handle a claim binds
Definition network.h:100
uint8_t ssh_slot
the SSH slot a call acts on
Definition network.h:98
SshNetMsgArgs msg
the message bytes an emit or a write carries
Definition network.h:102
SshChanReadArgs read_args
where a channel read writes
Definition network.h:104
RFC 4254 sec 5: which pool a handle indexes, and the channel it carries.
Definition network.h:33
SshStreamKind kind
which pool that handle indexes
Definition network.h:34
uint32_t channel
the channel a bridge call names
Definition network.h:35