objective-cxcodemacosdnsdns-sd

DNS Query using DNSServiceQueryRecord for kDNSServiceType_MX one entry returned


I'm using DNSServiceQueryRecord to get kDNSServiceType_MX using following:

DNSServiceQueryRecord(&self->_sdRef,
                      kDNSServiceFlagsReturnIntermediates,
                      0,
                      srvNameCStr, //for example "google.com"
                      kDNSServiceType_MX,
                      kDNSServiceClass_IN,
                      QueryRecordCallback,
                      (__bridge void *)(self)
                      );

and call back is called but there is only one entry:

NSMutableData *         rrData;
dns_resource_record_t * rr;
uint8_t                 u8;
uint16_t                u16;
uint32_t                u32;

assert(rdata != NULL);
assert(rdlen < 65536);
rrData = [NSMutableData data];
assert(rrData != nil);

u8 = 0;
[rrData appendBytes:&u8 length:sizeof(u8)];
u16 = htons(kDNSServiceType_MX);
[rrData appendBytes:&u16 length:sizeof(u16)];
u16 = htons(kDNSServiceClass_IN);
[rrData appendBytes:&u16 length:sizeof(u16)];
u32 = htonl(666);
[rrData appendBytes:&u32 length:sizeof(u32)];
u16 = htons(rdlen);
[rrData appendBytes:&u16 length:sizeof(u16)];
[rrData appendBytes:rdata length:rdlen];

rr = dns_parse_resource_record([rrData bytes], (uint32_t) [rrData length]);
assert(rr != NULL);

if (rr != NULL)
{
    NSString *result = nil;
    result = [NSString stringWithCString:rr->data.MX->name encoding:NSASCIIStringEncoding];
    dns_free_resource_record(rr);
}

so this returns only one entry. How to get the list of MX? What am I doing wrong? Thanks in advance!


Solution

  • Ok I have found that its better to do the query with select() and the result contains all MXes.

                while (dnsUpdateTimeout > 0)
                {
                    FD_ZERO(&readfds);
                    FD_SET(dns_sd_fd, &readfds);
    
                    struct timeval tv;
                    tv.tv_sec  = (time_t)dnsUpdateTimeout;
                    tv.tv_usec = (self.dnsUpdateTimeout - tv.tv_sec) * 1000000;
    
                    result = select(nfds, &readfds, (fd_set*)NULL, (fd_set*)NULL, &tv);
                    if (result == 1)
                    {
                        if (FD_ISSET(dns_sd_fd, &readfds))
                        {
                            err = DNSServiceProcessResult(sdRef); // triggers callback that will process the result
                            if (err != kDNSServiceErr_NoError)
                            {
                                NSLog(@"There was an error reading the DNS SRV records.");
                                break;
                            }
                        }
                    }
                    NSTimeInterval elapsed = [[NSDate date] timeIntervalSinceDate:startTime];
                    dnsUpdateTimeout -= elapsed;
                 }