# DNS (UDP/TCP), mDNS, SSDP, DHCPv4/v6 & Nanosecond Time Sync

> **FrameAssembly v3.2.2** · Anthology 06 (Layer 7 · 13 Recipes) · MIT License · Canonical URL: https://frameassembly-lang.dev/docs/examples/dns-dhcp-network-timing

Core DDI address and service resolution (`dns`, `mdns`, `ssdp`, `dhcp` DORA and IPv6 SARR) alongside stratum `ntp` polling and sub-microsecond IEEE 1588 `ptp` `.pcapng` captures.

## Key Wire & Syntax Takeaways

- Adding `transport tcp` to `dns` switches from UDP 53 to stateful TCP 53 and prepends the RFC 1035 2-byte message length automatically.
- `dhcp` adapts ports by IP family and message role: UDP `68 <-> 67` on IPv4 DORA vs. UDP `546 <-> 547` on IPv6 Solicit/Advertise/Request/Reply.
- `ptp` separates event messages (UDP 319) from general messages (UDP 320) and preserves 1ns `wait` precision when compiled to `.pcapng`.

## 06.01 · DNS over TCP Port 53 Zone Transfer Check (transport tcp) (`dns_tcp_zone_transfer.frasm`)

Tests firewall rules that mistakenly allow UDP 53 while blocking TCP 53 (required for truncated responses >512B, DNSSEC, and AXFR transfers).

- **Protocols:** `tcp`, `dns` (5 Ethernet frames)
- **Wire Behavior:** Adding transport tcp to dns prepends the mandatory RFC 1035 2-byte big-endian TCP message-length prefix automatically.
- **Wireshark Filter:** `dns && tcp.port == 53`
- **CLI Build:** `frameassembly dns_tcp_zone_transfer.frasm`

```frameassembly
host ns_secondary { ip 10.53.0.12 }
host ns_primary   { ip 10.53.0.11 }

compile {
    ns_secondary -> ns_primary tcp syn srcport 53053 dstport 53 win 65535 mss 1460 wait 1ms
    ns_secondary <- ns_primary tcp syn ack win 65535 mss 1460 wait 1ms
    ns_secondary -> ns_primary tcp ack wait 1ms
    ns_secondary -> ns_primary dns query txid 0x8801 qname "prod.corp.internal" qtype "AXFR" transport tcp wait 3ms
    ns_secondary <- ns_primary dns response txid 0x8801 qname "prod.corp.internal" qtype "A" rdata "10.53.10.20" ansttl 3600 transport tcp
}
```

## 06.02 · Stateful DHCPv6 Four-Packet SARR Lease (Solicit, Advertise, Request, Reply) (`dhcpv6_sarr_lease.frasm`)

Exercises IPv6 DHCPv6 client-to-relay/server port flipping (UDP 546 <-> 547) across Solicit, Advertise, Request, and Reply.

- **Protocols:** `dhcp` (4 Ethernet frames)
- **Wire Behavior:** Over IPv6 endpoints, dhcp automatically switches from BOOTP (68/67) to RFC 8415 DHCPv6 (546/547).
- **Wireshark Filter:** `dhcpv6`
- **CLI Build:** `frameassembly dhcpv6_sarr_lease.frasm`

```frameassembly
host client_v6 { ip fe80::250:56ff:feaa:0010 mac 00:50:56:aa:00:10 }
host dhcp_v6   { ip fe80::1                   mac 00:1c:73:00:00:53 }

compile {
    client_v6 -> dhcp_v6 dhcp solicit txid 0x102030 wait 3ms
    client_v6 <- dhcp_v6 dhcp advertise txid 0x102030 wait 2ms
    client_v6 -> dhcp_v6 dhcp request txid 0x102031 wait 2ms
    client_v6 <- dhcp_v6 dhcp reply txid 0x102031
}
```

## 06.03 · Nanosecond IEEE 1588 PTPv2 Two-Step Sync Burst (.pcapng Output) (`ptpv2_nanosecond_sync.frasm`)

Emits a grandmaster PTPv2 Sync (UDP 319), Follow_Up (UDP 320), Delay_Req (UDP 319), and Delay_Resp (UDP 320) spaced at sub-microsecond 250ns intervals.

