Webserver+3d print

Dependents:   Nucleo

Committer:
Sergunb
Date:
Sat Feb 04 18:15:49 2017 +0000
Revision:
0:8918a71cdbe9
nothing else

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Sergunb 0:8918a71cdbe9 1 /**
Sergunb 0:8918a71cdbe9 2 * @file ftp_server_events.c
Sergunb 0:8918a71cdbe9 3 * @brief FTP server (event handlers)
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 //Switch to the appropriate trace level
Sergunb 0:8918a71cdbe9 30 #define TRACE_LEVEL FTP_TRACE_LEVEL
Sergunb 0:8918a71cdbe9 31
Sergunb 0:8918a71cdbe9 32 //Dependencies
Sergunb 0:8918a71cdbe9 33 #include "ftp/ftp_server.h"
Sergunb 0:8918a71cdbe9 34 #include "ftp/ftp_server_events.h"
Sergunb 0:8918a71cdbe9 35 #include "ftp/ftp_server_commands.h"
Sergunb 0:8918a71cdbe9 36 #include "ftp/ftp_server_misc.h"
Sergunb 0:8918a71cdbe9 37 #include "str.h"
Sergunb 0:8918a71cdbe9 38 #include "path.h"
Sergunb 0:8918a71cdbe9 39 #include "error.h"
Sergunb 0:8918a71cdbe9 40 #include "debug.h"
Sergunb 0:8918a71cdbe9 41
Sergunb 0:8918a71cdbe9 42 //Check TCP/IP stack configuration
Sergunb 0:8918a71cdbe9 43 #if (FTP_SERVER_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 44
Sergunb 0:8918a71cdbe9 45 //Abbreviated months
Sergunb 0:8918a71cdbe9 46 static const char months[13][4] =
Sergunb 0:8918a71cdbe9 47 {
Sergunb 0:8918a71cdbe9 48 " ",
Sergunb 0:8918a71cdbe9 49 "Jan",
Sergunb 0:8918a71cdbe9 50 "Feb",
Sergunb 0:8918a71cdbe9 51 "Mar",
Sergunb 0:8918a71cdbe9 52 "Apr",
Sergunb 0:8918a71cdbe9 53 "May",
Sergunb 0:8918a71cdbe9 54 "Jun",
Sergunb 0:8918a71cdbe9 55 "Jul",
Sergunb 0:8918a71cdbe9 56 "Aug",
Sergunb 0:8918a71cdbe9 57 "Sep",
Sergunb 0:8918a71cdbe9 58 "Oct",
Sergunb 0:8918a71cdbe9 59 "Nov",
Sergunb 0:8918a71cdbe9 60 "Dec"
Sergunb 0:8918a71cdbe9 61 };
Sergunb 0:8918a71cdbe9 62
Sergunb 0:8918a71cdbe9 63
Sergunb 0:8918a71cdbe9 64 /**
Sergunb 0:8918a71cdbe9 65 * @brief Control connection event handler
Sergunb 0:8918a71cdbe9 66 * @param[in] context Pointer to the FTP server context
Sergunb 0:8918a71cdbe9 67 * @param[in] connection Pointer to the client connection
Sergunb 0:8918a71cdbe9 68 * @param[in] eventFlags Event to be processed
Sergunb 0:8918a71cdbe9 69 **/
Sergunb 0:8918a71cdbe9 70
Sergunb 0:8918a71cdbe9 71 void ftpServerControlEventHandler(FtpServerContext *context,
Sergunb 0:8918a71cdbe9 72 FtpClientConnection * connection, uint_t eventFlags)
Sergunb 0:8918a71cdbe9 73 {
Sergunb 0:8918a71cdbe9 74 error_t error;
Sergunb 0:8918a71cdbe9 75 size_t n;
Sergunb 0:8918a71cdbe9 76
Sergunb 0:8918a71cdbe9 77 //Send buffer is available for writing?
Sergunb 0:8918a71cdbe9 78 if(eventFlags == SOCKET_EVENT_TX_READY)
Sergunb 0:8918a71cdbe9 79 {
Sergunb 0:8918a71cdbe9 80 //Send data back to the client
Sergunb 0:8918a71cdbe9 81 error = socketSend(connection->controlSocket, connection->response +
Sergunb 0:8918a71cdbe9 82 connection->responsePos, connection->responseLength, &n, 0);
Sergunb 0:8918a71cdbe9 83
Sergunb 0:8918a71cdbe9 84 //Failed to send data?
Sergunb 0:8918a71cdbe9 85 if(error != NO_ERROR && error != ERROR_TIMEOUT)
Sergunb 0:8918a71cdbe9 86 {
Sergunb 0:8918a71cdbe9 87 //Close connection with the client
Sergunb 0:8918a71cdbe9 88 ftpServerCloseConnection(context, connection);
Sergunb 0:8918a71cdbe9 89 //Exit immediately
Sergunb 0:8918a71cdbe9 90 return;
Sergunb 0:8918a71cdbe9 91 }
Sergunb 0:8918a71cdbe9 92
Sergunb 0:8918a71cdbe9 93 //Advance data pointer
Sergunb 0:8918a71cdbe9 94 connection->responsePos += n;
Sergunb 0:8918a71cdbe9 95 //Number of bytes still available in the response buffer
Sergunb 0:8918a71cdbe9 96 connection->responseLength -= n;
Sergunb 0:8918a71cdbe9 97 }
Sergunb 0:8918a71cdbe9 98 //Data is pending in the receive buffer?
Sergunb 0:8918a71cdbe9 99 else if(eventFlags == SOCKET_EVENT_RX_READY)
Sergunb 0:8918a71cdbe9 100 {
Sergunb 0:8918a71cdbe9 101 //Read data from the client
Sergunb 0:8918a71cdbe9 102 error = socketReceive(connection->controlSocket,
Sergunb 0:8918a71cdbe9 103 connection->command + connection->commandLength,
Sergunb 0:8918a71cdbe9 104 FTP_SERVER_MAX_LINE_LEN - connection->commandLength, &n, 0);
Sergunb 0:8918a71cdbe9 105
Sergunb 0:8918a71cdbe9 106 //Failed to receive data?
Sergunb 0:8918a71cdbe9 107 if(error == ERROR_END_OF_STREAM)
Sergunb 0:8918a71cdbe9 108 {
Sergunb 0:8918a71cdbe9 109 //Gracefully disconnect from the remote host
Sergunb 0:8918a71cdbe9 110 connection->controlState = FTP_CONTROL_STATE_WAIT_ACK;
Sergunb 0:8918a71cdbe9 111 //Exit immediately
Sergunb 0:8918a71cdbe9 112 return;
Sergunb 0:8918a71cdbe9 113 }
Sergunb 0:8918a71cdbe9 114 else if(error)
Sergunb 0:8918a71cdbe9 115 {
Sergunb 0:8918a71cdbe9 116 //Close connection with the client
Sergunb 0:8918a71cdbe9 117 ftpServerCloseConnection(context, connection);
Sergunb 0:8918a71cdbe9 118 //Exit immediately
Sergunb 0:8918a71cdbe9 119 return;
Sergunb 0:8918a71cdbe9 120 }
Sergunb 0:8918a71cdbe9 121
Sergunb 0:8918a71cdbe9 122 //Number of bytes available in the command buffer
Sergunb 0:8918a71cdbe9 123 connection->commandLength += n;
Sergunb 0:8918a71cdbe9 124 //Process incoming command
Sergunb 0:8918a71cdbe9 125 ftpServerProcessCmd(context, connection);
Sergunb 0:8918a71cdbe9 126 }
Sergunb 0:8918a71cdbe9 127 //Data are transmitted and acknowledged?
Sergunb 0:8918a71cdbe9 128 else if(eventFlags == SOCKET_EVENT_TX_ACKED)
Sergunb 0:8918a71cdbe9 129 {
Sergunb 0:8918a71cdbe9 130 //Disable transmission
Sergunb 0:8918a71cdbe9 131 socketShutdown(connection->controlSocket, SOCKET_SD_SEND);
Sergunb 0:8918a71cdbe9 132 //Next state
Sergunb 0:8918a71cdbe9 133 connection->controlState = FTP_CONTROL_STATE_SHUTDOWN_TX;
Sergunb 0:8918a71cdbe9 134 }
Sergunb 0:8918a71cdbe9 135 //Transmission is shut down?
Sergunb 0:8918a71cdbe9 136 else if(eventFlags == SOCKET_EVENT_TX_SHUTDOWN)
Sergunb 0:8918a71cdbe9 137 {
Sergunb 0:8918a71cdbe9 138 //Disable reception
Sergunb 0:8918a71cdbe9 139 socketShutdown(connection->controlSocket, SOCKET_SD_RECEIVE);
Sergunb 0:8918a71cdbe9 140 //Next state
Sergunb 0:8918a71cdbe9 141 connection->controlState = FTP_CONTROL_STATE_SHUTDOWN_RX;
Sergunb 0:8918a71cdbe9 142 }
Sergunb 0:8918a71cdbe9 143 //Reception is shut down?
Sergunb 0:8918a71cdbe9 144 else if(eventFlags == SOCKET_EVENT_RX_SHUTDOWN)
Sergunb 0:8918a71cdbe9 145 {
Sergunb 0:8918a71cdbe9 146 //Properly close connection
Sergunb 0:8918a71cdbe9 147 ftpServerCloseConnection(context, connection);
Sergunb 0:8918a71cdbe9 148 }
Sergunb 0:8918a71cdbe9 149 }
Sergunb 0:8918a71cdbe9 150
Sergunb 0:8918a71cdbe9 151
Sergunb 0:8918a71cdbe9 152 /**
Sergunb 0:8918a71cdbe9 153 * @brief Data connection event handler
Sergunb 0:8918a71cdbe9 154 * @param[in] context Pointer to the FTP server context
Sergunb 0:8918a71cdbe9 155 * @param[in] connection Pointer to the client connection
Sergunb 0:8918a71cdbe9 156 * @param[in] eventFlags Event to be processed
Sergunb 0:8918a71cdbe9 157 **/
Sergunb 0:8918a71cdbe9 158
Sergunb 0:8918a71cdbe9 159 void ftpServerDataEventHandler(FtpServerContext *context,
Sergunb 0:8918a71cdbe9 160 FtpClientConnection * connection, uint_t eventFlags)
Sergunb 0:8918a71cdbe9 161 {
Sergunb 0:8918a71cdbe9 162 //Any connection attempt?
Sergunb 0:8918a71cdbe9 163 if(connection->dataState == FTP_DATA_STATE_LISTEN)
Sergunb 0:8918a71cdbe9 164 {
Sergunb 0:8918a71cdbe9 165 //Accept data connection
Sergunb 0:8918a71cdbe9 166 ftpServerAcceptDataConnection(connection);
Sergunb 0:8918a71cdbe9 167 }
Sergunb 0:8918a71cdbe9 168 //Ready to send data?
Sergunb 0:8918a71cdbe9 169 else if(connection->dataState == FTP_DATA_STATE_SEND)
Sergunb 0:8918a71cdbe9 170 {
Sergunb 0:8918a71cdbe9 171 //Send more data to the remote host
Sergunb 0:8918a71cdbe9 172 ftpServerSendData(context, connection);
Sergunb 0:8918a71cdbe9 173 }
Sergunb 0:8918a71cdbe9 174 //Any data pending in the receive buffer?
Sergunb 0:8918a71cdbe9 175 else if(connection->dataState == FTP_DATA_STATE_RECEIVE)
Sergunb 0:8918a71cdbe9 176 {
Sergunb 0:8918a71cdbe9 177 //Process incoming data
Sergunb 0:8918a71cdbe9 178 ftpServerReceiveData(context, connection);
Sergunb 0:8918a71cdbe9 179 }
Sergunb 0:8918a71cdbe9 180 //Data are transmitted and acknowledged?
Sergunb 0:8918a71cdbe9 181 else if(connection->dataState == FTP_DATA_STATE_WAIT_ACK)
Sergunb 0:8918a71cdbe9 182 {
Sergunb 0:8918a71cdbe9 183 //Disable transmission
Sergunb 0:8918a71cdbe9 184 socketShutdown(connection->dataSocket, SOCKET_SD_SEND);
Sergunb 0:8918a71cdbe9 185 //Next state
Sergunb 0:8918a71cdbe9 186 connection->dataState = FTP_DATA_STATE_SHUTDOWN_TX;
Sergunb 0:8918a71cdbe9 187 }
Sergunb 0:8918a71cdbe9 188 //Transmission is shut down?
Sergunb 0:8918a71cdbe9 189 else if(connection->dataState == FTP_DATA_STATE_SHUTDOWN_TX)
Sergunb 0:8918a71cdbe9 190 {
Sergunb 0:8918a71cdbe9 191 //Disable reception
Sergunb 0:8918a71cdbe9 192 socketShutdown(connection->dataSocket, SOCKET_SD_RECEIVE);
Sergunb 0:8918a71cdbe9 193 //Next state
Sergunb 0:8918a71cdbe9 194 connection->dataState = FTP_DATA_STATE_SHUTDOWN_RX;
Sergunb 0:8918a71cdbe9 195 }
Sergunb 0:8918a71cdbe9 196 //Reception is shut down?
Sergunb 0:8918a71cdbe9 197 else if(connection->dataState == FTP_DATA_STATE_SHUTDOWN_RX)
Sergunb 0:8918a71cdbe9 198 {
Sergunb 0:8918a71cdbe9 199 //Close the data connection
Sergunb 0:8918a71cdbe9 200 ftpServerCloseDataConnection(connection);
Sergunb 0:8918a71cdbe9 201
Sergunb 0:8918a71cdbe9 202 //Back to idle state
Sergunb 0:8918a71cdbe9 203 connection->controlState = FTP_CONTROL_STATE_IDLE;
Sergunb 0:8918a71cdbe9 204
Sergunb 0:8918a71cdbe9 205 //Transfer status
Sergunb 0:8918a71cdbe9 206 strcpy(connection->response, "226 Transfer complete\r\n");
Sergunb 0:8918a71cdbe9 207 //Debug message
Sergunb 0:8918a71cdbe9 208 TRACE_DEBUG("FTP server: %s", connection->response);
Sergunb 0:8918a71cdbe9 209
Sergunb 0:8918a71cdbe9 210 //Number of bytes in the response buffer
Sergunb 0:8918a71cdbe9 211 connection->responseLength = strlen(connection->response);
Sergunb 0:8918a71cdbe9 212 connection->responsePos = 0;
Sergunb 0:8918a71cdbe9 213 }
Sergunb 0:8918a71cdbe9 214 }
Sergunb 0:8918a71cdbe9 215
Sergunb 0:8918a71cdbe9 216
Sergunb 0:8918a71cdbe9 217 /**
Sergunb 0:8918a71cdbe9 218 * @brief Send data on the data connection
Sergunb 0:8918a71cdbe9 219 * @param[in] context Pointer to the FTP server context
Sergunb 0:8918a71cdbe9 220 * @param[in] connection Pointer to the client connection
Sergunb 0:8918a71cdbe9 221 **/
Sergunb 0:8918a71cdbe9 222
Sergunb 0:8918a71cdbe9 223 void ftpServerSendData(FtpServerContext *context, FtpClientConnection *connection)
Sergunb 0:8918a71cdbe9 224 {
Sergunb 0:8918a71cdbe9 225 error_t error;
Sergunb 0:8918a71cdbe9 226 size_t n;
Sergunb 0:8918a71cdbe9 227
Sergunb 0:8918a71cdbe9 228 //Any data waiting for transmission?
Sergunb 0:8918a71cdbe9 229 if(connection->bufferLength > 0)
Sergunb 0:8918a71cdbe9 230 {
Sergunb 0:8918a71cdbe9 231 //Send more data
Sergunb 0:8918a71cdbe9 232 error = socketSend(connection->dataSocket, connection->buffer +
Sergunb 0:8918a71cdbe9 233 connection->bufferPos, connection->bufferLength, &n, 0);
Sergunb 0:8918a71cdbe9 234
Sergunb 0:8918a71cdbe9 235 //Failed to send data?
Sergunb 0:8918a71cdbe9 236 if(error != NO_ERROR && error != ERROR_TIMEOUT)
Sergunb 0:8918a71cdbe9 237 {
Sergunb 0:8918a71cdbe9 238 //Close the data connection
Sergunb 0:8918a71cdbe9 239 ftpServerCloseDataConnection(connection);
Sergunb 0:8918a71cdbe9 240
Sergunb 0:8918a71cdbe9 241 //Release previously allocated resources
Sergunb 0:8918a71cdbe9 242 if(connection->file != NULL)
Sergunb 0:8918a71cdbe9 243 {
Sergunb 0:8918a71cdbe9 244 fsCloseFile(connection->file);
Sergunb 0:8918a71cdbe9 245 connection->file = NULL;
Sergunb 0:8918a71cdbe9 246 }
Sergunb 0:8918a71cdbe9 247 if(connection->dir != NULL)
Sergunb 0:8918a71cdbe9 248 {
Sergunb 0:8918a71cdbe9 249 fsCloseDir(connection->dir);
Sergunb 0:8918a71cdbe9 250 connection->dir = NULL;
Sergunb 0:8918a71cdbe9 251 }
Sergunb 0:8918a71cdbe9 252
Sergunb 0:8918a71cdbe9 253 //Back to idle state
Sergunb 0:8918a71cdbe9 254 connection->controlState = FTP_CONTROL_STATE_IDLE;
Sergunb 0:8918a71cdbe9 255
Sergunb 0:8918a71cdbe9 256 //Transfer status
Sergunb 0:8918a71cdbe9 257 strcpy(connection->response, "451 Transfer aborted\r\n");
Sergunb 0:8918a71cdbe9 258 //Debug message
Sergunb 0:8918a71cdbe9 259 TRACE_DEBUG("FTP server: %s", connection->response);
Sergunb 0:8918a71cdbe9 260
Sergunb 0:8918a71cdbe9 261 //Number of bytes in the response buffer
Sergunb 0:8918a71cdbe9 262 connection->responseLength = strlen(connection->response);
Sergunb 0:8918a71cdbe9 263 connection->responsePos = 0;
Sergunb 0:8918a71cdbe9 264
Sergunb 0:8918a71cdbe9 265 //Exit immediately
Sergunb 0:8918a71cdbe9 266 return;
Sergunb 0:8918a71cdbe9 267 }
Sergunb 0:8918a71cdbe9 268
Sergunb 0:8918a71cdbe9 269 //Advance data pointer
Sergunb 0:8918a71cdbe9 270 connection->bufferPos += n;
Sergunb 0:8918a71cdbe9 271 //Number of bytes still available in the buffer
Sergunb 0:8918a71cdbe9 272 connection->bufferLength -= n;
Sergunb 0:8918a71cdbe9 273 }
Sergunb 0:8918a71cdbe9 274
Sergunb 0:8918a71cdbe9 275 //Empty transmission buffer?
Sergunb 0:8918a71cdbe9 276 if(connection->bufferLength == 0)
Sergunb 0:8918a71cdbe9 277 {
Sergunb 0:8918a71cdbe9 278 //File transfer in progress?
Sergunb 0:8918a71cdbe9 279 if(connection->controlState == FTP_CONTROL_STATE_RETR)
Sergunb 0:8918a71cdbe9 280 {
Sergunb 0:8918a71cdbe9 281 //Read more data
Sergunb 0:8918a71cdbe9 282 error = fsReadFile(connection->file,
Sergunb 0:8918a71cdbe9 283 connection->buffer, FTP_SERVER_BUFFER_SIZE, &n);
Sergunb 0:8918a71cdbe9 284
Sergunb 0:8918a71cdbe9 285 //End of stream?
Sergunb 0:8918a71cdbe9 286 if(error)
Sergunb 0:8918a71cdbe9 287 {
Sergunb 0:8918a71cdbe9 288 //Close file
Sergunb 0:8918a71cdbe9 289 fsCloseFile(connection->file);
Sergunb 0:8918a71cdbe9 290 connection->file = NULL;
Sergunb 0:8918a71cdbe9 291
Sergunb 0:8918a71cdbe9 292 //Wait for all the data to be transmitted and acknowledged
Sergunb 0:8918a71cdbe9 293 connection->dataState = FTP_DATA_STATE_WAIT_ACK;
Sergunb 0:8918a71cdbe9 294
Sergunb 0:8918a71cdbe9 295 //Exit immediately
Sergunb 0:8918a71cdbe9 296 return;
Sergunb 0:8918a71cdbe9 297 }
Sergunb 0:8918a71cdbe9 298 }
Sergunb 0:8918a71cdbe9 299 //Directory listing in progress?
Sergunb 0:8918a71cdbe9 300 else if(connection->controlState == FTP_CONTROL_STATE_LIST)
Sergunb 0:8918a71cdbe9 301 {
Sergunb 0:8918a71cdbe9 302 uint_t perm;
Sergunb 0:8918a71cdbe9 303 time_t currentTime;
Sergunb 0:8918a71cdbe9 304 time_t modified;
Sergunb 0:8918a71cdbe9 305 char_t *path;
Sergunb 0:8918a71cdbe9 306 FsDirEntry dirEntry;
Sergunb 0:8918a71cdbe9 307
Sergunb 0:8918a71cdbe9 308 //Read a new entry in the directory
Sergunb 0:8918a71cdbe9 309 error = fsReadDir(connection->dir, &dirEntry);
Sergunb 0:8918a71cdbe9 310
Sergunb 0:8918a71cdbe9 311 //End of stream?
Sergunb 0:8918a71cdbe9 312 if(error)
Sergunb 0:8918a71cdbe9 313 {
Sergunb 0:8918a71cdbe9 314 //Close directory
Sergunb 0:8918a71cdbe9 315 fsCloseDir(connection->dir);
Sergunb 0:8918a71cdbe9 316 connection->dir = NULL;
Sergunb 0:8918a71cdbe9 317
Sergunb 0:8918a71cdbe9 318 //Wait for all the data to be transmitted and acknowledged
Sergunb 0:8918a71cdbe9 319 connection->dataState = FTP_DATA_STATE_WAIT_ACK;
Sergunb 0:8918a71cdbe9 320
Sergunb 0:8918a71cdbe9 321 //Exit immediately
Sergunb 0:8918a71cdbe9 322 return;
Sergunb 0:8918a71cdbe9 323 }
Sergunb 0:8918a71cdbe9 324
Sergunb 0:8918a71cdbe9 325 //Point to the scratch buffer
Sergunb 0:8918a71cdbe9 326 path = connection->buffer;
Sergunb 0:8918a71cdbe9 327
Sergunb 0:8918a71cdbe9 328 //Get the pathname of the directory being listed
Sergunb 0:8918a71cdbe9 329 strcpy(path, connection->path);
Sergunb 0:8918a71cdbe9 330 //Retrieve the full pathname
Sergunb 0:8918a71cdbe9 331 pathCombine(path, dirEntry.name, FTP_SERVER_MAX_PATH_LEN);
Sergunb 0:8918a71cdbe9 332 pathCanonicalize(path);
Sergunb 0:8918a71cdbe9 333
Sergunb 0:8918a71cdbe9 334 //Get permissions for the specified file
Sergunb 0:8918a71cdbe9 335 perm = ftpServerGetFilePermissions(context, connection, path);
Sergunb 0:8918a71cdbe9 336
Sergunb 0:8918a71cdbe9 337 //Enforce access rights
Sergunb 0:8918a71cdbe9 338 if(perm & FTP_FILE_PERM_LIST)
Sergunb 0:8918a71cdbe9 339 {
Sergunb 0:8918a71cdbe9 340 //Format links, owner, group and size fields
Sergunb 0:8918a71cdbe9 341 n = sprintf(connection->buffer, "---------- 1 owner group %10" PRIu32,
Sergunb 0:8918a71cdbe9 342 dirEntry.size);
Sergunb 0:8918a71cdbe9 343
Sergunb 0:8918a71cdbe9 344 //Check whether the current entry is a directory
Sergunb 0:8918a71cdbe9 345 if(dirEntry.attributes & FS_FILE_ATTR_DIRECTORY)
Sergunb 0:8918a71cdbe9 346 connection->buffer[0] = 'd';
Sergunb 0:8918a71cdbe9 347
Sergunb 0:8918a71cdbe9 348 //Read access permitted?
Sergunb 0:8918a71cdbe9 349 if(perm & FTP_FILE_PERM_READ)
Sergunb 0:8918a71cdbe9 350 {
Sergunb 0:8918a71cdbe9 351 connection->buffer[1] = 'r';
Sergunb 0:8918a71cdbe9 352 connection->buffer[4] = 'r';
Sergunb 0:8918a71cdbe9 353 connection->buffer[7] = 'r';
Sergunb 0:8918a71cdbe9 354 }
Sergunb 0:8918a71cdbe9 355
Sergunb 0:8918a71cdbe9 356 //Write access permitted?
Sergunb 0:8918a71cdbe9 357 if(perm & FTP_FILE_PERM_WRITE)
Sergunb 0:8918a71cdbe9 358 {
Sergunb 0:8918a71cdbe9 359 //Make sure the file is not marked as read-only
Sergunb 0:8918a71cdbe9 360 if(!(dirEntry.attributes & FS_FILE_ATTR_READ_ONLY))
Sergunb 0:8918a71cdbe9 361 {
Sergunb 0:8918a71cdbe9 362 connection->buffer[2] = 'w';
Sergunb 0:8918a71cdbe9 363 connection->buffer[5] = 'w';
Sergunb 0:8918a71cdbe9 364 connection->buffer[8] = 'w';
Sergunb 0:8918a71cdbe9 365 }
Sergunb 0:8918a71cdbe9 366 }
Sergunb 0:8918a71cdbe9 367
Sergunb 0:8918a71cdbe9 368 //Get current time
Sergunb 0:8918a71cdbe9 369 currentTime = getCurrentUnixTime();
Sergunb 0:8918a71cdbe9 370 //Get modification time
Sergunb 0:8918a71cdbe9 371 modified = convertDateToUnixTime(&dirEntry.modified);
Sergunb 0:8918a71cdbe9 372
Sergunb 0:8918a71cdbe9 373 //Check whether the modification time is within the previous 180 days
Sergunb 0:8918a71cdbe9 374 if(currentTime > modified && currentTime < (modified + FTP_SERVER_180_DAYS))
Sergunb 0:8918a71cdbe9 375 {
Sergunb 0:8918a71cdbe9 376 //The format of the date/time field is Mmm dd hh:mm
Sergunb 0:8918a71cdbe9 377 n += sprintf(connection->buffer + n, " %s %02" PRIu8 " %02" PRIu8 ":%02" PRIu8,
Sergunb 0:8918a71cdbe9 378 months[MIN(dirEntry.modified.month, 12)], dirEntry.modified.day,
Sergunb 0:8918a71cdbe9 379 dirEntry.modified.hours, dirEntry.modified.minutes);
Sergunb 0:8918a71cdbe9 380 }
Sergunb 0:8918a71cdbe9 381 else
Sergunb 0:8918a71cdbe9 382 {
Sergunb 0:8918a71cdbe9 383 //The format of the date/time field is Mmm dd yyyy
Sergunb 0:8918a71cdbe9 384 n += sprintf(connection->buffer + n, " %s %02" PRIu8 " %04" PRIu16,
Sergunb 0:8918a71cdbe9 385 months[MIN(dirEntry.modified.month, 12)], dirEntry.modified.day,
Sergunb 0:8918a71cdbe9 386 dirEntry.modified.year);
Sergunb 0:8918a71cdbe9 387 }
Sergunb 0:8918a71cdbe9 388
Sergunb 0:8918a71cdbe9 389 //Append filename
Sergunb 0:8918a71cdbe9 390 n += sprintf(connection->buffer + n, " %s\r\n", dirEntry.name);
Sergunb 0:8918a71cdbe9 391 //Debug message
Sergunb 0:8918a71cdbe9 392 TRACE_DEBUG("FTP server: %s", connection->buffer);
Sergunb 0:8918a71cdbe9 393 }
Sergunb 0:8918a71cdbe9 394 else
Sergunb 0:8918a71cdbe9 395 {
Sergunb 0:8918a71cdbe9 396 //Insufficient access rights
Sergunb 0:8918a71cdbe9 397 n = 0;
Sergunb 0:8918a71cdbe9 398 }
Sergunb 0:8918a71cdbe9 399 }
Sergunb 0:8918a71cdbe9 400 //Invalid state?
Sergunb 0:8918a71cdbe9 401 else
Sergunb 0:8918a71cdbe9 402 {
Sergunb 0:8918a71cdbe9 403 //The FTP server has encountered a critical error
Sergunb 0:8918a71cdbe9 404 ftpServerCloseConnection(context, connection);
Sergunb 0:8918a71cdbe9 405 //Exit immediately
Sergunb 0:8918a71cdbe9 406 return;
Sergunb 0:8918a71cdbe9 407 }
Sergunb 0:8918a71cdbe9 408
Sergunb 0:8918a71cdbe9 409 //Number of bytes in the buffer
Sergunb 0:8918a71cdbe9 410 connection->bufferPos = 0;
Sergunb 0:8918a71cdbe9 411 connection->bufferLength = n;
Sergunb 0:8918a71cdbe9 412 }
Sergunb 0:8918a71cdbe9 413 }
Sergunb 0:8918a71cdbe9 414
Sergunb 0:8918a71cdbe9 415
Sergunb 0:8918a71cdbe9 416 /**
Sergunb 0:8918a71cdbe9 417 * @brief Receive data on the data connection
Sergunb 0:8918a71cdbe9 418 * @param[in] context Pointer to the FTP server context
Sergunb 0:8918a71cdbe9 419 * @param[in] connection Pointer to the client connection
Sergunb 0:8918a71cdbe9 420 **/
Sergunb 0:8918a71cdbe9 421
Sergunb 0:8918a71cdbe9 422 void ftpServerReceiveData(FtpServerContext *context, FtpClientConnection *connection)
Sergunb 0:8918a71cdbe9 423 {
Sergunb 0:8918a71cdbe9 424 error_t error;
Sergunb 0:8918a71cdbe9 425 bool_t eof;
Sergunb 0:8918a71cdbe9 426 size_t n;
Sergunb 0:8918a71cdbe9 427
Sergunb 0:8918a71cdbe9 428 //File transfer in progress?
Sergunb 0:8918a71cdbe9 429 if(connection->controlState == FTP_CONTROL_STATE_STOR ||
Sergunb 0:8918a71cdbe9 430 connection->controlState == FTP_CONTROL_STATE_APPE)
Sergunb 0:8918a71cdbe9 431 {
Sergunb 0:8918a71cdbe9 432 //Read incoming data
Sergunb 0:8918a71cdbe9 433 error = socketReceive(connection->dataSocket,
Sergunb 0:8918a71cdbe9 434 connection->buffer + connection->bufferPos,
Sergunb 0:8918a71cdbe9 435 FTP_SERVER_BUFFER_SIZE - connection->bufferLength, &n, 0);
Sergunb 0:8918a71cdbe9 436
Sergunb 0:8918a71cdbe9 437 //Any error to report?
Sergunb 0:8918a71cdbe9 438 if(error)
Sergunb 0:8918a71cdbe9 439 {
Sergunb 0:8918a71cdbe9 440 //Cannot read more data
Sergunb 0:8918a71cdbe9 441 eof = TRUE;
Sergunb 0:8918a71cdbe9 442 }
Sergunb 0:8918a71cdbe9 443 else
Sergunb 0:8918a71cdbe9 444 {
Sergunb 0:8918a71cdbe9 445 //Successful read operation
Sergunb 0:8918a71cdbe9 446 eof = FALSE;
Sergunb 0:8918a71cdbe9 447
Sergunb 0:8918a71cdbe9 448 //Advance data pointer
Sergunb 0:8918a71cdbe9 449 connection->bufferPos += n;
Sergunb 0:8918a71cdbe9 450 connection->bufferLength += n;
Sergunb 0:8918a71cdbe9 451 }
Sergunb 0:8918a71cdbe9 452
Sergunb 0:8918a71cdbe9 453 //Read data until the buffer is full or the end of the file is reached
Sergunb 0:8918a71cdbe9 454 if(eof || connection->bufferLength >= FTP_SERVER_BUFFER_SIZE)
Sergunb 0:8918a71cdbe9 455 {
Sergunb 0:8918a71cdbe9 456 //Any data to be written?
Sergunb 0:8918a71cdbe9 457 if(connection->bufferLength > 0)
Sergunb 0:8918a71cdbe9 458 {
Sergunb 0:8918a71cdbe9 459 //Write data to the specified file
Sergunb 0:8918a71cdbe9 460 error = fsWriteFile(connection->file,
Sergunb 0:8918a71cdbe9 461 connection->buffer, connection->bufferLength);
Sergunb 0:8918a71cdbe9 462
Sergunb 0:8918a71cdbe9 463 //Any error to report?
Sergunb 0:8918a71cdbe9 464 if(error)
Sergunb 0:8918a71cdbe9 465 {
Sergunb 0:8918a71cdbe9 466 //Close the data connection
Sergunb 0:8918a71cdbe9 467 ftpServerCloseDataConnection(connection);
Sergunb 0:8918a71cdbe9 468
Sergunb 0:8918a71cdbe9 469 //Release previously allocated resources
Sergunb 0:8918a71cdbe9 470 fsCloseFile(connection->file);
Sergunb 0:8918a71cdbe9 471 connection->file = NULL;
Sergunb 0:8918a71cdbe9 472
Sergunb 0:8918a71cdbe9 473 //Back to idle state
Sergunb 0:8918a71cdbe9 474 connection->controlState = FTP_CONTROL_STATE_IDLE;
Sergunb 0:8918a71cdbe9 475
Sergunb 0:8918a71cdbe9 476 //Transfer status
Sergunb 0:8918a71cdbe9 477 strcpy(connection->response, "451 Transfer aborted\r\n");
Sergunb 0:8918a71cdbe9 478 //Debug message
Sergunb 0:8918a71cdbe9 479 TRACE_DEBUG("FTP server: %s", connection->response);
Sergunb 0:8918a71cdbe9 480
Sergunb 0:8918a71cdbe9 481 //Number of bytes in the response buffer
Sergunb 0:8918a71cdbe9 482 connection->responseLength = strlen(connection->response);
Sergunb 0:8918a71cdbe9 483 connection->responsePos = 0;
Sergunb 0:8918a71cdbe9 484
Sergunb 0:8918a71cdbe9 485 //Exit immediately
Sergunb 0:8918a71cdbe9 486 return;
Sergunb 0:8918a71cdbe9 487 }
Sergunb 0:8918a71cdbe9 488 }
Sergunb 0:8918a71cdbe9 489
Sergunb 0:8918a71cdbe9 490 //Flush reception buffer
Sergunb 0:8918a71cdbe9 491 connection->bufferLength = 0;
Sergunb 0:8918a71cdbe9 492 connection->bufferPos = 0;
Sergunb 0:8918a71cdbe9 493 }
Sergunb 0:8918a71cdbe9 494
Sergunb 0:8918a71cdbe9 495 //End of stream?
Sergunb 0:8918a71cdbe9 496 if(eof)
Sergunb 0:8918a71cdbe9 497 {
Sergunb 0:8918a71cdbe9 498 //Close file
Sergunb 0:8918a71cdbe9 499 fsCloseFile(connection->file);
Sergunb 0:8918a71cdbe9 500 connection->file = NULL;
Sergunb 0:8918a71cdbe9 501
Sergunb 0:8918a71cdbe9 502 //Graceful shutdown sequence
Sergunb 0:8918a71cdbe9 503 connection->dataState = FTP_DATA_STATE_WAIT_ACK;
Sergunb 0:8918a71cdbe9 504 }
Sergunb 0:8918a71cdbe9 505 }
Sergunb 0:8918a71cdbe9 506 //Invalid state?
Sergunb 0:8918a71cdbe9 507 else
Sergunb 0:8918a71cdbe9 508 {
Sergunb 0:8918a71cdbe9 509 //The FTP server has encountered a critical error
Sergunb 0:8918a71cdbe9 510 ftpServerCloseConnection(context, connection);
Sergunb 0:8918a71cdbe9 511 }
Sergunb 0:8918a71cdbe9 512 }
Sergunb 0:8918a71cdbe9 513
Sergunb 0:8918a71cdbe9 514 #endif
Sergunb 0:8918a71cdbe9 515