ObjFW  Check-in [fcbb1100da]

Overview
Comment:OFDNSResolver: Try all search domains
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: fcbb1100daab7b391e4b20550e8de2485c790d538ebdb919c163ad67c1e43ad7
User & Date: js on 2018-08-15 00:21:51
Other Links: manifest | tags
Context
2018-08-15
21:37
OFDNSResolver: Move settings into a single object check-in: 4162178e50 user: js tags: trunk
00:21
OFDNSResolver: Try all search domains check-in: fcbb1100da user: js tags: trunk
2018-08-14
00:26
Minor documentation fixes check-in: fbfe80e8fa user: js tags: trunk
Changes

Modified src/OFDNSResolver.h from [f06d637d29] to [818844ff98].

74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
{
	OFDictionary OF_GENERIC(OFString *, OFArray OF_GENERIC(OFString *) *)
	    *_staticHosts;
	OFArray OF_GENERIC(OFString *) *_nameServers;
	OFString *_Nullable _localDomain;
	OFArray OF_GENERIC(OFString *) *_searchDomains;
	of_time_interval_t _timeout;
	unsigned int _maxAttempts;
	size_t _minNumberOfDotsInAbsoluteName;
	bool _usesTCP;
	of_time_interval_t _configReloadInterval;
	OFDate *_lastConfigReload;
	OFUDPSocket *_IPv4Socket;
#ifdef OF_HAVE_IPV6
	OFUDPSocket *_IPv6Socket;
#endif







|
<







74
75
76
77
78
79
80
81

82
83
84
85
86
87
88
{
	OFDictionary OF_GENERIC(OFString *, OFArray OF_GENERIC(OFString *) *)
	    *_staticHosts;
	OFArray OF_GENERIC(OFString *) *_nameServers;
	OFString *_Nullable _localDomain;
	OFArray OF_GENERIC(OFString *) *_searchDomains;
	of_time_interval_t _timeout;
	unsigned int _maxAttempts, _minNumberOfDotsInAbsoluteName;

	bool _usesTCP;
	of_time_interval_t _configReloadInterval;
	OFDate *_lastConfigReload;
	OFUDPSocket *_IPv4Socket;
#ifdef OF_HAVE_IPV6
	OFUDPSocket *_IPv6Socket;
#endif
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
 * Trying all name servers once is considered a single attempt.
 */
@property (nonatomic) unsigned int maxAttempts;

/*!
 * @brief The minimum number of dots for a name to be considered absolute.
 */
@property (nonatomic) size_t minNumberOfDotsInAbsoluteName;

/*!
 * @brief Whether the resolver uses TCP to talk to a name server.
 */
@property (nonatomic) bool usesTCP;

/*!







|







127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
 * Trying all name servers once is considered a single attempt.
 */
@property (nonatomic) unsigned int maxAttempts;

/*!
 * @brief The minimum number of dots for a name to be considered absolute.
 */
@property (nonatomic) unsigned int minNumberOfDotsInAbsoluteName;

/*!
 * @brief Whether the resolver uses TCP to talk to a name server.
 */
@property (nonatomic) bool usesTCP;

/*!

Modified src/OFDNSResolver.m from [24a25534b2] to [c86401e34e].

105
106
107
108
109
110
111

112

113
114
115
116
117
118
119

- (instancetype)initWithHost: (OFString *)host
		  domainName: (OFString *)domainName
		 recordClass: (of_dns_resource_record_class_t)recordClass
		  recordType: (of_dns_resource_record_type_t)recordType
			  ID: (OFNumber *)ID
		 nameServers: (OFArray OF_GENERIC(OFString *) *)nameServers

	       searchDomains: (OFArray OF_GENERIC(OFString *) *)searchDomains

		     timeout: (of_time_interval_t)timeout
		 maxAttempts: (unsigned int)maxAttempts
		      target: (id)target
		    selector: (SEL)selector
		     context: (id)context;
@end








>

>







105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121

- (instancetype)initWithHost: (OFString *)host
		  domainName: (OFString *)domainName
		 recordClass: (of_dns_resource_record_class_t)recordClass
		  recordType: (of_dns_resource_record_type_t)recordType
			  ID: (OFNumber *)ID
		 nameServers: (OFArray OF_GENERIC(OFString *) *)nameServers
	    nameServersIndex: (size_t)nameServersIndex
	       searchDomains: (OFArray OF_GENERIC(OFString *) *)searchDomains
	  searchDomainsIndex: (size_t)searchDomainsIndex
		     timeout: (of_time_interval_t)timeout
		 maxAttempts: (unsigned int)maxAttempts
		      target: (id)target
		    selector: (SEL)selector
		     context: (id)context;
@end

127
128
129
130
131
132
133












134
135
136
137
138
139
140
- (void)of_parseResolvConfOption: (OFString *)option;
# endif
#endif
#ifdef OF_WINDOWS
- (void)of_parseNetworkParams;
#endif
- (void)of_reloadConfig;












- (void)of_sendQuery: (OFDNSResolverQuery *)query;
- (void)of_queryWithIDTimedOut: (OFDNSResolverQuery *)query;
- (size_t)of_socket: (OFUDPSocket *)sock
      didSendBuffer: (void **)buffer
	  bytesSent: (size_t)bytesSent
	   receiver: (of_socket_address_t *)receiver
	    context: (OFDNSResolverQuery *)query







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







129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
- (void)of_parseResolvConfOption: (OFString *)option;
# endif
#endif
#ifdef OF_WINDOWS
- (void)of_parseNetworkParams;
#endif
- (void)of_reloadConfig;
- (void)of_resolveHost: (OFString *)host
	   recordClass: (of_dns_resource_record_class_t)recordClass
	    recordType: (of_dns_resource_record_type_t)recordType
	   nameServers: (OFArray OF_GENERIC(OFString *) *)nameServers
      nameServersIndex: (size_t)nameServersIndex
	 searchDomains: (OFArray OF_GENERIC(OFString *) *)searchDomains
    searchDomainsIndex: (size_t)searchDomainsIndex
	       timeout: (unsigned int)timeout
	   maxAttempts: (unsigned int)maxAttempts
		target: (id)target
	      selector: (SEL)selector
	       context: (id)context;
- (void)of_sendQuery: (OFDNSResolverQuery *)query;
- (void)of_queryWithIDTimedOut: (OFDNSResolverQuery *)query;
- (size_t)of_socket: (OFUDPSocket *)sock
      didSendBuffer: (void **)buffer
	  bytesSent: (size_t)bytesSent
	   receiver: (of_socket_address_t *)receiver
	    context: (OFDNSResolverQuery *)query
158
159
160
161
162
163
164

















165
166
167
168
169
170
171

	if ((domain = strchr(hostname, '.')) == NULL)
		return nil;

	return [OFString stringWithCString: domain + 1
				  encoding: [OFLocale encoding]];
}


















static OFString *
parseString(const unsigned char *buffer, size_t length, size_t *i)
{
	uint8_t stringLength;
	OFString *string;








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







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

	if ((domain = strchr(hostname, '.')) == NULL)
		return nil;

	return [OFString stringWithCString: domain + 1
				  encoding: [OFLocale encoding]];
}

static bool
isFQDN(OFString *host, unsigned int minNumberOfDotsInAbsoluteName)
{
	const char *UTF8String = [host UTF8String];
	size_t length = [host UTF8StringLength];
	unsigned int dots = 0;

	if ([host hasSuffix: @"."])
		return true;

	for (size_t i = 0; i < length; i++)
		if (UTF8String[i] == '.')
			dots++;

	return (dots >= minNumberOfDotsInAbsoluteName);
}

static OFString *
parseString(const unsigned char *buffer, size_t length, size_t *i)
{
	uint8_t stringLength;
	OFString *string;

530
531
532
533
534
535
536

537

538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556

557

558
559
560
561
562
563
564
@implementation OFDNSResolverQuery
- (instancetype)initWithHost: (OFString *)host
		  domainName: (OFString *)domainName
		 recordClass: (of_dns_resource_record_class_t)recordClass
		  recordType: (of_dns_resource_record_type_t)recordType
			  ID: (OFNumber *)ID
		 nameServers: (OFArray OF_GENERIC(OFString *) *)nameServers

	       searchDomains: (OFArray OF_GENERIC(OFString *) *)searchDomains

		     timeout: (of_time_interval_t)timeout
		 maxAttempts: (unsigned int)maxAttempts
		      target: (id)target
		    selector: (SEL)selector
		     context: (id)context
{
	self = [super init];

	@try {
		void *pool = objc_autoreleasePoolPush();
		OFMutableData *queryData;
		uint16_t tmp;

		_host = [host copy];
		_domainName = [domainName copy];
		_recordClass = recordClass;
		_recordType = recordType;
		_ID = [ID retain];
		_nameServers = [nameServers copy];

		_searchDomains = [searchDomains copy];

		_timeout = timeout;
		_maxAttempts = maxAttempts;
		_target = [target retain];
		_selector = selector;
		_context = [context retain];

		queryData = [OFMutableData dataWithCapacity: 512];







>

>



















>

>







561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
@implementation OFDNSResolverQuery
- (instancetype)initWithHost: (OFString *)host
		  domainName: (OFString *)domainName
		 recordClass: (of_dns_resource_record_class_t)recordClass
		  recordType: (of_dns_resource_record_type_t)recordType
			  ID: (OFNumber *)ID
		 nameServers: (OFArray OF_GENERIC(OFString *) *)nameServers
	    nameServersIndex: (size_t)nameServersIndex
	       searchDomains: (OFArray OF_GENERIC(OFString *) *)searchDomains
	  searchDomainsIndex: (size_t)searchDomainsIndex
		     timeout: (of_time_interval_t)timeout
		 maxAttempts: (unsigned int)maxAttempts
		      target: (id)target
		    selector: (SEL)selector
		     context: (id)context
{
	self = [super init];

	@try {
		void *pool = objc_autoreleasePoolPush();
		OFMutableData *queryData;
		uint16_t tmp;

		_host = [host copy];
		_domainName = [domainName copy];
		_recordClass = recordClass;
		_recordType = recordType;
		_ID = [ID retain];
		_nameServers = [nameServers copy];
		_nameServersIndex = nameServersIndex;
		_searchDomains = [searchDomains copy];
		_searchDomainsIndex = searchDomainsIndex;
		_timeout = timeout;
		_maxAttempts = maxAttempts;
		_target = [target retain];
		_selector = selector;
		_context = [context retain];

		queryData = [OFMutableData dataWithCapacity: 512];
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
{
	@try {
		if ([option hasPrefix: @"ndots:"]) {
			option = [option substringWithRange:
			    of_range(6, [option length] - 6)];

			_minNumberOfDotsInAbsoluteName =
			    (size_t)[option decimalValue];
		} else if ([option hasPrefix: @"timeout:"]) {
			option = [option substringWithRange:
			    of_range(8, [option length] - 8)];

			_timeout = [option decimalValue];
		} else if ([option hasPrefix: @"attempts:"]) {
			option = [option substringWithRange:







|







951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
{
	@try {
		if ([option hasPrefix: @"ndots:"]) {
			option = [option substringWithRange:
			    of_range(6, [option length] - 6)];

			_minNumberOfDotsInAbsoluteName =
			    (unsigned int)[option decimalValue];
		} else if ([option hasPrefix: @"timeout:"]) {
			option = [option substringWithRange:
			    of_range(8, [option length] - 8)];

			_timeout = [option decimalValue];
		} else if ([option hasPrefix: @"attempts:"]) {
			option = [option substringWithRange:
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
		   recordClass: OF_DNS_RESOURCE_RECORD_CLASS_IN
		    recordType: OF_DNS_RESOURCE_RECORD_TYPE_ALL
			target: target
		      selector: selector
		       context: context];
}

- (void)asyncResolveHost: (OFString *)host
	     recordClass: (of_dns_resource_record_class_t)recordClass
	      recordType: (of_dns_resource_record_type_t)recordType






		  target: (id)target
		selector: (SEL)selector
		 context: (id)context
{
	void *pool = objc_autoreleasePoolPush();
	OFNumber *ID;
	OFString *domainName;
	OFDNSResolverQuery *query;

	[self of_reloadConfig];

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

	if ([host hasSuffix: @"."])
		domainName = host;
	else
		/* TODO: Properly try all search domains */
		domainName = [host stringByAppendingString: @"."];


	if ([domainName UTF8StringLength] > 253)
		@throw [OFOutOfRangeException exception];

	query = [[[OFDNSResolverQuery alloc]
	    initWithHost: host
	      domainName: domainName
	     recordClass: recordClass
	      recordType: recordType
		      ID: ID
	     nameServers: _nameServers

	   searchDomains: _searchDomains

		 timeout: _timeout
	     maxAttempts: _maxAttempts
		  target: target
		selector: selector
		 context: context] autorelease];
	[_queries setObject: query
		     forKey: ID];

	[self of_sendQuery: query];

	objc_autoreleasePoolPop(pool);
}






















- (void)of_sendQuery: (OFDNSResolverQuery *)query
{
	OFUDPSocket *sock;

	[query->_cancelTimer invalidate];
	[query->_cancelTimer release];







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













|


<
|
>





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







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







1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093

1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
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
		   recordClass: OF_DNS_RESOURCE_RECORD_CLASS_IN
		    recordType: OF_DNS_RESOURCE_RECORD_TYPE_ALL
			target: target
		      selector: selector
		       context: context];
}

