Redeye
Redeye

Reputation: 1602

DNS-SD on Windows using MFC

I have an application built using MFC that I need to add Bonjour/Zeroconf service discovery to. I've had a bit of trouble figuring out how best to do it, but I've settled on using the DLL stub provided in the mDNSresponder source code and linking my application to the static lib generated by that (which in turn uses the system dnssd.dll).

However, I'm still having problems as the callbacks don't always seem to be being called so my device discovery stalls. What confuses me is that it all works absolutely fine under OSX, using the OSX dns-sd terminal service and under Windows using the dns-sd command line service. On that basis, I'm ruling out the client service as being the problem and trying to figure out what's wrong with my Windows code.

I'm basically calling DNSBrowseService(), then in that callback calling DNSServiceResolve(), then finally calling DNSServiceGetAddrInfo() to get the IP address of the device so I can connect to it.

All of these calls are based on using WSAAsyncSelect like this :

DNSServiceErrorType err = DNSServiceResolve(&client,kDNSServiceFlagsWakeOnResolve,
                                                    interfaceIndex,
                                                    serviceName,
                                                    regtype,
                                                    replyDomain,
                                                    ResolveInstance,
                                                    context);

    if(err == 0) 
    {
        err = WSAAsyncSelect((SOCKET) DNSServiceRefSockFD(client), p->m_hWnd, MESSAGE_HANDLE_MDNS_EVENT, FD_READ|FD_CLOSE);
    }

But sometimes the callback just never gets called even though the service is there and using the command line will confirm that.

I'm totally stumped as to why this isn't 100% reliable, but it is if I use the same DLL from the command line. My only possible explanation is that the DNSServiceResolve function tries to call the callback function before the WSAAsyncSelect has registered the handling message for the socket, but I can't see any way around this.

I've spent ages on this and am now completely out of ideas. Any suggestions would be welcome, even if they're "that's a really dumb way to do it, why aren't you doing X, Y, Z".

Upvotes: 2

Views: 1988

Answers (1)

Patrick
Patrick

Reputation: 2337

I call DNSServiceBrowse, with a "shared connection" (see dns_sd.h for documentation) as in:

DNSServiceCreateConnection(&ServiceRef);
// Need to copy the main ref to another variable.
DNSServiceRef BrowseServiceRef = ServiceRef;
DNSServiceBrowse(&BrowseServiceRef,               // Receives reference to Bonjour browser object.
                 kDNSServiceFlagsShareConnection, // Indicate it's a shared connection.
                 kDNSServiceInterfaceIndexAny,    // Browse on all network interfaces.
                 "_servicename._tcp",             // Browse for service types.
                 NULL,                            // Browse on the default domain (e.g. local.).
                 BrowserCallBack,                 // Callback function when Bonjour events occur.
                 this);                           // Callback context.

This is inside a main run method of a thread class called ServiceDiscovery. ServiceRef is a member of ServiceDiscovery.

Then immediately following the above code, I have a main event loop like the following:

while (true)
{
   err = DNSServiceProcessResult(ServiceRef);
   if (err != kDNSServiceErr_NoError)
   {
      DNSServiceRefDeallocate(BrowseServiceRef);
      DNSServiceRefDeallocate(ServiceRef);
      ServiceRef = nullptr;
   }
}

Then, in BrowserCallback you have to setup the resolve request:

void DNSSD_API ServiceDiscovery::BrowserCallBack(DNSServiceRef inServiceRef,
                                                 DNSServiceFlags inFlags,
                                                 uint32_t inIFI,
                                                 DNSServiceErrorType inError,
                                                 const char* inName,
                                                 const char* inType,
                                                 const char* inDomain,
                                                 void* inContext)
{
   (void) inServiceRef; // Unused

   ServiceDiscovery* sd = (ServiceDiscovery*)inContext;
   ...
   // Pass a copy of the main DNSServiceRef (just a pointer).  We don't
   // hang to the local copy since it's passed in the resolve callback,
   // where we deallocate it.
   DNSServiceRef resolveServiceRef = sd->ServiceRef;
   DNSServiceErrorType err =
      DNSServiceResolve(&resolveServiceRef,
                        kDNSServiceFlagsShareConnection, // Indicate it's a shared connection.
                        inIFI,
                        inName,
                        inType,
                        inDomain,
                        ResolveCallBack,
                        sd);

Then in ResolveCallback you should have everything you need.

// Callback for Bonjour resolve events.
void DNSSD_API ServiceDiscovery::ResolveCallBack(DNSServiceRef inServiceRef,
                                                 DNSServiceFlags inFlags,
                                                 uint32_t inIFI,
                                                 DNSServiceErrorType inError,
                                                 const char* fullname,
                                                 const char* hosttarget,
                                                 uint16_t port,        /* In network byte order */
                                                 uint16_t txtLen,
                                                 const unsigned char* txtRecord,
                                                 void* inContext)
{
   ServiceDiscovery* sd = (ServiceDiscovery*)inContext;
   assert(sd);

   // Save off the connection info, get TXT records, etc.
   ...

   // Deallocate the DNSServiceRef.
   DNSServiceRefDeallocate(inServiceRef);
}

hosttarget and port contain your connection info, and any text records can be obtained using the DNS-SD API (e.g. TXTRecordGetCount and TXTRecordGetItemAtIndex).

With the shared connection references, you have to deallocate each one based on (or copied from) the parent reference when you are done with them. I think the DNS-SD API does some reference counting (and parent/child relationship) when you pass copies of a shared reference to one of their functions. Again, see the documentation for details.

I tried not using shared connections at first, and I was just passing down ServiceRef, causing it to be overwritten in the callbacks and my main loop to get confused. I imagine if you don't use shared connections, you need to maintain a list of references that need further processing (and process each one), then destroy them when you're done. The shared connection approach seemed much easier.

Upvotes: 1

Related Questions