Skip to content

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

  1. Confirm that domain and dnsServer are not null before any request object is created (BR-001).
  2. On failure: throw ArgumentNullException and stop; no request is sent.
  3. Create a new Request, taking the class defaults of recursion-desired querying and the standard-query opcode.
  4. Create a Question for the supplied domain, fixing the type to DnsType.MX and the class to DnsClass.IN.
  5. The constructor rejects domain strings shorter than 1 character or longer than 255 characters (BR-002).
  6. The constructor rejects an undefined or None DnsType (BR-003).
  7. The constructor rejects an undefined or None DnsClass (BR-004).
  8. Add the question to the request, making it the only question in the outbound message.
  9. Delegate to Generic DNS lookup to serialize, transmit, and parse the request/response round trip.
  10. If the delegated lookup throws NoResponseException or InvalidResponseException, let the exception propagate unchanged to this operation's caller.
  11. Check whether the returned Response is null.
  12. If null: return null immediately, without inspecting any answers.
  13. Walk response.Answers and retain only the entries whose runtime type is exactly MXRecord.
  14. If an entry is any other record type: discard it and continue to the next answer.
  15. Copy the retained MX entries into a typed MXRecord[].
  16. Sort the array with Array.Sort, which relies on MXRecord.CompareTo to place lower preference numbers first and to break equal-preference ties by descending domain-name comparison (BR-008).
  17. 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 domain or dnsServer fails immediately with ArgumentNullException, before any socket is opened.
  • An out-of-range domain length, or an invalid DnsType/DnsClass, fails while the Question is being constructed.
  • Answer entries that are not MXRecord are silently ignored rather than converted or reported.
  • A successful response containing zero MX answers yields an empty array, which is distinct from the null returned when the underlying response itself is null.
  • Any transport (NoResponseException) or parsing (InvalidResponseException) failure raised by the delegated lookup is returned to the caller unchanged.

Sources

  • SAIF.Framework.DNS\Resolver.cs
  • SAIF.Framework.DNS\Question.cs
  • SAIF.Framework.DNS\MXRecord.cs