Webserver+3d print
cyclone_tcp/tftp/tftp_server.h@0:8918a71cdbe9, 2017-02-04 (annotated)
- Committer:
- Sergunb
- Date:
- Sat Feb 04 18:15:49 2017 +0000
- Revision:
- 0:8918a71cdbe9
nothing else
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Sergunb | 0:8918a71cdbe9 | 1 | /** |
Sergunb | 0:8918a71cdbe9 | 2 | * @file tftp_server.h |
Sergunb | 0:8918a71cdbe9 | 3 | * @brief TFTP server |
Sergunb | 0:8918a71cdbe9 | 4 | * |
Sergunb | 0:8918a71cdbe9 | 5 | * @section License |
Sergunb | 0:8918a71cdbe9 | 6 | * |
Sergunb | 0:8918a71cdbe9 | 7 | * Copyright (C) 2010-2017 Oryx Embedded SARL. All rights reserved. |
Sergunb | 0:8918a71cdbe9 | 8 | * |
Sergunb | 0:8918a71cdbe9 | 9 | * This file is part of CycloneTCP Open. |
Sergunb | 0:8918a71cdbe9 | 10 | * |
Sergunb | 0:8918a71cdbe9 | 11 | * This program is free software; you can redistribute it and/or |
Sergunb | 0:8918a71cdbe9 | 12 | * modify it under the terms of the GNU General Public License |
Sergunb | 0:8918a71cdbe9 | 13 | * as published by the Free Software Foundation; either version 2 |
Sergunb | 0:8918a71cdbe9 | 14 | * of the License, or (at your option) any later version. |
Sergunb | 0:8918a71cdbe9 | 15 | * |
Sergunb | 0:8918a71cdbe9 | 16 | * This program is distributed in the hope that it will be useful, |
Sergunb | 0:8918a71cdbe9 | 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
Sergunb | 0:8918a71cdbe9 | 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Sergunb | 0:8918a71cdbe9 | 19 | * GNU General Public License for more details. |
Sergunb | 0:8918a71cdbe9 | 20 | * |
Sergunb | 0:8918a71cdbe9 | 21 | * You should have received a copy of the GNU General Public License |
Sergunb | 0:8918a71cdbe9 | 22 | * along with this program; if not, write to the Free Software Foundation, |
Sergunb | 0:8918a71cdbe9 | 23 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
Sergunb | 0:8918a71cdbe9 | 24 | * |
Sergunb | 0:8918a71cdbe9 | 25 | * @author Oryx Embedded SARL (www.oryx-embedded.com) |
Sergunb | 0:8918a71cdbe9 | 26 | * @version 1.7.6 |
Sergunb | 0:8918a71cdbe9 | 27 | **/ |
Sergunb | 0:8918a71cdbe9 | 28 | |
Sergunb | 0:8918a71cdbe9 | 29 | #ifndef _TFTP_SERVER_H |
Sergunb | 0:8918a71cdbe9 | 30 | #define _TFTP_SERVER_H |
Sergunb | 0:8918a71cdbe9 | 31 | |
Sergunb | 0:8918a71cdbe9 | 32 | //Dependencies |
Sergunb | 0:8918a71cdbe9 | 33 | #include "core/net.h" |
Sergunb | 0:8918a71cdbe9 | 34 | #include "tftp_common.h" |
Sergunb | 0:8918a71cdbe9 | 35 | |
Sergunb | 0:8918a71cdbe9 | 36 | //TFTP server support |
Sergunb | 0:8918a71cdbe9 | 37 | #ifndef TFTP_SERVER_SUPPORT |
Sergunb | 0:8918a71cdbe9 | 38 | #define TFTP_SERVER_SUPPORT ENABLED |
Sergunb | 0:8918a71cdbe9 | 39 | #elif (TFTP_SERVER_SUPPORT != ENABLED && TFTP_SERVER_SUPPORT != DISABLED) |
Sergunb | 0:8918a71cdbe9 | 40 | #error TFTP_SERVER_SUPPORT parameter is not valid |
Sergunb | 0:8918a71cdbe9 | 41 | #endif |
Sergunb | 0:8918a71cdbe9 | 42 | |
Sergunb | 0:8918a71cdbe9 | 43 | //Stack size required to run the TFTP server |
Sergunb | 0:8918a71cdbe9 | 44 | #ifndef TFTP_SERVER_STACK_SIZE |
Sergunb | 0:8918a71cdbe9 | 45 | #define TFTP_SERVER_STACK_SIZE 650 |
Sergunb | 0:8918a71cdbe9 | 46 | #elif (TFTP_SERVER_STACK_SIZE < 1) |
Sergunb | 0:8918a71cdbe9 | 47 | #error TFTP_SERVER_STACK_SIZE parameter is not valid |
Sergunb | 0:8918a71cdbe9 | 48 | #endif |
Sergunb | 0:8918a71cdbe9 | 49 | |
Sergunb | 0:8918a71cdbe9 | 50 | //Priority at which the TFTP server should run |
Sergunb | 0:8918a71cdbe9 | 51 | #ifndef TFTP_SERVER_PRIORITY |
Sergunb | 0:8918a71cdbe9 | 52 | #define TFTP_SERVER_PRIORITY OS_TASK_PRIORITY_NORMAL |
Sergunb | 0:8918a71cdbe9 | 53 | #endif |
Sergunb | 0:8918a71cdbe9 | 54 | |
Sergunb | 0:8918a71cdbe9 | 55 | //Maximum number of simultaneous connections |
Sergunb | 0:8918a71cdbe9 | 56 | #ifndef TFTP_SERVER_MAX_CONNECTIONS |
Sergunb | 0:8918a71cdbe9 | 57 | #define TFTP_SERVER_MAX_CONNECTIONS 2 |
Sergunb | 0:8918a71cdbe9 | 58 | #elif (TFTP_SERVER_MAX_CONNECTIONS < 1) |
Sergunb | 0:8918a71cdbe9 | 59 | #error TFTP_SERVER_MAX_CONNECTIONS parameter is not valid |
Sergunb | 0:8918a71cdbe9 | 60 | #endif |
Sergunb | 0:8918a71cdbe9 | 61 | |
Sergunb | 0:8918a71cdbe9 | 62 | //TFTP server tick interval |
Sergunb | 0:8918a71cdbe9 | 63 | #ifndef TFTP_SERVER_TICK_INTERVAL |
Sergunb | 0:8918a71cdbe9 | 64 | #define TFTP_SERVER_TICK_INTERVAL 500 |
Sergunb | 0:8918a71cdbe9 | 65 | #elif (TFTP_SERVER_TICK_INTERVAL < 100) |
Sergunb | 0:8918a71cdbe9 | 66 | #error TFTP_SERVER_TICK_INTERVAL parameter is not valid |
Sergunb | 0:8918a71cdbe9 | 67 | #endif |
Sergunb | 0:8918a71cdbe9 | 68 | |
Sergunb | 0:8918a71cdbe9 | 69 | //Maximum number of retransmissions of packets |
Sergunb | 0:8918a71cdbe9 | 70 | #ifndef TFTP_SERVER_MAX_RETRIES |
Sergunb | 0:8918a71cdbe9 | 71 | #define TFTP_SERVER_MAX_RETRIES 5 |
Sergunb | 0:8918a71cdbe9 | 72 | #elif (TFTP_SERVER_MAX_RETRIES < 1) |
Sergunb | 0:8918a71cdbe9 | 73 | #error TFTP_SERVER_MAX_RETRIES parameter is not valid |
Sergunb | 0:8918a71cdbe9 | 74 | #endif |
Sergunb | 0:8918a71cdbe9 | 75 | |
Sergunb | 0:8918a71cdbe9 | 76 | //Retransmission timeout |
Sergunb | 0:8918a71cdbe9 | 77 | #ifndef TFTP_SERVER_TIMEOUT |
Sergunb | 0:8918a71cdbe9 | 78 | #define TFTP_SERVER_TIMEOUT 5000 |
Sergunb | 0:8918a71cdbe9 | 79 | #elif (TFTP_SERVER_TIMEOUT < 1000) |
Sergunb | 0:8918a71cdbe9 | 80 | #error TFTP_SERVER_TIMEOUT parameter is not valid |
Sergunb | 0:8918a71cdbe9 | 81 | #endif |
Sergunb | 0:8918a71cdbe9 | 82 | |
Sergunb | 0:8918a71cdbe9 | 83 | //Additional delay before closing the connection (when sending the final ACK) |
Sergunb | 0:8918a71cdbe9 | 84 | #ifndef TFTP_SERVER_FINAL_DELAY |
Sergunb | 0:8918a71cdbe9 | 85 | #define TFTP_SERVER_FINAL_DELAY 10000 |
Sergunb | 0:8918a71cdbe9 | 86 | #elif (TFTP_SERVER_FINAL_DELAY < 1000) |
Sergunb | 0:8918a71cdbe9 | 87 | #error TFTP_SERVER_FINAL_DELAY parameter is not valid |
Sergunb | 0:8918a71cdbe9 | 88 | #endif |
Sergunb | 0:8918a71cdbe9 | 89 | |
Sergunb | 0:8918a71cdbe9 | 90 | //Block size |
Sergunb | 0:8918a71cdbe9 | 91 | #ifndef TFTP_SERVER_BLOCK_SIZE |
Sergunb | 0:8918a71cdbe9 | 92 | #define TFTP_SERVER_BLOCK_SIZE 512 |
Sergunb | 0:8918a71cdbe9 | 93 | #elif (TFTP_SERVER_BLOCK_SIZE < 512) |
Sergunb | 0:8918a71cdbe9 | 94 | #error TFTP_SERVER_BLOCK_SIZE parameter is not valid |
Sergunb | 0:8918a71cdbe9 | 95 | #endif |
Sergunb | 0:8918a71cdbe9 | 96 | |
Sergunb | 0:8918a71cdbe9 | 97 | //Maximum size of TFTP packets |
Sergunb | 0:8918a71cdbe9 | 98 | #define TFTP_SERVER_MAX_PACKET_SIZE (sizeof(TftpDataPacket) + TFTP_SERVER_BLOCK_SIZE) |
Sergunb | 0:8918a71cdbe9 | 99 | |
Sergunb | 0:8918a71cdbe9 | 100 | //Forward declaration of TftpClientConnection structure |
Sergunb | 0:8918a71cdbe9 | 101 | struct _TftpClientConnection; |
Sergunb | 0:8918a71cdbe9 | 102 | #define TftpClientConnection struct _TftpClientConnection |
Sergunb | 0:8918a71cdbe9 | 103 | |
Sergunb | 0:8918a71cdbe9 | 104 | //Forward declaration of TftpServerContext structure |
Sergunb | 0:8918a71cdbe9 | 105 | struct _TftpServerContext; |
Sergunb | 0:8918a71cdbe9 | 106 | #define TftpServerContext struct _TftpServerContext |
Sergunb | 0:8918a71cdbe9 | 107 | |
Sergunb | 0:8918a71cdbe9 | 108 | |
Sergunb | 0:8918a71cdbe9 | 109 | /** |
Sergunb | 0:8918a71cdbe9 | 110 | * @brief TFTP connection state |
Sergunb | 0:8918a71cdbe9 | 111 | **/ |
Sergunb | 0:8918a71cdbe9 | 112 | |
Sergunb | 0:8918a71cdbe9 | 113 | typedef enum |
Sergunb | 0:8918a71cdbe9 | 114 | { |
Sergunb | 0:8918a71cdbe9 | 115 | TFTP_STATE_CLOSED = 0, |
Sergunb | 0:8918a71cdbe9 | 116 | TFTP_STATE_OPEN = 1, |
Sergunb | 0:8918a71cdbe9 | 117 | TFTP_STATE_READING = 2, |
Sergunb | 0:8918a71cdbe9 | 118 | TFTP_STATE_WRITING = 3, |
Sergunb | 0:8918a71cdbe9 | 119 | TFTP_STATE_READ_COMPLETE = 4, |
Sergunb | 0:8918a71cdbe9 | 120 | TFTP_STATE_WRITE_COMPLETE = 5 |
Sergunb | 0:8918a71cdbe9 | 121 | } TftpConnectionState; |
Sergunb | 0:8918a71cdbe9 | 122 | |
Sergunb | 0:8918a71cdbe9 | 123 | |
Sergunb | 0:8918a71cdbe9 | 124 | /** |
Sergunb | 0:8918a71cdbe9 | 125 | * @brief Open file callback function |
Sergunb | 0:8918a71cdbe9 | 126 | **/ |
Sergunb | 0:8918a71cdbe9 | 127 | |
Sergunb | 0:8918a71cdbe9 | 128 | typedef void *(*TftpServerOpenFileCallback)(const char_t *filename, |
Sergunb | 0:8918a71cdbe9 | 129 | const char_t *mode, bool_t writeAccess); |
Sergunb | 0:8918a71cdbe9 | 130 | |
Sergunb | 0:8918a71cdbe9 | 131 | |
Sergunb | 0:8918a71cdbe9 | 132 | /** |
Sergunb | 0:8918a71cdbe9 | 133 | * @brief Write file callback function |
Sergunb | 0:8918a71cdbe9 | 134 | **/ |
Sergunb | 0:8918a71cdbe9 | 135 | |
Sergunb | 0:8918a71cdbe9 | 136 | typedef error_t (*TftpServerWriteFileCallback)(void *file, |
Sergunb | 0:8918a71cdbe9 | 137 | size_t offset, const uint8_t *data, size_t length); |
Sergunb | 0:8918a71cdbe9 | 138 | |
Sergunb | 0:8918a71cdbe9 | 139 | |
Sergunb | 0:8918a71cdbe9 | 140 | /** |
Sergunb | 0:8918a71cdbe9 | 141 | * @brief Read file callback function |
Sergunb | 0:8918a71cdbe9 | 142 | **/ |
Sergunb | 0:8918a71cdbe9 | 143 | |
Sergunb | 0:8918a71cdbe9 | 144 | typedef error_t (*TftpServerReadFileCallback)(void *file, |
Sergunb | 0:8918a71cdbe9 | 145 | size_t offset, uint8_t *data, size_t size, size_t *length); |
Sergunb | 0:8918a71cdbe9 | 146 | |
Sergunb | 0:8918a71cdbe9 | 147 | |
Sergunb | 0:8918a71cdbe9 | 148 | /** |
Sergunb | 0:8918a71cdbe9 | 149 | * @brief Close file callback function |
Sergunb | 0:8918a71cdbe9 | 150 | **/ |
Sergunb | 0:8918a71cdbe9 | 151 | |
Sergunb | 0:8918a71cdbe9 | 152 | typedef void (*TftpServerCloseFileCallback)(void *file); |
Sergunb | 0:8918a71cdbe9 | 153 | |
Sergunb | 0:8918a71cdbe9 | 154 | |
Sergunb | 0:8918a71cdbe9 | 155 | /** |
Sergunb | 0:8918a71cdbe9 | 156 | * @brief TFTP server settings |
Sergunb | 0:8918a71cdbe9 | 157 | **/ |
Sergunb | 0:8918a71cdbe9 | 158 | |
Sergunb | 0:8918a71cdbe9 | 159 | typedef struct |
Sergunb | 0:8918a71cdbe9 | 160 | { |
Sergunb | 0:8918a71cdbe9 | 161 | NetInterface *interface; ///<Underlying network interface |
Sergunb | 0:8918a71cdbe9 | 162 | uint16_t port; ///<TFTP port number |
Sergunb | 0:8918a71cdbe9 | 163 | TftpServerOpenFileCallback openFileCallback; ///<Open file callback function |
Sergunb | 0:8918a71cdbe9 | 164 | TftpServerWriteFileCallback writeFileCallback; ///<Write file callback function |
Sergunb | 0:8918a71cdbe9 | 165 | TftpServerReadFileCallback readFileCallback; ///<Read file callback function |
Sergunb | 0:8918a71cdbe9 | 166 | TftpServerCloseFileCallback closeFileCallback; ///<Close file callback function |
Sergunb | 0:8918a71cdbe9 | 167 | } TftpServerSettings; |
Sergunb | 0:8918a71cdbe9 | 168 | |
Sergunb | 0:8918a71cdbe9 | 169 | |
Sergunb | 0:8918a71cdbe9 | 170 | /** |
Sergunb | 0:8918a71cdbe9 | 171 | * @brief TFTP client connection |
Sergunb | 0:8918a71cdbe9 | 172 | **/ |
Sergunb | 0:8918a71cdbe9 | 173 | |
Sergunb | 0:8918a71cdbe9 | 174 | struct _TftpClientConnection |
Sergunb | 0:8918a71cdbe9 | 175 | { |
Sergunb | 0:8918a71cdbe9 | 176 | TftpServerSettings *settings; ///<User settings |
Sergunb | 0:8918a71cdbe9 | 177 | TftpConnectionState state; ///<Connection state |
Sergunb | 0:8918a71cdbe9 | 178 | Socket *socket; ///<Underlying socket |
Sergunb | 0:8918a71cdbe9 | 179 | void *file; ///<File pointer |
Sergunb | 0:8918a71cdbe9 | 180 | uint16_t block; ///<Block number |
Sergunb | 0:8918a71cdbe9 | 181 | systime_t timestamp; ///<Time stamp to manage retransmissions |
Sergunb | 0:8918a71cdbe9 | 182 | uint_t retransmitCount; ///<Retransmission counter |
Sergunb | 0:8918a71cdbe9 | 183 | uint8_t packet[TFTP_SERVER_MAX_PACKET_SIZE]; ///<Outgoing TFTP packet |
Sergunb | 0:8918a71cdbe9 | 184 | size_t packetLen; ///<Length of the outgoing packet |
Sergunb | 0:8918a71cdbe9 | 185 | }; |
Sergunb | 0:8918a71cdbe9 | 186 | |
Sergunb | 0:8918a71cdbe9 | 187 | |
Sergunb | 0:8918a71cdbe9 | 188 | /** |
Sergunb | 0:8918a71cdbe9 | 189 | * @brief TFTP server context |
Sergunb | 0:8918a71cdbe9 | 190 | **/ |
Sergunb | 0:8918a71cdbe9 | 191 | |
Sergunb | 0:8918a71cdbe9 | 192 | struct _TftpServerContext |
Sergunb | 0:8918a71cdbe9 | 193 | { |
Sergunb | 0:8918a71cdbe9 | 194 | TftpServerSettings settings; ///<User settings |
Sergunb | 0:8918a71cdbe9 | 195 | OsEvent event; ///<Event object used to poll the sockets |
Sergunb | 0:8918a71cdbe9 | 196 | Socket *socket; ///<Listening socket |
Sergunb | 0:8918a71cdbe9 | 197 | TftpClientConnection connection[TFTP_SERVER_MAX_CONNECTIONS]; ///<Client connections |
Sergunb | 0:8918a71cdbe9 | 198 | SocketEventDesc eventDesc[TFTP_SERVER_MAX_CONNECTIONS + 1]; ///<The events the application is interested in |
Sergunb | 0:8918a71cdbe9 | 199 | uint8_t packet[TFTP_SERVER_MAX_PACKET_SIZE]; ///<Incoming TFTP packet |
Sergunb | 0:8918a71cdbe9 | 200 | }; |
Sergunb | 0:8918a71cdbe9 | 201 | |
Sergunb | 0:8918a71cdbe9 | 202 | |
Sergunb | 0:8918a71cdbe9 | 203 | //TFTP server related functions |
Sergunb | 0:8918a71cdbe9 | 204 | void tftpServerGetDefaultSettings(TftpServerSettings *settings); |
Sergunb | 0:8918a71cdbe9 | 205 | error_t tftpServerInit(TftpServerContext *context, const TftpServerSettings *settings); |
Sergunb | 0:8918a71cdbe9 | 206 | error_t tftpServerStart(TftpServerContext *context); |
Sergunb | 0:8918a71cdbe9 | 207 | |
Sergunb | 0:8918a71cdbe9 | 208 | void tftpServerTask(TftpServerContext *context); |
Sergunb | 0:8918a71cdbe9 | 209 | void tftpServerTick(TftpServerContext *context); |
Sergunb | 0:8918a71cdbe9 | 210 | |
Sergunb | 0:8918a71cdbe9 | 211 | void tftpServerAcceptRequest(TftpServerContext *context); |
Sergunb | 0:8918a71cdbe9 | 212 | |
Sergunb | 0:8918a71cdbe9 | 213 | void tftpServerProcessPacket(TftpServerContext *context, |
Sergunb | 0:8918a71cdbe9 | 214 | TftpClientConnection *connection); |
Sergunb | 0:8918a71cdbe9 | 215 | |
Sergunb | 0:8918a71cdbe9 | 216 | void tftpServerProcessRrqPacket(TftpServerContext *context, const IpAddr *clientIpAddr, |
Sergunb | 0:8918a71cdbe9 | 217 | uint16_t clientPort, const TftpRrqPacket *rrqPacket, size_t length); |
Sergunb | 0:8918a71cdbe9 | 218 | |
Sergunb | 0:8918a71cdbe9 | 219 | void tftpServerProcessWrqPacket(TftpServerContext *context, const IpAddr *clientIpAddr, |
Sergunb | 0:8918a71cdbe9 | 220 | uint16_t clientPort, const TftpWrqPacket *wrqPacket, size_t length); |
Sergunb | 0:8918a71cdbe9 | 221 | |
Sergunb | 0:8918a71cdbe9 | 222 | void tftpServerProcessDataPacket(TftpClientConnection *connection, |
Sergunb | 0:8918a71cdbe9 | 223 | const TftpDataPacket *dataPacket, size_t length); |
Sergunb | 0:8918a71cdbe9 | 224 | |
Sergunb | 0:8918a71cdbe9 | 225 | void tftpServerProcessAckPacket(TftpClientConnection *connection, |
Sergunb | 0:8918a71cdbe9 | 226 | const TftpAckPacket *ackPacket, size_t length); |
Sergunb | 0:8918a71cdbe9 | 227 | |
Sergunb | 0:8918a71cdbe9 | 228 | void tftpServerProcessErrorPacket(TftpClientConnection *connection, |
Sergunb | 0:8918a71cdbe9 | 229 | const TftpErrorPacket *errorPacket, size_t length); |
Sergunb | 0:8918a71cdbe9 | 230 | |
Sergunb | 0:8918a71cdbe9 | 231 | error_t tftpServerSendDataPacket(TftpClientConnection *connection); |
Sergunb | 0:8918a71cdbe9 | 232 | error_t tftpServerSendAckPacket(TftpClientConnection *connection); |
Sergunb | 0:8918a71cdbe9 | 233 | |
Sergunb | 0:8918a71cdbe9 | 234 | error_t tftpServerSendErrorPacket(TftpClientConnection *connection, |
Sergunb | 0:8918a71cdbe9 | 235 | uint16_t errorCode, const char_t *errorMsg); |
Sergunb | 0:8918a71cdbe9 | 236 | |
Sergunb | 0:8918a71cdbe9 | 237 | error_t tftpServerRetransmitPacket(TftpClientConnection *connection); |
Sergunb | 0:8918a71cdbe9 | 238 | |
Sergunb | 0:8918a71cdbe9 | 239 | TftpClientConnection *tftpServerOpenConnection(TftpServerContext *context, |
Sergunb | 0:8918a71cdbe9 | 240 | const IpAddr *clientIpAddr, uint16_t clientPort); |
Sergunb | 0:8918a71cdbe9 | 241 | |
Sergunb | 0:8918a71cdbe9 | 242 | void tftpServerCloseConnection(TftpClientConnection *connection); |
Sergunb | 0:8918a71cdbe9 | 243 | |
Sergunb | 0:8918a71cdbe9 | 244 | #endif |
Sergunb | 0:8918a71cdbe9 | 245 |