libutil++  1.9.3
 All Classes Functions Variables
DNS.h
1 /*
2 ** libutil++
3 ** $Id: DNS.h 1653 2016-02-28 19:54:59Z sella $
4 ** Copyright (c) 2011-2016 Digital Genesis, LLC. All Rights Reserved.
5 ** Released under the LGPL Version 2.1 License.
6 ** http://www.digitalgenesis.com
7 */
8 
9 #ifndef __libutilxx__sella__net__DNS_H__
10 #define __libutilxx__sella__net__DNS_H__
11 
12 #include "../../common.h"
13 #include "DNSException.h"
14 #include "IPAddress.h"
15 #include "../util/Task.h"
16 #include "../variant/Variant.h"
17 
18 #include <sys/socket.h>
19 #include <stdint.h>
20 
21 #include <string>
22 #include <memory>
23 #include <mutex>
24 
25 /* Forward declarations */
26 struct dns_ctx;
27 struct dns_rr_ptr;
28 
29 namespace sella {
30  namespace net {
31  class DNS;
32  class DNSCallback;
33  }
34 
35  namespace util {
36  class ThreadPool;
37  }
38 }
39 
41  public:
42  typedef std::shared_ptr<DNS> shared;
43  typedef DNS *ptr;
44 
45  public:
46  static const uint64_t DefaultInterval = 5000;
47 
48  static const int AnyProtocol = 0x00;
49  static const int IPv4Preferred = 0x01;
50  static const int IPv4Required = 0x03;
51 
52  static const int IPv6Preferred = 0x04;
53  static const int IPv6Required = 0x12;
54 
55  static const int A = 0x20;
56  static const int PTR = 0x21;
57 #if 0 /* Not implemented yet */
58  static const int MX = 0x22;
59  static const int TXT = 0x23;
60  static const int SRV = 0x24;
61 #endif
62 
63  static const std::vector<std::string> empty;
64 
65  public:
66  DNS(const std::vector<std::string> &nameservers = empty, const std::vector<std::string> &searches = empty) throw (DNSException);
67  DNS(sella::util::ThreadPool *pool, const std::vector<std::string> &nameservers = empty, const std::vector<std::string> &searches = empty) throw (DNSException);
68  virtual ~DNS() throw ();
69 
70  public:
71  /* Sync Functions */
72  std::vector<std::string> resolve(const std::string &name, int record = A, int protocol = AnyProtocol) throw (DNSException);
73  sella::variant::Variant resolve(const std::vector<std::string> &names, int record = A, int protocol = AnyProtocol) throw (DNSException); /* Grouped by name */
74  std::vector<std::string> bulkResolve(const std::vector<std::string> &names, int record = A, int protocol = AnyProtocol) throw (DNSException); /* Non-grouped aggregate */
75 
76  /* Static Sync Functions */
77  static std::vector<std::string> sresolve(const std::string &name, int protocol = AnyProtocol) throw (DNSException); /* Will not use settings. */
78  static std::vector<IPAddress> sresolve_ip(const std::string &name, int protocol = AnyProtocol) throw (DNSException); /* Will not use settings. */
79 
80  /* Async Functions */
81  DNSCallback* resolve(DNSCallback *callback, const std::string &name, int record = A, int protocol = AnyProtocol) throw (DNSException);
82  bool receive(uint64_t usec = 0, time_t now = 0) throw (); /* Process incoming packets. Used with non-thread pool constructor. */
83 
84  void setInterval(uint64_t usec) throw ();
85  void setNoSearch(bool on = true) throw (); /* Not working with udns 0.9.9-5 */
86  void setNoRecursion(bool on = true) throw (); /* Not working with udns 0.0.9-5 */
87  void setAuthoritativeOnly(bool on = true) throw (); /* Not working with udns 0.0.9-5 */
88  void setTimeout(int seconds = 5) throw ();
89  void setRetries(int retries = 2) throw ();
90  void setDots(int ndots = 1) throw ();
91  void setUDPSize(int bytes = 4096) throw ();
92  void setUDPPort(short port = 53) throw ();
93 
94  int getActive() throw ();
95  int getFD() throw ();
96 
97  static const DNS::shared shared_ptr(const std::vector<std::string> &nameservers = empty, const std::vector<std::string> &searches = empty) throw (DNSException);
98  static const DNS::shared shared_ptr(sella::util::ThreadPool *pool, const std::vector<std::string> &nameservers = empty, const std::vector<std::string> &searches = empty) throw (DNSException);
99 
100  static const IPAddress convertArpa(const std::string &qname) throw (DNSException);
101 
102  protected:
103  static void callback_a(struct dns_ctx *context, void *result, void *argument);
104  static void callback_aaaa(struct dns_ctx *context, void *result, void *argument);
105  static void callback_ptr(struct dns_ctx *context, dns_rr_ptr *result, void *argument);
106 
107  private:
109  sella::util::Task::shared task;
110  std::mutex mutex;
111  struct dns_ctx *context;
112  int flags;
113  int interval;
114 
115  void init(const std::vector<std::string> &nameservers, const std::vector<std::string> &searches);
116 };
117 
118 #endif
119 
120 /*
121 ** vim: noet ts=3 sw=3
122 */