ObjFW
OFDNSResolver.h
1 /*
2  * Copyright (c) 2008-2024 Jonathan Schleifer <js@nil.im>
3  *
4  * All rights reserved.
5  *
6  * This program is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License version 3.0 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13  * version 3.0 for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * version 3.0 along with this program. If not, see
17  * <https://www.gnu.org/licenses/>.
18  */
19 
20 #import "OFObject.h"
21 #import "OFDNSQuery.h"
22 #import "OFDNSResourceRecord.h"
23 #import "OFDNSResponse.h"
24 #import "OFRunLoop.h"
25 #import "OFString.h"
26 
27 OF_ASSUME_NONNULL_BEGIN
28 
29 #define OFDNSResolverBufferLength 512
30 
31 @class OFArray OF_GENERIC(ObjectType);
32 @class OFDNSResolver;
33 @class OFDNSResolverContext;
34 @class OFDNSResolverSettings;
35 @class OFDate;
36 @class OFDictionary OF_GENERIC(KeyType, ObjectType);
37 @class OFMutableArray OF_GENERIC(ObjectType);
38 @class OFMutableDictionary OF_GENERIC(KeyType, ObjectType);
39 @class OFNumber;
40 @class OFPair OF_GENERIC(FirstType, SecondType);
41 @class OFTCPSocket;
42 @class OFUDPSocket;
43 
49 typedef enum {
51  OFDNSResolverErrorCodeUnknown,
53  OFDNSResolverErrorCodeTimeout,
55  OFDNSResolverErrorCodeCanceled,
62  OFDNSResolverErrorCodeNoResult,
64  OFDNSResolverErrorCodeServerInvalidFormat,
66  OFDNSResolverErrorCodeServerFailure,
68  OFDNSResolverErrorCodeServerNameError,
70  OFDNSResolverErrorCodeServerNotImplemented,
72  OFDNSResolverErrorCodeServerRefused,
74  OFDNSResolverErrorCodeNoNameServer
75 } OFDNSResolverErrorCode;
76 
92 - (void)resolver: (OFDNSResolver *)resolver
93  didPerformQuery: (OFDNSQuery *)query
94  response: (nullable OFDNSResponse *)response
95  exception: (nullable id)exception;
96 @end
97 
103 @protocol OFDNSResolverHostDelegate <OFObject>
114 - (void)resolver: (OFDNSResolver *)resolver
115  didResolveHost: (OFString *)host
116  addresses: (nullable OFData *)addresses
117  exception: (nullable id)exception;
118 @end
119 
130 OF_SUBCLASSING_RESTRICTED
132 {
133  OFDNSResolverSettings *_settings;
134  OFUDPSocket *_IPv4Socket;
135 #ifdef OF_HAVE_IPV6
136  OFUDPSocket *_IPv6Socket;
137 #endif
138  char _buffer[OFDNSResolverBufferLength];
139  OFMutableDictionary OF_GENERIC(OFNumber *, OFDNSResolverContext *)
140  *_queries;
141  OFMutableDictionary OF_GENERIC(OFTCPSocket *, OFDNSResolverContext *)
142  *_TCPQueries;
143  OFMutableDictionary OF_GENERIC(OFDNSQuery *,
144  OFPair OF_GENERIC(OFDate *, OFDNSResponse *) *) *_cache;
145  OFMutableArray OF_GENERIC(OFString *) *_lastNameServers;
146  OFTimeInterval _lastCacheCleanup;
147 }
148 
158 @property (copy, nonatomic) OFDictionary OF_GENERIC(OFString *,
159  OFArray OF_GENERIC(OFString *) *) *staticHosts;
160 
170 @property (copy, nonatomic) OFArray OF_GENERIC(OFString *) *nameServers;
171 
175 @property OF_NULLABLE_PROPERTY (readonly, nonatomic) OFString *localDomain;
176 
184 @property (copy, nonatomic) OFArray OF_GENERIC(OFString *) *searchDomains;
185 
194 @property (nonatomic) OFTimeInterval timeout;
195 
205 @property (nonatomic) unsigned int maxAttempts;
206 
214 @property (nonatomic) unsigned int minNumberOfDotsInAbsoluteName;
215 
223 @property (nonatomic) bool forcesTCP;
224 
234 @property (nonatomic) OFTimeInterval configReloadInterval;
235 
239 + (instancetype)resolver;
240 
244 - (instancetype)init;
245 
252 - (void)asyncPerformQuery: (OFDNSQuery *)query
253  delegate: (id <OFDNSResolverQueryDelegate>)delegate;
254 
262 - (void)asyncPerformQuery: (OFDNSQuery *)query
263  runLoopMode: (OFRunLoopMode)runLoopMode
264  delegate: (id <OFDNSResolverQueryDelegate>)delegate;
265 
272 - (void)asyncResolveAddressesForHost: (OFString *)host
273  delegate: (id <OFDNSResolverHostDelegate>)delegate;
274 
282 - (void)asyncResolveAddressesForHost: (OFString *)host
283  addressFamily: (OFSocketAddressFamily)addressFamily
284  delegate: (id <OFDNSResolverHostDelegate>)delegate;
285 
294 - (void)asyncResolveAddressesForHost: (OFString *)host
295  addressFamily: (OFSocketAddressFamily)addressFamily
296  runLoopMode: (OFRunLoopMode)runLoopMode
297  delegate: (id <OFDNSResolverHostDelegate>)delegate;
298 
308 - (OFData *)resolveAddressesForHost: (OFString *)host
309  addressFamily: (OFSocketAddressFamily)addressFamily;
310 
314 - (void)close;
315 @end
316 
317 OF_ASSUME_NONNULL_END
A class which provides methods to create and use UDP sockets.
Definition: OFUDPSocket.h:54
An abstract class for storing objects in an array.
Definition: OFArray.h:107
A class for storing, accessing and comparing dates.
Definition: OFDate.h:33
A class for storing constant strings using the @"" literal.
Definition: OFConstantString.h:41
The root class for all other classes inside ObjFW.
Definition: OFObject.h:690
An abstract class for storing, adding and removing objects in an array.
Definition: OFMutableArray.h:47
A class for storing a pair of two objects.
Definition: OFPair.h:29
A class for handling strings.
Definition: OFString.h:142
An abstract class for storing objects in a dictionary.
Definition: OFDictionary.h:82
A class for resolving DNS names.
Definition: OFDNSResolver.h:131
A class which provides methods to create and use TCP sockets.
Definition: OFTCPSocket.h:85
double OFTimeInterval
A time interval in seconds.
Definition: OFObject.h:154
An abstract class for storing and changing objects in a dictionary.
Definition: OFMutableDictionary.h:46
Provides a way to store a number in an object.
Definition: OFNumber.h:46
A delegate for performed DNS queries.
OFSocketAddressFamily
A socket address family.
Definition: OFSocket.h:110
A class for storing arbitrary data in an array.
Definition: OFData.h:45
A class storing a response from OFDNSResolver.
Definition: OFDNSResponse.h:37
A class representing a DNS query.
Definition: OFDNSQuery.h:33