1.1 --- a/Bonjour/MYAddressLookup.m Sun May 10 19:05:52 2009 -0700
1.2 +++ b/Bonjour/MYAddressLookup.m Fri Jul 03 17:50:28 2009 -0700
1.3 @@ -7,6 +7,7 @@
1.4 //
1.5
1.6 #import "MYAddressLookup.h"
1.7 +#import "MYBonjourService.h"
1.8 #import "IPAddress.h"
1.9 #import "ExceptionUtils.h"
1.10 #import "Test.h"
1.11 @@ -14,6 +15,11 @@
1.12 #import <dns_sd.h>
1.13
1.14
1.15 +@interface MYAddressLookup ()
1.16 +@property (copy) NSString *hostname;
1.17 +@end
1.18 +
1.19 +
1.20 @implementation MYAddressLookup
1.21
1.22 - (id) initWithHostname: (NSString*)hostname
1.23 @@ -30,10 +36,21 @@
1.24 return self;
1.25 }
1.26
1.27 +
1.28 +- (id) _initWithBonjourService: (MYBonjourService*)service {
1.29 + self = [super init];
1.30 + if (self) {
1.31 + _service = [service retain];
1.32 + _addresses = [[NSMutableSet alloc] init];
1.33 + }
1.34 + return self;
1.35 +}
1.36 +
1.37 - (void) dealloc
1.38 {
1.39 [_hostname release];
1.40 [_addresses release];
1.41 + [_service release];
1.42 [super dealloc];
1.43 }
1.44
1.45 @@ -44,7 +61,7 @@
1.46 }
1.47
1.48
1.49 -@synthesize port=_port, interfaceIndex=_interfaceIndex, addresses=_addresses;
1.50 +@synthesize hostname=_hostname, port=_port, interfaceIndex=_interfaceIndex, addresses=_addresses;
1.51
1.52
1.53 - (NSTimeInterval) timeToLive {
1.54 @@ -52,6 +69,38 @@
1.55 }
1.56
1.57
1.58 +- (BOOL) start {
1.59 + if (_hostname) {
1.60 + return [super start];
1.61 + } else {
1.62 + // Service doesn't know its hostname yet; wait till it does:
1.63 + LogTo(DNS,@"MYAddressLookup requesting hostname of %@ ...", _service);
1.64 + Assert(_service);
1.65 + return [_service start];
1.66 + }
1.67 +}
1.68 +
1.69 +
1.70 +// Called by my _service's gotResponse method:
1.71 +- (void) _serviceGotResponse {
1.72 + Assert(_service);
1.73 + DNSServiceErrorType err = _service.error;
1.74 + if (err) {
1.75 + [self cancel];
1.76 + self.error = err;
1.77 + } else {
1.78 + NSString *hostname = _service.hostname;
1.79 + UInt16 port = _service.port;
1.80 + if (port!=_port || !$equal(hostname,_hostname)) {
1.81 + self.hostname = hostname;
1.82 + self.port = port;
1.83 + if (_hostname)
1.84 + [self start];
1.85 + }
1.86 + }
1.87 +}
1.88 +
1.89 +
1.90 - (void) priv_resolvedAddress: (const struct sockaddr*)sockaddr
1.91 ttl: (uint32_t)ttl
1.92 flags: (DNSServiceFlags)flags
1.93 @@ -85,7 +134,7 @@
1.94 {
1.95 MYAddressLookup *lookup = context;
1.96 @try{
1.97 - //LogTo(Bonjour, @"lookupCallback for %s (err=%i)", hostname,errorCode);
1.98 + LogTo(DNS, @"lookupCallback for %s (err=%i)", hostname,errorCode);
1.99 if (errorCode)
1.100 [lookup setError: errorCode];
1.101 else
1.102 @@ -96,10 +145,12 @@
1.103
1.104
1.105 - (DNSServiceErrorType) createServiceRef: (DNSServiceRef*)sdRefPtr {
1.106 + Assert(_hostname);
1.107 kvSetSet(self, @"addresses", _addresses, nil);
1.108 return DNSServiceGetAddrInfo(sdRefPtr,
1.109 kDNSServiceFlagsShareConnection,
1.110 - _interfaceIndex, 0,
1.111 + _interfaceIndex,
1.112 + kDNSServiceProtocol_IPv4,
1.113 _hostname.UTF8String,
1.114 &lookupCallback, self);
1.115 }