This is a simple but complete TFTP server capable to run on MBED-OS5. It can receive and send files and listens on port 69. Please notice that it supports only octet (raw 8 bit bytes) mode transfers. It is part of the LaOS (Laser Open Source) project: http://www.laoslaser.org/

Dependents:   TFTPServerTest

Fork of TFTPServer by Jaap Vermaas

Import programTFTPServerTest

This is an example code for using the TFTPServer library.

A simple TFTP server. NOTE: Supports only octet (raw 8 bit bytes) mode transfers.

Committer:
hudakz
Date:
Tue Mar 20 17:31:42 2018 +0000
Revision:
2:f7c0fbc8c5aa
Parent:
1:9c973065a97e
Updated.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 1:9c973065a97e 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
hudakz 2:f7c0fbc8c5aa 6 * Modified by Zoltan Hudak 2018 for MBED-OS5
tuxic 0:3b0027b76acf 7 *
tuxic 0:3b0027b76acf 8 * This file is part of the LaOS project (see: http://wiki.laoslaser.org
tuxic 0:3b0027b76acf 9 *
tuxic 0:3b0027b76acf 10 * LaOS is free software: you can redistribute it and/or modify
tuxic 0:3b0027b76acf 11 * it under the terms of the GNU General Public License as published by
tuxic 0:3b0027b76acf 12 * the Free Software Foundation, either version 3 of the License, or
tuxic 0:3b0027b76acf 13 * (at your option) any later version.
tuxic 0:3b0027b76acf 14 *
tuxic 0:3b0027b76acf 15 * LaOS is distributed in the hope that it will be useful,
tuxic 0:3b0027b76acf 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
tuxic 0:3b0027b76acf 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
tuxic 0:3b0027b76acf 18 * GNU General Public License for more details.
tuxic 0:3b0027b76acf 19 *
tuxic 0:3b0027b76acf 20 * You should have received a copy of the GNU General Public License
tuxic 0:3b0027b76acf 21 * along with LaOS. If not, see <http://www.gnu.org/licenses/>.
tuxic 0:3b0027b76acf 22 *
tuxic 0:3b0027b76acf 23 * Minimal TFTP Server
tuxic 0:3b0027b76acf 24 * * Receive and send files via TFTP
tuxic 0:3b0027b76acf 25 * * Server handles only one transfer at a time
hudakz 2:f7c0fbc8c5aa 26 * * Supports only octet (raw 8 bit bytes) mode transfers
tuxic 0:3b0027b76acf 27 * * fixed block size: 512 bytes
tuxic 0:3b0027b76acf 28 *
tuxic 0:3b0027b76acf 29 * http://spectral.mscs.mu.edu/RFC/rfc1350.html
tuxic 0:3b0027b76acf 30 *
tuxic 0:3b0027b76acf 31 * Example:
tuxic 0:3b0027b76acf 32 * @code
hudakz 2:f7c0fbc8c5aa 33 * TFTPServer *server;
tuxic 0:3b0027b76acf 34 * ...
hudakz 2:f7c0fbc8c5aa 35 * server = new TFTPServer();
tuxic 0:3b0027b76acf 36 * ...
tuxic 0:3b0027b76acf 37 * @endcode
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"
hudakz 1:9c973065a97e 46
hudakz 1:9c973065a97e 47 class EthernetInterface;
tuxic 0:3b0027b76acf 48
hudakz 1:9c973065a97e 49 #define TFTP_PORT 69
tuxic 0:3b0027b76acf 50
hudakz 1:9c973065a97e 51 class TFTPServer
hudakz 1:9c973065a97e 52 {
tuxic 0:3b0027b76acf 53 public:
hudakz 1:9c973065a97e 54 enum State
hudakz 1:9c973065a97e 55 {
hudakz 1:9c973065a97e 56 LISTENING,
hudakz 1:9c973065a97e 57 READING,
hudakz 1:9c973065a97e 58 WRITING,
hudakz 1:9c973065a97e 59 ERROR,
hudakz 1:9c973065a97e 60 SUSPENDED,
hudakz 1:9c973065a97e 61 DELETED
hudakz 1:9c973065a97e 62 };
hudakz 1:9c973065a97e 63
hudakz 1:9c973065a97e 64 // Creates a new TFTP server listening on myPort.
hudakz 1:9c973065a97e 65 TFTPServer(EthernetInterface* net, uint16_t myPort = TFTP_PORT);
hudakz 1:9c973065a97e 66
hudakz 1:9c973065a97e 67 // Destroys this instance of the TFTP server.
tuxic 0:3b0027b76acf 68 ~TFTPServer();
hudakz 1:9c973065a97e 69
hudakz 1:9c973065a97e 70 // Resets the TFTP server.
hudakz 1:9c973065a97e 71 void reset();
hudakz 1:9c973065a97e 72
hudakz 1:9c973065a97e 73 // Gets current TFTP status
hudakz 1:9c973065a97e 74 State getState();
hudakz 1:9c973065a97e 75
hudakz 1:9c973065a97e 76 // Temporarily disables incoming TFTP connections.
hudakz 1:9c973065a97e 77 void suspend();
hudakz 1:9c973065a97e 78
hudakz 1:9c973065a97e 79 // Resumes incoming TFTP connections after suspension.
hudakz 1:9c973065a97e 80 void resume();
hudakz 1:9c973065a97e 81
hudakz 1:9c973065a97e 82 // Polls for data or new connection.
hudakz 1:9c973065a97e 83 void poll();
hudakz 1:9c973065a97e 84
hudakz 1:9c973065a97e 85 // Gets the filename during read and write.
hudakz 1:9c973065a97e 86 void getFileName(char* name);
hudakz 1:9c973065a97e 87
hudakz 1:9c973065a97e 88 // Returns number of received files.
hudakz 1:9c973065a97e 89 int fileCount();
hudakz 1:9c973065a97e 90
tuxic 0:3b0027b76acf 91 private:
hudakz 1:9c973065a97e 92 // Creates a new connection reading a file from server.
hudakz 1:9c973065a97e 93 void connectRead(char* buff);
hudakz 1:9c973065a97e 94
hudakz 1:9c973065a97e 95 // Creates a new connection writing a file to the server.
hudakz 1:9c973065a97e 96 void connectWrite(char* buff);
hudakz 1:9c973065a97e 97
hudakz 1:9c973065a97e 98 // Gets DATA block from file on disk into memory.
hudakz 1:9c973065a97e 99 void getBlock();
hudakz 1:9c973065a97e 100
hudakz 1:9c973065a97e 101 // Sends DATA block to remote client.
hudakz 1:9c973065a97e 102 void sendBlock();
hudakz 1:9c973065a97e 103
hudakz 1:9c973065a97e 104 // Compares host's IP and Port with connected remote machine.
hudakz 1:9c973065a97e 105 int cmpHost();
hudakz 1:9c973065a97e 106
hudakz 1:9c973065a97e 107 // Sends ACK to remote client.
hudakz 1:9c973065a97e 108 void ack(int val);
tuxic 0:3b0027b76acf 109
hudakz 1:9c973065a97e 110 // Sends ERROR message to remote client.
hudakz 1:9c973065a97e 111 void sendError(const char* msg);
hudakz 1:9c973065a97e 112
hudakz 1:9c973065a97e 113 // Checks if connection mode of client is octet/binary.
hudakz 1:9c973065a97e 114 int modeOctet(char* buff);
hudakz 1:9c973065a97e 115
hudakz 1:9c973065a97e 116 uint16_t port; // TFTP port
hudakz 1:9c973065a97e 117 UDPSocket* socket; // Main listening socket (dflt: UDP port 69)
hudakz 1:9c973065a97e 118 State state; // Current TFTP server state
hudakz 1:9c973065a97e 119 char* remoteIP; // Connected remote Host IP
hudakz 1:9c973065a97e 120 int remotePort; // Connected remote Host Port
hudakz 1:9c973065a97e 121 uint16_t blockCounter, dupCounter; // Block counter, and DUP counter
hudakz 1:9c973065a97e 122 FILE* file; // Current file to read or write
hudakz 1:9c973065a97e 123 char blockBuff[516]; // Current DATA block;
hudakz 1:9c973065a97e 124 int blockSize; // Last DATA block size while sending
hudakz 1:9c973065a97e 125 char fileName[256]; // Current (or most recent) filename
hudakz 1:9c973065a97e 126 int fileCounter; // Received file counter
hudakz 1:9c973065a97e 127 char errorBuff[128]; // Error message buffer
hudakz 1:9c973065a97e 128 SocketAddress socketAddr; // Socket's addres (used to get remote host's address)
tuxic 0:3b0027b76acf 129 };
tuxic 0:3b0027b76acf 130 #endif