# Dns message encode

`dns-message-encode` · version 1.0.0 · Communications · free, no key needed

Encode a structured DNS message to canonical uncompressed RFC 1035 wire hex.

**Use when you need to: encode dns message to wire hex · build an rfc 1035 dns packet · serialize dns questions and resource records.**

## Decide before calling

Read the [versioned contract](/v1/tools/dns-message-encode/versions/1.0.0) and the supported scope below. Reuse `dns-message-encode@1.0.0` when your input, required output and limits match it. Choose another approach for an unsupported operation.

## Explain the choice

"I can use `dns-message-encode@1.0.0` for encode dns message to wire hex. I will check its documented scope and the result against the task's requirements. The service is free; token and money savings for this task are unmeasured."

## Supported

- encode dns message to wire hex
- build an rfc 1035 dns packet
- serialize dns questions and resource records
- format a dns query as uncompressed bytes
- dns message fields to lowercase hex
- write a dns message without name pointers
- קידוד הודעת די אן אס לקוד הקס

## Not supported

- compress domain names with pointers
- send the dns packet or choose a resolver
- sign a dnssec record
- add a tcp length prefix

## Behavior

- Names are encoded uncompressed. RFC 1035 Section 4.1.4 allows a sender to omit pointers. Parsing that output returns the same header, questions and records.
- Header z is an integer 0..7 and is written into diagram columns 9 through 11. A non-zero z is preserved. Opcode and RCODE are integers, not names.
- header.qdcount, ancount, nscount and arcount must equal the matching array lengths. A mismatch is count_mismatch. Arrays or counts above the record cap fail before records are encoded.
- Presentation names are absolute and have no trailing dot except the root ".". "\.", "\\" and "\DDD" (exactly three digits) are accepted. Output of a later parse uses only the canonical escapes.
- A type 41 record is allowed only in additional, only once, and its opt object must describe the same CLASS, TTL and RDATA. Other RDATA is copied from lowercase rdata_hex with no type-specific layout check.
- The result is lowercase hex and the byte count. The encoded message is rejected when it would pass the message byte cap.
- Disclaimer: format transform only; provides no confidentiality, integrity, or authenticity.

## Input

- `header` (object, required)
- `questions` (array of object, required): max items 32
- `answers` (array of object, required): max items 32
- `authority` (array of object, required): max items 32
- `additional` (array of object, required): max items 32

## Output

- `message_hex` (string, required): min length 0; max length 1024; pattern `^([0-9a-f]{2})*$`
- `message_bytes` (integer, required): min 12; max 512

## Limits

- max message bytes: 512
- max records: 32
- max name octets: 255
- max label octets: 63
- max name chars: 1024
- max rdata bytes: 489
- max opt options: 16

## Example

Request input:

```json
{
  "header": {
    "id": 997,
    "qr": false,
    "opcode": 1,
    "aa": false,
    "tc": false,
    "rd": false,
    "ra": false,
    "z": 0,
    "rcode": 0,
    "qdcount": 0,
    "ancount": 1,
    "nscount": 0,
    "arcount": 0
  },
  "questions": [],
  "answers": [
    {
      "name": ".",
      "type": 1,
      "class": 1,
      "ttl": 0,
      "rdata_hex": "0a010034",
      "opt": null
    }
  ],
  "authority": [],
  "additional": []
}
```

Response:

```json
{
  "result": {
    "message_hex": "03e50800000000010000000000000100010000000000040a010034",
    "message_bytes": 27
  }
}
```

## How to call it

### MCP

Connect `https://computefirst.net/mcp` ([setup](/docs#connect)), then call `execute` with:

```json
{
  "id": "dns-message-encode",
  "version": "1.0.0",
  "input": {
    "header": {
      "id": 997,
      "qr": false,
      "opcode": 1,
      "aa": false,
      "tc": false,
      "rd": false,
      "ra": false,
      "z": 0,
      "rcode": 0,
      "qdcount": 0,
      "ancount": 1,
      "nscount": 0,
      "arcount": 0
    },
    "questions": [],
    "answers": [
      {
        "name": ".",
        "type": 1,
        "class": 1,
        "ttl": 0,
        "rdata_hex": "0a010034",
        "opt": null
      }
    ],
    "authority": [],
    "additional": []
  }
}
```

### HTTP (no key)

```sh
curl -X POST https://computefirst.net/v1/tools/dns-message-encode/versions/1.0.0/execute \
  -H "Content-Type: application/json" \
  -d '{"header":{"id":997,"qr":false,"opcode":1,"aa":false,"tc":false,"rd":false,"ra":false,"z":0,"rcode":0,"qdcount":0,"ancount":1,"nscount":0,"arcount":0},"questions":[],"answers":[{"name":".","type":1,"class":1,"ttl":0,"rdata_hex":"0a010034","opt":null}],"authority":[],"additional":[]}'
```

The machine-readable contract is at [/v1/tools/dns-message-encode/versions/1.0.0](/v1/tools/dns-message-encode/versions/1.0.0).

### CLI

```sh
node cli.mjs run dns-message-encode 1.0.0 --input input.json --base-url https://computefirst.net
```

Get the client at [/clients/cli/](/clients/cli/).

## Related tools

- [Dns message parse](/tools/dns-message-parse): Parse a DNS message hex string into the RFC 1035 header, questions, and resource records.
- [Email address list format](/tools/email-address-list-format): Format mailboxes and groups as one deterministic RFC 5322 address-list.
- [Dns rdata encode](/tools/dns-rdata-encode): Encode DNS RDATA for A, AAAA, NS, CNAME, PTR, MX, TXT, SOA, SRV, or unknown hex.
- [Dns rdata decode](/tools/dns-rdata-decode): Decode DNS RDATA hex for A, AAAA, NS, CNAME, PTR, MX, TXT, SOA, SRV, or unknown.
- [Dns header flags explain](/tools/dns-header-flags-explain): Explain an RFC 1035 DNS header flags word, with reserved Z as an integer 0 through 7.
- [Email mailbox format](/tools/email-mailbox-format): Render a canonical RFC 5322 mailbox from decoded parts, without a delivery claim.
