RTEMS 6.1-rc7
Loading...
Searching...
No Matches
tftpfs_udp_network_fake.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: BSD-2-Clause */
2
14/*
15 * Copyright (C) 2022 embedded brains GmbH & Co. KG
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39#include <stddef.h>
40#include <stdint.h>
41#include <stdbool.h>
42#include <sys/types.h> /* ssize_t */
43
44#ifndef _TFTPFS_UDP_NETWORK_FAKE_H
45#define _TFTPFS_UDP_NETWORK_FAKE_H
46
47#ifdef __cplusplus
48extern "C" {
49#endif
50
51
80#define TFTP_STD_PORT 69
81#define TFTP_MAX_IP_ADDR_STRLEN (16 * 2 + 7 + 1)
82#define TFTP_MAX_ERROR_STRLEN 20
83#define TFTP_MAX_OPTIONS_SIZE 40
84
89#define TFTP_KNOWN_IPV4_ADDR0_STR "127.0.0.1"
90#define TFTP_KNOWN_IPV4_ADDR0_ARRAY 127, 0, 0, 1
91#define TFTP_KNOWN_SERVER0_NAME "server.tftp"
92#define TFTP_KNOWN_SERVER0_IPV4 "10.7.0.2"
93#define TFTP_KNOWN_SERVER0_ARRAY 10, 7, 0, 2
94
100#define TFTP_FIRST_FD 33333
101
102typedef enum Tftp_Action_kind {
103 TFTP_IA_KIND_SOCKET,
104 TFTP_IA_KIND_CLOSE,
105 TFTP_IA_KIND_BIND,
106 TFTP_IA_KIND_SENDTO,
107 TFTP_IA_KIND_RECVFROM
108} Tftp_Action_kind;
109
110typedef struct Tftp_Action {
111 Tftp_Action_kind kind;
112 union {
113 struct {
114 int domain;
115 int type;
116 int protocol;
117 int result;
118 } socket;
119 struct {
120 int fd;
121 int result;
122 } close;
123 struct {
124 int fd;
125 int family;
126 uint16_t port;
127 const char *addr_str;
128 int result;
129 } bind;
130 struct {
131 int fd;
132 const void *buf;
133 size_t len;
134 int flags;
135 uint16_t dest_port;
136 const char *dest_addr_str;
137 int addrlen;
138 ssize_t result;
139 } sendto;
140 struct {
141 int fd;
142 void *buf;
143 size_t len;
144 int flags;
145 uint32_t timeout_ms;
146 uint16_t src_port;
147 char src_addr_str[TFTP_MAX_IP_ADDR_STRLEN];
148 int addrlen;
149 ssize_t result;
150 } recvfrom;
151 } data;
153
176typedef bool (*Tftp_Interaction_fn)( Tftp_Action *act, void *data );
178typedef struct Tftp_Interaction {
179 Tftp_Interaction *next;
180 Tftp_Action_kind kind;
182 void *data[0];
184
190void _Tftp_Reset( void );
191
214 Tftp_Action_kind kind,
216 size_t size
217);
218
219
230
231/*
232 * TFTP details from RFC1350, RFC2347, RFC2348 and RFC7440
233 *
234 * Note: The RFCs require modes and options to be case in-sensitive.
235 */
236
237#define TFTP_MODE_NETASCII "netascii"
238#define TFTP_MODE_OCTET "octet"
239#define TFTP_OPTION_BLKSIZE "blksize"
240#define TFTP_OPTION_TIMEOUT "timeout"
241#define TFTP_OPTION_TSIZE "tsize"
242#define TFTP_OPTION_WINDOWSIZE "windowsize"
243
244#define TFTP_WINDOW_SIZE_MIN 1
245#define TFTP_BLOCK_SIZE_MIN 8
246#define TFTP_BLOCK_SIZE_MAX 65464
247
248typedef enum Tftp_Opcode {
249 TFTP_OPCODE_RRQ = 1,
250 TFTP_OPCODE_WRQ = 2,
251 TFTP_OPCODE_DATA = 3,
252 TFTP_OPCODE_ACK = 4,
253 TFTP_OPCODE_ERROR = 5,
254 TFTP_OPCODE_OACK = 6,
255} Tftp_Opcode;
256
257typedef enum Tftp_Error_code {
258 TFTP_ERROR_CODE_NOT_DEFINED = 0,
259 TFTP_ERROR_CODE_NOT_FOUND = 1,
260 TFTP_ERROR_CODE_NO_ACCESS = 2,
261 TFTP_ERROR_CODE_DISK_FULL = 3,
262 TFTP_ERROR_CODE_ILLEGAL = 4,
263 TFTP_ERROR_CODE_UNKNOWN_ID = 5,
264 TFTP_ERROR_CODE_FILE_EXISTS = 6,
265 TFTP_ERROR_CODE_NO_USER = 7,
266 TFTP_ERROR_CODE_OPTION_NEGO = 8,
267} Tftp_Error_code;
268
269typedef struct Tftp_Packet {
270 uint16_t opcode;
271 union {
272 struct {
273 char opts[0];
274 } rrq;
275 struct {
276 char opts[0];
277 } wrq;
278 struct {
279 uint16_t block_num;
280 uint8_t bytes[0];
281 } data;
282 struct {
283 uint16_t block_num;
284 } ack;
285 struct {
286 uint16_t error_code;
287 char err_msg[0];
288 } error;
289 struct {
290 char opts[0];
291 } oack;
292 } content;
294
307const char *_Tftp_Get_error_str( uint16_t error_code );
308
311#ifdef __cplusplus
312}
313#endif
314
315#endif /* _TFTPFS_UDP_NETWORK_FAKE_H */
void * _Tftp_Append_interaction(Tftp_Action_kind kind, Tftp_Interaction_fn fn, size_t size)
Create an interaction and append it to the sequence of expected interactions.
Definition: tftpfs_udp_network_fake.c:136
bool(* Tftp_Interaction_fn)(Tftp_Action *act, void *data)
Carry out interactions with TFTP client.
Definition: tftpfs_udp_network_fake.h:176
const char * _Tftp_Get_error_str(uint16_t error_code)
Provides a human readable description for an error code from an TFTP error packet.
Definition: tftpfs_udp_network_fake.c:193
void _Tftp_Reset(void)
Initialize and free the singleton control object.
Definition: tftpfs_udp_network_fake.c:110
bool _Tftp_Has_no_more_interactions(void)
Have all queued interactions been processed?
Definition: tftpfs_udp_network_fake.c:154
Definition: tftpfs_udp_network_fake.h:110
Definition: tftpfs_udp_network_fake.h:178
Definition: tftpfs_udp_network_fake.h:269
Definition: mongoose.c:462