-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDNSCache.hpp
More file actions
48 lines (36 loc) · 963 Bytes
/
DNSCache.hpp
File metadata and controls
48 lines (36 loc) · 963 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#pragma once
#include "ws-util.h"
#include <string>
#include <map>
/// DNS 缓存
class DNSCache {
public:
/// 条目失效时间
///
/// 默认为一个小时之内没有任何访问条目则失效。
/// 每次访问都会更新时间戳。
static double EXPIRATION;
/// 一个缓存条目
struct Entry {
/// 构造函数
Entry(const addrinfo *ai_other);
/// 禁用复制构造函数
Entry(const Entry &other) = delete;
/// 析构函数
~Entry();
/// 条目是否有效
bool IsOk() const;
addrinfo ai;
bool home = false;
time_t ts = 0; ///< 加入缓存的时间
};
/// 解析域名
static const Entry *Resolve(const std::string &dname);
/// 将域名对应的 IP 地址加入缓存
static void Add(const std::string &dname, const addrinfo &ai);
/// 删除失效条目
static bool Remove(const std::string &dname);
private:
typedef std::map<std::string, Entry> Cache;
static Cache ms_cache;
};