- **Protocols:** `ptp` (4 Ethernet frames)
- **Wire Behavior:** Compile to .pcapng so IfTsResol(9) preserves exact 1-nanosecond timestamps on UDP ports 319 and 320.
- **Wireshark Filter:** `ptp`
- **CLI Build:** `frameassembly ptpv2_nanosecond_sync.frasm ptpv2_nanosecond_sync.pcapng`

```frameassembly
host gm_clock    { ip 10.158.0.1 mac 00:1c:73:15:88:01 }
host boundary_sw { ip 10.158.0.2 mac 00:1c:73:00:15:88 }

compile {
    gm_clock -> boundary_sw ptp sync domain 24 seqid 900 clockid "001c73fffe000001" dscp 46 wait 250ns
    gm_clock -> boundary_sw ptp followup domain 24 seqid 900 clockid "001c73fffe000001" dscp 46 wait 500ns
    boundary_sw -> gm_clock ptp delayreq domain 24 seqid 45 clockid "001c73fffe001588" dscp 46 wait 250ns
    boundary_sw <- gm_clock ptp delayresp domain 24 seqid 45 clockid "001c73fffe000001" dscp 46
}
```

## 06.04 · UDP Service Probe (DNS 53, NTP 123 & Closed Port ICMP Unreachable) (`scan_udp_services.frasm`)

Probes UDP 53 (DNS), UDP 123 (NTP), and closed UDP 1900 on an infrastructure host to show open UDP protocol replies vs. ICMP Port Unreachable (Type 3, Code 3).

- **Protocols:** `dns`, `ntp`, `udp`, `icmp` (6 Ethernet frames)
- **Wire Behavior:** Closed UDP ports do not send TCP RSTs—instead, the target OS sends an ICMP Destination Unreachable (Code 3: Port Unreachable) packet.
- **Wireshark Filter:** `dns || ntp || udp.port == 1900 || icmp`
- **CLI Build:** `frameassembly scan_udp_services.frasm`

```frameassembly
// Audit host checking active UDP services on an internal infrastructure server
host audit_pc  { ip 10.15.0.25 }
host infra_srv { ip 10.15.1.10 }

compile {
    // 1. UDP 53 (DNS) -> Open: answers standard A-record query
    audit_pc -> infra_srv dns query txid 0x1001 qname "ns1.corp.local" qtype "A" wait 2ms
    audit_pc <- infra_srv dns response txid 0x1001 qname "ns1.corp.local" qtype "A" rdata "10.15.1.10" wait 3ms

    // 2. UDP 123 (NTP) -> Open: answers client mode 3 poll with server mode 4 stratum 2
    audit_pc -> infra_srv ntp vn 4 mode 3 poll 6 wait 2ms
    audit_pc <- infra_srv ntp vn 4 mode 4 stratum 2 refid "GPS" wait 3ms

    // 3. UDP 1900 -> Closed: server OS returns ICMP Port Unreachable (Type 3, Code 3)
    audit_pc -> infra_srv udp srcport 55200 dstport 1900 payload "M-SEARCH" wait 2ms
    infra_srv -> audit_pc icmp destunreach type 3 code 3
}
```

## 06.05 · Minimal Two-Block DNS (UDP/TCP) (`dns`) Quickstart (`dns_quickstart.frasm`)

Smallest zero-boilerplate `host` + `compile` capture for Domain Name System over UDP or TCP (UDP 53 (or TCP 53 via transport tcp), Dual-Stack IPv4/IPv6).

- **Protocols:** `dns` (2 Ethernet frames)
- **Wire Behavior:** 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.
- **Wireshark Filter:** `dns`
- **CLI Build:** `frameassembly 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
}
```

## 06.06 · Minimal Two-Block DHCPv4 / DHCPv6 (`dhcp`) Quickstart (`dhcp_quickstart.frasm`)

Smallest zero-boilerplate `host` + `compile` capture for Dynamic Host Configuration Protocol — DHCPv4 & DHCPv6 (UDP 68/67 (DHCPv4) · UDP 546/547 (DHCPv6), Dual-Stack Morphing (v4/v6)).

- **Protocols:** `dhcp` (2 Ethernet frames)
- **Wire Behavior:** Selects UDP `68 -> 67` for client messages (`discover`, `request`, `release`) and UDP `67 -> 68` for server messages (`offer`, `ack`) on IPv4—or UDP `546 <-> 547` on IPv6.
- **Wireshark Filter:** `dhcp || dhcpv6`
- **CLI Build:** `frameassembly dhcp_quickstart.frasm`

