1 Reply
      Latest reply on Feb 16, 2020 9:21 AM by eskimo
      adarsh rai Level 1 Level 1 (0 points)

        I was trying to implement FQDN to Address resolution using DNSServiceGetAddrInfo API of DNS_SD. Learning from this thread I wanted to use DNSServiceSetDispatchQueue to get results back asynchronously. Here is how I have implemented it.

         

        var result = DNSServiceGetAddrInfo(&dnsServiceRef, kDNSServiceFlagsTimeout, 0, DNSServiceFlags(kDNSServiceProtocol_IPv4|kDNSServiceProtocol_IPv6), hostName.cString(using: .utf8), dnssd_callback, pointer)
                if result != kDNSServiceErr_NoError || dnsServiceRef == nil {
                    delegate?.didFinishDNSResolution(.failed(.error(from: result)))
                    stopResolution()
                } else {
                    result = DNSServiceSetDispatchQueue(dnsServiceRef, DispatchQueue.main)
                    if result != kDNSServiceErr_NoError {
                        delegate?.didFinishDNSResolution(.failed(.error(from: result)))
                        stopResolution()
                    }
                }

        However, using this implementation I do not get callbacks. If I use DNSServiceProcessResult, I am getting responses successfully. If I call DNSServiceProcessResult after DNSServiceSetDispatchQueue, then also I receive callbacks successfully. But, that is discouraged in the documentation of DNSServiceSetDispatchQueue.

         

        > * After using DNSServiceSetDispatchQueue on a DNSServiceRef, calling DNSServiceProcessResult

        * on the same DNSServiceRef will result in undefined behavior and should be avoided.

         

        I tried DNSSDObjects and SRVResolver (with a bit change DNSServiceSetDispatchQueue) samples and they both work pretty fine. But when I use the same in my code, it does not work. So, is it possible that DNSServiceSetDispatchQueue can be used with every DNS_SD API except for DNSServiceGetAddrInfo? Or is there a missing piece?

         

        Any help would be greatly appreciated.

        • Re: DNSServiceSetDispatchQueue with DNSServiceGetAddrInfo does not return callback
          eskimo Apple Staff Apple Staff (12,975 points)

          I was trying to implement FQDN to Address resolution using DNSServiceGetAddrInfo API of DNS_SD.

          Is there a reason you need to do this resolution using this very low-level API?  There are good reasons to use DNSServiceGetAddrInfo, but most of the time you’re better served by one of the alternatives:

          • If you’re making a network connection, you should aim to use one of our connect-by-name APIs.  These do the resolution for you, and handle a bunch of ‘fun’ edge cases.

          • If you want to use map a DNS name to a set of addresses for other reasons — say for logging purposes — you can use CFHost.  See CFHostSample for the details.

          Share and Enjoy

          Quinn “The Eskimo!”
          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
          let myEmail = "eskimo" + "1" + "@apple.com"