ObjFW  Check-in [ae92690f92]

Overview
Comment:OFDNSResolver: Send queries for unresolved CNAMEs
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: ae92690f927b7e9164342aa5e6e3c597e4321b5f2bab4fae9cf0461e0c81085e
User & Date: js on 2018-09-15 23:35:14
Other Links: manifest | tags
Context
2018-09-16
12:49
OFTCPSocket: Async connect without threads check-in: 6638379972 user: js tags: trunk
2018-09-15
23:35
OFDNSResolver: Send queries for unresolved CNAMEs check-in: ae92690f92 user: js tags: trunk
20:04
OFDNSResolver: Fix compilation with GCC check-in: c4836192b2 user: js tags: trunk
Changes

Modified src/OFDNSResolver.m from [c811322a6c] to [fc6f9bf825].

26
27
28
29
30
31
32

33
34
35
36
37
38
39
#import "OFCharacterSet.h"
#import "OFData.h"
#import "OFDate.h"
#import "OFDictionary.h"
#import "OFFile.h"
#import "OFLocale.h"
#import "OFNumber.h"

#import "OFString.h"
#import "OFTimer.h"
#import "OFUDPSocket.h"
#ifdef OF_WINDOWS
# import "OFWindowsRegistryKey.h"
#endif








>







26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#import "OFCharacterSet.h"
#import "OFData.h"
#import "OFDate.h"
#import "OFDictionary.h"
#import "OFFile.h"
#import "OFLocale.h"
#import "OFNumber.h"
#import "OFPair.h"
#import "OFString.h"
#import "OFTimer.h"
#import "OFUDPSocket.h"
#ifdef OF_WINDOWS
# import "OFWindowsRegistryKey.h"
#endif

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
@interface OFDNSResolver_ResolveSocketAddressContext: OFObject
{
	OFString *_host;
	id _target;
	SEL _selector;
	id _context;
	OFMutableArray OF_GENERIC(OF_KINDOF(OFDNSResourceRecord *)) *_records;


@public
	unsigned int _expectedResponses;
}

- (instancetype)initWithHost: (OFString *)host
		      target: (id)target
		    selector: (SEL)selector
		     context: (id)context;
- (bool)parseRecords: (OFArray *)records
	    resolver: (OFDNSResolver *)resolver
       answerRecords: (OFDictionary *)answerRecords
   additionalRecords: (OFDictionary *)additionalRecords
	  recordType: (of_dns_resource_record_type_t)recordType
	   recursion: (unsigned int)recursion;

- (void)resolveCNAME: (OFCNAMEDNSResourceRecord *)CNAME
	    resolver: (OFDNSResolver *)resolver
       answerRecords: (OFDictionary *)answerRecords
   additionalRecords: (OFDictionary *)additionalRecords
	  recordType: (of_dns_resource_record_type_t)recordType
	   recursion: (unsigned int)recursion;
- (void)doneWithDomainName: (OFString *)domainName
		  resolver: (OFDNSResolver *)resolver;







-	(void)resolver: (OFDNSResolver *)resolver
  didResolveDomainName: (OFString *)domainName
	 answerRecords: (OFDictionary *)answerRecords
      authorityRecords: (OFDictionary *)authorityRecords
     additionalRecords: (OFDictionary *)additionalRecords
	       context: (id)context
	     exception: (id)exception;







>
>









<



|
>

<



|
|
|
>
>
>
>
>
>
>







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
@interface OFDNSResolver_ResolveSocketAddressContext: OFObject
{
	OFString *_host;
	id _target;
	SEL _selector;
	id _context;
	OFMutableArray OF_GENERIC(OF_KINDOF(OFDNSResourceRecord *)) *_records;
	OFDNSResolver *_resolver;
	OFString *_domainName;
@public
	unsigned int _expectedResponses;
}

- (instancetype)initWithHost: (OFString *)host
		      target: (id)target
		    selector: (SEL)selector
		     context: (id)context;
- (bool)parseRecords: (OFArray *)records

       answerRecords: (OFDictionary *)answerRecords
   additionalRecords: (OFDictionary *)additionalRecords
	  recordType: (of_dns_resource_record_type_t)recordType
	   recursion: (unsigned int)recursion
	      result: (OFMutableArray *)result;
- (void)resolveCNAME: (OFCNAMEDNSResourceRecord *)CNAME

       answerRecords: (OFDictionary *)answerRecords
   additionalRecords: (OFDictionary *)additionalRecords
	  recordType: (of_dns_resource_record_type_t)recordType
	   recursion: (unsigned int)recursion
	      result: (OFMutableArray *)result;
