# dns — RFC 1035 Queries, RCODEs & UDP/TCP Port 53 Captures (`dns`)

> **FrameAssembly v3.2.2** · Protocol 35 of 81 · Canonical URL: https://frameassembly-lang.dev/docs/protocols/dns

- **Full Protocol Name:** Domain Name System over UDP or TCP
- **Keyword:** `dns`
- **OSI Layer & Transport:** Layer 7 · UDP 53 (or TCP 53 via transport tcp)
- **Host Endpoint Rule:** Dual-Stack IPv4/IPv6
- **Standards:** RFC 1035, RFC 3596, RFC 2782
- **Wireshark Display Filter:** `dns`
- **Wire Encapsulation:** `Ethernet II -> [802.1Q / QinQ / MPLS] -> IPv4 / IPv6 -> UDP 53 (or TCP 53 via transport tcp) (transport tcp|udp) -> DNS (UDP/TCP)`

## Overview

Whether you are testing a sinkhole rule, an Active Directory `SRV` lookup, or switching to `transport tcp` (which auto-prepends the 2-byte RFC 1035 TCP length prefix!), `dns` encodes wire labels and compressed answers cleanly.

Domain Name System (`dns`, RFC 1035 / RFC 3596) resolves domain names into IPv4 (`A`), IPv6 (`AAAA`), service (`SRV`), mail (`MX`), and policy (`TXT`) records on port `53`. `dns` defaults to **UDP port `53`**, and adding `transport tcp` switches to **TCP port `53`** with the required 2-byte RFC 1035 TCP length prefix.

FrameAssembly formats RFC 1035 domain labels automatically, checks the RFC 63-character label limit and 253-character total hostname limit before building your `.pcap`, and writes compressed Answer records (`A`, `AAAA`, `CNAME`, `TXT`, `PTR`, `NS`, `MX`, `SRV`) on `response` packets.

## Minimal Quickstart (`dns_quickstart.frasm`)

```frameassembly
host stub_client { ip 10.10.0.25 }
host dns_resolv  { ip 10.10.0.53 }

compile {
    stub_client -> dns_resolv dns query txid 0x4a12 qname "api.frameassembly.internal" qtype "A" wait 2ms
    stub_client <- dns_resolv dns response txid 0x4a12 qname "api.frameassembly.internal" qtype "A" rdata "10.10.50.80" ansttl 300
}
```

### Automatic Wire Calculations

- Encodes dotted `qname` strings into RFC 1035 length-prefixed labels (`\x03api...\x00`) and validates RFC label (`<= 63`B) and total length (`<= 253`B) limits at compile time.
- Sets DNS flags (`QR=0, RD=1` `0x0100` on `query`; `QR=1, RD=1, RA=1` `0x8180 | rcode` on `response`) and appends compressed `0xc00c` Answer RRs when `rdata` is provided.
- Prepends the 2-byte big-endian message length header automatically whenever `transport tcp` is specified.

## Practical Lab Example: UDP A/AAAA + NXDOMAIN Lookups Followed by Stateful DNS-over-TCP SRV Resolution (`dns_udp_and_tcp_lab.frasm`)

Captures standard UDP `AAAA` and `NXDOMAIN` (`rcode 3`) queries, then establishes a TCP 53 session to resolve an Active Directory `_kerberos._tcp` lookup via `transport tcp`.

```frameassembly
host workstation { ip 172.16.10.45 }
host corp_dns    { ip 172.16.10.10 }

compile {
    // 1. Standard UDP IPv6 AAAA lookup
    workstation -> corp_dns dns query txid 0x1001 srcport 53110 qname "git.corp.internal" qtype "AAAA" wait 1ms
    workstation <- corp_dns dns response txid 0x1001 qname "git.corp.internal" qtype "AAAA" rdata "2001:db8:10::443" ansttl 120 wait 3ms
    // 2. Non-existent domain returning NXDOMAIN (rcode 3)
    workstation -> corp_dns dns query txid 0x1002 srcport 53111 qname "missing-svc.corp.internal" qtype "A" wait 1ms
    workstation <- corp_dns dns response txid 0x1002 qname "missing-svc.corp.internal" qtype "A" rcode 3 wait 4ms
    // 3. RFC 1035 DNS-over-TCP on port 53 (auto-prefixes 2-byte TCP length field)
    workstation -> corp_dns tcp syn srcport 54053 dstport 53 win 65535 mss 1460 wait 1ms
    workstation <- corp_dns tcp syn ack win 65535 mss 1460 wait 1ms
    workstation -> corp_dns tcp ack wait 1ms
    workstation -> corp_dns dns query txid 0x2001 qname "dc01.corp.internal" qtype "TXT" transport tcp wait 1ms
    workstation <- corp_dns dns response txid 0x2001 qname "dc01.corp.internal" qtype "TXT" rdata "v=spf1 ip4:172.16.0.0/16 -all" ansttl 3600 transport tcp
}
```

