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-2019 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 1.9.6
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 //Maximum size of TFTP packets
100 #define TFTP_SERVER_MAX_PACKET_SIZE (sizeof(TftpDataPacket) + TFTP_SERVER_BLOCK_SIZE)
101 
102 //Forward declaration of TftpClientConnection structure
103 struct _TftpClientConnection;
104 #define TftpClientConnection struct _TftpClientConnection
105 
106 //Forward declaration of TftpServerContext structure
107 struct _TftpServerContext;
108 #define TftpServerContext struct _TftpServerContext
109 
110 //C++ guard
111 #ifdef __cplusplus
112 extern "C" {
113 #endif
114 
115 
116 /**
117  * @brief TFTP connection state
118  **/
119 
120 typedef enum
121 {
129 
130 
131 /**
132  * @brief Open file callback function
133  **/
134 
135 typedef void *(*TftpServerOpenFileCallback)(const char_t *filename,
136  const char_t *mode, bool_t writeAccess);
137 
138 
139 /**
140  * @brief Write file callback function
141  **/
142 
144  size_t offset, const uint8_t *data, size_t length);
145 
146 
147 /**
148  * @brief Read file callback function
149  **/
150 
152  size_t offset, uint8_t *data, size_t size, size_t *length);
153 
154 
155 /**
156  * @brief Close file callback function
157  **/
158 
159 typedef void (*TftpServerCloseFileCallback)(void *file);
160 
161 
162 /**
163  * @brief TFTP server settings
164  **/
165 
166 typedef struct
167 {
168  NetInterface *interface; ///<Underlying network interface
169  uint16_t port; ///<TFTP port number
170  TftpServerOpenFileCallback openFileCallback; ///<Open file callback function
171  TftpServerWriteFileCallback writeFileCallback; ///<Write file callback function
172  TftpServerReadFileCallback readFileCallback; ///<Read file callback function
173  TftpServerCloseFileCallback closeFileCallback; ///<Close file callback function
175 
176 
177 /**
178  * @brief TFTP client connection
179  **/
180 
182 {
183  TftpServerSettings *settings; ///<User settings
184  TftpConnectionState state; ///<Connection state
185  Socket *socket; ///<Underlying socket
186  void *file; ///<File pointer
187  uint16_t block; ///<Block number
188  systime_t timestamp; ///<Time stamp to manage retransmissions
189  uint_t retransmitCount; ///<Retransmission counter
190  uint8_t packet[TFTP_SERVER_MAX_PACKET_SIZE]; ///<Outgoing TFTP packet
191  size_t packetLen; ///<Length of the outgoing packet
192 };
193 
194 
195 /**
196  * @brief TFTP server context
197  **/
198 
200 {
201  TftpServerSettings settings; ///<User settings
202  OsEvent event; ///<Event object used to poll the sockets
203  Socket *socket; ///<Listening socket
205  SocketEventDesc eventDesc[TFTP_SERVER_MAX_CONNECTIONS + 1]; ///<The events the application is interested in
206  uint8_t packet[TFTP_SERVER_MAX_PACKET_SIZE]; ///<Incoming TFTP packet
207 };
208 
209 
210 //TFTP server related functions
214 
215 void tftpServerTask(TftpServerContext *context);
216 
217 //C++ guard
218 #ifdef __cplusplus
219 }
220 #endif
221 
222 #endif
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:151
uint8_t length
Definition: dtls_misc.h:149
int bool_t
Definition: compiler_port.h:49
TftpClientConnection connection[TFTP_SERVER_MAX_CONNECTIONS]
Client connections.
Definition: tftp_server.h:204
TftpConnectionState state
Connection state.
Definition: tftp_server.h:184
void(* TftpServerCloseFileCallback)(void *file)
Close file callback function.
Definition: tftp_server.h:159
@ TFTP_STATE_WRITING
Definition: tftp_server.h:125
TftpServerCloseFileCallback closeFileCallback
Close file callback function.
Definition: tftp_server.h:173
uint16_t block
Block number.
Definition: tftp_server.h:187
uint16_t port
TFTP port number.
Definition: tftp_server.h:169
NetInterface * interface
Underlying network interface.
Definition: tftp_server.h:168
Event object.
error_t tftpServerStart(TftpServerContext *context)
Start TFTP server.
Definition: tftp_server.c:158
#define TftpServerContext
Definition: tftp_server.h:108
void tftpServerTask(TftpServerContext *context)
TFTP server task.
Definition: tftp_server.c:187
TftpServerSettings * settings
User settings.
Definition: tftp_server.h:183
TftpServerSettings settings
User settings.
Definition: tftp_server.h:201
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:206
Structure describing socket events.
Definition: socket.h:307
TftpServerReadFileCallback readFileCallback
Read file callback function.
Definition: tftp_server.h:172
TftpServerWriteFileCallback writeFileCallback
Write file callback function.
Definition: tftp_server.h:171
char_t filename[]
Definition: tftp_common.h:91
error_t
Error codes.
Definition: error.h:42
#define TFTP_SERVER_MAX_PACKET_SIZE
Definition: tftp_server.h:100
#define NetInterface
Definition: net.h:36
TFTP client connection.
Definition: tftp_server.h:181
Socket * socket
Listening socket.
Definition: tftp_server.h:203
@ TFTP_STATE_OPEN
Definition: tftp_server.h:123
@ TFTP_STATE_READING
Definition: tftp_server.h:124
TftpServerOpenFileCallback openFileCallback
Open file callback function.
Definition: tftp_server.h:170
#define TFTP_SERVER_MAX_CONNECTIONS
Definition: tftp_server.h:59
@ TFTP_STATE_WRITE_COMPLETE
Definition: tftp_server.h:127
@ TFTP_STATE_READ_COMPLETE
Definition: tftp_server.h:126
TftpConnectionState
TFTP connection state.
Definition: tftp_server.h:120
systime_t timestamp
Time stamp to manage retransmissions.
Definition: tftp_server.h:188
SocketEventDesc eventDesc[TFTP_SERVER_MAX_CONNECTIONS+1]
The events the application is interested in.
Definition: tftp_server.h:205
@ TFTP_STATE_CLOSED
Definition: tftp_server.h:122
char char_t
Definition: compiler_port.h:43
size_t packetLen
Length of the outgoing packet.
Definition: tftp_server.h:191
void *(* TftpServerOpenFileCallback)(const char_t *filename, const char_t *mode, bool_t writeAccess)
Open file callback function.
Definition: tftp_server.h:135
uint8_t file[128]
Definition: dhcp_common.h:213
#define Socket
Definition: socket.h:36
uint8_t packet[TFTP_SERVER_MAX_PACKET_SIZE]
Outgoing TFTP packet.
Definition: tftp_server.h:190
#define TftpClientConnection
Definition: tftp_server.h:104
void * file
File pointer.
Definition: tftp_server.h:186
uint8_t mode
Definition: ntp_common.h:149
TFTP server settings.
Definition: tftp_server.h:166
Definitions common to TFTP client and server.
TFTP server context.
Definition: tftp_server.h:199
void tftpServerGetDefaultSettings(TftpServerSettings *settings)
Initialize settings with default values.
Definition: tftp_server.c:58
unsigned int uint_t
Definition: compiler_port.h:45
TCP/IP stack core.
uint8_t data[]
Definition: dtls_misc.h:176
error_t(* TftpServerWriteFileCallback)(void *file, size_t offset, const uint8_t *data, size_t length)
Write file callback function.
Definition: tftp_server.h:143
uint_t retransmitCount
Retransmission counter.
Definition: tftp_server.h:189
uint32_t systime_t
Definition: compiler_port.h:46
Socket * socket
Underlying socket.
Definition: tftp_server.h:185
OsEvent event
Event object used to poll the sockets.
Definition: tftp_server.h:202