Just An Application

November 20, 2013

Service Discovery In Android And iOS: Part Eight – iOS Take Four

If DNSServiceBrowse and friends are not to your liking there is always the function DNSServiceQueryRecord which enables us to obtain DNS records directly.

1.0 DNSServiceQueryRecord

The DNSServiceQueryRecord function declaration follows the common function pattern and looks like this

    DNSServiceErrorType DNSServiceQueryRecord(
                            DNSServiceRef*             sdRef,
                            DNSServiceFlags            flags,
                            uint32_t                   interfaceIndex,
                            const char*                fullname,
                            uint16_t                   rrtype,
                            uint16_t                   rrclass,
                            DNSServiceQueryRecordReply callBack,
                            void*                      context);

The fullname should be the absolute name of the node for which the record or records are being requested.

The rrtype should be the type of the record or records being requested.

The rrclass should be the class of the record or records being requested.

If the kDNSServiceFlagsTimeout bit is set in the flags argument then the function will timeout after a system dependent amount of time.

2.0 The DNSServiceQueryRecord Callback Function

The callback function will be invoked

  • once for each record that is received in response to the query

  • once for each record that is received in response to the query that subsequently expires

  • if an error occurs

  • if a timeout occurs

3.0 DNSServiceQueryRecordReply

The DNSServiceQueryRecordReply function type declaration follows the common function type pattern and looks like this

    typedef void (*DNSServiceQueryRecordReply)(
                       DNSServiceRef       sdRef,
                       DNSServiceFlags     flags,
                       uint32_t            interfaceIndex,
                       DNSServiceErrorType errorCode,
                       const char*         fullname,
                       uint16_t            rrtype,
                       uint16_t            rrclass,
                       uint16_t            rdlen,
                       const void*         rdata,
                       uint32_t            ttl,
                       void*               context);

When a function of this type is invoked, then, if the errorCode argument is kDNSServiceErr_NoError

  • fullname is the absolute name of the node with the record is associated

  • rrtype is the type of the record

  • rrclass is the class of the record.

  • rdlen is the length of the record data

  • rdate is the record data

  • ttl is the time in seconds for which the record is valid

The flags argument will have the kDNSServiceFlagsAdd bit set if the callback is being invoked when a record is received in response to the query.

If kDNSServiceFlagsAdd bit is clear then callback is being invoked because the record has expired,
in which case the ttl argument will be 0.

If a timeout occurs the value of the errorCode argument will be kDNSServiceErr_Timeout.

3.0 The Query Class

We can encapsulate the call to DNSServiceQueryRecord and its associated callback function in a class
like so

    //  Query.m
    //  XperTakeFour
    //  Created by Simon Lewis on 08/11/2013.
    //  Copyright (c) 2013 Simon Lewis. All rights reserved.
    #import <dns_sd.h>
    #import "QueryDelegate.h"
    #import "Record.h"
    #import "Query.h"
    @interface Query()
    @property NSString* name;
    @property uint16_t  type;
    @property uint16_t class;
    - (void)record:(const Record*)theRecord onInterface:(uint32_t)theIndex;
    - (void)recordExpired:(const Record*)theRecord;
    - (void)failed:(DNSServiceErrorType)theName;
    @implementation Query
        DNSServiceRef   ref;
    - (Query*)init:(NSString*)theName type:(uint16_t)theType class:(uint16_t)theClass
        self = [super init];
        if (self != nil)
            self.name  = theName;
            self.type  = theType;
            self.class = theClass;
        return self;
    static void queryCallback(
                    DNSServiceRef       sdRef,
                    DNSServiceFlags     theFlags,
                    uint32_t            theInterfaceIndex,
                    DNSServiceErrorType theErrorCode,
                    const char*         theName,
                    uint16_t            theType,
                    uint16_t            theClass,
                    uint16_t            theDataLength,
                    const void*         theData,
                    uint32_t           theTTL,
                    void*               theContext)
        NSLog(@"queryCallback: flags == %d error code == %d", theFlags, theErrorCode);
        if (theErrorCode != kDNSServiceErr_NoError)
            [(__bridge Query*)theContext failed:theErrorCode];
            NSLog(@"theName == %s theType == %u", theName, theType);
            Record rr = {
            if ((theFlags & kDNSServiceFlagsAdd) != 0)
                [(__bridge Query*)theContext record:&rr onInterface:theInterfaceIndex];
                [(__bridge Query*)theContext recordExpired:&rr];
    - (void)start:(DNSServiceRef)theServiceRef interface:(uint32_t)theInterfaceIndex timeout:(BOOL)timeout
        ref = theServiceRef;
        DNSServiceErrorType error;
        DNSServiceFlags     flags;
        flags = kDNSServiceFlagsShareConnection;
        if (timeout)
            flags |= kDNSServiceFlagsTimeout;
        error = DNSServiceQueryRecord(
                    [self.name UTF8String],
                    (__bridge void*)self);
        if (error != kDNSServiceErr_NoError)
            NSLog(@"DNSServiceQueryRecord: %d", error);
            [self.delegate queryDidFail:self withError:error];
    - (void)record:(const Record*)theRecord onInterface:(uint32_t)theIndex
        [self.delegate query:self didGetResponse:theRecord onInterface:theIndex];
    - (void)recordExpired:(const Record *)theRecord
        [self.delegate query:self recordDidExpire:theRecord];
    - (void)failed:(DNSServiceErrorType)theErrorCode
        if (theErrorCode != kDNSServiceErr_Timeout)
            [self.delegate queryDidFail:self withError:theErrorCode];
            [self.delegate queryDidTimeout:self];

4.0 Using The Query Class

The start method of FindServices v4 starts the search by querying for PTR records associated with the service type node.

    - (BOOL)start
        DNSServiceErrorType error = DNSServiceCreateConnection(&dnsServiceRef);
        if (error != kDNSServiceErr_NoError)
            NSLog(@"Error: DNSServiceCreateConnection %d", error);
            return NO;
        error = DNSServiceSetDispatchQueue(dnsServiceRef, dispatch_get_main_queue());
        if (error != kDNSServiceErr_NoError)
            NSLog(@"Error: DNSServiceSetDispatchQueue %d", error);
            return NO;
        self.ptrQuery = [[Query alloc] init:self.type type: kDNSServiceType_PTR class:kDNSServiceClass_IN];
        self.ptrQuery.delegate = self;
        [self.ptrQuery start:dnsServiceRef interface:kDNSServiceInterfaceIndexAny timeout:NO];
        return YES;

5.0 Examples

I will forgo the output from the examples as there is only so many console log messages anybody can be expected to find interesting.

Needless to say everything works in pretty much the same way as it did in the three preceding incarnations of the
FindServices class.

Copyright (c) 2013 By Simon Lewis. All Rights Reserved.

Unauthorized use and/or duplication of this material without express and written permission from this blog’s author and owner Simon Lewis is strictly prohibited.

Excerpts and links may be used, provided that full and clear credit is given to Simon Lewis and justanapplication.wordpress.com with appropriate and specific direction to the original content.


Create a free website or blog at WordPress.com.

%d bloggers like this: