Skip to content

TFTP errorParser

Elbasiouny, Mahmoud edited this page Feb 27, 2026 · 7 revisions

ERROR packets (opcode 5) report error conditions during a TFTP transfer. They contain an error code, a human-readable error message, and a terminating zero byte.

Source file: tftp.c
Hammer concepts: h_int_range · h_ch_range · h_many1 · h_sequence


ERROR Packet Layout

 2 bytes    2 bytes      string    1 byte
+---------+---------+-----------+------+
| Opcode  | ErrCode |  ErrMsg   |  0   |
+---------+---------+-----------+------+

According to the RFC:

  • Opcode - 2 bytes, value 5
  • ErrorCode - 2 bytes, ranges from 0 to 7
  • ErrMsg - Variable-length error string in netascii, terminated by a zero byte (\x00)

Step 1: Opcode

H_RULE(opc, h_int_range(h_uint16(), 5, 5));

This only accepts a value of exactly 5.


Step 2: Error Code

H_RULE(errCode, h_int_range(h_uint16(), 0, 7));

The RFC defines error codes 0–7. Any value outside this range is invalid.


Step 3: Zero Byte Terminator

H_RULE(zbyt, h_int_range(h_uint8(), 0, 0));

The error message is terminated by a zero byte, same as in RRQ/WRQ packets.


Step 4: Error Message with h_ch_range

The error message is a netascii string. We define parsers for valid characters:

H_RULE(validChars, h_ch_range(' ', '~'));
H_RULE(frmtChars, h_choice(h_ch_range('\x01', '\x04'),
    h_ch_range('\x09', '\x0D'), NULL));
H_RULE(errMsg, h_sequence(h_many1(h_choice(validChars, frmtChars, NULL)),
    zbyt, NULL));

validChars covers printable ASCII characters (space through tilde). frmtChars covers formatting control characters. The message must have at least one character (h_many1), followed by the zero byte.


Step 5: Assemble the ERROR Parser

H_RULE(errPacket, h_sequence(opc, errCode, errMsg, NULL));

Summary

Concept Hammer Function Why It Matters in TFTP
Exact value match h_int_range(p, 5, 5) Opcode must be exactly 5
Bounded range h_int_range(p, 0, 7) Error codes are 0–7
Character classes h_ch_range Error messages use netascii
One or more h_many1 Error message must not be empty

Next: Assembling the Parser

Previous: ACK Packets

Clone this wiki locally