summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonathan Schleifer <js@heap.zone>2019-10-07 02:07:55 +0200
committerJonathan Schleifer <js@heap.zone>2019-10-07 02:07:55 +0200
commit97e121593865dfb7dbefc4716a4aaaa48384413c (patch)
treecb19de87f3c29bf29fceec6ae0044e87cb262ec0
parentdb0834bfd5be9b338a34b597db9e3e758f5a4112 (diff)
OFDNSResolver: Major refactor
This temporarily doesn't use the search domains anymore and makes CNAMEs slightly more inefficient. The next commits will fix both.
-rw-r--r--src/Makefile1
-rw-r--r--src/OFDNSQuery.h51
-rw-r--r--src/OFDNSQuery.m58
-rw-r--r--src/OFDNSResolver.h60
-rw-r--r--src/OFDNSResolver.m1038
-rw-r--r--src/OFDNSResourceRecord.h78
-rw-r--r--src/OFDNSResourceRecord.m184
-rw-r--r--src/OFDNSResponse.h46
-rw-r--r--src/OFDNSResponse.m50
-rw-r--r--src/OFHostAddressResolver.h54
-rw-r--r--src/OFHostAddressResolver.m343
-rw-r--r--src/OFTCPSocket.m22
-rw-r--r--src/OFUDPSocket.m4
-rw-r--r--src/ObjFW.h9
-rw-r--r--src/exceptions/Makefile3
-rw-r--r--src/exceptions/OFDNSQueryFailedException.h14
-rw-r--r--src/exceptions/OFDNSQueryFailedException.m67
-rw-r--r--src/exceptions/OFResolveHostFailedException.h78
-rw-r--r--src/exceptions/OFResolveHostFailedException.m67
-rw-r--r--utils/ofdns/OFDNS.m24
-rw-r--r--utils/ofhttp/OFHTTP.m8
-rw-r--r--utils/ofhttp/lang/de.json4
22 files changed, 1067 insertions, 1196 deletions
diff --git a/src/Makefile b/src/Makefile
index 7fb9e456..b68515ff 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -216,6 +216,7 @@ SRCS_FILES += OFFileURLHandler.m \
OFINIFileSettings.m
SRCS_SOCKETS += OFDNSResolverSettings.m \
OFHTTPURLHandler.m \
+ OFHostAddressResolver.m \
OFKernelEventObserver.m \
${OFEPOLLKERNELEVENTOBSERVER_M} \
${OFKQUEUEKERNELEVENTOBSERVER_M} \
diff --git a/src/OFDNSQuery.h b/src/OFDNSQuery.h
index 5c6101d0..84f72d23 100644
--- a/src/OFDNSQuery.h
+++ b/src/OFDNSQuery.h
@@ -20,6 +20,8 @@
OF_ASSUME_NONNULL_BEGIN
+@class OFString;
+
/*!
* @class OFDNSQuery OFDNSQuery.h ObjFW/OFDNSQuery.h
*
@@ -27,21 +29,21 @@ OF_ASSUME_NONNULL_BEGIN
*/
@interface OFDNSQuery: OFObject <OFCopying>
{
- OFString *_host;
- of_dns_resource_record_class_t _recordClass;
+ OFString *_domainName;
+ of_dns_class_t _DNSClass;
of_dns_resource_record_type_t _recordType;
OF_RESERVE_IVARS(4)
}
/*!
- * @brief The host to resolve.
+ * @brief The domain name of the query.
*/
-@property (readonly, nonatomic) OFString *host;
+@property (readonly, nonatomic) OFString *domainName;
/*!
- * @brief The record class of the query.
+ * @brief The DNS class of the query.
*/
-@property (readonly, nonatomic) of_dns_resource_record_class_t recordClass;
+@property (readonly, nonatomic) of_dns_class_t DNSClass;
/*!
* @brief The record type of the query.
@@ -49,45 +51,28 @@ OF_ASSUME_NONNULL_BEGIN
@property (readonly, nonatomic) of_dns_resource_record_type_t recordType;
/*!
- * @brief Creates a new, autoreleased OFDNSQuery with IN class and type ALL.
- *
- * @param host The host to resolve
- * @return A new, autoreleased OFDNSQuery
- */
-+ (instancetype)queryWithHost: (OFString *)host;
-
-/*!
* @brief Creates a new, autoreleased OFDNSQuery.
*
- * @param host The host to resolve
- * @param recordClass The record class of the query
+ * @param domainName The domain name to query
+ * @param DNSClass The DNS class of the query
* @param recordType The record type of the query
* @return A new, autoreleased OFDNSQuery
*/
-+ (instancetype)queryWithHost: (OFString *)host
- recordClass: (of_dns_resource_record_class_t)recordClass
- recordType: (of_dns_resource_record_type_t)recordType;
-
-/*!
- * @brief Initializes an already allocated OFDNSQuery with IN class and type
- * ALL.
- *
- * @param host The host to resolve
- * @return An initialized OFDNSQuery
- */
-- (instancetype)initWithHost: (OFString *)host;
++ (instancetype)queryWithDomainName: (OFString *)domainName
+ DNSClass: (of_dns_class_t)DNSClass
+ recordType: (of_dns_resource_record_type_t)recordType;
/*!
* @brief Initializes an already allocated OFDNSQuery.
*
- * @param host The host to resolve
- * @param recordClass The record class of the query
+ * @param domainName The domain name to query
+ * @param DNSClass The DNS class of the query
* @param recordType The record type of the query
* @return An initialized OFDNSQuery
*/
-- (instancetype)initWithHost: (OFString *)host
- recordClass: (of_dns_resource_record_class_t)recordClass
- recordType: (of_dns_resource_record_type_t)recordType
+- (instancetype)initWithDomainName: (OFString *)domainName
+ DNSClass: (of_dns_class_t)DNSClass
+ recordType: (of_dns_resource_record_type_t)recordType
OF_DESIGNATED_INITIALIZER;
- (instancetype)init OF_UNAVAILABLE;
diff --git a/src/OFDNSQuery.m b/src/OFDNSQuery.m
index 740d433c..de1b3ed2 100644
--- a/src/OFDNSQuery.m
+++ b/src/OFDNSQuery.m
@@ -21,39 +21,35 @@
#import "OFString.h"
@implementation OFDNSQuery
-@synthesize host = _host, recordClass = _recordClass, recordType = _recordType;
+@synthesize domainName = _domainName, DNSClass = _DNSClass;
+@synthesize recordType = _recordType;
-+ (instancetype)queryWithHost: (OFString *)host
++ (instancetype)queryWithDomainName: (OFString *)domainName
+ DNSClass: (of_dns_class_t)DNSClass
+ recordType: (of_dns_resource_record_type_t)recordType
{
- return [[[self alloc] initWithHost: host] autorelease];
+ return [[[self alloc] initWithDomainName: domainName
+ DNSClass: DNSClass
+ recordType: recordType] autorelease];
}
-+ (instancetype)queryWithHost: (OFString *)host
- recordClass: (of_dns_resource_record_class_t)recordClass
- recordType: (of_dns_resource_record_type_t)recordType
-{
- return [[[self alloc] initWithHost: host
- recordClass: recordClass
- recordType: recordType] autorelease];
-}
-
-- (instancetype)initWithHost: (OFString *)host
-{
- return [self initWithHost: host
- recordClass: OF_DNS_RESOURCE_RECORD_CLASS_IN
- recordType: OF_DNS_RESOURCE_RECORD_TYPE_ALL];
-}
-
-- (instancetype)initWithHost: (OFString *)host
- recordClass: (of_dns_resource_record_class_t)recordClass
- recordType: (of_dns_resource_record_type_t)recordType
+- (instancetype)initWithDomainName: (OFString *)domainName
+ DNSClass: (of_dns_class_t)DNSClass
+ recordType: (of_dns_resource_record_type_t)recordType
{
self = [super init];
@try {
- _host = [host copy];
- _recordClass = recordClass;
+ void *pool = objc_autoreleasePoolPush();
+
+ if (![domainName hasSuffix: @"."])
+ domainName = [domainName stringByAppendingString: @"."];
+
+ _domainName = [domainName copy];
+ _DNSClass = DNSClass;
_recordType = recordType;
+
+ objc_autoreleasePoolPop(pool);
} @catch (id e) {
[self release];
@throw e;
@@ -69,7 +65,7 @@
- (void)dealloc
{
- [_host release];
+ [_domainName release];
[super dealloc];
}
@@ -83,9 +79,10 @@
query = object;
- if (query->_host != _host && ![query->_host isEqual: _host])
+ if (query->_domainName != _domainName &&
+ ![query->_domainName isEqual: _domainName])
return false;
- if (query->_recordClass != _recordClass)
+ if (query->_DNSClass != _DNSClass)
return false;
if (query->_recordType != _recordType)
return false;
@@ -98,8 +95,8 @@
uint32_t hash;
OF_HASH_INIT(hash);
- OF_HASH_ADD_HASH(hash, _host.hash);
- OF_HASH_ADD(hash, _recordClass);
+ OF_HASH_ADD_HASH(hash, _domainName.hash);
+ OF_HASH_ADD(hash, _DNSClass);
OF_HASH_ADD(hash, _recordType);
OF_HASH_FINALIZE(hash);
@@ -114,8 +111,7 @@
- (OFString *)description
{
return [OFString stringWithFormat: @"<%@ %@ %@ %@>",
- self.className, _host,
- of_dns_resource_record_class_to_string(_recordClass),
+ self.className, _domainName, of_dns_class_to_string(_DNSClass),
of_dns_resource_record_type_to_string(_recordType)];
}
@end
diff --git a/src/OFDNSResolver.h b/src/OFDNSResolver.h
index 7869bc2c..0d2f49c8 100644
--- a/src/OFDNSResolver.h
+++ b/src/OFDNSResolver.h
@@ -28,7 +28,7 @@ OF_ASSUME_NONNULL_BEGIN
@class OFArray OF_GENERIC(ObjectType);
@class OFDNSResolver;
-@class OFDNSResolverQuery;
+@class OFDNSResolverContext;
@class OFDNSResolverSettings;
@class OFDate;
@class OFDictionary OF_GENERIC(KeyType, ObjectType);
@@ -75,33 +75,33 @@ typedef enum of_dns_resolver_error_t {
@protocol OFDNSResolverDelegate <OFObject>
@optional
/*!
- * @brief This method is called when a DNS resolver resolved a domain name.
+ * @brief This method is called when a DNS resolver performed a query.
*
* @param resolver The acting resolver
- * @param domainName The fully qualified domain name used to resolve the host
+ * @param query The query performed by the resolver
* @param response The response from the DNS server, or nil on error
* @param exception An exception that happened during resolving, or nil on
* success
*/
-- (void)resolver: (OFDNSResolver *)resolver
- didResolveDomainName: (OFString *)domainName
- response: (nullable OFDNSResponse *)response
- exception: (nullable id)exception;
+- (void)resolver: (OFDNSResolver *)resolver
+ didPerformQuery: (OFDNSQuery *)query
+ response: (nullable OFDNSResponse *)response
+ exception: (nullable id)exception;
/*!
- * @brief This method is called when a DNS resolver resolved a domain name to
- * socket addresses.
+ * @brief This method is called when a DNS resolver resolved a host to
+ * 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 host The host the resolver resolved
+ * @param addresses 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;
+- (void)resolver: (OFDNSResolver *)resolver
+ didResolveHost: (OFString *)host
+ addresses: (nullable OFData *)addresses
+ exception: (nullable id)exception;
@end
/*!
@@ -123,7 +123,7 @@ OF_SUBCLASSING_RESTRICTED
OFUDPSocket *_IPv6Socket;
#endif
char _buffer[OF_DNS_RESOLVER_BUFFER_LENGTH];
- OFMutableDictionary OF_GENERIC(OFNumber *, OFDNSResolverQuery *)
+ OFMutableDictionary OF_GENERIC(OFNumber *, OFDNSResolverContext *)
*_queries;
}
@@ -218,9 +218,8 @@ OF_SUBCLASSING_RESTRICTED
* @param host The host to resolve
* @param delegate The delegate to use for callbacks
*/
-- (void)asyncResolveSocketAddressesForHost: (OFString *)host
- delegate: (id <OFDNSResolverDelegate>)
- delegate;
+- (void)asyncResolveAddressesForHost: (OFString *)host
+ delegate: (id <OFDNSResolverDelegate>)delegate;
/*!
* @brief Asynchronously resolves the specified host to socket addresses.
@@ -229,11 +228,9 @@ OF_SUBCLASSING_RESTRICTED
* @param addressFamily The desired socket address family
* @param delegate The delegate to use for callbacks
*/
-- (void)asyncResolveSocketAddressesForHost: (OFString *)host
- addressFamily: (of_socket_address_family_t)
- addressFamily
- delegate: (id <OFDNSResolverDelegate>)
- delegate;
+- (void)asyncResolveAddressesForHost: (OFString *)host
+ addressFamily: (of_socket_address_family_t)addressFamily
+ delegate: (id <OFDNSResolverDelegate>)delegate;
/*!
* @brief Asynchronously resolves the specified host to socket addresses.
@@ -243,12 +240,10 @@ OF_SUBCLASSING_RESTRICTED
* @param runLoopMode The run loop mode in which to resolve
* @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
- delegate: (id <OFDNSResolverDelegate>)
- delegate;
+- (void)asyncResolveAddressesForHost: (OFString *)host
+ addressFamily: (of_socket_address_family_t)addressFamily
+ runLoopMode: (of_run_loop_mode_t)runLoopMode
+ delegate: (id <OFDNSResolverDelegate>)delegate;
/*!
* @brief Synchronously resolves the specified host to socket addresses.
@@ -257,9 +252,8 @@ OF_SUBCLASSING_RESTRICTED
* @param addressFamily The desired socket address family
* @return OFData containing several of_socket_address_t
*/
-- (OFData *)resolveSocketAddressesForHost: (OFString *)host
- addressFamily: (of_socket_address_family_t)
- addressFamily;
+- (OFData *)resolveAddressesForHost: (OFString *)host
+ addressFamily: (of_socket_address_family_t)addressFamily;
/*!
* @brief Closes all sockets and cancels all ongoing queries.
diff --git a/src/OFDNSResolver.m b/src/OFDNSResolver.m
index 5bff368d..150356ac 100644
--- a/src/OFDNSResolver.m
+++ b/src/OFDNSResolver.m
@@ -27,6 +27,7 @@
#import "OFData.h"
#import "OFDate.h"
#import "OFDictionary.h"
+#import "OFHostAddressResolver.h"
#import "OFNumber.h"
#import "OFPair.h"
#import "OFString.h"
@@ -64,114 +65,30 @@
* - Fallback to TCP
*/
-static const of_run_loop_mode_t resolveRunLoopMode =
- @"of_dns_resolver_resolve_mode";
+@interface OFDNSResolver () <OFUDPSocketDelegate>
+- (void)of_contextTimedOut: (OFDNSResolverContext *)context;
+@end
-@interface OFDNSResolverQuery: OFObject
+@interface OFDNSResolverContext: OFObject
{
@public
OFDNSQuery *_query;
- OFString *_domainName;
OFNumber *_ID;
OFDNSResolverSettings *_settings;
- size_t _nameServersIndex, _searchDomainsIndex;
+ size_t _nameServersIndex;
unsigned int _attempt;
- id _target;
- SEL _selector;
- id _context;
+ id <OFDNSResolverDelegate> _delegate;
OFData *_queryData;
of_socket_address_t _usedNameServer;
OFTimer *_cancelTimer;
}
- (instancetype)initWithQuery: (OFDNSQuery *)query
- domainName: (OFString *)domainName
ID: (OFNumber *)ID
settings: (OFDNSResolverSettings *)settings
- nameServersIndex: (size_t)nameServersIndex
- searchDomainsIndex: (size_t)searchDomainsIndex
- target: (id)target
- selector: (SEL)selector
- context: (id)context;
-@end
-
-@interface OFDNSResolverAsyncResolveSocketAddressesContext: OFObject
-{
- OFString *_host;
- id _delegate;
- OFMutableArray OF_GENERIC(OF_KINDOF(OFDNSResourceRecord *)) *_records;
- OFDNSResolver *_resolver;
- OFString *_domainName;
-@public
- unsigned int _expectedResponses;
-}
-
-- (instancetype)initWithHost: (OFString *)host
- delegate: (id)delegate;
-- (bool)parseRecords: (OFArray *)records
- response: (OFDNSResponse *)response
- recordType: (of_dns_resource_record_type_t)recordType
- recursion: (unsigned int)recursion
- result: (OFMutableArray *)result;
-- (void)resolveCNAME: (OFCNAMEDNSResourceRecord *)CNAME
- response: (OFDNSResponse *)response
- recordType: (of_dns_resource_record_type_t)recordType
- recursion: (unsigned int)recursion
- result: (OFMutableArray *)result;
-- (void)resolver: (OFDNSResolver *)resolver
- didResolveCNAME: (OFString *)CNAME
- response: (OFDNSResponse *)response
- context: (OFNumber *)context
- exception: (id)exception;
-- (void)done;
-- (void)resolver: (OFDNSResolver *)resolver
- didResolveDomainName: (OFString *)domainName
- response: (OFDNSResponse *)response
- context: (OFNumber *)context
- exception: (id)exception;
-@end
-
-@interface OFDNSResolverResolveSocketAddressesDelegate: OFObject
- <OFDNSResolverDelegate>
-{
-@public
- bool _done;
- OFData *_socketAddresses;
- id _exception;
-}
-@end
-
-@interface OFDNSResolver () <OFUDPSocketDelegate>
-- (void)of_asyncPerformQuery: (OFDNSQuery *)query
- 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_sendQuery: (OFDNSResolverQuery *)query
- runLoopMode: (of_run_loop_mode_t)runLoopMode;
-- (void)of_queryWithIDTimedOut: (OFDNSResolverQuery *)query;
+ delegate: (id <OFDNSResolverDelegate>)delegate;
@end
-static bool
-isFQDN(OFString *host, OFDNSResolverSettings *settings)
-{
- 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 >= settings->_minNumberOfDotsInAbsoluteName);
-}
-
static OFString *
parseString(const unsigned char *buffer, size_t length, size_t *i)
{
@@ -252,12 +169,12 @@ parseName(const unsigned char *buffer, size_t length, size_t *i,
}
static OF_KINDOF(OFDNSResourceRecord *)
-parseResourceRecord(OFString *name, of_dns_resource_record_class_t recordClass,
+parseResourceRecord(OFString *name, of_dns_class_t DNSClass,
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) {
+ DNSClass == OF_DNS_CLASS_IN) {
of_socket_address_t address;
if (dataLength != 4)
@@ -284,7 +201,7 @@ parseResourceRecord(OFString *name, of_dns_resource_record_class_t recordClass,
return [[[OFNSDNSResourceRecord alloc]
initWithName: name
- recordClass: recordClass
+ DNSClass: DNSClass
authoritativeHost: authoritativeHost
TTL: TTL] autorelease];
} else if (recordType == OF_DNS_RESOURCE_RECORD_TYPE_CNAME) {
@@ -297,7 +214,7 @@ parseResourceRecord(OFString *name, of_dns_resource_record_class_t recordClass,
return [[[OFCNAMEDNSResourceRecord alloc]
initWithName: name
- recordClass: recordClass
+ DNSClass: DNSClass
alias: alias
TTL: TTL] autorelease];
} else if (recordType == OF_DNS_RESOURCE_RECORD_TYPE_SOA) {
@@ -332,7 +249,7 @@ parseResourceRecord(OFString *name, of_dns_resource_record_class_t recordClass,
return [[[OFSOADNSResourceRecord alloc]
initWithName: name
- recordClass: recordClass
+ DNSClass: DNSClass
primaryNameServer: primaryNameServer
responsiblePerson: responsiblePerson
serialNumber: serialNumber
@@ -351,7 +268,7 @@ parseResourceRecord(OFString *name, of_dns_resource_record_class_t recordClass,
return [[[OFPTRDNSResourceRecord alloc]
initWithName: name
- recordClass: recordClass
+ DNSClass: DNSClass
domainName: domainName
TTL: TTL] autorelease];
} else if (recordType == OF_DNS_RESOURCE_RECORD_TYPE_HINFO) {
@@ -369,7 +286,7 @@ parseResourceRecord(OFString *name, of_dns_resource_record_class_t recordClass,
return [[[OFHINFODNSResourceRecord alloc]
initWithName: name
- recordClass: recordClass
+ DNSClass: DNSClass
CPU: CPU
OS: OS
TTL: TTL] autorelease];
@@ -392,7 +309,7 @@ parseResourceRecord(OFString *name, of_dns_resource_record_class_t recordClass,
return [[[OFMXDNSResourceRecord alloc]
initWithName: name
- recordClass: recordClass
+ DNSClass: DNSClass
preference: preference
mailExchange: mailExchange
TTL: TTL] autorelease];
@@ -402,7 +319,7 @@ parseResourceRecord(OFString *name, of_dns_resource_record_class_t recordClass,
return [[[OFTXTDNSResourceRecord alloc]
initWithName: name
- recordClass: recordClass
+ DNSClass: DNSClass
textData: textData
TTL: TTL] autorelease];
} else if (recordType == OF_DNS_RESOURCE_RECORD_TYPE_RP) {
@@ -422,12 +339,12 @@ parseResourceRecord(OFString *name, of_dns_resource_record_class_t recordClass,
return [[[OFRPDNSResourceRecord alloc]
initWithName: name
- recordClass: recordClass
+ DNSClass: DNSClass
mailbox: mailbox
TXTDomainName: TXTDomainName
TTL: TTL] autorelease];
} else if (recordType == OF_DNS_RESOURCE_RECORD_TYPE_AAAA &&
- recordClass == OF_DNS_RESOURCE_RECORD_CLASS_IN) {
+ DNSClass == OF_DNS_CLASS_IN) {
of_socket_address_t address;
if (dataLength != 16)
@@ -449,7 +366,7 @@ parseResourceRecord(OFString *name, of_dns_resource_record_class_t recordClass,
address: &address
TTL: TTL] autorelease];
} else if (recordType == OF_DNS_RESOURCE_RECORD_TYPE_SRV &&
- recordClass == OF_DNS_RESOURCE_RECORD_CLASS_IN) {
+ DNSClass == OF_DNS_CLASS_IN) {
uint16_t priority, weight, port;
size_t j;
OFString *target;
@@ -477,7 +394,7 @@ parseResourceRecord(OFString *name, of_dns_resource_record_class_t recordClass,
} else
return [[[OFDNSResourceRecord alloc]
initWithName: name
- recordClass: recordClass
+ DNSClass: DNSClass
recordType: recordType
TTL: TTL] autorelease];
}
@@ -493,7 +410,7 @@ parseSection(const unsigned char *buffer, size_t length, size_t *i,
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_class_t DNSClass;
of_dns_resource_record_type_t recordType;
uint32_t TTL;
uint16_t dataLength;
@@ -503,7 +420,7 @@ parseSection(const unsigned char *buffer, size_t length, size_t *i,
@throw [OFTruncatedDataException exception];
recordType = (buffer[*i] << 16) | buffer[*i + 1];
- recordClass = (buffer[*i + 2] << 16) | buffer[*i + 3];
+ DNSClass = (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];
@@ -513,7 +430,7 @@ parseSection(const unsigned char *buffer, size_t length, size_t *i,
if (*i + dataLength > length)
@throw [OFTruncatedDataException exception];
- record = parseResourceRecord(name, recordClass, recordType, TTL,
+ record = parseResourceRecord(name, DNSClass, recordType, TTL,
buffer, length, *i, dataLength);
*i += dataLength;
@@ -537,28 +454,11 @@ parseSection(const unsigned char *buffer, size_t length, size_t *i,
return ret;
}
-static void
-callback(id target, SEL selector, OFDNSResolver *resolver, OFString *domainName,
- OFDNSResponse *response, id context, id exception)
-{
- void (*method)(id, SEL, OFDNSResolver *, OFString *, OFDNSResponse *,
- id, id) = (void (*)(id, SEL, OFDNSResolver *, OFString *,
- OFDNSResponse *, id, id))[target methodForSelector: selector];
-
- method(target, selector, resolver, domainName, response, context,
- exception);
-}
-
-@implementation OFDNSResolverQuery
+@implementation OFDNSResolverContext
- (instancetype)initWithQuery: (OFDNSQuery *)query
- domainName: (OFString *)domainName
ID: (OFNumber *)ID
settings: (OFDNSResolverSettings *)settings
- nameServersIndex: (size_t)nameServersIndex
- searchDomainsIndex: (size_t)searchDomainsIndex
- target: (id)target
- selector: (SEL)selector
- context: (id)context
+ delegate: (id <OFDNSResolverDelegate>)delegate
{
self = [super init];
@@ -568,14 +468,9 @@ callback(id target, SEL selector, OFDNSResolver *resolver, OFString *domainName,
uint16_t tmp;
_query = [query copy];
- _domainName = [domainName copy];
_ID = [ID retain];
_settings = [settings copy];
- _nameServersIndex = nameServersIndex;
- _searchDomainsIndex = searchDomainsIndex;
- _target = [target retain];
- _selector = selector;
- _context = [context retain];
+ _delegate = [delegate retain];
queryData = [OFMutableData dataWithCapacity: 512];
@@ -602,7 +497,7 @@ callback(id target, SEL selector, OFDNSResolver *resolver, OFString *domainName,
/* QNAME */
for (OFString *component in
- [_domainName componentsSeparatedByString: @"."]) {
+ [_query.domainName componentsSeparatedByString: @"."]) {
size_t length = component.UTF8StringLength;
uint8_t length8;
@@ -621,7 +516,7 @@ callback(id target, SEL selector, OFDNSResolver *resolver, OFString *domainName,
count: 2];
/* QCLASS */
- tmp = OF_BSWAP16_IF_LE(_query.recordClass);
+ tmp = OF_BSWAP16_IF_LE(_query.DNSClass);
[queryData addItems: &tmp
count: 2];
@@ -641,11 +536,9 @@ callback(id target, SEL selector, OFDNSResolver *resolver, OFString *domainName,
- (void)dealloc
{
[_query release];
- [_domainName release];
[_ID release];
[_settings release];
- [_target release];
- [_context release];
+ [_delegate release];
[_queryData release];
[_cancelTimer release];
@@ -653,300 +546,6 @@ callback(id target, SEL selector, OFDNSResolver *resolver, OFString *domainName,
}
@end
-@implementation OFDNSResolverAsyncResolveSocketAddressesContext
-- (instancetype)initWithHost: (OFString *)host
- delegate: (id)delegate
-{
- self = [super init];
-
- @try {
- _host = [host copy];
- _delegate = [delegate retain];
-
- _records = [[OFMutableArray alloc] init];
- } @catch (id e) {
- [self release];
- @throw e;
- }
-
- return self;
-}
-
-- (void)dealloc
-{
- [_host release];
- [_delegate release];
- [_records release];
- [_resolver release];
- [_domainName release];
-
- [super dealloc];
-}
-
-- (bool)parseRecords: (OFArray *)records
- response: (OFDNSResponse *)response
- 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
- response: response
- recordType: recordType
- recursion: recursion
- result: result];
- found = true;
- }
- }
-
- return found;
-}
-
-- (void)resolveCNAME: (OFCNAMEDNSResourceRecord *)CNAME
- response: (OFDNSResponse *)response
- 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: [response.answerRecords objectForKey: alias]
- response: response
- recordType: recordType
- recursion: recursion - 1
- result: result])
- found = true;
-
- if ([self parseRecords: [response.additionalRecords objectForKey: alias]
- response: response
- recordType: recordType
- recursion: recursion - 1
- result: result])
- found = true;
-
- if (!found) {
- of_run_loop_mode_t runLoopMode =
- [OFRunLoop currentRunLoop].currentMode;
- OFNumber *recordTypeNumber =
- [OFNumber numberWithInt: recordType];
- OFDNSQuery *query;
-
- _expectedResponses++;
-
- [result addObject:
- [OFPair pairWithFirstObject: CNAME
- secondObject: recordTypeNumber]];
-
- query = [OFDNSQuery
- queryWithHost: alias
- recordClass: OF_DNS_RESOURCE_RECORD_CLASS_IN
- recordType: recordType];
- [_resolver of_asyncPerformQuery: query
- settings: nil
- nameServersIndex: 0
- searchDomainsIndex: 0
- runLoopMode: runLoopMode
- target: self
- selector: @selector(resolver:
- didResolveCNAME:response:
- context:exception:)
- context: recordTypeNumber];
- }
-}
-
-- (void)resolver: (OFDNSResolver *)resolver
- didResolveCNAME: (OFString *)CNAME
- response: (OFDNSResponse *)response
- context: (OFNumber *)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: [response.answerRecords objectForKey: CNAME]
- response: response
- recordType: recordType
- recursion: CNAME_RECURSION
- result: records])
- found = true;
-
- if ([self parseRecords: [response.additionalRecords objectForKey: CNAME]
- response: response
- 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
-{
- 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) {
- OFDNSQuery *query = [OFDNSQuery
- queryWithHost: _host
- recordClass: OF_DNS_RESOURCE_RECORD_CLASS_IN
- recordType: 0];
-
- exception = [OFDNSQueryFailedException
- exceptionWithQuery: query
- error: OF_DNS_RESOLVER_ERROR_UNKNOWN];
- }
-
- if ([_delegate respondsToSelector: @selector(
- resolver:didResolveDomainName:socketAddresses:exception:)])
- [_delegate resolver: _resolver
- didResolveDomainName: _domainName
- socketAddresses: (exception == nil ? addresses : nil)
- exception: exception];
-}
-
-- (void)resolver: (OFDNSResolver *)resolver
- didResolveDomainName: (OFString *)domainName
- response: (OFDNSResponse *)response
- context: (OFNumber *)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 in between? */
- return;
- } else
- _domainName = [domainName copy];
-
- if (exception != nil) {
- if (_expectedResponses == 0)
- [self done];
-
- return;
- }
-
- [self parseRecords: [response.answerRecords objectForKey: _domainName]
- response: response
- recordType: recordType
- recursion: CNAME_RECURSION
- result: _records];
-
- if (_expectedResponses == 0)
- [self done];
-}
-@end
-
-@implementation OFDNSResolverResolveSocketAddressesDelegate
-- (void)dealloc
-{
- [_socketAddresses release];
- [_exception release];
-
- [super dealloc];
-}
-
-- (void)resolver: (OFDNSResolver *)resolver
- didResolveDomainName: (OFString *)domainName
- socketAddresses: (OFData *)socketAddresses
- exception: (id)exception
-{
- _socketAddresses = [socketAddresses retain];
- _exception = [exception retain];
- _done = true;
-}
-@end
-
@implementation OFDNSResolver
#ifdef OF_AMIGAOS
+ (void)initialize
@@ -1091,134 +690,31 @@ callback(id target, SEL selector, OFDNSResolver *resolver, OFString *domainName,
_settings->_configReloadInterval = configReloadInterval;
}
-- (void)of_asyncPerformQuery: (OFDNSQuery *)query
- 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 *pool = objc_autoreleasePoolPush();
- OFNumber *ID;
- OFString *host, *domainName;
- OFDNSResolverQuery *resolverQuery;
-
- if (settings == nil) {
- [_settings reload];
- settings = _settings;
- }
-
- /* Random, unused ID */
- do {
- ID = [OFNumber numberWithUInt16: (uint16_t)of_random()];
- } while ([_queries objectForKey: ID] != nil);
-
- host = query.host;
- if (isFQDN(host, settings)) {
- domainName = host;
-
- if (![domainName hasSuffix: @"."])
- domainName = [domainName stringByAppendingString: @"."];
- } else {
- OFString *searchDomain = [settings->_searchDomains
- objectAtIndex: searchDomainsIndex];
-
- domainName = [OFString stringWithFormat: @"%@.%@.",
- host, searchDomain];
- }
-
- if (domainName.UTF8StringLength > 253)
- @throw [OFOutOfRangeException exception];
-
- resolverQuery = [[[OFDNSResolverQuery alloc]
- initWithQuery: query
- domainName: domainName
- ID: ID
- settings: settings
- nameServersIndex: nameServersIndex
- searchDomainsIndex: searchDomainsIndex
- target: target
- selector: selector
- context: context] autorelease];
- [_queries setObject: resolverQuery
- forKey: ID];
-
- [self of_sendQuery: resolverQuery
- runLoopMode: runLoopMode];
-
- objc_autoreleasePoolPop(pool);
-}
-
-- (void)of_resolver: (OFDNSResolver *)resolver
- didResolveDomainName: (OFString *)domainName
- response: (OFDNSResponse *)response
- context: (id)delegate
- exception: (id)exception
-{
- if ([delegate respondsToSelector: @selector(resolver:
- didResolveDomainName:response:exception:)])
- [delegate resolver: resolver
- didResolveDomainName: domainName
- response: response
- exception: exception];
-}
-
-- (void)asyncPerformQuery: (OFDNSQuery *)query
- delegate: (id <OFDNSResolverDelegate>)delegate
-{
- [self of_asyncPerformQuery: query
- settings: nil
- nameServersIndex: 0
- searchDomainsIndex: 0
- runLoopMode: of_run_loop_mode_default
- target: self
- selector: @selector(of_resolver:didResolveDomainName:
- response:context:exception:)
- context: delegate];
-}
-
-- (void)asyncPerformQuery: (OFDNSQuery *)query
- runLoopMode: (of_run_loop_mode_t)runLoopMode
- delegate: (id <OFDNSResolverDelegate>)delegate
-{
- [self of_asyncPerformQuery: query
- settings: nil
- nameServersIndex: 0
- searchDomainsIndex: 0
- runLoopMode: runLoopMode
- target: self
- selector: @selector(of_resolver:didResolveDomainName:
- response:context:exception:)
- context: delegate];
-}
-
-- (void)of_sendQuery: (OFDNSResolverQuery *)query
- runLoopMode: (of_run_loop_mode_t)runLoopMode
+- (void)of_sendQueryForContext: (OFDNSResolverContext *)context
+ runLoopMode: (of_run_loop_mode_t)runLoopMode
{
OFUDPSocket *sock;
OFString *nameServer;
- [query->_cancelTimer invalidate];
- [query->_cancelTimer release];
- query->_cancelTimer = nil;
- query->_cancelTimer = [[OFTimer alloc]
+ [context->_cancelTimer invalidate];
+ [context->_cancelTimer release];
+ context->_cancelTimer = nil;
+ context->_cancelTimer = [[OFTimer alloc]
initWithFireDate: [OFDate dateWithTimeIntervalSinceNow:
- query->_settings->_timeout]
- interval: query->_settings->_timeout
+ context->_settings->_timeout]
+ interval: context->_settings->_timeout
target: self
- selector: @selector(of_queryWithIDTimedOut:)
- object: query
+ selector: @selector(of_contextTimedOut:)
+ object: context
repeats: false];
- [[OFRunLoop currentRunLoop] addTimer: query->_cancelTimer
+ [[OFRunLoop currentRunLoop] addTimer: context->_cancelTimer
forMode: runLoopMode];
- nameServer = [query->_settings->_nameServers
- objectAtIndex: query->_nameServersIndex];
- query->_usedNameServer = of_socket_address_parse_ip(nameServer, 53);
+ nameServer = [context->_settings->_nameServers
+ objectAtIndex: context->_nameServersIndex];
+ context->_usedNameServer = of_socket_address_parse_ip(nameServer, 53);
- switch (query->_usedNameServer.family) {
+ switch (context->_usedNameServer.family) {
#ifdef OF_HAVE_IPV6
case OF_SOCKET_ADDRESS_FAMILY_IPV6:
if (_IPv6Socket == nil) {
@@ -1230,8 +726,6 @@ callback(id target, SEL selector, OFDNSResolver *resolver, OFString *domainName,
extraType: SOCK_DNS];
_IPv6Socket.blocking = false;
_IPv6Socket.delegate = self;
- [_IPv6Socket asyncReceiveIntoBuffer: _buffer
- length: BUFFER_LENGTH];
}
sock = _IPv6Socket;
@@ -1247,8 +741,6 @@ callback(id target, SEL selector, OFDNSResolver *resolver, OFString *domainName,
extraType: SOCK_DNS];
_IPv4Socket.blocking = false;
_IPv4Socket.delegate = self;
- [_IPv4Socket asyncReceiveIntoBuffer: _buffer
- length: BUFFER_LENGTH];
}
sock = _IPv4Socket;
@@ -1257,36 +749,75 @@ callback(id target, SEL selector, OFDNSResolver *resolver, OFString *domainName,
@throw [OFInvalidArgumentException exception];
}
- [sock asyncSendData: query->_queryData
- receiver: &query->_usedNameServer
+ [sock asyncSendData: context->_queryData
+ receiver: &context->_usedNameServer
runLoopMode: runLoopMode];
+ [sock asyncReceiveIntoBuffer: _buffer
+ length: BUFFER_LENGTH
+ runLoopMode: runLoopMode];
}
-- (void)of_queryWithIDTimedOut: (OFDNSResolverQuery *)query
+- (void)asyncPerformQuery: (OFDNSQuery *)query
+ delegate: (id <OFDNSResolverDelegate>)delegate
{
- OFDNSQueryFailedException *exception;
+ [self asyncPerformQuery: query
+ runLoopMode: of_run_loop_mode_default
+ delegate: delegate];
+}
- if (query == nil)
- return;
+- (void)asyncPerformQuery: (OFDNSQuery *)query
+ runLoopMode: (of_run_loop_mode_t)runLoopMode
+ delegate: (id <OFDNSResolverDelegate>)delegate
+{
+ void *pool = objc_autoreleasePoolPush();
+ OFNumber *ID;
+ OFDNSResolverContext *context;
+
+ /* Random, unused ID */
+ do {
+ ID = [OFNumber numberWithUInt16: (uint16_t)of_random()];
+ } while ([_queries objectForKey: ID] != nil);
+
+ if (query.domainName.UTF8StringLength > 253)
+ @throw [OFOutOfRangeException exception];
- if (query->_nameServersIndex + 1 <
- query->_settings->_nameServers.count) {
- query->_nameServersIndex++;
- [self of_sendQuery: query
- runLoopMode: [OFRunLoop currentRunLoop].currentMode];
+ context = [[[OFDNSResolverContext alloc]
+ initWithQuery: query
+ ID: ID
+ settings: _settings
+ delegate: delegate] autorelease];
+ [_queries setObject: context
+ forKey: ID];
+
+ [self of_sendQueryForContext: context
+ runLoopMode: runLoopMode];
+
+ objc_autoreleasePoolPop(pool);
+}
+
+- (void)of_contextTimedOut: (OFDNSResolverContext *)context
+{
+ of_run_loop_mode_t runLoopMode = [OFRunLoop currentRunLoop].currentMode;
+ OFDNSQueryFailedException *exception;
+
+ if (context->_nameServersIndex + 1 <
+ context->_settings->_nameServers.count) {
+ context->_nameServersIndex++;
+ [self of_sendQueryForContext: context
+ runLoopMode: runLoopMode];
return;
}
- if (query->_attempt < query->_settings->_maxAttempts) {
- query->_attempt++;
- query->_nameServersIndex = 0;
- [self of_sendQuery: query
- runLoopMode: [OFRunLoop currentRunLoop].currentMode];
+ if (context->_attempt < context->_settings->_maxAttempts) {
+ context->_attempt++;
+ context->_nameServersIndex = 0;
+ [self of_sendQueryForContext: context
+ runLoopMode: runLoopMode];
return;
}
- query = [[query retain] autorelease];
- [_queries removeObjectForKey: query->_ID];
+ context = [[context retain] autorelease];
+ [_queries removeObjectForKey: context->_ID];
/*
* Cancel any pending queries, to avoid a send being still pending and
@@ -1302,11 +833,15 @@ callback(id target, SEL selector, OFDNSResolver *resolver, OFString *domainName,
#endif
exception = [OFDNSQueryFailedException
- exceptionWithQuery: query->_query
+ exceptionWithQuery: context->_query
error: OF_DNS_RESOLVER_ERROR_TIMEOUT];
- callback(query->_target, query->_selector, self, query->_domainName,
- nil, query->_context, exception);
+ if ([context->_delegate respondsToSelector:
+ @selector(resolver:didPerformQuery:response:exception:)])
+ [context->_delegate resolver: self
+ didPerformQuery: context->_query
+ response: nil
+ exception: exception];
}
- (bool)socket: (OFUDPSocket *)sock
@@ -1320,27 +855,27 @@ callback(id target, SEL selector, OFDNSResolver *resolver, OFString *domainName,
OFDictionary *additionalRecords = nil;
OFDNSResponse *response = nil;
OFNumber *ID;
- OFDNSResolverQuery *query;
+ OFDNSResolverContext *context;
if (exception != nil)
return true;
if (length < 2)
- /* We can't get the ID to get the query. Ignore packet. */
+ /* We can't get the ID to get the context. Ignore packet. */
return true;
ID = [OFNumber numberWithUInt16: (buffer[0] << 8) | buffer[1]];
- query = [[[_queries objectForKey: ID] retain] autorelease];
+ context = [[[_queries objectForKey: ID] retain] autorelease];
- if (query == nil)
+ if (context == nil)
return true;
- if (!of_socket_address_equal(sender, &query->_usedNameServer))
+ if (!of_socket_address_equal(sender, &context->_usedNameServer))
return true;
- [query->_cancelTimer invalidate];
- [query->_cancelTimer release];
- query->_cancelTimer = nil;
+ [context->_cancelTimer invalidate];
+ [context->_cancelTimer release];
+ context->_cancelTimer = nil;
[_queries removeObjectForKey: ID];
@try {
@@ -1354,11 +889,11 @@ callback(id target, SEL selector, OFDNSResolver *resolver, OFString *domainName,
if (length < 12)
@throw [OFTruncatedDataException exception];
- if (query->_queryData.itemSize != 1 ||
- query->_queryData.count < 12)
+ if (context->_queryData.itemSize != 1 ||
+ context->_queryData.count < 12)
@throw [OFInvalidArgumentException exception];
- queryDataBuffer = query->_queryData.items;
+ queryDataBuffer = context->_queryData.items;
/* QR */
if ((buffer[2] & 0x80) == 0)
@@ -1401,33 +936,22 @@ callback(id target, SEL selector, OFDNSResolver *resolver, OFString *domainName,
}
if (tryNextNameServer) {
- if (query->_searchDomainsIndex + 1 <
- query->_settings->_searchDomains.count) {
+ if (context->_nameServersIndex + 1 <
+ context->_settings->_nameServers.count) {
of_run_loop_mode_t runLoopMode =
[OFRunLoop currentRunLoop].currentMode;
- size_t nameServersIndex =
- query->_nameServersIndex + 1;
- size_t searchDomainsIndex =
- query->_searchDomainsIndex + 1;
-
- query->_searchDomainsIndex++;
-
- [self of_asyncPerformQuery: query->_query
- settings: query->_settings
- nameServersIndex: nameServersIndex
- searchDomainsIndex: searchDomainsIndex
- runLoopMode: runLoopMode
- target: query->_target
- selector: query->_selector
- context: query->_context];
-
- return true;
+
+ context->_nameServersIndex++;
+
+ [self of_sendQueryForContext: context
+ runLoopMode: runLoopMode];
+ return false;
}
}
if (buffer[3] & 0x0F)
@throw [OFDNSQueryFailedException
- exceptionWithQuery: query->_query
+ exceptionWithQuery: context->_query
error: error];
numQuestions = (buffer[4] << 8) | buffer[5];
@@ -1454,279 +978,87 @@ callback(id target, SEL selector, OFDNSResolver *resolver, OFString *domainName,
additionalRecords = parseSection(buffer, length, &i,
numAdditionalRecords);
response = [OFDNSResponse
- responseWithAnswerRecords: answerRecords
- authorityRecords: authorityRecords
- additionalRecords: additionalRecords];
+ responseWithDomainName: context->_query.domainName
+ answerRecords: answerRecords
+ authorityRecords: authorityRecords
+ additionalRecords: additionalRecords];
} @catch (id e) {
- callback(query->_target, query->_selector, self,
- query->_domainName, nil, query->_context, e);
- return true;
+ exception = e;
}
- callback(query->_target, query->_selector, self, query->_domainName,
- response, query->_context, nil);
+ if ([context->_delegate respondsToSelector:
+ @selector(resolver:didPerformQuery:response:exception:)])
+ [context->_delegate resolver: self
+ didPerformQuery: context->_query
+ response: response
+ exception: exception];
- return true;
+ return false;
}
-- (void)asyncResolveSocketAddressesForHost: (OFString *)host
- delegate: (id <OFDNSResolverDelegate>)delegate
+- (void)asyncResolveAddressesForHost: (OFString *)host
+ delegate: (id <OFDNSResolverDelegate>)delegate
{
- [self asyncResolveSocketAddressesForHost: host
- addressFamily: OF_SOCKET_ADDRESS_FAMILY_ANY
- runLoopMode: of_run_loop_mode_default
- delegate: delegate];
+ [self asyncResolveAddressesForHost: host
+ addressFamily: OF_SOCKET_ADDRESS_FAMILY_ANY
+ runLoopMode: of_run_loop_mode_default
+ delegate: delegate];
}
-- (void)asyncResolveSocketAddressesForHost: (OFString *)host
- addressFamily: (of_socket_address_family_t)
- addressFamily
- delegate: (id <OFDNSResolverDelegate>)delegate
+- (void)asyncResolveAddressesForHost: (OFString *)host
+ addressFamily: (of_socket_address_family_t)addressFamily
+ delegate: (id <OFDNSResolverDelegate>)delegate
{
- [self asyncResolveSocketAddressesForHost: host
- addressFamily: addressFamily
- runLoopMode: of_run_loop_mode_default
- delegate: delegate];
+ [self asyncResolveAddressesForHost: host
+ addressFamily: addressFamily
+ runLoopMode: of_run_loop_mode_default
+ delegate: delegate];
}
-- (void)asyncResolveSocketAddressesForHost: (OFString *)host
- addressFamily: (of_socket_address_family_t)
- addressFamily
- runLoopMode: (of_run_loop_mode_t)runLoopMode
- delegate: (id <OFDNSResolverDelegate>)delegate
+- (void)asyncResolveAddressesForHost: (OFString *)host
+ addressFamily: (of_socket_address_family_t)addressFamily
+ runLoopMode: (of_run_loop_mode_t)runLoopMode
+ delegate: (id <OFDNSResolverDelegate>)delegate
{
- OFArray OF_GENERIC(OFString *) *aliases;
- void *pool;
- OFDNSResolverAsyncResolveSocketAddressesContext *context;
-
- @try {
- of_socket_address_t address =
- of_socket_address_parse_ip(host, 0);
- OFData *addresses = nil;
- id exception = nil;
-
- if (addressFamily == OF_SOCKET_ADDRESS_FAMILY_ANY ||
- addressFamily == address.family)
- addresses = [OFData dataWithItems: &address
- itemSize: sizeof(address)
- count: 1];
- else
- exception = [OFInvalidArgumentException exception];
-
- if ([delegate respondsToSelector: @selector(resolver:
- didResolveDomainName:socketAddresses:exception:)]) {
- OFTimer *timer = [OFTimer
- timerWithTimeInterval: 0
- target: delegate
- selector: @selector(resolver:
- didResolveDomainName:
- socketAddresses:
- exception:)
- object: self
- object: host
- object: addresses
- object: exception
- repeats: false];
- [[OFRunLoop currentRunLoop] addTimer: timer
- forMode: runLoopMode];
- }
-
- return;
- } @catch (OFInvalidFormatException *e) {
- }
-
- if ((aliases = [_settings->_staticHosts objectForKey: host]) != nil) {
- OFMutableData *addresses = [OFMutableData
- dataWithItemSize: sizeof(of_socket_address_t)];
- id exception = nil;
-
- for (OFString *alias in aliases) {
- of_socket_address_t address;
-
- @try {
- address = of_socket_address_parse_ip(alias, 0);
- } @catch (OFInvalidFormatException *e) {
- continue;
- }
-
- if (addressFamily != OF_SOCKET_ADDRESS_FAMILY_ANY &&
- address.family != addressFamily)
- continue;
-
- [addresses addItem: &address];
- }
-
- [addresses makeImmutable];
-
- if (addresses.count == 0) {
- of_dns_resource_record_type_t recordType = 0;
-
- addresses = nil;
-
- switch (addressFamily) {
- case OF_SOCKET_ADDRESS_FAMILY_ANY:
- recordType = OF_DNS_RESOURCE_RECORD_TYPE_ALL;
- break;
- case OF_SOCKET_ADDRESS_FAMILY_IPV4:
- recordType = OF_DNS_RESOURCE_RECORD_TYPE_A;
- break;
- case OF_SOCKET_ADDRESS_FAMILY_IPV6:
- recordType = OF_DNS_RESOURCE_RECORD_TYPE_AAAA;
- break;
- default:
- 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;
- OFDNSQuery *query = [OFDNSQuery
- queryWithHost: host
- recordClass: recordClass
- recordType: recordType];
-
- exception = [OFDNSQueryFailedException
- exceptionWithQuery: query
- error: error];
- }
- }
-
- if ([delegate respondsToSelector: @selector(resolver:
- didResolveDomainName:socketAddresses:exception:)]) {
- OFTimer *timer = [OFTimer
- timerWithTimeInterval: 0
- target: delegate
- selector: @selector(resolver:
- didResolveDomainName:
- socketAddresses:
- exception:)
- object: self
- object: host
- object: addresses
- object: exception
- repeats: false];
- [[OFRunLoop currentRunLoop] addTimer: timer
- forMode: runLoopMode];
- }
-
- return;
- }
-
- pool = objc_autoreleasePoolPush();
-
- context = [[[OFDNSResolverAsyncResolveSocketAddressesContext alloc]
+ void *pool = objc_autoreleasePoolPush();
+ OFHostAddressResolver *resolver = [[[OFHostAddressResolver alloc]
initWithHost: host
+ addressFamily: addressFamily
+ resolver: self
+ settings: _settings
+ runLoopMode: runLoopMode
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;
- break;
- case OF_SOCKET_ADDRESS_FAMILY_ANY:
-#ifdef OF_HAVE_IPV6
- context->_expectedResponses = 2;
-#else
- context->_expectedResponses = 1;
-#endif
- break;
- default:
- @throw [OFInvalidArgumentException exception];
- }
-
-#ifdef OF_HAVE_IPV6
- if (addressFamily == OF_SOCKET_ADDRESS_FAMILY_IPV6 ||
- addressFamily == OF_SOCKET_ADDRESS_FAMILY_ANY) {
- OFDNSQuery *query = [OFDNSQuery
- queryWithHost: host
- recordClass: OF_DNS_RESOURCE_RECORD_CLASS_IN
- recordType: OF_DNS_RESOURCE_RECORD_TYPE_AAAA];
- OFNumber *recordTypeNumber =
- [OFNumber numberWithInt: OF_DNS_RESOURCE_RECORD_TYPE_AAAA];
-
- [self of_asyncPerformQuery: query
- settings: nil
- nameServersIndex: 0
- searchDomainsIndex: 0
- runLoopMode: runLoopMode
- target: context
- selector: @selector(resolver:
- didResolveDomainName:response:
- context:exception:)
- context: recordTypeNumber];
- }
-#endif
-
- if (addressFamily == OF_SOCKET_ADDRESS_FAMILY_IPV4 ||
- addressFamily == OF_SOCKET_ADDRESS_FAMILY_ANY) {
- OFDNSQuery *query = [OFDNSQuery
- queryWithHost: host
- recordClass: OF_DNS_RESOURCE_RECORD_CLASS_IN
- recordType: OF_DNS_RESOURCE_RECORD_TYPE_A];
- OFNumber *recordTypeNumber =
- [OFNumber numberWithInt: OF_DNS_RESOURCE_RECORD_TYPE_A];
-
- [self of_asyncPerformQuery: query
- settings: nil
- nameServersIndex: 0
- searchDomainsIndex: 0
- runLoopMode: runLoopMode
- target: context
- selector: @selector(resolver:
- didResolveDomainName:response:
- context:exception:)
- context: recordTypeNumber];
- }
+ [resolver asyncResolve];
objc_autoreleasePoolPop(pool);
}
-- (OFData *)resolveSocketAddressesForHost: (OFString *)host
- addressFamily: (of_socket_address_family_t)
- addressFamily
+- (OFData *)resolveAddressesForHost: (OFString *)host
+ addressFamily: (of_socket_address_family_t)addressFamily
{
void *pool = objc_autoreleasePoolPush();
- OFRunLoop *runLoop = [OFRunLoop currentRunLoop];
- OFDNSResolverResolveSocketAddressesDelegate *delegate;
- OFData *ret;
-
- delegate = [[[OFDNSResolverResolveSocketAddressesDelegate
- alloc] init] autorelease];
-
- [self asyncResolveSocketAddressesForHost: host
- addressFamily: addressFamily
- runLoopMode: resolveRunLoopMode
- delegate: delegate];
-
- while (!delegate->_done)
- [runLoop runMode: resolveRunLoopMode
- beforeDate: nil];
-
- /* Cleanup */
- [runLoop runMode: resolveRunLoopMode
- beforeDate: [OFDate date]];
-
- if (delegate->_exception != nil)
- @throw delegate->_exception;
+ OFHostAddressResolver *resolver = [[[OFHostAddressResolver alloc]
+ initWithHost: host
+ addressFamily: addressFamily
+ resolver: self
+ settings: _settings
+ runLoopMode: nil
+ delegate: nil] autorelease];
+ OFData *addresses = [resolver resolve];
- ret = [delegate->_socketAddresses retain];
+ [addresses retain];
objc_autoreleasePoolPop(pool);
- return [ret autorelease];
+ return [addresses autorelease];
}
- (void)close
{
void *pool = objc_autoreleasePoolPush();
- OFEnumerator OF_GENERIC(OFDNSResolverQuery *) *enumerator;
- OFDNSResolverQuery *query;
+ OFEnumerator OF_GENERIC(OFDNSResolverContext *) *enumerator;
+ OFDNSResolverContext *context;
[_IPv4Socket cancelAsyncRequests];
[_IPv4Socket release];
@@ -1739,15 +1071,19 @@ callback(id target, SEL selector, OFDNSResolver *resolver, OFString *domainName,
#endif
enumerator = [_queries objectEnumerator];
- while ((query = [enumerator nextObject]) != nil) {
+ while ((context = [enumerator nextObject]) != nil) {
OFDNSQueryFailedException *exception;
exception = [OFDNSQueryFailedException
- exceptionWithQuery: query->_query
+ exceptionWithQuery: context->_query
error: OF_DNS_RESOLVER_ERROR_CANCELED];
- callback(query->_target, query->_selector, self,
- query->_domainName, nil, query->_context, exception);
+ if ([context->_delegate respondsToSelector:
+ @selector(resolver:didPerformQuery:response:exception:)])
+ [context->_delegate resolver: self
+ didPerformQuery: context->_query
+ response: nil
+ exception: exception];
}
[_queries removeAllObjects];
diff --git a/src/OFDNSResourceRecord.h b/src/OFDNSResourceRecord.h
index 2d0fa668..088040fe 100644
--- a/src/OFDNSResourceRecord.h
+++ b/src/OFDNSResourceRecord.h
@@ -27,14 +27,14 @@ OF_ASSUME_NONNULL_BEGIN
@class OFData;
/*!
- * @brief The class of a DNS resource record.
+ * @brief The DNS class.
*/
typedef enum {
/*! IN */
- OF_DNS_RESOURCE_RECORD_CLASS_IN = 1,
+ OF_DNS_CLASS_IN = 1,
/*! Any class. Only for queries. */
- OF_DNS_RESOURCE_RECORD_CLASS_ANY = 255,
-} of_dns_resource_record_class_t;
+ OF_DNS_CLASS_ANY = 255,
+} of_dns_class_t;
/*!
* @brief The type of a DNS resource record.
@@ -74,7 +74,7 @@ typedef enum {
@interface OFDNSResourceRecord: OFObject <OFCopying>
{
OFString *_name;
- of_dns_resource_record_class_t _recordClass;
+ of_dns_class_t _DNSClass;
of_dns_resource_record_type_t _recordType;
uint32_t _TTL;
OF_RESERVE_IVARS(4)
@@ -86,9 +86,9 @@ typedef enum {
@property (readonly, nonatomic) OFString *name;
/*!
- * @brief The resource record class code.
+ * @brief The DNS class.
*/
-@property (readonly, nonatomic) of_dns_resource_record_class_t recordClass;
+@property (readonly, nonatomic) of_dns_class_t DNSClass;
/*!
* @brief The resource record type code.
@@ -106,13 +106,13 @@ typedef enum {
* specified name, class, type, data and time to live.
*
* @param name The name for the resource record
- * @param recordClass The class code for the resource record
+ * @param DNSClass The class code for the resource record
* @param recordType The type code for the resource record
* @param TTL The time to live for the resource record
* @return An initialized OFDNSResourceRecord
*/
- (instancetype)initWithName: (OFString *)name
- recordClass: (of_dns_resource_record_class_t)recordClass
+ DNSClass: (of_dns_class_t)DNSClass
recordType: (of_dns_resource_record_type_t)recordType
TTL: (uint32_t)TTL OF_DESIGNATED_INITIALIZER;
@end
@@ -134,7 +134,7 @@ OF_SUBCLASSING_RESTRICTED
@property (readonly, nonatomic) const of_socket_address_t *address;
- (instancetype)initWithName: (OFString *)name
- recordClass: (of_dns_resource_record_class_t)recordClass
+ DNSClass: (of_dns_class_t)DNSClass
recordType: (of_dns_resource_record_type_t)recordType
TTL: (uint32_t)TTL OF_UNAVAILABLE;
@@ -170,7 +170,7 @@ OF_SUBCLASSING_RESTRICTED
@property (readonly, nonatomic) const of_socket_address_t *address;
- (instancetype)initWithName: (OFString *)name
- recordClass: (of_dns_resource_record_class_t)recordClass
+ DNSClass: (of_dns_class_t)DNSClass
recordType: (of_dns_resource_record_type_t)recordType
TTL: (uint32_t)TTL OF_UNAVAILABLE;
@@ -206,7 +206,7 @@ OF_SUBCLASSING_RESTRICTED
@property (readonly, nonatomic) OFString *alias;
- (instancetype)initWithName: (OFString *)name
- recordClass: (of_dns_resource_record_class_t)recordClass
+ DNSClass: (of_dns_class_t)DNSClass
recordType: (of_dns_resource_record_type_t)recordType
TTL: (uint32_t)TTL OF_UNAVAILABLE;
@@ -215,13 +215,13 @@ OF_SUBCLASSING_RESTRICTED
* specified name, class, alias and time to live.
*
* @param name The name for the resource record
- * @param recordClass The class code for the resource record
+ * @param DNSClass The class code for the resource record
* @param alias The alias for the resource record
* @param TTL The time to live for the resource record
* @return An initialized OFCNAMEDNSResourceRecord
*/
- (instancetype)initWithName: (OFString *)name
- recordClass: (of_dns_resource_record_class_t)recordClass
+ DNSClass: (of_dns_class_t)DNSClass
alias: (OFString *)alias
TTL: (uint32_t)TTL OF_DESIGNATED_INITIALIZER;
@end
@@ -249,7 +249,7 @@ OF_SUBCLASSING_RESTRICTED
@property (readonly, nonatomic) OFString *OS;
- (instancetype)initWithName: (OFString *)name
- recordClass: (of_dns_resource_record_class_t)recordClass
+ DNSClass: (of_dns_class_t)DNSClass
recordType: (of_dns_resource_record_type_t)recordType
TTL: (uint32_t)TTL OF_UNAVAILABLE;
@@ -258,14 +258,14 @@ OF_SUBCLASSING_RESTRICTED
* specified name, class, domain name and time to live.
*
* @param name The name for the resource record
- * @param recordClass The class code for the resource record
+ * @param DNSClass The class code for the resource record
* @param CPU The CPU of the host info for the resource record
* @param OS The OS of the host info for the resource record
* @param TTL The time to live for the resource record
* @return An initialized OFHINFODNSResourceRecord
*/
- (instancetype)initWithName: (OFString *)name
- recordClass: (of_dns_resource_record_class_t)recordClass
+ DNSClass: (of_dns_class_t)DNSClass
CPU: (OFString *)CPU
OS: (OFString *)OS
TTL: (uint32_t)TTL OF_DESIGNATED_INITIALIZER;
@@ -295,7 +295,7 @@ OF_SUBCLASSING_RESTRICTED
@property (readonly, nonatomic) OFString *mailExchange;
- (instancetype)initWithName: (OFString *)name
- recordClass: (of_dns_resource_record_class_t)recordClass
+ DNSClass: (of_dns_class_t)DNSClass
recordType: (of_dns_resource_record_type_t)recordType
TTL: (uint32_t)TTL OF_UNAVAILABLE;
@@ -304,14 +304,14 @@ OF_SUBCLASSING_RESTRICTED
* specified name, class, preference, mail exchange and time to live.
*
* @param name The name for the resource record
- * @param recordClass The class code for the resource record
+ * @param DNSClass The class code for the resource record
* @param preference The preference for the resource record
* @param mailExchange The mail exchange for the resource record
* @param TTL The time to live for the resource record
* @return An initialized OFMXDNSResourceRecord
*/
- (instancetype)initWithName: (OFString *)name
- recordClass: (of_dns_resource_record_class_t)recordClass
+ DNSClass: (of_dns_class_t)DNSClass
preference: (uint16_t)preference
mailExchange: (OFString *)mailExchange
TTL: (uint32_t)TTL OF_DESIGNATED_INITIALIZER;
@@ -335,7 +335,7 @@ OF_SUBCLASSING_RESTRICTED
@property (readonly, nonatomic) OFString *authoritativeHost;
- (instancetype)initWithName: (OFString *)name
- recordClass: (of_dns_resource_record_class_t)recordClass
+ DNSClass: (of_dns_class_t)DNSClass
recordType: (of_dns_resource_record_type_t)recordType
TTL: (uint32_t)TTL OF_UNAVAILABLE;
@@ -344,13 +344,13 @@ OF_SUBCLASSING_RESTRICTED
* specified name, class, authoritative host and time to live.
*
* @param name The name for the resource record
- * @param recordClass The class code for the resource record
+ * @param DNSClass The class code for the resource record
* @param authoritativeHost The authoritative host for the resource record
* @param TTL The time to live for the resource record
* @return An initialized OFNSDNSResourceRecord
*/
- (instancetype)initWithName: (OFString *)name
- recordClass: (of_dns_resource_record_class_t)recordClass
+ DNSClass: (of_dns_class_t)DNSClass
authoritativeHost: (OFString *)authoritativeHost
TTL: (uint32_t)TTL OF_DESIGNATED_INITIALIZER;
@end
@@ -373,7 +373,7 @@ OF_SUBCLASSING_RESTRICTED
@property (readonly, nonatomic) OFString *domainName;
- (instancetype)initWithName: (OFString *)name
- recordClass: (of_dns_resource_record_class_t)recordClass
+ DNSClass: (of_dns_class_t)DNSClass
recordType: (of_dns_resource_record_type_t)recordType
TTL: (uint32_t)TTL OF_UNAVAILABLE;
@@ -382,13 +382,13 @@ OF_SUBCLASSING_RESTRICTED
* specified name, class, domain name and time to live.
*
* @param name The name for the resource record
- * @param recordClass The class code for the resource record
+ * @param DNSClass The class code for the resource record
* @param domainName The domain name for the resource record
* @param TTL The time to live for the resource record
* @return An initialized OFPTRDNSResourceRecord
*/
- (instancetype)initWithName: (OFString *)name
- recordClass: (of_dns_resource_record_class_t)recordClass
+ DNSClass: (of_dns_class_t)DNSClass
domainName: (OFString *)domainName
TTL: (uint32_t)TTL OF_DESIGNATED_INITIALIZER;
@end
@@ -417,7 +417,7 @@ OF_SUBCLASSING_RESTRICTED
@property (readonly, nonatomic) OFString *TXTDomainName;
- (instancetype)initWithName: (OFString *)name
- recordClass: (of_dns_resource_record_class_t)recordClass
+ DNSClass: (of_dns_class_t)DNSClass
recordType: (of_dns_resource_record_type_t)recordType
TTL: (uint32_t)TTL OF_UNAVAILABLE;
@@ -426,7 +426,7 @@ OF_SUBCLASSING_RESTRICTED
* specified name, class, alias and time to live.
*
* @param name The name for the resource record
- * @param recordClass The class code for the resource record
+ * @param DNSClass The class code for the resource record
* @param mailbox The mailbox of the responsible person of the resource record
* @param TXTDomainName A domain name that contains a TXT resource record for
* the responsible person of the resource record
@@ -434,7 +434,7 @@ OF_SUBCLASSING_RESTRICTED
* @return An initialized OFRPDNSResourceRecord
*/
- (instancetype)initWithName: (OFString *)name
- recordClass: (of_dns_resource_record_class_t)recordClass
+ DNSClass: (of_dns_class_t)DNSClass
mailbox: (OFString *)mailbox
TXTDomainName: (OFString *)TXTDomainName
TTL: (uint32_t)TTL OF_DESIGNATED_INITIALIZER;
@@ -490,7 +490,7 @@ OF_SUBCLASSING_RESTRICTED
@property (readonly, nonatomic) uint32_t minTTL;
- (instancetype)initWithName: (OFString *)name
- recordClass: (of_dns_resource_record_class_t)recordClass
+ DNSClass: (of_dns_class_t)DNSClass
recordType: (of_dns_resource_record_type_t)recordType
TTL: (uint32_t)TTL OF_UNAVAILABLE;
@@ -499,7 +499,7 @@ OF_SUBCLASSING_RESTRICTED
* specified name, class, text data and time to live.
*
* @param name The name for the resource record
- * @param recordClass The class code for the resource record
+ * @param DNSClass The class code for the resource record
* @param primaryNameServer The the primary name server for the zone
* @param responsiblePerson The mailbox of the person responsible for the zone
* @param serialNumber The serial number of the original copy of the zone
@@ -511,7 +511,7 @@ OF_SUBCLASSING_RESTRICTED
* @return An initialized OFSOADNSResourceRecord
*/
- (instancetype)initWithName: (OFString *)name
- recordClass: (of_dns_resource_record_class_t)recordClass
+ DNSClass: (of_dns_class_t)DNSClass
primaryNameServer: (OFString *)primaryNameServer
responsiblePerson: (OFString *)responsiblePerson
serialNumber: (uint32_t)serialNumber
@@ -557,7 +557,7 @@ OF_SUBCLASSING_RESTRICTED
@property (readonly, nonatomic) uint16_t port;
- (instancetype)initWithName: (OFString *)name
- recordClass: (of_dns_resource_record_class_t)recordClass
+ DNSClass: (of_dns_class_t)DNSClass
recordType: (of_dns_resource_record_type_t)recordType
TTL: (uint32_t)TTL OF_UNAVAILABLE;
@@ -599,7 +599,7 @@ OF_SUBCLASSING_RESTRICTED
@property (readonly, nonatomic) OFData *textData;
- (instancetype)initWithName: (OFString *)name
- recordClass: (of_dns_resource_record_class_t)recordClass
+ DNSClass: (of_dns_class_t)DNSClass
recordType: (of_dns_resource_record_type_t)recordType
TTL: (uint32_t)TTL OF_UNAVAILABLE;
@@ -608,13 +608,13 @@ OF_SUBCLASSING_RESTRICTED
* specified name, class, text data and time to live.
*
* @param name The name for the resource record
- * @param recordClass The class code for the resource record
+ * @param DNSClass The class code for the resource record
* @param textData The data for the resource record
* @param TTL The time to live for the resource record
* @return An initialized OFTXTDNSResourceRecord
*/
- (instancetype)initWithName: (OFString *)name
- recordClass: (of_dns_resource_record_class_t)recordClass
+ DNSClass: (of_dns_class_t)DNSClass
textData: (OFData *)textData
TTL: (uint32_t)TTL OF_DESIGNATED_INITIALIZER;
@end
@@ -622,12 +622,10 @@ OF_SUBCLASSING_RESTRICTED
#ifdef __cplusplus
extern "C" {
#endif
-extern OFString *_Nonnull of_dns_resource_record_class_to_string(
- of_dns_resource_record_class_t recordClass);
+extern OFString *_Nonnull of_dns_class_to_string(of_dns_class_t DNSClass);
extern OFString *_Nonnull of_dns_resource_record_type_to_string(
of_dns_resource_record_type_t recordType);
-extern of_dns_resource_record_class_t of_dns_resource_record_class_parse(
- OFString *_Nonnull string);
+extern of_dns_class_t of_dns_class_parse(OFString *_Nonnull string);
extern of_dns_resource_record_type_t of_dns_resource_record_type_parse(
OFString *_Nonnull string);
#ifdef __cplusplus
diff --git a/src/OFDNSResourceRecord.m b/src/OFDNSResourceRecord.m
index 6be51501..c4c2710c 100644
--- a/src/OFDNSResourceRecord.m
+++ b/src/OFDNSResourceRecord.m
@@ -24,16 +24,15 @@
#import "OFInvalidFormatException.h"
OFString *
-of_dns_resource_record_class_to_string(
- of_dns_resource_record_class_t recordClass)
+of_dns_class_to_string(of_dns_class_t DNSClass)
{
- switch (recordClass) {
- case OF_DNS_RESOURCE_RECORD_CLASS_IN:
+ switch (DNSClass) {
+ case OF_DNS_CLASS_IN:
return @"IN";
- case OF_DNS_RESOURCE_RECORD_CLASS_ANY:
+ case OF_DNS_CLASS_ANY:
return @"any";
default:
- return [OFString stringWithFormat: @"%u", recordClass];
+ return [OFString stringWithFormat: @"%u", DNSClass];
}
}
@@ -70,22 +69,21 @@ of_dns_resource_record_type_to_string(of_dns_resource_record_type_t recordType)
}
}
-of_dns_resource_record_class_t of_dns_resource_record_class_parse(
- OFString *string)
+of_dns_class_t of_dns_class_parse(OFString *string)
{
void *pool = objc_autoreleasePoolPush();
- of_dns_resource_record_class_t recordClass;
+ of_dns_class_t DNSClass;
string = string.uppercaseString;
if ([string isEqual: @"IN"])
- recordClass = OF_DNS_RESOURCE_RECORD_CLASS_IN;
+ DNSClass = OF_DNS_CLASS_IN;
else
@throw [OFInvalidArgumentException exception];
objc_autoreleasePoolPop(pool);
- return recordClass;
+ return DNSClass;
}
of_dns_resource_record_type_t of_dns_resource_record_type_parse(
@@ -127,11 +125,11 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
}
@implementation OFDNSResourceRecord
-@synthesize name = _name, recordClass = _recordClass, recordType = _recordType;
+@synthesize name = _name, DNSClass = _DNSClass, recordType = _recordType;
@synthesize TTL = _TTL;
- (instancetype)initWithName: (OFString *)name
- recordClass: (of_dns_resource_record_class_t)recordClass
+ DNSClass: (of_dns_class_t)DNSClass
recordType: (of_dns_resource_record_type_t)recordType
TTL: (uint32_t)TTL
{
@@ -139,7 +137,7 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
@try {
_name = [name copy];
- _recordClass = recordClass;
+ _DNSClass = DNSClass;
_recordType = recordType;
_TTL = TTL;
} @catch (id e) {
@@ -171,15 +169,14 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
@"\tType = %@\n"
@"\tTTL = %" PRIu32 "\n"
@">",
- self.className, _name,
- of_dns_resource_record_class_to_string(_recordClass),
+ self.className, _name, of_dns_class_to_string(_DNSClass),
of_dns_resource_record_type_to_string(_recordType), _TTL];
}
@end
@implementation OFADNSResourceRecord
- (instancetype)initWithName: (OFString *)name
- recordClass: (of_dns_resource_record_class_t)recordClass
+ DNSClass: (of_dns_class_t)DNSClass
recordType: (of_dns_resource_record_type_t)recordType
TTL: (uint32_t)TTL
{
@@ -191,7 +188,7 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
TTL: (uint32_t)TTL
{
self = [super initWithName: name
- recordClass: OF_DNS_RESOURCE_RECORD_CLASS_IN
+ DNSClass: OF_DNS_CLASS_IN
recordType: OF_DNS_RESOURCE_RECORD_TYPE_A
TTL: TTL];
@@ -217,7 +214,7 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
if (otherRecord->_name != _name && ![otherRecord->_name isEqual: _name])
return false;
- if (otherRecord->_recordClass != _recordClass)
+ if (otherRecord->_DNSClass != _DNSClass)
return false;
if (otherRecord->_recordType != _recordType)
@@ -236,8 +233,8 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
OF_HASH_INIT(hash);
OF_HASH_ADD_HASH(hash, _name.hash);
- OF_HASH_ADD(hash, _recordClass >> 8);
- OF_HASH_ADD(hash, _recordClass);
+ OF_HASH_ADD(hash, _DNSClass >> 8);
+ OF_HASH_ADD(hash, _DNSClass);
OF_HASH_ADD(hash, _recordType >> 8);
OF_HASH_ADD(hash, _recordType);
OF_HASH_ADD_HASH(hash, of_socket_address_hash(&_address));
@@ -262,7 +259,7 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
@implementation OFAAAADNSResourceRecord
- (instancetype)initWithName: (OFString *)name
- recordClass: (of_dns_resource_record_class_t)recordClass
+ DNSClass: (of_dns_class_t)DNSClass
recordType: (of_dns_resource_record_type_t)recordType
TTL: (uint32_t)TTL
{
@@ -274,7 +271,7 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
TTL: (uint32_t)TTL
{
self = [super initWithName: name
- recordClass: OF_DNS_RESOURCE_RECORD_CLASS_IN
+ DNSClass: OF_DNS_CLASS_IN
recordType: OF_DNS_RESOURCE_RECORD_TYPE_AAAA
TTL: TTL];
@@ -300,7 +297,7 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
if (otherRecord->_name != _name && ![otherRecord->_name isEqual: _name])
return false;
- if (otherRecord->_recordClass != _recordClass)
+ if (otherRecord->_DNSClass != _DNSClass)
return false;
if (otherRecord->_recordType != _recordType)
@@ -319,8 +316,8 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
OF_HASH_INIT(hash);
OF_HASH_ADD_HASH(hash, _name.hash);
- OF_HASH_ADD(hash, _recordClass >> 8);
- OF_HASH_ADD(hash, _recordClass);
+ OF_HASH_ADD(hash, _DNSClass >> 8);
+ OF_HASH_ADD(hash, _DNSClass);
OF_HASH_ADD(hash, _recordType >> 8);
OF_HASH_ADD(hash, _recordType);
OF_HASH_ADD_HASH(hash, of_socket_address_hash(&_address));
@@ -347,7 +344,7 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
@synthesize alias = _alias;
- (instancetype)initWithName: (OFString *)name
- recordClass: (of_dns_resource_record_class_t)recordClass
+ DNSClass: (of_dns_class_t)DNSClass
recordType: (of_dns_resource_record_type_t)recordType
TTL: (uint32_t)TTL
{
@@ -355,12 +352,12 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
}
- (instancetype)initWithName: (OFString *)name
- recordClass: (of_dns_resource_record_class_t)recordClass
+ DNSClass: (of_dns_class_t)DNSClass
alias: (OFString *)alias
TTL: (uint32_t)TTL
{
self = [super initWithName: name
- recordClass: recordClass
+ DNSClass: DNSClass
recordType: OF_DNS_RESOURCE_RECORD_TYPE_CNAME
TTL: TTL];
@@ -393,7 +390,7 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
if (otherRecord->_name != _name && ![otherRecord->_name isEqual: _name])
return false;
- if (otherRecord->_recordClass != _recordClass)
+ if (otherRecord->_DNSClass != _DNSClass)
return false;
if (otherRecord->_recordType != _recordType)
@@ -413,8 +410,8 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
OF_HASH_INIT(hash);
OF_HASH_ADD_HASH(hash, _name.hash);
- OF_HASH_ADD(hash, _recordClass >> 8);
- OF_HASH_ADD(hash, _recordClass);
+ OF_HASH_ADD(hash, _DNSClass >> 8);
+ OF_HASH_ADD(hash, _DNSClass);
OF_HASH_ADD(hash, _recordType >> 8);
OF_HASH_ADD(hash, _recordType);
OF_HASH_ADD_HASH(hash, _alias.hash);
@@ -433,8 +430,8 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
@"\tAlias = %@\n"
@"\tTTL = %" PRIu32 "\n"
@">",
- self.className, _name,
- of_dns_resource_record_class_to_string(_recordClass), _alias, _TTL];
+ self.className, _name, of_dns_class_to_string(_DNSClass), _alias,
+ _TTL];
}
@end
@@ -442,7 +439,7 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
@synthesize CPU = _CPU, OS = _OS;
- (instancetype)initWithName: (OFString *)name
- recordClass: (of_dns_resource_record_class_t)recordClass
+ DNSClass: (of_dns_class_t)DNSClass
recordType: (of_dns_resource_record_type_t)recordType
TTL: (uint32_t)TTL
{
@@ -450,13 +447,13 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
}
- (instancetype)initWithName: (OFString *)name
- recordClass: (of_dns_resource_record_class_t)recordClass
+ DNSClass: (of_dns_class_t)DNSClass
CPU: (OFString *)CPU
OS: (OFString *)OS
TTL: (uint32_t)TTL
{
self = [super initWithName: name
- recordClass: recordClass
+ DNSClass: DNSClass
recordType: OF_DNS_RESOURCE_RECORD_TYPE_HINFO
TTL: TTL];
@@ -491,7 +488,7 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
if (otherRecord->_name != _name && ![otherRecord->_name isEqual: _name])
return false;
- if (otherRecord->_recordClass != _recordClass)
+ if (otherRecord->_DNSClass != _DNSClass)
return false;
if (otherRecord->_recordType != _recordType)
@@ -513,8 +510,8 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
OF_HASH_INIT(hash);
OF_HASH_ADD_HASH(hash, _name.hash);
- OF_HASH_ADD(hash, _recordClass >> 8);
- OF_HASH_ADD(hash, _recordClass);
+ OF_HASH_ADD(hash, _DNSClass >> 8);
+ OF_HASH_ADD(hash, _DNSClass);
OF_HASH_ADD(hash, _recordType >> 8);
OF_HASH_ADD(hash, _recordType);
OF_HASH_ADD_HASH(hash, _CPU.hash);
@@ -535,8 +532,7 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
@"\tOS = %@\n"
@"\tTTL = %" PRIu32 "\n"
@">",
- self.className, _name,
- of_dns_resource_record_class_to_string(_recordClass), _CPU, _OS,
+ self.className, _name, of_dns_class_to_string(_DNSClass), _CPU, _OS,
_TTL];
}
@end
@@ -545,7 +541,7 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
@synthesize preference = _preference, mailExchange = _mailExchange;
- (instancetype)initWithName: (OFString *)name
- recordClass: (of_dns_resource_record_class_t)recordClass
+ DNSClass: (of_dns_class_t)DNSClass
recordType: (of_dns_resource_record_type_t)recordType
TTL: (uint32_t)TTL
{
@@ -553,13 +549,13 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
}
- (instancetype)initWithName: (OFString *)name
- recordClass: (of_dns_resource_record_class_t)recordClass
+ DNSClass: (of_dns_class_t)DNSClass
preference: (uint16_t)preference
mailExchange: (OFString *)mailExchange
TTL: (uint32_t)TTL
{
self = [super initWithName: name
- recordClass: recordClass
+ DNSClass: DNSClass
recordType: OF_DNS_RESOURCE_RECORD_TYPE_MX
TTL: TTL];
@@ -593,7 +589,7 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
if (otherRecord->_name != _name && ![otherRecord->_name isEqual: _name])
return false;
- if (otherRecord->_recordClass != _recordClass)
+ if (otherRecord->_DNSClass != _DNSClass)
return false;
if (otherRecord->_recordType != _recordType)
@@ -616,8 +612,8 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
OF_HASH_INIT(hash);
OF_HASH_ADD_HASH(hash, _name.hash);
- OF_HASH_ADD(hash, _recordClass >> 8);
- OF_HASH_ADD(hash, _recordClass);
+ OF_HASH_ADD(hash, _DNSClass >> 8);
+ OF_HASH_ADD(hash, _DNSClass);
OF_HASH_ADD(hash, _recordType >> 8);
OF_HASH_ADD(hash, _recordType);
OF_HASH_ADD(hash, _preference >> 8);
@@ -639,9 +635,8 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
@"\tMail Exchange = %@\n"
@"\tTTL = %" PRIu32 "\n"
@">",
- self.className, _name,
- of_dns_resource_record_class_to_string(_recordClass), _preference,
- _mailExchange, _TTL];
+ self.className, _name, of_dns_class_to_string(_DNSClass),
+ _preference, _mailExchange, _TTL];
}
@end
@@ -649,7 +644,7 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
@synthesize authoritativeHost = _authoritativeHost;
- (instancetype)initWithName: (OFString *)name
- recordClass: (of_dns_resource_record_class_t)recordClass
+ DNSClass: (of_dns_class_t)DNSClass
recordType: (of_dns_resource_record_type_t)recordType
TTL: (uint32_t)TTL
{
@@ -657,12 +652,12 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
}
- (instancetype)initWithName: (OFString *)name
- recordClass: (of_dns_resource_record_class_t)recordClass
+ DNSClass: (of_dns_class_t)DNSClass
authoritativeHost: (OFString *)authoritativeHost
TTL: (uint32_t)TTL
{
self = [super initWithName: name
- recordClass: recordClass
+ DNSClass: DNSClass
recordType: OF_DNS_RESOURCE_RECORD_TYPE_NS
TTL: TTL];
@@ -695,7 +690,7 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
if (otherRecord->_name != _name && ![otherRecord->_name isEqual: _name])
return false;
- if (otherRecord->_recordClass != _recordClass)
+ if (otherRecord->_DNSClass != _DNSClass)
return false;
if (otherRecord->_recordType != _recordType)
@@ -715,8 +710,8 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
OF_HASH_INIT(hash);
OF_HASH_ADD_HASH(hash, _name.hash);
- OF_HASH_ADD(hash, _recordClass >> 8);
- OF_HASH_ADD(hash, _recordClass);
+ OF_HASH_ADD(hash, _DNSClass >> 8);
+ OF_HASH_ADD(hash, _DNSClass);
OF_HASH_ADD(hash, _recordType >> 8);
OF_HASH_ADD(hash, _recordType);
OF_HASH_ADD_HASH(hash, _authoritativeHost.hash);
@@ -735,8 +730,7 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
@"\tAuthoritative Host = %@\n"
@"\tTTL = %" PRIu32 "\n"
@">",
- self.className, _name,
- of_dns_resource_record_class_to_string(_recordClass),
+ self.className, _name, of_dns_class_to_string(_DNSClass),
_authoritativeHost, _TTL];
}
@end
@@ -745,7 +739,7 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
@synthesize domainName = _domainName;
- (instancetype)initWithName: (OFString *)name
- recordClass: (of_dns_resource_record_class_t)recordClass
+ DNSClass: (of_dns_class_t)DNSClass
recordType: (of_dns_resource_record_type_t)recordType
TTL: (uint32_t)TTL
{
@@ -753,12 +747,12 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
}
- (instancetype)initWithName: (OFString *)name
- recordClass: (of_dns_resource_record_class_t)recordClass
+ DNSClass: (of_dns_class_t)DNSClass
domainName: (OFString *)domainName
TTL: (uint32_t)TTL
{
self = [super initWithName: name
- recordClass: recordClass
+ DNSClass: DNSClass
recordType: OF_DNS_RESOURCE_RECORD_TYPE_PTR
TTL: TTL];
@@ -791,7 +785,7 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
if (otherRecord->_name != _name && ![otherRecord->_name isEqual: _name])
return false;
- if (otherRecord->_recordClass != _recordClass)
+ if (otherRecord->_DNSClass != _DNSClass)
return false;
if (otherRecord->_recordType != _recordType)
@@ -811,8 +805,8 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
OF_HASH_INIT(hash);
OF_HASH_ADD_HASH(hash, _name.hash);
- OF_HASH_ADD(hash, _recordClass >> 8);
- OF_HASH_ADD(hash, _recordClass);
+ OF_HASH_ADD(hash, _DNSClass >> 8);
+ OF_HASH_ADD(hash, _DNSClass);
OF_HASH_ADD(hash, _recordType >> 8);
OF_HASH_ADD(hash, _recordType);
OF_HASH_ADD_HASH(hash, _domainName.hash);
@@ -831,9 +825,8 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
@"\tDomain Name = %@\n"
@"\tTTL = %" PRIu32 "\n"
@">",
- self.className, _name,
- of_dns_resource_record_class_to_string(_recordClass), _domainName,
- _TTL];
+ self.className, _name, of_dns_class_to_string(_DNSClass),
+ _domainName, _TTL];
}
@end
@@ -841,7 +834,7 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
@synthesize mailbox = _mailbox, TXTDomainName = _TXTDomainName;
- (instancetype)initWithName: (OFString *)name
- recordClass: (of_dns_resource_record_class_t)recordClass
+ DNSClass: (of_dns_class_t)DNSClass
recordType: (of_dns_resource_record_type_t)recordType
TTL: (uint32_t)TTL
{
@@ -849,13 +842,13 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
}
- (instancetype)initWithName: (OFString *)name
- recordClass: (of_dns_resource_record_class_t)recordClass
+ DNSClass: (of_dns_class_t)DNSClass
mailbox: (OFString *)mailbox
TXTDomainName: (OFString *)TXTDomainName
TTL: (uint32_t)TTL
{
self = [super initWithName: name
- recordClass: recordClass
+ DNSClass: DNSClass
recordType: OF_DNS_RESOURCE_RECORD_TYPE_RP
TTL: TTL];
@@ -890,7 +883,7 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
if (otherRecord->_name != _name && ![otherRecord->_name isEqual: _name])
return false;
- if (otherRecord->_recordClass != _recordClass)
+ if (otherRecord->_DNSClass != _DNSClass)
return false;
if (otherRecord->_recordType != _recordType)
@@ -914,8 +907,8 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
OF_HASH_INIT(hash);
OF_HASH_ADD_HASH(hash, _name.hash);
- OF_HASH_ADD(hash, _recordClass >> 8);
- OF_HASH_ADD(hash, _recordClass);
+ OF_HASH_ADD(hash, _DNSClass >> 8);
+ OF_HASH_ADD(hash, _DNSClass);
OF_HASH_ADD(hash, _recordType >> 8);
OF_HASH_ADD(hash, _recordType);
OF_HASH_ADD_HASH(hash, _mailbox.hash);
@@ -936,8 +929,7 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
@"\tTXT Domain Name = %@\n"
@"\tTTL = %" PRIu32 "\n"
@">",
- self.className, _name,
- of_dns_resource_record_class_to_string(_recordClass), _mailbox,
+ self.className, _name, of_dns_class_to_string(_DNSClass), _mailbox,
_TXTDomainName, _TTL];
}
@end
@@ -950,7 +942,7 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
@synthesize expirationInterval = _expirationInterval, minTTL = _minTTL;
- (instancetype)initWithName: (OFString *)name
- recordClass: (of_dns_resource_record_class_t)recordClass
+ DNSClass: (of_dns_class_t)DNSClass
recordType: (of_dns_resource_record_type_t)recordType
TTL: (uint32_t)TTL
{
@@ -958,7 +950,7 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
}
- (instancetype)initWithName: (OFString *)name
- recordClass: (of_dns_resource_record_class_t)recordClass
+ DNSClass: (of_dns_class_t)DNSClass
primaryNameServer: (OFString *)primaryNameServer
responsiblePerson: (OFString *)responsiblePerson
serialNumber: (uint32_t)serialNumber
@@ -969,7 +961,7 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
TTL: (uint32_t)TTL
{
self = [super initWithName: name
- recordClass: recordClass
+ DNSClass: DNSClass
recordType: OF_DNS_RESOURCE_RECORD_TYPE_SOA
TTL: TTL];
@@ -1009,7 +1001,7 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
if (otherRecord->_name != _name && ![otherRecord->_name isEqual: _name])
return false;
- if (otherRecord->_recordClass != _recordClass)
+ if (otherRecord->_DNSClass != _DNSClass)
return false;
if (otherRecord->_recordType != _recordType)
@@ -1048,8 +1040,8 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
OF_HASH_INIT(hash);
OF_HASH_ADD_HASH(hash, _name.hash);
- OF_HASH_ADD(hash, _recordClass >> 8);
- OF_HASH_ADD(hash, _recordClass);
+ OF_HASH_ADD(hash, _DNSClass >> 8);
+ OF_HASH_ADD(hash, _DNSClass);
OF_HASH_ADD(hash, _recordType >> 8);
OF_HASH_ADD(hash, _recordType);
OF_HASH_ADD_HASH(hash, _primaryNameServer.hash);
@@ -1095,8 +1087,7 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
@"\tMinimum TTL = %" PRIu32 "\n"
@"\tTTL = %" PRIu32 "\n"
@">",
- self.className, _name,
- of_dns_resource_record_class_to_string(_recordClass),
+ self.className, _name, of_dns_class_to_string(_DNSClass),
_primaryNameServer, _responsiblePerson, _serialNumber,
_refreshInterval, _retryInterval, _expirationInterval, _minTTL,
_TTL];
@@ -1108,7 +1099,7 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
@synthesize port = _port;
- (instancetype)initWithName: (OFString *)name
- recordClass: (of_dns_resource_record_class_t)recordClass
+ DNSClass: (of_dns_class_t)DNSClass
recordType: (of_dns_resource_record_type_t)recordType
TTL: (uint32_t)TTL
{
@@ -1123,7 +1114,7 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
TTL: (uint32_t)TTL
{
self = [super initWithName: name
- recordClass: OF_DNS_RESOURCE_RECORD_CLASS_IN
+ DNSClass: OF_DNS_CLASS_IN
recordType: OF_DNS_RESOURCE_RECORD_TYPE_SRV
TTL: TTL];
@@ -1159,7 +1150,7 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
if (otherRecord->_name != _name && ![otherRecord->_name isEqual: _name])
return false;
- if (otherRecord->_recordClass != _recordClass)
+ if (otherRecord->_DNSClass != _DNSClass)
return false;
if (otherRecord->_recordType != _recordType)
@@ -1188,8 +1179,8 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
OF_HASH_INIT(hash);
OF_HASH_ADD_HASH(hash, _name.hash);
- OF_HASH_ADD(hash, _recordClass >> 8);
- OF_HASH_ADD(hash, _recordClass);
+ OF_HASH_ADD(hash, _DNSClass >> 8);
+ OF_HASH_ADD(hash, _DNSClass);
OF_HASH_ADD(hash, _recordType >> 8);
OF_HASH_ADD(hash, _recordType);
OF_HASH_ADD(hash, _priority >> 8);
@@ -1224,7 +1215,7 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
@synthesize textData = _textData;
- (instancetype)initWithName: (OFString *)name
- recordClass: (of_dns_resource_record_class_t)recordClass
+ DNSClass: (of_dns_class_t)DNSClass
recordType: (of_dns_resource_record_type_t)recordType
TTL: (uint32_t)TTL
{
@@ -1232,12 +1223,12 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
}
- (instancetype)initWithName: (OFString *)name
- recordClass: (of_dns_resource_record_class_t)recordClass
+ DNSClass: (of_dns_class_t)DNSClass
textData: (OFData *)textData
TTL: (uint32_t)TTL
{
self = [super initWithName: name
- recordClass: recordClass
+ DNSClass: DNSClass
recordType: OF_DNS_RESOURCE_RECORD_TYPE_TXT
TTL: TTL];
@@ -1270,7 +1261,7 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
if (otherRecord->_name != _name && ![otherRecord->_name isEqual: _name])
return false;
- if (otherRecord->_recordClass != _recordClass)
+ if (otherRecord->_DNSClass != _DNSClass)
return false;
if (otherRecord->_recordType != _recordType)
@@ -1290,8 +1281,8 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
OF_HASH_INIT(hash);
OF_HASH_ADD_HASH(hash, _name.hash);
- OF_HASH_ADD(hash, _recordClass >> 8);
- OF_HASH_ADD(hash, _recordClass);
+ OF_HASH_ADD(hash, _DNSClass >> 8);
+ OF_HASH_ADD(hash, _DNSClass);
OF_HASH_ADD(hash, _recordType >> 8);
OF_HASH_ADD(hash, _recordType);
OF_HASH_ADD_HASH(hash, _textData.hash);
@@ -1310,8 +1301,7 @@ of_dns_resource_record_type_t of_dns_resource_record_type_parse(
@"\tText Data = %@\n"
@"\tTTL = %" PRIu32 "\n"
@">",
- self.className, _name,
- of_dns_resource_record_class_to_string(_recordClass), _textData,
+ self.className, _name, of_dns_class_to_string(_DNSClass), _textData,
_TTL];
}
@end
diff --git a/src/OFDNSResponse.h b/src/OFDNSResponse.h
index dab755b5..6200b686 100644
--- a/src/OFDNSResponse.h
+++ b/src/OFDNSResponse.h
@@ -33,29 +33,41 @@ typedef OFDictionary OF_GENERIC(OFString *, OFArray OF_GENERIC(
*/
@interface OFDNSResponse: OFObject
{
- of_dns_response_records_t _Nullable _answerRecords;
- of_dns_response_records_t _Nullable _authorityRecords;
- of_dns_response_records_t _Nullable _additionalRecords;
+ OFString *_domainName;
+ of_dns_response_records_t _answerRecords;
+ of_dns_response_records_t _authorityRecords;
+ of_dns_response_records_t _additionalRecords;
OF_RESERVE_IVARS(4)
}
/*!
+ * @brief The domain name of the response.
+ */
+@property (readonly, nonatomic) OFString *domainName;
+
+/*!
* @brief The answer records of the response.
+ *
+ * This is a dictionary with the key being the domain name and the value being
+ * an array of @ref OFDNSResourceRecord.
*/
-@property OF_NULLABLE_PROPERTY (readonly, nonatomic)
- of_dns_response_records_t answerRecords;
+@property (readonly, nonatomic) of_dns_response_records_t answerRecords;
/*!
* @brief The authority records of the response.
+ *
+ * This is a dictionary with the key being the domain name and the value being
+ * an array of @ref OFDNSResourceRecord.
*/
-@property OF_NULLABLE_PROPERTY (readonly, nonatomic)
- of_dns_response_records_t authorityRecords;
+@property (readonly, nonatomic) of_dns_response_records_t authorityRecords;
/*!
* @brief The additional records of the response.
+ *
+ * This is a dictionary with the key being the domain name and the value being
+ * an array of @ref OFDNSResourceRecord.
*/
-@property OF_NULLABLE_PROPERTY (readonly, nonatomic)
- of_dns_response_records_t additionalRecords;
+@property (readonly, nonatomic) of_dns_response_records_t additionalRecords;
/*!
* @brief Creates a new, autoreleased OFDNSResponse.
@@ -66,11 +78,10 @@ typedef OFDictionary OF_GENERIC(OFString *, OFArray OF_GENERIC(
* @return A new, autoreleased OFDNSResponse
*/
+ (instancetype)
- responseWithAnswerRecords: (nullable of_dns_response_records_t)answerRecords
- authorityRecords: (nullable of_dns_response_records_t)
- authorityRecords
- additionalRecords: (nullable of_dns_response_records_t)
- additionalRecords;
+ responseWithDomainName: (OFString *)domainName
+ answerRecords: (of_dns_response_records_t)answerRecords
+ authorityRecords: (of_dns_response_records_t)authorityRecords
+ additionalRecords: (of_dns_response_records_t)additionalRecords;
/*!
* @brief Initializes an already allocated OFDNSResponse.
@@ -81,9 +92,10 @@ typedef OFDictionary OF_GENERIC(OFString *, OFArray OF_GENERIC(
* @return An initialized OFDNSResponse
*/
- (instancetype)
- initWithAnswerRecords: (nullable of_dns_response_records_t)answerRecords
- authorityRecords: (nullable of_dns_response_records_t)authorityRecords
- additionalRecords: (nullable of_dns_response_records_t)additionalRecords
+ initWithDomainName: (OFString *)domainName
+ answerRecords: (of_dns_response_records_t)answerRecords
+ authorityRecords: (of_dns_response_records_t)authorityRecords
+ additionalRecords: (of_dns_response_records_t)additionalRecords
OF_DESIGNATED_INITIALIZER;
- (instancetype)init OF_UNAVAILABLE;
diff --git a/src/OFDNSResponse.m b/src/OFDNSResponse.m
index c90babad..f73bcb97 100644
--- a/src/OFDNSResponse.m
+++ b/src/OFDNSResponse.m
@@ -22,29 +22,32 @@
#import "OFString.h"
@implementation OFDNSResponse
-@synthesize answerRecords = _answerRecords;
+@synthesize domainName = _domainName, answerRecords = _answerRecords;
@synthesize authorityRecords = _authorityRecords;
@synthesize additionalRecords = _additionalRecords;
+ (instancetype)
- responseWithAnswerRecords: (of_dns_response_records_t)answerRecords
- authorityRecords: (of_dns_response_records_t)authorityRecords
- additionalRecords: (of_dns_response_records_t)additionalRecords
+ responseWithDomainName: (OFString *)domainName
+ answerRecords: (of_dns_response_records_t)answerRecords
+ authorityRecords: (of_dns_response_records_t)authorityRecords
+ additionalRecords: (of_dns_response_records_t)additionalRecords
{
return [[[self alloc]
- initWithAnswerRecords: answerRecords
- authorityRecords: authorityRecords
- additionalRecords: additionalRecords] autorelease];
+ initWithDomainName: domainName
+ answerRecords: answerRecords
+ authorityRecords: authorityRecords
+ additionalRecords: additionalRecords] autorelease];
}
-- (instancetype)
- initWithAnswerRecords: (of_dns_response_records_t)answerRecords
- authorityRecords: (of_dns_response_records_t)authorityRecords
- additionalRecords: (of_dns_response_records_t)additionalRecords
+- (instancetype)initWithDomainName: (OFString *)domainName
+ answerRecords: (of_dns_response_records_t)answerRecords
+ authorityRecords: (of_dns_response_records_t)authorityRecords
+ additionalRecords: (of_dns_response_records_t)additionalRecords
{
self = [super init];
@try {
+ _domainName = [domainName copy];
_answerRecords = [answerRecords copy];
_authorityRecords = [authorityRecords copy];
_additionalRecords = [additionalRecords copy];
@@ -63,6 +66,7 @@
- (void)dealloc
{
+ [_domainName release];
[_answerRecords release];
[_authorityRecords release];
[_additionalRecords release];
@@ -72,21 +76,24 @@
- (bool)isEqual: (id)object
{
- OFDNSResponse *other;
+ OFDNSResponse *response;
if (![object isKindOfClass: [OFDNSResponse class]])
return false;
- other = object;
+ response = object;
- if (other->_answerRecords != _answerRecords &&
- ![other->_answerRecords isEqual: _answerRecords])
+ if (response->_domainName != _domainName &&
+ ![response->_domainName isEqual: _domainName])
return false;
- if (other->_authorityRecords != _authorityRecords &&
- ![other->_authorityRecords isEqual: _authorityRecords])
+ if (response->_answerRecords != _answerRecords &&
+ ![response->_answerRecords isEqual: _answerRecords])
return false;
- if (other->_additionalRecords != _additionalRecords &&
- ![other->_additionalRecords isEqual: _additionalRecords])
+ if (response->_authorityRecords != _authorityRecords &&
+ ![response->_authorityRecords isEqual: _authorityRecords])
+ return false;
+ if (response->_additionalRecords != _additionalRecords &&
+ ![response->_additionalRecords isEqual: _additionalRecords])
return false;
return true;
@@ -97,6 +104,7 @@
uint32_t hash;
OF_HASH_INIT(hash);
+ OF_HASH_ADD_HASH(hash, _domainName.hash);
OF_HASH_ADD_HASH(hash, [_answerRecords hash]);
OF_HASH_ADD_HASH(hash, [_authorityRecords hash]);
OF_HASH_ADD_HASH(hash, [_additionalRecords hash]);
@@ -119,10 +127,12 @@
return [OFString stringWithFormat:
@"<%@:\n"
+ @"\tDomain name = %@\n"
@"\tAnswer records = %@\n"
@"\tAuthority records = %@\n"
@"\tAdditional records = %@\n"
@">",
- self.className, answerRecords, authorityRecords, additionalRecords];
+ self.className, _domainName, answerRecords, authorityRecords,
+ additionalRecords];
}
@end
diff --git a/src/OFHostAddressResolver.h b/src/OFHostAddressResolver.h
new file mode 100644
index 00000000..db4167b7
--- /dev/null
+++ b/src/OFHostAddressResolver.h
@@ -0,0 +1,54 @@
+/*
+ * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017,
+ * 2018, 2019
+ * Jonathan Schleifer <js@heap.zone>
+ *
+ * All rights reserved.
+ *
+ * This file is part of ObjFW. It may be distributed under the terms of the
+ * Q Public License 1.0, which can be found in the file LICENSE.QPL included in
+ * the packaging of this file.
+ *
+ * Alternatively, it may be distributed under the terms of the GNU General
+ * Public License, either version 2 or 3, which can be found in the file
+ * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
+ * file.
+ */
+
+#import "OFObject.h"
+#import "OFDNSResolver.h"
+#import "OFRunLoop.h"
+
+#import "socket.h"
+
+OF_ASSUME_NONNULL_BEGIN
+
+@class OFDNSResolverSettings;
+@class OFDNSResourceRecord;
+@class OFMutableArray OF_GENERIC(ObjectType);
+@class OFMutableData;
+@class OFString;
+
+@interface OFHostAddressResolver: OFObject <OFDNSResolverDelegate>
+{
+ OFString *_host;
+ of_socket_address_family_t _addressFamily;
+ OFDNSResolver *_resolver;
+ OFDNSResolverSettings *_settings;
+ of_run_loop_mode_t _Nullable _runLoopMode;
+ id <OFDNSResolverDelegate> _Nullable _delegate;
+ unsigned int _numExpectedResponses;
+ OFMutableData *_addresses;
+}
+
+- (instancetype)initWithHost: (OFString *)host
+ addressFamily: (of_socket_address_family_t)addressFamily
+ resolver: (OFDNSResolver *)resolver
+ settings: (OFDNSResolverSettings *)settings
+ runLoopMode: (nullable of_run_loop_mode_t)runLoopMode
+ delegate: (nullable id <OFDNSResolverDelegate>)delegate;
+- (void)asyncResolve;
+- (OFData *)resolve;
+@end
+
+OF_ASSUME_NONNULL_END
diff --git a/src/OFHostAddressResolver.m b/src/OFHostAddressResolver.m
new file mode 100644
index 00000000..dd5dc846
--- /dev/null
+++ b/src/OFHostAddressResolver.m
@@ -0,0 +1,343 @@
+/*
+ * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017,
+ * 2018, 2019
+ * Jonathan Schleifer <js@heap.zone>
+ *
+ * All rights reserved.
+ *
+ * This file is part of ObjFW. It may be distributed under the terms of the
+ * Q Public License 1.0, which can be found in the file LICENSE.QPL included in
+ * the packaging of this file.
+ *
+ * Alternatively, it may be distributed under the terms of the GNU General
+ * Public License, either version 2 or 3, which can be found in the file
+ * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
+ * file.
+ */
+
+#include "config.h"
+
+#import "OFHostAddressResolver.h"
+#import "OFDNSResolver.h"
+#import "OFDNSResolverSettings.h"
+#import "OFData.h"
+#import "OFDate.h"
+#import "OFDictionary.h"
+#import "OFRunLoop.h"
+#import "OFString.h"
+#import "OFTimer.h"
+
+#import "OFInvalidArgumentException.h"
+#import "OFInvalidFormatException.h"
+#import "OFResolveHostFailedException.h"
+
+@interface OFHostAddressResolverDelegate: OFObject <OFDNSResolverDelegate>
+{
+@public
+ bool _done;
+ OFData *_addresses;
+ id _exception;
+}
+@end
+
+static const of_run_loop_mode_t resolveRunLoopMode =
+ @"of_host_address_resolver_resolve_mode";
+
+static bool
+addressForRecord(OF_KINDOF(OFDNSResourceRecord *) record,
+ const of_socket_address_t **address,
+ of_socket_address_family_t addressFamily)
+{
+ switch ([record recordType]) {
+#ifdef OF_HAVE_IPV6
+ case OF_DNS_RESOURCE_RECORD_TYPE_AAAA:
+ if (addressFamily != OF_SOCKET_ADDRESS_FAMILY_IPV6 &&
+ addressFamily != OF_SOCKET_ADDRESS_FAMILY_ANY)
+ return false;
+ break;
+#endif
+ case OF_DNS_RESOURCE_RECORD_TYPE_A:
+ if (addressFamily != OF_SOCKET_ADDRESS_FAMILY_IPV4 &&
+ addressFamily != OF_SOCKET_ADDRESS_FAMILY_ANY)
+ return false;
+ break;
+ default:
+ return false;
+ }
+
+ *address = [record address];
+ return true;
+}
+
+static void
+callDelegateInMode(of_run_loop_mode_t runLoopMode,
+ id <OFDNSResolverDelegate> delegate, OFDNSResolver *resolver,
+ OFString *host, OFData *addresses, id exception)
+{
+ SEL selector = @selector(resolver:didResolveHost:addresses:exception:);
+
+ if ([delegate respondsToSelector: selector]) {
+ OFTimer *timer = [OFTimer
+ timerWithTimeInterval: 0
+ target: delegate
+ selector: selector
+ object: resolver
+ object: host
+ object: addresses
+ object: exception
+ repeats: false];
+ [[OFRunLoop currentRunLoop] addTimer: timer
+ forMode: runLoopMode];
+ }
+}
+
+@implementation OFHostAddressResolver: OFObject
+- (instancetype)initWithHost: (OFString *)host
+ addressFamily: (of_socket_address_family_t)addressFamily
+ resolver: (OFDNSResolver *)resolver
+ settings: (OFDNSResolverSettings *)settings
+ runLoopMode: (of_run_loop_mode_t)runLoopMode
+ delegate: (id <OFDNSResolverDelegate>)delegate
+{
+ self = [super init];
+
+ @try {
+ _host = [host copy];
+ _addressFamily = addressFamily;
+ _resolver = [resolver retain];
+ _settings = [settings copy];
+ _runLoopMode = [runLoopMode copy];
+ _delegate = [delegate retain];
+ } @catch (id e) {
+ [self release];
+ @throw e;
+ }
+
+ return self;
+}
+
+- (void)dealloc
+{
+ [_host release];
+ [_resolver release];
+ [_settings release];
+ [_runLoopMode release];
+ [_delegate release];
+ [_addresses release];
+
+ [super dealloc];
+}
+
+- (void)resolver: (OFDNSResolver *)resolver
+ didPerformQuery: (OFDNSQuery *)query
+ response: (OFDNSResponse *)response
+ exception: (id)exception
+{
+ _numExpectedResponses--;
+
+ if (exception != nil && _numExpectedResponses == 0) {
+ if ([_delegate respondsToSelector:
+ @selector(resolver:didResolveHost:addresses:exception:)])
+ [_delegate resolver: _resolver
+ didResolveHost: _host
+ addresses: nil
+ exception: exception];
+ return;
+ }
+
+ for (OF_KINDOF(OFDNSResourceRecord *) record in
+ [response.answerRecords objectForKey: query.domainName]) {
+ const of_socket_address_t *address;
+ OFDNSQuery *CNAMEQuery;
+
+ if ([record DNSClass] != OF_DNS_CLASS_IN)
+ continue;
+
+ if (addressForRecord(record, &address, _addressFamily)) {
+ [_addresses addItem: address];
+ continue;
+ }
+
+ if ([record recordType] != OF_DNS_RESOURCE_RECORD_TYPE_CNAME)
+ continue;
+
+ /* FIXME: Check if it's already in answers */
+ CNAMEQuery = [OFDNSQuery queryWithDomainName: [record alias]
+ DNSClass: OF_DNS_CLASS_IN
+ recordType: query.recordType];
+ _numExpectedResponses++;
+ [_resolver asyncPerformQuery: CNAMEQuery
+ runLoopMode: _runLoopMode
+ delegate: self];
+ }
+
+ if (_numExpectedResponses > 0)
+ return;
+
+ [_addresses makeImmutable];
+
+ if ([_delegate respondsToSelector:
+ @selector(resolver:didResolveHost:addresses:exception:)])
+ [_delegate resolver: _resolver
+ didResolveHost: _host
+ addresses: _addresses
+ exception: exception];
+}
+
+- (void)sendQueries
+{
+ /* FIXME: Add seach domain */
+ OFString *domainName = _host;
+ OFDNSQuery *query;
+
+#ifdef OF_HAVE_IPV6
+ if (_addressFamily == OF_SOCKET_ADDRESS_FAMILY_IPV6 ||
+ _addressFamily == OF_SOCKET_ADDRESS_FAMILY_ANY) {
+ query = [OFDNSQuery
+ queryWithDomainName: domainName
+ DNSClass: OF_DNS_CLASS_IN
+ recordType: OF_DNS_RESOURCE_RECORD_TYPE_AAAA];
+ _numExpectedResponses++;
+ [_resolver asyncPerformQuery: query
+ runLoopMode: _runLoopMode
+ delegate: self];
+ }
+#endif
+
+ if (_addressFamily == OF_SOCKET_ADDRESS_FAMILY_IPV4 ||
+ _addressFamily == OF_SOCKET_ADDRESS_FAMILY_ANY) {
+ query = [OFDNSQuery
+ queryWithDomainName: domainName
+ DNSClass: OF_DNS_CLASS_IN
+ recordType: OF_DNS_RESOURCE_RECORD_TYPE_A];
+ _numExpectedResponses++;
+ [_resolver asyncPerformQuery: query
+ runLoopMode: _runLoopMode
+ delegate: self];
+ }
+}
+
+- (void)asyncResolve
+{
+ void *pool = objc_autoreleasePoolPush();
+ OFArray OF_GENERIC(OFString *) *aliases;
+
+ @try {
+ of_socket_address_t address =
+ of_socket_address_parse_ip(_host, 0);
+ OFData *addresses = nil;
+ id exception = nil;
+
+ if (_addressFamily == address.family ||
+ _addressFamily == OF_SOCKET_ADDRESS_FAMILY_ANY)
+ addresses = [OFData dataWithItems: &address
+ itemSize: sizeof(address)
+ count: 1];
+ else
+ exception = [OFInvalidArgumentException exception];
+
+ callDelegateInMode(_runLoopMode, _delegate, _resolver, _host,
+ addresses, exception);
+
+ objc_autoreleasePoolPop(pool);
+ return;
+ } @catch (OFInvalidFormatException *e) {
+ }
+
+ if ((aliases = [_settings->_staticHosts objectForKey: _host]) != nil) {
+ OFMutableData *addresses = [OFMutableData
+ dataWithItemSize: sizeof(of_socket_address_t)];
+ id exception = nil;
+
+ for (OFString *alias in aliases) {
+ of_socket_address_t address;
+
+ @try {
+ address = of_socket_address_parse_ip(alias, 0);
+ } @catch (OFInvalidFormatException *e) {
+ continue;
+ }
+
+ if (_addressFamily != address.family &&
+ _addressFamily != OF_SOCKET_ADDRESS_FAMILY_ANY)
+ continue;
+
+ [addresses addItem: &address];
+ }
+
+ [addresses makeImmutable];
+
+ if (addresses.count == 0) {
+ addresses = nil;
+ exception = [OFResolveHostFailedException
+ exceptionWithHost: _host
+ addressFamily: _addressFamily
+ error: OF_DNS_RESOLVER_ERROR_NO_RESULT];
+ }
+
+ callDelegateInMode(_runLoopMode, _delegate, _resolver, _host,
+ addresses, exception);
+
+ objc_autoreleasePoolPop(pool);
+ return;
+ }
+
+ _addresses = [[OFMutableData alloc]
+ initWithItemSize: sizeof(of_socket_address_t)];
+
+ [self sendQueries];
+
+ objc_autoreleasePoolPop(pool);
+}
+
+- (OFData *)resolve
+{
+ void *pool = objc_autoreleasePoolPush();
+ OFRunLoop *runLoop = [OFRunLoop currentRunLoop];
+ OFHostAddressResolverDelegate *delegate;
+ OFData *ret;
+
+ delegate = [[[OFHostAddressResolverDelegate alloc] init] autorelease];
+ _runLoopMode = [resolveRunLoopMode copy];
+ _delegate = [delegate retain];
+
+ [self asyncResolve];
+
+ while (!delegate->_done)
+ [runLoop runMode: resolveRunLoopMode
+ beforeDate: nil];
+
+ /* Cleanup */
+ [runLoop runMode: resolveRunLoopMode
+ beforeDate: [OFDate date]];
+
+ if (delegate->_exception != nil)
+ @throw delegate->_exception;
+
+ ret = [delegate->_addresses copy];
+
+ objc_autoreleasePoolPop(pool);
+
+ return [ret autorelease];
+}
+@end
+
+@implementation OFHostAddressResolverDelegate
+- (void)dealloc
+{
+ [_addresses release];
+ [_exception release];
+
+ [super dealloc];
+}
+
+- (void)resolver: (OFDNSResolver *)resolver
+ didResolveHost: (OFString *)host
+ addresses: (OFData *)addresses
+ exception: (id)exception
+{
+ _addresses = [addresses copy];
+ _exception = [exception retain];
+ _done = true;
+}
+@end
diff --git a/src/OFTCPSocket.m b/src/OFTCPSocket.m
index 2caf13eb..beff4c05 100644
--- a/src/OFTCPSocket.m
+++ b/src/OFTCPSocket.m
@@ -321,10 +321,10 @@ static uint16_t defaultSOCKS5Port = 1080;
[self didConnect];
}
-- (void)resolver: (OFDNSResolver *)resolver
- didResolveDomainName: (OFString *)domainName
- socketAddresses: (OFData *)socketAddresses
- exception: (id)exception
+- (void)resolver: (OFDNSResolver *)resolver
+ didResolveHost: (OFString *)host
+ addresses: (OFData *)addresses
+ exception: (id)exception
{
if (exception != nil) {
_exception = [exception retain];
@@ -332,7 +332,7 @@ static uint16_t defaultSOCKS5Port = 1080;
return;
}
- _socketAddresses = [socketAddresses copy];
+ _socketAddresses = [addresses copy];
[self tryNextAddressWithRunLoopMode:
[OFRunLoop currentRunLoop].currentMode];
@@ -369,10 +369,10 @@ static uint16_t defaultSOCKS5Port = 1080;
}
[[OFThread DNSResolver]
- asyncResolveSocketAddressesForHost: host
- addressFamily: OF_SOCKET_ADDRESS_FAMILY_ANY
- runLoopMode: runLoopMode
- delegate: self];
+ asyncResolveAddressesForHost: host
+ addressFamily: OF_SOCKET_ADDRESS_FAMILY_ANY
+ runLoopMode: runLoopMode
+ delegate: self];
}
- (void)sendSOCKS5Request
@@ -812,8 +812,8 @@ static uint16_t defaultSOCKS5Port = 1080;
object: self];
socketAddresses = [[OFThread DNSResolver]
- resolveSocketAddressesForHost: host
- addressFamily: OF_SOCKET_ADDRESS_FAMILY_ANY];
+ resolveAddressesForHost: host
+ addressFamily: OF_SOCKET_ADDRESS_FAMILY_ANY];
address = *(of_socket_address_t *)[socketAddresses itemAtIndex: 0];
of_socket_address_set_port(&address, port);
diff --git a/src/OFUDPSocket.m b/src/OFUDPSocket.m
index de0fc38b..e10ef746 100644
--- a/src/OFUDPSocket.m
+++ b/src/OFUDPSocket.m
@@ -266,8 +266,8 @@
@throw [OFAlreadyConnectedException exceptionWithSocket: self];
socketAddresses = [[OFThread DNSResolver]
- resolveSocketAddressesForHost: host
- addressFamily: OF_SOCKET_ADDRESS_FAMILY_ANY];
+ resolveAddressesForHost: host
+ addressFamily: OF_SOCKET_ADDRESS_FAMILY_ANY];
address = *(of_socket_address_t *)[socketAddresses itemAtIndex: 0];
of_socket_address_set_port(&address, port);
diff --git a/src/ObjFW.h b/src/ObjFW.h
index 39619811..8f249810 100644
--- a/src/ObjFW.h
+++ b/src/ObjFW.h
@@ -148,7 +148,6 @@
# import "OFAcceptFailedException.h"
# import "OFAlreadyConnectedException.h"
# import "OFBindFailedException.h"
-# import "OFDNSQueryFailedException.h"
#endif
#import "OFChangeCurrentDirectoryPathFailedException.h"
#import "OFChecksumMismatchException.h"
@@ -166,6 +165,11 @@
#import "OFCreateSymbolicLinkFailedException.h"
#ifdef OF_WINDOWS
# import "OFCreateWindowsRegistryKeyFailedException.h"
+#endif
+#ifdef OF_HAVE_SOCKETS
+# import "OFDNSQueryFailedException.h"
+#endif
+#ifdef OF_WINDOWS
# import "OFDeleteWindowsRegistryKeyFailedException.h"
# import "OFDeleteWindowsRegistryValueFailedException.h"
#endif
@@ -212,6 +216,9 @@
#import "OFReadFailedException.h"
#import "OFReadOrWriteFailedException.h"
#import "OFRemoveItemFailedException.h"
+#ifdef OF_HAVE_SOCKETS
+# import "OFResolveHostFailedException.h"
+#endif
#import "OFRetrieveItemAttributesFailedException.h"
#import "OFSandboxActivationFailedException.h"
#import "OFSeekFailedException.h"
diff --git a/src/exceptions/Makefile b/src/exceptions/Makefile
index b71bc3b2..0e38adce 100644
--- a/src/exceptions/Makefile
+++ b/src/exceptions/Makefile
@@ -61,7 +61,8 @@ SRCS_SOCKETS = OFAcceptFailedException.m \
OFDNSQueryFailedException.m \
OFHTTPRequestFailedException.m \
OFListenFailedException.m \
- OFObserveFailedException.m
+ OFObserveFailedException.m \
+ OFResolveHostFailedException.m
SRCS_THREADS = OFConditionBroadcastFailedException.m \
OFConditionSignalFailedException.m \
OFConditionStillWaitingException.m \
diff --git a/src/exceptions/OFDNSQueryFailedException.h b/src/exceptions/OFDNSQueryFailedException.h
index 6897d395..f111a398 100644
--- a/src/exceptions/OFDNSQueryFailedException.h
+++ b/src/exceptions/OFDNSQueryFailedException.h
@@ -26,7 +26,7 @@ OF_ASSUME_NONNULL_BEGIN
* @class OFDNSQueryFailedException \
* OFDNSQueryFailedException.h ObjFW/OFDNSQueryFailedException.h
*
- * @brief An exception indicating the resolving a host failed.
+ * @brief An exception indicating that a DNS query failed.
*/
@interface OFDNSQueryFailedException: OFException
{
@@ -45,7 +45,7 @@ OF_ASSUME_NONNULL_BEGIN
@property (readonly, nonatomic) of_dns_resolver_error_t error;
/*!
- * @brief Creates a new, autoreleased resolve host failed exception.
+ * @brief Creates a new, autoreleased DNS query failed exception.
*
* @param query The query which could not be performed
* @param error The error from the resolver
@@ -55,7 +55,7 @@ OF_ASSUME_NONNULL_BEGIN
error: (of_dns_resolver_error_t)error;
/*!
- * @brief Initializes an already allocated address translation failed exception.
+ * @brief Initializes an already allocated DNS query failed exception.
*
* @param query The query which could not be performed
* @param error The error from the resolver
@@ -65,4 +65,12 @@ OF_ASSUME_NONNULL_BEGIN
error: (of_dns_resolver_error_t)error;
@end
+#ifdef __cplusplus
+extern "C" {
+#endif
+extern OFString *of_dns_resolver_error_to_string(of_dns_resolver_error_t error);
+#ifdef __cplusplus
+}
+#endif
+
OF_ASSUME_NONNULL_END
diff --git a/src/exceptions/OFDNSQueryFailedException.m b/src/exceptions/OFDNSQueryFailedException.m
index 327e5c9a..c3eaaff3 100644
--- a/src/exceptions/OFDNSQueryFailedException.m
+++ b/src/exceptions/OFDNSQueryFailedException.m
@@ -20,6 +20,35 @@
#import "OFDNSQueryFailedException.h"
#import "OFString.h"
+OFString *
+of_dns_resolver_error_to_string(of_dns_resolver_error_t error)
+{
+ switch (error) {
+ case OF_DNS_RESOLVER_ERROR_TIMEOUT:
+ return @"The query timed out.";
+ case OF_DNS_RESOLVER_ERROR_CANCELED:
+ return @"The query was canceled.";
+ case OF_DNS_RESOLVER_ERROR_NO_RESULT:
+ return @"No result for the specified host with the specified "
+ @"type and class.";
+ case OF_DNS_RESOLVER_ERROR_SERVER_INVALID_FORMAT:
+ return @"The server considered the query to be malformed.";
+ case OF_DNS_RESOLVER_ERROR_SERVER_FAILURE:
+ return @"The server was unable to process due to an internal "
+ @"error.";
+ case OF_DNS_RESOLVER_ERROR_SERVER_NAME_ERROR:
+ return @"The server returned an error that the domain does not "
+ @"exist.";
+ case OF_DNS_RESOLVER_ERROR_SERVER_NOT_IMPLEMENTED:
+ return @"The server does not have support for the requested "
+ @"query.";
+ case OF_DNS_RESOLVER_ERROR_SERVER_REFUSED:
+ return @"The server refused the query.";
+ default:
+ return @"Unknown error.";
+ }
+}
+
@implementation OFDNSQueryFailedException
@synthesize query = _query, error = _error;
@@ -55,42 +84,8 @@
- (OFString *)description
{
- OFString *error;
-
- switch (_error) {
- case OF_DNS_RESOLVER_ERROR_TIMEOUT:
- error = @"The query timed out.";
- break;
- case OF_DNS_RESOLVER_ERROR_CANCELED:
- error = @"The query was canceled.";
- break;
- case OF_DNS_RESOLVER_ERROR_NO_RESULT:
- error = @"No result for the specified host with the specified "
- @"type and class.";
- break;
- case OF_DNS_RESOLVER_ERROR_SERVER_INVALID_FORMAT:
- error = @"The server considered the query to be malformed.";
- break;
- case OF_DNS_RESOLVER_ERROR_SERVER_FAILURE:
- error = @"The server was unable to process due to an internal "
- @"error.";
- break;
- case OF_DNS_RESOLVER_ERROR_SERVER_NAME_ERROR:
- error = @"The server returned an error that the domain does "
- @"not exist.";
- break;
- case OF_DNS_RESOLVER_ERROR_SERVER_NOT_IMPLEMENTED:
- error = @"The server does not have support for the requested "
- @"query.";
- case OF_DNS_RESOLVER_ERROR_SERVER_REFUSED:
- error = @"The server refused the query.";
- break;
- default:
- error = @"Unknown error.";
- break;
- }
-
return [OFString stringWithFormat:
- @"Query %@ could not be performed: %@", _query, error];
+ @"DNS query %@ could not be performed: %@",
+ _query, of_dns_resolver_error_to_string(_error)];
}
@end
diff --git a/src/exceptions/OFResolveHostFailedException.h b/src/exceptions/OFResolveHostFailedException.h
new file mode 100644
index 00000000..8b361509
--- /dev/null
+++ b/src/exceptions/OFResolveHostFailedException.h
@@ -0,0 +1,78 @@
+/*
+ * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017,
+ * 2018, 2019
+ * Jonathan Schleifer <js@heap.zone>
+ *
+ * All rights reserved.
+ *
+ * This file is part of ObjFW. It may be distributed under the terms of the
+ * Q Public License 1.0, which can be found in the file LICENSE.QPL included in
+ * the packaging of this file.
+ *
+ * Alternatively, it may be distributed under the terms of the GNU General
+ * Public License, either version 2 or 3, which can be found in the file
+ * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
+ * file.
+ */
+
+#import "OFException.h"
+#import "OFDNSResolver.h"
+
+OF_ASSUME_NONNULL_BEGIN
+
+/*!
+ * @class OFResolveHostFailedException \
+ * OFResolveHostFailedException.h ObjFW/OFResolveHostFailedException.h
+ *
+ * @brief An exception indicating that resolving a host failed.
+ */
+@interface OFResolveHostFailedException: OFException
+{
+ OFString *_host;
+ of_socket_address_family_t _addressFamily;
+ of_dns_resolver_error_t _error;
+}
+
+/*!
+ * @brief The host which could not be resolved.
+ */
+@property (readonly, nonatomic) OFString *host;
+
+/*!
+ * @brief The address family for which the host could not be resolved.
+ */
+@property (readonly, nonatomic) of_socket_address_family_t addressFamily;
+
+/*!
+ * @brief The error from the resolver.
+ */
+@property (readonly, nonatomic) of_dns_resolver_error_t error;
+
+/*!
+ * @brief Creates a new, autoreleased resolve host failed exception.
+ *
+ * @param host The host which could not be resolved
+ * @param addressFamily The address family for which the host could not be
+ * resolved
+ * @param error The error from the resolver
+ * @return A new, autoreleased address translation failed exception
+ */
++ (instancetype)exceptionWithHost: (OFString *)host
+ addressFamily: (of_socket_address_family_t)addressFamily
+ error: (of_dns_resolver_error_t)error;
+
+/*!
+ * @brief Initializes an already allocated resolve host failed exception.
+ *
+ * @param host The host which could not be resolved
+ * @param addressFamily The address family for which the host could not be
+ * resolved
+ * @param error The error from the resolver
+ * @return An initialized address translation failed exception
+ */
+- (instancetype)initWithHost: (OFString *)host
+ addressFamily: (of_socket_address_family_t)addressFamily
+ error: (of_dns_resolver_error_t)error;
+@end
+
+OF_ASSUME_NONNULL_END
diff --git a/src/exceptions/OFResolveHostFailedException.m b/src/exceptions/OFResolveHostFailedException.m
new file mode 100644
index 00000000..92702467
--- /dev/null
+++ b/src/exceptions/OFResolveHostFailedException.m
@@ -0,0 +1,67 @@
+/*
+ * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017,
+ * 2018, 2019
+ * Jonathan Schleifer <js@heap.zone>
+ *
+ * All rights reserved.
+ *
+ * This file is part of ObjFW. It may be distributed under the terms of the
+ * Q Public License 1.0, which can be found in the file LICENSE.QPL included in
+ * the packaging of this file.
+ *
+ * Alternatively, it may be distributed under the terms of the GNU General
+ * Public License, either version 2 or 3, which can be found in the file
+ * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
+ * file.
+ */
+
+#include "config.h"
+
+#import "OFResolveHostFailedException.h"
+#import "OFDNSQueryFailedException.h"
+#import "OFString.h"
+
+@implementation OFResolveHostFailedException
+@synthesize host = _host, addressFamily = _addressFamily, error = _error;
+
++ (instancetype)exceptionWithHost: (OFString *)host
+ addressFamily: (of_socket_address_family_t)addressFamily
+ error: (of_dns_resolver_error_t)error
+{
+ return [[[self alloc] initWithHost: host
+ addressFamily: addressFamily
+ error: error] autorelease];
+}
+
+- (instancetype)initWithHost: (OFString *)host
+ addressFamily: (of_socket_address_family_t)addressFamily
+ error: (of_dns_resolver_error_t)error
+{
+ self = [super init];
+
+ @try {
+ _host = [host copy];
+ _addressFamily = addressFamily;
+ _error = error;
+ } @catch (id e) {
+ [self release];
+ @throw e;
+ }
+
+ return self;
+}
+
+- (void)dealloc
+{
+ [_host release];
+
+ [super dealloc];
+}
+
+- (OFString *)description
+{
+ return [OFString stringWithFormat:
+ @"The host %@ could not be resolved: %@",
+ _host, of_dns_resolver_error_to_string(_error)];
+}
+@end
diff --git a/utils/ofdns/OFDNS.m b/utils/ofdns/OFDNS.m
index 53e90bc2..566f6fdc 100644
--- a/utils/ofdns/OFDNS.m
+++ b/utils/ofdns/OFDNS.m
@@ -29,19 +29,17 @@
OF_APPLICATION_DELEGATE(OFDNS)
@implementation OFDNS
-- (void)resolver: (OFDNSResolver *)resolver
- didResolveDomainName: (OFString *)domainName
- response: (OFDNSResponse *)response
- exception: (id)exception
+- (void)resolver: (OFDNSResolver *)resolver
+ didPerformQuery: (OFDNSQuery *)query
+ response: (OFDNSResponse *)response
+ exception: (id)exception
{
if (exception != nil) {
[of_stderr writeFormat: @"Failed to resolve: %@\n", exception];
[OFApplication terminateWithStatus: 1];
}
- [of_stdout writeFormat: @"FQDN: %@\n"
- @"Response: %@\n",
- domainName, response];
+ [of_stdout writeFormat: @"%@\n", response];
[OFApplication terminate];
}
@@ -49,8 +47,7 @@ OF_APPLICATION_DELEGATE(OFDNS)
- (void)applicationDidFinishLaunching
{
OFArray OF_GENERIC(OFString *) *arguments = [OFApplication arguments];
- of_dns_resource_record_class_t recordClass =
- OF_DNS_RESOURCE_RECORD_CLASS_ANY;
+ of_dns_class_t DNSClass = OF_DNS_CLASS_ANY;
of_dns_resource_record_type_t recordType =
OF_DNS_RESOURCE_RECORD_TYPE_ALL;
OFDNSQuery *query;
@@ -82,8 +79,7 @@ OF_APPLICATION_DELEGATE(OFDNS)
[arguments objectAtIndex: 1]);
if (arguments.count >= 3)
- recordClass = of_dns_resource_record_class_parse(
- [arguments objectAtIndex: 2]);
+ DNSClass = of_dns_class_parse([arguments objectAtIndex: 2]);
if (arguments.count >= 4) {
resolver.configReloadInterval = 0;
@@ -91,9 +87,9 @@ OF_APPLICATION_DELEGATE(OFDNS)
[arguments objectsInRange: of_range(3, 1)];
}
- query = [OFDNSQuery queryWithHost: [arguments objectAtIndex: 0]
- recordClass: recordClass
- recordType: recordType];
+ query = [OFDNSQuery queryWithDomainName: [arguments objectAtIndex: 0]
+ DNSClass: DNSClass
+ recordType: recordType];
[resolver asyncPerformQuery: query
delegate: self];
}
diff --git a/utils/ofhttp/OFHTTP.m b/utils/ofhttp/OFHTTP.m
index 1086722c..019f8a53 100644
--- a/utils/ofhttp/OFHTTP.m
+++ b/utils/ofhttp/OFHTTP.m
@@ -36,13 +36,13 @@
#import "OFURL.h"
#import "OFConnectionFailedException.h"
-#import "OFDNSQueryFailedException.h"
#import "OFHTTPRequestFailedException.h"
#import "OFInvalidFormatException.h"
#import "OFInvalidServerReplyException.h"
#import "OFOpenItemFailedException.h"
#import "OFOutOfRangeException.h"
#import "OFReadFailedException.h"
+#import "OFResolveHostFailedException.h"
#import "OFRetrieveItemAttributesFailedException.h"
#import "OFUnsupportedProtocolException.h"
#import "OFWriteFailedException.h"
@@ -612,14 +612,14 @@ fileNameFromContentDisposition(OFString *contentDisposition)
didFailWithException: (id)e
request: (OFHTTPRequest *)request
{
- if ([e isKindOfClass: [OFDNSQueryFailedException class]]) {
+ if ([e isKindOfClass: [OFResolveHostFailedException class]]) {
if (!_quiet)
[of_stdout writeString: @"\n"];
[of_stderr writeLine:
- OF_LOCALIZED(@"download_dns_query_failed",
+ OF_LOCALIZED(@"download_resolve_host_failed",
@"%[prog]: Failed to download <%[url]>!\n"
- @" DNS query failed: %[exception]",
+ @" Failed to resolve host: %[exception]",
@"prog", [OFApplication programName],
@"url", request.URL.string,
@"exception", e)];
diff --git a/utils/ofhttp/lang/de.json b/utils/ofhttp/lang/de.json
index d0726976..5db82137 100644
--- a/utils/ofhttp/lang/de.json
+++ b/utils/ofhttp/lang/de.json
@@ -38,9 +38,9 @@
"%[prog]: -o / --output kann nicht mit mehr als einer URL benutzt ",
"werden!"
],
- "download_dns_query_failed": [
+ "download_resolve_host_failed": [
"%[prog]: Fehler beim Download von <%[url]>!\n",
- " DNS-Abfrage fehlgeschlagen: %[exception]"
+ " Host auflösen fehlgeschlagen: %[exception]"
],
"download_failed_connection_failed": [
"%[prog]: Fehler beim Download von <%[url]>!\n",