gpt4 book ai didi

ios - 查询 DNS 以在 iOS 中查找 NAPTR

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:47:16 24 4
gpt4 key购买 nike

我在寻找查询 DNS 以在 iOS 中查找 NAPTR 的方法时遇到了很多麻烦。似乎有很多相对简单的方法来解析 IP,但我特别需要在 DNS 查找中找到所有 NAPTR 记录。如果可能的话,我更愿意这样做而不必引入任何外部库。如果有人能够做到这一点(或我可以推断出的类似事情),我将不胜感激。

所有代码都必须在 iOS 5.0+ 中运行

最佳答案

我最终使用了 DNSServiceQueryRecord。

            DNSServiceRef sdRef;
DNSServiceQueryRecord(&sdRef, 0, 0,
"google.com",
kDNSServiceType_NAPTR,
kDNSServiceClass_IN,
callback,
NULL);

DNSServiceProcessResult(sdRef);
DNSServiceRefDeallocate(sdRef);

在实际使用中,我发现如果没有结果,应用程序会无限期挂起,所以我最终不得不调整我的代码以在结果上添加超时。

/*
Attempt to fetch the NAPTR from the stored server address. Since iOS will continue waiting
until told directly to stop (even if there is no result) we must set our own timeout on the
request (set to 5 seconds).
On success, the callback function is called. On timeout, the kSRVLookupComplete notification
is sent.
*/
- (void)attemptNAPTRFetch {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
DNSServiceRef sdRef;
DNSServiceErrorType err;

err = DNSServiceQueryRecord(&sdRef, 0, 0,
[server cStringUsingEncoding:[NSString defaultCStringEncoding]],
kDNSServiceType_NAPTR,
kDNSServiceClass_IN,
callback,
NULL);

// This stuff is necessary so we don't hang forever if there are no results
int dns_sd_fd = DNSServiceRefSockFD(sdRef);
int nfds = dns_sd_fd + 1;
fd_set readfds;
struct timeval tv;
int result;

int stopNow = 0;
int timeOut = 5; // Timeout in seconds

while (!stopNow) {
FD_ZERO(&readfds);
FD_SET(dns_sd_fd, &readfds);
tv.tv_sec = timeOut;
tv.tv_usec = 0;

result = select(nfds, &readfds, (fd_set*)NULL, (fd_set*)NULL, &tv);
if (result > 0) {
if(FD_ISSET(dns_sd_fd, &readfds)) {
err = DNSServiceProcessResult(sdRef);
if (err != kDNSServiceErr_NoError){
NSLog(@"There was an error");
}
stopNow = 1;
}
}
else {
printf("select() returned %d errno %d %s\n", result, errno, strerror(errno));
if (errno != EINTR) {
stopNow = 1;
postNotification(kSRVLookupComplete, nil);
}
}
}

DNSServiceRefDeallocate(sdRef);
});
}

然后,对于回调:

static void callback(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)
{
uint16_t order, pref;
char flag;
NSMutableString *service = [[NSMutableString alloc] init];
NSMutableString *replacement = [[NSMutableString alloc] init];

const char *data = (const char*)rdata;

order = data[1];
pref = data[3];
flag = data[5];
int i = 7;
while (data[i] != 0){
[service appendString:[NSString stringWithFormat:@"%c", data[i]]];
i++;
}
i += 2;
while(data[i] != 0){
if(data[i] >= 32 && data[i] <= 127)
[replacement appendString:[NSString stringWithFormat:@"%c", data[i]]];
else
[replacement appendString:@"."];
i++;
}
NSLog(@"\nOrder: %i\nPreference: %i\nFlag: %c\nService: %@\nReplacement: %@\n", order, pref, flag, service, replacement);
}

这似乎对我有用。您当然会使用回调中的所有已解析数据做任何其他必要的工作,或者将数据存储在某个地方以备后用。

关于ios - 查询 DNS 以在 iOS 中查找 NAPTR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15484813/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com