```frameassembly
host client_pc { ip 0.0.0.0 mac 00:1b:21:44:55:66 }
host dhcp_srv  { ip 10.10.10.1 mac 00:1b:21:00:00:01 }

compile {
    client_pc -> dhcp_srv dhcp discover xid 0x3903f326 chaddr 00:1b:21:44:55:66 hostname "eng-laptop-04" wait 2ms
    client_pc <- dhcp_srv dhcp offer xid 0x3903f326 yiaddr 10.10.10.150 siaddr 10.10.10.1 chaddr 00:1b:21:44:55:66 subnetmask 255.255.255.0 router 10.10.10.1 dnsserver 10.10.10.53 leasetime 86400
}
```

## 06.07 · Minimal Two-Block PTPv2 (IEEE 1588) (`ptp`) Quickstart (`ptp_quickstart.frasm`)

Smallest zero-boilerplate `host` + `compile` capture for IEEE 1588 Precision Time Protocol v2 (UDP 319 (Event) / UDP 320 (General), Dual-Stack IPv4/IPv6).

- **Protocols:** `ptp` (2 Ethernet frames)
- **Wire Behavior:** Routes time-critical Event messages (`sync`, `delayreq`) to UDP `319` and General messages (`followup`, `delayresp`, `announce`) to UDP `320` automatically.
- **Wireshark Filter:** `ptp`
- **CLI Build:** `frameassembly ptp_quickstart.frasm`

```frameassembly
host grandmaster { ip 10.60.1.1 }
host boundary_sw { ip 10.60.1.10 }

compile {
    grandmaster -> boundary_sw ptp sync domain 0 seqid 100 clockid "001c73fffe000001" wait 200us
    grandmaster -> boundary_sw ptp followup domain 0 seqid 100 clockid "001c73fffe000001"
}
```

## 06.08 · DNS (UDP/TCP) (`dns`): 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`.

- **Protocols:** `dns`, `tcp` (9 Ethernet frames)
- **Wire Behavior:** 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.
- **Wireshark Filter:** `dns`
- **CLI Build:** `frameassembly show dns_udp_and_tcp_lab.frasm`

```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
}
```

## 06.09 · mDNS (Zero-Conf) (`mdns`): Dual-Stack DNS-SD Service Browse (`PTR`) and Host Address Announcement (`AAAA`) (`mdns_ipv6_matter_discovery.frasm`)

Discovers `_matterc._udp.local` commissioning nodes and announces an IPv6 link-local service record over VLAN 30.

- **Protocols:** `mdns`, `vlan` (3 Ethernet frames)
- **Wire Behavior:** Overrides the L3 destination IP to link-local multicast `224.0.0.251` (IPv4) or `ff02::fb` (IPv6) with matching multicast Ethernet MAC (`01:00:5e:00:00:fb` / `33:33:00:00:00:fb`). Fixes both UDP source and destination ports to `5353 -> 5353` and defaults `txid` to `0` per RFC 6762.
- **Wireshark Filter:** `mdns`
- **CLI Build:** `frameassembly show mdns_ipv6_matter_discovery.frasm`

```frameassembly
host controller { ip fe80::10 mac 00:1c:73:aa:00:10 }
host sensor_hub { ip fe80::55 mac 00:1c:73:aa:00:55 }

compile {
    // 1. Multicast browse query to ff02::fb on UDP 5353 -> 5353
    controller -> sensor_hub mdns query qname "_matter._tcp.local" qtype "PTR" vlan 30 ttl 255 wait 2ms
    // 2. Hub announces PTR instance and AAAA address record
    sensor_hub -> controller mdns response qname "_matter._tcp.local" qtype "PTR" rdata "hub-01._matter._tcp.local" ansttl 4500 vlan 30 ttl 255 wait 1ms
    sensor_hub -> controller mdns response qname "hub-01.local" qtype "AAAA" rdata "fe80::55" ansttl 120 vlan 30 ttl 255
}
```

## 06.10 · DHCPv4 / DHCPv6 (`dhcp`): Complete 4-Step DHCPv4 DORA Lease Workflow + DHCPv6 SARR Exchange (`dhcp_v4_dora_and_v6_sarr.frasm`)

Captures a full 4-frame IPv4 `discover -> offer -> request -> ack` cycle on VLAN 10 followed by a 4-frame IPv6 `solicit -> advertise -> request -> reply` exchange.

