1#ifndef NEFORCE_NETWORK_DNS_DNS_CLIENT_HPP__
2#define NEFORCE_NETWORK_DNS_DNS_CLIENT_HPP__
17#ifndef NEFORCE_PLATFORM_WINDOWS
25NEFORCE_BEGIN_NAMESPACE__
59 struct pending_entry {
66 unordered_map<string, pair<dns_query_result, steady_clock::time_point>> cache_;
67 mutable shared_mutex cache_mutex_;
69 bool use_tcp_ =
false;
70 bool recursion_desired_ =
true;
72 bool dnssec_ok_ =
false;
74 udp_socket shared_socket_;
75 unordered_map<uint16_t, pending_entry> pending_queries_;
76 mutable mutex pending_mutex_;
77 mutable mutex send_mutex_;
79#ifndef NEFORCE_PLATFORM_WINDOWS
82 atomic<bool> io_running_{
false};
85 void ensure_io_started();
88 void io_receive_loop();
93 optional<dns_query_result> check_cache(
const string& key);
94 void update_cache(
const string& key,
const dns_query_result& result);
136 void set_use_tcp(
const bool use_tcp)
noexcept { use_tcp_ = use_tcp; }
285NEFORCE_END_NAMESPACE__
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)
批量查询
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记录(邮件交换器)
vector< byte_t > byte_vector
字节向量类型别名
unsigned short uint16_t
16位无符号整数类型
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 建议值)
milliseconds timeout
查询超时时间
static time_point now() noexcept
获取当前时间点
_NEFORCE time_point< steady_clock > time_point
时间点类型