Objective-C:DNS TXT记录

时间:2013-06-26 21:31:21

标签: objective-c dns dig

我一直在寻找一段时间没有任何成功,我怎么能在Objective-C中获取域的DNS TXT记录?

我的目标是获得与dig -t txt google.com +short

相同的输出

PS:请NSTask /请注意! (我现在正在这样做,但这很糟糕)。代码可以用C编写,我稍后会写一个包装器。我不关心AppStore规则。

谢谢!

2 个答案:

答案 0 :(得分:3)

DNSServiceQueryRecord中使用dns_sd.h

#import <dns_sd.h>

// ...

DNSServiceRef serviceRef;

DNSServiceQueryRecord(&serviceRef, 0, 0, "hmspl.de", kDNSServiceType_TXT,
                      kDNSServiceClass_IN, queryCallback, NULL);

DNSServiceProcessResult(serviceRef);
DNSServiceRefDeallocate(serviceRef);

// ...

static void queryCallback(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) {

    if (errorCode == kDNSServiceErr_NoError && rdlen > 1) {
        NSMutableData *txtData = [NSMutableData dataWithCapacity:rdlen];

        for (uint16_t i = 1; i < rdlen; i += 256) {
            [txtData appendBytes:rdata + i length:MIN(rdlen - i, 255)];
        }

        NSString *theTXT = [[NSString alloc] initWithBytes:txtData.bytes length:txtData.length encoding:NSASCIIStringEncoding];
        NSLog(@"%@", theTXT);
    }
}

请参阅https://developer.apple.com/library/mac/#documentation/Networking/Reference/DNSServiceDiscovery_CRef/Reference/reference.html#//apple_ref/doc/c_ref/DNSServiceQueryRecord

答案 1 :(得分:0)

可以使用像这样的代码删除\ x02 \ x03,我删除\ 0x12 \ 0x17 使用$ man ascii check     char结果[256] = {0};             dn_expand(rdata,rdata + rdlen,rdata,result,256);

        NSMutableData *txtData = [NSMutableData dataWithCapacity:rdlen];

        char *p=(char*)rdata;

        for (uint16_t i = 0; i < rdlen; i++) {

            DLog(@"%c\n",*p);
            if (*p > 0x20) {
                [txtData appendBytes:p length:1];


            }
            p++;
        }