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-2022 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.2.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  NetInterface *interface; ///<Underlying network interface
174  uint16_t port; ///<TFTP port number
175  TftpServerOpenFileCallback openFileCallback; ///<Open file callback function
176  TftpServerWriteFileCallback writeFileCallback; ///<Write file callback function
177  TftpServerReadFileCallback readFileCallback; ///<Read file callback function
178  TftpServerCloseFileCallback closeFileCallback; ///<Close file callback function
180 
181 
182 /**
183  * @brief TFTP client connection
184  **/
185 
187 {
188  TftpServerSettings *settings; ///<User settings
189  TftpConnectionState state; ///<Connection state
190  Socket *socket; ///<Underlying socket
191  void *file; ///<File pointer
192  uint16_t block; ///<Block number
193  systime_t timestamp; ///<Time stamp to manage retransmissions
194  uint_t retransmitCount; ///<Retransmission counter
195  uint8_t packet[TFTP_SERVER_MAX_PACKET_SIZE]; ///<Outgoing TFTP packet
196  size_t packetLen; ///<Length of the outgoing packet
197 };
198 
199 
200 /**
201  * @brief TFTP server context
202  **/
203 
205 {
206  TftpServerSettings settings; ///<User settings
207  bool_t running; ///<Operational state of the TFTP server
208  bool_t stop; ///<Stop request
209  OsEvent event; ///<Event object used to poll the sockets
210  OsTaskId taskId; ///<Task identifier
211 #if (OS_STATIC_TASK_SUPPORT == ENABLED)
212  OsTaskTcb taskTcb; ///<Task control block
214 #endif
215  Socket *socket; ///<Listening socket
217  SocketEventDesc eventDesc[TFTP_SERVER_MAX_CONNECTIONS + 1]; ///<The events the application is interested in
218  uint8_t packet[TFTP_SERVER_MAX_PACKET_SIZE]; ///<Incoming TFTP packet
219  TFTP_SERVER_PRIVATE_CONTEXT ///<Application specific context
220 };
221 
222 
223 //TFTP server related functions
225 
227  const TftpServerSettings *settings);
228 
231 
232 void tftpServerTask(TftpServerContext *context);
233 
234 void tftpServerDeinit(TftpServerContext *context);
235 
236 //C++ guard
237 #ifdef __cplusplus
238 }
239 #endif
240 
241 #endif
uint8_t length
Definition: coap_common.h:193
bool_t running
Operational state of the TFTP server.
Definition: tftp_server.h:207
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
OsStackType taskStack[TFTP_SERVER_STACK_SIZE]
Task stack.
Definition: tftp_server.h:213
int bool_t
Definition: compiler_port.h:48
TftpClientConnection connection[TFTP_SERVER_MAX_CONNECTIONS]
Client connections.
Definition: tftp_server.h:216
uint8_t data[]
Definition: ethernet.h:220
TftpConnectionState state
Connection state.
Definition: tftp_server.h:189
void(* TftpServerCloseFileCallback)(void *file)
Close file callback function.
Definition: tftp_server.h:164
@ TFTP_STATE_WRITING
Definition: tftp_server.h:130
TftpServerCloseFileCallback closeFileCallback
Close file callback function.
Definition: tftp_server.h:178
uint16_t block
Block number.
Definition: tftp_server.h:192
uint16_t port
TFTP port number.
Definition: tftp_server.h:174
NetInterface * interface
Underlying network interface.
Definition: tftp_server.h:173
Event object.
error_t tftpServerStart(TftpServerContext *context)
Start TFTP server.
Definition: tftp_server.c:130
#define TftpServerContext
Definition: tftp_server.h:113
void tftpServerTask(TftpServerContext *context)
TFTP server task.
Definition: tftp_server.c:266
TftpServerSettings * settings
User settings.
Definition: tftp_server.h:188
TftpServerSettings settings
User settings.
Definition: tftp_server.h:206
error_t tftpServerInit(TftpServerContext *context, const TftpServerSettings *settings)
TFTP server initialization.
Definition: tftp_server.c:84
uint8_t packet[TFTP_SERVER_MAX_PACKET_SIZE]
Incoming TFTP packet.
Definition: tftp_server.h:218
Structure describing socket events.
Definition: socket.h:365
TftpServerReadFileCallback readFileCallback
Read file callback function.
Definition: tftp_server.h:177
TftpServerWriteFileCallback writeFileCallback
Write file callback function.
Definition: tftp_server.h:176
char_t filename[]
Definition: tftp_common.h:91
error_t
Error codes.
Definition: error.h:43
bool_t stop
Stop request.
Definition: tftp_server.h:208
#define TFTP_SERVER_MAX_PACKET_SIZE
Definition: tftp_server.h:105
Task control block.
#define NetInterface
Definition: net.h:36
uint32_t OsStackType
Stack data type.
TFTP client connection.
Definition: tftp_server.h:187
Socket * socket
Listening socket.
Definition: tftp_server.h:215
OsTaskTcb taskTcb
Task control block.
Definition: tftp_server.h:212
@ TFTP_STATE_OPEN
Definition: tftp_server.h:128
@ TFTP_STATE_READING
Definition: tftp_server.h:129
TftpServerOpenFileCallback openFileCallback
Open file callback function.
Definition: tftp_server.h:175
#define TFTP_SERVER_MAX_CONNECTIONS
Definition: tftp_server.h:59
@ TFTP_STATE_WRITE_COMPLETE
Definition: tftp_server.h:132
@ TFTP_STATE_READ_COMPLETE
Definition: tftp_server.h:131
TftpConnectionState
TFTP connection state.
Definition: tftp_server.h:126
systime_t timestamp
Time stamp to manage retransmissions.
Definition: tftp_server.h:193
#define TFTP_SERVER_PRIVATE_CONTEXT
Definition: tftp_server.h:101
SocketEventDesc eventDesc[TFTP_SERVER_MAX_CONNECTIONS+1]
The events the application is interested in.
Definition: tftp_server.h:217
uint32_t systime_t
System time.
@ TFTP_STATE_CLOSED
Definition: tftp_server.h:127
char char_t
Definition: compiler_port.h:43
error_t tftpServerStop(TftpServerContext *context)
Stop TFTP server.
Definition: tftp_server.c:219
size_t packetLen
Length of the outgoing packet.
Definition: tftp_server.h:196
void *(* TftpServerOpenFileCallback)(const char_t *filename, const char_t *mode, bool_t writeAccess)
Open file callback function.
Definition: tftp_server.h:140
void tftpServerDeinit(TftpServerContext *context)
Release TFTP server context.
Definition: tftp_server.c:360
uint8_t file[128]
Definition: dhcp_common.h:221
#define Socket
Definition: socket.h:36
uint8_t packet[TFTP_SERVER_MAX_PACKET_SIZE]
Outgoing TFTP packet.
Definition: tftp_server.h:195
#define TftpClientConnection
Definition: tftp_server.h:109
void * file
File pointer.
Definition: tftp_server.h:191
uint8_t mode
Definition: ntp_common.h:149
OsTaskId taskId
Task identifier.
Definition: tftp_server.h:210
TFTP server settings.
Definition: tftp_server.h:172
Definitions common to TFTP client and server.
TFTP server context.
Definition: tftp_server.h:205
void tftpServerGetDefaultSettings(TftpServerSettings *settings)
Initialize settings with default values.
Definition: tftp_server.c:58
thread_t * OsTaskId
Task identifier.
#define TFTP_SERVER_STACK_SIZE
Definition: tftp_server.h:47
unsigned int uint_t
Definition: compiler_port.h:45
TCP/IP stack core.
error_t(* TftpServerWriteFileCallback)(void *file, size_t offset, const uint8_t *data, size_t length)
Write file callback function.
Definition: tftp_server.h:148
uint_t retransmitCount
Retransmission counter.
Definition: tftp_server.h:194
Socket * socket
Underlying socket.
Definition: tftp_server.h:190
OsEvent event
Event object used to poll the sockets.
Definition: tftp_server.h:209