MX record lookup¶
This operation is the library's dedicated convenience path for mail-exchanger resolution. Rather than asking callers to assemble a Question themselves, it builds a single MX query, delegates the network round trip to Generic DNS lookup, then narrows and orders the reply so the caller receives a ready-to-use, preference-sorted list of mail exchangers.
| Field | Value |
|---|---|
| Operation ID | OP-DNS-001 |
| Trigger | Caller requests MX records for a domain |
| Inputs | domain, dnsServer |
| Outputs | Sorted MXRecord[] (or null); may throw ArgumentNullException, ArgumentException, ArgumentOutOfRangeException, NoResponseException, or InvalidResponseException |
| Primary components | Resolver.MXLookup, Request, Question, Resolver.Lookup, MXRecord.CompareTo |
| Entry point | SAIF.Framework.DNS\Resolver.cs |
Steps¶
- Confirm that
domainanddnsServerare not null before any request object is created (BR-001). - On failure: throw
ArgumentNullExceptionand stop; no request is sent. - Create a new
Request, taking the class defaults of recursion-desired querying and the standard-query opcode. - Create a
Questionfor the supplieddomain, fixing the type toDnsType.MXand the class toDnsClass.IN. - The constructor rejects domain strings shorter than 1 character or longer than 255 characters (BR-002).
- The constructor rejects an undefined or
NoneDnsType(BR-003). - The constructor rejects an undefined or
NoneDnsClass(BR-004). - Add the question to the request, making it the only question in the outbound message.
- Delegate to Generic DNS lookup to serialize, transmit, and parse the request/response round trip.
- If the delegated lookup throws
NoResponseExceptionorInvalidResponseException, let the exception propagate unchanged to this operation's caller. - Check whether the returned
Responseisnull. - If
null: returnnullimmediately, without inspecting any answers. - Walk
response.Answersand retain only the entries whose runtime type is exactlyMXRecord. - If an entry is any other record type: discard it and continue to the next answer.
- Copy the retained MX entries into a typed
MXRecord[]. - Sort the array with
Array.Sort, which relies onMXRecord.CompareToto place lower preference numbers first and to break equal-preference ties by descending domain-name comparison (BR-008). - Return the sorted
MXRecord[]to the caller.
Process flow¶
The following diagram visualizes this operation's control flow.
flowchart TD
Start([Caller invokes MXLookup]) --> V[Validate domain and dnsServer]
V --> Q[Build Request with MX Question]
Q --> L[Delegate to Generic DNS lookup]
L --> D{Response is null?}
D -->|Yes| EndNull([Return null])
D -->|No| F[Filter Answers to MXRecord entries]
F --> S[Sort by ascending preference]
S --> EndOk([Return sorted MXRecord array])
Failure and edge-case behavior¶
- A null
domainordnsServerfails immediately withArgumentNullException, before any socket is opened. - An out-of-range domain length, or an invalid
DnsType/DnsClass, fails while theQuestionis being constructed. - Answer entries that are not
MXRecordare silently ignored rather than converted or reported. - A successful response containing zero MX answers yields an empty array, which is distinct from the
nullreturned when the underlying response itself isnull. - Any transport (
NoResponseException) or parsing (InvalidResponseException) failure raised by the delegated lookup is returned to the caller unchanged.
Related documentation¶
Sources¶
- SAIF.Framework.DNS\Resolver.cs
- SAIF.Framework.DNS\Question.cs
- SAIF.Framework.DNS\MXRecord.cs