Skip to content
This repository
tree: 7d730614bd
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 707 lines (535 sloc) 18.823 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
//
// XMPPSRVResolver.m
//
// Originally created by Eric Chamberlain on 6/15/10.
// Based on SRVResolver by Apple, Inc.
//

#import "XMPPSRVResolver.h"
#import "XMPPLogging.h"

#include <dns_util.h>
#include <stdlib.h>

#if ! __has_feature(objc_arc)
#warning This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC).
#endif

/**
* Does ARC support support GCD objects?
* It does if the minimum deployment target is iOS 6+ or Mac OS X 10.8+
**/
#if TARGET_OS_IPHONE

  // Compiling for iOS

#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 60000 // iOS 6.0 or later
#define NEEDS_DISPATCH_RETAIN_RELEASE 0
#else // iOS 5.X or earlier
#define NEEDS_DISPATCH_RETAIN_RELEASE 1
#endif

#else

  // Compiling for Mac OS X

#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1080 // Mac OS X 10.8 or later
#define NEEDS_DISPATCH_RETAIN_RELEASE 0
#else
#define NEEDS_DISPATCH_RETAIN_RELEASE 1 // Mac OS X 10.7 or earlier
#endif

#endif

NSString *const XMPPSRVResolverErrorDomain = @"XMPPSRVResolverErrorDomain";

// Log levels: off, error, warn, info, verbose
#if DEBUG
  static const int xmppLogLevel = XMPP_LOG_LEVEL_WARN; // | XMPP_LOG_FLAG_TRACE;
#else
  static const int xmppLogLevel = XMPP_LOG_LEVEL_WARN;
#endif

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

@interface XMPPSRVRecord ()

@property(nonatomic, assign) NSUInteger srvResultsIndex;
@property(nonatomic, assign) NSUInteger sum;

- (NSComparisonResult)compareByPriority:(XMPPSRVRecord *)aRecord;

@end

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

@implementation XMPPSRVResolver

- (id)initWithdDelegate:(id)aDelegate delegateQueue:(dispatch_queue_t)dq resolverQueue:(dispatch_queue_t)rq
{
NSParameterAssert(aDelegate != nil);
NSParameterAssert(dq != NULL);

if ((self = [super init]))
{
XMPPLogTrace();

delegate = aDelegate;
delegateQueue = dq;
#if NEEDS_DISPATCH_RETAIN_RELEASE
dispatch_retain(delegateQueue);
#endif

if (rq)
{
resolverQueue = rq;
#if NEEDS_DISPATCH_RETAIN_RELEASE
dispatch_retain(resolverQueue);
#endif
}
else
{
resolverQueue = dispatch_queue_create("XMPPSRVResolver", NULL);
}

results = [[NSMutableArray alloc] initWithCapacity:2];
}
return self;
}

