︙ | | | ︙ | |
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
|
*
* - Resolve with each search domain
* - Fallback to TCP
*/
@interface OFDNSResolverQuery: OFObject
{
OFString *_host;
of_dns_resource_record_class_t _recordClass;
of_dns_resource_record_type_t _recordType;
OFNumber *_ID;
OFArray OF_GENERIC(OFString *) *_nameServers, *_searchDomains;
size_t _nameServersIndex, _searchDomainsIndex;
size_t _attempt;
id _target;
SEL _selector;
id _context;
OFData *_queryData;
OFTimer *_cancelTimer;
}
@property (readonly, nonatomic) OFString *host;
@property (readonly, nonatomic) of_dns_resource_record_class_t recordClass;
@property (readonly, nonatomic) of_dns_resource_record_type_t recordType;
@property (readonly, nonatomic) OFNumber *ID;
@property (readonly, nonatomic) OFArray OF_GENERIC(OFString *) *nameServers;
@property (readonly, nonatomic) OFArray OF_GENERIC(OFString *) *searchDomains;
@property (nonatomic) size_t nameServersIndex;
@property (nonatomic) size_t searchDomainsIndex;
@property (nonatomic) size_t attempt;
@property (readonly, nonatomic) id target;
@property (readonly, nonatomic) SEL selector;
@property (readonly, nonatomic) id context;
@property (readonly, nonatomic) OFData *queryData;
@property (retain, nonatomic) OFTimer *cancelTimer;
- (instancetype)initWithHost: (OFString *)host
recordClass: (of_dns_resource_record_class_t)recordClass
recordType: (of_dns_resource_record_type_t)recordType
ID: (OFNumber *)ID
nameServers: (OFArray OF_GENERIC(OFString *) *)nameServers
searchDomains: (OFArray OF_GENERIC(OFString *) *)searchDomains
target: (id)target
selector: (SEL)selector
|
|
|
>
|
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
|
*
* - Resolve with each search domain
* - Fallback to TCP
*/
@interface OFDNSResolverQuery: OFObject
{
OFString *_host, *_domainName;
of_dns_resource_record_class_t _recordClass;
of_dns_resource_record_type_t _recordType;
OFNumber *_ID;
OFArray OF_GENERIC(OFString *) *_nameServers, *_searchDomains;
size_t _nameServersIndex, _searchDomainsIndex;
size_t _attempt;
id _target;
SEL _selector;
id _context;
OFData *_queryData;
OFTimer *_cancelTimer;
}
@property (readonly, nonatomic) OFString *host, *domainName;
@property (readonly, nonatomic) of_dns_resource_record_class_t recordClass;
@property (readonly, nonatomic) of_dns_resource_record_type_t recordType;
@property (readonly, nonatomic) OFNumber *ID;
@property (readonly, nonatomic) OFArray OF_GENERIC(OFString *) *nameServers;
@property (readonly, nonatomic) OFArray OF_GENERIC(OFString *) *searchDomains;
@property (nonatomic) size_t nameServersIndex;
@property (nonatomic) size_t searchDomainsIndex;
@property (nonatomic) size_t attempt;
@property (readonly, nonatomic) id target;
@property (readonly, nonatomic) SEL selector;
@property (readonly, nonatomic) id context;
@property (readonly, nonatomic) OFData *queryData;
@property (retain, nonatomic) OFTimer *cancelTimer;
- (instancetype)initWithHost: (OFString *)host
domainName: (OFString *)domainName
recordClass: (of_dns_resource_record_class_t)recordClass
recordType: (of_dns_resource_record_type_t)recordType
ID: (OFNumber *)ID
nameServers: (OFArray OF_GENERIC(OFString *) *)nameServers
searchDomains: (OFArray OF_GENERIC(OFString *) *)searchDomains
target: (id)target
selector: (SEL)selector
|
︙ | | | ︙ | |
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
|
[ret makeImmutable];
return ret;
}
static void callback(id target, SEL selector, OFDNSResolver *resolver,
OFArray *answerRecords, OFArray *authorityRecords,
OFArray *additionalRecords, id context, id exception)
{
void (*method)(id, SEL, OFDNSResolver *, OFArray *, OFArray *,
OFArray *, id, id) = (void (*)(id, SEL, OFDNSResolver *, OFArray *,
OFArray *, OFArray *, id, id))
[target methodForSelector: selector];
method(target, selector, resolver, answerRecords, authorityRecords,
additionalRecords, context, exception);
}
@implementation OFDNSResolverQuery
@synthesize host = _host, recordClass = _recordClass, recordType = _recordType;
@synthesize ID = _ID, nameServers = _nameServers;
@synthesize searchDomains = _searchDomains;
@synthesize nameServersIndex = _nameServersIndex;
@synthesize searchDomainsIndex = _searchDomainsIndex, attempt = _attempt;
@synthesize target = _target, selector = _selector, context = _context;
@synthesize queryData = _queryData, cancelTimer = _cancelTimer;
- (instancetype)initWithHost: (OFString *)host
recordClass: (of_dns_resource_record_class_t)recordClass
recordType: (of_dns_resource_record_type_t)recordType
ID: (OFNumber *)ID
nameServers: (OFArray OF_GENERIC(OFString *) *)nameServers
searchDomains: (OFArray OF_GENERIC(OFString *) *)searchDomains
target: (id)target
selector: (SEL)selector
context: (id)context
{
self = [super init];
@try {
void *pool = objc_autoreleasePoolPush();
OFMutableData *queryData;
uint16_t tmp;
_host = [host copy];
_recordClass = recordClass;
_recordType = recordType;
_ID = [ID retain];
_nameServers = [nameServers copy];
_searchDomains = [searchDomains copy];
_target = [target retain];
_selector = selector;
|
|
|
|
|
|
|
|
|
>
>
|
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
|
[ret makeImmutable];
return ret;
}
static void callback(id target, SEL selector, OFDNSResolver *resolver,
OFString *domainName, OFArray *answerRecords, OFArray *authorityRecords,
OFArray *additionalRecords, id context, id exception)
{
void (*method)(id, SEL, OFDNSResolver *, OFString *, OFArray *,
OFArray *, OFArray *, id, id) = (void (*)(id, SEL, OFDNSResolver *,
OFString *, OFArray *, OFArray *, OFArray *, id, id))
[target methodForSelector: selector];
method(target, selector, resolver, domainName, answerRecords,
authorityRecords, additionalRecords, context, exception);
}
@implementation OFDNSResolverQuery
@synthesize host = _host, domainName = _domainName, recordClass = _recordClass;
@synthesize recordType = _recordType, ID = _ID, nameServers = _nameServers;
@synthesize searchDomains = _searchDomains;
@synthesize nameServersIndex = _nameServersIndex;
@synthesize searchDomainsIndex = _searchDomainsIndex, attempt = _attempt;
@synthesize target = _target, selector = _selector, context = _context;
@synthesize queryData = _queryData, cancelTimer = _cancelTimer;
- (instancetype)initWithHost: (OFString *)host
domainName: (OFString *)domainName
recordClass: (of_dns_resource_record_class_t)recordClass
recordType: (of_dns_resource_record_type_t)recordType
ID: (OFNumber *)ID
nameServers: (OFArray OF_GENERIC(OFString *) *)nameServers
searchDomains: (OFArray OF_GENERIC(OFString *) *)searchDomains
target: (id)target
selector: (SEL)selector
context: (id)context
{
self = [super init];
@try {
void *pool = objc_autoreleasePoolPush();
OFMutableData *queryData;
uint16_t tmp;
_host = [host copy];
_domainName = [domainName copy];
_recordClass = recordClass;
_recordType = recordType;
_ID = [ID retain];
_nameServers = [nameServers copy];
_searchDomains = [searchDomains copy];
_target = [target retain];
_selector = selector;
|
︙ | | | ︙ | |
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
|
/* ANCOUNT, NSCOUNT and ARCOUNT */
[queryData increaseCountBy: 6];
/* Question */
/* QNAME */
for (OFString *component in
[host componentsSeparatedByString: @"."]) {
size_t length = [component UTF8StringLength];
uint8_t length8;
if (length > 63 || [queryData count] + length > 512)
@throw [OFOutOfRangeException exception];
length8 = (uint8_t)length;
|
|
|
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
|
/* ANCOUNT, NSCOUNT and ARCOUNT */
[queryData increaseCountBy: 6];
/* Question */
/* QNAME */
for (OFString *component in
[domainName componentsSeparatedByString: @"."]) {
size_t length = [component UTF8StringLength];
uint8_t length8;
if (length > 63 || [queryData count] + length > 512)
@throw [OFOutOfRangeException exception];
length8 = (uint8_t)length;
|
︙ | | | ︙ | |
611
612
613
614
615
616
617
618
619
620
621
622
623
624
|
return self;
}
- (void)dealloc
{
[_host release];
[_ID release];
[_nameServers release];
[_searchDomains release];
[_target release];
[_context release];
[_queryData release];
[_cancelTimer release];
|
>
|
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
|
return self;
}
- (void)dealloc
{
[_host release];
[_domainName release];
[_ID release];
[_nameServers release];
[_searchDomains release];
[_target release];
[_context release];
[_queryData release];
[_cancelTimer release];
|
︙ | | | ︙ | |
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
|
recordType: (of_dns_resource_record_type_t)recordType
target: (id)target
selector: (SEL)selector
context: (id)context
{
void *pool = objc_autoreleasePoolPush();
OFNumber *ID;
OFDNSResolverQuery *query;
/* TODO: Properly try all search domains */
if (![host hasSuffix: @"."])
host = [host stringByAppendingString: @"."];
if ([host UTF8StringLength] > 253)
@throw [OFOutOfRangeException exception];
/* Random, unused ID */
do {
ID = [OFNumber numberWithUInt16: (uint16_t)of_random()];
} while ([_queries objectForKey: ID] != nil);
query = [[[OFDNSResolverQuery alloc]
initWithHost: host
recordClass: recordClass
recordType: recordType
ID: ID
nameServers: _nameServers
searchDomains: _searchDomains
target: target
selector: selector
|
>
<
<
<
<
<
<
<
>
>
>
>
>
>
>
>
>
>
|
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
|
recordType: (of_dns_resource_record_type_t)recordType
target: (id)target
selector: (SEL)selector
context: (id)context
{
void *pool = objc_autoreleasePoolPush();
OFNumber *ID;
OFString *domainName;
OFDNSResolverQuery *query;
/* Random, unused ID */
do {
ID = [OFNumber numberWithUInt16: (uint16_t)of_random()];
} while ([_queries objectForKey: ID] != nil);
if ([host hasSuffix: @"."])
domainName = host;
else
/* TODO: Properly try all search domains */
domainName = [host stringByAppendingString: @"."];
if ([domainName UTF8StringLength] > 253)
@throw [OFOutOfRangeException exception];
query = [[[OFDNSResolverQuery alloc]
initWithHost: host
domainName: domainName
recordClass: recordClass
recordType: recordType
ID: ID
nameServers: _nameServers
searchDomains: _searchDomains
target: target
selector: selector
|
︙ | | | ︙ | |
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
|
selector: @selector(of_socket:didSendBuffer:bytesSent:
receiver:context:exception:)
context: query];
}
- (void)of_queryWithIDTimedOut: (OFDNSResolverQuery *)query
{
id target, context;
SEL selector;
OFResolveHostFailedException *exception;
if (query == nil)
return;
if ([query nameServersIndex] + 1 < [[query nameServers] count]) {
[query setNameServersIndex: [query nameServersIndex] + 1];
[self of_sendQuery: query];
return;
}
if ([query attempt] < ATTEMPTS) {
[query setAttempt: [query attempt] + 1];
[query setNameServersIndex: 0];
[self of_sendQuery: query];
return;
}
target = [[[query target] retain] autorelease];
selector = [query selector];
context = [[[query context] retain] autorelease];
exception = [OFResolveHostFailedException
exceptionWithHost: [query host]
recordClass: [query recordClass]
recordType: [query recordType]
error: OF_DNS_RESOLVER_ERROR_TIMEOUT];
[_queries removeObjectForKey: [query ID]];
callback(target, selector, self, nil, nil, nil, context, exception);
}
- (size_t)of_socket: (OFUDPSocket *)sock
didSendBuffer: (void **)buffer
bytesSent: (size_t)bytesSent
receiver: (of_socket_address_t *)receiver
context: (OFDNSResolverQuery *)query
exception: (id)exception
{
if (exception != nil) {
id target = [[[query target] retain] autorelease];
SEL selector = [query selector];
id context = [[[query context] retain] autorelease];
[_queries removeObjectForKey: [query ID]];
callback(target, selector, self, nil, nil, nil, context,
exception);
return 0;
}
[sock asyncReceiveIntoBuffer: [self allocMemoryWithSize: 512]
length: 512
|
<
<
|
|
<
<
|
|
|
<
<
<
>
|
|
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
|
selector: @selector(of_socket:didSendBuffer:bytesSent:
receiver:context:exception:)
context: query];
}
- (void)of_queryWithIDTimedOut: (OFDNSResolverQuery *)query
{
OFResolveHostFailedException *exception;
if (query == nil)
return;
if ([query nameServersIndex] + 1 < [[query nameServers] count]) {
[query setNameServersIndex: [query nameServersIndex] + 1];
[self of_sendQuery: query];
return;
}
if ([query attempt] < ATTEMPTS) {
[query setAttempt: [query attempt] + 1];
[query setNameServersIndex: 0];
[self of_sendQuery: query];
return;
}
query = [[query retain] autorelease];
[_queries removeObjectForKey: [query ID]];
exception = [OFResolveHostFailedException
exceptionWithHost: [query host]
recordClass: [query recordClass]
recordType: [query recordType]
error: OF_DNS_RESOLVER_ERROR_TIMEOUT];
callback([query target], [query selector], self, [query domainName],
nil, nil, nil, [query context], exception);
}
- (size_t)of_socket: (OFUDPSocket *)sock
didSendBuffer: (void **)buffer
bytesSent: (size_t)bytesSent
receiver: (of_socket_address_t *)receiver
context: (OFDNSResolverQuery *)query
exception: (id)exception
{
if (exception != nil) {
query = [[query retain] autorelease];
[_queries removeObjectForKey: [query ID]];
callback([query target], [query selector], self,
[query domainName], nil, nil, nil, [query context],
exception);
return 0;
}
[sock asyncReceiveIntoBuffer: [self allocMemoryWithSize: 512]
length: 512
|
︙ | | | ︙ | |
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
|
answerRecords = parseSection(buffer, length, &i, numAnswers);
authorityRecords = parseSection(buffer, length, &i,
numAuthorityRecords);
additionalRecords = parseSection(buffer, length, &i,
numAdditionalRecords);
} @catch (id e) {
callback([query target], [query selector], self, nil, nil, nil,
[query context], e);
return false;
}
callback([query target], [query selector], self, answerRecords,
authorityRecords, additionalRecords, [query context], nil);
return false;
}
- (void)close
{
void *pool = objc_autoreleasePoolPush();
|
|
|
|
|
>
|
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
|
answerRecords = parseSection(buffer, length, &i, numAnswers);
authorityRecords = parseSection(buffer, length, &i,
numAuthorityRecords);
additionalRecords = parseSection(buffer, length, &i,
numAdditionalRecords);
} @catch (id e) {
callback([query target], [query selector], self,
[query domainName], nil, nil, nil, [query context], e);
return false;
}
callback([query target], [query selector], self, [query domainName],
answerRecords, authorityRecords, additionalRecords,
[query context], nil);
return false;
}
- (void)close
{
void *pool = objc_autoreleasePoolPush();
|
︙ | | | ︙ | |
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
|
exception = [OFResolveHostFailedException
exceptionWithHost: [query host]
recordClass: [query recordClass]
recordType: [query recordType]
error: OF_DNS_RESOLVER_ERROR_CANCELED];
callback([query target], [query selector], self, nil, nil, nil,
[query context], exception);
}
[_queries removeAllObjects];
objc_autoreleasePoolPop(pool);
}
@end
|
|
>
|
|
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
|
exception = [OFResolveHostFailedException
exceptionWithHost: [query host]
recordClass: [query recordClass]
recordType: [query recordType]
error: OF_DNS_RESOLVER_ERROR_CANCELED];
callback([query target], [query selector], self,
[query domainName], nil, nil, nil, [query context],
exception);
}
[_queries removeAllObjects];
objc_autoreleasePoolPop(pool);
}
@end
|