36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
@public
bool _done;
OFData *_addresses;
id _exception;
}
@end
static const of_run_loop_mode_t resolveRunLoopMode =
@"of_host_address_resolver_resolve_mode";
static bool
isFQDN(OFString *host, unsigned int minNumberOfDotsInAbsoluteName)
{
const char *UTF8String;
size_t length;
unsigned int dots;
|
|
|
|
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
@public
bool _done;
OFData *_addresses;
id _exception;
}
@end
static const OFRunLoopMode resolveRunLoopMode =
@"OFHostAddressResolverResolveRunLoopMode";
static bool
isFQDN(OFString *host, unsigned int minNumberOfDotsInAbsoluteName)
{
const char *UTF8String;
size_t length;
unsigned int dots;
|
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
|
dots++;
return (dots >= minNumberOfDotsInAbsoluteName);
}
static bool
addressForRecord(OF_KINDOF(OFDNSResourceRecord *) record,
const of_socket_address_t **address,
of_socket_address_family_t addressFamily)
{
switch ([record recordType]) {
#ifdef OF_HAVE_IPV6
case OF_DNS_RECORD_TYPE_AAAA:
if (addressFamily != OF_SOCKET_ADDRESS_FAMILY_IPV6 &&
addressFamily != OF_SOCKET_ADDRESS_FAMILY_ANY)
return false;
break;
#endif
case OF_DNS_RECORD_TYPE_A:
if (addressFamily != OF_SOCKET_ADDRESS_FAMILY_IPV4 &&
addressFamily != OF_SOCKET_ADDRESS_FAMILY_ANY)
return false;
break;
default:
return false;
}
*address = [record address];
return true;
}
static void
callDelegateInMode(of_run_loop_mode_t runLoopMode,
id <OFDNSResolverHostDelegate> delegate, OFDNSResolver *resolver,
OFString *host, OFData *addresses, id exception)
{
SEL selector = @selector(resolver:didResolveHost:addresses:exception:);
if ([delegate respondsToSelector: selector]) {
OFTimer *timer = [OFTimer
|
|
<
|
|
|
|
|
|
|
|
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
|
dots++;
return (dots >= minNumberOfDotsInAbsoluteName);
}
static bool
addressForRecord(OF_KINDOF(OFDNSResourceRecord *) record,
const OFSocketAddress **address, OFSocketAddressFamily addressFamily)
{
switch ([record recordType]) {
#ifdef OF_HAVE_IPV6
case OFDNSRecordTypeAAAA:
if (addressFamily != OFSocketAddressFamilyIPv6 &&
addressFamily != OFSocketAddressFamilyAny)
return false;
break;
#endif
case OFDNSRecordTypeA:
if (addressFamily != OFSocketAddressFamilyIPv4 &&
addressFamily != OFSocketAddressFamilyAny)
return false;
break;
default:
return false;
}
*address = [record address];
return true;
}
static void
callDelegateInMode(OFRunLoopMode runLoopMode,
id <OFDNSResolverHostDelegate> delegate, OFDNSResolver *resolver,
OFString *host, OFData *addresses, id exception)
{
SEL selector = @selector(resolver:didResolveHost:addresses:exception:);
if ([delegate respondsToSelector: selector]) {
OFTimer *timer = [OFTimer
|
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
[[OFRunLoop currentRunLoop] addTimer: timer
forMode: runLoopMode];
}
}
@implementation OFHostAddressResolver: OFObject
- (instancetype)initWithHost: (OFString *)host
addressFamily: (of_socket_address_family_t)addressFamily
resolver: (OFDNSResolver *)resolver
settings: (OFDNSResolverSettings *)settings
runLoopMode: (of_run_loop_mode_t)runLoopMode
delegate: (id <OFDNSResolverHostDelegate>)delegate
{
self = [super init];
@try {
_host = [host copy];
_addressFamily = addressFamily;
|
|
|
|
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
[[OFRunLoop currentRunLoop] addTimer: timer
forMode: runLoopMode];
}
}
@implementation OFHostAddressResolver: OFObject
- (instancetype)initWithHost: (OFString *)host
addressFamily: (OFSocketAddressFamily)addressFamily
resolver: (OFDNSResolver *)resolver
settings: (OFDNSResolverSettings *)settings
runLoopMode: (OFRunLoopMode)runLoopMode
delegate: (id <OFDNSResolverHostDelegate>)delegate
{
self = [super init];
@try {
_host = [host copy];
_addressFamily = addressFamily;
|
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
|
domainName = [OFString stringWithFormat: @"%@.%@",
_host, searchDomain];
} else
domainName = _host;
#ifdef OF_HAVE_IPV6
if (_addressFamily == OF_SOCKET_ADDRESS_FAMILY_IPV6 ||
_addressFamily == OF_SOCKET_ADDRESS_FAMILY_ANY) {
OFDNSQuery *query = [OFDNSQuery
queryWithDomainName: domainName
DNSClass: OF_DNS_CLASS_IN
recordType: OF_DNS_RECORD_TYPE_AAAA];
_numExpectedResponses++;
[_resolver asyncPerformQuery: query
runLoopMode: _runLoopMode
delegate: self];
}
#endif
if (_addressFamily == OF_SOCKET_ADDRESS_FAMILY_IPV4 ||
_addressFamily == OF_SOCKET_ADDRESS_FAMILY_ANY) {
OFDNSQuery *query = [OFDNSQuery
queryWithDomainName: domainName
DNSClass: OF_DNS_CLASS_IN
recordType: OF_DNS_RECORD_TYPE_A];
_numExpectedResponses++;
[_resolver asyncPerformQuery: query
runLoopMode: _runLoopMode
delegate: self];
}
}
- (void)resolver: (OFDNSResolver *)resolver
didPerformQuery: (OFDNSQuery *)query
response: (OFDNSResponse *)response
exception: (id)exception
{
_numExpectedResponses--;
if ([exception isKindOfClass: [OFDNSQueryFailedException class]] &&
[exception error] == OF_DNS_RESOLVER_ERROR_SERVER_NAME_ERROR &&
!_isFQDN && _numExpectedResponses == 0 && _addresses.count == 0 &&
_searchDomainIndex + 1 < _settings->_searchDomains.count) {
_searchDomainIndex++;
[self sendQueries];
return;
}
for (OF_KINDOF(OFDNSResourceRecord *) record in
[response.answerRecords objectForKey: query.domainName]) {
const of_socket_address_t *address = NULL;
OFDNSQuery *CNAMEQuery;
if ([record DNSClass] != OF_DNS_CLASS_IN)
continue;
if (addressForRecord(record, &address, _addressFamily)) {
[_addresses addItem: address];
continue;
}
if ([record recordType] != OF_DNS_RECORD_TYPE_CNAME)
continue;
/* FIXME: Check if it's already in answers */
CNAMEQuery = [OFDNSQuery queryWithDomainName: [record alias]
DNSClass: OF_DNS_CLASS_IN
recordType: query.recordType];
_numExpectedResponses++;
[_resolver asyncPerformQuery: CNAMEQuery
runLoopMode: _runLoopMode
delegate: self];
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
domainName = [OFString stringWithFormat: @"%@.%@",
_host, searchDomain];
} else
domainName = _host;
#ifdef OF_HAVE_IPV6
if (_addressFamily == OFSocketAddressFamilyIPv6 ||
_addressFamily == OFSocketAddressFamilyAny) {
OFDNSQuery *query = [OFDNSQuery
queryWithDomainName: domainName
DNSClass: OFDNSClassIN
recordType: OFDNSRecordTypeAAAA];
_numExpectedResponses++;
[_resolver asyncPerformQuery: query
runLoopMode: _runLoopMode
delegate: self];
}
#endif
if (_addressFamily == OFSocketAddressFamilyIPv4 ||
_addressFamily == OFSocketAddressFamilyAny) {
OFDNSQuery *query = [OFDNSQuery
queryWithDomainName: domainName
DNSClass: OFDNSClassIN
recordType: OFDNSRecordTypeA];
_numExpectedResponses++;
[_resolver asyncPerformQuery: query
runLoopMode: _runLoopMode
delegate: self];
}
}
- (void)resolver: (OFDNSResolver *)resolver
didPerformQuery: (OFDNSQuery *)query
response: (OFDNSResponse *)response
exception: (id)exception
{
_numExpectedResponses--;
if ([exception isKindOfClass: [OFDNSQueryFailedException class]] &&
[exception errorCode] == OFDNSResolverErrorCodeServerNameError &&
!_isFQDN && _numExpectedResponses == 0 && _addresses.count == 0 &&
_searchDomainIndex + 1 < _settings->_searchDomains.count) {
_searchDomainIndex++;
[self sendQueries];
return;
}
for (OF_KINDOF(OFDNSResourceRecord *) record in
[response.answerRecords objectForKey: query.domainName]) {
const OFSocketAddress *address = NULL;
OFDNSQuery *CNAMEQuery;
if ([record DNSClass] != OFDNSClassIN)
continue;
if (addressForRecord(record, &address, _addressFamily)) {
[_addresses addItem: address];
continue;
}
if ([record recordType] != OFDNSRecordTypeCNAME)
continue;
/* FIXME: Check if it's already in answers */
CNAMEQuery = [OFDNSQuery queryWithDomainName: [record alias]
DNSClass: OFDNSClassIN
recordType: query.recordType];
_numExpectedResponses++;
[_resolver asyncPerformQuery: CNAMEQuery
runLoopMode: _runLoopMode
delegate: self];
}
|
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
|
_addresses = nil;
if ([exception isKindOfClass:
[OFDNSQueryFailedException class]])
exception = [OFResolveHostFailedException
exceptionWithHost: _host
addressFamily: _addressFamily
error: [exception error]];
if (exception == nil)
exception = [OFResolveHostFailedException
exceptionWithHost: _host
addressFamily: _addressFamily
error: OF_DNS_RESOLVER_ERROR_NO_RESULT];
} else
exception = nil;
if ([_delegate respondsToSelector:
@selector(resolver:didResolveHost:addresses:exception:)])
[_delegate resolver: _resolver
didResolveHost: _host
addresses: _addresses
exception: exception];
}
- (void)asyncResolve
{
void *pool = objc_autoreleasePoolPush();
OFArray OF_GENERIC(OFString *) *aliases;
@try {
of_socket_address_t address =
of_socket_address_parse_ip(_host, 0);
OFData *addresses = nil;
id exception = nil;
if (_addressFamily == address.family ||
_addressFamily == OF_SOCKET_ADDRESS_FAMILY_ANY)
addresses = [OFData dataWithItems: &address
count: 1
itemSize: sizeof(address)];
else
exception = [OFInvalidArgumentException exception];
callDelegateInMode(_runLoopMode, _delegate, _resolver, _host,
addresses, exception);
objc_autoreleasePoolPop(pool);
return;
} @catch (OFInvalidFormatException *e) {
}
if ((aliases = [_settings->_staticHosts objectForKey: _host]) != nil) {
OFMutableData *addresses = [OFMutableData
dataWithItemSize: sizeof(of_socket_address_t)];
id exception = nil;
for (OFString *alias in aliases) {
of_socket_address_t address;
@try {
address = of_socket_address_parse_ip(alias, 0);
} @catch (OFInvalidFormatException *e) {
continue;
}
if (_addressFamily != address.family &&
_addressFamily != OF_SOCKET_ADDRESS_FAMILY_ANY)
continue;
[addresses addItem: &address];
}
[addresses makeImmutable];
if (addresses.count == 0) {
addresses = nil;
exception = [OFResolveHostFailedException
exceptionWithHost: _host
addressFamily: _addressFamily
error: OF_DNS_RESOLVER_ERROR_NO_RESULT];
}
callDelegateInMode(_runLoopMode, _delegate, _resolver, _host,
addresses, exception);
objc_autoreleasePoolPop(pool);
return;
}
_isFQDN = isFQDN(_host, _settings->_minNumberOfDotsInAbsoluteName);
_addresses = [[OFMutableData alloc]
initWithItemSize: sizeof(of_socket_address_t)];
[self sendQueries];
objc_autoreleasePoolPop(pool);
}
- (OFData *)resolve
{
void *pool = objc_autoreleasePoolPush();
OFRunLoop *runLoop = [OFRunLoop currentRunLoop];
OFHostAddressResolverDelegate *delegate;
OFData *ret;
delegate = [[[OFHostAddressResolverDelegate alloc] init] autorelease];
_runLoopMode = [resolveRunLoopMode copy];
_delegate = [delegate retain];
[self asyncResolve];
while (!delegate->_done)
[runLoop runMode: resolveRunLoopMode
beforeDate: nil];
/* Cleanup */
[runLoop runMode: resolveRunLoopMode
beforeDate: [OFDate date]];
if (delegate->_exception != nil)
@throw delegate->_exception;
ret = [delegate->_addresses copy];
objc_autoreleasePoolPop(pool);
return [ret autorelease];
}
@end
@implementation OFHostAddressResolverDelegate
- (void)dealloc
{
|
|
|
|
<
|
|
|
|
|
|
|
|
<
|
<
<
<
|
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
|
_addresses = nil;
if ([exception isKindOfClass:
[OFDNSQueryFailedException class]])
exception = [OFResolveHostFailedException
exceptionWithHost: _host
addressFamily: _addressFamily
errorCode: [exception errorCode]];
if (exception == nil)
exception = [OFResolveHostFailedException
exceptionWithHost: _host
addressFamily: _addressFamily
errorCode: OFDNSResolverErrorCodeNoResult];
} else
exception = nil;
if ([_delegate respondsToSelector:
@selector(resolver:didResolveHost:addresses:exception:)])
[_delegate resolver: _resolver
didResolveHost: _host
addresses: _addresses
exception: exception];
}
- (void)asyncResolve
{
void *pool = objc_autoreleasePoolPush();
OFArray OF_GENERIC(OFString *) *aliases;
@try {
OFSocketAddress address = OFSocketAddressParseIP(_host, 0);
OFData *addresses = nil;
id exception = nil;
if (_addressFamily == address.family ||
_addressFamily == OFSocketAddressFamilyAny)
addresses = [OFData dataWithItems: &address
count: 1
itemSize: sizeof(address)];
else
exception = [OFInvalidArgumentException exception];
callDelegateInMode(_runLoopMode, _delegate, _resolver, _host,
addresses, exception);
objc_autoreleasePoolPop(pool);
return;
} @catch (OFInvalidFormatException *e) {
}
if ((aliases = [_settings->_staticHosts objectForKey: _host]) != nil) {
OFMutableData *addresses = [OFMutableData
dataWithItemSize: sizeof(OFSocketAddress)];
id exception = nil;
for (OFString *alias in aliases) {
OFSocketAddress address;
@try {
address = OFSocketAddressParseIP(alias, 0);
} @catch (OFInvalidFormatException *e) {
continue;
}
if (_addressFamily != address.family &&
_addressFamily != OFSocketAddressFamilyAny)
continue;
[addresses addItem: &address];
}
[addresses makeImmutable];
if (addresses.count == 0) {
addresses = nil;
exception = [OFResolveHostFailedException
exceptionWithHost: _host
addressFamily: _addressFamily
errorCode: OFDNSResolverErrorCodeNoResult];
}
callDelegateInMode(_runLoopMode, _delegate, _resolver, _host,
addresses, exception);
objc_autoreleasePoolPop(pool);
return;
}
_isFQDN = isFQDN(_host, _settings->_minNumberOfDotsInAbsoluteName);
_addresses = [[OFMutableData alloc]
initWithItemSize: sizeof(OFSocketAddress)];
[self sendQueries];
objc_autoreleasePoolPop(pool);
}
- (OFData *)resolve
{
void *pool = objc_autoreleasePoolPush();
OFRunLoop *runLoop = [OFRunLoop currentRunLoop];
OFHostAddressResolverDelegate *delegate;
OFData *ret;
delegate = [[[OFHostAddressResolverDelegate alloc] init] autorelease];
_runLoopMode = [resolveRunLoopMode copy];
_delegate = [delegate retain];
[self asyncResolve];
while (!delegate->_done)
[runLoop runMode: resolveRunLoopMode beforeDate: nil];
/* Cleanup */
[runLoop runMode: resolveRunLoopMode beforeDate: [OFDate date]];
if (delegate->_exception != nil)
@throw delegate->_exception;
ret = [delegate->_addresses copy];
objc_autoreleasePoolPop(pool);
return [ret autorelease];
}
@end
@implementation OFHostAddressResolverDelegate
- (void)dealloc
{
|