︙ | | |
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
+
|
#import "OFString.h"
OF_ASSUME_NONNULL_BEGIN
#define OF_DNS_RESOLVER_BUFFER_LENGTH 512
@class OFArray OF_GENERIC(ObjectType);
@class OFDNSResolver;
@class OFDNSResolverQuery;
@class OFDate;
@class OFDictionary OF_GENERIC(KeyType, ObjectType);
@class OFMutableDictionary OF_GENERIC(KeyType, ObjectType);
@class OFNumber;
@class OFUDPSocket;
|
︙ | | |
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
59
60
61
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
114
115
116
117
118
119
|
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
/*! The server returned an error that the domain does not exist */
OF_DNS_RESOLVER_ERROR_SERVER_NAME_ERROR,
/*! The server does not have support for the requested query */
OF_DNS_RESOLVER_ERROR_SERVER_NOT_IMPLEMENTED,
/*! The server refused the query */
OF_DNS_RESOLVER_ERROR_SERVER_REFUSED
} of_dns_resolver_error_t;
typedef OFDictionary OF_GENERIC(OFString *,
OFArray OF_GENERIC(OFDNSResourceRecord *) *) *of_dns_resolver_records_t;
/*!
* @protocol OFDNSResolverDelegate OFDNSResolver.h ObjFW/OFDNSResolver.h
*
* @brief A delegate for OFDNSResolver.
*/
@protocol OFDNSResolverDelegate <OFObject>
@optional
/*!
* @brief This method is called when a DNS resolver resolved a domain name.
*
* @param resolver The acting resolver
* @param domainName The fully qualified domain name used to resolve the host
* @param answerRecords The answer records from the name server, grouped by
* domain name
* @param authorityRecords The authority records from the name server, grouped
* by domain name
* @param additionalRecords Additional records sent by the name server, grouped
* by domain name
* @param exception An exception that happened during resolving, or nil on
* success
*/
- (void)resolver: (OFDNSResolver *)resolver
didResolveDomainName: (OFString *)domainName
answerRecords: (nullable of_dns_resolver_records_t)answerRecords
authorityRecords: (nullable of_dns_resolver_records_t)authorityRecords
additionalRecords: (nullable of_dns_resolver_records_t)additionalRecords
exception: (nullable id)exception;
/*!
* @brief This method is called when a DNS resolver resolved a domain name to
* socket addresses.
*
* @param resolver The acting resolver
* @param domainName The fully qualified domain name used to resolve the host
* @param socketAddresses OFData containing several of_socket_address_t
* @param exception The exception that occurred during resolving, or nil on
* success
*/
- (void)resolver: (OFDNSResolver *)resolver
didResolveDomainName: (OFString *)domainName
socketAddresses: (nullable OFData *)socketAddresses
exception: (nullable id)exception;
@end
/*!
* @class OFDNSResolver OFDNSResolver.h ObjFW/OFDNSResolver.h
*
* @brief A class for resolving DNS names.
*
* @note If you change any of the properties, make sure to set
|
︙ | | |
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
|
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
+
+
-
|
*/
- (instancetype)init;
/*!
* @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
* the following:
* @parblock
*
* void (OFDNSResolver *resolver, OFString *domainName,
* OFArray<OFDictionary<OFString *,
* __kindof OFDNSResourceRecord *> *>
* *_Nullable answerRecords,
* OFArray<OFDictionary<OFString *,
* __kindof OFDNSResourceRecord *> *>
* *_Nullable authorityRecords,
* OFArray<OFDictionary<OFString *,
* __kindof OFDNSResourceRecord *> *>
* *_Nullable additionalRecords,
* id _Nullable context, id _Nullable exception)
*
* `resolver` is the acting resolver.@n
* `domainName` is the fully qualified domain name used to
* resolve the host.@n
* `answerRecords` are the answer records from the name server,
* grouped by domain name.
* @n
* `authorityRecords` are the authority records from the name
* server, grouped by domain name.@n
* `additionalRecords` are additional records sent by the name
* server, grouped by domain name.@n
* `context` is the context object originally passed.@n
* `exception` is an exception that happened during resolving,
* otherwise nil.
* @endparblock
* @param context A context object to pass along to the target
* @param delegate The delegate to use for callbacks
*/
- (void)asyncResolveHost: (OFString *)host
target: (id)target
delegate: (id <OFDNSResolverDelegate>)delegate;
selector: (SEL)selector
context: (nullable id)context;
/*!
* @brief Asynchronously resolves the specified host.
*
* @param host The host to resolve
* @param recordClass The desired class of the records to query
* @param recordType The desired type of the records to query
* @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
* the following:
* @parblock
*
* void (OFDNSResolver *resolver, OFString *domainName,
* OFArray<OFDictionary<OFString *,
* __kindof OFDNSResourceRecord *> *>
* *_Nullable answerRecords,
* OFArray<OFDictionary<OFString *,
* __kindof OFDNSResourceRecord *> *>
* *_Nullable authorityRecords,
* OFArray<OFDictionary<OFString *,
* __kindof OFDNSResourceRecord *> *>
* *_Nullable additionalRecords,
* id _Nullable context, id _Nullable exception)
*
* `resolver` is the acting resolver.@n
* `domainName` is the fully qualified domain name used to
* resolve the host.@n
* `answerRecords` are the answer records from the name server,
* grouped by domain name.
* @n
* `authorityRecords` are the authority records from the name
* server, grouped by domain name.@n
* `additionalRecords` are additional records sent by the name
* server, grouped by domain name.@n
* `context` is the context object originally passed.@n
* `exception` is an exception that happened during resolving,
* otherwise nil.
* @endparblock
* @param context A context object to pass along to the target
* @param delegate The delegate to use for callbacks
*/
- (void)asyncResolveHost: (OFString *)host
recordClass: (of_dns_resource_record_class_t)recordClass
recordType: (of_dns_resource_record_type_t)recordType
target: (id)target
delegate: (id <OFDNSResolverDelegate>)delegate;
selector: (SEL)selector
context: (nullable id)context;
/*!
* @brief Asynchronously resolves the specified host.
*
* @param host The host to resolve
* @param recordClass The desired class of the records to query
* @param recordType The desired type of the records to query
* @param runLoopMode The run loop mode in which 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
* the following:
* @parblock
*
* void (OFDNSResolver *resolver, OFString *domainName,
* OFArray<OFDictionary<OFString *,
* __kindof OFDNSResourceRecord *> *>
* *_Nullable answerRecords,
* OFArray<OFDictionary<OFString *,
* __kindof OFDNSResourceRecord *> *>
* *_Nullable authorityRecords,
* OFArray<OFDictionary<OFString *,
* __kindof OFDNSResourceRecord *> *>
* *_Nullable additionalRecords,
* id _Nullable context, id _Nullable exception)
*
* `resolver` is the acting resolver.@n
* `domainName` is the fully qualified domain name used to
* resolve the host.@n
* `answerRecords` are the answer records from the name server,
* grouped by domain name.
* @n
* `authorityRecords` are the authority records from the name
* server, grouped by domain name.@n
* `additionalRecords` are additional records sent by the name
* server, grouped by domain name.@n
* `context` is the context object originally passed.@n
* `exception` is an exception that happened during resolving,
* otherwise nil.
* @endparblock
* @param context A context object to pass along to the target
* @param delegate The delegate to use for callbacks
*/
- (void)asyncResolveHost: (OFString *)host
recordClass: (of_dns_resource_record_class_t)recordClass
recordType: (of_dns_resource_record_type_t)recordType
runLoopMode: (of_run_loop_mode_t)runLoopMode
target: (id)target
delegate: (id <OFDNSResolverDelegate>)delegate;
selector: (SEL)selector
context: (nullable id)context;
/*!
* @brief Asynchronously resolves the specified host to socket addresses.
*
* @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
* the following:
* @parblock
*
* void (OFDNSResolver *resolver, OFString *domainName,
* OFData *_Nullable, socketAddresses,
* id _Nullable context, id _Nullable exception)
*
* `resolver` is the acting resolver.@n
* `domainName` is the fully qualified domain name used to
* resolve the host.@n
* `socketAddresses` is OFData containing several
* of_socket_address_t.@n
* `context` is the context object originally passed.@n
* `exception` is an exception that happened during resolving,
* otherwise nil.
* @endparblock
* @param context A context object to pass along to the target
* @param delegate The delegate to use for callbacks
*/
- (void)asyncResolveSocketAddressesForHost: (OFString *)host
target: (id)target
selector: (SEL)selector
delegate: (id <OFDNSResolverDelegate>)
delegate;
context: (nullable id)context;
/*!
* @brief Asynchronously resolves the specified host to socket addresses.
*
* @param host The host to resolve
* @param addressFamily The desired socket address family
* @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
* the following:
* @parblock
*
* void (OFDNSResolver *resolver, OFString *domainName,
* OFData *_Nullable socketAddresses,
* id _Nullable context, id _Nullable exception)
*
* `resolver` is the acting resolver.@n
* `domainName` is the fully qualified domain name used to
* resolve the host.@n
* `socketAddresses` is OFData containing several
* of_socket_address_t.@n
* `context` is the context object originally passed.@n
* `exception` is an exception that happened during resolving,
* otherwise nil.
* @endparblock
* @param context A context object to pass along to the target
* @param delegate The delegate to use for callbacks
*/
- (void)asyncResolveSocketAddressesForHost: (OFString *)host
addressFamily: (of_socket_address_family_t)
addressFamily
target: (id)target
selector: (SEL)selector
delegate: (id <OFDNSResolverDelegate>)
delegate;
context: (nullable id)context;
/*!
* @brief Asynchronously resolves the specified host to socket addresses.
*
* @param host The host to resolve
* @param addressFamily The desired socket address family
* @param runLoopMode The run loop mode in which 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
* the following:
* @parblock
*
* void (OFDNSResolver *resolver, OFString *domainName,
* OFData *_Nullable socketAddresses,
* id _Nullable context, id _Nullable exception)
*
* `resolver` is the acting resolver.@n
* `domainName` is the fully qualified domain name used to
* resolve the host.@n
* `socketAddresses` is OFData containing several
* of_socket_address_t.@n
* `context` is the context object originally passed.@n
* `exception` is an exception that happened during resolving,
* otherwise nil.
* @endparblock
* @param context A context object to pass along to the target
* @param delegate The delegate to use for callbacks
*/
- (void)asyncResolveSocketAddressesForHost: (OFString *)host
addressFamily: (of_socket_address_family_t)
addressFamily
runLoopMode: (of_run_loop_mode_t)runLoopMode
target: (id)target
selector: (SEL)selector
delegate: (id <OFDNSResolverDelegate>)
delegate;
context: (nullable id)context;
/*!
* @brief Synchronously resolves the specified host to socket addresses.
*
* @param host The host to resolve
* @param addressFamily The desired socket address family
* @return OFData containing several of_socket_address_t
|
︙ | | |
︙ | | |
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
|
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
-
+
-
-
-
+
-
-
|
selector: (SEL)selector
context: (id)context;
@end
@interface OFDNSResolver_AsyncResolveSocketAddressesContext: OFObject
{
OFString *_host;
id _target;
id _delegate;
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
delegate: (id)delegate;
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
|
︙ | | |
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
|
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
-
+
+
-
-
-
-
-
-
|
answerRecords: (OFDictionary *)answerRecords
authorityRecords: (OFDictionary *)authorityRecords
additionalRecords: (OFDictionary *)additionalRecords
context: (id)context
exception: (id)exception;
@end
@interface OFDNSResolver_ResolveSocketAddressesContext: OFObject
@interface OFDNSResolver_ResolveSocketAddressesDelegate: OFObject
<OFDNSResolverDelegate>
{
@public
bool _done;
OFData *_socketAddresses;
id _exception;
}
- (void)resolver: (OFDNSResolver *)resolver
didResolveDomainName: (OFString *)domainName
socketAddresses: (OFData *)socketAddresses
context: (id)context
exception: (id)exception;
@end
@interface OFDNSResolver () <OFUDPSocketDelegate>
- (void)of_setDefaults;
- (void)of_obtainSystemConfig;
#if defined(OF_HAVE_FILES) && !defined(OF_NINTENDO_3DS)
- (void)of_parseHosts: (OFString *)path;
|
︙ | | |
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
+
+
+
+
+
+
+
|
settings: (OFDNSResolverSettings *)settings
nameServersIndex: (size_t)nameServersIndex
searchDomainsIndex: (size_t)searchDomainsIndex
runLoopMode: (of_run_loop_mode_t)runLoopMode
target: (id)target
selector: (SEL)selector
context: (id)context;
- (void)of_asyncResolveHost: (OFString *)host
recordClass: (of_dns_resource_record_class_t)recordClass
recordType: (of_dns_resource_record_type_t)recordType
runLoopMode: (of_run_loop_mode_t)runLoopMode
target: (id)target
selector: (SEL)selector
context: (id)context;
- (void)of_sendQuery: (OFDNSResolverQuery *)query
runLoopMode: (of_run_loop_mode_t)runLoopMode;
- (void)of_queryWithIDTimedOut: (OFDNSResolverQuery *)query;
@end
#ifndef OF_WII
static OFString *
|
︙ | | |
833
834
835
836
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
|
831
832
833
834
835
836
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
|
-
+
-
-
-
+
-
-
-
+
-
|
[super dealloc];
}
@end
@implementation OFDNSResolver_AsyncResolveSocketAddressesContext
- (instancetype)initWithHost: (OFString *)host
target: (id)target
delegate: (id)delegate
selector: (SEL)selector
context: (id)context
{
self = [super init];
@try {
_host = [host copy];
_target = [target retain];
_delegate = [delegate retain];
_selector = selector;
_context = [context retain];
_records = [[OFMutableArray alloc] init];
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
- (void)dealloc
{
[_host release];
[_target release];
[_delegate release];
[_context release];
[_records release];
[_resolver release];
[_domainName release];
[super dealloc];
}
|
︙ | | |
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
|
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
|
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
|
_expectedResponses++;
[result addObject:
[OFPair pairWithFirstObject: CNAME
secondObject: recordTypeNumber]];
[_resolver asyncResolveHost: alias
recordClass: OF_DNS_RESOURCE_RECORD_CLASS_IN
recordType: recordType
runLoopMode: runLoopMode
target: self
selector: @selector(resolver:
didResolveCNAME:
answerRecords:authorityRecords:
additionalRecords:context:
exception:)
context: recordTypeNumber];
[_resolver of_asyncResolveHost: alias
recordClass: OF_DNS_RESOURCE_RECORD_CLASS_IN
recordType: recordType
runLoopMode: runLoopMode
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
|
︙ | | |
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
|
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
|
-
-
-
+
-
+
-
-
-
-
-
-
+
+
+
+
-
-
+
+
+
-
|
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)];
id exception = nil;
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)
if ([addresses count] == 0)
method(_target, _selector, _resolver, _domainName, addresses,
_context, nil);
else {
OFResolveHostFailedException *e;
e = [OFResolveHostFailedException
exception = [OFResolveHostFailedException
exceptionWithHost: _host
recordClass: OF_DNS_RESOURCE_RECORD_CLASS_IN
recordType: 0
error: OF_DNS_RESOLVER_ERROR_UNKNOWN];
if ([_delegate respondsToSelector: @selector(
resolver:didResolveDomainName:socketAddresses:exception:)])
[_delegate resolver: _resolver
method(_target, _selector, _resolver, _domainName, nil,
_context, e);
didResolveDomainName: _domainName
socketAddresses: (exception == nil ? addresses : nil)
exception: exception];
}
}
- (void)resolver: (OFDNSResolver *)resolver
didResolveDomainName: (OFString *)domainName
answerRecords: (OFDictionary *)answerRecords
authorityRecords: (OFDictionary *)authorityRecords
additionalRecords: (OFDictionary *)additionalRecords
|
︙ | | |
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
|
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
|
-
+
-
|
result: _records];
if (_expectedResponses == 0)
[self done];
}
@end
@implementation OFDNSResolver_ResolveSocketAddressesContext
@implementation OFDNSResolver_ResolveSocketAddressesDelegate
- (void)dealloc
{
[_socketAddresses release];
[_exception release];
[super dealloc];
}
- (void)resolver: (OFDNSResolver *)resolver
didResolveDomainName: (OFString *)domainName
socketAddresses: (OFData *)socketAddresses
context: (id)context
exception: (id)exception
{
_socketAddresses = [socketAddresses retain];
_exception = [exception retain];
_done = true;
}
@end
|
︙ | | |
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
|
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
|
+
-
+
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
-
+
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
+
+
+
|
forKey: ID];
[self of_sendQuery: query
runLoopMode: runLoopMode];
objc_autoreleasePoolPop(pool);
}
- (void)of_resolver: (OFDNSResolver *)resolver
- (void)asyncResolveHost: (OFString *)host
didResolveDomainName: (OFString *)domainName
target: (id)target
selector: (SEL)selector
context: (id)context
answerRecords: (of_dns_resolver_records_t)answerRecords
authorityRecords: (of_dns_resolver_records_t)authorityRecords
additionalRecords: (of_dns_resolver_records_t)additionalRecords
context: (id)delegate
exception: (id)exception
{
if ([delegate respondsToSelector: @selector(resolver:
didResolveDomainName:answerRecords:authorityRecords:
additionalRecords:exception:)])
[delegate resolver: resolver
didResolveDomainName: domainName
answerRecords: answerRecords
authorityRecords: authorityRecords
additionalRecords: additionalRecords
exception: exception];
}
- (void)asyncResolveHost: (OFString *)host
delegate: (id <OFDNSResolverDelegate>)delegate
{
[self asyncResolveHost: host
recordClass: OF_DNS_RESOURCE_RECORD_CLASS_IN
recordType: OF_DNS_RESOURCE_RECORD_TYPE_ALL
runLoopMode: of_run_loop_mode_default
target: target
selector: selector
context: context];
[self of_asyncResolveHost: host
recordClass: OF_DNS_RESOURCE_RECORD_CLASS_IN
recordType: OF_DNS_RESOURCE_RECORD_TYPE_ALL
runLoopMode: of_run_loop_mode_default
target: self
selector: @selector(of_resolver:didResolveDomainName:
answerRecords:authorityRecords:
additionalRecords:context:exception:)
context: delegate];
}
- (void)asyncResolveHost: (OFString *)host
recordClass: (of_dns_resource_record_class_t)recordClass
recordType: (of_dns_resource_record_type_t)recordType
target: (id)target
delegate: (id <OFDNSResolverDelegate>)delegate
selector: (SEL)selector
context: (id)context
{
[self asyncResolveHost: host
recordClass: recordClass
recordType: recordType
runLoopMode: of_run_loop_mode_default
target: target
selector: selector
context: context];
[self of_asyncResolveHost: host
recordClass: recordClass
recordType: recordType
runLoopMode: of_run_loop_mode_default
target: self
selector: @selector(of_resolver:didResolveDomainName:
answerRecords:authorityRecords:
additionalRecords:context:exception:)
context: delegate];
}
- (void)asyncResolveHost: (OFString *)host
recordClass: (of_dns_resource_record_class_t)recordClass
recordType: (of_dns_resource_record_type_t)recordType
runLoopMode: (of_run_loop_mode_t)runLoopMode
delegate: (id <OFDNSResolverDelegate>)delegate
{
[self of_asyncResolveHost: host
recordClass: recordClass
recordType: recordType
runLoopMode: runLoopMode
target: self
selector: @selector(of_resolver:didResolveDomainName:
answerRecords:authorityRecords:
additionalRecords:context:exception:)
context: delegate];
}
- (void)of_asyncResolveHost: (OFString *)host
recordClass: (of_dns_resource_record_class_t)recordClass
recordType: (of_dns_resource_record_type_t)recordType
runLoopMode: (of_run_loop_mode_t)runLoopMode
target: (id)target
selector: (SEL)selector
context: (id)context
target: (id)target
selector: (SEL)selector
context: (id)context
{
void *pool = objc_autoreleasePoolPush();
OFDNSResolverSettings *settings = [[[OFDNSResolverSettings alloc]
initWithNameServers: _nameServers
searchDomains: _searchDomains
timeout: _timeout
maxAttempts: _maxAttempts
|
︙ | | |
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
|
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
|
-
+
-
-
-
-
+
-
-
+
-
-
-
-
+
-
-
+
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
-
-
-
-
+
+
+
+
+
+
+
+
+
+
-
-
-
|
answerRecords, authorityRecords, additionalRecords,
query->_context, nil);
return true;
}
- (void)asyncResolveSocketAddressesForHost: (OFString *)host
target: (id)target
delegate: (id <OFDNSResolverDelegate>)delegate
selector: (SEL)selector
context: (id)context
{
[self asyncResolveSocketAddressesForHost: host
addressFamily: OF_SOCKET_ADDRESS_FAMILY_ANY
runLoopMode: of_run_loop_mode_default
target: target
selector: selector
delegate: delegate];
context: context];
}
- (void)asyncResolveSocketAddressesForHost: (OFString *)host
addressFamily: (of_socket_address_family_t)
addressFamily
target: (id)target
delegate: (id <OFDNSResolverDelegate>)delegate
selector: (SEL)selector
context: (id)context
{
[self asyncResolveSocketAddressesForHost: host
addressFamily: addressFamily
runLoopMode: of_run_loop_mode_default
target: target
selector: selector
delegate: delegate];
context: context];
}
- (void)asyncResolveSocketAddressesForHost: (OFString *)host
addressFamily: (of_socket_address_family_t)
addressFamily
runLoopMode: (of_run_loop_mode_t)runLoopMode
target: (id)target
delegate: (id <OFDNSResolverDelegate>)delegate
selector: (SEL)selector
context: (id)userContext
{
OFArray OF_GENERIC(OFString *) *aliases;
void *pool;
OFDNSResolver_AsyncResolveSocketAddressesContext *context;
@try {
of_socket_address_t address =
of_socket_address_parse_ip(host, 0);
void (*method)(id, SEL, OFDNSResolver *, OFString *, OFData *,
id, id) = (void (*)(id, SEL, OFDNSResolver *, OFString *,
OFData *, id, id))[target methodForSelector: selector];
OFData *addresses;
if (addressFamily != OF_SOCKET_ADDRESS_FAMILY_ANY &&
address.family != addressFamily) {
method(target, selector, self, host, nil, userContext,
[OFInvalidArgumentException exception]);
if ([delegate respondsToSelector: @selector(resolver:
didResolveDomainName:socketAddresses:exception:)]) {
OFInvalidArgumentException *exception =
[OFInvalidArgumentException exception];
[delegate resolver: self
didResolveDomainName: host
socketAddresses: nil
exception: exception];
}
return;
}
addresses = [OFData dataWithItems: &address
itemSize: sizeof(address)
count: 1];
method(target, selector, self, host, addresses, userContext,
nil);
itemSize: sizeof(address)
count: 1];
if ([delegate respondsToSelector: @selector(resolver:
didResolveDomainName:socketAddresses:exception:)])
[delegate resolver: self
didResolveDomainName: host
socketAddresses: addresses
exception: nil];
return;
} @catch (OFInvalidFormatException *e) {
}
if ((aliases = [_staticHosts objectForKey: host]) != nil) {
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 (OFString *alias in aliases) {
of_socket_address_t address;
@try {
|
︙ | | |
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
|
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
|
-
-
+
+
-
+
-
+
-
+
-
-
-
-
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
+
+
-
-
+
+
+
-
-
-
+
|
[addresses addItem: &address];
}
[addresses makeImmutable];
if ([addresses count] == 0) {
OFResolveHostFailedException *exception;
of_dns_resource_record_type_t type;
id exception = nil;
of_dns_resource_record_type_t recordType;
switch (addressFamily) {
case OF_SOCKET_ADDRESS_FAMILY_ANY:
type = OF_DNS_RESOURCE_RECORD_TYPE_ALL;
recordType = OF_DNS_RESOURCE_RECORD_TYPE_ALL;
break;
case OF_SOCKET_ADDRESS_FAMILY_IPV4:
type = OF_DNS_RESOURCE_RECORD_TYPE_A;
recordType = OF_DNS_RESOURCE_RECORD_TYPE_A;
break;
case OF_SOCKET_ADDRESS_FAMILY_IPV6:
type = OF_DNS_RESOURCE_RECORD_TYPE_AAAA;
recordType = OF_DNS_RESOURCE_RECORD_TYPE_AAAA;
break;
default:
method(target, selector, self, host, nil,
userContext,
[OFInvalidArgumentException exception]);
return;
exception =
[OFInvalidArgumentException exception];
break;
}
if (exception == nil) {
of_dns_resource_record_class_t recordClass =
OF_DNS_RESOURCE_RECORD_CLASS_IN;
of_dns_resolver_error_t error =
OF_DNS_RESOLVER_ERROR_NO_RESULT;
exception = [OFResolveHostFailedException
exceptionWithHost: host
recordClass: OF_DNS_RESOURCE_RECORD_CLASS_IN
recordType: type
error: OF_DNS_RESOLVER_ERROR_NO_RESULT];
method(target, selector, self, host, nil, userContext,
exception);
exception = [OFResolveHostFailedException
exceptionWithHost: host
recordClass: recordClass
recordType: recordType
error: error];
}
if ([delegate respondsToSelector: @selector(resolver:
didResolveDomainName:socketAddresses:exception:)])
[delegate resolver: self
didResolveDomainName: host
socketAddresses: nil
exception: exception];
return;
}
if ([delegate respondsToSelector: @selector(resolver:
didResolveDomainName:socketAddresses:exception:)])
[delegate resolver: self
didResolveDomainName: host
method(target, selector, self, host, addresses, userContext,
nil);
socketAddresses: addresses
exception: nil];
return;
}
pool = objc_autoreleasePoolPush();
context = [[[OFDNSResolver_AsyncResolveSocketAddressesContext alloc]
initWithHost: host
target: target
selector: selector
context: userContext] autorelease];
delegate: delegate] autorelease];
switch (addressFamily) {
case OF_SOCKET_ADDRESS_FAMILY_IPV4:
#ifdef OF_HAVE_IPV6
case OF_SOCKET_ADDRESS_FAMILY_IPV6:
#endif
context->_expectedResponses = 1;
|
︙ | | |
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
|
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
|
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
+
-
+
-
-
-
-
-
-
+
-
-
+
+
-
+
|
break;
default:
@throw [OFInvalidArgumentException exception];
}
#ifdef OF_HAVE_IPV6
if (addressFamily == OF_SOCKET_ADDRESS_FAMILY_IPV6 ||
addressFamily == OF_SOCKET_ADDRESS_FAMILY_ANY)
[self asyncResolveHost: host
recordClass: OF_DNS_RESOURCE_RECORD_CLASS_IN
recordType: OF_DNS_RESOURCE_RECORD_TYPE_AAAA
runLoopMode: runLoopMode
target: context
selector: @selector(resolver:didResolveDomainName:
answerRecords:authorityRecords:
additionalRecords:context:
exception:)
context: [OFNumber numberWithInt:
addressFamily == OF_SOCKET_ADDRESS_FAMILY_ANY) {
OFNumber *recordTypeNumber =
[OFNumber numberWithInt: OF_DNS_RESOURCE_RECORD_TYPE_AAAA];
[self of_asyncResolveHost: host
recordClass: OF_DNS_RESOURCE_RECORD_CLASS_IN
recordType: OF_DNS_RESOURCE_RECORD_TYPE_AAAA
runLoopMode: runLoopMode
target: context
selector: @selector(resolver:
didResolveDomainName:
answerRecords:authorityRecords:
additionalRecords:context:
exception:)
context: recordTypeNumber];
OF_DNS_RESOURCE_RECORD_TYPE_AAAA]];
}
#endif
if (addressFamily == OF_SOCKET_ADDRESS_FAMILY_IPV4 ||
addressFamily == OF_SOCKET_ADDRESS_FAMILY_ANY)
[self asyncResolveHost: host
recordClass: OF_DNS_RESOURCE_RECORD_CLASS_IN
recordType: OF_DNS_RESOURCE_RECORD_TYPE_A
runLoopMode: runLoopMode
target: context
selector: @selector(resolver:didResolveDomainName:
answerRecords:authorityRecords:
additionalRecords:context:
exception:)
context: [OFNumber numberWithInt:
OF_DNS_RESOURCE_RECORD_TYPE_A]];
[self of_asyncResolveHost: host
recordClass: OF_DNS_RESOURCE_RECORD_CLASS_IN
recordType: OF_DNS_RESOURCE_RECORD_TYPE_A
runLoopMode: runLoopMode
target: context
selector: @selector(resolver:
didResolveDomainName:
answerRecords:authorityRecords:
additionalRecords:context:
exception:)
context: [OFNumber numberWithInt:
OF_DNS_RESOURCE_RECORD_TYPE_A]];
objc_autoreleasePoolPop(pool);
}
- (OFData *)resolveSocketAddressesForHost: (OFString *)host
addressFamily: (of_socket_address_family_t)
addressFamily
{
void *pool = objc_autoreleasePoolPush();
OFRunLoop *runLoop = [OFRunLoop currentRunLoop];
OFDNSResolver_ResolveSocketAddressesContext *context;
OFDNSResolver_ResolveSocketAddressesDelegate *delegate;
OFData *ret;
context = [[[OFDNSResolver_ResolveSocketAddressesContext
delegate = [[[OFDNSResolver_ResolveSocketAddressesDelegate
alloc] init] autorelease];
[self asyncResolveSocketAddressesForHost: host
addressFamily: addressFamily
runLoopMode: resolveRunLoopMode
target: context
delegate: delegate];
selector: @selector(resolver:
didResolveDomainName:
socketAddresses:context:
exception:)
context: nil];
while (!context->_done)
while (!delegate->_done)
[runLoop runMode: resolveRunLoopMode
beforeDate: nil];
/* Cleanup */
[runLoop runMode: resolveRunLoopMode
beforeDate: [OFDate date]];
if (context->_exception != nil)
@throw context->_exception;
if (delegate->_exception != nil)
@throw delegate->_exception;
ret = [context->_socketAddresses retain];
ret = [delegate->_socketAddresses retain];
objc_autoreleasePoolPop(pool);
return [ret autorelease];
}
- (void)close
|
︙ | | |
︙ | | |
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
-
-
+
+
|
Class of_tls_socket_class = Nil;
static of_run_loop_mode_t connectRunLoopMode = @"of_tcp_socket_connect_mode";
static OFString *defaultSOCKS5Host = nil;
static uint16_t defaultSOCKS5Port = 1080;
@interface OFTCPSocket_AsyncConnectContext: OFObject <OFTCPSocketDelegate,
OFTCPSocketDelegate_Private>
@interface OFTCPSocket_AsyncConnectDelegate: OFObject <OFTCPSocketDelegate,
OFTCPSocketDelegate_Private, OFDNSResolverDelegate>
{
OFTCPSocket *_socket;
OFString *_host;
uint16_t _port;
OFString *_SOCKS5Host;
uint16_t _SOCKS5Port;
id <OFTCPSocketDelegate> _delegate;
|
︙ | | |
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
-
-
-
-
-
-
+
|
port: (uint16_t)port
SOCKS5Host: (OFString *)SOCKS5Host
SOCKS5Port: (uint16_t)SOCKS5Port
block: (of_tcp_socket_async_connect_block_t)block;
#endif
- (void)didConnect;
- (void)tryNextAddressWithRunLoopMode: (of_run_loop_mode_t)runLoopMode;
- (void)resolver: (OFDNSResolver *)resolver
didResolveDomainName: (OFString *)domainName
socketAddresses: (OFData *)socketAddresses
context: (id)context
exception: (id)exception;
- (void)startWithRunLoopMode: (of_run_loop_mode_t)runLoopMode;
- (void)sendSOCKS5Request;
@end
@interface OFTCPSocket_ConnectDelegate: OFObject <OFTCPSocketDelegate>
{
@public
bool _done;
id _exception;
}
@end
@implementation OFTCPSocket_AsyncConnectContext
@implementation OFTCPSocket_AsyncConnectDelegate
- (instancetype)initWithSocket: (OFTCPSocket *)sock
host: (OFString *)host
port: (uint16_t)port
SOCKS5Host: (OFString *)SOCKS5Host
SOCKS5Port: (uint16_t)SOCKS5Port
delegate: (id <OFTCPSocketDelegate>)delegate
{
|
︙ | | |
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
|
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
-
|
[self didConnect];
}
- (void)resolver: (OFDNSResolver *)resolver
didResolveDomainName: (OFString *)domainName
socketAddresses: (OFData *)socketAddresses
context: (id)context
exception: (id)exception
{
if (exception != nil) {
_exception = [exception retain];
[self didConnect];
return;
}
|
︙ | | |
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
|
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
-
+
-
-
-
-
-
|
} @catch (OFInvalidFormatException *e) {
}
[[OFThread DNSResolver]
asyncResolveSocketAddressesForHost: host
addressFamily: OF_SOCKET_ADDRESS_FAMILY_ANY
runLoopMode: runLoopMode
target: self
delegate: self];
selector: @selector(resolver:
didResolveDomainName:
socketAddresses:context:
exception:)
context: nil];
}
- (void)sendSOCKS5Request
{
OFData *data = [OFData dataWithItems: "\x05\x01\x00"
count: 3];
|
︙ | | |
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
|
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
|
-
+
|
- (void)asyncConnectToHost: (OFString *)host
port: (uint16_t)port
runLoopMode: (of_run_loop_mode_t)runLoopMode
{
void *pool = objc_autoreleasePoolPush();
[[[[OFTCPSocket_AsyncConnectContext alloc]
[[[[OFTCPSocket_AsyncConnectDelegate alloc]
initWithSocket: self
host: host
port: port
SOCKS5Host: _SOCKS5Host
SOCKS5Port: _SOCKS5Port
delegate: _delegate] autorelease]
startWithRunLoopMode: runLoopMode];
|
︙ | | |
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
|
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
|
-
+
|
- (void)asyncConnectToHost: (OFString *)host
port: (uint16_t)port
runLoopMode: (of_run_loop_mode_t)runLoopMode
block: (of_tcp_socket_async_connect_block_t)block
{
void *pool = objc_autoreleasePoolPush();
[[[[OFTCPSocket_AsyncConnectContext alloc]
[[[[OFTCPSocket_AsyncConnectDelegate alloc]
initWithSocket: self
host: host
port: port
SOCKS5Host: _SOCKS5Host
SOCKS5Port: _SOCKS5Port
block: block] autorelease]
startWithRunLoopMode: runLoopMode];
|
︙ | | |