它不使用NSHost
,但使用 Bonjour-API,它似乎可以按您的意愿工作:
#import <Cocoa/Cocoa.h>
#import <dns_sd.h>
#import <resolv.h>
static void callback(DNSServiceRef serviceRef, 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) {
char result[1024] = {0};
dn_expand(rdata, rdata + rdlen, rdata, result, 1024);
NSLog(@"Found: %s", result);
}
int main(int argc, char const *argv[]) {
DNSServiceRef reverseLookupService = NULL;
DNSServiceErrorType error = kDNSServiceErr_NoError;
error = DNSServiceQueryRecord(&reverseLookupService, kDNSServiceFlagsForceMulticast,
kDNSServiceInterfaceIndexAny, "5.1.168.192.in-addr.arpa.",
kDNSServiceType_PTR, kDNSServiceClass_IN,
callback, NULL);
if (error != kDNSServiceErr_NoError) {
NSLog(@"Error: %d", error);
exit(1);
}
error = DNSServiceProcessResult(reverseLookupService);
DNSServiceRefDeallocate(reverseLookupService);
return 0;
}
重要的部分是使用DNSServiceQueryRecord
with kDNSServiceFlagsForceMulticast
。有关此功能的更多信息,请查看https://developer.apple.com/library/mac/#documentation/Networking/Reference/DNSServiceDiscovery_CRef/dns_sd_h/index.html#//apple_ref/c/func/DNSServiceQueryRecord。
您必须自己将 IP 地址转换为 in-addr.arpa.-format,但这并不难(八位位组在结尾处以“in-addr.arpa.”向后。IPv6 可能类似,但我没有测试过)。
它导入resolv.h
(并且您需要将其链接到 libresolv),但仅适用于dn_expand
. 传递给回调的数据被压缩,并dn_expand
创建人类可读的表示。