```bash
# 1. Eyeball relative timestamps, ports & wire lengths in your terminal
frameassembly show dns_udp_and_tcp_lab.frasm

# 2. Compile .frasm -> classic .pcap (tcpdump/tcpreplay/Suricata) or nanosecond .pcapng
frameassembly dns_udp_and_tcp_lab.frasm dns_udp_and_tcp_lab.pcap
frameassembly dns_udp_and_tcp_lab.frasm dns_udp_and_tcp_lab.pcapng

# 3. Round-trip .pcap <-> .frasm whenever you want to scrub or tweak an existing capture
frameassembly reverse dns_udp_and_tcp_lab.pcap dns_udp_and_tcp_lab_edited.frasm

# 4. Verify dissected protocol fields in tshark
tshark -r dns_udp_and_tcp_lab.pcap
```

## All `dns` Keywords & Header Options

Packet line pattern: `src_host -> dst_host dns [query | response] [txid <0..65535>] [qname "<text>"] [qtype "<text>"] [rdata "<text>"] [rcode <0..15>] [ansttl <number>] [transport <udp|tcp>]`

| Keyword / Option | Accepted Value | Default | Effect on Packet |
| --- | --- | --- | --- |
| `query \| response` | Message direction keyword | `query (QR=0)` | Toggles the `QR` bit in the 12-byte DNS header (`0` query, `1` response with `Answer RRs`). |
| `txid <0..65535>` | Whole number (0..65535) | `0x1234` | Sets the 16-bit DNS Transaction ID matching requests to responses. |
| `qname "<text>"` | Domain string (labels <= 63B, total <= 253B) | `"example.com"` | Encodes the Question `QNAME` wire labels. |
| `qtype "<text>"` | "A" \| "AAAA" \| "CNAME" \| "MX" \| "NS" \| "PTR" \| "SOA" \| "SRV" \| "TXT" \| "AXFR" \| "ANY" | `"A"` | Sets the 16-bit `QTYPE` (and Answer RR `TYPE` on responses). |
| `rdata "<text>"` | IP string, hostname, or text value | `Omitted (or 93.184.216.34 on A response)` | Populates the Answer Resource Record `RDATA` payload on `response` frames. |
| `rcode <0..15>` | 4-bit response code | `0 (NOERROR)` | Sets the lower 4 bits of DNS Flags (`0`=NOERROR, `1`=FORMERR, `2`=SERVFAIL, `3`=NXDOMAIN, `5`=REFUSED). |
| `ansttl <number>` | Whole number (seconds) | `300` | Sets the 32-bit caching TTL inside the Answer Resource Record (never `ans_ttl`). |
| `transport <udp\|tcp>` | `udp` or `tcp` | `udp` | Switches between UDP `53` and stateful TCP `53` (adding the 2-byte RFC 1035 message length prefix). |

## Cross-Layer Options Valid with `dns`

