CernVM-FS  2.12.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
dns.h
Go to the documentation of this file.
1 
5 #ifndef CVMFS_NETWORK_DNS_H_
6 #define CVMFS_NETWORK_DNS_H_
7 
8 #include <stdint.h>
9 
10 #include <cstdio>
11 #include <ctime>
12 #include <map>
13 #include <set>
14 #include <string>
15 #include <vector>
16 
17 #include "duplex_cares.h"
18 #include "gtest/gtest_prod.h"
19 #include "util/atomic.h"
20 #include "util/prng.h"
21 #include "util/single_copy.h"
22 
23 namespace dns {
24 
28 enum Failures {
29  kFailOk = 0,
38 
40 };
41 
42 
47  // use system default, currently unused and mapped to IPv4
51 };
52 
53 inline const char *Code2Ascii(const Failures error) {
54  const char *texts[kFailNumEntries + 1];
55  texts[0] = "OK";
56  texts[1] = "invalid resolver addresses";
57  texts[2] = "DNS query timeout";
58  texts[3] = "invalid host name to resolve";
59  texts[4] = "unknown host name";
60  texts[5] = "malformed DNS request";
61  texts[6] = "no IP address for host";
62  texts[7] = "internal error, not yet resolved";
63  texts[8] = "unknown name resolving error";
64  texts[9] = "no text";
65  return texts[error];
66 }
67 
68 
69 std::string ExtractHost(const std::string &url);
70 std::string ExtractPort(const std::string &url);
71 std::string RewriteUrl(const std::string &url, const std::string &ip);
72 std::string StripIp(const std::string &decorated_ip);
73 std::string AddDefaultScheme(const std::string &proxy);
74 
75 
90 class Host {
91  FRIEND_TEST(T_Dns, HostEquivalent);
92  FRIEND_TEST(T_Dns, HostExpired);
93  FRIEND_TEST(T_Dns, HostValid);
94  FRIEND_TEST(T_Dns, HostExtendDeadline);
95  FRIEND_TEST(T_Dns, HostBestAddresses);
96  friend class Resolver;
97 
98  public:
99  static Host ExtendDeadline(const Host &original, unsigned seconds_from_now);
100  Host();
101  Host(const Host &other);
102  Host &operator= (const Host &other);
103  bool IsEquivalent(const Host &other) const;
104  bool IsExpired() const;
105  bool IsValid() const;
106 
107  time_t deadline() const { return deadline_; }
108  int64_t id() const { return id_; }
109  bool HasIpv4() const { return !ipv4_addresses_.empty(); }
110  bool HasIpv6() const { return !ipv6_addresses_.empty(); }
111  const std::set<std::string> &ipv4_addresses() const {
112  return ipv4_addresses_;
113  }
114  const std::set<std::string> &ipv6_addresses() const {
115  return ipv6_addresses_;
116  }
117  const std::set<std::string> &ViewBestAddresses(IpPreference preference) const;
118  const std::string &name() const { return name_; }
119  Failures status() const { return status_; }
120 
121  private:
122  void CopyFrom(const Host &other);
123 
130 
135  time_t deadline_;
136 
140  int64_t id_;
141 
146  std::set<std::string> ipv4_addresses_;
147 
152  std::set<std::string> ipv6_addresses_;
153 
157  std::string name_;
158 
163 };
164 
165 
173  public:
177  static const unsigned kDefaultMinTtl = 60;
178 
182  static const unsigned kDefaultMaxTtl = 86400;
183 
184  Resolver(const bool ipv4_only,
185  const unsigned retries,
186  const unsigned timeout_ms);
187  virtual ~Resolver() { }
188 
192  virtual bool SetResolvers(const std::vector<std::string> &resolvers) = 0;
193  virtual bool SetSearchDomains(const std::vector<std::string> &domains) = 0;
194  virtual void SetSystemResolvers() = 0;
195  virtual void SetSystemSearchDomains() = 0;
196  Host Resolve(const std::string &name);
197  void ResolveMany(const std::vector<std::string> &names,
198  std::vector<Host> *hosts);
199 
200  const std::vector<std::string> &domains() const { return domains_; }
201  bool ipv4_only() const { return ipv4_only_; }
202  const std::vector<std::string> &resolvers() const { return resolvers_; }
203  unsigned retries() const { return retries_; }
204  unsigned timeout_ms() const { return timeout_ms_; }
205  void set_throttle(const unsigned throttle) { throttle_ = throttle; }
206  unsigned throttle() const { return throttle_; }
207  void set_min_ttl(unsigned seconds) { min_ttl_ = seconds; }
208  unsigned min_ttl() const { return min_ttl_; }
209  void set_max_ttl(unsigned seconds) { max_ttl_ = seconds; }
210  unsigned max_ttl() const { return max_ttl_; }
211 
212  protected:
221  virtual void DoResolve(const std::vector<std::string> &names,
222  const std::vector<bool> &skip,
223  std::vector<std::vector<std::string> > *ipv4_addresses,
224  std::vector<std::vector<std::string> > *ipv6_addresses,
225  std::vector<Failures> *failures,
226  std::vector<unsigned> *ttls,
227  std::vector<std::string> *fqdns) = 0;
228  bool IsIpv4Address(const std::string &address);
229  bool IsIpv6Address(const std::string &address);
230 
234  std::vector<std::string> domains_;
235 
240 
244  std::vector<std::string> resolvers_;
245 
250  unsigned retries_;
251 
255  unsigned timeout_ms_;
256 
262  unsigned throttle_;
263 
267  unsigned min_ttl_;
268 
272  unsigned max_ttl_;
273 
278 };
279 
280 
284 class CaresResolver : public Resolver {
285  friend class NormalResolver;
286  public:
293  static const unsigned kMaxAddresses = 16;
294 
295  static CaresResolver *Create(const bool ipv4_only,
296  const unsigned retries,
297  const unsigned timeout_ms);
298  virtual ~CaresResolver();
299 
300  virtual bool SetResolvers(const std::vector<std::string> &resolvers);
301  virtual bool SetSearchDomains(const std::vector<std::string> &domains);
302  virtual void SetSystemResolvers();
303  virtual void SetSystemSearchDomains();
304 
305  protected:
306  CaresResolver(const bool ipv4_only,
307  const unsigned retries,
308  const unsigned timeout_ms);
309  virtual void DoResolve(const std::vector<std::string> &names,
310  const std::vector<bool> &skip,
311  std::vector<std::vector<std::string> > *ipv4_addresses,
312  std::vector<std::vector<std::string> > *ipv6_addresses,
313  std::vector<Failures> *failures,
314  std::vector<unsigned> *ttls,
315  std::vector<std::string> *fqdns);
316 
317  private:
318  void WaitOnCares();
319  ares_channel *channel_;
321  std::vector<std::string> system_resolvers_;
322  std::vector<std::string> system_domains_;
323 };
324 
325 
333 class HostfileResolver : public Resolver {
334  friend class NormalResolver;
335  public:
336  static HostfileResolver *Create(const std::string &path, bool ipv4_only);
337  virtual ~HostfileResolver();
338 
339  virtual bool SetResolvers(const std::vector<std::string> & /* resolvers */) {
340  return true;
341  }
342  virtual bool SetSearchDomains(const std::vector<std::string> &domains);
343  virtual void SetSystemResolvers() { }
344  virtual void SetSystemSearchDomains();
345 
346  protected:
347  explicit HostfileResolver(const bool ipv4_only);
348  virtual void DoResolve(const std::vector<std::string> &names,
349  const std::vector<bool> &skip,
350  std::vector<std::vector<std::string> > *ipv4_addresses,
351  std::vector<std::vector<std::string> > *ipv6_addresses,
352  std::vector<Failures> *failures,
353  std::vector<unsigned> *ttls,
354  std::vector<std::string> *fqdns);
355 
356  private:
357  struct HostEntry {
358  std::vector<std::string> ipv4_addresses;
359  std::vector<std::string> ipv6_addresses;
360  };
361  static const int kIpMaxLength = 45;
362  static const int kHostnameMaxLength = 253;
363  void ParseHostFile();
368  std::map<std::string, HostEntry> host_map_;
369 
375  FILE *fhosts_;
376 };
377 
378 
383 class NormalResolver : public Resolver {
384  FRIEND_TEST(T_Dns, NormalResolverConstruct);
385 
386  public:
387  static NormalResolver *Create(const bool ipv4_only,
388  const unsigned retries,
389  const unsigned timeout_ms);
390  virtual bool SetResolvers(const std::vector<std::string> &resolvers);
391  virtual bool SetSearchDomains(const std::vector<std::string> &domains);
392  virtual void SetSystemResolvers();
393  virtual void SetSystemSearchDomains();
394  virtual ~NormalResolver();
395 
396  protected:
397  virtual void DoResolve(const std::vector<std::string> &names,
398  const std::vector<bool> &skip,
399  std::vector<std::vector<std::string> > *ipv4_addresses,
400  std::vector<std::vector<std::string> > *ipv6_addresses,
401  std::vector<Failures> *failures,
402  std::vector<unsigned> *ttls,
403  std::vector<std::string> *fqdns);
404  NormalResolver();
405 
406  private:
409 };
410 
411 } // namespace dns
412 
413 #endif // CVMFS_NETWORK_DNS_H_
Failures
Definition: dns.h:28
static const int kHostnameMaxLength
Definition: dns.h:362
Definition: prng.h:28
int64_t id_
Definition: dns.h:140
virtual bool SetSearchDomains(const std::vector< std::string > &domains)
Definition: dns.cc:933
unsigned throttle() const
Definition: dns.h:206
const char * Code2Ascii(const Failures error)
Definition: dns.h:53
int64_t id() const
Definition: dns.h:108
int64_t atomic_int64
Definition: atomic.h:18
FRIEND_TEST(T_Dns, HostEquivalent)
virtual void SetSystemResolvers()
Definition: dns.h:343
void ResolveMany(const std::vector< std::string > &names, std::vector< Host > *hosts)
Definition: dns.cc:370
Resolver returned a negative reply.
Definition: dns.h:33
bool ipv4_only_
Definition: dns.h:239
HostfileResolver * hostfile_resolver_
Definition: dns.h:408
static NormalResolver * Create(const bool ipv4_only, const unsigned retries, const unsigned timeout_ms)
Definition: dns.cc:1269
Definition: dns.h:357
void CopyFrom(const Host &other)
Definition: dns.cc:217
virtual ~Resolver()
Definition: dns.h:187
void set_min_ttl(unsigned seconds)
Definition: dns.h:207
CaresResolver * cares_resolver_
Definition: dns.h:407
unsigned retries_
Definition: dns.h:250
bool IsIpv4Address(const std::string &address)
Definition: dns.cc:308
const std::set< std::string > & ipv6_addresses() const
Definition: dns.h:114
virtual bool SetSearchDomains(const std::vector< std::string > &domains)
Definition: dns.cc:1250
std::vector< std::string > ipv4_addresses
Definition: dns.h:358
virtual bool SetResolvers(const std::vector< std::string > &resolvers)=0
unsigned min_ttl() const
Definition: dns.h:208
FILE * fhosts_
Definition: dns.h:375
Resolver returned a positive reply but without IPs.
Definition: dns.h:35
bool IsIpv6Address(const std::string &address)
Definition: dns.cc:332
std::set< std::string > ipv6_addresses_
Definition: dns.h:152
static const unsigned kDefaultMaxTtl
Definition: dns.h:182
bool ipv4_only() const
Definition: dns.h:201
const std::string & name() const
Definition: dns.h:118
virtual void SetSystemResolvers()
Definition: dns.cc:1323
CaresResolver(const bool ipv4_only, const unsigned retries, const unsigned timeout_ms)
Definition: dns.cc:720
Resolver(const bool ipv4_only, const unsigned retries, const unsigned timeout_ms)
Definition: dns.cc:339
virtual void SetSystemResolvers()=0
static const int kIpMaxLength
Definition: dns.h:361
const std::set< std::string > & ipv4_addresses() const
Definition: dns.h:111
Failures status_
Definition: dns.h:162
Host()
Definition: dns.cc:243
static HostfileResolver * Create(const std::string &path, bool ipv4_only)
Definition: dns.cc:1051
string StripIp(const string &decorated_ip)
Definition: dns.cc:172
bool IsValid() const
Definition: dns.cc:292
void set_max_ttl(unsigned seconds)
Definition: dns.h:209
virtual ~NormalResolver()
Definition: dns.cc:1370
const std::set< std::string > & ViewBestAddresses(IpPreference preference) const
Definition: dns.cc:205
Host & operator=(const Host &other)
Definition: dns.cc:256
unsigned throttle_
Definition: dns.h:262
time_t deadline() const
Definition: dns.h:107
std::vector< std::string > system_resolvers_
Definition: dns.h:321
virtual void SetSystemSearchDomains()
Definition: dns.cc:1328
unsigned max_ttl() const
Definition: dns.h:210
Prng prng_
Definition: dns.h:277
bool HasIpv6() const
Definition: dns.h:110
std::string name_
Definition: dns.h:157
virtual void SetSystemResolvers()
Definition: dns.cc:975
std::vector< std::string > domains_
Definition: dns.h:234
const std::vector< std::string > & resolvers() const
Definition: dns.h:202
std::string AddDefaultScheme(const std::string &proxy)
Definition: dns.cc:187
std::map< std::string, HostEntry > host_map_
Definition: dns.h:368
unsigned min_ttl_
Definition: dns.h:267
HostfileResolver(const bool ipv4_only)
Definition: dns.cc:1147
Definition: dns.h:90
virtual bool SetSearchDomains(const std::vector< std::string > &domains)
Definition: dns.cc:1308
virtual void DoResolve(const std::vector< std::string > &names, const std::vector< bool > &skip, std::vector< std::vector< std::string > > *ipv4_addresses, std::vector< std::vector< std::string > > *ipv6_addresses, std::vector< Failures > *failures, std::vector< unsigned > *ttls, std::vector< std::string > *fqdns)
Definition: dns.cc:1094
bool IsEquivalent(const Host &other) const
Definition: dns.cc:269
virtual bool SetResolvers(const std::vector< std::string > &resolvers)
Definition: dns.cc:917
bool IsExpired() const
Definition: dns.cc:280
void set_throttle(const unsigned throttle)
Definition: dns.h:205
ares_channel * channel_
Definition: dns.h:319
virtual bool SetSearchDomains(const std::vector< std::string > &domains)=0
std::string ExtractPort(const std::string &url)
Definition: dns.cc:125
virtual void DoResolve(const std::vector< std::string > &names, const std::vector< bool > &skip, std::vector< std::vector< std::string > > *ipv4_addresses, std::vector< std::vector< std::string > > *ipv6_addresses, std::vector< Failures > *failures, std::vector< unsigned > *ttls, std::vector< std::string > *fqdns)
Definition: dns.cc:840
std::string ExtractHost(const std::string &url)
Definition: dns.cc:110
time_t deadline_
Definition: dns.h:135
FRIEND_TEST(T_Dns, NormalResolverConstruct)
virtual ~HostfileResolver()
Definition: dns.cc:1153
char * lookup_options_
Definition: dns.h:320
IpPreference
Definition: dns.h:46
unsigned retries() const
Definition: dns.h:203
void ParseHostFile()
Definition: dns.cc:1162
bool HasIpv4() const
Definition: dns.h:109
void WaitOnCares()
Definition: dns.cc:991
Host Resolve(const std::string &name)
Definition: dns.cc:357
std::vector< std::string > resolvers_
Definition: dns.h:244
static CaresResolver * Create(const bool ipv4_only, const unsigned retries, const unsigned timeout_ms)
Definition: dns.cc:743
virtual void DoResolve(const std::vector< std::string > &names, const std::vector< bool > &skip, std::vector< std::vector< std::string > > *ipv4_addresses, std::vector< std::vector< std::string > > *ipv6_addresses, std::vector< Failures > *failures, std::vector< unsigned > *ttls, std::vector< std::string > *fqdns)=0
static const unsigned kDefaultMinTtl
Definition: dns.h:177
virtual void SetSystemSearchDomains()=0
static Host ExtendDeadline(const Host &original, unsigned seconds_from_now)
Definition: dns.cc:231
unsigned timeout_ms_
Definition: dns.h:255
std::vector< std::string > system_domains_
Definition: dns.h:322
virtual void DoResolve(const std::vector< std::string > &names, const std::vector< bool > &skip, std::vector< std::vector< std::string > > *ipv4_addresses, std::vector< std::vector< std::string > > *ipv6_addresses, std::vector< Failures > *failures, std::vector< unsigned > *ttls, std::vector< std::string > *fqdns)
Definition: dns.cc:1340
virtual ~CaresResolver()
Definition: dns.cc:731
virtual void SetSystemSearchDomains()
Definition: dns.cc:981
unsigned max_ttl_
Definition: dns.h:272
std::set< std::string > ipv4_addresses_
Definition: dns.h:146
virtual void SetSystemSearchDomains()
Definition: dns.cc:1256
virtual bool SetResolvers(const std::vector< std::string > &)
Definition: dns.h:339
const std::vector< std::string > & domains() const
Definition: dns.h:200
static const unsigned kMaxAddresses
Definition: dns.h:293
Failures status() const
Definition: dns.h:119
std::vector< std::string > ipv6_addresses
Definition: dns.h:359
static atomic_int64 global_id_
Definition: dns.h:129
virtual bool SetResolvers(const std::vector< std::string > &resolvers)
Definition: dns.cc:1300
string RewriteUrl(const string &url, const string &ip)
Definition: dns.cc:156
unsigned timeout_ms() const
Definition: dns.h:204