Repository navigation
Expand file tree
/
Copy patharp-cache.cpp
More file actions
251 lines (212 loc) · 6.71 KB
/
Copy patharp-cache.cpp
File metadata and controls
251 lines (212 loc) · 6.71 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/* -*- 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
// printf("periodicCheck\n");
// Process ARP requests
for (auto it = m_arpRequests.begin(); it != m_arpRequests.end();)
{
auto &request = *it;
if (request->nTimesSent >= 5)
{
// Remove ARP request and send ICMP error for pending packets
for (auto &pendingPacket : request->packets)
{
m_router.SendICMPUnreachable(pendingPacket, 3, 3);
}
it = m_arpRequests.erase(it); // erase returns the next iterator
}
else
{
request->nTimesSent++;
m_router.SendArpRequest(request->ip);
++it; // Continue to next element
}
}
// Process ARP cache entries
for (auto it = m_cacheEntries.begin(); it != m_cacheEntries.end();)
{
auto &entry = *it;
if (!entry->isValid)
{
it = m_cacheEntries.erase(it); // erase returns the next iterator
}
else
{
++it; // Continue to next element
}
}
}
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// 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<ArpEntry>
ArpCache::lookupForce(const uint32_t ip)
{
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;
}
std::shared_ptr<ArpRequest>
ArpCache::queueRequestForce(const uint32_t ip, const Buffer &packet, const std::string &iface) {
// 查找是否存在相同的IP请求
for (auto it = m_arpRequests.begin(); it != m_arpRequests.end(); ++it) {
if ((*it)->ip == ip) {
// 如果找到了,返回这个请求的 shared_ptr
return *it;
}
}
// 如果没有找到,创建新的ARP请求并插入到队列中
auto newRequest = std::make_shared<ArpRequest>(ip);
// m_arpRequests.push_back(newRequest);
m_arpRequests.insert(m_arpRequests.end(),newRequest);
// 将数据包添加到请求的数据包列表中
newRequest->packets.push_back({packet, iface});
return newRequest;
}
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