- **Protocols:** `dhcp`, `vlan` (8 Ethernet frames)
- **Wire Behavior:** Selects UDP `68 -> 67` for client messages (`discover`, `request`, `release`) and UDP `67 -> 68` for server messages (`offer`, `ack`) on IPv4—or UDP `546 <-> 547` on IPv6. Writes the 4-byte RFC 2132 DHCP magic cookie (`0x63825363`), packs every specified option TLV (`1`, `3`, `6`, `12`, `51`, `53`), and terminates with Option `255` (`0xff`).
- **Wireshark Filter:** `dhcp || dhcpv6`
- **CLI Build:** `frameassembly show dhcp_v4_dora_and_v6_sarr.frasm`

```frameassembly
host phone_v4 { ip 0.0.0.0 mac 00:50:56:aa:10:01 }
host srv_v4   { ip 10.20.10.1 mac 00:50:56:aa:00:01 }
host host_v6  { ip fe80::25 mac 00:50:56:bb:10:25 }
host srv_v6   { ip fe80::1 mac 00:50:56:bb:00:01 }

compile {
    // 1. DHCPv4 DORA on VLAN 10 (UDP 68 <-> 67)
    phone_v4 -> srv_v4 dhcp discover xid 0x77889900 chaddr 00:50:56:aa:10:01 hostname "voip-desk-12" vlan 10 wait 2ms
    phone_v4 <- srv_v4 dhcp offer xid 0x77889900 yiaddr 10.20.10.112 siaddr 10.20.10.1 chaddr 00:50:56:aa:10:01 subnetmask 255.255.255.0 router 10.20.10.1 dnsserver 10.20.10.53 leasetime 43200 vlan 10 wait 1ms
    phone_v4 -> srv_v4 dhcp request xid 0x77889900 chaddr 00:50:56:aa:10:01 hostname "voip-desk-12" vlan 10 wait 1ms
    phone_v4 <- srv_v4 dhcp ack xid 0x77889900 yiaddr 10.20.10.112 siaddr 10.20.10.1 chaddr 00:50:56:aa:10:01 subnetmask 255.255.255.0 router 10.20.10.1 dnsserver 10.20.10.53 leasetime 43200 vlan 10 wait 5ms
    // 2. DHCPv6 SARR over IPv6 link-local endpoints (UDP 546 <-> 547)
    host_v6 -> srv_v6 dhcp solicit xid 0x00a1b2c3 dnsserver 2001:db8:20::53 wait 2ms
    host_v6 <- srv_v6 dhcp advertise xid 0x00a1b2c3 dnsserver 2001:db8:20::53 wait 1ms
    host_v6 -> srv_v6 dhcp request xid 0x00a1b2c3 wait 1ms
    host_v6 <- srv_v6 dhcp reply xid 0x00a1b2c3 dnsserver 2001:db8:20::53
}
```

## 06.11 · NTPv4 (`ntp`): Stratum 1 Synchronization + Kiss-o'-Death Rate-Limit & Mode 6 Audit Probe (`ntp_sync_and_security_audit.frasm`)

Captures normal NTPv4 client/server clock sync, a `stratum 0 refid "RATE"` Kiss-o'-Death response, and a blocked `mode 6` control query for IDS testing.

- **Protocols:** `ntp` (4 Ethernet frames)
- **Wire Behavior:** Packs the first byte `(li << 6) | (vn << 3) | mode` cleanly (`0x23` for `li 0 vn 4 mode 3` client; `0x24` for `mode 4` server) inside a 48-byte RFC 5905 header. Zero-pads 4-character ASCII `refid` strings (`"GPS\0"`, `"PPS\0"`, `"RATE"`) for Stratum 0/1 servers.
- **Wireshark Filter:** `ntp`
- **CLI Build:** `frameassembly show ntp_sync_and_security_audit.frasm`

```frameassembly
host branch_rtr { ip 172.16.30.1 }
host ntp_core   { ip 172.16.30.123 }

compile {
    // 1. Standard NTPv4 Client (mode 3) -> Stratum 1 GPS Server (mode 4)
    branch_rtr -> ntp_core ntp vn 4 mode 3 poll 6 precision -18 dscp 48 wait 2ms
    branch_rtr <- ntp_core ntp li 0 vn 4 mode 4 stratum 1 poll 6 precision -24 refid "GPS" txtimestamp 0xe9b4d01080000000 dscp 48 wait 5ms
    // 2. RFC 5905 Kiss-o'-Death (stratum 0, li 3, refid "RATE")
    branch_rtr <- ntp_core ntp li 3 vn 4 mode 4 stratum 0 refid "RATE" wait 5ms
    // 3. Mode 6 NTP Control query probe (tested against perimeter IDS/firewall)
    branch_rtr -> ntp_core ntp vn 2 mode 6
}
```

