ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
telnet.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 telnet.h
6 * @brief Layer 6/7 - minimal RFC 854 Telnet server (PROTOCORE_ENABLE_TELNET).
7 *
8 * A zero-heap line-oriented Telnet console dispatched from the session layer's
9 * ProtoConn::PROTO_TELNET arms (the same way SSH is dispatched to ssh_conn). On connect it
10 * negotiates server-side echo + suppress-go-ahead (so the client runs in
11 * character mode and the server draws the line), accumulates a line, echoes
12 * keystrokes (with backspace handling), and hands each completed line to a
13 * command callback. Output can be pushed to all connected clients.
14 *
15 * Telnet is plaintext - no authentication or encryption. Use it only on a
16 * trusted network; prefer SSH or the WebSocket terminal otherwise.
17 *
18 * Usage:
19 * @code
20 * server.listen(23, ProtoConn::PROTO_TELNET); // open the Telnet port
21 * protocore_telnet_on_command(my_cmd_handler); // void(const char *line, uint8_t id)
22 * @endcode
23 */
24
25#ifndef PROTOCORE_TELNET_H
26#define PROTOCORE_TELNET_H
27
28#include "numeros_scribo/numeros_scribo.h"
29#include "protocore_config.h"
30
31EMBED_BEGIN_DECLS
32
33#if PROTOCORE_ENABLE_TELNET
34
35/** @brief Called with each completed input line (NUL-terminated, no CR/LF) and its client id. */
36typedef void (*TelnetCommandCb)(const char *line, uint8_t conn_id);
37
38struct ProtoHandler;
39
40/** @brief RFC 854 NVT data: what a write puts on the terminal, as a line or as a field set. */
41typedef struct
42{
43 const char *text; ///< a line of NVT ASCII
44 const mmgr_field *spec; ///< the field layout a frame is built from
45 const mmgr_fval *val; ///< the values that fill it
46 size_t nv; ///< how many
47} TelnetOutArgs;
48
49/**
50 * @brief The console an application drives, and the three arms the session layer turns.
51 *
52 * The first five are the application's; the three after them are called for a
53 * ProtoConn::PROTO_TELNET slot and are not an application's business.
54 *
55 * A caller sets the members a call takes, invokes it through ::Telnet, and reads the outcome off
56 * the same handle.
57 *
58 * @var TelnetNs::slot the connection a call acts on
59 * @var TelnetNs::cb the per-line command handler an install registers
60 * @var TelnetNs::out what a write puts on the NVT: a line, or a field set
61 * @var TelnetNs::u8 a call's 8-bit outcome
62 * @var TelnetNs::handler the ProtoHandler a lookup reports
63 * @var TelnetNs::on_command register the per-line command handler
64 * @var TelnetNs::print text to every connected client, no trailing newline added
65 * @var TelnetNs::println text + CRLF to every connected client
66 * @var TelnetNs::frame build @c out.spec and broadcast it. The shape is a `static const
67 * mmgr_field[]` the caller declares, so a console line costs a table
68 * walk rather than a format-string parse, and one longer than
69 * TELNET_BUF_SIZE is dropped rather than clipped mid-word
70 * @var TelnetNs::client_count connected clients
71 * @var TelnetNs::accept a connection was accepted on TCP slot @c slot
72 * @var TelnetNs::rx process the received bytes for the connection on @c slot
73 * @var TelnetNs::close the connection on @c slot closed; release its state
74 * @var TelnetNs::proto_handler the ProtoHandler the builtins list installs, which is what keeps
75 * this module free of a dependency on the session layer
76 *
77 * @code
78 * static const mmgr_field HEAP[] = {{MMGR_FK_LIT, 0, 11, "free heap: "}, MMGR_U32,
79 * {MMGR_FK_LIT, 0, 8, " bytes\r\n"}, MMGR_END};
80 * Telnet.out.spec = HEAP;
81 * Telnet.out.val = (const mmgr_fval[]){MMGR_VU32(free_heap_bytes())};
82 * Telnet.out.nv = 1;
83 * Telnet.frame(Telnet.internal);
84 * @endcode
85 */
86
87typedef struct
88{
89 uint8_t slot; ///< the NVT every call names
90 TelnetCommandCb cb; ///< what a received command line is dispatched to
91 TelnetOutArgs out; ///< what a write puts on the NVT
92 uint8_t u8;
93 const struct ProtoHandler *handler;
94} TelnetVars;
95
96/** @brief The operands and the outcome. */
97extern TelnetVars TelnetV;
98
99/** @brief The entries. */
100typedef struct
101{
102 void (*const on_command)(uint8_t *work);
103 void (*const print)(uint8_t *work);
104 void (*const println)(uint8_t *work);
105 void (*const frame)(uint8_t *work);
106 void (*const client_count)(uint8_t *work);
107 void (*const accept)(uint8_t *work);
108 void (*const rx)(uint8_t *work);
109 void (*const close)(uint8_t *work);
110 void (*const proto_handler)(const uint8_t *work);
111} TelnetNs;
112
113// What the table binds, defined once in the .c and taking one parameter each: everything
114// else an entry needs is an operand in TelnetV or a region of the borrow at a fixed offset.
115void protocore_telnet_on_command(uint8_t *work);
116void protocore_telnet_print(uint8_t *work);
117void protocore_telnet_println(uint8_t *work);
118void protocore_telnet_frame(uint8_t *work);
119void protocore_telnet_client_count(uint8_t *work);
120void protocore_telnet_accept(uint8_t *work);
121void protocore_telnet_rx(uint8_t *work);
122void protocore_telnet_close(uint8_t *work);
123void protocore_telnet_proto_handler(const uint8_t *work);
124
125// `static const`, initialised HERE rather than `extern` against a definition in the .c: a
126// const object whose initializer every translation unit can see is a COMPILE-TIME FACT, so
127// `Telnet.on_command(work)` resolves to a named function and becomes a DIRECT call. An extern table
128// leaves the call indirect and the symbol live at every level, -O2 -flto included.
129static const TelnetNs Telnet __attribute__((unused)) = {
130 .on_command = protocore_telnet_on_command,
131 .print = protocore_telnet_print,
132 .println = protocore_telnet_println,
133 .frame = protocore_telnet_frame,
134 .client_count = protocore_telnet_client_count,
135 .accept = protocore_telnet_accept,
136 .rx = protocore_telnet_rx,
137 .close = protocore_telnet_close,
138 .proto_handler = protocore_telnet_proto_handler,
139};
140
141/**
142 * @brief The PROTOCORE_TELNET_BORROW bytes this module's state lives in.
143 *
144 * Stated beside the namespace rather than on it: an entry takes a borrow, and this is where
145 * that borrow comes from. Taken once from the end of the pool, which no mark and no release
146 * walks, so the state lasts the life of the program.
147 *
148 * @return the span.
149 */
150uint8_t *protocore_telnet_span(void);
151
152#endif // PROTOCORE_ENABLE_TELNET
153
154EMBED_END_DECLS
155
156#endif // PROTOCORE_TELNET_H
Per-protocol connection event/poll callbacks (the server's dispatch vtable).