Rewrote the Bonjour classes, using the low-level <dns_sd.h> API. They're now subclasses of MYDNSService.
5 // Created by Jens Alfke on 4/24/09.
6 // Copyright 2009 Jens Alfke. All rights reserved.
9 #import "MYAddressLookup.h"
11 #import "ExceptionUtils.h"
17 @implementation MYAddressLookup
19 - (id) initWithHostname: (NSString*)hostname
27 _hostname = [hostname copy];
28 _addresses = [[NSMutableArray alloc] init];
41 - (NSString*) description
43 return $sprintf(@"%@[%@]", self.class,_hostname);
47 @synthesize port=_port, interfaceIndex=_interfaceIndex, addresses=_addresses;
50 - (NSTimeInterval) timeToLive {
51 return MAX(0.0, _expires - CFAbsoluteTimeGetCurrent());
55 - (void) priv_resolvedAddress: (const struct sockaddr*)sockaddr
57 flags: (DNSServiceFlags)flags
59 HostAddress *address = [[HostAddress alloc] initWithHostname: _hostname
63 if (flags & kDNSServiceFlagsAdd)
64 [_addresses addObject: address];
66 [_addresses removeObject: address];
70 _expires = CFAbsoluteTimeGetCurrent() + ttl;
72 if (!(flags & kDNSServiceFlagsMoreComing))
73 LogTo(DNS,@"Got addresses of %@: %@ [TTL = %u]", self, _addresses, ttl);
77 static void lookupCallback(DNSServiceRef sdRef,
78 DNSServiceFlags flags,
79 uint32_t interfaceIndex,
80 DNSServiceErrorType errorCode,
82 const struct sockaddr *address,
86 MYAddressLookup *lookup = context;
88 //LogTo(Bonjour, @"lookupCallback for %s (err=%i)", hostname,errorCode);
90 [lookup setError: errorCode];
92 [lookup priv_resolvedAddress: address ttl: ttl flags: flags];
93 }catchAndReport(@"MYDNSLookup query callback");
97 - (DNSServiceRef) createServiceRef {
98 [_addresses removeAllObjects];
99 DNSServiceRef serviceRef = NULL;
100 self.error = DNSServiceGetAddrInfo(&serviceRef, 0,
102 _hostname.UTF8String,
103 &lookupCallback, self);
112 TestCase(MYDNSLookup) {
113 EnableLogTo(Bonjour,YES);
114 EnableLogTo(DNS,YES);
115 [NSRunLoop currentRunLoop]; // create runloop
117 MYAddressLookup *lookup = [[MYAddressLookup alloc] initWithHostname: @"www.apple.com" port: 80];
120 [[NSRunLoop currentRunLoop] runUntilDate: [NSDate dateWithTimeIntervalSinceNow: 10]];