tftp_server.h
Go to the documentation of this file.
1 /**
2  * @file tftp_server.h
3  * @brief TFTP server
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.0
29  **/
30 
31 #ifndef _TFTP_SERVER_H
32 #define _TFTP_SERVER_H
33 
34 //Dependencies
35 #include "core/net.h"
36 #include "tftp/tftp_common.h"
37 
38 //TFTP server support
39 #ifndef TFTP_SERVER_SUPPORT
40  #define TFTP_SERVER_SUPPORT ENABLED
41 #elif (TFTP_SERVER_SUPPORT != ENABLED && TFTP_SERVER_SUPPORT != DISABLED)
42  #error TFTP_SERVER_SUPPORT parameter is not valid
43 #endif
44 
45 //Stack size required to run the TFTP server
46 #ifndef TFTP_SERVER_STACK_SIZE
47  #define TFTP_SERVER_STACK_SIZE 650
48 #elif (TFTP_SERVER_STACK_SIZE < 1)
49  #error TFTP_SERVER_STACK_SIZE parameter is not valid
50 #endif
51 
52 //Priority at which the TFTP server should run
53 #ifndef TFTP_SERVER_PRIORITY
54  #define TFTP_SERVER_PRIORITY OS_TASK_PRIORITY_NORMAL
55 #endif
56 
57 //Maximum number of simultaneous connections
58 #ifndef TFTP_SERVER_MAX_CONNECTIONS
59  #define TFTP_SERVER_MAX_CONNECTIONS 2
60 #elif (TFTP_SERVER_MAX_CONNECTIONS < 1)
61  #error TFTP_SERVER_MAX_CONNECTIONS parameter is not valid
62 #endif
63 
64 //TFTP server tick interval
65 #ifndef TFTP_SERVER_TICK_INTERVAL
66  #define TFTP_SERVER_TICK_INTERVAL 500
67 #elif (TFTP_SERVER_TICK_INTERVAL < 100)
68  #error TFTP_SERVER_TICK_INTERVAL parameter is not valid
69 #endif
70 
71 //Maximum number of retransmissions of packets
72 #ifndef TFTP_SERVER_MAX_RETRIES
73  #define TFTP_SERVER_MAX_RETRIES 5
74 #elif (TFTP_SERVER_MAX_RETRIES < 1)
75  #error TFTP_SERVER_MAX_RETRIES parameter is not valid
76 #endif
77 
78 //Retransmission timeout
79 #ifndef TFTP_SERVER_TIMEOUT
80  #define TFTP_SERVER_TIMEOUT 5000
81 #elif (TFTP_SERVER_TIMEOUT < 1000)
82  #error TFTP_SERVER_TIMEOUT parameter is not valid
83 #endif
84 
85 //Additional delay before closing the connection (when sending the final ACK)
86 #ifndef TFTP_SERVER_FINAL_DELAY
87  #define TFTP_SERVER_FINAL_DELAY 10000
88 #elif (TFTP_SERVER_FINAL_DELAY < 1000)
89  #error TFTP_SERVER_FINAL_DELAY parameter is not valid
90 #endif
91 
92 //Block size
93 #ifndef TFTP_SERVER_BLOCK_SIZE
94  #define TFTP_SERVER_BLOCK_SIZE 512
95 #elif (TFTP_SERVER_BLOCK_SIZE < 512)
96  #error TFTP_SERVER_BLOCK_SIZE parameter is not valid
97 #endif
98 
99 //Application specific context
100 #ifndef TFTP_SERVER_PRIVATE_CONTEXT
101  #define TFTP_SERVER_PRIVATE_CONTEXT
102 #endif
103 
104 //Maximum size of TFTP packets
105 #define TFTP_SERVER_MAX_PACKET_SIZE (sizeof(TftpDataPacket) + TFTP_SERVER_BLOCK_SIZE)
106 
107 //Forward declaration of TftpClientConnection structure
108 struct _TftpClientConnection;
109 #define TftpClientConnection struct _TftpClientConnection
110 
111 //Forward declaration of TftpServerContext structure
112 struct _TftpServerContext;
113 #define TftpServerContext struct _TftpServerContext
114 
115 //C++ guard
116 #ifdef __cplusplus
117 extern "C" {
118 #endif
119 
120 
121 /**
122  * @brief TFTP connection state
123  **/
124 
125 typedef enum
126 {
134 
135 
136 /**
137  * @brief Open file callback function
138  **/
139 
140 typedef void *(*TftpServerOpenFileCallback)(const char_t *filename,
141  const char_t *mode, bool_t writeAccess);
142 
143 
144 /**
145  * @brief Write file callback function
146  **/
147 
149  size_t offset, const uint8_t *data, size_t length);
150 
151 
152 /**
153  * @brief Read file callback function
154  **/
155 
157  size_t offset, uint8_t *data, size_t size, size_t *length);
158 
159 
160 /**
161  * @brief Close file callback function
162  **/
163 
164 typedef void (*TftpServerCloseFileCallback)(void *file);
165 
166 
167 /**
168  * @brief TFTP server settings
169  **/
170 
171 typedef struct
172 {
173  OsTaskParameters task; ///<Task parameters
174  NetInterface *interface; ///<Underlying network interface
175  uint16_t port; ///<TFTP port number
176  TftpServerOpenFileCallback openFileCallback; ///<Open file callback function
177  TftpServerWriteFileCallback writeFileCallback; ///<Write file callback function
178  TftpServerReadFileCallback readFileCallback; ///<Read file callback function
179  TftpServerCloseFileCallback closeFileCallback; ///<Close file callback function
181 
182 
183 /**
184  * @brief TFTP client connection
185  **/
186 
188 {
189  TftpServerSettings *settings; ///<User settings
190  TftpConnectionState state; ///<Connection state
191  Socket *socket; ///<Underlying socket
192  void *file; ///<File pointer
193  uint16_t block; ///<Block number
194  systime_t timestamp; ///<Time stamp to manage retransmissions
195  uint_t retransmitCount; ///<Retransmission counter
196  uint8_t packet[TFTP_SERVER_MAX_PACKET_SIZE]; ///<Outgoing TFTP packet
197  size_t packetLen; ///<Length of the outgoing packet
198 };
199 
200 
201 /**
202  * @brief TFTP server context
203  **/
204 
206 {
207  TftpServerSettings settings; ///<User settings
208  bool_t running; ///<Operational state of the TFTP server
209  bool_t stop; ///<Stop request
210  OsEvent event; ///<Event object used to poll the sockets
211  OsTaskParameters taskParams; ///<Task parameters
212  OsTaskId taskId; ///<Task identifier
213  Socket *socket; ///<Listening socket
215  SocketEventDesc eventDesc[TFTP_SERVER_MAX_CONNECTIONS + 1]; ///<The events the application is interested in
216  uint8_t packet[TFTP_SERVER_MAX_PACKET_SIZE]; ///<Incoming TFTP packet
217  TFTP_SERVER_PRIVATE_CONTEXT ///<Application specific context
218 };
219 
220 
221 //TFTP server related functions
223 
225  const TftpServerSettings *settings);
226 
229 
230 void tftpServerTask(TftpServerContext *context);
231 
232 void tftpServerDeinit(TftpServerContext *context);
233 
234 //C++ guard
235 #ifdef __cplusplus
236 }
237 #endif
238 
239 #endif
unsigned int uint_t
Definition: compiler_port.h:50
char char_t
Definition: compiler_port.h:48
int bool_t
Definition: compiler_port.h:53
uint8_t file[128]
Definition: dhcp_common.h:223
error_t
Error codes.
Definition: error.h:43
uint8_t data[]
Definition: ethernet.h:222
TCP/IP stack core.
#define NetInterface
Definition: net.h:36
uint32_t systime_t
System time.
thread_t * OsTaskId
Task identifier.
#define Socket
Definition: socket.h:36
TFTP client connection.
Definition: tftp_server.h:188
systime_t timestamp
Time stamp to manage retransmissions.
Definition: tftp_server.h:194
TftpConnectionState state
Connection state.
Definition: tftp_server.h:190
uint16_t block
Block number.
Definition: tftp_server.h:193
uint8_t packet[TFTP_SERVER_MAX_PACKET_SIZE]
Outgoing TFTP packet.
Definition: tftp_server.h:196
void * file
File pointer.
Definition: tftp_server.h:192
uint_t retransmitCount
Retransmission counter.
Definition: tftp_server.h:195
TftpServerSettings * settings
User settings.
Definition: tftp_server.h:189
Socket * socket
Underlying socket.
Definition: tftp_server.h:191
size_t packetLen
Length of the outgoing packet.
Definition: tftp_server.h:197
TFTP server context.
Definition: tftp_server.h:206
bool_t stop
Stop request.
Definition: tftp_server.h:209
uint8_t packet[TFTP_SERVER_MAX_PACKET_SIZE]
Incoming TFTP packet.
Definition: tftp_server.h:216
TftpClientConnection connection[TFTP_SERVER_MAX_CONNECTIONS]
Client connections.
Definition: tftp_server.h:214
OsTaskId taskId
Task identifier.
Definition: tftp_server.h:212
bool_t running
Operational state of the TFTP server.
Definition: tftp_server.h:208
OsTaskParameters taskParams
Task parameters.
Definition: tftp_server.h:211
SocketEventDesc eventDesc[TFTP_SERVER_MAX_CONNECTIONS+1]
The events the application is interested in.
Definition: tftp_server.h:215
Socket * socket
Listening socket.
Definition: tftp_server.h:213
OsEvent event
Event object used to poll the sockets.
Definition: tftp_server.h:210
TftpServerSettings settings
User settings.
Definition: tftp_server.h:207
Event object.
Task parameters.
Structure describing socket events.
Definition: socket.h:398
TFTP server settings.
Definition: tftp_server.h:172
OsTaskParameters task
Task parameters.
Definition: tftp_server.h:173
TftpServerCloseFileCallback closeFileCallback
Close file callback function.
Definition: tftp_server.h:179
uint16_t port
TFTP port number.
Definition: tftp_server.h:175
TftpServerWriteFileCallback writeFileCallback
Write file callback function.
Definition: tftp_server.h:177
TftpServerOpenFileCallback openFileCallback
Open file callback function.
Definition: tftp_server.h:176
TftpServerReadFileCallback readFileCallback
Read file callback function.
Definition: tftp_server.h:178
NetInterface * interface
Underlying network interface.
Definition: tftp_server.h:174
uint8_t length
Definition: tcp.h:368
Definitions common to TFTP client and server.
char_t filename[]
Definition: tftp_common.h:93
error_t tftpServerInit(TftpServerContext *context, const TftpServerSettings *settings)
TFTP server initialization.
Definition: tftp_server.c:89
#define TFTP_SERVER_MAX_CONNECTIONS
Definition: tftp_server.h:59
#define TftpClientConnection
Definition: tftp_server.h:109
#define TFTP_SERVER_PRIVATE_CONTEXT
Definition: tftp_server.h:101
void *(* TftpServerOpenFileCallback)(const char_t *filename, const char_t *mode, bool_t writeAccess)
Open file callback function.
Definition: tftp_server.h:140
#define TFTP_SERVER_MAX_PACKET_SIZE
Definition: tftp_server.h:105
#define TftpServerContext
Definition: tftp_server.h:113
void tftpServerGetDefaultSettings(TftpServerSettings *settings)
Initialize settings with default values.
Definition: tftp_server.c:58
TftpConnectionState
TFTP connection state.
Definition: tftp_server.h:126
@ TFTP_STATE_WRITE_COMPLETE
Definition: tftp_server.h:132
@ TFTP_STATE_OPEN
Definition: tftp_server.h:128
@ TFTP_STATE_READING
Definition: tftp_server.h:129
@ TFTP_STATE_WRITING
Definition: tftp_server.h:130
@ TFTP_STATE_READ_COMPLETE
Definition: tftp_server.h:131
@ TFTP_STATE_CLOSED
Definition: tftp_server.h:127
error_t tftpServerStart(TftpServerContext *context)
Start TFTP server.
Definition: tftp_server.c:139
error_t(* TftpServerWriteFileCallback)(void *file, size_t offset, const uint8_t *data, size_t length)
Write file callback function.
Definition: tftp_server.h:148
void tftpServerTask(TftpServerContext *context)
TFTP server task.
Definition: tftp_server.c:268
error_t tftpServerStop(TftpServerContext *context)
Stop TFTP server.
Definition: tftp_server.c:221
void(* TftpServerCloseFileCallback)(void *file)
Close file callback function.
Definition: tftp_server.h:164
void tftpServerDeinit(TftpServerContext *context)
Release TFTP server context.
Definition: tftp_server.c:363
error_t(* TftpServerReadFileCallback)(void *file, size_t offset, uint8_t *data, size_t size, size_t *length)
Read file callback function.
Definition: tftp_server.h:156