- (void)of_resolveHost: (OFString *)host
	   recordClass: (of_dns_resource_record_class_t)recordClass
	    recordType: (of_dns_resource_record_type_t)recordType
	   nameServers: (OFArray OF_GENERIC(OFString *) *)nameServers
      nameServersIndex: (size_t)nameServersIndex
	 searchDomains: (OFArray OF_GENERIC(OFString *) *)searchDomains
    searchDomainsIndex: (size_t)searchDomainsIndex
	       timeout: (unsigned int)timeout
	   maxAttempts: (unsigned int)maxAttempts
		target: (id)target
	      selector: (SEL)selector
	       context: (id)context
{
	void *pool = objc_autoreleasePoolPush();
	OFNumber *ID;
	OFString *domainName;
	OFDNSResolverQuery *query;

	[self of_reloadConfig];

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

	if (isFQDN(host, _minNumberOfDotsInAbsoluteName))
		domainName = host;
	else

		domainName = [OFString stringWithFormat: @"%@.%@.",
		    host, [searchDomains objectAtIndex: searchDomainsIndex]];

	if ([domainName UTF8StringLength] > 253)
		@throw [OFOutOfRangeException exception];

	query = [[[OFDNSResolverQuery alloc]
		  initWithHost: host
		    domainName: domainName
		   recordClass: recordClass
		    recordType: recordType
			    ID: ID
		   nameServers: _nameServers
	      nameServersIndex: nameServersIndex
		 searchDomains: _searchDomains
	    searchDomainsIndex: searchDomainsIndex
		       timeout: _timeout
		   maxAttempts: _maxAttempts
			target: target
		      selector: selector
		       context: context] autorelease];
	[_queries setObject: query
		     forKey: ID];

	[self of_sendQuery: query];

	objc_autoreleasePoolPop(pool);
}

