ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
smb_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#ifndef PROTOCORE_SMB_CLIENT_H
5#define PROTOCORE_SMB_CLIENT_H
6
7#include "network_drivers/application/smb/smb2/smb2.h" // the complete type a public struct below holds by value
8#include "protocore_config.h" // the entry point: protocore_types.h for the widths
9
10EMBED_BEGIN_DECLS
11
12/**
13 * @file smb_client.h
14 * @brief SMB2 client dialogue engine (PROTOCORE_ENABLE_SMB) - drives the smb2 / ntlm / spnego wire
15codecs through a real session to open a file on a Windows share.
16 *
17 * The wire codecs (smb2.h, ntlm.h, ntlmssp.h, spnego.h) are pure builders/parsers; this ties them
18 * into the actual exchange: NEGOTIATE, the two-round NTLMv2 SESSION_SETUP (SPNEGO-wrapped),
19 * TREE_CONNECT to `\\server\share`, and CREATE to open the file - handing back a handle that
20 * smb_read / smb_write / smb_close use. Like the SMTP engine it is written against a send/recv seam,
21 * so the whole exchange is host-tested with a scripted mock SMB2 server (no lwIP / real share).
22 *
23 * Direct-TCP framing (the 4-byte length prefix) is handled here: each request is framed before
24 * `send`, each response is de-framed after `recv` (accumulating until a full message arrives).
25 *
26 * @c work is PROTOCORE_SMB_CLIENT_BORROW bytes the CALLER took, at an address it knows. It is not held past the call,
27so nothing here aliases it. How those bytes are
28 * carved is this module's and is never named here.
29 *
30 * @author Douglas Quigg (dstroy0)
31 * @date 2026
32 */
33
34/** @brief Result of an SMB client operation. 0 is success; each failure is a distinct code. */
35EMBED_ENUM_BYTE(SmbResult, SMB_OK = 0,
36 SMB_ERR_ARG = -1, ///< a required field was null/empty
37 SMB_ERR_IO = -2, ///< a send/recv failed, timed out, or the peer closed mid-message
38 SMB_ERR_PROTOCOL = -3, ///< a malformed response, or an unexpected NT status
39 SMB_ERR_AUTH = -4, ///< SESSION_SETUP was rejected (bad user/password/domain)
40 SMB_ERR_OVERFLOW = -5, ///< a message did not fit the work buffer (PROTOCORE_SMB_BUF)
41);
42
43/**
44 * @brief Transport seam: the engine moves raw bytes only through these, so it runs against a real
45 * socket (protocore_client) or a test mock.
46 * @return send: bytes written (must equal @p len), else < 0. recv: bytes read (> 0), else <= 0 on
47 * close / error / timeout.
48 */
49typedef int (*SmbSendFn)(void *ctx, const uint8_t *data, size_t len);
50
51typedef int (*SmbRecvFn)(void *ctx, uint8_t *buf, size_t cap);
52
53/** @brief Server credentials + the file to open. Strings are ASCII/UTF-8 (encoded UTF-16LE for you). */
54typedef struct
55{
56 const char *user; ///< account name
57 const char *pass; ///< password
58 const char *domain; ///< NTLM domain (null/empty for a local account)
59 const char *workstation; ///< client name to announce (null => none)
60 const char *share; ///< the tree path, UNC `\\server\share`
61 const char *path; ///< file name relative to the share root (e.g. `PROGRAMS\A.NC`)
62 uint32_t desired_access; ///< SMB2_FILE_GENERIC_READ and/or _WRITE
63 uint32_t disposition; ///< SMB2_FILE_OPEN / _OPEN_IF / _OVERWRITE_IF / _CREATE
64 embed_bool encrypt; ///< request SMB 3.x transport encryption from the session on (client-forced, like
65 ///< smbclient -e): needed to reach a share whose server requires encryption, which
66 ///< rejects the unencrypted TREE_CONNECT before it can advertise the share flag.
67 uint16_t cipher_pref; ///< preferred Smb2Cipher to negotiate (moved to the front of the offer); 0 = default
68 ///< order (AES-128-GCM, AES-256-GCM, AES-128-CCM, AES-256-CCM).
69} SmbConfig;
70
71/** @brief An open file on an authenticated session; the ids thread the follow-up requests. */
72typedef struct
73{
74 uint64_t session_id;
75 uint32_t tree_id;
76 uint8_t file_id[16];
77 uint64_t file_size; ///< EndofFile from CREATE (the current size)
78 uint64_t next_message_id; ///< the MessageId for the next request on this handle
79 embed_bool signing_active; ///< the session negotiated SMB signing (server set SigningRequired, not guest/null)
80 Smb2SignAlgo signing_algo; ///< HMAC-SHA256 (SMB 2.x) or AES-CMAC (SMB 3.x), from the negotiated dialect
81 uint8_t signing_key[16]; ///< the session signing key when @ref signing_active (2.x: NTLMv2 key; 3.x: KDF-derived)
82 embed_bool encrypt_active; ///< SMB 3.x transport encryption is in force (server session or share required it)
83 uint16_t enc_cipher; ///< negotiated Smb2Cipher id (selects the key + nonce length) when @ref encrypt_active
84 uint8_t enc_c2s[PROTOCORE_SMB2_MAX_CIPHER_KEY_LEN]; ///< client->server cipher key (encrypts requests)
85 uint8_t enc_s2c[PROTOCORE_SMB2_MAX_CIPHER_KEY_LEN]; ///< server->client cipher key (decrypts responses)
86 uint64_t enc_nonce; ///< monotonic per-session AEAD nonce counter, persisted across read/write/close
87} SmbHandle;
88
89/** @brief Dispatch table. Addressed by offset, so the layout is asserted below. */
90typedef struct
91{
92 SmbResult (*smb_open)(uint8_t *, const SmbConfig *, SmbHandle *, SmbSendFn, SmbRecvFn, void *);
93 SmbResult (*smb_close)(uint8_t *, SmbHandle *, SmbSendFn, SmbRecvFn, void *);
94 SmbResult (*smb_read)(uint8_t *, SmbHandle *, uint64_t, uint8_t *, size_t, size_t *, SmbSendFn, SmbRecvFn, void *);
95 SmbResult (*smb_write)(uint8_t *, SmbHandle *, uint64_t, const uint8_t *, size_t, size_t *, SmbSendFn, SmbRecvFn,
96 void *);
98PROTOCORE_NS_LAYOUT(SmbClientNs, smb_open, smb_close, smb_read, smb_write);
99
100/**
101 * @brief Run NEGOTIATE -> NTLMv2 SESSION_SETUP -> TREE_CONNECT -> CREATE and .
102 * @param work PROTOCORE_SMB_CLIENT_BORROW bytes the caller took. Not held past the call.
103 * @param cfg Cfg
104 * @param h H
105 * @param send Send
106 * @param recv Recv
107 * @param ctx Ctx
108 * @return The SmbResult.
109 */
110SmbResult protocore_smb_client_smb_open(uint8_t *work, const SmbConfig *cfg, SmbHandle *h, SmbSendFn send,
111 SmbRecvFn recv, void *ctx);
112/**
113 * @brief CLOSE the open handle (releases the server-side FileId).
114 * @param work PROTOCORE_SMB_CLIENT_BORROW bytes the caller took. Not held past the call.
115 * @param h H
116 * @param send Send
117 * @param recv Recv
118 * @param ctx Ctx
119 * @return The SmbResult.
120 */
121SmbResult protocore_smb_client_smb_close(uint8_t *work, SmbHandle *h, SmbSendFn send, SmbRecvFn recv, void *ctx);
122/**
123 * @brief Read up to cap bytes from offset of the open handle, looping READ .
124 * @param work PROTOCORE_SMB_CLIENT_BORROW bytes the caller took. Not held past the call.
125 * @param h H
126 * @param offset Offset
127 * @param out Out
128 * @param cap Cap
129 * @param out_len receives the number of bytes actually read (may be < cap at EOF)
130 * @param send Send
131 * @param recv Recv
132 * @param ctx Ctx
133 * @return The SmbResult.
134 */
135SmbResult protocore_smb_client_smb_read(uint8_t *work, SmbHandle *h, uint64_t offset, uint8_t *out, size_t cap,
136 size_t *out_len, SmbSendFn send, SmbRecvFn recv, void *ctx);
137/**
138 * @brief Write len bytes at offset of the open handle, looping WRITE .
139 * @param work PROTOCORE_SMB_CLIENT_BORROW bytes the caller took. Not held past the call.
140 * @param h H
141 * @param offset Offset
142 * @param data Data
143 * @param len Len
144 * @param written receives the number of bytes written (equals len on success)
145 * @param send Send
146 * @param recv Recv
147 * @param ctx Ctx
148 * @return The SmbResult.
149 */
150SmbResult protocore_smb_client_smb_write(uint8_t *work, SmbHandle *h, uint64_t offset, const uint8_t *data, size_t len,
151 size_t *written, SmbSendFn send, SmbRecvFn recv, void *ctx);
152
153/**
154 * @brief Transport seam: the engine moves raw bytes only through these, so it runs against a real
155 * socket (protocore_client) or a test mock.
156 * @return send: bytes written (must equal @p len), else < 0. recv: bytes read (> 0), else <= 0 on
157 * close / error / timeout.
158 */
159typedef int (*SmbSendFn)(void *ctx, const uint8_t *data, size_t len);
160/**
161 * @brief The PROTOCORE_SMB_CLIENT_BORROW bytes this module's state lives in.
162 *
163 * Stated beside the namespace rather than on it: an entry takes a borrow, and this is where
164 * that borrow comes from. Taken once from the end of the pool, which no mark and no release
165 * walks, so the state lasts the life of the program.
166 *
167 * @return the span.
168 */
170
171/** @brief Module namespace. */
176
177EMBED_END_DECLS
178
179#endif // PROTOCORE_SMB_CLIENT_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.
SMB2 client wire codec (MS-SMB2), PROTOCORE_ENABLE_SMB - increment 1: the transport frame,...
#define PROTOCORE_SMB2_MAX_CIPHER_KEY_LEN
Largest cipher key length across the four SMB 3.1.1 ciphers (AES-256), for buffer sizing.
Definition smb2.h:157
SmbResult protocore_smb_client_smb_open(uint8_t *work, const SmbConfig *cfg, SmbHandle *h, SmbSendFn send, SmbRecvFn recv, void *ctx)
Run NEGOTIATE -> NTLMv2 SESSION_SETUP -> TREE_CONNECT -> CREATE and .
int(* SmbRecvFn)(void *ctx, uint8_t *buf, size_t cap)
Definition smb_client.h:51
int(* SmbSendFn)(void *ctx, const uint8_t *data, size_t len)
Transport seam: the engine moves raw bytes only through these, so it runs against a real socket (prot...
Definition smb_client.h:49
uint8_t * protocore_smb_client_span(void)
The PROTOCORE_SMB_CLIENT_BORROW bytes this module's state lives in.
SmbResult protocore_smb_client_smb_write(uint8_t *work, SmbHandle *h, uint64_t offset, const uint8_t *data, size_t len, size_t *written, SmbSendFn send, SmbRecvFn recv, void *ctx)
Write len bytes at offset of the open handle, looping WRITE .
SmbResult protocore_smb_client_smb_close(uint8_t *work, SmbHandle *h, SmbSendFn send, SmbRecvFn recv, void *ctx)
CLOSE the open handle (releases the server-side FileId).
SmbResult protocore_smb_client_smb_read(uint8_t *work, SmbHandle *h, uint64_t offset, uint8_t *out, size_t cap, size_t *out_len, SmbSendFn send, SmbRecvFn recv, void *ctx)
Read up to cap bytes from offset of the open handle, looping READ .
EMBED_ENUM_BYTE(SmbResult, SMB_OK=0, SMB_ERR_ARG=-1, SMB_ERR_IO=-2, SMB_ERR_PROTOCOL=-3, SMB_ERR_AUTH=-4, SMB_ERR_OVERFLOW=-5,)
Result of an SMB client operation. 0 is success; each failure is a distinct code.
Dispatch table. Addressed by offset, so the layout is asserted below.
Definition smb_client.h:91
SmbResult(* smb_open)(uint8_t *, const SmbConfig *, SmbHandle *, SmbSendFn, SmbRecvFn, void *)
Definition smb_client.h:92
SmbResult(* smb_write)(uint8_t *, SmbHandle *, uint64_t, const uint8_t *, size_t, size_t *, SmbSendFn, SmbRecvFn, void *)
Definition smb_client.h:95
SmbResult(* smb_close)(uint8_t *, SmbHandle *, SmbSendFn, SmbRecvFn, void *)
Definition smb_client.h:93
SmbResult(* smb_read)(uint8_t *, SmbHandle *, uint64_t, uint8_t *, size_t, size_t *, SmbSendFn, SmbRecvFn, void *)
Definition smb_client.h:94
Server credentials + the file to open. Strings are ASCII/UTF-8 (encoded UTF-16LE for you).
Definition smb_client.h:55
embed_bool encrypt
Definition smb_client.h:64
const char * path
file name relative to the share root (e.g. PROGRAMS\A.NC)
Definition smb_client.h:61
uint32_t disposition
SMB2_FILE_OPEN / _OPEN_IF / _OVERWRITE_IF / _CREATE.
Definition smb_client.h:63
uint16_t cipher_pref
Definition smb_client.h:67
const char * workstation
client name to announce (null => none)
Definition smb_client.h:59
const char * user
account name
Definition smb_client.h:56
uint32_t desired_access
SMB2_FILE_GENERIC_READ and/or _WRITE.
Definition smb_client.h:62
const char * pass
password
Definition smb_client.h:57
const char * share
the tree path, UNC \\server\share
Definition smb_client.h:60
const char * domain
NTLM domain (null/empty for a local account).
Definition smb_client.h:58
An open file on an authenticated session; the ids thread the follow-up requests.
Definition smb_client.h:73
embed_bool encrypt_active
SMB 3.x transport encryption is in force (server session or share required it).
Definition smb_client.h:82
uint64_t enc_nonce
monotonic per-session AEAD nonce counter, persisted across read/write/close
Definition smb_client.h:86
uint64_t next_message_id
the MessageId for the next request on this handle
Definition smb_client.h:78
uint8_t signing_key[16]
the session signing key when signing_active (2.x: NTLMv2 key; 3.x: KDF-derived)
Definition smb_client.h:81
uint64_t file_size
EndofFile from CREATE (the current size).
Definition smb_client.h:77
uint8_t enc_c2s[PROTOCORE_SMB2_MAX_CIPHER_KEY_LEN]
client->server cipher key (encrypts requests)
Definition smb_client.h:84
Smb2SignAlgo signing_algo
HMAC-SHA256 (SMB 2.x) or AES-CMAC (SMB 3.x), from the negotiated dialect.
Definition smb_client.h:80
uint8_t file_id[16]
Definition smb_client.h:76
uint32_t tree_id
Definition smb_client.h:75
embed_bool signing_active
the session negotiated SMB signing (server set SigningRequired, not guest/null)
Definition smb_client.h:79
uint8_t enc_s2c[PROTOCORE_SMB2_MAX_CIPHER_KEY_LEN]
server->client cipher key (decrypts responses)
Definition smb_client.h:85
uint16_t enc_cipher
negotiated Smb2Cipher id (selects the key + nonce length) when encrypt_active
Definition smb_client.h:83
uint64_t session_id
Definition smb_client.h:74