ObjFW  Check-in [252f11c431]

Overview
Comment:OFDNSResolver: Cache responses
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 252f11c431578d91abc519688ee62727f152c5355f36469d0bb2595b54bdf875
User & Date: js on 2023-12-29 15:53:23
Other Links: manifest | tags
References
2023-12-29
16:30 Fixed ticket [316edadc24]: Add a cache in OFDNSResolver plus 4 other changes artifact: 911cb9ad04 user: js
Context
2023-12-29
16:25
OFDNSResolver: Clean up expired cache entries check-in: 219aa9cf65 user: js tags: trunk
15:53
OFDNSResolver: Cache responses check-in: 252f11c431 user: js tags: trunk
2023-12-28
01:21
OFStream: Fix indentation check-in: 58fea9d1b2 user: js tags: trunk
Changes

Modified src/OFDNSResolver.h from [511ca7b000] to [f7643163de].

28
29
30
31
32
33
34

35
36
37
38
39
40
41
@class OFDNSResolver;
@class OFDNSResolverContext;
@class OFDNSResolverSettings;
@class OFDate;
@class OFDictionary OF_GENERIC(KeyType, ObjectType);
@class OFMutableDictionary OF_GENERIC(KeyType, ObjectType);
@class OFNumber;

@class OFTCPSocket;
@class OFUDPSocket;

/**
 * @enum OFDNSResolverErrorCode OFDNSResolver.h ObjFW/OFDNSResolver.h
 *
 * @brief An enum describing why resolving a host failed.







>







28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
@class OFDNSResolver;
@class OFDNSResolverContext;
@class OFDNSResolverSettings;
@class OFDate;
@class OFDictionary OF_GENERIC(KeyType, ObjectType);
@class OFMutableDictionary OF_GENERIC(KeyType, ObjectType);
@class OFNumber;
@class OFPair OF_GENERIC(FirstType, SecondType);
@class OFTCPSocket;
@class OFUDPSocket;

/**
 * @enum OFDNSResolverErrorCode OFDNSResolver.h ObjFW/OFDNSResolver.h
 *
 * @brief An enum describing why resolving a host failed.
130
131
132
133
134
135
136


137
138
139
140
141
142
143
	OFUDPSocket *_IPv6Socket;
#endif
	char _buffer[OFDNSResolverBufferLength];
	OFMutableDictionary OF_GENERIC(OFNumber *, OFDNSResolverContext *)
	    *_queries;
	OFMutableDictionary OF_GENERIC(OFTCPSocket *, OFDNSResolverContext *)
	    *_TCPQueries;


}

/**
 * @brief A dictionary of static hosts.
 *
 * This dictionary is checked before actually looking up a host.
 */







>
>







131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
	OFUDPSocket *_IPv6Socket;
#endif
	char _buffer[OFDNSResolverBufferLength];
	OFMutableDictionary OF_GENERIC(OFNumber *, OFDNSResolverContext *)
	    *_queries;
	OFMutableDictionary OF_GENERIC(OFTCPSocket *, OFDNSResolverContext *)
	    *_TCPQueries;
	OFMutableDictionary OF_GENERIC(OFDNSQuery *,
	    OFPair OF_GENERIC(OFDate *, OFDNSResponse *) *) *_cache;
}

/**
 * @brief A dictionary of static hosts.
 *
 * This dictionary is checked before actually looking up a host.
 */

Modified src/OFDNSResolver.m from [1f222a13ca] to [8da319df43].

462
463
464
465
466
467
468














469
470
471
472
473
474
475
	while ((array = [objectEnumerator nextObject]) != nil)
		[array makeImmutable];

	[ret makeImmutable];

	return ret;
}















@implementation OFDNSResolverContext
- (instancetype)initWithQuery: (OFDNSQuery *)query
			   ID: (OFNumber *)ID
		     settings: (OFDNSResolverSettings *)settings
		     delegate: (id <OFDNSResolverQueryDelegate>)delegate
{







>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
	while ((array = [objectEnumerator nextObject]) != nil)
		[array makeImmutable];

	[ret makeImmutable];

	return ret;
}