| Option | Range / Format | Default | Effect |
| --- | --- | --- | --- |
| `wait <number><ns\|us\|ms\|s\|m>` | Time gap (ns, us, ms, s, m) | `10ms` | Advances the packet timestamp clock after sending this packet and before the next packet (`wait 0ns` sends back-to-back). |
| `payload "<text>" \| hexpayload "<hex>"` | Quoted text or hex bytes (choose one) | `Omitted` | Appends or overrides raw payload bytes (`"deadbeef"` or `"de:ad:be:ef"`) and updates packet lengths and checksums. |
| `srcmac <mac> \| dstmac <mac> \| ethertype <0..65535>` | Colon/hyphen/Cisco MAC · EtherType u16 (e.g. 0x0800) | `Host MAC / RFC multicast MAC / auto EtherType` | Overrides the 14-byte Ethernet II source MAC, destination MAC, or 16-bit EtherType for this single packet. |
| `vlan <0..4095> [pcp <0..7>] [dei <0\|1>]` | VLAN ID 0..4095, PCP 0..7, DEI 0..1 | `Untagged` | Wraps the packet in an IEEE 802.1Q VLAN tag (`0x8100`) with optional 802.1p QoS priority. |
| `qinq <0..4095> [innervlan <0..4095>]` | Outer S-VLAN 0..4095, Inner C-VLAN 0..4095 | `Untagged (innervlan 1 when qinq is set)` | Double-tags the packet with outer IEEE 802.1ad S-Tag (`0x88a8`) plus inner IEEE 802.1Q C-Tag (`0x8100`). |
| `mpls <0..1048575> [label <0..1048575>] [exp\|tc <0..7>] [bos <0\|1>] [mplsttl <0..255>]` | Label 0..1048575, exp/tc 0..7, bos 0..1, ttl 0..255 | `Unlabeled (at most 1 mpls tag per packet)` | Inserts a 32-bit MPLS label header (`0x8847`) ahead of the Layer 3 header. |
| `ttl <0..255> [dscp <0..63>] [ecn <0..3>]` | TTL/Hop Limit 0..255, DSCP 0..63, ECN 0..3 | `ttl 64, dscp 0, ecn 0` | Sets IPv4 TTL or IPv6 Hop Limit plus DiffServ (DSCP) and ECN QoS markings. |
| `ipid <0..65535> [df] [mf] [fragoffset <0..8191>]` | IPv4 ID, DF/MF flags, fragment offset 0..8191 | `ipid 1, flags 0, fragoffset 0` | Sets IPv4 Identification, Do-Not-Fragment (`df`), More-Fragments (`mf`), and fragment offset (ignored over IPv6). |
| `flowlabel <0..1048575>` | 20-bit IPv6 Flow Label (0..1048575) | `0` | Sets the 20-bit IPv6 Flow Label for ECMP routing tests (ignored when running over IPv4 hosts). |
| `srcport <0..65535> \| dstport <0..65535>` | Port number 0..65535 | `srcport 49152 (remembered), UDP 53 (or TCP 53 via transport tcp)` | Overrides source or destination port and remembers the port pair automatically on `<-` replies. |
| `transport <tcp\|udp>` | `tcp` or `udp` (supported on 9 dual-L4 protocols) | `transport udp` | Switches Layer 4 transport between TCP (`psh ack` + length framing where required) and UDP. |
| `seq <number> \| acknum <number> \| win\|window <0..65535> \| urgent <0..65535> \| syn \| ack \| psh \| fin \| rst \| urg \| ece \| cwr` | TCP sequence/ack numbers, window size, flags & RFC 7323 options | `psh ack, win 65535, automatic TCP seq/acknum` | When using TCP (`transport tcp` or default TCP): overrides automatic TCP sequence tracking, flags, or `mss`/`wscale`/`sackperm`/`tsval`/`tsecr`. |

## Common Mistakes & Quick Fixes

### Spell Answer TTL as ansttl (Zero Underscores)

- **Avoid:** `stub_client <- dns_resolv dns response rdata "10.0.0.5" ans_ttl 300`
- **Valid v3.2.2 Syntax:** `stub_client <- dns_resolv dns response rdata "10.0.0.5" ansttl 300`

Built-in modifiers never use underscores—use `ansttl <number>` for the DNS Answer RR TTL so L3 `ttl <0..255>` remains free to set the IP Hop Limit.

### Each Domain Label Must Be 63 Characters or Fewer

- **Avoid:** `stub_client -> dns_resolv dns query qname "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.internal"`
- **Valid v3.2.2 Syntax:** `stub_client -> dns_resolv dns query qname "part1.part2.exfil.internal"`

RFC 1035 limits each dot-separated label to `63` bytes and the full domain name to `253` bytes. Split long DNS tunneling test payloads across multiple dots.
