Overview
Comment: | OFDNSResolver: Parse authority and additional RRs |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
6305a0c7a41d93bb674c7ffac83ed777 |
User & Date: | js on 2018-08-12 12:37:39 |
Other Links: | manifest | tags |
Context
2018-08-12
| ||
22:21 | OFDNSResolver: Pass the FQDN to the callback check-in: 457f722d18 user: js tags: trunk | |
12:37 | OFDNSResolver: Parse authority and additional RRs check-in: 6305a0c7a4 user: js tags: trunk | |
01:12 | OFDNSResolver: Multiple attempts for resolving check-in: 42cb3d287c user: js tags: trunk | |
Changes
Modified src/OFDNSResolver.h from [878890e8bc] to [015a85d33b].
︙ | ︙ | |||
128 129 130 131 132 133 134 | /*! * @brief Asynchronously resolves the specified host. * * @param host The host to resolve * @param target The target to call with the result once resolving is done * @param selector The selector to call on the target. The signature must be | > | > > | | 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | /*! * @brief Asynchronously resolves the specified host. * * @param host The host to resolve * @param target The target to call with the result once resolving is done * @param selector The selector to call on the target. The signature must be * `void (OFDNSResolver *resolver, * nullable OFArray<OFDNSResourceRecord *> *answerRecords, * nullable OFArray<OFDNSResourceRecord *> *authorityRecords, * nullable OFArray<OFDNSResourceRecord *> *additionalRecords, * nullable id context, nullable id exception)`. * @param context A context object to pass along to the target */ - (void)asyncResolveHost: (OFString *)host target: (id)target selector: (SEL)selector context: (nullable id)context; |
︙ | ︙ |
Modified src/OFDNSResolver.m from [f296db2c83] to [3362d8ffb1].
︙ | ︙ | |||
148 149 150 151 152 153 154 | return nil; return [OFString stringWithCString: domain + 1 encoding: [OFLocale encoding]]; } static OFString * | | < | | | | | < < | < | | | | < | | | | < < | | 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | 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; if (*i >= length) @throw [OFTruncatedDataException exception]; stringLength = buffer[(*i)++]; if (*i + stringLength > length) @throw [OFTruncatedDataException exception]; string = [OFString stringWithUTF8String: (char *)&buffer[*i] length: stringLength]; *i += stringLength; return string; } static OFString * parseName(const unsigned char *buffer, size_t length, size_t *i, uint_fast8_t pointerLevel) { OFMutableArray *components = [OFMutableArray array]; uint8_t componentLength; do { OFString *component; if (*i >= length) @throw [OFTruncatedDataException exception]; componentLength = buffer[(*i)++]; if (componentLength & 0xC0) { size_t j; OFString *suffix; if (pointerLevel == 0) @throw [OFInvalidServerReplyException exception]; if (*i >= length) @throw [OFTruncatedDataException exception]; j = ((componentLength & 0x3F) << 8) | buffer[(*i)++]; if (j == *i - 2) /* Pointing to itself?! */ @throw [OFInvalidServerReplyException exception]; suffix = parseName(buffer, length, &j, pointerLevel - 1); if ([components count] == 0) return suffix; else { [components addObject: suffix]; return [components componentsJoinedByString: @"."]; } } if (*i + componentLength > length) @throw [OFTruncatedDataException exception]; component = [OFString stringWithUTF8String: (char *)&buffer[*i] length: componentLength]; *i += componentLength; [components addObject: component]; } while (componentLength > 0); return [components componentsJoinedByString: @"."]; } static OF_KINDOF(OFDNSResourceRecord *) parseResourceRecord(OFString *name, of_dns_resource_record_class_t recordClass, of_dns_resource_record_type_t recordType, uint32_t TTL, const unsigned char *buffer, size_t length, size_t i, uint16_t dataLength) { if (recordType == OF_DNS_RESOURCE_RECORD_TYPE_A && recordClass == OF_DNS_RESOURCE_RECORD_CLASS_IN) { of_socket_address_t address; |
︙ | ︙ | |||
463 464 465 466 467 468 469 470 471 472 473 474 475 476 | } else return [[[OFDNSResourceRecord alloc] initWithName: name recordClass: recordClass recordType: recordType TTL: TTL] autorelease]; } @implementation OFDNSResolverQuery @synthesize host = _host, recordClass = _recordClass, recordType = _recordType; @synthesize ID = _ID, nameServers = _nameServers; @synthesize searchDomains = _searchDomains; @synthesize nameServersIndex = _nameServersIndex; @synthesize searchDomainsIndex = _searchDomainsIndex, attempt = _attempt; | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 456 457 458 459 460 461 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 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 515 516 517 518 519 520 521 522 523 | } else return [[[OFDNSResourceRecord alloc] initWithName: name recordClass: recordClass recordType: recordType TTL: TTL] autorelease]; } static OFArray * parseSection(const unsigned char *buffer, size_t length, size_t *i, uint_fast16_t count) { OFMutableArray *ret = [OFMutableArray array]; for (uint_fast16_t j = 0; j < count; j++) { OFString *name = parseName(buffer, length, i, MAX_ALLOWED_POINTERS); of_dns_resource_record_class_t recordClass; of_dns_resource_record_type_t recordType; uint32_t TTL; uint16_t dataLength; OFDNSResourceRecord *record; if (*i + 10 > length) @throw [OFTruncatedDataException exception]; recordType = (buffer[*i] << 16) | buffer[*i + 1]; recordClass = (buffer[*i + 2] << 16) | buffer[*i + 3]; TTL = (buffer[*i + 4] << 24) | (buffer[*i + 5] << 16) | (buffer[*i + 6] << 8) | buffer[*i + 7]; dataLength = (buffer[*i + 8] << 16) | buffer[*i + 9]; *i += 10; if (*i + dataLength > length) @throw [OFTruncatedDataException exception]; record = parseResourceRecord(name, recordClass, recordType, TTL, buffer, length, *i, dataLength); *i += dataLength; [ret addObject: record]; } [ret makeImmutable]; return ret; } static void callback(id target, SEL selector, OFDNSResolver *resolver, OFArray *answerRecords, OFArray *authorityRecords, OFArray *additionalRecords, id context, id exception) { void (*method)(id, SEL, OFDNSResolver *, OFArray *, OFArray *, OFArray *, id, id) = (void (*)(id, SEL, OFDNSResolver *, OFArray *, OFArray *, OFArray *, id, id)) [target methodForSelector: selector]; method(target, selector, resolver, answerRecords, authorityRecords, additionalRecords, context, exception); } @implementation OFDNSResolverQuery @synthesize host = _host, recordClass = _recordClass, recordType = _recordType; @synthesize ID = _ID, nameServers = _nameServers; @synthesize searchDomains = _searchDomains; @synthesize nameServersIndex = _nameServersIndex; @synthesize searchDomainsIndex = _searchDomainsIndex, attempt = _attempt; |
︙ | ︙ | |||
1012 1013 1014 1015 1016 1017 1018 | selector: @selector(of_socket:didSendBuffer:bytesSent: receiver:context:exception:) context: query]; } - (void)of_queryWithIDTimedOut: (OFDNSResolverQuery *)query { | | < | < | | < < | > | 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 | selector: @selector(of_socket:didSendBuffer:bytesSent: receiver:context:exception:) context: query]; } - (void)of_queryWithIDTimedOut: (OFDNSResolverQuery *)query { id target, context; SEL selector; OFResolveHostFailedException *exception; if (query == nil) return; if ([query nameServersIndex] + 1 < [[query nameServers] count]) { [query setNameServersIndex: [query nameServersIndex] + 1]; [self of_sendQuery: query]; return; } if ([query attempt] < ATTEMPTS) { [query setAttempt: [query attempt] + 1]; [query setNameServersIndex: 0]; [self of_sendQuery: query]; return; } target = [[[query target] retain] autorelease]; selector = [query selector]; context = [[[query context] retain] autorelease]; exception = [OFResolveHostFailedException exceptionWithHost: [query host] recordClass: [query recordClass] recordType: [query recordType] error: OF_DNS_RESOLVER_ERROR_TIMEOUT]; [_queries removeObjectForKey: [query ID]]; callback(target, selector, self, nil, nil, nil, context, exception); } - (size_t)of_socket: (OFUDPSocket *)sock didSendBuffer: (void **)buffer bytesSent: (size_t)bytesSent receiver: (of_socket_address_t *)receiver context: (OFDNSResolverQuery *)query exception: (id)exception { if (exception != nil) { id target = [[[query target] retain] autorelease]; SEL selector = [query selector]; id context = [[[query context] retain] autorelease]; [_queries removeObjectForKey: [query ID]]; callback(target, selector, self, nil, nil, nil, context, exception); return 0; } [sock asyncReceiveIntoBuffer: [self allocMemoryWithSize: 512] length: 512 target: self |
︙ | ︙ | |||
1087 1088 1089 1090 1091 1092 1093 | - (bool)of_socket: (OFUDPSocket *)sock didReceiveIntoBuffer: (unsigned char *)buffer length: (size_t)length sender: (of_socket_address_t)sender context: (id)context exception: (id)exception { | | > < < < | < < < < | > | 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 | - (bool)of_socket: (OFUDPSocket *)sock didReceiveIntoBuffer: (unsigned char *)buffer length: (size_t)length sender: (of_socket_address_t)sender context: (id)context exception: (id)exception { OFArray *answerRecords = nil, *authorityRecords = nil; OFArray *additionalRecords = nil; OFNumber *ID; OFDNSResolverQuery *query; OFData *queryData; if (exception != nil) return false; if (length < 2) /* We can't get the ID to get the query. Give up. */ return false; ID = [OFNumber numberWithUInt16: (buffer[0] << 8) | buffer[1]]; query = [[[_queries objectForKey: ID] retain] autorelease]; if (query == nil) return false; [[query cancelTimer] invalidate]; [_queries removeObjectForKey: ID]; queryData = [query queryData]; @try { const unsigned char *queryDataBuffer; size_t i; of_dns_resolver_error_t error; uint16_t numQuestions, numAnswers, numAuthorityRecords; uint16_t numAdditionalRecords; if (length < 12) @throw [OFTruncatedDataException exception]; if ([queryData itemSize] != 1 || [queryData count] < 12) @throw [OFInvalidArgumentException exception]; |
︙ | ︙ | |||
1175 1176 1177 1178 1179 1180 1181 | @throw [OFResolveHostFailedException exceptionWithHost: [query host] recordClass: [query recordClass] recordType: [query recordType] error: error]; numQuestions = (buffer[4] << 8) | buffer[5]; | < > | < | < < < < < < | < < | < < < < < | < | < < < < < < < < < > | > | | 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 1249 1250 1251 1252 1253 1254 1255 1256 1257 | @throw [OFResolveHostFailedException exceptionWithHost: [query host] recordClass: [query recordClass] recordType: [query recordType] 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, numAdditionalRecords); } @catch (id e) { callback([query target], [query selector], self, nil, nil, nil, [query context], e); return false; } callback([query target], [query selector], self, answerRecords, authorityRecords, additionalRecords, [query context], nil); return false; } - (void)close { void *pool = objc_autoreleasePoolPush(); |
︙ | ︙ | |||
1249 1250 1251 1252 1253 1254 1255 | [_IPv6Socket cancelAsyncRequests]; [_IPv6Socket release]; _IPv6Socket = nil; #endif enumerator = [_queries objectEnumerator]; while ((query = [enumerator nextObject]) != nil) { | < < < < < > | | 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 | [_IPv6Socket cancelAsyncRequests]; [_IPv6Socket release]; _IPv6Socket = nil; #endif enumerator = [_queries objectEnumerator]; while ((query = [enumerator nextObject]) != nil) { OFResolveHostFailedException *exception; exception = [OFResolveHostFailedException exceptionWithHost: [query host] recordClass: [query recordClass] recordType: [query recordType] error: OF_DNS_RESOLVER_ERROR_CANCELED]; callback([query target], [query selector], self, nil, nil, nil, [query context], exception); } [_queries removeAllObjects]; objc_autoreleasePoolPop(pool); } @end |
Modified utils/ofdns/OFDNS.m from [8f48efd875] to [3c9332d43f].
︙ | ︙ | |||
24 25 26 27 28 29 30 | @interface OFDNS: OFObject <OFApplicationDelegate> @end OF_APPLICATION_DELEGATE(OFDNS) @implementation OFDNS | > | > > | > > > > | 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | @interface OFDNS: OFObject <OFApplicationDelegate> @end OF_APPLICATION_DELEGATE(OFDNS) @implementation OFDNS - (void)DNSResolver: (OFDNSResolver *)resolver didReceiveAnswerRecords: (OFArray *)answerRecords authorityRecords: (OFArray *)authorityRecords additionalRecords: (OFArray *)additionalRecords context: (id)context exception: (id)exception { if (exception != nil) { [of_stderr writeFormat: @"Failed to resolve: %@\n", exception]; [OFApplication terminateWithStatus: 1]; } [of_stdout writeFormat: @"Answer records: %@\n" @"Authority records: %@\n" @"Additional records: %@\n", answerRecords, authorityRecords, additionalRecords]; [OFApplication terminate]; } - (void)applicationDidFinishLaunching { OFArray OF_GENERIC(OFString *) *arguments = [OFApplication arguments]; |
︙ | ︙ | |||
72 73 74 75 76 77 78 | [resolver setNameServers: [OFArray arrayWithObject: [arguments objectAtIndex: 3]]]; [resolver asyncResolveHost: [arguments objectAtIndex: 0] recordClass: recordClass recordType: recordType target: self | | > > | | 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | [resolver setNameServers: [OFArray arrayWithObject: [arguments objectAtIndex: 3]]]; [resolver asyncResolveHost: [arguments objectAtIndex: 0] recordClass: recordClass recordType: recordType target: self selector: @selector(DNSResolver: didReceiveAnswerRecords: authorityRecords:additionalRecords: context:exception:) context: nil]; } @end |