static bool
containsExpiredRecord(OFDNSResponseRecords responseRecords, uint32_t age)
{
	OFEnumerator *enumerator = [responseRecords objectEnumerator];
	OFArray OF_GENERIC(OFDNSResourceRecord *) *records;

	while ((records = [enumerator nextObject]) != nil)
		for (OFDNSResourceRecord *record in records)
			if (record.TTL < age)
				return true;

	return false;
}

@implementation OFDNSResolverContext
- (instancetype)initWithQuery: (OFDNSQuery *)query
			   ID: (OFNumber *)ID
		     settings: (OFDNSResolverSettings *)settings
		     delegate: (id <OFDNSResolverQueryDelegate>)delegate
{
574
575
576
577
578
579
580

581
582
583
584
585
586
587
{
	self = [super init];

	@try {
		_settings = [[OFDNSResolverSettings alloc] init];
		_queries = [[OFMutableDictionary alloc] init];
		_TCPQueries = [[OFMutableDictionary alloc] init];


		[_settings reload];
	} @catch (id e) {
		[self release];
		@throw e;
	}








>







588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
{
	self = [super init];

	@try {
		_settings = [[OFDNSResolverSettings alloc] init];
		_queries = [[OFMutableDictionary alloc] init];
		_TCPQueries = [[OFMutableDictionary alloc] init];
		_cache = [[OFMutableDictionary alloc] init];

		[_settings reload];
	} @catch (id e) {
		[self release];
		@throw e;
	}

597
598
599
600
601
602
603

604
605
606
607
608
609
610
	[_IPv4Socket release];
#ifdef OF_HAVE_IPV6
	[_IPv6Socket cancelAsyncRequests];
	[_IPv6Socket release];
#endif
	[_queries release];
	[_TCPQueries release];


	[super dealloc];
}

- (OFDictionary *)staticHosts
{
	return _settings->_staticHosts;







>







612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
	[_IPv4Socket release];
#ifdef OF_HAVE_IPV6
	[_IPv6Socket cancelAsyncRequests];
	[_IPv6Socket release];
#endif
	[_queries release];
	[_TCPQueries release];
	[_cache release];

	[super dealloc];
}

- (OFDictionary *)staticHosts
{
	return _settings->_staticHosts;
800
801
802
803
804
805
806



















807
808
809
810
811
812
813
- (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)







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
- (void)asyncPerformQuery: (OFDNSQuery *)query
	      runLoopMode: (OFRunLoopMode)runLoopMode
		 delegate: (id <OFDNSResolverQueryDelegate>)delegate
{
	void *pool = objc_autoreleasePoolPush();
	OFNumber *ID;
	OFDNSResolverContext *context;
	OFPair OF_GENERIC(OFDate *, OFDNSResponse *) *cacheEntry;

	if ((cacheEntry = [_cache objectForKey: query]) != nil) {
		uint32_t age =
		    (uint32_t)-[cacheEntry.firstObject timeIntervalSinceNow];
		OFDNSResponse *response = cacheEntry.secondObject;

		if (!containsExpiredRecord(response.answerRecords, age) &&
		    !containsExpiredRecord(response.authorityRecords, age) &&
		    !containsExpiredRecord(response.additionalRecords, age)) {
			[delegate resolver: self
			   didPerformQuery: query
				  response: response
				 exception: nil];

			objc_autoreleasePoolPop(pool);
			return;
		}
	}

	/* Random, unused ID */
	do {
		ID = [OFNumber numberWithUnsignedShort: OFRandom16()];
	} while ([_queries objectForKey: ID] != nil);

	if (query.domainName.UTF8StringLength > 253)
1036
1037
1038
1039
1040
1041
1042







1043
1044
1045
1046
1047
1048
1049
	} @catch (id e) {
		exception = e;
	}

	if (exception != nil)
		response = nil;








	[context->_delegate resolver: self
		     didPerformQuery: context->_query
			    response: response
			   exception: exception];

	return false;
}







>
>
>
>
>
>
>







1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
	} @catch (id e) {
		exception = e;
	}

	if (exception != nil)
		response = nil;

	if (response != nil)
		[_cache setObject: [OFPair pairWithFirstObject: [OFDate date]
						  secondObject: response]
			   forKey: context->_query];
	else
		[_cache removeObjectForKey: context->_query];

	[context->_delegate resolver: self
		     didPerformQuery: context->_query
			    response: response
			   exception: exception];

	return false;
}