ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
ftp_session.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 ftp_session.h
6 * @brief FTP client session driver: the two sockets the ftp.h codec deliberately does not own.
7 *
8 * ftp.h is pure bytes-on-the-wire. This is the other half: it drives a real control connection
9 * (`protocore_client_*`) through the RFC 959 login -> TYPE I -> passive-mode -> transfer -> QUIT
10 * sequence, opens the second (data) connection the server names, and streams a payload across it.
11 *
12 * The payload is **pulled**, not pushed: the caller supplies a `protocore_ftp_source` that fills a chunk at
13 * a given offset. So the bytes can come from anywhere - a file, a sensor log, or the core-dump
14 * partition (`protocore_exc_coredump_read`) - without this owner knowing about any of them, and nothing
15 * ever has to fit in RAM at once.
16 *
17 * Non-blocking: a call advances the sequence as far as the sockets allow, then reports
18 * ::PROTOCORE_FTP_BUSY. A reply is bounded by PROTOCORE_FTP_TIMEOUT_MS.
19 *
20 * @author Douglas Quigg (dstroy0)
21 * @date 2026
22 */
23
24#ifndef PROTOCORE_FTP_SESSION_H
25#define PROTOCORE_FTP_SESSION_H
26
27#include "protocore_config.h" // the entry point: protocore_types.h for the widths
28
29#if PROTOCORE_ENABLE_FTP_SESSION
30
31EMBED_BEGIN_DECLS
32
33// PROTOCORE_FTP_SESSION_BORROW - the bytes this module runs out of - is stated in protocore_config.h, which sums
34// it into its arena. A caller takes them once and passes the pointer to every call. How they
35// are carved is this module's and is never named here.
36
37/** @brief Where a transfer stands. */
38EMBED_ENUM_BYTE(protocore_ftp_state,
39 PROTOCORE_FTP_READY = 0, ///< the server confirmed the completed transfer
40 PROTOCORE_FTP_BUSY, ///< the sockets have no more to give; ask again on the next tick
41 PROTOCORE_FTP_FAILED, ///< the sequence broke, or a reply passed its deadline
42);
43
44/** @brief Where to connect and who to log in as. */
45typedef struct
46{
47 const char *host; ///< server hostname or dotted-quad
48 uint16_t port; ///< control port, or 0 for the default 21
49 const char *user; ///< username, or nullptr for "anonymous"
50 const char *pass; ///< password, or nullptr for "" (anonymous)
51} FtpTarget;
52
53/**
54 * @brief Fill up to @p cap bytes of the payload starting at @p offset.
55 *
56 * Called repeatedly with ascending offsets until the declared total is sent. Returning fewer than
57 * @p cap bytes ends the transfer early and fails it, so a source that cannot satisfy a chunk should
58 * return 0 rather than pad.
59 *
60 * @return bytes written into @p buf.
61 */
62typedef size_t (*protocore_ftp_source)(void *ctx, size_t offset, uint8_t *buf, size_t cap);
63
64/** @brief What store takes: target, remote_path, total, ... */
65typedef struct
66{
67 const FtpTarget *target;
68 const char *remote_path;
69 size_t total;
70 protocore_ftp_source src;
71 void *ctx;
72} FtpSessionStoreArgs;
73
74/**
75 * @brief FTP client session driver: the two sockets the ftp.h codec deliberately does not own. ftp.h is pure
76 * bytes-on-the-wire.
77 *
78 * A caller sets the members a call takes, invokes it through ::FtpSession with the bytes it runs
79 * out of, and reads the outcome off the same handle.
80 *
81 * FtpSession.store_args.target = ...;
82 * FtpSession.store_args.remote_path = ...;
83 * FtpSession.store_args.total = ...;
84 * FtpSession.store_args.src = ...;
85 * FtpSession.store_args.ctx = ...;
86 * FtpSession.store(work);
87 * // FtpSession.value is what the call reports
88 *
89 * @var FtpSessionNs::store_args what store takes: target, remote_path, total,
90 * @var FtpSessionNs::ok a call's true/false outcome
91 * @var FtpSessionNs::value ::PROTOCORE_FTP_READY only if the server confirmed the completed ...
92 * @var FtpSessionNs::store upload total bytes pulled from src to remote_path (RFC 959 STOR). ...
93 *
94 * @c work is PROTOCORE_FTP_SESSION_BORROW bytes the CALLER took, at an address it knows. It is not held past the call,
95 * so nothing here aliases it. How those bytes are carved is this module's and is never named here.
96 */
97typedef struct
98{
99 FtpSessionStoreArgs store_args;
100 embed_bool ok;
101 protocore_ftp_state value;
102} FtpSessionVars;
103
104/** @brief The operands and the outcome. */
105extern FtpSessionVars FtpSessionV;
106
107/** @brief The entries. */
108typedef struct
109{
110 void (*const store)(uint8_t *work);
111} FtpSessionNs;
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 FtpSessionV or a region of the borrow at a fixed offset.
115void protocore_ftp_session_store(uint8_t *work);
116
117// `static const`, initialised HERE rather than `extern` against a definition in the .c: a
118// const object whose initializer every translation unit can see is a COMPILE-TIME FACT, so
119// `FtpSession.store(work)` resolves to a named function and becomes a DIRECT call. An extern table
120// leaves the call indirect and the symbol live at every level, -O2 -flto included.
121static const FtpSessionNs FtpSession __attribute__((unused)) = {
122 .store = protocore_ftp_session_store,
123};
124
125/**
126 * @brief The PROTOCORE_FTP_SESSION_BORROW bytes this module's state lives in.
127 *
128 * Stated beside the namespace rather than on it: an entry takes a borrow, and this is where
129 * that borrow comes from. Taken once from the end of the pool, which no mark and no release
130 * walks, so the state lasts the life of the program.
131 *
132 * @return the span.
133 */
134uint8_t *protocore_ftp_session_span(void);
135
136EMBED_END_DECLS
137
138#endif // PROTOCORE_ENABLE_FTP_SESSION
139
140#endif // PROTOCORE_FTP_SESSION_H
EMBED_ENUM_BYTE(ProtoConn, PROTO_NONE=0, PROTO_HTTP=1, PROTO_TELNET=2, PROTO_SSH=3, PROTO_MODBUS=4, PROTO_OPCUA=5, PROTO_SSH_RFWD=6, PROTO_RELAY=7, PROTO_BRIDGE=8, PROTO_NTRIP_CASTER=9, PROTO_MESH=10, PROTO_UDP=11,)
Application protocol spoken on a listener port or connection slot.