Zoltan Hudak / TFTPServer

Dependents:   TFTPServerTest

Fork of TFTPServer by Jaap Vermaas

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TFTPServer.h Source File

TFTPServer.h

00001 /*
00002  * TFTPServer.h
00003  * Simple TFTP server 
00004  *
00005  * Copyright (c) 2011 Jaap Vermaas
00006  * Modified by Zoltan Hudak 2018 for MBED-OS5
00007  *
00008  *   This file is part of the LaOS project (see: http://wiki.laoslaser.org
00009  *
00010  *   LaOS is free software: you can redistribute it and/or modify
00011  *   it under the terms of the GNU General Public License as published by
00012  *   the Free Software Foundation, either version 3 of the License, or
00013  *   (at your option) any later version.
00014  *
00015  *   LaOS is distributed in the hope that it will be useful,
00016  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *   GNU General Public License for more details.
00019  *
00020  *   You should have received a copy of the GNU General Public License
00021  *   along with LaOS.  If not, see <http://www.gnu.org/licenses/>.
00022  *
00023  * Minimal TFTP Server
00024  *      * Receive and send files via TFTP
00025  *      * Server handles only one transfer at a time
00026  *      * Supports only octet (raw 8 bit bytes) mode transfers
00027  *      * fixed block size: 512 bytes
00028  *
00029  * http://spectral.mscs.mu.edu/RFC/rfc1350.html
00030  *
00031  * Example:
00032  * @code 
00033  * TFTPServer *server;
00034  * ...
00035  * server = new TFTPServer();
00036  * ...
00037  * @endcode
00038  *
00039  */
00040 #ifndef _TFTPSERVER_H_
00041 #define _TFTPSERVER_H_
00042 
00043 #include <stdio.h>
00044 #include <ctype.h>
00045 #include "mbed.h"
00046 
00047 class EthernetInterface;
00048 
00049 #define TFTP_PORT   69
00050 
00051 class TFTPServer
00052 {
00053 public:
00054     enum State
00055     {
00056         LISTENING,
00057         READING,
00058         WRITING,
00059         ERROR,
00060         SUSPENDED,
00061         DELETED
00062     };
00063 
00064     // Creates a new TFTP server listening on myPort.
00065     TFTPServer(EthernetInterface* net, uint16_t myPort = TFTP_PORT);
00066     
00067     // Destroys this instance of the TFTP server.
00068     ~TFTPServer();
00069     
00070     // Resets the TFTP server.
00071     void            reset();
00072     
00073     // Gets current TFTP status
00074     State           getState();
00075     
00076     // Temporarily disables incoming TFTP connections.
00077     void            suspend();
00078     
00079     // Resumes incoming TFTP connections after suspension.
00080     void            resume();
00081     
00082     // Polls for data or new connection.
00083     void            poll();
00084     
00085     // Gets the filename during read and write. 
00086     void            getFileName(char* name);
00087     
00088     // Returns number of received files.
00089     int             fileCount();
00090     
00091 private:
00092     // Creates a new connection reading a file from server.
00093     void            connectRead(char* buff);
00094     
00095     // Creates a new connection writing a file to the server.
00096     void            connectWrite(char* buff);
00097     
00098     // Gets DATA block from file on disk into memory.
00099     void            getBlock();
00100     
00101     // Sends DATA block to remote client.
00102     void            sendBlock();
00103     
00104     // Compares host's IP and Port with connected remote machine.
00105     int             cmpHost();
00106     
00107     // Sends ACK to remote client.
00108     void            ack(int val);
00109     
00110     // Sends ERROR message to remote client.
00111     void            sendError(const char* msg);
00112     
00113     // Checks if connection mode of client is octet/binary.
00114     int             modeOctet(char* buff);
00115     
00116     uint16_t        port;                       // TFTP port
00117     UDPSocket*      socket;                     // Main listening socket (dflt: UDP port 69)
00118     State           state;                      // Current TFTP server state
00119     char*           remoteIP;                   // Connected remote Host IP
00120     int             remotePort;                 // Connected remote Host Port
00121     uint16_t        blockCounter, dupCounter;   // Block counter, and DUP counter
00122     FILE*           file;                       // Current file to read or write
00123     char            blockBuff[516];             // Current DATA block;
00124     int             blockSize;                  // Last DATA block size while sending
00125     char            fileName[256];              // Current (or most recent) filename
00126     int             fileCounter;                // Received file counter
00127     char            errorBuff[128];             // Error message buffer
00128     SocketAddress   socketAddr;                 // Socket's addres (used to get remote host's address)
00129 };
00130 #endif