## 06.12 · PTPv2 (IEEE 1588) (`ptp`): Complete Two-Step IEEE 1588v2 Grandmaster <-> Ordinary Clock Exchange (.pcapng Ready) (`ptp_twostep_grandmaster.frasm`)

Executes a 5-frame PTPv2 `announce -> sync -> followup -> delayreq -> delayresp` cycle with sub-millisecond `wait` intervals ideal for `.pcapng` nanosecond export.

- **Protocols:** `ptp` (5 Ethernet frames)
- **Wire Behavior:** Routes time-critical Event messages (`sync`, `delayreq`) to UDP `319` and General messages (`followup`, `delayresp`, `announce`) to UDP `320` automatically. Computes exact IEEE 1588-2008 `messageLength` (`44` bytes for `sync`/`followup`/`delayreq`, `54` bytes for `delayresp`, `64` bytes for `announce`) and sets `twoStepFlag` on `sync`.
- **Wireshark Filter:** `ptp`
- **CLI Build:** `frameassembly show ptp_twostep_grandmaster.frasm`

```frameassembly
host gm_clock    { ip 172.22.0.1 }
host media_node  { ip 172.22.0.45 }

compile {
    // 1. Grandmaster announces BMCA hierarchy on UDP 320 (domain 127 = SMPTE ST 2059-2)
    gm_clock -> media_node ptp announce domain 127 seqid 500 clockid "ec4670fffe010203" dscp 46 wait 500us
    // 2. Two-step Sync (UDP 319) + Follow_Up (UDP 320) with matching seqid 501
    gm_clock -> media_node ptp sync domain 127 seqid 501 clockid "ec4670fffe010203" dscp 46 wait 120us
    gm_clock -> media_node ptp followup domain 127 seqid 501 clockid "ec4670fffe010203" dscp 46 wait 250us
    // 3. Slave measures reverse path delay via Delay_Req (UDP 319) -> Delay_Resp (UDP 320)
    media_node -> gm_clock ptp delayreq domain 127 seqid 88 clockid "001b21fffeaa0045" dscp 46 wait 180us
    media_node <- gm_clock ptp delayresp domain 127 seqid 88 clockid "ec4670fffe010203" dscp 46
}
```

## 06.13 · SSDP (UPnP) (`ssdp`): IoT Gateway SSDP M-SEARCH Sweep & UPnP IGD Alive Notification (`ssdp_upnp_audit.frasm`)

Sends a multicast `msearch` looking for UPnP Internet Gateway Devices, followed by a router `notify` advertisement pointing to its XML description URL.

- **Protocols:** `ssdp` (2 Ethernet frames)
- **Wire Behavior:** Defaults UDP destination port to `1900` and automatically derives the RFC 1112 multicast MAC `01:00:5e:7f:ff:fa` when sending to `239.255.255.250`. Formats `M-SEARCH * HTTP/1.1
` with `HOST`, `MAN: "ssdp:discover"`, `MX`, and `ST` headers on `msearch` packets.
- **Wireshark Filter:** `ssdp`
- **CLI Build:** `frameassembly show ssdp_upnp_audit.frasm`

```frameassembly
host scanner   { ip 192.168.10.55 }
host upnp_grp  { ip 239.255.255.250 }
host rogue_rtr { ip 192.168.10.254 }

compile {
    scanner -> upnp_grp ssdp msearch hosthdr "239.255.255.250:1900" man "\"ssdp:discover\"" mx 2 st "urn:schemas-upnp-org:device:InternetGatewayDevice:1" wait 5ms
    rogue_rtr -> upnp_grp ssdp notify hosthdr "239.255.255.250:1900" nt "urn:schemas-upnp-org:device:InternetGatewayDevice:1" nts "ssdp:alive" usn "uuid:f81d4fae-7dec::upnp:rootdevice" location "http://192.168.10.254:49152/rootDesc.xml"
}
```