- (void)dealloc
{
XMPPLogTrace();

    [self stop];
#if NEEDS_DISPATCH_RETAIN_RELEASE
if (resolverQueue)
dispatch_release(resolverQueue);
#endif
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark Properties
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

@dynamic srvName;
@dynamic timeout;

- (NSString *)srvName
{
__block NSString *result = nil;

dispatch_block_t block = ^{
result = [srvName copy];
};

if (dispatch_get_current_queue() == resolverQueue)
block();
else
dispatch_sync(resolverQueue, block);

return result;
}

- (NSTimeInterval)timeout
{
__block NSTimeInterval result = 0.0;

dispatch_block_t block = ^{
result = timeout;
};

if (dispatch_get_current_queue() == resolverQueue)
block();
else
dispatch_sync(resolverQueue, block);

return result;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark Private Methods
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

- (void)sortResults
{
NSAssert(dispatch_get_current_queue() == resolverQueue, @"Invoked on incorrect queue");

XMPPLogTrace();

// Sort results
NSMutableArray *sortedResults = [NSMutableArray arrayWithCapacity:[results count]];

// Sort the list by priority (lowest number first)
[results sortUsingSelector:@selector(compareByPriority:)];

/* From RFC 2782
*
* For each distinct priority level
* While there are still elements left at this priority level
*
* Select an element as specified above, in the
* description of Weight in "The format of the SRV
* RR" Section, and move it to the tail of the new
* list.
*
* The following algorithm SHOULD be used to order
* the SRV RRs of the same priority:
*/

NSUInteger srvResultsCount;

while ([results count] > 0)
{
srvResultsCount = [results count];

if (srvResultsCount == 1)
{
XMPPSRVRecord *srvRecord = [results objectAtIndex:0];

[sortedResults addObject:srvRecord];
[results removeObjectAtIndex:0];
}
else // (srvResultsCount > 1)
{
// more than two records so we need to sort

/* To select a target to be contacted next, arrange all SRV RRs
* (that have not been ordered yet) in any order, except that all
* those with weight 0 are placed at the beginning of the list.
*
* Compute the sum of the weights of those RRs, and with each RR
* associate the running sum in the selected order.
*/

NSUInteger runningSum = 0;
NSMutableArray *samePriorityRecords = [NSMutableArray arrayWithCapacity:srvResultsCount];

XMPPSRVRecord *srvRecord = [results objectAtIndex:0];

NSUInteger initialPriority = srvRecord.priority;
NSUInteger index = 0;

do
{
if (srvRecord.weight == 0)
{
// add to front of array
[samePriorityRecords insertObject:srvRecord atIndex:0];

srvRecord.srvResultsIndex = index;
srvRecord.sum = 0;
}
else
{
// add to end of array and update the running sum
[samePriorityRecords addObject:srvRecord];

runningSum += srvRecord.weight;

srvRecord.srvResultsIndex = index;
srvRecord.sum = runningSum;
}

if (++index < srvResultsCount)
{
srvRecord = [results objectAtIndex:index];
}
else
{
srvRecord = nil;
}

} while(srvRecord && (srvRecord.priority == initialPriority));

/* Then choose a uniform random number between 0 and the sum computed
* (inclusive), and select the RR whose running sum value is the
* first in the selected order which is greater than or equal to
* the random number selected.
*/

NSUInteger randomIndex = arc4random() % (runningSum + 1);

for (srvRecord in samePriorityRecords)
{
if (srvRecord.sum >= randomIndex)
{
/* The target host specified in the
* selected SRV RR is the next one to be contacted by the client.
* Remove this SRV RR from the set of the unordered SRV RRs and
* apply the described algorithm to the unordered SRV RRs to select
* the next target host. Continue the ordering process until there
* are no unordered SRV RRs. This process is repeated for each
* Priority.
*/

[sortedResults addObject:srvRecord];
[results removeObjectAtIndex:srvRecord.srvResultsIndex];

break;
}
}
}
}

results = sortedResults;

XMPPLogVerbose(@"%@: Sorted results:\n%@", THIS_FILE, results);
}

- (void)succeed
{
NSAssert(dispatch_get_current_queue() == resolverQueue, @"Invoked on incorrect queue");

XMPPLogTrace();

[self sortResults];

id theDelegate = delegate;
NSArray *records = [results copy];

dispatch_async(delegateQueue, ^{ @autoreleasepool {

SEL selector = @selector(srvResolver:didResolveRecords:);

if ([theDelegate respondsToSelector:selector])
{
[theDelegate srvResolver:self didResolveRecords:records];
}
else
{
XMPPLogWarn(@"%@: delegate doesn't implement %@", THIS_FILE, NSStringFromSelector(selector));
}

}});

[self stop];
}

- (void)failWithError:(NSError *)error
{
NSAssert(dispatch_get_current_queue() == resolverQueue, @"Invoked on incorrect queue");

XMPPLogTrace2(@"%@: %@ %@", THIS_FILE, THIS_METHOD, error);

id theDelegate = delegate;

    if (delegateQueue != NULL)
{
dispatch_async(delegateQueue, ^{ @autoreleasepool {

SEL selector = @selector(srvResolver:didNotResolveDueToError:);

if ([theDelegate respondsToSelector:selector])
{
[theDelegate srvResolver:self didNotResolveDueToError:error];
}
else
{
XMPPLogWarn(@"%@: delegate doesn't implement %@", THIS_FILE, NSStringFromSelector(selector));
}

}});
}

[self stop];
}

- (void)failWithDNSError:(DNSServiceErrorType)sdErr
{
XMPPLogTrace2(@"%@: %@ %i", THIS_FILE, THIS_METHOD, (int)sdErr);

[self failWithError:[NSError errorWithDomain:XMPPSRVResolverErrorDomain code:sdErr userInfo:nil]];
}

- (XMPPSRVRecord *)processRecord:(const void *)rdata length:(uint16_t)rdlen
{
XMPPLogTrace();

// Note: This method is almost entirely from Apple's sample code.
//
// Otherwise there would be a lot more comments and explanation...

if (rdata == NULL)
{
XMPPLogWarn(@"%@: %@ - rdata == NULL", THIS_FILE, THIS_METHOD);
return nil;
}

// Rather than write a whole bunch of icky parsing code, I just synthesise
// a resource record and use <dns_util.h>.

XMPPSRVRecord *result = nil;

NSMutableData * rrData;
dns_resource_record_t * rr;
uint8_t u8; // 1 byte
uint16_t u16; // 2 bytes
uint32_t u32; // 4 bytes

rrData = [NSMutableData dataWithCapacity:(1 + 2 + 2 + 4 + 2 + rdlen)];

u8 = 0;
[rrData appendBytes:&u8 length:sizeof(u8)];
u16 = htons(kDNSServiceType_SRV);
[rrData appendBytes:&u16 length:sizeof(u16)];
u16 = htons(kDNSServiceClass_IN);
[rrData appendBytes:&u16 length:sizeof(u16)];
u32 = htonl(666);
[rrData appendBytes:&u32 length:sizeof(u32)];
u16 = htons(rdlen);
[rrData appendBytes:&u16 length:sizeof(u16)];
[rrData appendBytes:rdata length:rdlen];

// Parse the record.

rr = dns_parse_resource_record([rrData bytes], (uint32_t) [rrData length]);
    if (rr != NULL)
{
        NSString *target;
        
        target = [NSString stringWithCString:rr->data.SRV->target encoding:NSASCIIStringEncoding];
        if (target != nil)
{
UInt16 priority = rr->data.SRV->priority;
UInt16 weight = rr->data.SRV->weight;
UInt16 port = rr->data.SRV->port;

result = [XMPPSRVRecord recordWithPriority:priority weight:weight port:port target:target];
        }

        dns_free_resource_record(rr);
    }

return result;
}

static void QueryRecordCallback(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)
{
// Called when we get a response to our query.
// It does some preliminary work, but the bulk of the interesting stuff
// is done in the processRecord:length: method.

    XMPPSRVResolver *resolver = (__bridge XMPPSRVResolver *)context;

NSCAssert(dispatch_get_current_queue() == resolver->resolverQueue, @"Invoked on incorrect queue");
    
XMPPLogCTrace();

if (!(flags & kDNSServiceFlagsAdd))
{
// If the kDNSServiceFlagsAdd flag is not set, the domain information is not valid.
return;
    }

    if (errorCode == kDNSServiceErr_NoError &&
        rrtype == kDNSServiceType_SRV)
    {
        XMPPSRVRecord *record = [resolver processRecord:rdata length:rdlen];
        if (record)
        {
            [resolver->results addObject:record];
        }

        if ( ! (flags & kDNSServiceFlagsMoreComing) )
        {
            [resolver succeed];
        }
    }
    else
    {
        [resolver failWithDNSError:errorCode];
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark Public Methods
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

- (void)startWithSRVName:(NSString *)aSRVName timeout:(NSTimeInterval)aTimeout
{
dispatch_block_t block = ^{ @autoreleasepool {

if (resolveInProgress)
{
return;
}

XMPPLogTrace2(@"%@: startWithSRVName:%@ timeout:%f", THIS_FILE, aSRVName, aTimeout);

// Save parameters

srvName = [aSRVName copy];

timeout = aTimeout;

// Check parameters

const char *srvNameCStr = [srvName cStringUsingEncoding:NSASCIIStringEncoding];
if (srvNameCStr == NULL)
{
[self failWithDNSError:kDNSServiceErr_BadParam];
return;

}

// Create DNS Service

DNSServiceErrorType sdErr;
sdErr = DNSServiceQueryRecord(&sdRef, // Pointer to unitialized DNSServiceRef
kDNSServiceFlagsReturnIntermediates, // Flags
kDNSServiceInterfaceIndexAny, // Interface index
srvNameCStr, // Full domain name
kDNSServiceType_SRV, // rrtype
kDNSServiceClass_IN, // rrclass
QueryRecordCallback, // Callback method
(__bridge void *)self); // Context pointer

if (sdErr != kDNSServiceErr_NoError)
{
[self failWithDNSError:sdErr];
return;
}

// Extract unix socket (so we can poll for events)

sdFd = DNSServiceRefSockFD(sdRef);
if (sdFd < 0)
{
// Todo...
}

// Create GCD read source for sd file descriptor

sdReadSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, sdFd, 0, resolverQueue);

dispatch_source_set_event_handler(sdReadSource, ^{ @autoreleasepool {

XMPPLogVerbose(@"%@: sdReadSource_eventHandler", THIS_FILE);

// There is data to be read on the socket (or an error occurred).
//
// Invoking DNSServiceProcessResult will invoke our QueryRecordCallback,
// the callback we set when we created the sdRef.

DNSServiceErrorType dnsErr = DNSServiceProcessResult(sdRef);
if (dnsErr != kDNSServiceErr_NoError)
{
[self failWithDNSError:dnsErr];
}

}});
#if NEEDS_DISPATCH_RETAIN_RELEASE
dispatch_source_t theSdReadSource = sdReadSource;
#endif
DNSServiceRef theSdRef = sdRef;

dispatch_source_set_cancel_handler(sdReadSource, ^{ @autoreleasepool {

XMPPLogVerbose(@"%@: sdReadSource_cancelHandler", THIS_FILE);
#if NEEDS_DISPATCH_RETAIN_RELEASE
dispatch_release(theSdReadSource);
#endif
DNSServiceRefDeallocate(theSdRef);

}});

dispatch_resume(sdReadSource);

// Create timer (if requested timeout > 0)

if (timeout > 0.0)
{
timeoutTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, resolverQueue);

dispatch_source_set_event_handler(timeoutTimer, ^{ @autoreleasepool {

NSString *errMsg = @"Operation timed out";
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:errMsg forKey:NSLocalizedDescriptionKey];

NSError *err = [NSError errorWithDomain:XMPPSRVResolverErrorDomain code:0 userInfo:userInfo];

[self failWithError:err];

}});

dispatch_time_t tt = dispatch_time(DISPATCH_TIME_NOW, (timeout * NSEC_PER_SEC));

dispatch_source_set_timer(timeoutTimer, tt, DISPATCH_TIME_FOREVER, 0);
dispatch_resume(timeoutTimer);
}

resolveInProgress = YES;
}};

if (dispatch_get_current_queue() == resolverQueue)
block();
else
dispatch_async(resolverQueue, block);
}

- (void)stop
{
dispatch_block_t block = ^{ @autoreleasepool {

XMPPLogTrace();

delegate = nil;
if (delegateQueue)
{
#if NEEDS_DISPATCH_RETAIN_RELEASE
dispatch_release(delegateQueue);
#endif
delegateQueue = NULL;
}

[results removeAllObjects];

if (sdReadSource)
{
// Cancel the readSource.
// It will be released from within the cancel handler.
dispatch_source_cancel(sdReadSource);
sdReadSource = NULL;
sdFd = -1;

// The sdRef will be deallocated from within the cancel handler too.
sdRef = NULL;
}

if (timeoutTimer)
{
dispatch_source_cancel(timeoutTimer);
#if NEEDS_DISPATCH_RETAIN_RELEASE
dispatch_release(timeoutTimer);
#endif
timeoutTimer = NULL;
}

resolveInProgress = NO;
}};

if (dispatch_get_current_queue() == resolverQueue)
block();
else
dispatch_sync(resolverQueue, block);
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark Utility Methods
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

+ (NSString *)srvNameFromXMPPDomain:(NSString *)xmppDomain
{
if (xmppDomain == nil)
return nil;
else
return [NSString stringWithFormat:@"_xmpp-client._tcp.%@", xmppDomain];
}

@end

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

@implementation XMPPSRVRecord

@synthesize priority;
@synthesize weight;
@synthesize port;
@synthesize target;

@synthesize sum;
@synthesize srvResultsIndex;


+ (XMPPSRVRecord *)recordWithPriority:(UInt16)p1 weight:(UInt16)w port:(UInt16)p2 target:(NSString *)t
{
return [[XMPPSRVRecord alloc] initWithPriority:p1 weight:w port:p2 target:t];
}

- (id)initWithPriority:(UInt16)p1 weight:(UInt16)w port:(UInt16)p2 target:(NSString *)t
{
if ((self = [super init]))
{
priority = p1;
weight = w;
port = p2;
target = [t copy];

sum = 0;
srvResultsIndex = 0;
}
return self;
}


- (NSString *)description
{
return [NSString stringWithFormat:@"<%@:%p target(%@) port(%hu) priority(%hu) weight(%hu)>",
NSStringFromClass([self class]), self, target, port, priority, weight];
}

- (NSComparisonResult)compareByPriority:(XMPPSRVRecord *)aRecord
{
UInt16 mPriority = self.priority;
UInt16 aPriority = aRecord.priority;

if (mPriority < aPriority)
return NSOrderedAscending;

if (mPriority > aPriority)
return NSOrderedDescending;

return NSOrderedSame;
}

@end
Something went wrong with that request. Please try again.