-    (void)resolver: (OFDNSResolver *)resolver
    didResolveCNAME: (OFString *)CNAME
      answerRecords: (OFDictionary *)answerRecords
   authorityRecords: (OFDictionary *)authorityRecords
  additionalRecords: (OFDictionary *)additionalRecords
	    context: (id)context
	  exception: (id)exception;
- (void)done;
-	(void)resolver: (OFDNSResolver *)resolver
  didResolveDomainName: (OFString *)domainName
	 answerRecords: (OFDictionary *)answerRecords
      authorityRecords: (OFDictionary *)authorityRecords
     additionalRecords: (OFDictionary *)additionalRecords
	       context: (id)context
	     exception: (id)exception;
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


910



911


912


913


914




915
916



917

918
919
920
921
922

923
924
925
926
927
928
929


930



931


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

- (void)dealloc
{
	[_host release];
	[_target release];
	[_context release];
	[_records release];



	[super dealloc];
}

- (bool)parseRecords: (OFArray *)records
	    resolver: (OFDNSResolver *)resolver
       answerRecords: (OFDictionary *)answerRecords
   additionalRecords: (OFDictionary *)additionalRecords
	  recordType: (of_dns_resource_record_type_t)recordType
	   recursion: (unsigned int)recursion

{
	bool found = false;

	for (OF_KINDOF(OFDNSResourceRecord *) record in records) {
		if ([record recordClass] != OF_DNS_RESOURCE_RECORD_CLASS_IN)
			continue;

		if ([record recordType] == recordType) {
			[_records addObject: record];
			found = true;
		} else if ([record recordType] ==
		    OF_DNS_RESOURCE_RECORD_TYPE_CNAME) {
			[self	resolveCNAME: record
				    resolver: resolver
			       answerRecords: answerRecords
			   additionalRecords: additionalRecords
				  recordType: recordType
				   recursion: recursion];

			found = true;
		}
	}

	return found;
}

- (void)resolveCNAME: (OFCNAMEDNSResourceRecord *)CNAME
	    resolver: (OFDNSResolver *)resolver
       answerRecords: (OFDictionary *)answerRecords
   additionalRecords: (OFDictionary *)additionalRecords
	  recordType: (of_dns_resource_record_type_t)recordType
	   recursion: (unsigned int)recursion

{
	OFString *domainName = [CNAME alias];
	bool found = false;

	if (recursion == 0)
		return;

	if ([self parseRecords: [answerRecords objectForKey: domainName]
		      resolver: resolver
		 answerRecords: answerRecords
	     additionalRecords: additionalRecords
		    recordType: recordType
		     recursion: recursion - 1])

		found = true;

	if ([self parseRecords: [additionalRecords objectForKey: domainName]
		      resolver: resolver
		 answerRecords: answerRecords
	     additionalRecords: additionalRecords
		    recordType: recordType
		     recursion: recursion - 1])

		found = true;






































































	if (!found) {


		/*


		 * TODO: Send request, add CNAME to _records and replace once



		 *	 resolved.


		 */


	}


}





- (void)doneWithDomainName: (OFString *)domainName



		  resolver: (OFDNSResolver *)resolver

{
	void (*method)(id, SEL, OFDNSResolver *, OFString *, OFData *, id, id) =
	    (void (*)(id, SEL, OFDNSResolver *, OFString *, OFData *, id, id))
	    [_target methodForSelector: _selector];
	OFMutableData *addresses = nil;


	if ([_records count] > 0) {
		addresses = [OFMutableData
		    dataWithItemSize: sizeof(of_socket_address_t)
			    capacity: [_records count]];

		for (OF_KINDOF(OFDNSResourceRecord *) record in _records)


			[addresses addItem: [record address]];






		[addresses makeImmutable];


		method(_target, _selector, resolver, domainName, addresses,
		    _context, nil);
	} else {
		OFResolveHostFailedException *e;

		e = [OFResolveHostFailedException
		    exceptionWithHost: _host
			  recordClass: OF_DNS_RESOURCE_RECORD_CLASS_IN
			   recordType: 0
				error: OF_DNS_RESOLVER_ERROR_UNKNOWN];

		method(_target, _selector, resolver, domainName, nil, _context,
		    e);
	}
}

