dns.client.resolver:resolve

dns.client

resolver:resolve(qname, qtype) -> records, err

Resolve a domain name to records of the specified type

Answers A and AAAA for qname as typed from the hosts table first, when the resolver has one. Those records carry ttl 0 and skip the cache. A name the table lacks, or holds no address of the asked family for, goes to the servers.

Otherwise resolves qname by querying upstream servers with retry, failover, and optional caching. Follows CNAME chains (depth limit 10) and applies search list expansion based on ndots.

qtype is a record type as a string ("A", "AAAA", "MX") or number (1, 28, 15). String matching is case-insensitive.

Returns a list of matching answer records on success, each with name, type, class, ttl, and rdata fields. Returns nil, err on failure (NXDOMAIN, timeout, no servers, etc.).

local answers, err = resolver:resolve("example.com", "A")
if not answers then
    print("error: " .. err)
else
    for _, rr in ipairs(answers) do
        print(rr.rdata.address, "TTL", rr.ttl)
    end
end

-- MX records
local mx, err = resolver:resolve("gmail.com", "MX")
if mx then
    for _, rr in ipairs(mx) do
        print(rr.rdata.preference, rr.rdata.exchange)
    end
end