我一直在寻找一段时间没有任何成功,我怎么能在Objective-C中获取域的DNS TXT
记录?
我的目标是获得与dig -t txt google.com +short
PS:请NSTask
/请注意! (我现在正在这样做,但这很糟糕)。代码可以用C编写,我稍后会写一个包装器。我不关心AppStore规则。
谢谢!
答案 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);
}
}
答案 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++;
}