-	(void)resolver: (OFDNSResolver *)resolver
  didResolveDomainName: (OFString *)domainName
	 answerRecords: (OFDictionary *)answerRecords
      authorityRecords: (OFDictionary *)authorityRecords
     additionalRecords: (OFDictionary *)additionalRecords
	       context: (id)context
	     exception: (id)exception
{
	/*
	 * TODO: Error handling could be improved. Ignore error if there are
	 * responses, otherwise propagate error.
	 */

	of_dns_resource_record_type_t recordType = [context intValue];






	_expectedResponses--;








	if (exception != nil) {
		if (_expectedResponses == 0)
			[self doneWithDomainName: domainName
					resolver: resolver];

		return;
	}

	[self parseRecords: [answerRecords objectForKey: domainName]
		  resolver: resolver
	     answerRecords: answerRecords
	 additionalRecords: additionalRecords
		recordType: recordType
		 recursion: CNAME_RECURSION];


	if (_expectedResponses == 0)
		[self doneWithDomainName: domainName
				resolver: resolver];
}
@end

@implementation OFDNSResolver
@synthesize staticHosts = _staticHosts, nameServers = _nameServers;
@synthesize localDomain = _localDomain, searchDomains = _searchDomains;
@synthesize timeout = _timeout, maxAttempts = _maxAttempts;







>
>





<




>








|




<



|
>








<




>

|





|
<



|
>


|
<



|
>

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

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

>
>
|
>
>
|
>
>
>
|
>
>
|
>
>
|
>
>
|
>
>
>
>
|
|
>
>
>
|
>




|
>

|
<
|
|

|
>
>

>
>
>
|
>
>
|

>
|

|








|
|
















|
>
>
>
>
>



>
>
>
>
>
>
>


|
<




|
<



|
>


|
<







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
910
911

912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
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
1020
1021
1022
1023
1024
1025
1026

1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
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
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

- (void)dealloc
{
	[_host release];
	[_target release];
	[_context release];
	[_records release];
	[_resolver release];
	[_domainName release];

	[super dealloc];
}

- (bool)parseRecords: (OFArray *)records

       answerRecords: (OFDictionary *)answerRecords
   additionalRecords: (OFDictionary *)additionalRecords
	  recordType: (of_dns_resource_record_type_t)recordType
	   recursion: (unsigned int)recursion
	      result: (OFMutableArray *)result
{
	bool found = false;

	for (OF_KINDOF(OFDNSResourceRecord *) record in records) {
		if ([record recordClass] != OF_DNS_RESOURCE_RECORD_CLASS_IN)
			continue;

		if ([record recordType] == recordType) {
			[result addObject: record];
			found = true;
		} else if ([record recordType] ==
		    OF_DNS_RESOURCE_RECORD_TYPE_CNAME) {
			[self	resolveCNAME: record

			       answerRecords: answerRecords
			   additionalRecords: additionalRecords
				  recordType: recordType
				   recursion: recursion
				      result: result];
			found = true;
		}
	}

	return found;
}

- (void)resolveCNAME: (OFCNAMEDNSResourceRecord *)CNAME

       answerRecords: (OFDictionary *)answerRecords
   additionalRecords: (OFDictionary *)additionalRecords
	  recordType: (of_dns_resource_record_type_t)recordType
	   recursion: (unsigned int)recursion
	      result: (OFMutableArray *)result
{
	OFString *alias = [CNAME alias];
	bool found = false;

	if (recursion == 0)
		return;

	if ([self parseRecords: [answerRecords objectForKey: alias]

		 answerRecords: answerRecords
	     additionalRecords: additionalRecords
		    recordType: recordType
		     recursion: recursion - 1
			result: result])
		found = true;

	if ([self parseRecords: [additionalRecords objectForKey: alias]

		 answerRecords: answerRecords
	     additionalRecords: additionalRecords
		    recordType: recordType
		     recursion: recursion - 1
			result: result])
		found = true;

	if (!found) {
		OFNumber *recordTypeNumber =
		    [OFNumber numberWithInt: recordType];
		_expectedResponses++;

		[result addObject:
		    [OFPair pairWithFirstObject: CNAME
				   secondObject: recordTypeNumber]];

		[_resolver asyncResolveHost: alias
				recordClass: OF_DNS_RESOURCE_RECORD_CLASS_IN
				 recordType: recordType
				     target: self
				   selector: @selector(resolver:
						 didResolveCNAME:
						 answerRecords:authorityRecords:
						 additionalRecords:context:
						 exception:)
				    context: recordTypeNumber];
	}
}

