DNS request serialization¶
This operation turns a Request's in-memory state into the raw byte array that DNS servers expect on the wire. It writes the header, the section counts, and each encoded question in order, leaving the two transaction-id bytes blank so the transport stage can stamp them immediately before the datagram is sent.
| Field | Value |
|---|---|
| Operation ID | OP-DNS-003 |
| Trigger | Generic DNS lookup needs request bytes to transmit |
| Inputs | Opcode, RecursionDesired, the _questions collection |
| Outputs | Request byte[] |
| Primary components | Request.GetMessage, Request.AddDomain |
| Entry point | SAIF.Framework.DNS\Request.cs |
Steps¶
- Create a growable
ArrayListto accumulate the message bytes; the final length is not known until every question is encoded. - Reserve the first two bytes as zero-value placeholders for the transaction identifier; the transport stage overwrites these bytes with a real id immediately before sending.
- Write the first flag byte by combining the request's
Opcodewith the recursion-desired bit whenRecursionDesiredis set. - Write a zero-value second flag byte.
- Write the question count as a two-byte value taken from
_questions.Count. - Write zero counts for the answer, name-server, and additional-record sections, because this library only ever sends a question section.
- Iterate each
Questionin_questions, in the order they were added. - For each question, encode the domain name using
AddDomain. - Split the dotted domain name into its labels.
- Append each label's length byte, followed immediately by that label's character bytes.
- Append a trailing zero byte to terminate the domain name.
- After the encoded domain, append the question's type and class, each as a two-byte value with a zero high byte.
- Copy the accumulated bytes into a fixed-length
byte[]and return it.
Process flow¶
The following diagram visualizes this operation's control flow.
flowchart TD
Start([GetMessage called]) --> H[Reserve id bytes and write flags]
H --> C[Write question count and zeroed section counts]
C --> Q[Encode each question: domain, type, class]
Q --> A[Copy accumulated bytes to fixed byte array]
A --> EndOk([Return request bytes])
Failure and edge-case behavior¶
- This operation performs no validation of its own; it assumes every
Questionalready holds a valid domain, type, and class, since those were checked when theQuestionwas constructed. - A request with zero questions still serializes successfully, producing a message with a question count of zero.
- A request with multiple questions serializes successfully as well, even though the source code's own comments note that many DNS servers do not support multi-question messages; the library does not enforce a single-question limit.
Related documentation¶
Sources¶
- SAIF.Framework.DNS\Request.cs