OStack程序员社区-中国程序员成长平台

标题: ios - 在 iOS 中查询 DNS 以查找 NAPTR [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 01:34
标题: ios - 在 iOS 中查询 DNS 以查找 NAPTR

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

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



Best Answer-推荐答案


我最终使用了 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 - 在 iOS 中查询 DNS 以查找 NAPTR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15484813/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4