NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
dns_client.hpp
浏览该文件的文档.
1#ifndef NEFORCE_NETWORK_DNS_DNS_CLIENT_HPP__
2#define NEFORCE_NETWORK_DNS_DNS_CLIENT_HPP__
3
11
17#ifndef NEFORCE_PLATFORM_WINDOWS
19#endif
25NEFORCE_BEGIN_NAMESPACE__
26
31
46class NEFORCE_API dns_client {
47public:
52 struct config {
53 string server{"8.8.8.8"};
56 };
57
58private:
59 struct pending_entry {
61 byte_vector query_data;
63 };
64
65 config config_{};
66 unordered_map<string, pair<dns_query_result, steady_clock::time_point>> cache_;
67 mutable shared_mutex cache_mutex_;
68 seconds cache_ttl_{300};
69 bool use_tcp_ = false;
70 bool recursion_desired_ = true;
71 uint16_t edns_udp_payload_{edns::DEFAULT_UDP_PAYLOAD};
72 bool dnssec_ok_ = false;
73
74 udp_socket shared_socket_;
75 unordered_map<uint16_t, pending_entry> pending_queries_;
76 mutable mutex pending_mutex_;
77 mutable mutex send_mutex_;
78 thread_pool io_pool_;
79#ifndef NEFORCE_PLATFORM_WINDOWS
80 pipe wake_pipe_;
81#endif
82 atomic<bool> io_running_{false};
83
84private:
85 void ensure_io_started();
86 void start_io();
87 void stop_io();
88 void io_receive_loop();
89
90 void send_query(const byte_vector& query);
91 byte_vector send_tcp_query(const byte_vector& query);
92
93 optional<dns_query_result> check_cache(const string& key);
94 void update_cache(const string& key, const dns_query_result& result);
95
96public:
104
111 explicit dns_client(config cfg, bool use_tcp = false);
112
113 ~dns_client();
114
115 dns_client(const dns_client&) = delete;
116 dns_client& operator=(const dns_client&) = delete;
117 dns_client(dns_client&&) = delete;
118 dns_client& operator=(dns_client&&) = delete;
119
124 void set_config(config cfg) noexcept { config_ = move(cfg); }
125
130 void set_timeout(const milliseconds timeout) noexcept { config_.timeout = timeout; }
131
136 void set_use_tcp(const bool use_tcp) noexcept { use_tcp_ = use_tcp; }
137
142 void set_cache_ttl(const seconds ttl) noexcept { cache_ttl_ = ttl; }
143
148 void set_recursion_desired(const bool rd) { recursion_desired_ = rd; }
149
154 void set_edns_udp_payload(const uint16_t payload_size) noexcept { edns_udp_payload_ = payload_size; }
155
160 void set_dnssec_ok(const bool ok) noexcept { dnssec_ok_ = ok; }
161
165 void clear_cache() noexcept { cache_.clear(); }
166
177
187
194
201
208
215
222
229
236
247
257
270 dns_class qclass = dns_class::INTERNET, bool rd = true, bool edns_enable = true,
271 bool dnssec_ok = false, uint16_t edns_payload = edns::DEFAULT_UDP_PAYLOAD);
272
280 static dns_query_result parse_response(const byte_vector& response, uint16_t expected_id = 0);
281};
282 // DNS
284
285NEFORCE_END_NAMESPACE__
286#endif // NEFORCE_NETWORK_DNS_DNS_CLIENT_HPP__
static dns_query_result parse_response(const byte_vector &response, uint16_t expected_id=0)
解析DNS响应消息
vector< string > resolve_aaaa(string_view domain)
解析AAAA记录(IPv6地址)
future< dns_query_result > query_async(const string &domain, dns_record::raw type=dns_record::A, dns_class qclass=dns_class::INTERNET)
异步DNS查询
void set_recursion_desired(const bool rd)
设置是否期望递归查询(RD)
vector< dns_srv_record > resolve_srv(string_view domain)
解析SRV记录(服务定位器)
dns_client(config cfg, bool use_tcp=false)
构造函数
string reverse_query(string_view ip)
反向查询(从IP获取域名)
void set_edns_udp_payload(const uint16_t payload_size) noexcept
设置EDNS0 UDP载荷大小
void set_config(config cfg) noexcept
设置客户端配置
void set_timeout(const milliseconds timeout) noexcept
设置超时时间
optional< dns_soa_record > resolve_soa(string_view domain)
解析SOA记录(授权区域起始)
void clear_cache() noexcept
清空缓存
static byte_vector build_query(string_view domain, dns_record::raw type=dns_record::A, dns_class qclass=dns_class::INTERNET, bool rd=true, bool edns_enable=true, bool dnssec_ok=false, uint16_t edns_payload=edns::DEFAULT_UDP_PAYLOAD)
构建DNS查询消息
vector< string > resolve_txt(string_view domain)
解析TXT记录(文本记录)
void set_use_tcp(const bool use_tcp) noexcept
设置是否使用TCP
vector< dns_query_result > batch_query(const vector< string > &domains, dns_record::raw type=dns_record::A)
批量查询
dns_client()
默认构造函数
vector< string > resolve_a(string_view domain)
解析A记录(IPv4地址)
void set_dnssec_ok(const bool ok) noexcept
设置是否请求DNSSEC(DO)
vector< string > resolve_cname(string_view domain)
解析CNAME记录(别名)
dns_query_result query(string_view domain, dns_record::raw type=dns_record::A, dns_class qclass=dns_class::INTERNET)
执行DNS查询
void set_cache_ttl(const seconds ttl) noexcept
设置缓存TTL
vector< string > resolve_mx(string_view domain)
解析MX记录(邮件交换器)
独占future类模板
可选值类
Promise类模板
动态大小数组容器
时钟类型
DNS协议消息定义
异步结果消费者
vector< byte_t > byte_vector
字节向量类型别名
unsigned short uint16_t
16位无符号整数类型
dns_class
DNS查询类枚举
@ INTERNET
Internet (IN)
duration< int64_t, milli > milliseconds
毫秒持续时间
duration< int64_t > seconds
秒持续时间
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result) noexcept(noexcept(inner::__move_aux(first, last, result)))
移动范围元素
basic_string_view< char > string_view
字符字符串视图
constexpr uint16_t DEFAULT_UDP_PAYLOAD
默认UDP载荷大小(RFC 6891 建议值)
可选值类型
跨平台管道封装
网络端口定义和转换工具
异步结果生产者
共享互斥锁支持
DNS客户端配置
milliseconds timeout
查询超时时间
string server
DNS服务器地址
ports port
DNS服务器端口
DNS查询结果结构
raw
DNS记录类型枚举
@ A
IPv4地址记录
网络端口封装类
@ DNS
DNS协议端口
static time_point now() noexcept
获取当前时间点
_NEFORCE time_point< steady_clock > time_point
时间点类型
线程池实现
UDP Socket实现
无序映射容器