Tweaked release to be immediate instead of on autorelease pool.
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 LogTo(DNS,@"%@ got %@ [TTL = %u]", self, address, ttl);
65 [_addresses addObject: address];
67 LogTo(DNS,@"%@ lost %@ [TTL = %u]", self, address, ttl);
68 [_addresses removeObject: address];
73 _expires = CFAbsoluteTimeGetCurrent() + 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");
94 [lookup gotResponse: errorCode];
98 - (DNSServiceErrorType) createServiceRef: (DNSServiceRef*)sdRefPtr {
99 [_addresses removeAllObjects];
100 return DNSServiceGetAddrInfo(sdRefPtr,
101 kDNSServiceFlagsShareConnection,
103 _hostname.UTF8String,
104 &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]];
126 Copyright (c) 2009, Jens Alfke <jens@mooseyard.com>. All rights reserved.
128 Redistribution and use in source and binary forms, with or without modification, are permitted
129 provided that the following conditions are met:
131 * Redistributions of source code must retain the above copyright notice, this list of conditions
132 and the following disclaimer.
133 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
134 and the following disclaimer in the documentation and/or other materials provided with the
137 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
138 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
139 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
140 BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
141 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
142 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
143 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
144 THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.