︙ | | | ︙ | |
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
|
#import "OFOutOfRangeException.h"
#import "OFTruncatedDataException.h"
#ifndef SOCK_DNS
# define SOCK_DNS 0
#endif
#define BUFFER_LENGTH OF_DNS_RESOLVER_BUFFER_LENGTH
#define MAX_DNS_RESPONSE_LENGTH 65536
/*
* RFC 1035 doesn't specify if pointers to pointers are allowed, and if so how
* many. Since it's unspecified, we have to assume that it might happen, but we
* also want to limit it to avoid DoS. Limiting it to 16 levels of pointers and
* immediately rejecting pointers to itself seems like a fair balance.
*/
#define MAX_ALLOWED_POINTERS 16
#define CNAME_RECURSION 3
@interface OFDNSResolver () <OFUDPSocketDelegate, OFTCPSocketDelegate>
- (void)of_contextTimedOut: (OFDNSResolverContext *)context;
@end
OF_DIRECT_MEMBERS
@interface OFDNSResolverContext: OFObject
{
@public
OFDNSQuery *_query;
OFNumber *_ID;
OFDNSResolverSettings *_settings;
size_t _nameServersIndex;
unsigned int _attempt;
id <OFDNSResolverQueryDelegate> _delegate;
OFData *_queryData;
of_socket_address_t _usedNameServer;
OFTCPSocket *_TCPSocket;
OFMutableData *_TCPQueryData;
void *_TCPBuffer;
size_t _responseLength;
OFTimer *_cancelTimer;
}
|
|
|
<
|
<
|
|
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
|
#import "OFOutOfRangeException.h"
#import "OFTruncatedDataException.h"
#ifndef SOCK_DNS
# define SOCK_DNS 0
#endif
static const size_t bufferLength = OFDNSResolverBufferLength;
static const size_t maxDNSResponseLength = 65536;
/*
* RFC 1035 doesn't specify if pointers to pointers are allowed, and if so how
* many. Since it's unspecified, we have to assume that it might happen, but we
* also want to limit it to avoid DoS. Limiting it to 16 levels of pointers and
* immediately rejecting pointers to itself seems like a fair balance.
*/
static const uint_fast8_t maxAllowedPointers = 16;
@interface OFDNSResolver () <OFUDPSocketDelegate, OFTCPSocketDelegate>
- (void)of_contextTimedOut: (OFDNSResolverContext *)context;
@end
OF_DIRECT_MEMBERS
@interface OFDNSResolverContext: OFObject
{
@public
OFDNSQuery *_query;
OFNumber *_ID;
OFDNSResolverSettings *_settings;
size_t _nameServersIndex;
unsigned int _attempt;
id <OFDNSResolverQueryDelegate> _delegate;
OFData *_queryData;
OFSocketAddress _usedNameServer;
OFTCPSocket *_TCPSocket;
OFMutableData *_TCPQueryData;
void *_TCPBuffer;
size_t _responseLength;
OFTimer *_cancelTimer;
}
|
︙ | | | ︙ | |
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
|
[components addObject: component];
} while (componentLength > 0);
return [components componentsJoinedByString: @"."];
}
static OF_KINDOF(OFDNSResourceRecord *)
parseResourceRecord(OFString *name, of_dns_class_t DNSClass,
of_dns_record_type_t recordType, uint32_t TTL, const unsigned char *buffer,
size_t length, size_t i, uint16_t dataLength)
{
if (recordType == OF_DNS_RECORD_TYPE_A && DNSClass == OF_DNS_CLASS_IN) {
of_socket_address_t address;
if (dataLength != 4)
@throw [OFInvalidServerReplyException exception];
memset(&address, 0, sizeof(address));
address.family = OF_SOCKET_ADDRESS_FAMILY_IPV4;
address.length = sizeof(address.sockaddr.in);
address.sockaddr.in.sin_family = AF_INET;
memcpy(&address.sockaddr.in.sin_addr.s_addr, buffer + i, 4);
return [[[OFADNSResourceRecord alloc]
initWithName: name
address: &address
TTL: TTL] autorelease];
} else if (recordType == OF_DNS_RECORD_TYPE_NS) {
size_t j = i;
OFString *authoritativeHost = parseName(buffer, length, &j,
MAX_ALLOWED_POINTERS);
if (j != i + dataLength)
@throw [OFInvalidServerReplyException exception];
return [[[OFNSDNSResourceRecord alloc]
initWithName: name
DNSClass: DNSClass
authoritativeHost: authoritativeHost
TTL: TTL] autorelease];
} else if (recordType == OF_DNS_RECORD_TYPE_CNAME) {
size_t j = i;
OFString *alias = parseName(buffer, length, &j,
MAX_ALLOWED_POINTERS);
if (j != i + dataLength)
@throw [OFInvalidServerReplyException exception];
return [[[OFCNAMEDNSResourceRecord alloc]
initWithName: name
DNSClass: DNSClass
alias: alias
TTL: TTL] autorelease];
} else if (recordType == OF_DNS_RECORD_TYPE_SOA) {
size_t j = i;
OFString *primaryNameServer = parseName(buffer, length, &j,
MAX_ALLOWED_POINTERS);
OFString *responsiblePerson;
uint32_t serialNumber, refreshInterval, retryInterval;
uint32_t expirationInterval, minTTL;
if (j > i + dataLength)
@throw [OFInvalidServerReplyException exception];
responsiblePerson = parseName(buffer, length, &j,
MAX_ALLOWED_POINTERS);
if (dataLength - (j - i) != 20)
@throw [OFInvalidServerReplyException exception];
serialNumber = (buffer[j] << 24) | (buffer[j + 1] << 16) |
(buffer[j + 2] << 8) | buffer[j + 3];
refreshInterval = (buffer[j + 4] << 24) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
[components addObject: component];
} while (componentLength > 0);
return [components componentsJoinedByString: @"."];
}
static OF_KINDOF(OFDNSResourceRecord *)
parseResourceRecord(OFString *name, OFDNSClass DNSClass,
OFDNSRecordType recordType, uint32_t TTL, const unsigned char *buffer,
size_t length, size_t i, uint16_t dataLength)
{
if (recordType == OFDNSRecordTypeA && DNSClass == OFDNSClassIN) {
OFSocketAddress address;
if (dataLength != 4)
@throw [OFInvalidServerReplyException exception];
memset(&address, 0, sizeof(address));
address.family = OFSocketAddressFamilyIPv4;
address.length = sizeof(address.sockaddr.in);
address.sockaddr.in.sin_family = AF_INET;
memcpy(&address.sockaddr.in.sin_addr.s_addr, buffer + i, 4);
return [[[OFADNSResourceRecord alloc]
initWithName: name
address: &address
TTL: TTL] autorelease];
} else if (recordType == OFDNSRecordTypeNS) {
size_t j = i;
OFString *authoritativeHost = parseName(buffer, length, &j,
maxAllowedPointers);
if (j != i + dataLength)
@throw [OFInvalidServerReplyException exception];
return [[[OFNSDNSResourceRecord alloc]
initWithName: name
DNSClass: DNSClass
authoritativeHost: authoritativeHost
TTL: TTL] autorelease];
} else if (recordType == OFDNSRecordTypeCNAME) {
size_t j = i;
OFString *alias = parseName(buffer, length, &j,
maxAllowedPointers);
if (j != i + dataLength)
@throw [OFInvalidServerReplyException exception];
return [[[OFCNAMEDNSResourceRecord alloc]
initWithName: name
DNSClass: DNSClass
alias: alias
TTL: TTL] autorelease];
} else if (recordType == OFDNSRecordTypeSOA) {
size_t j = i;
OFString *primaryNameServer = parseName(buffer, length, &j,
maxAllowedPointers);
OFString *responsiblePerson;
uint32_t serialNumber, refreshInterval, retryInterval;
uint32_t expirationInterval, minTTL;
if (j > i + dataLength)
@throw [OFInvalidServerReplyException exception];
responsiblePerson = parseName(buffer, length, &j,
maxAllowedPointers);
if (dataLength - (j - i) != 20)
@throw [OFInvalidServerReplyException exception];
serialNumber = (buffer[j] << 24) | (buffer[j + 1] << 16) |
(buffer[j + 2] << 8) | buffer[j + 3];
refreshInterval = (buffer[j + 4] << 24) |
|
︙ | | | ︙ | |
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
|
responsiblePerson: responsiblePerson
serialNumber: serialNumber
refreshInterval: refreshInterval
retryInterval: retryInterval
expirationInterval: expirationInterval
minTTL: minTTL
TTL: TTL] autorelease];
} else if (recordType == OF_DNS_RECORD_TYPE_PTR) {
size_t j = i;
OFString *domainName = parseName(buffer, length, &j,
MAX_ALLOWED_POINTERS);
if (j != i + dataLength)
@throw [OFInvalidServerReplyException exception];
return [[[OFPTRDNSResourceRecord alloc]
initWithName: name
DNSClass: DNSClass
domainName: domainName
TTL: TTL] autorelease];
} else if (recordType == OF_DNS_RECORD_TYPE_HINFO) {
size_t j = i;
OFString *CPU = parseString(buffer, length, &j);
OFString *OS;
if (j > i + dataLength)
@throw [OFInvalidServerReplyException exception];
OS = parseString(buffer, length, &j);
if (j != i + dataLength)
@throw [OFInvalidServerReplyException exception];
return [[[OFHINFODNSResourceRecord alloc]
initWithName: name
DNSClass: DNSClass
CPU: CPU
OS: OS
TTL: TTL] autorelease];
} else if (recordType == OF_DNS_RECORD_TYPE_MX) {
uint16_t preference;
size_t j;
OFString *mailExchange;
if (dataLength < 2)
@throw [OFInvalidServerReplyException exception];
preference = (buffer[i] << 8) | buffer[i + 1];
j = i + 2;
mailExchange = parseName(buffer, length, &j,
MAX_ALLOWED_POINTERS);
if (j != i + dataLength)
@throw [OFInvalidServerReplyException exception];
return [[[OFMXDNSResourceRecord alloc]
initWithName: name
DNSClass: DNSClass
preference: preference
mailExchange: mailExchange
TTL: TTL] autorelease];
} else if (recordType == OF_DNS_RECORD_TYPE_TXT) {
OFMutableArray *textStrings = [OFMutableArray array];
while (dataLength > 0) {
uint_fast8_t stringLength = buffer[i++];
dataLength--;
if (stringLength > dataLength)
|
|
|
|
|
|
|
|
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
|
responsiblePerson: responsiblePerson
serialNumber: serialNumber
refreshInterval: refreshInterval
retryInterval: retryInterval
expirationInterval: expirationInterval
minTTL: minTTL
TTL: TTL] autorelease];
} else if (recordType == OFDNSRecordTypePTR) {
size_t j = i;
OFString *domainName = parseName(buffer, length, &j,
maxAllowedPointers);
if (j != i + dataLength)
@throw [OFInvalidServerReplyException exception];
return [[[OFPTRDNSResourceRecord alloc]
initWithName: name
DNSClass: DNSClass
domainName: domainName
TTL: TTL] autorelease];
} else if (recordType == OFDNSRecordTypeHINFO) {
size_t j = i;
OFString *CPU = parseString(buffer, length, &j);
OFString *OS;
if (j > i + dataLength)
@throw [OFInvalidServerReplyException exception];
OS = parseString(buffer, length, &j);
if (j != i + dataLength)
@throw [OFInvalidServerReplyException exception];
return [[[OFHINFODNSResourceRecord alloc]
initWithName: name
DNSClass: DNSClass
CPU: CPU
OS: OS
TTL: TTL] autorelease];
} else if (recordType == OFDNSRecordTypeMX) {
uint16_t preference;
size_t j;
OFString *mailExchange;
if (dataLength < 2)
@throw [OFInvalidServerReplyException exception];
preference = (buffer[i] << 8) | buffer[i + 1];
j = i + 2;
mailExchange = parseName(buffer, length, &j,
maxAllowedPointers);
if (j != i + dataLength)
@throw [OFInvalidServerReplyException exception];
return [[[OFMXDNSResourceRecord alloc]
initWithName: name
DNSClass: DNSClass
preference: preference
mailExchange: mailExchange
TTL: TTL] autorelease];
} else if (recordType == OFDNSRecordTypeTXT) {
OFMutableArray *textStrings = [OFMutableArray array];
while (dataLength > 0) {
uint_fast8_t stringLength = buffer[i++];
dataLength--;
if (stringLength > dataLength)
|
︙ | | | ︙ | |
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
|
[textStrings makeImmutable];
return [[[OFTXTDNSResourceRecord alloc]
initWithName: name
DNSClass: DNSClass
textStrings: textStrings
TTL: TTL] autorelease];
} else if (recordType == OF_DNS_RECORD_TYPE_RP) {
size_t j = i;
OFString *mailbox = parseName(buffer, length, &j,
MAX_ALLOWED_POINTERS);
OFString *TXTDomainName;
if (j > i + dataLength)
@throw [OFInvalidServerReplyException exception];
TXTDomainName = parseName(buffer, length, &j,
MAX_ALLOWED_POINTERS);
if (j != i + dataLength)
@throw [OFInvalidServerReplyException exception];
return [[[OFRPDNSResourceRecord alloc]
initWithName: name
DNSClass: DNSClass
mailbox: mailbox
TXTDomainName: TXTDomainName
TTL: TTL] autorelease];
} else if (recordType == OF_DNS_RECORD_TYPE_AAAA &&
DNSClass == OF_DNS_CLASS_IN) {
of_socket_address_t address;
if (dataLength != 16)
@throw [OFInvalidServerReplyException exception];
memset(&address, 0, sizeof(address));
address.family = OF_SOCKET_ADDRESS_FAMILY_IPV6;
address.length = sizeof(address.sockaddr.in6);
#ifdef AF_INET6
address.sockaddr.in6.sin6_family = AF_INET6;
#else
address.sockaddr.in6.sin6_family = AF_UNSPEC;
#endif
memcpy(address.sockaddr.in6.sin6_addr.s6_addr, buffer + i, 16);
return [[[OFAAAADNSResourceRecord alloc]
initWithName: name
address: &address
TTL: TTL] autorelease];
} else if (recordType == OF_DNS_RECORD_TYPE_SRV &&
DNSClass == OF_DNS_CLASS_IN) {
uint16_t priority, weight, port;
size_t j;
OFString *target;
if (dataLength < 6)
@throw [OFInvalidServerReplyException exception];
priority = (buffer[i] << 8) | buffer[i + 1];
weight = (buffer[i + 2] << 8) | buffer[i + 3];
port = (buffer[i + 4] << 8) | buffer[i + 5];
j = i + 6;
target = parseName(buffer, length, &j, MAX_ALLOWED_POINTERS);
if (j != i + dataLength)
@throw [OFInvalidServerReplyException exception];
return [[[OFSRVDNSResourceRecord alloc]
initWithName: name
priority: priority
|
|
|
|
|
|
|
|
|
|
|
|
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
|
[textStrings makeImmutable];
return [[[OFTXTDNSResourceRecord alloc]
initWithName: name
DNSClass: DNSClass
textStrings: textStrings
TTL: TTL] autorelease];
} else if (recordType == OFDNSRecordTypeRP) {
size_t j = i;
OFString *mailbox = parseName(buffer, length, &j,
maxAllowedPointers);
OFString *TXTDomainName;
if (j > i + dataLength)
@throw [OFInvalidServerReplyException exception];
TXTDomainName = parseName(buffer, length, &j,
maxAllowedPointers);
if (j != i + dataLength)
@throw [OFInvalidServerReplyException exception];
return [[[OFRPDNSResourceRecord alloc]
initWithName: name
DNSClass: DNSClass
mailbox: mailbox
TXTDomainName: TXTDomainName
TTL: TTL] autorelease];
} else if (recordType == OFDNSRecordTypeAAAA &&
DNSClass == OFDNSClassIN) {
OFSocketAddress address;
if (dataLength != 16)
@throw [OFInvalidServerReplyException exception];
memset(&address, 0, sizeof(address));
address.family = OFSocketAddressFamilyIPv6;
address.length = sizeof(address.sockaddr.in6);
#ifdef AF_INET6
address.sockaddr.in6.sin6_family = AF_INET6;
#else
address.sockaddr.in6.sin6_family = AF_UNSPEC;
#endif
memcpy(address.sockaddr.in6.sin6_addr.s6_addr, buffer + i, 16);
return [[[OFAAAADNSResourceRecord alloc]
initWithName: name
address: &address
TTL: TTL] autorelease];
} else if (recordType == OFDNSRecordTypeSRV &&
DNSClass == OFDNSClassIN) {
uint16_t priority, weight, port;
size_t j;
OFString *target;
if (dataLength < 6)
@throw [OFInvalidServerReplyException exception];
priority = (buffer[i] << 8) | buffer[i + 1];
weight = (buffer[i + 2] << 8) | buffer[i + 3];
port = (buffer[i + 4] << 8) | buffer[i + 5];
j = i + 6;
target = parseName(buffer, length, &j, maxAllowedPointers);
if (j != i + dataLength)
@throw [OFInvalidServerReplyException exception];
return [[[OFSRVDNSResourceRecord alloc]
initWithName: name
priority: priority
|
︙ | | | ︙ | |
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
|
{
OFMutableDictionary *ret = [OFMutableDictionary dictionary];
OFEnumerator OF_GENERIC(OFMutableArray *) *objectEnumerator;
OFMutableArray *array;
for (uint_fast16_t j = 0; j < count; j++) {
OFString *name = parseName(buffer, length, i,
MAX_ALLOWED_POINTERS);
of_dns_class_t DNSClass;
of_dns_record_type_t recordType;
uint32_t TTL;
uint16_t dataLength;
OFDNSResourceRecord *record;
if (*i + 10 > length)
@throw [OFTruncatedDataException exception];
|
|
|
|
|
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
|
{
OFMutableDictionary *ret = [OFMutableDictionary dictionary];
OFEnumerator OF_GENERIC(OFMutableArray *) *objectEnumerator;
OFMutableArray *array;
for (uint_fast16_t j = 0; j < count; j++) {
OFString *name = parseName(buffer, length, i,
maxAllowedPointers);
OFDNSClass DNSClass;
OFDNSRecordType recordType;
uint32_t TTL;
uint16_t dataLength;
OFDNSResourceRecord *record;
if (*i + 10 > length)
@throw [OFTruncatedDataException exception];
|
︙ | | | ︙ | |
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
|
buffer, length, *i, dataLength);
*i += dataLength;
array = [ret objectForKey: name];
if (array == nil) {
array = [OFMutableArray array];
[ret setObject: array
forKey: name];
}
[array addObject: record];
}
objectEnumerator = [ret objectEnumerator];
while ((array = [objectEnumerator nextObject]) != nil)
|
|
<
|
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
|
buffer, length, *i, dataLength);
*i += dataLength;
array = [ret objectForKey: name];
if (array == nil) {
array = [OFMutableArray array];
[ret setObject: array forKey: name];
}
[array addObject: record];
}
objectEnumerator = [ret objectEnumerator];
while ((array = [objectEnumerator nextObject]) != nil)
|
︙ | | | ︙ | |
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
|
_settings = [settings copy];
_delegate = [delegate retain];
queryData = [OFMutableData dataWithCapacity: 512];
/* Header */
tmp = OF_BSWAP16_IF_LE(_ID.unsignedShortValue);
[queryData addItems: &tmp
count: 2];
/* RD */
tmp = OF_BSWAP16_IF_LE(1u << 8);
[queryData addItems: &tmp
count: 2];
/* QDCOUNT */
tmp = OF_BSWAP16_IF_LE(1);
[queryData addItems: &tmp
count: 2];
/* ANCOUNT, NSCOUNT and ARCOUNT */
[queryData increaseCountBy: 6];
/* Question */
/* QNAME */
for (OFString *component in
|
|
|
<
<
|
|
<
<
|
|
<
<
|
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
|
_settings = [settings copy];
_delegate = [delegate retain];
queryData = [OFMutableData dataWithCapacity: 512];
/* Header */
tmp = OFToBigEndian16(_ID.unsignedShortValue);
[queryData addItems: &tmp count: 2];
/* RD */
tmp = OFToBigEndian16(1u << 8);
[queryData addItems: &tmp count: 2];
/* QDCOUNT */
tmp = OFToBigEndian16(1);
[queryData addItems: &tmp count: 2];
/* ANCOUNT, NSCOUNT and ARCOUNT */
[queryData increaseCountBy: 6];
/* Question */
/* QNAME */
for (OFString *component in
|
︙ | | | ︙ | |
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
|
length8 = (uint8_t)length;
[queryData addItem: &length8];
[queryData addItems: component.UTF8String
count: length];
}
/* QTYPE */
tmp = OF_BSWAP16_IF_LE(_query.recordType);
[queryData addItems: &tmp
count: 2];
/* QCLASS */
tmp = OF_BSWAP16_IF_LE(_query.DNSClass);
[queryData addItems: &tmp
count: 2];
[queryData makeImmutable];
_queryData = [queryData copy];
objc_autoreleasePoolPop(pool);
} @catch (id e) {
[self release];
|
|
|
<
<
|
|
<
<
|
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
|
length8 = (uint8_t)length;
[queryData addItem: &length8];
[queryData addItems: component.UTF8String
count: length];
}
/* QTYPE */
tmp = OFToBigEndian16(_query.recordType);
[queryData addItems: &tmp count: 2];
/* QCLASS */
tmp = OFToBigEndian16(_query.DNSClass);
[queryData addItems: &tmp count: 2];
[queryData makeImmutable];
_queryData = [queryData copy];
objc_autoreleasePoolPop(pool);
} @catch (id e) {
[self release];
|
︙ | | | ︙ | |
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
|
[_query release];
[_ID release];
[_settings release];
[_delegate release];
[_queryData release];
[_TCPSocket release];
[_TCPQueryData release];
free(_TCPBuffer);
[_cancelTimer release];
[super dealloc];
}
@end
@implementation OFDNSResolver
#ifdef OF_AMIGAOS
+ (void)initialize
{
if (self != [OFDNSResolver class])
return;
if (!of_socket_init())
@throw [OFInitializationFailedException
exceptionWithClass: self];
}
#endif
+ (instancetype)resolver
{
|
|
|
|
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
|
[_query release];
[_ID release];
[_settings release];
[_delegate release];
[_queryData release];
[_TCPSocket release];
[_TCPQueryData release];
OFFreeMemory(_TCPBuffer);
[_cancelTimer release];
[super dealloc];
}
@end
@implementation OFDNSResolver
#ifdef OF_AMIGAOS
+ (void)initialize
{
if (self != [OFDNSResolver class])
return;
if (!OFSocketInit())
@throw [OFInitializationFailedException
exceptionWithClass: self];
}
#endif
+ (instancetype)resolver
{
|
︙ | | | ︙ | |
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
|
- (void)setSearchDomains: (OFArray *)searchDomains
{
OFArray *old = _settings->_searchDomains;
_settings->_searchDomains = [searchDomains copy];
[old release];
}
- (of_time_interval_t)timeout
{
return _settings->_timeout;
}
- (void)setTimeout: (of_time_interval_t)timeout
{
_settings->_timeout = timeout;
}
- (unsigned int)maxAttempts
{
return _settings->_maxAttempts;
|
|
|
|
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
|
- (void)setSearchDomains: (OFArray *)searchDomains
{
OFArray *old = _settings->_searchDomains;
_settings->_searchDomains = [searchDomains copy];
[old release];
}
- (OFTimeInterval)timeout
{
return _settings->_timeout;
}
- (void)setTimeout: (OFTimeInterval)timeout
{
_settings->_timeout = timeout;
}
- (unsigned int)maxAttempts
{
return _settings->_maxAttempts;
|
︙ | | | ︙ | |
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
|
}
- (void)setUsesTCP: (bool)usesTCP
{
_settings->_usesTCP = usesTCP;
}
- (of_time_interval_t)configReloadInterval
{
return _settings->_configReloadInterval;
}
- (void)setConfigReloadInterval: (of_time_interval_t)configReloadInterval
{
_settings->_configReloadInterval = configReloadInterval;
}
- (void)of_sendQueryForContext: (OFDNSResolverContext *)context
runLoopMode: (of_run_loop_mode_t)runLoopMode
{
OFUDPSocket *sock;
OFString *nameServer;
[_queries setObject: context
forKey: context->_ID];
[context->_cancelTimer invalidate];
[context->_cancelTimer release];
context->_cancelTimer = nil;
context->_cancelTimer = [[OFTimer alloc]
initWithFireDate: [OFDate dateWithTimeIntervalSinceNow:
context->_settings->_timeout]
interval: context->_settings->_timeout
target: self
selector: @selector(of_contextTimedOut:)
object: context
repeats: false];
[[OFRunLoop currentRunLoop] addTimer: context->_cancelTimer
forMode: runLoopMode];
nameServer = [context->_settings->_nameServers
objectAtIndex: context->_nameServersIndex];
if (context->_settings->_usesTCP) {
OF_ENSURE(context->_TCPSocket == nil);
context->_TCPSocket = [[OFTCPSocket alloc] init];
[_TCPQueries setObject: context
forKey: context->_TCPSocket];
context->_TCPSocket.delegate = self;
[context->_TCPSocket asyncConnectToHost: nameServer
port: 53
runLoopMode: runLoopMode];
return;
}
context->_usedNameServer = of_socket_address_parse_ip(nameServer, 53);
switch (context->_usedNameServer.family) {
#ifdef OF_HAVE_IPV6
case OF_SOCKET_ADDRESS_FAMILY_IPV6:
if (_IPv6Socket == nil) {
of_socket_address_t address =
of_socket_address_parse_ip(@"::", 0);
_IPv6Socket = [[OFUDPSocket alloc] init];
[_IPv6Socket of_bindToAddress: &address
extraType: SOCK_DNS];
_IPv6Socket.canBlock = false;
_IPv6Socket.delegate = self;
}
sock = _IPv6Socket;
break;
#endif
case OF_SOCKET_ADDRESS_FAMILY_IPV4:
if (_IPv4Socket == nil) {
of_socket_address_t address =
of_socket_address_parse_ip(@"0.0.0.0", 0);
_IPv4Socket = [[OFUDPSocket alloc] init];
[_IPv4Socket of_bindToAddress: &address
extraType: SOCK_DNS];
_IPv4Socket.canBlock = false;
_IPv4Socket.delegate = self;
}
sock = _IPv4Socket;
break;
default:
@throw [OFInvalidArgumentException exception];
}
[sock asyncSendData: context->_queryData
receiver: &context->_usedNameServer
runLoopMode: runLoopMode];
[sock asyncReceiveIntoBuffer: _buffer
length: BUFFER_LENGTH
runLoopMode: runLoopMode];
}
- (void)asyncPerformQuery: (OFDNSQuery *)query
delegate: (id <OFDNSResolverQueryDelegate>)delegate
{
[self asyncPerformQuery: query
runLoopMode: of_run_loop_mode_default
delegate: delegate];
}
- (void)asyncPerformQuery: (OFDNSQuery *)query
runLoopMode: (of_run_loop_mode_t)runLoopMode
delegate: (id <OFDNSResolverQueryDelegate>)delegate
{
void *pool = objc_autoreleasePoolPush();
OFNumber *ID;
OFDNSResolverContext *context;
/* Random, unused ID */
do {
ID = [OFNumber numberWithUnsignedShort: of_random16()];
} while ([_queries objectForKey: ID] != nil);
if (query.domainName.UTF8StringLength > 253)
@throw [OFOutOfRangeException exception];
if (_settings->_nameServers.count == 0) {
id exception = [OFDNSQueryFailedException
exceptionWithQuery: query
error: OF_DNS_RESOLVER_ERROR_NO_NAME_SERVER];
[delegate resolver: self
didPerformQuery: query
response: nil
exception: exception];
return;
}
context = [[[OFDNSResolverContext alloc]
initWithQuery: query
ID: ID
settings: _settings
delegate: delegate] autorelease];
[self of_sendQueryForContext: context
runLoopMode: runLoopMode];
objc_autoreleasePoolPop(pool);
}
- (void)of_contextTimedOut: (OFDNSResolverContext *)context
{
of_run_loop_mode_t runLoopMode = [OFRunLoop currentRunLoop].currentMode;
OFDNSQueryFailedException *exception;
if (context->_TCPSocket != nil) {
context->_TCPSocket.delegate = nil;
[context->_TCPSocket cancelAsyncRequests];
[_TCPQueries removeObjectForKey: context->_TCPSocket];
[context->_TCPSocket release];
context->_TCPSocket = nil;
context->_responseLength = 0;
}
if (context->_nameServersIndex + 1 <
context->_settings->_nameServers.count) {
context->_nameServersIndex++;
[self of_sendQueryForContext: context
runLoopMode: runLoopMode];
return;
}
if (++context->_attempt < context->_settings->_maxAttempts) {
context->_nameServersIndex = 0;
[self of_sendQueryForContext: context
runLoopMode: runLoopMode];
return;
}
context = [[context retain] autorelease];
[_queries removeObjectForKey: context->_ID];
/*
* Cancel any pending queries, to avoid a send being still pending and
* trying to access the query once it no longer exists.
*/
[_IPv4Socket cancelAsyncRequests];
[_IPv4Socket asyncReceiveIntoBuffer: _buffer
length: BUFFER_LENGTH];
#ifdef OF_HAVE_IPV6
[_IPv6Socket cancelAsyncRequests];
[_IPv6Socket asyncReceiveIntoBuffer: _buffer
length: BUFFER_LENGTH];
#endif
exception = [OFDNSQueryFailedException
exceptionWithQuery: context->_query
error: OF_DNS_RESOLVER_ERROR_TIMEOUT];
[context->_delegate resolver: self
didPerformQuery: context->_query
response: nil
exception: exception];
}
- (bool)of_handleResponseBuffer: (unsigned char *)buffer
length: (size_t)length
sender: (const of_socket_address_t *)sender
{
OFDictionary *answerRecords = nil, *authorityRecords = nil;
OFDictionary *additionalRecords = nil;
OFDNSResponse *response = nil;
id exception = nil;
OFNumber *ID;
OFDNSResolverContext *context;
|
|
|
|
|
<
|
|
<
|
|
|
|
|
|
|
|
|
|
|
|
|
<
|
|
<
|
<
|
<
|
<
|
|
|
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
|
}
- (void)setUsesTCP: (bool)usesTCP
{
_settings->_usesTCP = usesTCP;
}
- (OFTimeInterval)configReloadInterval
{
return _settings->_configReloadInterval;
}
- (void)setConfigReloadInterval: (OFTimeInterval)configReloadInterval
{
_settings->_configReloadInterval = configReloadInterval;
}
- (void)of_sendQueryForContext: (OFDNSResolverContext *)context
runLoopMode: (OFRunLoopMode)runLoopMode
{
OFUDPSocket *sock;
OFString *nameServer;
[_queries setObject: context forKey: context->_ID];
[context->_cancelTimer invalidate];
[context->_cancelTimer release];
context->_cancelTimer = nil;
context->_cancelTimer = [[OFTimer alloc]
initWithFireDate: [OFDate dateWithTimeIntervalSinceNow:
context->_settings->_timeout]
interval: context->_settings->_timeout
target: self
selector: @selector(of_contextTimedOut:)
object: context
repeats: false];
[[OFRunLoop currentRunLoop] addTimer: context->_cancelTimer
forMode: runLoopMode];
nameServer = [context->_settings->_nameServers
objectAtIndex: context->_nameServersIndex];
if (context->_settings->_usesTCP) {
OFEnsure(context->_TCPSocket == nil);
context->_TCPSocket = [[OFTCPSocket alloc] init];
[_TCPQueries setObject: context forKey: context->_TCPSocket];
context->_TCPSocket.delegate = self;
[context->_TCPSocket asyncConnectToHost: nameServer
port: 53
runLoopMode: runLoopMode];
return;
}
context->_usedNameServer = OFSocketAddressParseIP(nameServer, 53);
switch (context->_usedNameServer.family) {
#ifdef OF_HAVE_IPV6
case OFSocketAddressFamilyIPv6:
if (_IPv6Socket == nil) {
OFSocketAddress address =
OFSocketAddressParseIPv6(@"::", 0);
_IPv6Socket = [[OFUDPSocket alloc] init];
[_IPv6Socket of_bindToAddress: &address
extraType: SOCK_DNS];
_IPv6Socket.canBlock = false;
_IPv6Socket.delegate = self;
}
sock = _IPv6Socket;
break;
#endif
case OFSocketAddressFamilyIPv4:
if (_IPv4Socket == nil) {
OFSocketAddress address =
OFSocketAddressParseIPv4(@"0.0.0.0", 0);
_IPv4Socket = [[OFUDPSocket alloc] init];
[_IPv4Socket of_bindToAddress: &address
extraType: SOCK_DNS];
_IPv4Socket.canBlock = false;
_IPv4Socket.delegate = self;
}
sock = _IPv4Socket;
break;
default:
@throw [OFInvalidArgumentException exception];
}
[sock asyncSendData: context->_queryData
receiver: &context->_usedNameServer
runLoopMode: runLoopMode];
[sock asyncReceiveIntoBuffer: _buffer
length: bufferLength
runLoopMode: runLoopMode];
}
- (void)asyncPerformQuery: (OFDNSQuery *)query
delegate: (id <OFDNSResolverQueryDelegate>)delegate
{
[self asyncPerformQuery: query
runLoopMode: OFDefaultRunLoopMode
delegate: delegate];
}
- (void)asyncPerformQuery: (OFDNSQuery *)query
runLoopMode: (OFRunLoopMode)runLoopMode
delegate: (id <OFDNSResolverQueryDelegate>)delegate
{
void *pool = objc_autoreleasePoolPush();
OFNumber *ID;
OFDNSResolverContext *context;
/* Random, unused ID */
do {
ID = [OFNumber numberWithUnsignedShort: OFRandom16()];
} while ([_queries objectForKey: ID] != nil);
if (query.domainName.UTF8StringLength > 253)
@throw [OFOutOfRangeException exception];
if (_settings->_nameServers.count == 0) {
id exception = [OFDNSQueryFailedException
exceptionWithQuery: query
errorCode: OFDNSResolverErrorCodeNoNameServer];
[delegate resolver: self
didPerformQuery: query
response: nil
exception: exception];
return;
}
context = [[[OFDNSResolverContext alloc]
initWithQuery: query
ID: ID
settings: _settings
delegate: delegate] autorelease];
[self of_sendQueryForContext: context runLoopMode: runLoopMode];
objc_autoreleasePoolPop(pool);
}
- (void)of_contextTimedOut: (OFDNSResolverContext *)context
{
OFRunLoopMode runLoopMode = [OFRunLoop currentRunLoop].currentMode;
OFDNSQueryFailedException *exception;
if (context->_TCPSocket != nil) {
context->_TCPSocket.delegate = nil;
[context->_TCPSocket cancelAsyncRequests];
[_TCPQueries removeObjectForKey: context->_TCPSocket];
[context->_TCPSocket release];
context->_TCPSocket = nil;
context->_responseLength = 0;
}
if (context->_nameServersIndex + 1 <
context->_settings->_nameServers.count) {
context->_nameServersIndex++;
[self of_sendQueryForContext: context runLoopMode: runLoopMode];
return;
}
if (++context->_attempt < context->_settings->_maxAttempts) {
context->_nameServersIndex = 0;
[self of_sendQueryForContext: context runLoopMode: runLoopMode];
return;
}
context = [[context retain] autorelease];
[_queries removeObjectForKey: context->_ID];
/*
* Cancel any pending queries, to avoid a send being still pending and
* trying to access the query once it no longer exists.
*/
[_IPv4Socket cancelAsyncRequests];
[_IPv4Socket asyncReceiveIntoBuffer: _buffer length: bufferLength];
#ifdef OF_HAVE_IPV6
[_IPv6Socket cancelAsyncRequests];
[_IPv6Socket asyncReceiveIntoBuffer: _buffer length: bufferLength];
#endif
exception = [OFDNSQueryFailedException
exceptionWithQuery: context->_query
errorCode: OFDNSResolverErrorCodeTimeout];
[context->_delegate resolver: self
didPerformQuery: context->_query
response: nil
exception: exception];
}
- (bool)of_handleResponseBuffer: (unsigned char *)buffer
length: (size_t)length
sender: (const OFSocketAddress *)sender
{
OFDictionary *answerRecords = nil, *authorityRecords = nil;
OFDictionary *additionalRecords = nil;
OFDNSResponse *response = nil;
id exception = nil;
OFNumber *ID;
OFDNSResolverContext *context;
|
︙ | | | ︙ | |
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
|
if (context == nil)
return true;
if (context->_TCPSocket != nil) {
if ([_TCPQueries objectForKey: context->_TCPSocket] != context)
return true;
} else if (!of_socket_address_equal(sender, &context->_usedNameServer))
return true;
[context->_cancelTimer invalidate];
[context->_cancelTimer release];
context->_cancelTimer = nil;
[_queries removeObjectForKey: ID];
@try {
of_dns_resolver_error_t error = 0;
bool tryNextNameServer = false;
const unsigned char *queryDataBuffer;
size_t i;
uint16_t numQuestions, numAnswers, numAuthorityRecords;
uint16_t numAdditionalRecords;
if (length < 12)
|
|
|
|
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
|
if (context == nil)
return true;
if (context->_TCPSocket != nil) {
if ([_TCPQueries objectForKey: context->_TCPSocket] != context)
return true;
} else if (!OFSocketAddressEqual(sender, &context->_usedNameServer))
return true;
[context->_cancelTimer invalidate];
[context->_cancelTimer release];
context->_cancelTimer = nil;
[_queries removeObjectForKey: ID];
@try {
OFDNSResolverErrorCode errorCode = 0;
bool tryNextNameServer = false;
const unsigned char *queryDataBuffer;
size_t i;
uint16_t numQuestions, numAnswers, numAuthorityRecords;
uint16_t numAdditionalRecords;
if (length < 12)
|
︙ | | | ︙ | |
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
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
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
|
/* Opcode */
if ((buffer[2] & 0x78) != (queryDataBuffer[2] & 0x78))
@throw [OFInvalidServerReplyException exception];
/* TC */
if (buffer[2] & 0x02) {
of_run_loop_mode_t runLoopMode;
if (context->_settings->_usesTCP)
@throw [OFTruncatedDataException exception];
context->_settings->_usesTCP = true;
runLoopMode = [OFRunLoop currentRunLoop].currentMode;
[self of_sendQueryForContext: context
runLoopMode: runLoopMode];
return false;
}
/* RCODE */
switch (buffer[3] & 0x0F) {
case 0:
break;
case 1:
error = OF_DNS_RESOLVER_ERROR_SERVER_INVALID_FORMAT;
break;
case 2:
error = OF_DNS_RESOLVER_ERROR_SERVER_FAILURE;
tryNextNameServer = true;
break;
case 3:
error = OF_DNS_RESOLVER_ERROR_SERVER_NAME_ERROR;
break;
case 4:
error = OF_DNS_RESOLVER_ERROR_SERVER_NOT_IMPLEMENTED;
tryNextNameServer = true;
break;
case 5:
error = OF_DNS_RESOLVER_ERROR_SERVER_REFUSED;
tryNextNameServer = true;
break;
default:
error = OF_DNS_RESOLVER_ERROR_UNKNOWN;
tryNextNameServer = true;
break;
}
if (tryNextNameServer) {
if (context->_nameServersIndex + 1 <
context->_settings->_nameServers.count) {
of_run_loop_mode_t runLoopMode =
[OFRunLoop currentRunLoop].currentMode;
context->_nameServersIndex++;
[self of_sendQueryForContext: context
runLoopMode: runLoopMode];
return false;
}
}
if (buffer[3] & 0x0F)
@throw [OFDNSQueryFailedException
exceptionWithQuery: context->_query
error: error];
numQuestions = (buffer[4] << 8) | buffer[5];
numAnswers = (buffer[6] << 8) | buffer[7];
numAuthorityRecords = (buffer[8] << 8) | buffer[9];
numAdditionalRecords = (buffer[10] << 8) | buffer[11];
i = 12;
/*
* Skip over the questions - we use the ID to identify the
* query.
*
* TODO: Compare to our query, just in case?
*/
for (uint_fast16_t j = 0; j < numQuestions; j++) {
parseName(buffer, length, &i, MAX_ALLOWED_POINTERS);
i += 4;
}
answerRecords = parseSection(buffer, length, &i, numAnswers);
authorityRecords = parseSection(buffer, length, &i,
numAuthorityRecords);
additionalRecords = parseSection(buffer, length, &i,
|
|
|
|
|
|
|
|
|
|
|
|
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
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
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
|
/* Opcode */
if ((buffer[2] & 0x78) != (queryDataBuffer[2] & 0x78))
@throw [OFInvalidServerReplyException exception];
/* TC */
if (buffer[2] & 0x02) {
OFRunLoopMode runLoopMode;
if (context->_settings->_usesTCP)
@throw [OFTruncatedDataException exception];
context->_settings->_usesTCP = true;
runLoopMode = [OFRunLoop currentRunLoop].currentMode;
[self of_sendQueryForContext: context
runLoopMode: runLoopMode];
return false;
}
/* RCODE */
switch (buffer[3] & 0x0F) {
case 0:
break;
case 1:
errorCode = OFDNSResolverErrorCodeServerInvalidFormat;
break;
case 2:
errorCode = OFDNSResolverErrorCodeServerFailure;
tryNextNameServer = true;
break;
case 3:
errorCode = OFDNSResolverErrorCodeServerNameError;
break;
case 4:
errorCode = OFDNSResolverErrorCodeServerNotImplemented;
tryNextNameServer = true;
break;
case 5:
errorCode = OFDNSResolverErrorCodeServerRefused;
tryNextNameServer = true;
break;
default:
errorCode = OFDNSResolverErrorCodeUnknown;
tryNextNameServer = true;
break;
}
if (tryNextNameServer) {
if (context->_nameServersIndex + 1 <
context->_settings->_nameServers.count) {
OFRunLoopMode runLoopMode =
[OFRunLoop currentRunLoop].currentMode;
context->_nameServersIndex++;
[self of_sendQueryForContext: context
runLoopMode: runLoopMode];
return false;
}
}
if (buffer[3] & 0x0F)
@throw [OFDNSQueryFailedException
exceptionWithQuery: context->_query
errorCode: errorCode];
numQuestions = (buffer[4] << 8) | buffer[5];
numAnswers = (buffer[6] << 8) | buffer[7];
numAuthorityRecords = (buffer[8] << 8) | buffer[9];
numAdditionalRecords = (buffer[10] << 8) | buffer[11];
i = 12;
/*
* Skip over the questions - we use the ID to identify the
* query.
*
* TODO: Compare to our query, just in case?
*/
for (uint_fast16_t j = 0; j < numQuestions; j++) {
parseName(buffer, length, &i, maxAllowedPointers);
i += 4;
}
answerRecords = parseSection(buffer, length, &i, numAnswers);
authorityRecords = parseSection(buffer, length, &i,
numAuthorityRecords);
additionalRecords = parseSection(buffer, length, &i,
|
︙ | | | ︙ | |
1057
1058
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
|
return false;
}
- (bool)socket: (OFDatagramSocket *)sock
didReceiveIntoBuffer: (void *)buffer
length: (size_t)length
sender: (const of_socket_address_t *)sender
exception: (id)exception
{
if (exception != nil)
return true;
return [self of_handleResponseBuffer: buffer
length: length
sender: sender];
}
- (void)socket: (OFTCPSocket *)sock
didConnectToHost: (OFString *)host
port: (uint16_t)port
exception: (id)exception
{
OFDNSResolverContext *context = [_TCPQueries objectForKey: sock];
OF_ENSURE(context != nil);
if (exception != nil) {
/*
* TODO: Handle error immediately instead of waiting for the
* timer to try the next nameserver or to retry.
*/
[_TCPQueries removeObjectForKey: context->_TCPSocket];
|
|
|
|
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
|
return false;
}
- (bool)socket: (OFDatagramSocket *)sock
didReceiveIntoBuffer: (void *)buffer
length: (size_t)length
sender: (const OFSocketAddress *)sender
exception: (id)exception
{
if (exception != nil)
return true;
return [self of_handleResponseBuffer: buffer
length: length
sender: sender];
}
- (void)socket: (OFTCPSocket *)sock
didConnectToHost: (OFString *)host
port: (uint16_t)port
exception: (id)exception
{
OFDNSResolverContext *context = [_TCPQueries objectForKey: sock];
OFEnsure(context != nil);
if (exception != nil) {
/*
* TODO: Handle error immediately instead of waiting for the
* timer to try the next nameserver or to retry.
*/
[_TCPQueries removeObjectForKey: context->_TCPSocket];
|
︙ | | | ︙ | |
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
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
|
if (queryDataCount > UINT16_MAX)
@throw [OFOutOfRangeException exception];
context->_TCPQueryData = [[OFMutableData alloc]
initWithCapacity: queryDataCount + 2];
tmp = OF_BSWAP16_IF_LE(queryDataCount);
[context->_TCPQueryData addItems: &tmp
count: sizeof(tmp)];
[context->_TCPQueryData addItems: context->_queryData.items
count: queryDataCount];
}
[sock asyncWriteData: context->_TCPQueryData];
}
- (OFData *)stream: (OFStream *)stream
didWriteData: (OFData *)data
bytesWritten: (size_t)bytesWritten
exception: (id)exception
{
OFTCPSocket *sock = (OFTCPSocket *)stream;
OFDNSResolverContext *context = [_TCPQueries objectForKey: sock];
OF_ENSURE(context != nil);
if (exception != nil) {
/*
* TODO: Handle error immediately instead of waiting for the
* timer to try the next nameserver or to retry.
*/
[_TCPQueries removeObjectForKey: context->_TCPSocket];
[context->_TCPSocket release];
context->_TCPSocket = nil;
context->_responseLength = 0;
return nil;
}
if (context->_TCPBuffer == nil)
context->_TCPBuffer = of_alloc(MAX_DNS_RESPONSE_LENGTH, 1);
[sock asyncReadIntoBuffer: context->_TCPBuffer
exactLength: 2];
return nil;
}
- (bool)stream: (OFStream *)stream
didReadIntoBuffer: (void *)buffer
length: (size_t)length
exception: (id)exception
{
OFTCPSocket *sock = (OFTCPSocket *)stream;
OFDNSResolverContext *context = [_TCPQueries objectForKey: sock];
OF_ENSURE(context != nil);
if (exception != nil) {
/*
* TODO: Handle error immediately instead of waiting for the
* timer to try the next nameserver or to retry.
*/
goto done;
}
if (context->_responseLength == 0) {
unsigned char *ucBuffer = buffer;
OF_ENSURE(length == 2);
context->_responseLength = (ucBuffer[0] << 8) | ucBuffer[1];
if (context->_responseLength > MAX_DNS_RESPONSE_LENGTH)
@throw [OFOutOfRangeException exception];
if (context->_responseLength == 0)
goto done;
[sock asyncReadIntoBuffer: context->_TCPBuffer
exactLength: context->_responseLength];
return false;
}
if (length != context->_responseLength)
/*
* The connection was closed before we received the entire
* response.
*/
goto done;
[self of_handleResponseBuffer: buffer
length: length
sender: NULL];
done:
[_TCPQueries removeObjectForKey: context->_TCPSocket];
[context->_TCPSocket release];
context->_TCPSocket = nil;
context->_responseLength = 0;
return false;
}
- (void)asyncResolveAddressesForHost: (OFString *)host
delegate: (id <OFDNSResolverHostDelegate>)delegate
{
[self asyncResolveAddressesForHost: host
addressFamily: OF_SOCKET_ADDRESS_FAMILY_ANY
runLoopMode: of_run_loop_mode_default
delegate: delegate];
}
- (void)asyncResolveAddressesForHost: (OFString *)host
addressFamily: (of_socket_address_family_t)addressFamily
delegate: (id <OFDNSResolverHostDelegate>)delegate
{
[self asyncResolveAddressesForHost: host
addressFamily: addressFamily
runLoopMode: of_run_loop_mode_default
delegate: delegate];
}
- (void)asyncResolveAddressesForHost: (OFString *)host
addressFamily: (of_socket_address_family_t)addressFamily
runLoopMode: (of_run_loop_mode_t)runLoopMode
delegate: (id <OFDNSResolverHostDelegate>)delegate
{
void *pool = objc_autoreleasePoolPush();
OFHostAddressResolver *resolver = [[[OFHostAddressResolver alloc]
initWithHost: host
addressFamily: addressFamily
resolver: self
settings: _settings
runLoopMode: runLoopMode
delegate: delegate] autorelease];
[resolver asyncResolve];
objc_autoreleasePoolPop(pool);
}
- (OFData *)resolveAddressesForHost: (OFString *)host
addressFamily: (of_socket_address_family_t)addressFamily
{
void *pool = objc_autoreleasePoolPush();
OFHostAddressResolver *resolver = [[[OFHostAddressResolver alloc]
initWithHost: host
addressFamily: addressFamily
resolver: self
settings: _settings
|
|
|
<
|
|
|
<
|
|
|
|
<
<
|
|
|
|
|
|
|
|
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
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
|
if (queryDataCount > UINT16_MAX)
@throw [OFOutOfRangeException exception];
context->_TCPQueryData = [[OFMutableData alloc]
initWithCapacity: queryDataCount + 2];
tmp = OFToBigEndian16(queryDataCount);
[context->_TCPQueryData addItems: &tmp count: sizeof(tmp)];
[context->_TCPQueryData addItems: context->_queryData.items
count: queryDataCount];
}
[sock asyncWriteData: context->_TCPQueryData];
}
- (OFData *)stream: (OFStream *)stream
didWriteData: (OFData *)data
bytesWritten: (size_t)bytesWritten
exception: (id)exception
{
OFTCPSocket *sock = (OFTCPSocket *)stream;
OFDNSResolverContext *context = [_TCPQueries objectForKey: sock];
OFEnsure(context != nil);
if (exception != nil) {
/*
* TODO: Handle error immediately instead of waiting for the
* timer to try the next nameserver or to retry.
*/
[_TCPQueries removeObjectForKey: context->_TCPSocket];
[context->_TCPSocket release];
context->_TCPSocket = nil;
context->_responseLength = 0;
return nil;
}
if (context->_TCPBuffer == nil)
context->_TCPBuffer = OFAllocMemory(maxDNSResponseLength, 1);
[sock asyncReadIntoBuffer: context->_TCPBuffer exactLength: 2];
return nil;
}
- (bool)stream: (OFStream *)stream
didReadIntoBuffer: (void *)buffer
length: (size_t)length
exception: (id)exception
{
OFTCPSocket *sock = (OFTCPSocket *)stream;
OFDNSResolverContext *context = [_TCPQueries objectForKey: sock];
OFEnsure(context != nil);
if (exception != nil) {
/*
* TODO: Handle error immediately instead of waiting for the
* timer to try the next nameserver or to retry.
*/
goto done;
}
if (context->_responseLength == 0) {
unsigned char *ucBuffer = buffer;
OFEnsure(length == 2);
context->_responseLength = (ucBuffer[0] << 8) | ucBuffer[1];
if (context->_responseLength > maxDNSResponseLength)
@throw [OFOutOfRangeException exception];
if (context->_responseLength == 0)
goto done;
[sock asyncReadIntoBuffer: context->_TCPBuffer
exactLength: context->_responseLength];
return false;
}
if (length != context->_responseLength)
/*
* The connection was closed before we received the entire
* response.
*/
goto done;
[self of_handleResponseBuffer: buffer length: length sender: NULL];
done:
[_TCPQueries removeObjectForKey: context->_TCPSocket];
[context->_TCPSocket release];
context->_TCPSocket = nil;
context->_responseLength = 0;
return false;
}
- (void)asyncResolveAddressesForHost: (OFString *)host
delegate: (id <OFDNSResolverHostDelegate>)delegate
{
[self asyncResolveAddressesForHost: host
addressFamily: OFSocketAddressFamilyAny
runLoopMode: OFDefaultRunLoopMode
delegate: delegate];
}
- (void)asyncResolveAddressesForHost: (OFString *)host
addressFamily: (OFSocketAddressFamily)addressFamily
delegate: (id <OFDNSResolverHostDelegate>)delegate
{
[self asyncResolveAddressesForHost: host
addressFamily: addressFamily
runLoopMode: OFDefaultRunLoopMode
delegate: delegate];
}
- (void)asyncResolveAddressesForHost: (OFString *)host
addressFamily: (OFSocketAddressFamily)addressFamily
runLoopMode: (OFRunLoopMode)runLoopMode
delegate: (id <OFDNSResolverHostDelegate>)delegate
{
void *pool = objc_autoreleasePoolPush();
OFHostAddressResolver *resolver = [[[OFHostAddressResolver alloc]
initWithHost: host
addressFamily: addressFamily
resolver: self
settings: _settings
runLoopMode: runLoopMode
delegate: delegate] autorelease];
[resolver asyncResolve];
objc_autoreleasePoolPop(pool);
}
- (OFData *)resolveAddressesForHost: (OFString *)host
addressFamily: (OFSocketAddressFamily)addressFamily
{
void *pool = objc_autoreleasePoolPush();
OFHostAddressResolver *resolver = [[[OFHostAddressResolver alloc]
initWithHost: host
addressFamily: addressFamily
resolver: self
settings: _settings
|
︙ | | | ︙ | |
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
|
enumerator = [_queries objectEnumerator];
while ((context = [enumerator nextObject]) != nil) {
OFDNSQueryFailedException *exception;
exception = [OFDNSQueryFailedException
exceptionWithQuery: context->_query
error: OF_DNS_RESOLVER_ERROR_CANCELED];
[context->_delegate resolver: self
didPerformQuery: context->_query
response: nil
exception: exception];
}
[_queries removeAllObjects];
objc_autoreleasePoolPop(pool);
}
@end
|
|
|
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
|
enumerator = [_queries objectEnumerator];
while ((context = [enumerator nextObject]) != nil) {
OFDNSQueryFailedException *exception;
exception = [OFDNSQueryFailedException
exceptionWithQuery: context->_query
errorCode: OFDNSResolverErrorCodeCanceled];
[context->_delegate resolver: self
didPerformQuery: context->_query
response: nil
exception: exception];
}
[_queries removeAllObjects];
objc_autoreleasePoolPop(pool);
}
@end
|