-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDNSCache.cpp
More file actions
89 lines (76 loc) · 2.73 KB
/
DNSCache.cpp
File metadata and controls
89 lines (76 loc) · 2.73 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "DNSCache.h"
#include <cassert>
DNSCache::DNSCache(size_t max_size):
max_size{max_size} {
if(max_size <= 0) throw std::runtime_error("DNSCache should have positive size");
name_ip_mapping.reserve(max_size);
}
void DNSCache::update_it(Name_ip_map::iterator it, const std::string& name, const std::string& ip) {
if(it != name_ip_mapping.end()) {
it->second.ip = ip;
update_links(it);
return;
}
if(!is_filled()) {
add_new_ip(name, ip);
return;
}
update_last_elem(name, ip);
}
bool DNSCache::is_filled() const {
return name_ip_mapping.size() >= max_size;
}
std::string DNSCache::resolve_it(DNSCache::Name_ip_map::iterator it) {
update_links(it);
return it->second.ip;
}
DNSCache::Name_ip_map::iterator DNSCache::get_it_by_name(const std::string& name) {
return name_ip_mapping.find(name);
}
void DNSCache::update_links(Name_ip_map::iterator it) {
auto p_prev_node = it->second.prev;
auto p_next_node = it->second.next;
if(p_prev_node) p_prev_node->next = p_next_node;
if(p_next_node) p_next_node->prev = p_prev_node;
if(!p_next_node && p_prev_node) tail = p_prev_node;
it->second.next = head;
head->prev = &it->second;
head = &it->second;
it->second.prev = nullptr;
assert(head && tail);
assert(name_ip_mapping.size() <= max_size);
}
void DNSCache::add_new_ip(const std::string& name, const std::string& ip) {
bool cache_empty = name_ip_mapping.empty();
auto[it, emplaced] = name_ip_mapping.emplace(std::piecewise_construct,
std::forward_as_tuple(name),
std::forward_as_tuple(ip));
assert(emplaced);
if(cache_empty) {
head = &it->second;
tail = &it->second;
return;
}
it->second.next = head;
head->prev = &it->second;
head = &it->second;
}
void DNSCache::update_last_elem(const std::string& name, const std::string& ip) {
auto* p_value = get_name_ip_value_by_elem(tail);
assert(name_ip_mapping.find(p_value->first) != name_ip_mapping.end());
auto node = name_ip_mapping.extract(p_value->first);
node.key() = name;
node.mapped().ip = ip;
auto irt = name_ip_mapping.insert(std::move(node));
assert(irt.inserted);
update_links(irt.position);
}
DNSCache::Name_ip_map::value_type* DNSCache::get_name_ip_value_by_elem(DNSCache::Elem* elem_p) const {
return (typename DNSCache::Name_ip_map::value_type*)
((char*) elem_p
+ offsetof(DNSCache::Name_ip_map::value_type, first)
- offsetof(DNSCache::Name_ip_map::value_type, second));
}
bool DNSCache::end(Name_ip_map::iterator it) {
return it == name_ip_mapping.end();
}