Overview
Comment: | OFDNSResolver: Minor refactoring |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
4afaa63320057dfaaedf10c6a3187056 |
User & Date: | js on 2018-08-11 11:58:54 |
Other Links: | manifest | tags |
Context
2018-08-11
| ||
14:00 | Change the layout of of_socket_address_t check-in: 8f810ecb7e user: js tags: trunk | |
11:58 | OFDNSResolver: Minor refactoring check-in: 4afaa63320 user: js tags: trunk | |
2018-08-06
| ||
22:34 | socket.m: Replace inet_{ntop,pton} with own impl check-in: ab9810ef69 user: js tags: trunk | |
Changes
Modified src/OFDNSResolver.h from [1905ee4e94] to [878890e8bc].
︙ | ︙ | |||
18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #import "OFObject.h" #import "OFString.h" #import "OFDNSResourceRecord.h" OF_ASSUME_NONNULL_BEGIN @class OFArray OF_GENERIC(ObjectType); @class OFDictionary OF_GENERIC(KeyType, ObjectType); @class OFMutableDictionary OF_GENERIC(KeyType, ObjectType); @class OFNumber; @class OFUDPSocket; /*! * @enum of_dns_resolver_error_t OFDNSResolver.h ObjFW/OFDNSResolver.h | > | 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | #import "OFObject.h" #import "OFString.h" #import "OFDNSResourceRecord.h" OF_ASSUME_NONNULL_BEGIN @class OFArray OF_GENERIC(ObjectType); @class OFDNSResolverQuery; @class OFDictionary OF_GENERIC(KeyType, ObjectType); @class OFMutableDictionary OF_GENERIC(KeyType, ObjectType); @class OFNumber; @class OFUDPSocket; /*! * @enum of_dns_resolver_error_t OFDNSResolver.h ObjFW/OFDNSResolver.h |
︙ | ︙ | |||
72 73 74 75 76 77 78 | OFArray OF_GENERIC(OFString *) *_searchDomains; size_t _minNumberOfDotsInAbsoluteName; bool _usesTCP; OFUDPSocket *_IPv4Socket; #ifdef OF_HAVE_IPV6 OFUDPSocket *_IPv6Socket; #endif | | > | 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | OFArray OF_GENERIC(OFString *) *_searchDomains; size_t _minNumberOfDotsInAbsoluteName; bool _usesTCP; OFUDPSocket *_IPv4Socket; #ifdef OF_HAVE_IPV6 OFUDPSocket *_IPv6Socket; #endif OFMutableDictionary OF_GENERIC(OFNumber *, OFDNSResolverQuery *) *_queries; } /*! * @brief A dictionary of static hosts. * * This dictionary is checked before actually looking up a host. */ |
︙ | ︙ |
Modified src/OFDNSResolver.m from [5d4867eed8] to [037c3906cf].
︙ | ︙ | |||
62 63 64 65 66 67 68 | * TODO: * * - Resolve with each search domain * - Iterate through name servers * - Fallback to TCP */ | | < | > | | | | | | < | < | 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | * TODO: * * - Resolve with each search domain * - Iterate through name servers * - Fallback to TCP */ @interface OFDNSResolverQuery: OFObject { OFString *_host; of_dns_resource_record_class_t _recordClass; of_dns_resource_record_type_t _recordType; OFNumber *_ID; OFArray OF_GENERIC(OFString *) *_nameServers, *_searchDomains; size_t _nameServersIndex, _searchDomainsIndex; id _target; SEL _selector; id _context; OFData *_queryData; OFTimer *_cancelTimer; } @property (readonly, nonatomic) OFString *host; @property (readonly, nonatomic) of_dns_resource_record_class_t recordClass; @property (readonly, nonatomic) of_dns_resource_record_type_t recordType; @property (readonly, nonatomic) OFNumber *ID; @property (readonly, nonatomic) OFArray OF_GENERIC(OFString *) *nameServers; @property (readonly, nonatomic) OFArray OF_GENERIC(OFString *) *searchDomains; @property (nonatomic) size_t nameServersIndex; @property (nonatomic) size_t searchDomainsIndex; @property (readonly, nonatomic) id target; @property (readonly, nonatomic) SEL selector; @property (readonly, nonatomic) id context; @property (readonly, nonatomic) OFData *queryData; @property (retain, nonatomic) OFTimer *cancelTimer; - (instancetype)initWithHost: (OFString *)host 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 target: (id)target selector: (SEL)selector context: (id)context; @end @interface OFDNSResolver () #ifdef OF_HAVE_FILES - (void)of_parseHosts: (OFString *)path; # ifndef OF_WINDOWS - (void)of_parseResolvConf: (OFString *)path; |
︙ | ︙ | |||
497 498 499 500 501 502 503 | return [[[OFDNSResourceRecord alloc] initWithName: name recordClass: recordClass recordType: recordType TTL: TTL] autorelease]; } | | | | | < | < > > > > < > | > | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > < | > | | 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 524 525 526 527 528 529 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 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 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 | 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, target = _target; @synthesize selector = _selector, context = _context, queryData = _queryData; @synthesize cancelTimer = _cancelTimer; - (instancetype)initWithHost: (OFString *)host 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 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]; _recordClass = recordClass; _recordType = recordType; _ID = [ID retain]; _nameServers = [nameServers copy]; _searchDomains = [searchDomains copy]; _target = [target retain]; _selector = selector; _context = [context retain]; queryData = [OFMutableData dataWithCapacity: 512]; /* Header */ tmp = OF_BSWAP16_IF_LE([ID uInt16Value]); [queryData addItems: &tmp count: 2]; /* RD */ tmp = OF_BSWAP16_IF_LE(1 << 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 [host componentsSeparatedByString: @"."]) { size_t length = [component UTF8StringLength]; uint8_t length8; if (length > 63 || [queryData count] + length > 512) @throw [OFOutOfRangeException exception]; length8 = (uint8_t)length; [queryData addItem: &length8]; [queryData addItems: [component UTF8String] count: length]; } /* QTYPE */ tmp = OF_BSWAP16_IF_LE(recordType); [queryData addItems: &tmp count: 2]; /* QCLASS */ tmp = OF_BSWAP16_IF_LE(recordClass); [queryData addItems: &tmp count: 2]; [queryData makeImmutable]; _queryData = [queryData copy]; objc_autoreleasePoolPop(pool); } @catch (id e) { [self release]; @throw e; } return self; } - (void)dealloc { [_host release]; [_ID release]; [_nameServers release]; [_searchDomains release]; [_target release]; [_context release]; [_queryData release]; [_cancelTimer release]; [super dealloc]; } @end @implementation OFDNSResolver @synthesize staticHosts = _staticHosts, nameServers = _nameServers; |
︙ | ︙ | |||
892 893 894 895 896 897 898 | length: (size_t)length sender: (of_socket_address_t)sender context: (id)context exception: (id)exception { OFMutableArray *answers = nil; OFNumber *ID; | | | | | | | | | | | | 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 | length: (size_t)length sender: (of_socket_address_t)sender context: (id)context exception: (id)exception { OFMutableArray *answers = nil; OFNumber *ID; OFDNSResolverQuery *query; id target; SEL selector; void (*callback)(id, SEL, OFArray *, id, id); OFData *queryData; if (exception != nil) return false; if (length < 2) /* We can't get the ID to get the context. 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]; target = [query target]; selector = [query selector]; callback = (void (*)(id, SEL, OFArray *, id, id)) [target methodForSelector: selector]; queryData = [query queryData]; @try { const unsigned char *queryDataBuffer; size_t i; of_dns_resolver_error_t error; uint16_t numQuestions, numAnswers; if (length < 12) @throw [OFTruncatedDataException exception]; if ([queryData itemSize] != 1 || [queryData count] < 12) @throw [OFInvalidArgumentException exception]; queryDataBuffer = [queryData items]; /* QR */ if ((buffer[2] & 0x80) == 0) @throw [OFInvalidServerReplyException exception]; /* Opcode */ if ((buffer[2] & 0x78) != (queryDataBuffer[2] & 0x78)) @throw [OFInvalidServerReplyException exception]; /* TC */ if (buffer[2] & 0x02) @throw [OFTruncatedDataException exception]; /* RCODE */ |
︙ | ︙ | |||
972 973 974 975 976 977 978 | default: error = OF_DNS_RESOLVER_ERROR_UNKNOWN; break; } if (buffer[3] & 0x0F) @throw [OFResolveHostFailedException | | | | | 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 | default: error = OF_DNS_RESOLVER_ERROR_UNKNOWN; break; } if (buffer[3] & 0x0F) @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]; answers = [OFMutableArray arrayWithCapacity: numAnswers]; |
︙ | ︙ | |||
1025 1026 1027 1028 1029 1030 1031 | record = createResourceRecord(name, recordClass, recordType, TTL, buffer, length, i, dataLength); i += dataLength; [answers addObject: record]; } } @catch (id e) { | | < | < | | | | | | | | | < | | | | | < | 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 | record = createResourceRecord(name, recordClass, recordType, TTL, buffer, length, i, dataLength); i += dataLength; [answers addObject: record]; } } @catch (id e) { callback(target, selector, nil, [query context], e); return false; } callback(target, selector, answers, [query context], nil); return false; } - (void)of_queryWithIDTimedOut: (OFNumber *)ID { OFDNSResolverQuery *query = [_queries objectForKey: ID]; id target; SEL selector; void (*callback)(id, SEL, OFArray *, id, id); OFResolveHostFailedException *exception; if (query == nil) return; target = [[[query target] retain] autorelease]; selector = [query selector]; callback = (void (*)(id, SEL, OFArray *, id, id)) [target methodForSelector: selector]; exception = [OFResolveHostFailedException exceptionWithHost: [query host] recordClass: [query recordClass] recordType: [query recordType] error: OF_DNS_RESOLVER_ERROR_TIMEOUT]; [_queries removeObjectForKey: [query ID]]; callback(target, selector, nil, [query 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]; void (*callback)(id, SEL, OFArray *, id, id) = (void (*)(id, SEL, OFArray *, id, id)) [target methodForSelector: selector]; [_queries removeObjectForKey: [query ID]]; callback(target, selector, nil, [query context], exception); return 0; } [sock asyncReceiveIntoBuffer: [self allocMemoryWithSize: 512] length: 512 target: self |
︙ | ︙ | |||
1117 1118 1119 1120 1121 1122 1123 | 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(); | < < | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | < | < | > > > > > > > | | 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 | 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(); OFDNSResolverQuery *query; OFNumber *ID; OFUDPSocket *sock; of_socket_address_t address; /* TODO: Properly try all search domains */ if (![host hasSuffix: @"."]) host = [host stringByAppendingString: @"."]; if ([host UTF8StringLength] > 253) @throw [OFOutOfRangeException exception]; /* Random, unused ID */ do { ID = [OFNumber numberWithUInt16: (uint16_t)of_random()]; } while ([_queries objectForKey: ID] != nil); query = [[[OFDNSResolverQuery alloc] initWithHost: host recordClass: recordClass recordType: recordType ID: ID nameServers: _nameServers searchDomains: _searchDomains target: target selector: selector context: context] autorelease]; [_queries setObject: query forKey: ID]; [query setCancelTimer: [OFTimer scheduledTimerWithTimeInterval: TIMEOUT target: self selector: @selector(of_queryWithIDTimedOut:) object: ID repeats: false]]; address = of_socket_address_parse_ip( [[query nameServers] firstObject], 53); #ifdef OF_HAVE_IPV6 if (address.address.ss_family == AF_INET6) { if (_IPv6Socket == nil) { _IPv6Socket = [[OFUDPSocket alloc] init]; [_IPv6Socket bindToHost: @"::" port: 0]; |
︙ | ︙ | |||
1231 1232 1233 1234 1235 1236 1237 | } sock = _IPv4Socket; #ifdef OF_HAVE_IPV6 } #endif | | | | | | | | | | | | | < | 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 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 | } sock = _IPv4Socket; #ifdef OF_HAVE_IPV6 } #endif [sock asyncSendBuffer: [[query queryData] items] length: [[query queryData] count] receiver: address target: self selector: @selector(of_socket:didSendBuffer:bytesSent: receiver:context:exception:) context: query]; objc_autoreleasePoolPop(pool); } - (void)close { void *pool = objc_autoreleasePoolPush(); OFEnumerator OF_GENERIC(OFDNSResolverQuery *) *enumerator; OFDNSResolverQuery *query; [_IPv4Socket cancelAsyncRequests]; [_IPv4Socket release]; _IPv4Socket = nil; #ifdef OF_HAVE_IPV6 [_IPv6Socket cancelAsyncRequests]; [_IPv6Socket release]; _IPv6Socket = nil; #endif enumerator = [_queries objectEnumerator]; while ((query = [enumerator nextObject]) != nil) { id target = [[[query target] retain] autorelease]; SEL selector = [query selector]; void (*callback)(id, SEL, OFArray *, id, id) = (void (*)(id, SEL, OFArray *, id, id)) [target methodForSelector: selector]; OFResolveHostFailedException *exception; exception = [OFResolveHostFailedException exceptionWithHost: [query host] recordClass: [query recordClass] recordType: [query recordType] error: OF_DNS_RESOLVER_ERROR_CANCELED]; callback(target, selector, nil, [query context], exception); } [_queries removeAllObjects]; objc_autoreleasePoolPop(pool); } @end |