test4.c 1.54 KB
O'Reilly Media, Inc. committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
// DNSServiceResolve example

#include <dns_sd.h>
#include <stdio.h>			// For stdout, stderr
#include <string.h>			// For strlen(), strcpy(), bzero()

extern void HandleEvents(DNSServiceRef);

static void
MyResolveCallBack(DNSServiceRef serviceRef,
				  DNSServiceFlags flags,
				  uint32_t interface,
				  DNSServiceErrorType errorCode,
				  const char *fullname,
				  const char *hosttarget,
				  uint16_t port,
				  uint16_t txtLen,
				  const char *txtRecord,
				  void *context)
	{
	#pragma unused(flags)
	#pragma unused(fullname)

	if (errorCode != kDNSServiceErr_NoError)
		fprintf(stderr, "MyResolveCallBack returned %d\n", errorCode);
	else
		printf("RESOLVE: %s is at %s:%d\n", fullname, hosttarget, ntohs(port));
	if (!(flags & kDNSServiceFlagsMoreComing)) fflush(stdout);
	}


static DNSServiceErrorType MyDNSServiceResolve()
	{
	DNSServiceErrorType error;
	DNSServiceRef  serviceRef;
	
	error = DNSServiceResolve(&serviceRef,
							   0,  // no flags
							   0,  // all network interfaces
							   "Not a real page", //name
							   "_http._tcp", // service type
							   "local", //domain
							   MyResolveCallBack,
							   NULL);	 // no context					
	
	if (error == kDNSServiceErr_NoError)
		{
		HandleEvents(serviceRef);  // Add service to runloop to get callbacks
		DNSServiceRefDeallocate(serviceRef);
		}
	return error;
	}

int main (int argc, const char * argv[])
	{
	DNSServiceErrorType error = MyDNSServiceResolve();
	fprintf(stderr, "DNSServiceDiscovery returned %d\n", error);
									//if function returns print error
	return 0;
	}