tftp_server.c
Go to the documentation of this file.
1 /**
2  * @file tftp_server.c
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  * @section Description
28  *
29  * TFTP is a very simple protocol used to transfer files. Refer to the
30  * following RFCs for complete details:
31  * - RFC 1123: Requirements for Internet Hosts
32  * - RFC 1350: The TFTP Protocol (Revision 2)
33  * - RFC 1782: TFTP Option Extension
34  * - RFC 1783: TFTP Blocksize Option
35  * - RFC 1784: TFTP Timeout Interval and Transfer Size Options
36  *
37  * @author Oryx Embedded SARL (www.oryx-embedded.com)
38  * @version 1.9.6
39  **/
40 
41 //Switch to the appropriate trace level
42 #define TRACE_LEVEL TFTP_TRACE_LEVEL
43 
44 //Dependencies
45 #include "tftp/tftp_server.h"
46 #include "tftp/tftp_server_misc.h"
47 #include "debug.h"
48 
49 //Check TCP/IP stack configuration
50 #if (TFTP_SERVER_SUPPORT == ENABLED)
51 
52 
53 /**
54  * @brief Initialize settings with default values
55  * @param[out] settings Structure that contains TFTP server settings
56  **/
57 
59 {
60  //The TFTP server is not bound to any interface
61  settings->interface = NULL;
62 
63  //TFTP port number
64  settings->port = TFTP_PORT;
65 
66  //Open file callback function
67  settings->openFileCallback = NULL;
68  //Write file callback function
69  settings->writeFileCallback = NULL;
70  //Read file callback function
71  settings->readFileCallback = NULL;
72  //Close file callback function
73  settings->closeFileCallback = NULL;
74 }
75 
76 
77 /**
78  * @brief TFTP server initialization
79  * @param[in] context Pointer to the TFTP server context
80  * @param[in] settings TFTP server specific settings
81  * @return Error code
82  **/
83 
85 {
86  error_t error;
87 
88  //Debug message
89  TRACE_INFO("Initializing TFTP server...\r\n");
90 
91  //Ensure the parameters are valid
92  if(context == NULL || settings == NULL)
94 
95  //Clear the TFTP server context
96  memset(context, 0, sizeof(TftpServerContext));
97 
98  //Save user settings
99  context->settings = *settings;
100 
101  //Create an event object to poll the state of sockets
102  if(!osCreateEvent(&context->event))
103  {
104  //Failed to create event
105  return ERROR_OUT_OF_RESOURCES;
106  }
107 
108  //Start of exception handling block
109  do
110  {
111  //Open a UDP socket
113 
114  //Failed to open socket?
115  if(context->socket == NULL)
116  {
117  //Report an error
118  error = ERROR_OPEN_FAILED;
119  //Exit immediately
120  break;
121  }
122 
123  //Associate the socket with the relevant interface
124  error = socketBindToInterface(context->socket, settings->interface);
125  //Unable to bind the socket to the desired interface?
126  if(error)
127  break;
128 
129  //The TFTP server listens for connection requests on port 69
130  error = socketBind(context->socket, &IP_ADDR_ANY, settings->port);
131  //Unable to bind the socket to the desired port?
132  if(error)
133  break;
134 
135  //End of exception handling block
136  } while(0);
137 
138  //Did we encounter an error?
139  if(error)
140  {
141  //Free previously allocated resources
142  osDeleteEvent(&context->event);
143  //Close socket
144  socketClose(context->socket);
145  }
146 
147  //Return status code
148  return error;
149 }
150 
151 
152 /**
153  * @brief Start TFTP server
154  * @param[in] context Pointer to the TFTP server context
155  * @return Error code
156  **/
157 
159 {
160  OsTask *task;
161 
162  //Debug message
163  TRACE_INFO("Starting TFTP server...\r\n");
164 
165  //Make sure the TFTP server context is valid
166  if(context == NULL)
168 
169  //Create the TFTP server task
170  task = osCreateTask("TFTP Server", (OsTaskCode) tftpServerTask,
172 
173  //Unable to create the task?
174  if(task == OS_INVALID_HANDLE)
175  return ERROR_OUT_OF_RESOURCES;
176 
177  //Successful processing
178  return NO_ERROR;
179 }
180 
181 
182 /**
183  * @brief TFTP server task
184  * @param[in] context Pointer to the TFTP server context
185  **/
186 
188 {
189  error_t error;
190  uint_t i;
191  TftpClientConnection *connection;
192 
193 #if (NET_RTOS_SUPPORT == ENABLED)
194  //Task prologue
195  osEnterTask();
196 
197  //Process events
198  while(1)
199  {
200 #endif
201  //Clear event descriptor set
202  memset(context->eventDesc, 0, sizeof(context->eventDesc));
203 
204  //Specify the events the application is interested in
205  for(i = 0; i < TFTP_SERVER_MAX_CONNECTIONS; i++)
206  {
207  //Point to the structure describing the current connection
208  connection = &context->connection[i];
209 
210  //Loop through active connections only
211  if(connection->state != TFTP_STATE_CLOSED)
212  {
213  //Wait for a packet to be received
214  context->eventDesc[i].socket = connection->socket;
215  context->eventDesc[i].eventMask = SOCKET_EVENT_RX_READY;
216  }
217  }
218 
219  //The TFTP server listens for connection requests on port 69
220  context->eventDesc[i].socket = context->socket;
221  context->eventDesc[i].eventMask = SOCKET_EVENT_RX_READY;
222 
223  //Wait for one of the set of sockets to become ready to perform I/O
224  error = socketPoll(context->eventDesc, TFTP_SERVER_MAX_CONNECTIONS + 1,
225  &context->event, TFTP_SERVER_TICK_INTERVAL);
226 
227  //Verify status code
228  if(!error)
229  {
230  //Event-driven processing
231  for(i = 0; i < TFTP_SERVER_MAX_CONNECTIONS; i++)
232  {
233  //Point to the structure describing the current connection
234  connection = &context->connection[i];
235 
236  //Loop through active connections only
237  if(connection->state != TFTP_STATE_CLOSED)
238  {
239  //Check whether a packet has been received
240  if(context->eventDesc[i].eventFlags & SOCKET_EVENT_RX_READY)
241  {
242  //Process incoming packet
243  tftpServerProcessPacket(context, connection);
244  }
245  }
246  }
247 
248  //Any connection request received on port 69?
249  if(context->eventDesc[i].eventFlags & SOCKET_EVENT_RX_READY)
250  {
251  //Accept connection request
252  tftpServerAcceptRequest(context);
253  }
254  }
255 
256  //Handle periodic operations
257  tftpServerTick(context);
258 
259 #if (NET_RTOS_SUPPORT == ENABLED)
260  }
261 #endif
262 }
263 
264 #endif
@ SOCKET_IP_PROTO_UDP
Definition: socket.h:93
error_t socketBind(Socket *socket, const IpAddr *localIpAddr, uint16_t localPort)
Associate a local address with a socket.
Definition: socket.c:355
void tftpServerProcessPacket(TftpServerContext *context, TftpClientConnection *connection)
Process incoming packet.
#define TFTP_SERVER_TICK_INTERVAL
Definition: tftp_server.h:66
TftpServerCloseFileCallback closeFileCallback
Close file callback function.
Definition: tftp_server.h:173
error_t tftpServerInit(TftpServerContext *context, const TftpServerSettings *settings)
TFTP server initialization.
Definition: tftp_server.c:84
uint16_t port
TFTP port number.
Definition: tftp_server.h:169
NetInterface * interface
Underlying network interface.
Definition: tftp_server.h:168
void socketClose(Socket *socket)
Close an existing socket.
Definition: socket.c:822
@ ERROR_OUT_OF_RESOURCES
Definition: error.h:64
#define TftpServerContext
Definition: tftp_server.h:108
@ SOCKET_TYPE_DGRAM
Definition: socket.h:78
void tftpServerTask(TftpServerContext *context)
TFTP server task.
Definition: tftp_server.c:187
TFTP server.
TftpServerReadFileCallback readFileCallback
Read file callback function.
Definition: tftp_server.h:172
TftpServerWriteFileCallback writeFileCallback
Write file callback function.
Definition: tftp_server.h:171
@ ERROR_OPEN_FAILED
Definition: error.h:74
const IpAddr IP_ADDR_ANY
Definition: ip.c:45
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
OsTask * osCreateTask(const char_t *name, OsTaskCode taskCode, void *param, size_t stackSize, int_t priority)
Create a new task.
error_t
Error codes.
Definition: error.h:42
void osDeleteEvent(OsEvent *event)
Delete an event object.
error_t tftpServerStart(TftpServerContext *context)
Start TFTP server.
Definition: tftp_server.c:158
Task object.
TftpServerOpenFileCallback openFileCallback
Open file callback function.
Definition: tftp_server.h:170
#define TRACE_INFO(...)
Definition: debug.h:94
#define TFTP_SERVER_MAX_CONNECTIONS
Definition: tftp_server.h:59
Socket * socketOpen(uint_t type, uint_t protocol)
Create a socket (UDP or TCP)
Definition: socket.c:95
#define osEnterTask()
error_t socketPoll(SocketEventDesc *eventDesc, uint_t size, OsEvent *extEvent, systime_t timeout)
Wait for one of a set of sockets to become ready to perform I/O.
Definition: socket.c:882
void tftpServerTick(TftpServerContext *context)
Handle periodic operations.
#define socketBindToInterface
Definition: net_legacy.h:270
@ TFTP_STATE_CLOSED
Definition: tftp_server.h:122
void tftpServerAcceptRequest(TftpServerContext *context)
Accept connection request.
#define OS_INVALID_HANDLE
Definition: os_port.h:79
#define TFTP_PORT
Definition: tftp_common.h:38
@ SOCKET_EVENT_RX_READY
Definition: socket.h:160
void(* OsTaskCode)(void *param)
Task routine.
#define TFTP_SERVER_PRIORITY
Definition: tftp_server.h:54
bool_t osCreateEvent(OsEvent *event)
Create an event object.
#define TftpClientConnection
Definition: tftp_server.h:104
Helper functions for TFTP server.
TFTP server settings.
Definition: tftp_server.h:166
#define TFTP_SERVER_STACK_SIZE
Definition: tftp_server.h:47
unsigned int uint_t
Definition: compiler_port.h:45
void tftpServerGetDefaultSettings(TftpServerSettings *settings)
Initialize settings with default values.
Definition: tftp_server.c:58
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.