我最终通过 Apple 的 Bonjour Dev 邮件列表得到了这个问题的答案。
根据奎因“爱斯基摩人!” ,您可以这样做:
static void DNSCallback(
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
)
{
const uint8_t * cursor;
fprintf(stderr, "DNSCallback\n");
assert(rrtype == kDNSServiceType_PTR);
assert(rrclass == kDNSServiceClass_IN);
cursor = (const uint8_t *) rdata;
while ( *cursor != 0 ) {
fprintf(stderr, "%.*s.", (int) *cursor, cursor + 1);
cursor += *cursor + 1;
}
fprintf(stderr, "\n");
}
static void SocketCallBack(CFSocketRef s, CFSocketCallBackType type, CFDataRef address, const void *data, void *info)
{
DNSServiceErrorType err;
fprintf(stderr, "SocketCallBack\n");
err = DNSServiceProcessResult(((AppDelegate *) info)->sdRef);
fprintf(stderr, "err = %d\n", (int) err);
}
- (IBAction)testAction:(id)sender
{
DNSServiceErrorType err;
int sock;
CFSocketRef cfSock;
fprintf(stderr, "-[AppDelegate testAction:]\n");
assert(self->sdRef == NULL);
err = DNSServiceQueryRecord(
&self->sdRef,
kDNSServiceFlagsForceMulticast,
0,
"9.40.0.10.in-addr.arpa.",
kDNSServiceType_PTR,
kDNSServiceClass_IN,
DNSCallback,
self
);
fprintf(stderr, "err = %d\n", (int) err);
sock = DNSServiceRefSockFD(self->sdRef);
fprintf(stderr, "sock = %d\n", sock);
CFSocketContext context = { 0, self, NULL, NULL, NULL };
cfSock = CFSocketCreateWithNative(NULL, sock, kCFSocketReadCallBack, SocketCallBack, &context);
assert(cfSock != NULL);
CFRunLoopSourceRef rls;
rls = CFSocketCreateRunLoopSource(NULL, cfSock, 0);
assert(rls != NULL);
CFRunLoopAddSource(CFRunLoopGetCurrent(), rls, kCFRunLoopDefaultMode);
}
此示例将查找 IP 地址 10.0.40.9 的链接本地主机名。IP 地址以相反的顺序插入。