ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
scp.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#ifndef PROTOCORE_SCP_H
5#define PROTOCORE_SCP_H
6
7#include "protocore_config.h" // the entry point: protocore_types.h for the widths
8
9EMBED_BEGIN_DECLS
10
11/**
12 * @file scp.h
13 * @brief SCP (RCP) protocol wire codec - the pure, host-testable half of the SCP-over-SSH server
14(PROTOCORE_ENABLE_SSH_SCP).
15 *
16 * SCP transfers a file over an SSH `exec "scp …"` channel using the old rcp line protocol: the source side
17 * sends a control line `C<mode> <size> <name>\n`, the peer acks with a 0 byte, then the file bytes flow,
18 * ended by a 0 byte and another ack. This file parses/builds the command line and the control line and knows
19 * the ack bytes - no filesystem, no SSH, no Arduino, zero heap. The fs::FS sink/source state machine + the
20 * channel glue live in network_drivers/session/scp/ssh_scp.
21 *
22 * @c work is bytes the CALLER holds. This module reads none of them: it carries nothing
23 * between calls, so there is no state to keep and nothing to wipe. The parameter is there so
24 * a caller drives every namespace the same way.
25 *
26 * @author Douglas Quigg (dstroy0)
27 * @date 2026
28 */
29
30// PROTOCORE_SCP_BORROW - the bytes this module runs out of - is stated in protocore_config.h, which sums
31// it into its arena. Its size and its offset are each a static_assert, so a feature
32// combination that does not fit fails to compile rather than overrunning at run time.
33
34// rcp acknowledgement bytes (sent between records).
35#define PROTOCORE_SCP_ACK_OK 0 ///< proceed
36#define PROTOCORE_SCP_ACK_WARN 1 ///< warning: followed by a message + '\n'
37#define PROTOCORE_SCP_ACK_ERROR 2 ///< fatal error: followed by a message + '\n'
38
39/** @brief The role of an `scp` invocation, parsed from the exec command. */
40EMBED_ENUM_BYTE(ScpMode, SCP_MODE_INVALID = 0,
41 SCP_MODE_SINK, ///< `scp -t <path>`: the client sends a file TO the device (device receives)
42 SCP_MODE_SOURCE ///< `scp -f <path>`: the client fetches a file FROM the device (device sends)
43);
44
45/** @brief Dispatch table. Addressed by offset, so the layout is asserted below. */
46typedef struct
47{
48 ScpMode (*parse_cmd)(const uint8_t *, const char *, size_t, char *, size_t);
49 embed_bool (*parse_cline)(const uint8_t *, const char *, size_t, uint32_t *, uint64_t *, char *, size_t);
50 size_t (*build_cline)(const uint8_t *, uint32_t, uint64_t, const char *, char *, size_t);
51} ScpNs;
52PROTOCORE_NS_LAYOUT(ScpNs, parse_cmd, parse_cline, build_cline);
53
54/**
55 * @brief Parse an exec command `scp [-v] [-r] [-p] [-d] -t|-f <path>` into .
56 * @param work PROTOCORE_SCP_BORROW bytes the caller took. Not held past the call.
57 * @param cmd not NUL-terminated (cmd_len bytes). the mode; path_out gets the (NUL-terminated) target,
58 * @param cmd_len Cmd len
59 * @param path_out Path out
60 * @param path_cap Path cap
61 * @return The ScpMode.
62 */
63ScpMode protocore_scp_parse_cmd(const uint8_t *work, const char *cmd, size_t cmd_len, char *path_out, size_t path_cap);
64/**
65 * @brief Parse a control line `C<mode> <size> <name>` (a trailing '\n' .
66 * @param work PROTOCORE_SCP_BORROW bytes the caller took. Not held past the call.
67 * @param line Line
68 * @param len Len
69 * @param mode_out Mode out
70 * @param size_out Size out
71 * @param name_out Name out
72 * @param name_cap Name cap
73 * @return EMBED_TRUE on success.
74 */
75embed_bool protocore_scp_parse_cline(const uint8_t *work, const char *line, size_t len, uint32_t *mode_out,
76 uint64_t *size_out, char *name_out, size_t name_cap);
77/**
78 * @brief Build a control line `C<mode> <size> <name>\n` for a source .
79 * @param work PROTOCORE_SCP_BORROW bytes the caller took. Not held past the call.
80 * @param mode Mode
81 * @param size Size
82 * @param name Name
83 * @param out Out
84 * @param cap Cap
85 * @return The size_t.
86 */
87size_t protocore_scp_build_cline(const uint8_t *work, uint32_t mode, uint64_t size, const char *name, char *out,
88 size_t cap);
89
90/** @brief Module namespace. */
94
95EMBED_END_DECLS
96
97#endif // PROTOCORE_SCP_H
#define PROTOCORE_UNUSED
Suppress the unused warning. Every module ends in a namespace most callers only partly use.
Definition ns_contract.h:36
#define PROTOCORE_NS_LAYOUT(T,...)
Pin every dispatch slot of a table that is nothing but function pointers.
#define PROTOCORE_NS
Storage for a dispatch table. The const is load bearing.
EMBED_ENUM_BYTE(ScpMode, SCP_MODE_INVALID=0, SCP_MODE_SINK, SCP_MODE_SOURCE)
The role of an scp invocation, parsed from the exec command.
embed_bool protocore_scp_parse_cline(const uint8_t *work, const char *line, size_t len, uint32_t *mode_out, uint64_t *size_out, char *name_out, size_t name_cap)
Parse a control line C<mode> <size> <name> (a trailing ' ' .
ScpMode protocore_scp_parse_cmd(const uint8_t *work, const char *cmd, size_t cmd_len, char *path_out, size_t path_cap)
Parse an exec command scp [-v] [-r] [-p] [-d] -t|-f <path> into .
size_t protocore_scp_build_cline(const uint8_t *work, uint32_t mode, uint64_t size, const char *name, char *out, size_t cap)
Build a control line C<mode> <size> <name>\n for a source .
Dispatch table. Addressed by offset, so the layout is asserted below.
Definition scp.h:47
embed_bool(* parse_cline)(const uint8_t *, const char *, size_t, uint32_t *, uint64_t *, char *, size_t)
Definition scp.h:49
size_t(* build_cline)(const uint8_t *, uint32_t, uint64_t, const char *, char *, size_t)
Definition scp.h:50
ScpMode(* parse_cmd)(const uint8_t *, const char *, size_t, char *, size_t)
Definition scp.h:48