-
Notifications
You must be signed in to change notification settings - Fork 7
/
arp-cache.cpp
171 lines (139 loc) · 4.45 KB
/
arp-cache.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (c) 2017 Alexander Afanasyev
*
* This program is free software: you can redistribute it and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation, either version
* 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "arp-cache.hpp"
#include "core/utils.hpp"
#include "core/interface.hpp"
#include "simple-router.hpp"
#include <algorithm>
#include <iostream>
namespace simple_router {
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// IMPLEMENT THIS METHOD
void
ArpCache::periodicCheckArpRequestsAndCacheEntries()
{
// FILL THIS IN
}
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// You should not need to touch the rest of this code.
ArpCache::ArpCache(SimpleRouter& router)
: m_router(router)
, m_shouldStop(false)
, m_tickerThread(std::bind(&ArpCache::ticker, this))
{
}
ArpCache::~ArpCache()
{
m_shouldStop = true;
m_tickerThread.join();
}
std::shared_ptr<ArpEntry>
ArpCache::lookup(uint32_t ip)
{
std::lock_guard<std::mutex> lock(m_mutex);
for (const auto& entry : m_cacheEntries) {
if (entry->isValid && entry->ip == ip) {
return entry;
}
}
return nullptr;
}
std::shared_ptr<ArpRequest>
ArpCache::queueRequest(uint32_t ip, const Buffer& packet, const std::string& iface)
{
std::lock_guard<std::mutex> lock(m_mutex);
auto request = std::find_if(m_arpRequests.begin(), m_arpRequests.end(),
[ip] (const std::shared_ptr<ArpRequest>& request) {
return (request->ip == ip);
});
if (request == m_arpRequests.end()) {
request = m_arpRequests.insert(m_arpRequests.end(), std::make_shared<ArpRequest>(ip));
}
// Add the packet to the list of packets for this request
(*request)->packets.push_back({packet, iface});
return *request;
}
void
ArpCache::removeRequest(const std::shared_ptr<ArpRequest>& entry)
{
std::lock_guard<std::mutex> lock(m_mutex);
m_arpRequests.remove(entry);
}
std::shared_ptr<ArpRequest>
ArpCache::insertArpEntry(const Buffer& mac, uint32_t ip)
{
std::lock_guard<std::mutex> lock(m_mutex);
auto entry = std::make_shared<ArpEntry>();
entry->mac = mac;
entry->ip = ip;
entry->timeAdded = steady_clock::now();
entry->isValid = true;
m_cacheEntries.push_back(entry);
auto request = std::find_if(m_arpRequests.begin(), m_arpRequests.end(),
[ip] (const std::shared_ptr<ArpRequest>& request) {
return (request->ip == ip);
});
if (request != m_arpRequests.end()) {
return *request;
}
else {
return nullptr;
}
}
void
ArpCache::clear()
{
std::lock_guard<std::mutex> lock(m_mutex);
m_cacheEntries.clear();
m_arpRequests.clear();
}
void
ArpCache::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();
for (auto& entry : m_cacheEntries) {
if (entry->isValid && (now - entry->timeAdded > SR_ARPCACHE_TO)) {
entry->isValid = false;
}
}
periodicCheckArpRequestsAndCacheEntries();
}
}
}
std::ostream&
operator<<(std::ostream& os, const ArpCache& cache)
{
std::lock_guard<std::mutex> lock(cache.m_mutex);
os << "\nMAC IP AGE VALID\n"
<< "-----------------------------------------------------------\n";
auto now = steady_clock::now();
for (const auto& entry : cache.m_cacheEntries) {
os << macToString(entry->mac) << " "
<< ipToString(entry->ip) << " "
<< std::chrono::duration_cast<seconds>((now - entry->timeAdded)).count() << " seconds "
<< entry->isValid
<< "\n";
}
os << std::endl;
return os;
}
} // namespace simple_router