Contents · Forum Home · StreamSec Tools 4.1.5.363 documentation

StreamSec.Mail.Dns

WHY THIS EXISTS AND WHY IT IS OVER TCP.

  Delivering mail without a smarthost needs the recipient domain's MX RRset, and
  nothing in this library could ask for a typed DNS record. There was also no way
  to build the usual answer: THE SOCKET ABSTRACTION HAS NO UDP AT ALL -
  tAbstractSocket is SOCK_STREAM in both backends - so a UDP resolver would have
  cost a new socket abstraction and two platform backends before a line of DNS.

  RFC 7766 makes DNS over TCP a first-class transport: the identical wire message
  behind a two-octet big-endian length prefix. That gives one code path, no
  dnsapi.dll, no libresolv (three different external targets, and absent from
  bionic), no 512-octet truncation dance, and a parser that can be driven from
  canned packets with no socket at all - which is how the vectors at the bottom
  test it.

  THE PARSER IS THE ATTACK SURFACE. Everything it reads came off the network from
  something that may not be a resolver at all, so every read is bounds-checked
  against the buffer, names are capped at 255 octets and labels at 63, and
  compression pointers are followed at most cDnsMaxPtrHops times - a pointer that
  points at itself is the classic way to hang a DNS parser for ever. A malformed
  message is rejected whole; nothing is returned "as far as it parsed".

  WHAT IT DOES NOT DO: no DNSSEC, so it reports no security status and cannot
  support DANE. A caller must treat every answer as UNAUTHENTICATED - which for
  MX delivery is exactly why the TLS policy there is unauthenticated too.

  SUBSTITUTABLE. Resolution goes through iDnsResolver, so DoH, DnsQuery_W or a
  validating stub can replace this without touching a caller, and a scripted
  resolver is what the delivery tests run against.

Feature notes: Mail subsystem notes (SMTP / POP3 / IMAP)

Classes, interfaces and records

classeDnsError

inherits from Exception

(no public members)

recordtDnsMxRecord

One MX RR. Exchange is absolute with the trailing dot stripped. AN EMPTY Exchange IS THE ROOT LABEL, i.e. a NULL MX (RFC 7505): the domain has published that it accepts no mail. Test it with DnsIsNullMx - an empty string here means something very specific and is not "missing".

Preference: Word; Exchange: string;

interfaceiDnsResolver

THE RESOLUTION SEAM. Substitute DoH, DnsQuery_W or a DNSSEC-validating stub without touching a caller; a scripted implementation is what lets the mail delivery tests run with no network. Every out-parameter is cleared before use. aDest lists are APPENDED to, never cleared, so several lookups can accumulate.

function LookupMx(const aDomain: string; out aRecords: tDnsMxArray): tDnsStatus; function LookupAddresses(const aHost: string; aWantIPv6: Boolean; aDest: TStrings): tDnsStatus;
Every A - and, when aWantIPv6, AAAA - address of aHost, as literals.
function LookupText(const aName: string; aDest: TStrings): tDnsStatus;
Every TXT record of aName, one string per record with its character-strings already concatenated (RFC 6376 and RFC 7208 both require that join, because a long policy is split into 255-octet chunks on the wire).
function LookupNames(const aAddress: string; aDest: TStrings): tDnsStatus;
Reverse lookup. aAddress is an address LITERAL, not an in-addr.arpa name - a caller should not have to know how the two families spell that. Every PTR name found is appended. A PTR ANSWER IS AN UNVERIFIED CLAIM by whoever runs the reverse zone, which is usually not whoever runs the forward zone. Nothing may be authorised on a PTR name without confirming it forward again.

classtDnsScriptResolver

inherits from TInterfacedObject

implements iDnsResolver

A resolver whose answers come from a SCRIPT rather than the network, so a policy test can pin every lookup and run with no DNS at all. Entries are 'TYPE:name=value' lines: TXT:example.com=v=spf1 -all A:mail.example.com=192.0.2.1 AAAA:mail.example.com=2001:db8::1 MX:example.com=10 mail.example.com PTR:192.0.2.1=mail.example.com Repeat a key for several records of one name. Two reserved values stand for the outcomes that are not simply "no answer": __NXDOMAIN__ and __SERVFAIL__. A NAME WITH NO ENTRY ANSWERS NODATA, NOT NXDOMAIN - they are different outcomes, and a rig that cannot tell them apart cannot test the split that decides between requeueing mail and bouncing it.

