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

Committer:
tuxic
Date:
Mon Dec 26 18:18:32 2011 +0000
Revision:
0:3b0027b76acf
First version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tuxic 0:3b0027b76acf 1 /**
tuxic 0:3b0027b76acf 2 * TFTPServer.h
tuxic 0:3b0027b76acf 3 * Simple TFTP server
tuxic 0:3b0027b76acf 4 *
tuxic 0:3b0027b76acf 5 * Copyright (c) 2011 Jaap Vermaas
tuxic 0:3b0027b76acf 6 *
tuxic 0:3b0027b76acf 7 * This file is part of the LaOS project (see: http://wiki.laoslaser.org
tuxic 0:3b0027b76acf 8 *
tuxic 0:3b0027b76acf 9 * LaOS is free software: you can redistribute it and/or modify
tuxic 0:3b0027b76acf 10 * it under the terms of the GNU General Public License as published by
tuxic 0:3b0027b76acf 11 * the Free Software Foundation, either version 3 of the License, or
tuxic 0:3b0027b76acf 12 * (at your option) any later version.
tuxic 0:3b0027b76acf 13 *
tuxic 0:3b0027b76acf 14 * LaOS is distributed in the hope that it will be useful,
tuxic 0:3b0027b76acf 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
tuxic 0:3b0027b76acf 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
tuxic 0:3b0027b76acf 17 * GNU General Public License for more details.
tuxic 0:3b0027b76acf 18 *
tuxic 0:3b0027b76acf 19 * You should have received a copy of the GNU General Public License
tuxic 0:3b0027b76acf 20 * along with LaOS. If not, see <http://www.gnu.org/licenses/>.
tuxic 0:3b0027b76acf 21 *
tuxic 0:3b0027b76acf 22 * Minimal TFTP Server
tuxic 0:3b0027b76acf 23 * * Receive and send files via TFTP
tuxic 0:3b0027b76acf 24 * * Server handles only one transfer at a time
tuxic 0:3b0027b76acf 25 * * Supports only binary mode transfers, no (net)ascii
tuxic 0:3b0027b76acf 26 * * fixed block size: 512 bytes
tuxic 0:3b0027b76acf 27 *
tuxic 0:3b0027b76acf 28 * http://spectral.mscs.mu.edu/RFC/rfc1350.html
tuxic 0:3b0027b76acf 29 *
tuxic 0:3b0027b76acf 30 * Example:
tuxic 0:3b0027b76acf 31 * @code
tuxic 0:3b0027b76acf 32 * TFTPServer *srv;
tuxic 0:3b0027b76acf 33 * ...
tuxic 0:3b0027b76acf 34 * srv = new TFTPServer();
tuxic 0:3b0027b76acf 35 * ...
tuxic 0:3b0027b76acf 36 * @endcode
tuxic 0:3b0027b76acf 37 *
tuxic 0:3b0027b76acf 38 */
tuxic 0:3b0027b76acf 39
tuxic 0:3b0027b76acf 40 #ifndef _TFTPSERVER_H_
tuxic 0:3b0027b76acf 41 #define _TFTPSERVER_H_
tuxic 0:3b0027b76acf 42
tuxic 0:3b0027b76acf 43 #include <stdio.h>
tuxic 0:3b0027b76acf 44 #include <ctype.h>
tuxic 0:3b0027b76acf 45 #include "mbed.h"
tuxic 0:3b0027b76acf 46 #include "SDFileSystem.h" // http://mbed.org/users/NeoBelerophon/libraries/ChaNFSSD
tuxic 0:3b0027b76acf 47 #include "UDPSocket.h" // http://mbed.org/users/donatien/programs/EthernetNetIf
tuxic 0:3b0027b76acf 48
tuxic 0:3b0027b76acf 49 #define TFTP_PORT 69
tuxic 0:3b0027b76acf 50 //#define TFTP_DEBUG(x) printf("%s\n\r", x);
tuxic 0:3b0027b76acf 51
tuxic 0:3b0027b76acf 52 enum TFTPServerState { listen, reading, writing, error, suspended, deleted };
tuxic 0:3b0027b76acf 53
tuxic 0:3b0027b76acf 54 class TFTPServer {
tuxic 0:3b0027b76acf 55
tuxic 0:3b0027b76acf 56 public:
tuxic 0:3b0027b76acf 57 // create a new tftp server, with file directory dir and
tuxic 0:3b0027b76acf 58 // listening on port
tuxic 0:3b0027b76acf 59 TFTPServer(char* dir);
tuxic 0:3b0027b76acf 60 // destroy this instance of the tftp server
tuxic 0:3b0027b76acf 61 void reset();
tuxic 0:3b0027b76acf 62 // reset socket
tuxic 0:3b0027b76acf 63 ~TFTPServer();
tuxic 0:3b0027b76acf 64 // get current tftp status
tuxic 0:3b0027b76acf 65 TFTPServerState State();
tuxic 0:3b0027b76acf 66 // Temporarily disable incoming TFTP connections
tuxic 0:3b0027b76acf 67 void suspend();
tuxic 0:3b0027b76acf 68 // Resume after suspension
tuxic 0:3b0027b76acf 69 void resume();
tuxic 0:3b0027b76acf 70 // During read and write, this gives you the filename
tuxic 0:3b0027b76acf 71 void getFilename(char* name);
tuxic 0:3b0027b76acf 72 // Return number of received files
tuxic 0:3b0027b76acf 73 int fileCnt();
tuxic 0:3b0027b76acf 74
tuxic 0:3b0027b76acf 75 private:
tuxic 0:3b0027b76acf 76 // create a new connection reading a file from server
tuxic 0:3b0027b76acf 77 void ConnectRead(char* infile, Host* client);
tuxic 0:3b0027b76acf 78 // create a new connection writing a file to the server
tuxic 0:3b0027b76acf 79 void ConnectWrite(char* infile, Host* client);
tuxic 0:3b0027b76acf 80 // get DATA block from file on disk into memory
tuxic 0:3b0027b76acf 81 void getBlock();
tuxic 0:3b0027b76acf 82 // send DATA block to the client
tuxic 0:3b0027b76acf 83 void sendBlock();
tuxic 0:3b0027b76acf 84 // handle special files
tuxic 0:3b0027b76acf 85 // anything called *.bin is written to /local/FIRMWARE.BIN
tuxic 0:3b0027b76acf 86 // anything called config.txt is written to /local/config.txt
tuxic 0:3b0027b76acf 87 // even if workdir is not /local
tuxic 0:3b0027b76acf 88 int cmpHost(Host* client);
tuxic 0:3b0027b76acf 89 // send ACK to remote
tuxic 0:3b0027b76acf 90 void Ack(int val);
tuxic 0:3b0027b76acf 91 // send ERR message to named client
tuxic 0:3b0027b76acf 92 void Err(char* msg, Host* client);
tuxic 0:3b0027b76acf 93 // check if connection mode of client is octet/binary
tuxic 0:3b0027b76acf 94 int modeOctet(char* buff);
tuxic 0:3b0027b76acf 95 // timed routine to avoid hanging after interrupted transfers
tuxic 0:3b0027b76acf 96 void cleanUp();
tuxic 0:3b0027b76acf 97 // event driven routines to handle incoming packets
tuxic 0:3b0027b76acf 98 void onListenUDPSocketEvent(UDPSocketEvent e);
tuxic 0:3b0027b76acf 99
tuxic 0:3b0027b76acf 100 UDPSocket* ListenSock; // main listening socket (dflt: UDP port 69)
tuxic 0:3b0027b76acf 101 char workdir[256]; // file working directory
tuxic 0:3b0027b76acf 102 TFTPServerState state; // current TFTP server state
tuxic 0:3b0027b76acf 103 Host* remote; // connected remote Host IP and Port
tuxic 0:3b0027b76acf 104 int blockcnt, dupcnt; // block counter, and DUP counter
tuxic 0:3b0027b76acf 105 FILE* fp; // current file to read or write
tuxic 0:3b0027b76acf 106 char sendbuff[516]; // current DATA block;
tuxic 0:3b0027b76acf 107 int blocksize; // last DATA block size while sending
tuxic 0:3b0027b76acf 108 char filename[256]; // current (or most recent) filename
tuxic 0:3b0027b76acf 109 Ticker TFTPServerTimer; // timeout timer
tuxic 0:3b0027b76acf 110 int filecnt; // received file counter
tuxic 0:3b0027b76acf 111 };
tuxic 0:3b0027b76acf 112
tuxic 0:3b0027b76acf 113 #endif