-
Notifications
You must be signed in to change notification settings - Fork 1
Examples
Elbasiouny, Mahmoud edited this page May 29, 2026
·
7 revisions
Each example walks through building a complete, working parser for a real network protocol using Hammer. The examples are self-contained and introduce Hammer concepts as they're needed, with links back to the Fundamentals and Quick Reference for more detail.
| Difficulty | Beginner |
| Source |
ntp.c, hex.c, main.c
|
| Packet size | 48 bytes minimum, variable with extensions |
| What you'll build | A validator for NTP packets (RFC 5905) |
Hammer concepts covered:
| Concept | Combinator | What NTP Teaches You |
|---|---|---|
| Sub-byte fields | h_bits |
Parsing 2-bit and 3-bit header fields |
| Value constraints | h_int_range |
Rejecting invalid version numbers |
| Ordered fields | h_sequence |
Composing header, timestamps, etc. |
| Alternatives | h_choice |
Matching different packet types |
| Repetition | h_many |
Zero or more extension fields |
| Length-prefixed data |
h_put_value, h_free_value, h_length_value
|
Variable-length extension field values |
| Semantic actions |
h_action, H_ARULE
|
Adjusting lengths, hex-to-byte conversion |
| Parse tree walking |
h_seq_index, H_MAKE_*
|
Building transformed results |
| End-of-input assertion |
h_end_p, h_left
|
Ensuring no trailing garbage |
Pages:
- NTP Overview - Protocol intro, project structure, quick start
-
Parsing the Header -
h_bits,h_int_range,h_sequence - Parsing Data Fields - Timestamps, fixed-point numbers
-
Extension Fields and MAC -
h_put_value,h_length_value,h_many -
Assembling the Parser -
h_choice,h_left,h_end_p -
Hex Input Preprocessing -
h_ch_range,h_repeat_n,H_ARULE - Running and Testing - Build, run, test with real packets
| Difficulty | Intermediate |
| Source |
dns.c, dns.h, main.c
|
| Packet size | 12-byte header + variable-length body |
| What you'll build | A structural validator for DNS query and response packets RFC 1035 |
Hammer concepts covered:
| Concept | Combinator | What DNS Teaches You |
|---|---|---|
| Sub-byte fields | h_bits |
Parsing packed DNS flag bits (QR, Opcode, AA, etc.) |
| Grammar-level invariants | h_attr_bool |
Enforcing reserved Z bits = 0, validating TYPE/CLASS |
| Ordered fields | h_sequence |
Composing header and resource record structure |
| Alternatives | h_choice |
Handling label-only, pointer-only, and label+pointer names |
| Length-prefixed data | h_length_value |
Parsing labels and RDATA fields |
| Context-driven parsing | h_action |
Storing header counts for body parsing |
| Position awareness | h_tell |
Validating compression pointer offsets |
| Failure propagation | (automatic) | Rejecting malformed packets immediately |
Pages:
- DNS Overview - Protocol intro, project structure, quick start
-
Parsing the Header -
h_bits,h_attr_bool,h_sequence -
Parsing the Body - Labels, compression pointers,
h_length_value,h_repeat_n -
Assembling the Parser - Context wiring via
DNSContext,h_parseflow -
Running and Testing - Manual testing and automated suite with
runtests.py
| Difficulty | Beginner |
| Source |
tftp.c, main.c
|
| Packet size | Variable (up to 516 bytes for data packets) |
| What you'll build | A parser for TFTP request, data, ACK, and error packets (RFC 1350) |
Hammer concepts covered:
| Concept | Combinator | What TFTP Teaches You |
|---|---|---|
| Value constraints | h_int_range |
Validating opcodes, error codes, block numbers |
| String matching | h_token |
Matching exact mode strings ("netascii", "octet", "mail") |
| Character parsing | h_ch_range |
Parsing printable ASCII filenames and error messages |
| Variable-length data |
h_many, h_many1
|
Filenames, error messages, data payloads |
| Alternatives | h_choice |
Dispatching to the correct packet type parser |
| Null termination | h_int_range(h_uint8(), 0, 0) |
Validating \x00 terminators |
| End-of-input | h_end_p |
Ensuring all bytes are consumed |
Pages:
- TFTP Overview - Protocol intro, project structure, quick start
-
RRQ/WRQ Packets -
h_int_range,h_ch_range,h_token,h_many1 -
DATA Packets -
h_many,h_end_p -
ACK Packets -
h_int_range,h_sequence -
ERROR Packets -
h_ch_range,h_many1 -
Assembling the Parser -
h_choice,h_parse - Running and Testing - Build, run, and test
Have a protocol you'd like to parse with Hammer? See Adding a New Example for a template and guidelines.
Learn Hammer
Protocol Examples
NTP
- NTP Overview
- Parsing the Header
- Parsing Data Fields
- Extension Fields and MAC
- Assembling the Parser
- Hex Input Preprocessing
- Running and Testing
DNS
TFTP
- TFTP Overview
- RRQ/WRQ Packets
- DATA Packets
- ACK Packets
- ERROR Packets
- Assembling the Parser
- Running and Testing
References
- Hammer Quick Reference
- Parsing Backends
- Unit Testing
- Using RTEMS
- Extending Hammer
- Adding a New Example
- Adding a New Binding
Further Reading