-    (void)resolver: (OFDNSResolver *)resolver
    didResolveCNAME: (OFString *)CNAME
      answerRecords: (OFDictionary *)answerRecords
   authorityRecords: (OFDictionary *)authorityRecords
  additionalRecords: (OFDictionary *)additionalRecords
	    context: (id)context
	  exception: (id)exception
{
	/*
	 * TODO: Error handling could be improved. Ignore error if there are
	 * responses, otherwise propagate error.
	 */

	of_dns_resource_record_type_t recordType = [context unsignedIntValue];
	bool found = false;
	OFMutableArray *records;
	size_t count;

	OF_ENSURE(resolver == _resolver);

	_expectedResponses--;

	if (exception != nil) {
		if (_expectedResponses == 0)
			[self done];

		return;
	}

	records = [OFMutableArray array];

	if ([self parseRecords: [answerRecords objectForKey: CNAME]
		 answerRecords: answerRecords
	     additionalRecords: additionalRecords
		    recordType: recordType
		     recursion: CNAME_RECURSION
			result: records])
		found = true;

	if ([self parseRecords: [additionalRecords objectForKey: CNAME]
		 answerRecords: answerRecords
	     additionalRecords: additionalRecords
		    recordType: recordType
		     recursion: CNAME_RECURSION
			result: records])
		found = true;

	if (!found) {
		if (_expectedResponses == 0)
			[self done];

		return;
	}

	count = [_records count];
	for (size_t i = 0; i < count; i++) {
		id object = [_records objectAtIndex: i];

		if (![object isKindOfClass: [OFPair class]])
			continue;

		if (![[[object firstObject] alias] isEqual: CNAME])
			continue;

		if ([[object secondObject] unsignedIntValue] != recordType)
			continue;

		[_records removeObjectAtIndex: i];
		[_records insertObjectsFromArray: records
					 atIndex: i];
		i += [records count] - 1;
	}

	if (_expectedResponses == 0)
		[self done];
}

- (void)done
{
	void (*method)(id, SEL, OFDNSResolver *, OFString *, OFData *, id, id) =
	    (void (*)(id, SEL, OFDNSResolver *, OFString *, OFData *, id, id))
	    [_target methodForSelector: _selector];
	OFMutableData *addresses =
	    [OFMutableData dataWithItemSize: sizeof(of_socket_address_t)];

	for (id record in _records) {

		if (![record isKindOfClass: [OFDNSResourceRecord class]])
			continue;

		switch ([record recordType]) {
		case OF_DNS_RESOURCE_RECORD_TYPE_A:
		case OF_DNS_RESOURCE_RECORD_TYPE_AAAA:
			[addresses addItem: [record address]];
			break;
		default:
			break;
		}
	}

	[addresses makeImmutable];

	if ([addresses count] > 0)
		method(_target, _selector, _resolver, _domainName, addresses,
		    _context, nil);
	else {
		OFResolveHostFailedException *e;

		e = [OFResolveHostFailedException
		    exceptionWithHost: _host
			  recordClass: OF_DNS_RESOURCE_RECORD_CLASS_IN
			   recordType: 0
				error: OF_DNS_RESOLVER_ERROR_UNKNOWN];

		method(_target, _selector, _resolver, _domainName, nil,
		    _context, e);
	}
}

-	(void)resolver: (OFDNSResolver *)resolver
  didResolveDomainName: (OFString *)domainName
	 answerRecords: (OFDictionary *)answerRecords
      authorityRecords: (OFDictionary *)authorityRecords
     additionalRecords: (OFDictionary *)additionalRecords
	       context: (id)context
	     exception: (id)exception
{
	/*
	 * TODO: Error handling could be improved. Ignore error if there are
	 * responses, otherwise propagate error.
	 */

	of_dns_resource_record_type_t recordType = [context unsignedIntValue];

	if (_resolver != nil)
		OF_ENSURE(resolver == _resolver);
	else
		_resolver = [resolver retain];

	_expectedResponses--;

	if (_domainName != nil) {
		if (![domainName isEqual: _domainName])
			/* Did the config change between requests? */
			return;
	} else
		_domainName = [domainName copy];

	if (exception != nil) {
		if (_expectedResponses == 0)
			[self done];


		return;
	}

	[self parseRecords: [answerRecords objectForKey: _domainName]

	     answerRecords: answerRecords
	 additionalRecords: additionalRecords
		recordType: recordType
		 recursion: CNAME_RECURSION
		    result: _records];

	if (_expectedResponses == 0)
		[self done];

}
@end

@implementation OFDNSResolver
@synthesize staticHosts = _staticHosts, nameServers = _nameServers;
@synthesize localDomain = _localDomain, searchDomains = _searchDomains;
@synthesize timeout = _timeout, maxAttempts = _maxAttempts;