3 * Copyright (c) 2016 Apple Inc. All rights reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #import "_CNDomainBrowser.h"
19 #import "CNDomainBrowserPathUtils.h"
22 const NSString * _CNSubDomainKey_defaultFlag = @"defaultFlag";
23 const NSString * _CNSubDomainKey_subPath = @"subPath";
24 const NSString * _CNSubDomainKey_reverseDomainPath = @"reverseDomainPath";
26 @interface _CNDomainBrowser ()
28 @property (assign) DNSServiceRef browseDomainR;
29 @property (strong) NSMutableDictionary * browseDomainD;
31 @property (weak) id<_CNDomainBrowserDelegate> delegate;
35 @implementation _CNDomainBrowser
37 static void enumReply(DNSServiceRef sdRef, DNSServiceFlags flags, uint32_t interfaceIndex, DNSServiceErrorType errorCode, const char *replyDomain, void *context);
39 - (instancetype)initWithDelegate:(id<_CNDomainBrowserDelegate>)delegate
41 if (self = [super init])
51 self.browseDomainD = [NSMutableDictionary dictionary];
52 self.callbackQueue = dispatch_get_main_queue();
64 dispatch_async(dispatch_get_main_queue(), ^{
65 dispatch_queue_t queue = dispatch_queue_create("DNSServiceEnumerateDomains", DISPATCH_QUEUE_PRIORITY_DEFAULT);
66 dispatch_set_context(queue, (void *)CFBridgingRetain(self));
67 dispatch_set_finalizer_f(queue, finalizer);
70 DNSServiceErrorType error;
71 if ((error = DNSServiceEnumerateDomains(&ref, self->_browseRegistration ? kDNSServiceFlagsRegistrationDomains : kDNSServiceFlagsBrowseDomains, 0, enumReply, (__bridge void *)self)) != 0)
72 NSLog(@"DNSServiceEnumerateDomains failed err: %ld", error);
75 self->_browseDomainR = ref;
76 (void)DNSServiceSetDispatchQueue(self->_browseDomainR, queue);
86 DNSServiceRefDeallocate(_browseDomainR);
91 - (BOOL)foundInstanceInMoreThanLocalDomain
95 if( self.browseDomainD.count )
97 for( NSDictionary *next in [self.browseDomainD allValues] )
99 if( [next[_CNSubDomainKey_reverseDomainPath][0] isEqual: @"local"] ) continue;
111 - (NSArray *)defaultDomainPath
113 NSArray * revDomainArray = nil;
115 NSArray *defaults = [[self.browseDomainD allValues] filteredArrayUsingPredicate: [NSPredicate predicateWithFormat: @"(%K == %@)", _CNSubDomainKey_defaultFlag, @YES]];
116 if (defaults.count) revDomainArray = defaults[0][_CNSubDomainKey_reverseDomainPath];
117 if (!revDomainArray) revDomainArray = [NSArray arrayWithObject: @"local"]; // If no defaults found
119 return(revDomainArray);
122 - (NSArray *)flattenedDNSDomains
124 return([self.browseDomainD allKeys]);
127 - (NSArray *)subDomainsAtDomainPath:(NSArray *)domainPath
129 NSMutableDictionary * subs = [NSMutableDictionary dictionary];
130 for (NSDictionary * next in [self.browseDomainD allValues])
132 NSArray * bdomain = next[_CNSubDomainKey_reverseDomainPath];
133 if (bdomain.count > domainPath.count)
136 for (NSUInteger i = 0 ; i < domainPath.count ; i++)
138 if (![bdomain[i] isEqualToString: domainPath[i]]) { match = NO; break; }
142 NSString * key = bdomain[domainPath.count];
143 [subs setObject: @{ _CNSubDomainKey_subPath: key, _CNSubDomainKey_defaultFlag: next[_CNSubDomainKey_defaultFlag] } forKey: key];
147 return([subs allValues]);
150 - (void) reloadBrowser
152 if ([_delegate respondsToSelector: @selector(bonjourBrowserDomainUpdate:)])
154 dispatch_async(self.callbackQueue, ^{
155 [self->_delegate bonjourBrowserDomainUpdate: [self defaultDomainPath]];
162 return(_browseDomainR != nil);
165 #pragma mark - Dispatch
167 static void finalizer(void * context)
169 _CNDomainBrowser *self = (__bridge _CNDomainBrowser *)context;
170 // NSLog(@"finalizer: %@", self);
171 (void)CFBridgingRelease((__bridge void *)self);
174 #pragma mark - Commands
176 #pragma mark - Static Callbacks
178 static void enumReply(DNSServiceRef sdRef, DNSServiceFlags flags, uint32_t interfaceIndex, DNSServiceErrorType errorCode,
179 const char *replyDomain, void *context)
182 (void)interfaceIndex;
185 if (!*replyDomain) return;
187 _CNDomainBrowser *self = (__bridge _CNDomainBrowser *)context;
188 NSString *key = [NSString stringWithUTF8String: replyDomain];
190 if (self.ignoreLocal && [key isEqualToString: @"local."]) goto exit;
191 if (self.ignoreBTMM && [key hasSuffix: @".members.btmm.icloud.com."]) goto exit;
193 if (!(flags & kDNSServiceFlagsAdd))
195 [self.browseDomainD removeObjectForKey:key];
199 NSArray * pathArray = DNSDomainToDomainPath(key);
200 [self.browseDomainD setObject: @{ _CNSubDomainKey_reverseDomainPath: pathArray,
201 _CNSubDomainKey_defaultFlag: (flags & kDNSServiceFlagsDefault) ? @YES : @NO }
206 if (!(flags & kDNSServiceFlagsMoreComing))
208 [self reloadBrowser];