- (void)asyncResolveHost: (OFString *)host
	     recordClass: (of_dns_resource_record_class_t)recordClass
	      recordType: (of_dns_resource_record_type_t)recordType
		  target: (id)target
		selector: (SEL)selector
		 context: (id)context
{
	[self of_resolveHost: host
		 recordClass: recordClass
		  recordType: recordType
		 nameServers: _nameServers
	    nameServersIndex: 0
	       searchDomains: _searchDomains
	  searchDomainsIndex: 0
		     timeout: _timeout
		 maxAttempts: _maxAttempts
		      target: target
		    selector: selector
		     context: context];
}

- (void)of_sendQuery: (OFDNSResolverQuery *)query
{
	OFUDPSocket *sock;

	[query->_cancelTimer invalidate];
	[query->_cancelTimer release];
1263
1264
1265
1266
1267
1268
1269




















1270
1271
1272
1273
1274
1275
1276
		case 1:
			error = OF_DNS_RESOLVER_ERROR_SERVER_INVALID_FORMAT;
			break;
		case 2:
			error = OF_DNS_RESOLVER_ERROR_SERVER_FAILURE;
			break;
		case 3:




















			error = OF_DNS_RESOLVER_ERROR_SERVER_NAME_ERROR;
			break;
		case 4:
			error = OF_DNS_RESOLVER_ERROR_SERVER_NOT_IMPLEMENTED;
			break;
		case 5:
			error = OF_DNS_RESOLVER_ERROR_SERVER_REFUSED;







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







1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
		case 1:
			error = OF_DNS_RESOLVER_ERROR_SERVER_INVALID_FORMAT;
			break;
		case 2:
			error = OF_DNS_RESOLVER_ERROR_SERVER_FAILURE;
			break;
		case 3:
			if (query->_searchDomainsIndex + 1 <
			    [query->_searchDomains count]) {
				query->_searchDomainsIndex++;

				[self of_resolveHost: query->_host
					 recordClass: query->_recordClass
					  recordType: query->_recordType
					 nameServers: query->_nameServers
				    nameServersIndex: query->_nameServersIndex
				       searchDomains: query->_searchDomains
				  searchDomainsIndex: query->_searchDomainsIndex
					     timeout: query->_timeout
					 maxAttempts: query->_maxAttempts
					      target: query->_target
					    selector: query->_selector
					     context: query->_context];

				return false;
			}

			error = OF_DNS_RESOLVER_ERROR_SERVER_NAME_ERROR;
			break;
		case 4:
			error = OF_DNS_RESOLVER_ERROR_SERVER_NOT_IMPLEMENTED;
			break;
		case 5:
			error = OF_DNS_RESOLVER_ERROR_SERVER_REFUSED;