Dependents:   Nucleo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers tftp_server.h Source File

tftp_server.h

Go to the documentation of this file.
00001 /**
00002  * @file tftp_server.h
00003  * @brief TFTP server
00004  *
00005  * @section License
00006  *
00007  * Copyright (C) 2010-2017 Oryx Embedded SARL. All rights reserved.
00008  *
00009  * This file is part of CycloneTCP Open.
00010  *
00011  * This program is free software; you can redistribute it and/or
00012  * modify it under the terms of the GNU General Public License
00013  * as published by the Free Software Foundation; either version 2
00014  * of the License, or (at your option) any later version.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software Foundation,
00023  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00024  *
00025  * @author Oryx Embedded SARL (www.oryx-embedded.com)
00026  * @version 1.7.6
00027  **/
00028 
00029 #ifndef _TFTP_SERVER_H
00030 #define _TFTP_SERVER_H
00031 
00032 //Dependencies
00033 #include "core/net.h"
00034 #include "tftp_common.h"
00035 
00036 //TFTP server support
00037 #ifndef TFTP_SERVER_SUPPORT
00038    #define TFTP_SERVER_SUPPORT ENABLED
00039 #elif (TFTP_SERVER_SUPPORT != ENABLED && TFTP_SERVER_SUPPORT != DISABLED)
00040    #error TFTP_SERVER_SUPPORT parameter is not valid
00041 #endif
00042 
00043 //Stack size required to run the TFTP server
00044 #ifndef TFTP_SERVER_STACK_SIZE
00045    #define TFTP_SERVER_STACK_SIZE 650
00046 #elif (TFTP_SERVER_STACK_SIZE < 1)
00047    #error TFTP_SERVER_STACK_SIZE parameter is not valid
00048 #endif
00049 
00050 //Priority at which the TFTP server should run
00051 #ifndef TFTP_SERVER_PRIORITY
00052    #define TFTP_SERVER_PRIORITY OS_TASK_PRIORITY_NORMAL
00053 #endif
00054 
00055 //Maximum number of simultaneous connections
00056 #ifndef TFTP_SERVER_MAX_CONNECTIONS
00057    #define TFTP_SERVER_MAX_CONNECTIONS 2
00058 #elif (TFTP_SERVER_MAX_CONNECTIONS < 1)
00059    #error TFTP_SERVER_MAX_CONNECTIONS parameter is not valid
00060 #endif
00061 
00062 //TFTP server tick interval
00063 #ifndef TFTP_SERVER_TICK_INTERVAL
00064    #define TFTP_SERVER_TICK_INTERVAL 500
00065 #elif (TFTP_SERVER_TICK_INTERVAL < 100)
00066    #error TFTP_SERVER_TICK_INTERVAL parameter is not valid
00067 #endif
00068 
00069 //Maximum number of retransmissions of packets
00070 #ifndef TFTP_SERVER_MAX_RETRIES
00071    #define TFTP_SERVER_MAX_RETRIES 5
00072 #elif (TFTP_SERVER_MAX_RETRIES < 1)
00073    #error TFTP_SERVER_MAX_RETRIES parameter is not valid
00074 #endif
00075 
00076 //Retransmission timeout
00077 #ifndef TFTP_SERVER_TIMEOUT
00078    #define TFTP_SERVER_TIMEOUT 5000
00079 #elif (TFTP_SERVER_TIMEOUT < 1000)
00080    #error TFTP_SERVER_TIMEOUT parameter is not valid
00081 #endif
00082 
00083 //Additional delay before closing the connection (when sending the final ACK)
00084 #ifndef TFTP_SERVER_FINAL_DELAY
00085    #define TFTP_SERVER_FINAL_DELAY 10000
00086 #elif (TFTP_SERVER_FINAL_DELAY < 1000)
00087    #error TFTP_SERVER_FINAL_DELAY parameter is not valid
00088 #endif
00089 
00090 //Block size
00091 #ifndef TFTP_SERVER_BLOCK_SIZE
00092    #define TFTP_SERVER_BLOCK_SIZE 512
00093 #elif (TFTP_SERVER_BLOCK_SIZE < 512)
00094    #error TFTP_SERVER_BLOCK_SIZE parameter is not valid
00095 #endif
00096 
00097 //Maximum size of TFTP packets
00098 #define TFTP_SERVER_MAX_PACKET_SIZE (sizeof(TftpDataPacket) + TFTP_SERVER_BLOCK_SIZE)
00099 
00100 //Forward declaration of TftpClientConnection structure
00101 struct _TftpClientConnection;
00102 #define TftpClientConnection struct _TftpClientConnection
00103 
00104 //Forward declaration of TftpServerContext structure
00105 struct _TftpServerContext;
00106 #define TftpServerContext struct _TftpServerContext
00107 
00108 
00109 /**
00110  * @brief TFTP connection state
00111  **/
00112 
00113 typedef enum
00114 {
00115    TFTP_STATE_CLOSED         = 0,
00116    TFTP_STATE_OPEN           = 1,
00117    TFTP_STATE_READING        = 2,
00118    TFTP_STATE_WRITING        = 3,
00119    TFTP_STATE_READ_COMPLETE  = 4,
00120    TFTP_STATE_WRITE_COMPLETE = 5
00121 } TftpConnectionState;
00122 
00123 
00124 /**
00125  * @brief Open file callback function
00126  **/
00127 
00128 typedef void *(*TftpServerOpenFileCallback)(const char_t *filename,
00129    const char_t *mode, bool_t writeAccess);
00130 
00131 
00132 /**
00133  * @brief Write file callback function
00134  **/
00135 
00136 typedef error_t (*TftpServerWriteFileCallback)(void *file,
00137    size_t offset, const uint8_t *data, size_t length);
00138 
00139 
00140 /**
00141  * @brief Read file callback function
00142  **/
00143 
00144 typedef error_t (*TftpServerReadFileCallback)(void *file,
00145    size_t offset, uint8_t *data, size_t size, size_t *length);
00146 
00147 
00148 /**
00149  * @brief Close file callback function
00150  **/
00151 
00152 typedef void (*TftpServerCloseFileCallback)(void *file);
00153 
00154 
00155 /**
00156  * @brief TFTP server settings
00157  **/
00158 
00159 typedef struct
00160 {
00161    NetInterface *interface;                       ///<Underlying network interface
00162    uint16_t port;                                 ///<TFTP port number
00163    TftpServerOpenFileCallback openFileCallback;   ///<Open file callback function
00164    TftpServerWriteFileCallback writeFileCallback; ///<Write file callback function
00165    TftpServerReadFileCallback readFileCallback;   ///<Read file callback function
00166    TftpServerCloseFileCallback closeFileCallback; ///<Close file callback function
00167 } TftpServerSettings;
00168 
00169 
00170 /**
00171  * @brief TFTP client connection
00172  **/
00173 
00174 struct _TftpClientConnection
00175 {
00176    TftpServerSettings *settings;                ///<User settings
00177    TftpConnectionState state;                   ///<Connection state
00178    Socket *socket;                              ///<Underlying socket
00179    void *file;                                  ///<File pointer
00180    uint16_t block;                              ///<Block number
00181    systime_t timestamp;                         ///<Time stamp to manage retransmissions
00182    uint_t retransmitCount;                      ///<Retransmission counter
00183    uint8_t packet[TFTP_SERVER_MAX_PACKET_SIZE]; ///<Outgoing TFTP packet
00184    size_t packetLen;                            ///<Length of the outgoing packet
00185 };
00186 
00187 
00188 /**
00189  * @brief TFTP server context
00190  **/
00191 
00192 struct _TftpServerContext
00193 {
00194    TftpServerSettings settings;                                  ///<User settings
00195    OsEvent event;                                                ///<Event object used to poll the sockets
00196    Socket *socket;                                               ///<Listening socket
00197    TftpClientConnection connection[TFTP_SERVER_MAX_CONNECTIONS]; ///<Client connections
00198    SocketEventDesc eventDesc[TFTP_SERVER_MAX_CONNECTIONS + 1];   ///<The events the application is interested in
00199    uint8_t packet[TFTP_SERVER_MAX_PACKET_SIZE];                  ///<Incoming TFTP packet
00200 };
00201 
00202 
00203 //TFTP server related functions
00204 void tftpServerGetDefaultSettings(TftpServerSettings *settings);
00205 error_t tftpServerInit(TftpServerContext *context, const TftpServerSettings *settings);
00206 error_t tftpServerStart(TftpServerContext *context);
00207 
00208 void tftpServerTask(TftpServerContext *context);
00209 void tftpServerTick(TftpServerContext *context);
00210 
00211 void tftpServerAcceptRequest(TftpServerContext *context);
00212 
00213 void tftpServerProcessPacket(TftpServerContext *context,
00214    TftpClientConnection *connection);
00215 
00216 void tftpServerProcessRrqPacket(TftpServerContext *context, const IpAddr *clientIpAddr,
00217    uint16_t clientPort, const TftpRrqPacket *rrqPacket, size_t length);
00218 
00219 void tftpServerProcessWrqPacket(TftpServerContext *context, const IpAddr *clientIpAddr,
00220    uint16_t clientPort, const TftpWrqPacket *wrqPacket, size_t length);
00221 
00222 void tftpServerProcessDataPacket(TftpClientConnection *connection,
00223    const TftpDataPacket *dataPacket, size_t length);
00224 
00225 void tftpServerProcessAckPacket(TftpClientConnection *connection,
00226    const TftpAckPacket *ackPacket, size_t length);
00227 
00228 void tftpServerProcessErrorPacket(TftpClientConnection *connection,
00229    const TftpErrorPacket *errorPacket, size_t length);
00230 
00231 error_t tftpServerSendDataPacket(TftpClientConnection *connection);
00232 error_t tftpServerSendAckPacket(TftpClientConnection *connection);
00233 
00234 error_t tftpServerSendErrorPacket(TftpClientConnection *connection,
00235    uint16_t errorCode, const char_t *errorMsg);
00236 
00237 error_t tftpServerRetransmitPacket(TftpClientConnection *connection);
00238 
00239 TftpClientConnection *tftpServerOpenConnection(TftpServerContext *context,
00240    const IpAddr *clientIpAddr, uint16_t clientPort);
00241 
00242 void tftpServerCloseConnection(TftpClientConnection *connection);
00243 
00244 #endif
00245