00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef __libutilxx__sella__net__DNS_H__
00010 #define __libutilxx__sella__net__DNS_H__
00011
00012 #include "../../common.h"
00013 #include "DNSException.h"
00014 #include "DNSCallback.h"
00015 #include "IPAddress.h"
00016 #include "../util/Task.h"
00017
00018 #include <sys/socket.h>
00019 #include <stdint.h>
00020 #include <udns.h>
00021
00022 #include <string>
00023 #include <memory>
00024 #include <mutex>
00025
00026 namespace sella {
00027 namespace net {
00028 class DNS;
00029 }
00030
00031 namespace util {
00032 class ThreadPool;
00033 }
00034 }
00035
00036 class sella::net::DNS {
00037 public:
00038 typedef std::shared_ptr<DNS> shared;
00039 typedef DNS *ptr;
00040
00041 public:
00042 static const uint64_t DefaultInterval = 5000;
00043
00044 static const int AnyProtocol = 0x00;
00045 static const int IPv4Preferred = 0x01;
00046 static const int IPv4Required = 0x03;
00047
00048 static const int IPv6Preferred = 0x04;
00049 static const int IPv6Required = 0x12;
00050
00051 static const std::vector<std::string> empty;
00052
00053 public:
00054 DNS(const std::vector<std::string> &nameservers = empty, const std::vector<std::string> &searches = empty) throw (DNSException);
00055 DNS(sella::util::ThreadPool *pool, const std::vector<std::string> &nameservers = empty, const std::vector<std::string> &searches = empty) throw (DNSException);
00056 virtual ~DNS() throw ();
00057
00058 public:
00059
00060 static std::vector<IPAddress> resolve(const std::string &name, int protocol = IPv4Preferred) throw (DNSException);
00061
00062
00063 DNSCallback* resolve(DNSCallback *callback, const std::string &name, int protocol = IPv4Preferred) throw (DNSException);
00064 bool receive(time_t delay = 0, time_t now = 0) throw ();
00065
00066 void setInterval(uint64_t usec) throw ();
00067 void setNoSearch(bool on = true) throw ();
00068 void setNoRecursion(bool on = true) throw ();
00069 void setAuthoritativeOnly(bool on = true) throw ();
00070 void setTimeout(int seconds = 5) throw ();
00071 void setRetries(int retries = 2) throw ();
00072 void setDots(int ndots = 1) throw ();
00073 void setUDPSize(int bytes = 4096) throw ();
00074 void setUDPPort(short port = 53) throw ();
00075
00076 int getActive() throw ();
00077 int getSocket() throw ();
00078
00079 static const DNS::shared shared_ptr() throw ();
00080
00081 protected:
00082 static void callback_a(struct dns_ctx *context, void *result, void *argument);
00083 static void callback_aaaa(struct dns_ctx *context, void *result, void *argument);
00084
00085 private:
00086 sella::util::ThreadPool *pool;
00087 sella::util::Task::shared task;
00088 std::mutex mutex;
00089 struct dns_ctx *context;
00090 int flags;
00091 int interval;
00092
00093 void init(const std::vector<std::string> &nameservers, const std::vector<std::string> &searches);
00094 };
00095
00096 #endif
00097
00098
00099
00100