-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnat.cpp
executable file
·103 lines (80 loc) · 2.37 KB
/
nat.cpp
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "nat.hpp"
#include "core/utils.hpp"
#include "core/interface.hpp"
#include "simple-router.hpp"
#include <algorithm>
#include <iostream>
namespace simple_router {
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// IMPLEMENT THESE METHODS
void
NatTable::checkNatTable()
{
}
std::shared_ptr<NatEntry>
NatTable::lookup(uint16_t id)
{
return nullptr;
}
void
NatTable::insertNatEntry(uint16_t id, uint32_t in_ip, uint32_t ex_ip)
{
}
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// You should not need to touch the rest of this code.
NatTable::NatTable(SimpleRouter& router)
: m_router(router)
, m_shouldStop(false)
, m_tickerThread(std::bind(&NatTable::ticker, this))
{
}
NatTable::~NatTable()
{
m_shouldStop = true;
m_tickerThread.join();
}
void
NatTable::clear()
{
std::lock_guard<std::mutex> lock(m_mutex);
m_natTable.clear();
}
void
NatTable::ticker()
{
while (!m_shouldStop) {
std::this_thread::sleep_for(std::chrono::seconds(1));
{
std::lock_guard<std::mutex> lock(m_mutex);
auto now = steady_clock::now();
std::map<uint16_t, std::shared_ptr<NatEntry>>::iterator entryIt;
for (entryIt = m_natTable.begin(); entryIt != m_natTable.end(); entryIt++ ) {
if (entryIt->second->isValid && (now - entryIt->second->timeUsed > SR_ARPCACHE_TO)) {
entryIt->second->isValid = false;
}
}
checkNatTable();
}
}
}
std::ostream&
operator<<(std::ostream& os, const NatTable& table)
{
std::lock_guard<std::mutex> lock(table.m_mutex);
os << "\nID Internal IP External IP AGE VALID\n"
<< "-----------------------------------------------------------------------------------\n";
auto now = steady_clock::now();
for (auto const& entryIt : table.m_natTable) {
os << entryIt.first << " "
<< ipToString(entryIt.second->internal_ip) << " "
<< ipToString(entryIt.second->external_ip) << " "
<< std::chrono::duration_cast<seconds>((now - entryIt.second->timeUsed)).count() << " seconds "
<< entryIt.second->isValid
<< "\n";
}
os << std::endl;
return os;
}
} // namespace simple_router