Skip to main content
Department of Information Technology

TFTP Programming Lab

This lab involves writing a TFTP client in C. TFTP stands for Trivial File Transfer Protocol and is the predecessor to FTP. It is still used today for transferring new firmware to routers and other embedded devices and also for booting network terminals. It is a very simple protocol and therefore straightforward to understand. It has two main uses:

  • Transferring a file from a client to server
  • Transferring a file from a server to a client

The goal of this lab is to write a TFTP client which can perform both of these functions.

Begin by taking the following steps:

  1. Read the How to Proceed section.
  2. When handing in your assignment:
    1. Make sure you have completed all the points on the Checklist.
    2. Make sure you follow the instructions in the Hand in section.

If you do not follow these instructions correctly your assignment will immediately be returned to you without grading!

How to Proceed

TFTP is defined in RFC 1350. Read this document carefully! The RFC defines the protocol header format and how the protocol messages should be interpreted by both the client and the server. Note that for this lab you are only concerned with writing the client code. You will be provided with a skeleton piece of code which will provide certain pieces of functionality for you: Skeleton Code.

Please work in groups of either two or three.

Your goals

You are to write a TFTP client which can:

  1. Transfer a file from a client to server
  2. Transfer a file from a server to a client

You will need to:

  • Use the pre-defined packet header format for:
    • Read/Write requests
    • Ack packets
    • Data packets
  • Write the socket handling code to open & close the socket to the server and subsequently handle data and ack packets
  • Open & close the file being dealt with
  • Read or write to the file being dealt with

The above is done by extending the supplied lab code.


There is a TFTP server running on the machine which is accessible only inside the university network. If you wish to retrieve a file, there is a test file available: 132.txt. If you wish to upload something feel free. However, do not upload objectionable material as all TFTP traffic is logged and offenders will be reported. The university Acceptable Usage Policy (AUP) is available here.


Socket programming is one of the key techniques to learn for this lab. TFTP uses UDP sockets. Here are some good tutorials:

The socket programming calls that you will need for this lab are:

One of the first things that you will need to do is to open a UDP socket to the server. Once you have opened the socket you will need to send a either a read or write request packet to the server on the default TFTP port (69). Consult the RFC to see how the packet format should be. Once this has been done, you will need to use either the putting a file loop or the getting a file loop as detailed below.


The two main loops that you will need for your client are as follows:

Putting a file

  1. Open network socket to the server
  2. Construct a write request
  3. Send write request to the server
  4. Receive acknowledgement from server
  5. Open file
  6. Read data from file
  7. Place data from file in packet to send to server
  8. Send data packet to server
  9. Receive acknowledgement from server
  10. Repeat steps 6 to 9 until data read from file is less than 512 bytes
  11. Close file socket
  12. Close network socket

Getting a file

  1. Open network socket to the server
  2. Construct a read request
  3. Send read request to the server
  4. Open file
  5. Receive data packet from server
  6. Write data to file
  7. Send acknowledgement to server
  8. Repeat steps 5 to 7 until data read from file is less than 512 bytes
  9. Close file socket
  10. Close network socket

Things to remember

  • Snprintf: Snprintf takes a string and outputs a result according to which format is specified. Snprintf is preferable to sprintf as it forces the programmer to specify the length of the string being used. It is important to note that the length field of snprint refers to the size of the buffer being manipulated (e.g., str from the manpage), not the length of the string being placed into the buffer. This is because strings are typically null-terminated, i.e., they contain a trailing zero. Since TFTP uses padding to refer to the end of the string, it is not necessary to add a trailing zero yourself. A good sign that you're doing something wrong is when the string datakom2 appears as datakom in your packets.
  • Host/network byte order: Different computer architectures store the most significant bit of a binary number in different places. There are two types: big-endian (most significant bit first) and little-endian (most significant bit last). In order to avoid confusion since networks consist of different types of communicating computers, network byte order was chosen as a standard. Network byte order is big-endian. In order to cope with this, various helper functions are implemented by most Operating Systems to allow a programmer to easily convert between host and network byte order. In order for you to send information over the network, it must be converted into the correct byte order. More information is available at the wikipedia entry on Endianness & network/host byte order conversion man pages.
  • Ports: The default TFTP port (69) only receives RRQ/WRQ messages. Do not try sending any other message type to this port.
  • Pointers: You cannot send information referenced by pointers over the network. This kind of information must be dereferenced first and appropriately serialized.
  • Block number: You will need to keep track of which block you are sending & receiving so that you know how to deal with erroneous block numbers.
  • Signed vs. unsigned: binary numbers often have a sign bit which allows the programmer to know if a binary number is positive or negative. Sometimes this information is not necessary (for example, you cannot have a port number with a negative value). In order to maximize the range of number allowed by a particular number of bits (e.g., 16 bits), a binary number can be unsigned. This means that the full 16 bits can be used for expressing a positive number. You will encounter both signed and unsigned values whilst writing your TFTP client.
  • Compiler Warnings: Compile your code with gcc -Wall. This will help you detect errors quicker. Your program should be able to compile without warnings.


In order for you to successfully complete this assignment your code must be able to:

  1. Send a file to the server
  2. Receive a file from the server

In addition to this we will be checking for the following attributes:

  • Your program must perform the correct network/host byte order conversions
  • Your program must not crash
  • Your program must send the correct length of packets. A simple way to check this is to see with Ethereal network protocol analyzer. If Ethereal reports "malformed packet" for the packets that you have sent out, then you are doing something wrong. Often it is a case of a trailing zero.
  • Your program must be able to perform error handling. That is, if the TFTP server returns an error message, your program must be able to fail in the correct way as specified in the RFC. A simple way to test is to ask for a file that does not exist.
  • You must have edited the supplied Makefile so that your code compiles via make. You may want to consult a GNU Make tutorial.

Hand in Procedure

The procedure to hand in the assignment is as follows:

  1. Your source tree should contain the files tftp.c and Makefile. They should automatically compile into their corresponding executable file by a make from the top directory tftp/.
  2. Create a tar ball with the complete source tree via tar, e.g., tar cvfz tftplab-foba5194.tar.gz. Replace foba5194 with your own username!
  3. Write down the full path to your tarball, e.g., /home/foba5194/code/tftplab-foba5194.tar.gz. Do not forget to note the filename too!
  4. Make sure that your tarball has the correct permission, i.e., that it is world readable. Use the command: chmod a+r tftplab-foba5194.tar.gz. This is so the instructors can retrieve your code for grading. If we cannot retrieve your code we cannot grade your assignment.
  5. Print out and fill in the form correctly, e.g., do not forget to provide the full path to your tarball or provide the details of all people involved in the assignment!
  6. Hand in the form to the postbox of Erik Nordström, number 130.

If you do not follow these instructions correctly your assignment will immediately be returned to you without grading!

Updated  2006-04-05 17:21:26 by Richard Gold.