1 | #pragma once |
2 | #include <Poco/Net/IPAddress.h> |
3 | #include <Poco/Net/SocketAddress.h> |
4 | #include <memory> |
5 | #include <Core/Types.h> |
6 | #include <Core/Names.h> |
7 | #include <boost/noncopyable.hpp> |
8 | |
9 | |
10 | namespace DB |
11 | { |
12 | |
13 | /// A singleton implementing DNS names resolving with optional DNS cache |
14 | /// The cache is being updated asynchronous in separate thread (see DNSCacheUpdater) |
15 | /// or it could be updated manually via drop() method. |
16 | class DNSResolver : private boost::noncopyable |
17 | { |
18 | public: |
19 | static DNSResolver & instance(); |
20 | |
21 | DNSResolver(const DNSResolver &) = delete; |
22 | |
23 | /// Accepts host names like 'example.com' or '127.0.0.1' or '::1' and resolve its IP |
24 | Poco::Net::IPAddress resolveHost(const std::string & host); |
25 | |
26 | /// Accepts host names like 'example.com:port' or '127.0.0.1:port' or '[::1]:port' and resolve its IP and port |
27 | Poco::Net::SocketAddress resolveAddress(const std::string & host_and_port); |
28 | |
29 | Poco::Net::SocketAddress resolveAddress(const std::string & host, UInt16 port); |
30 | |
31 | /// Get this server host name |
32 | String getHostName(); |
33 | |
34 | /// Disables caching |
35 | void setDisableCacheFlag(bool is_disabled = true); |
36 | |
37 | /// Drops all caches |
38 | void dropCache(); |
39 | |
40 | /// Updates all known hosts in cache. |
41 | /// Returns true if IP of any host has been changed. |
42 | bool updateCache(); |
43 | |
44 | ~DNSResolver(); |
45 | |
46 | private: |
47 | |
48 | DNSResolver(); |
49 | |
50 | struct Impl; |
51 | std::unique_ptr<Impl> impl; |
52 | |
53 | /// Returns true if IP of host has been changed. |
54 | bool updateHost(const String & host); |
55 | |
56 | void addToNewHosts(const String & host); |
57 | }; |
58 | |
59 | } |
60 | |