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