constructor Create; destructor Destroy; override; function LookupMx(const aDomain: string; out aRecords: tDnsMxArray): tDnsStatus; function LookupAddresses(const aHost: string; aWantIPv6: Boolean; aDest: TStrings): tDnsStatus; function LookupText(const aName: string; aDest: TStrings): tDnsStatus;
Delegates to Collect, forwarding 'TXT', aName and aDest, and returns its result.
function LookupNames(const aAddress: string; aDest: TStrings): tDnsStatus;
Delegates to Collect, forwarding 'PTR', aAddress and aDest, and returns its result.
property Script: TStringList read fScript; property Queries: Integer read fQueries;
How many queries have been asked. The SPF limits are counted in DNS lookups, so a test that asserts them has to be able to see this.

classtDnsTcpResolver

inherits from TInterfacedObject

implements iDnsResolver

RFC 7766 DNS over TCP against the system - or configured - recursive resolvers, with a TTL-bounded cache. THREAD-SAFE: the spool calls it from several delivery workers at once.

constructor Create; destructor Destroy; override; function LookupMx(const aDomain: string; out aRecords: tDnsMxArray): tDnsStatus; function LookupAddresses(const aHost: string; aWantIPv6: Boolean; aDest: TStrings): tDnsStatus; function LookupText(const aName: string; aDest: TStrings): tDnsStatus; function LookupNames(const aAddress: string; aDest: TStrings): tDnsStatus; procedure FlushCache; property Servers: TStrings read GetServers;
Empty means "discover from the platform at first use". Setting any entry stops discovery - which is how a container, or an Android build where neither discovery mechanism exists, gets a resolver at all.
property Timeout: Cardinal read fTimeout write fTimeout; property DeadlineTick: UInt64 read fDeadlineTick write fDeadlineTick;
Absolute stTlsUtils.GetTickCount64 deadline for network exchanges; zero preserves the existing per-progress timeout behavior. Configure while idle. Deadline-bound exchanges require literal server addresses, because platform hostname resolution cannot be given this deadline.
property Port: Word read fPort write fPort;
The TCP port every server in Servers is asked on: 53 (cDnsPort) unless set, for a resolver that listens elsewhere - a local forwarder, or a test responder that cannot bind a port below 1024.
property MaxCacheEntries: Integer read fMaxCacheEntries write fMaxCacheEntries; property WantIPv6: Boolean read fWantIPv6 write fWantIPv6;

Types (enumerations, sets, aliases)

enumtDnsStatus

WHAT A LOOKUP CAME TO, and the 4xx/5xx split for mail lives in this enum. Conflating any two of the first three either strands mail during a resolver outage or bounces mail that would have been deliverable.

  tDnsStatus = (
    { At least one record of the type asked for. }
    dsOk,
    { The NAME EXISTS but has no record of this type (NOERROR/NODATA). For MX
      this is not a failure at all - it is the condition under which RFC 5321
      sec.5.1's implicit MX applies and the domain's own address is used. }
    dsNoData,
    { NXDOMAIN. The name does not exist and will not start existing because you
      retried - PERMANENT, i.e. bounce rather than requeue. }
    dsNoName,
    { SERVFAIL, REFUSED, a timeout, a malformed reply, no reachable server.
      TEMPORARY: requeue. Bouncing on this turns a resolver outage into
      permanent mail loss. }
    dsTempFail,
    { No resolver address is configured and none could be discovered. A
      configuration fault rather than a network one, reported separately so it
      can be said plainly in a log. }
    dsNoServer);

enumtDnsRecordType

  tDnsRecordType = (drtA, drtAAAA, drtMX, drtTXT, drtPTR);

arraytDnsMxArray

  tDnsMxArray = array of tDnsMxRecord;

Routines