NexusForce 1.0.0
A rigorously engineered full-stack C++ backend library.
载入中...
搜索中...
未找到
dns_message.hpp
浏览该文件的文档.
1#ifndef NEFORCE_NETWORK_DNS_DNS_MESSAGE_HPP__
2#define NEFORCE_NETWORK_DNS_DNS_MESSAGE_HPP__
3
19
21#include "NeForce/core/exception/system_exception.hpp"
24NEFORCE_BEGIN_NAMESPACE__
25
169
176enum class dns_opcode : uint8_t {
177 QUERY = 0,
178 IQUERY = 1,
179 STATUS = 2,
180 NOTIFY = 4,
182};
183
191enum class dns_class : uint16_t {
193 CHAOS = 3,
194 HESIOD = 4,
195 ANY = 255
196};
197
212 // DNS
214
219
226class dns_exception final : public network_exception {
227public:
232 enum class code {
233 TIMEOUT,
234 NETWORK_ERROR,
235 PARSE_ERROR,
237 TRUNCATED,
238 NO_RECORD
239 };
240
241 explicit dns_exception(const string& info) :
242 network_exception(info.data()) {}
243
244 dns_exception(const string& info, const code code) :
245 network_exception(info.data(), error_code{static_cast<int>(code), generic_category()}) {}
246
251 static dns_exception timeout() { return {"DNS query timeout", code::TIMEOUT}; }
252
259 static dns_exception network_error(const string& detail, const code code = code::NETWORK_ERROR) {
260 return {"Network error: " + detail, code};
261 }
262
268 static dns_exception parse_error(const string& detail) { return {"Parse error: " + detail, code::PARSE_ERROR}; }
269
270 NEFORCE_NODISCARD const char* type() const noexcept override { return "dns_exception"; }
271};
272 // Exceptions
274
279
293 enum raw : uint16_t {
294 A = 1,
295 NS = 2,
296 CNAME = 5,
297 SOA = 6,
298 PTR = 12,
299 MX = 15,
300 TXT = 16,
301 AAAA = 28,
302 SRV = 33
303 };
304
305 string name;
306 string data;
310
314 dns_record() = default;
315
324 dns_record(string n, const raw t, const dns_class c, const uint32_t ttl_val, string d) noexcept :
325 name(_NEFORCE move(n)),
326 data(_NEFORCE move(d)),
327 ttl(ttl_val),
328 type(t),
329 class_type(c) {}
330};
331
366
381
389namespace edns {
390 NEFORCE_INLINE17 constexpr uint16_t OPT_TYPE = 41;
391 NEFORCE_INLINE17 constexpr uint16_t DEFAULT_UDP_PAYLOAD = 1232;
392 NEFORCE_INLINE17 constexpr uint16_t MAX_UDP_PAYLOAD = 4096;
393 NEFORCE_INLINE17 constexpr uint8_t VERSION = 0;
394 NEFORCE_INLINE17 constexpr uint8_t EXT_RCODE_SHIFT = 24;
395 NEFORCE_INLINE17 constexpr uint8_t VERSION_SHIFT = 16;
396 NEFORCE_INLINE17 constexpr uint16_t DO_BIT = 0x8000;
397 NEFORCE_INLINE17 constexpr uint8_t MAX_UDP_RETRIES = 2;
398 NEFORCE_INLINE17 constexpr seconds NEGATIVE_CACHE_TTL{30};
399} // namespace edns
400
407struct dns_srv_record {
411 string target;
412
413 dns_srv_record() = default;
414 dns_srv_record(const uint16_t prio, const uint16_t w, const uint16_t p, string t) :
415 priority(prio),
416 weight(w),
417 port(p),
418 target(_NEFORCE move(t)) {}
419};
420
427struct dns_soa_record {
428 string mname;
429 string rname;
435
436 dns_soa_record() = default;
437};
438 // DNS
440
441NEFORCE_END_NAMESPACE__
442#endif // NEFORCE_NETWORK_DNS_DNS_MESSAGE_HPP__
static dns_exception network_error(const string &detail, const code code=code::NETWORK_ERROR)
创建网络错误异常
const char * type() const noexcept override
获取异常类型
static dns_exception parse_error(const string &detail)
创建解析错误异常
static dns_exception timeout()
创建超时异常
动态大小数组容器
constexpr uint16_t DO_BIT
DNSSEC OK标志位
constexpr uint16_t DEFAULT_UDP_PAYLOAD
默认UDP载荷大小(RFC 6891 建议值)
constexpr uint8_t VERSION
EDNS版本号
constexpr uint8_t VERSION_SHIFT
EDNS版本在TTL字段中的偏移
constexpr uint16_t OPT_TYPE
OPT伪资源记录类型
constexpr uint8_t MAX_UDP_RETRIES
UDP最大重试次数
constexpr uint16_t MAX_UDP_PAYLOAD
最大UDP载荷大小
constexpr uint8_t EXT_RCODE_SHIFT
扩展RCODE在TTL字段中的偏移
constexpr seconds NEGATIVE_CACHE_TTL
否定缓存TTL(RFC 2308)
持续时间类型
unsigned int uint32_t
32位无符号整数类型
unsigned char uint8_t
8位无符号整数类型
unsigned short uint16_t
16位无符号整数类型
dns_response
DNS响应码枚举
dns_class
DNS查询类枚举
dns_opcode
DNS操作码枚举
@ REFUSED
拒绝,服务器拒绝执行查询
@ NOT_IMPLEMENTED
未实现,服务器不支持该查询类型
@ FORMAT_ERROR
格式错误,服务器无法解析查询
@ NON_ERROR
无错误,查询成功
@ SERVER_FAILURE
服务器失败,无法处理查询
@ NAME_ERROR
名称错误,域名不存在
@ STATUS
服务器状态请求
duration< int64_t > seconds
秒持续时间
duration< int64_t, milli > milliseconds
毫秒持续时间
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result) noexcept(noexcept(inner::__move_aux(first, last, result)))
移动范围元素
constexpr decltype(auto) data(Container &cont) noexcept(noexcept(cont.data()))
获取容器的底层数据指针
EDNS0 协议常量(RFC 6891)
字符串类型别名和实用函数
DNS消息头部结构
uint16_t nscount
权威计数
uint16_t qdcount
问题计数
uint16_t flags
标志位
uint16_t arcount
附加计数
uint16_t ancount
答案计数
dns_response response_code
响应码
uint8_t extended_rcode
扩展响应码(EDNS0,RFC 6891)
uint16_t udp_payload_size
服务器支持的UDP载荷大小(EDNS0)
uint16_t full_rcode() const noexcept
获取完整响应码(含EDNS0扩展位)
vector< dns_record > authorities
权威记录
vector< dns_record > additional
附加记录
bool is_success() const noexcept
检查查询是否成功
bool authoritative
权威应答(AA标志位)
bool dnssec_ok
服务器支持DNSSEC(DO标志位)
vector< dns_record > answers
答案记录
bool recursive_available
递归查询是否可用
milliseconds query_time
查询耗时
bool truncated
响应是否被截断
uint8_t edns_version
EDNS版本号
raw
DNS记录类型枚举
@ MX
邮件交换记录
@ SOA
授权开始记录
@ CNAME
规范名称记录
@ NS
名称服务器记录
@ SRV
服务定位记录
uint32_t ttl
生存时间(秒)
string data
记录数据
dns_record()=default
默认构造函数
string name
记录名称
dns_class class_type
查询类
dns_record(string n, const raw t, const dns_class c, const uint32_t ttl_val, string d) noexcept
构造函数
DNS SOA(Start of Authority)资源记录
uint32_t expire
过期时间(秒)
uint32_t retry
重试间隔(秒)
uint32_t refresh
刷新间隔(秒)
string rname
管理员邮箱
string mname
主名称服务器
uint32_t serial
区域序列号
uint32_t minimum
最小TTL(秒)
uint16_t weight
权重(同优先级下负载均衡)
uint16_t priority
优先级(越低越优先)
uint16_t port
服务端口号
string target
目标主机名
网络操作或行为异常
动态大小数组容器