Commit | Line | Data |
---|---|---|
36ad976b CH |
1 | /* |
2 | The software in this package is distributed under the GNU General | |
3 | Public License version 2 (with a special exception described below). | |
4 | ||
5 | A copy of GNU General Public License (GPL) is included in this distribution, | |
6 | in the file COPYING.GPL. | |
7 | ||
8 | As a special exception, if other files instantiate templates or use macros | |
9 | or inline functions from this file, or you compile this file and link it | |
10 | with other works to produce a work based on this file, this file | |
11 | does not by itself cause the resulting work to be covered | |
12 | by the GNU General Public License. | |
13 | ||
14 | However the source code for this file must still be made available | |
15 | in accordance with section (3) of the GNU General Public License. | |
16 | ||
17 | This exception does not invalidate any other reasons why a work based | |
18 | on this file might be covered by the GNU General Public License. | |
19 | ||
20 | Christian Herdtweck, Intra2net AG 2015 | |
21 | */ | |
22 | ||
c5b4902d | 23 | #include "dns/dnsmaster.h" |
36ad976b CH |
24 | |
25 | #include <logfunc.hpp> | |
26 | #include <boost/bind.hpp> | |
36ad976b CH |
27 | #include <boost/asio/placeholders.hpp> |
28 | ||
c5b4902d CH |
29 | #include "dns/ippseudoresolver.h" |
30 | #include "dns/dnsresolver.h" | |
96779587 | 31 | |
36ad976b | 32 | using boost::bind; |
36ad976b CH |
33 | using I2n::Logger::GlobalLogger; |
34 | ||
96779587 | 35 | |
36ad976b CH |
36 | DnsMasterItem DnsMaster::TheOnlyInstance; |
37 | ||
96779587 | 38 | void DnsMaster::create_master(const IoServiceItem &io_serv, |
ad83004d CH |
39 | const boost::asio::ip::address &default_name_server, |
40 | const int resolved_ip_ttl_threshold, | |
41 | const int max_address_resolution_attempts, | |
42 | const std::string &cache_file) | |
36ad976b CH |
43 | { |
44 | if (TheOnlyInstance) | |
45 | { | |
46 | GlobalLogger.warning() | |
96779587 CH |
47 | << "Blocking attempt to create another DnsMaster instance!"; |
48 | return; | |
36ad976b CH |
49 | } |
50 | ||
923626c0 | 51 | GlobalLogger.info() << "Creating DNS Cache and Master"; |
96779587 | 52 | DnsCacheItem cache( new DnsCache(io_serv, cache_file) ); |
923626c0 | 53 | TheOnlyInstance.reset( new DnsMaster(io_serv, |
ad83004d | 54 | default_name_server, |
923626c0 CH |
55 | resolved_ip_ttl_threshold, |
56 | max_address_resolution_attempts, | |
57 | cache) | |
58 | ); | |
36ad976b CH |
59 | } |
60 | ||
61 | ||
62 | DnsMaster::DnsMaster(const IoServiceItem &io_serv, | |
ad83004d | 63 | const boost::asio::ip::address &default_name_server, |
923626c0 CH |
64 | const int resolved_ip_ttl_threshold, |
65 | const int max_address_resolution_attempts, | |
96779587 | 66 | const DnsCacheItem &cache) |
36ad976b | 67 | : IoService( io_serv ) |
ad83004d | 68 | , DefaultNameServer( default_name_server ) |
923626c0 CH |
69 | , ResolvedIpTtlThreshold( resolved_ip_ttl_threshold ) |
70 | , MaxAddressResolutionAttempts( max_address_resolution_attempts ) | |
96779587 | 71 | , Cache(cache) |
4e7b6ff9 | 72 | , ResolverMap() |
36ad976b | 73 | { |
36ad976b CH |
74 | } |
75 | ||
76 | ||
77 | DnsMasterItem& DnsMaster::get_instance() | |
78 | { | |
79 | if ( !TheOnlyInstance ) | |
80 | GlobalLogger.error() | |
81 | << "Request to return DnsMaster instance before creating it!"; | |
82 | return TheOnlyInstance; | |
83 | } | |
84 | ||
c5b4902d CH |
85 | DnsMaster::~DnsMaster() |
86 | { | |
87 | GlobalLogger.info() << "DnsMaster is being destructed"; | |
88 | ||
89 | if (DnsMaster::TheOnlyInstance) | |
26b0f687 CH |
90 | { // apparently, this static variable still exists while itself is |
91 | // destructed... | |
92 | //GlobalLogger.warning() << "DnsMaster is being destructed that is not " | |
93 | // << "singleton instance TheOnlyInstance!"; | |
c5b4902d CH |
94 | DnsMaster::TheOnlyInstance.reset(); |
95 | } | |
96 | ||
97 | // Items in ResolverMap and the DnsCache might still be referenced by | |
98 | // Resolvers and are smart pointers, anyway --> nothing to do here | |
99 | } | |
100 | ||
36ad976b CH |
101 | |
102 | ||
923626c0 CH |
103 | ResolverItem& DnsMaster::get_resolver_for( const std::string &hostname, |
104 | const PingProtocol &ping_protocol ) | |
105 | { | |
106 | // find suitable DnsIpProtocol for ping protocol | |
dbe986b9 | 107 | DnsIpProtocol protocol = ping2dns_protocol(ping_protocol); |
923626c0 CH |
108 | return get_resolver_for(hostname, protocol); |
109 | } | |
110 | ||
111 | ||
ad83004d | 112 | ResolverItem& DnsMaster::get_resolver_for(const std::string &hostname, |
e18c1337 | 113 | const DnsIpProtocol &protocol) |
36ad976b | 114 | { |
923626c0 CH |
115 | // create key to ResolverMap |
116 | resolver_key_type key(hostname, protocol); | |
dbe986b9 | 117 | if ( ResolverMap.count(key) == 0 ) |
36ad976b CH |
118 | { // need to create a resolver |
119 | ||
120 | // check if it is an ip address, so can create a simple pseudo resolver | |
dbe986b9 | 121 | if ( is_ip(hostname) ) |
36ad976b | 122 | { |
923626c0 CH |
123 | boost::asio::ip::address ip |
124 | = boost::asio::ip::address::from_string(hostname); | |
125 | if ( (protocol == DNS_IPv4 && !ip.is_v4()) || | |
126 | (protocol == DNS_IPv6 && !ip.is_v6()) ) | |
127 | GlobalLogger.warning() << "Asked to create a DNS resolver " | |
128 | << "for wrong IP protocol: v4 != v6! " | |
129 | << "We will comply."; | |
130 | GlobalLogger.info() << "Creating PseudoResolver for IP " << ip; | |
4e7b6ff9 CH |
131 | ResolverItem new_resolver( new IpPseudoResolver(IoService, |
132 | hostname, | |
133 | Cache) ); | |
dbe986b9 | 134 | ResolverMap[key] = new_resolver; |
36ad976b CH |
135 | } |
136 | else | |
137 | { | |
dbe986b9 CH |
138 | GlobalLogger.info() << "DnsMaster: Creating Resolver for host " |
139 | << hostname << " and protocol " << to_string(protocol); | |
4e7b6ff9 CH |
140 | ResolverItem new_resolver( new DnsResolver(IoService, |
141 | hostname, | |
923626c0 | 142 | protocol, |
4e7b6ff9 | 143 | Cache, |
ad83004d | 144 | DefaultNameServer) ); |
dbe986b9 | 145 | ResolverMap[key] = new_resolver; |
36ad976b CH |
146 | } |
147 | } | |
dbe986b9 | 148 | return ResolverMap[key]; |
36ad976b CH |
149 | } |
150 | ||
151 | /** | |
152 | * return true if given hostname string actually is an IP | |
153 | * | |
96779587 | 154 | * delegates decision to boost::asio::ip::address::from_string |
36ad976b CH |
155 | */ |
156 | bool DnsMaster::is_ip(const std::string &hostname) const | |
157 | { | |
158 | try | |
159 | { | |
96779587 CH |
160 | boost::asio::ip::address ip = boost::asio::ip::address::from_string( |
161 | hostname); | |
36ad976b CH |
162 | return ip.is_v4() || ip.is_v6(); |
163 | } | |
164 | catch ( const std::exception &ex ) | |
165 | { | |
166 | return false; | |
167 | } | |
168 | } | |
169 | ||
923626c0 CH |
170 | |
171 | DnsIpProtocol DnsMaster::ping2dns_protocol(const PingProtocol& pprot) | |
172 | { | |
173 | switch (pprot) | |
174 | { | |
175 | case PingProtocol_ICMP: return DNS_IPv4; break; | |
176 | case PingProtocol_ICMPv6: return DNS_IPv6; break; | |
177 | case PingProtocol_TCP: return DNS_IPv4; break; | |
178 | case PingProtocol_TCP_IPv6: return DNS_IPv6; break; | |
179 | default: | |
180 | GlobalLogger.warning() << "Unexpected ping protocol: " | |
181 | << static_cast<int>(pprot); | |
182 | return DNS_IPALL; | |
183 | break; | |
184 | } | |
185 | } | |
186 | ||
187 | /*boost::asio::ip::address &DnsMaster::get_name_server() const | |
188 | { | |
189 | return NameServer; | |
190 | }*/ | |
191 | ||
192 | int DnsMaster::get_resolved_ip_ttl_threshold() const | |
193 | { | |
194 | return ResolvedIpTtlThreshold; | |
195 | } | |
196 | ||
197 | int DnsMaster::get_max_address_resolution_attempts() const | |
96779587 | 198 | { |
923626c0 | 199 | return MaxAddressResolutionAttempts; |
96779587 CH |
200 | } |
201 | ||
e18c1337 CH |
202 | std::string to_string(const DnsIpProtocol &protocol) |
203 | { | |
204 | switch (protocol) | |
205 | { | |
206 | case DNS_IPv4: return "IPv4"; break; | |
207 | case DNS_IPv6: return "IPv6"; break; | |
208 | case DNS_IPALL: return "IPv4/6"; break; | |
209 | default: GlobalLogger.warning() << "Unexpected protocol in to_string!"; | |
210 | return "Unexpected Protocol"; break; | |
211 | } | |
212 | } | |
36ad976b CH |
213 | // (created using vim -- the world's best text editor) |
214 |