Search code examples
swiftdns

Using Apple's AsyncDNSResolver() and I need to change options servers and timeoutMillis. CAresDNSResolver.Options does not init


I am using Apple's AsyncDNSResolver (GitHub link) and would like to specify CAresDNSResolver.Options (timeout (2) and name servers (3) to query) when creating the AsyncDNSResolver(options:) (1) The problem is when I init var options: CAresDNSResolver.Options = CAresDNSResolver.Options() I receive an Xcode error 'CAresDNSResolver.Options' initializer is inaccessible due to 'internal' protection level.

What I am asking for is a way to init the options structure so I can pass it on the AsyncDNSResolver(). If not available, a way to set the name servers AsyncDNSResolver will use in its queries.

Thank you.

Here is the function, working except for when I want to set the options:

func getDmarcTxtWithDomainName(domainName: String, altDnsServer: String = "") async throws -> String {
    var dmarcString: String
    var resolver: AsyncDNSResolver
    do {
        
        if altDnsServer.isEmpty {
            resolver = try AsyncDNSResolver()
        } else {
            // TODO: set the resolver to use altDnsServer and timeout
            var options: CAresDNSResolver.Options = CAresDNSResolver.Options() // (1)
            options.servers = [altDnsServer]                                   // (2)
            options.timeoutMillis = 2000.                                      // (3)
            resolver = try AsyncDNSResolver(options: options)
       }
        let txtRecords = try await resolver.queryTXT(name: domainName)
        if txtRecords.count > 0 {
            dmarcString = subStringInTxtRecords(subString: "v=DMARC", txtRecords: txtRecords)
            if dmarcString.count == 0 {
                dmarcString = "None found"
            }
        } else {
            dmarcString = "None found"
        }
    } catch {
        debugLog(object: error.localizedDescription)
        throw error
    }
    return dmarcString
}


Solution

  • The problem was in the creation of var options. Changing the line to:

                var options = CAresDNSResolver.Options.default
    

    Resolved the problem.