Skip to content

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.


Available Examples

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:

  1. NTP Overview - Protocol intro, project structure, quick start
  2. Parsing the Header - h_bits, h_int_range, h_sequence
  3. Parsing Data Fields - Timestamps, fixed-point numbers
  4. Extension Fields and MAC - h_put_value, h_length_value, h_many
  5. Assembling the Parser - h_choice, h_left, h_end_p
  6. Hex Input Preprocessing - h_ch_range, h_repeat_n, H_ARULE
  7. 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:

  1. DNS Overview - Protocol intro, project structure, quick start
  2. Parsing the Header - h_bits, h_attr_bool, h_sequence
  3. Parsing the Body - Labels, compression pointers, h_length_value, h_repeat_n
  4. Assembling the Parser - Context wiring via DNSContext, h_parse flow
  5. 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:

  1. TFTP Overview - Protocol intro, project structure, quick start
  2. RRQ/WRQ Packets - h_int_range, h_ch_range, h_token, h_many1
  3. DATA Packets - h_many, h_end_p
  4. ACK Packets - h_int_range, h_sequence
  5. ERROR Packets - h_ch_range, h_many1
  6. Assembling the Parser - h_choice, h_parse
  7. Running and Testing - Build, run, and test

Adding Your Own Example

Have a protocol you'd like to parse with Hammer? See Adding a New Example for a template and guidelines.

Clone this wiki locally