This is a simple but complete TFTP server, It can receive and send files and listens on port 69. It is part of the LaOS (Laser Open Source) project: http://www.laoslaser.org/

Dependents:   TFTPServerTest RMFWeb

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  *
00007  *   This file is part of the LaOS project (see: http://wiki.laoslaser.org
00008  *
00009  *   LaOS is free software: you can redistribute it and/or modify
00010  *   it under the terms of the GNU General Public License as published by
00011  *   the Free Software Foundation, either version 3 of the License, or
00012  *   (at your option) any later version.
00013  *
00014  *   LaOS is distributed in the hope that it will be useful,
00015  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  *   GNU General Public License for more details.
00018  *
00019  *   You should have received a copy of the GNU General Public License
00020  *   along with LaOS.  If not, see <http://www.gnu.org/licenses/>.
00021  *
00022  * Minimal TFTP Server
00023  *      * Receive and send files via TFTP
00024  *      * Server handles only one transfer at a time
00025  *      * Supports only binary mode transfers, no (net)ascii
00026  *      * fixed block size: 512 bytes
00027  *
00028  * http://spectral.mscs.mu.edu/RFC/rfc1350.html
00029  *
00030  * Example:
00031  * @code 
00032  * TFTPServer *srv;
00033  * ...
00034  * srv = new TFTPServer();
00035  * ...
00036  * @endcode
00037  *
00038  */
00039 
00040 #ifndef _TFTPSERVER_H_
00041 #define _TFTPSERVER_H_
00042 
00043 #include <stdio.h>
00044 #include <ctype.h>
00045 #include "mbed.h"
00046 #include "SDFileSystem.h"   // http://mbed.org/users/NeoBelerophon/libraries/ChaNFSSD
00047 #include "UDPSocket.h"      // http://mbed.org/users/donatien/programs/EthernetNetIf
00048 
00049 #define TFTP_PORT 69
00050 //#define TFTP_DEBUG(x) printf("%s\n\r", x);
00051 
00052 enum TFTPServerState { listen, reading, writing, error, suspended, deleted }; 
00053 
00054 class TFTPServer {
00055 
00056 public:
00057     // create a new tftp server, with file directory dir and 
00058     // listening on port
00059     TFTPServer(char* dir);
00060     // destroy this instance of the tftp server
00061     void reset();
00062     // reset socket
00063     ~TFTPServer();
00064     // get current tftp status
00065     TFTPServerState State();
00066     // Temporarily disable incoming TFTP connections
00067     void suspend();
00068     // Resume after suspension
00069     void resume();
00070     // During read and write, this gives you the filename
00071     void getFilename(char* name);
00072     // Return number of received files
00073     int fileCnt();
00074 
00075 private:
00076     // create a new connection reading a file from server
00077     void ConnectRead(char* infile, Host* client);
00078     // create a new connection writing a file to the server
00079     void ConnectWrite(char* infile, Host* client);
00080     // get DATA block from file on disk into memory
00081     void getBlock();
00082     // send DATA block to the client
00083     void sendBlock();
00084     // handle special files
00085     // anything called *.bin is written to /local/FIRMWARE.BIN
00086     // anything called config.txt is written to /local/config.txt
00087     // even if workdir is not /local
00088     int cmpHost(Host* client);
00089     // send ACK to remote
00090     void Ack(int val);
00091     // send ERR message to named client
00092     void Err(char* msg, Host* client);
00093     // check if connection mode of client is octet/binary
00094     int modeOctet(char* buff);
00095     // timed routine to avoid hanging after interrupted transfers
00096     void cleanUp();
00097     // event driven routines to handle incoming packets
00098     void onListenUDPSocketEvent(UDPSocketEvent e);
00099     
00100     UDPSocket* ListenSock;      // main listening socket (dflt: UDP port 69)
00101     char workdir[256];          // file working directory
00102     TFTPServerState state;      // current TFTP server state
00103     Host* remote;               // connected remote Host IP and Port
00104     int blockcnt, dupcnt;       // block counter, and DUP counter
00105     FILE* fp;                   // current file to read or write
00106     char sendbuff[516];         // current DATA block;
00107     int blocksize;              // last DATA block size while sending
00108     char filename[256];         // current (or most recent) filename
00109     Ticker TFTPServerTimer;     // timeout timer
00110     int filecnt;                // received file counter
00111 };
00112 
00113 #endif