Repository navigation
Expand file tree
/
Copy pathsimple-router.cpp
More file actions
622 lines (519 loc) · 22.3 KB
/
Copy pathsimple-router.cpp
File metadata and controls
622 lines (519 loc) · 22.3 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
/* -*- 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 "simple-router.hpp"
#include "core/utils.hpp"
#include <fstream>
namespace simple_router
{
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// IMPLEMENT THIS METHOD
uint16_t SimpleRouter::CalculateIPChecksum(const void *data, size_t length)
{
// Cast data to uint16_t pointer for easy access
const uint16_t *header = reinterpret_cast<const uint16_t *>(data);
uint32_t sum = 0;
// Sum up all 16-bit words
for (size_t i = 0; i < length / sizeof(uint16_t); i++)
{
sum += *header++;
}
// Fold 32-bit sum to 16 bits
while (sum >> 16)
{
sum = (sum & 0xffff) + (sum >> 16);
}
// Take the one's complement of the sum
return ~sum;
}
uint16_t SimpleRouter::CalculateICMPChecksum(const void *data, size_t length)
{
// Cast data to uint16_t pointer for easy access
const uint16_t *header = reinterpret_cast<const uint16_t *>(data);
uint32_t sum = 0;
// Sum up all 16-bit words
for (size_t i = 0; i < length / sizeof(uint16_t); i++)
{
sum += *header++;
}
// Fold 32-bit sum to 16 bits
while (sum >> 16)
{
sum = (sum & 0xffff) + (sum >> 16);
}
// Take the one's complement of the sum
return ~sum;
}
void SimpleRouter::SendICMPEchoReply(const Buffer &packet, const std::string &interfaceName)
{
Buffer replyPacket(packet);
uint8_t *data = replyPacket.data();
// Parse Ethernet, IP, and ICMP headers
ethernet_hdr *ethernetHeader = reinterpret_cast<ethernet_hdr *>(data);
ip_hdr *ipHeader = reinterpret_cast<ip_hdr *>(data + sizeof(ethernet_hdr));
icmp_hdr *icmpHeader = reinterpret_cast<icmp_hdr *>(data + sizeof(ethernet_hdr) + sizeof(ip_hdr));
// Get original packet headers
ip_hdr *originalIPHeader = (ip_hdr *)(packet.data() + sizeof(ethernet_hdr));
// Modify Ethernet header
uint8_t temp[ETHER_ADDR_LEN];
memcpy(temp, ethernetHeader->ether_dhost, ETHER_ADDR_LEN);
memcpy(ethernetHeader->ether_dhost, ethernetHeader->ether_shost, ETHER_ADDR_LEN);
memcpy(ethernetHeader->ether_shost, temp, ETHER_ADDR_LEN);
// Modify IP header
ipHeader->ip_id = 0;
ipHeader->ip_ttl = 0x40; // Set TTL to 64
ipHeader->ip_dst = originalIPHeader->ip_src;
ipHeader->ip_src = originalIPHeader->ip_dst;
ipHeader->ip_sum = 0; // Clear original IP checksum
ipHeader->ip_sum = cksum(ipHeader, sizeof(ip_hdr));
// Modify ICMP header
icmpHeader->icmp_sum = 0; // Clear original ICMP checksum
icmpHeader->icmp_code = 0x00; // Echo reply code
icmpHeader->icmp_type = 0x00; // Echo reply type
int icmpLength = packet.size() - sizeof(ip_hdr) - sizeof(ethernet_hdr); // Calculate ICMP length
icmpHeader->icmp_sum = cksum(icmpHeader, icmpLength);
// Send reply packet
sendPacket(replyPacket, interfaceName);
// Debug output
std::cout << "Sent ICMP echo reply to: " << ipToString(ipHeader->ip_dst) << std::endl;
}
void SimpleRouter::SendICMPType3(const PendingPacket &pendingPacket, uint8_t type, uint8_t code)
{
std::cout << "Sending ICMP type 3" << std::endl;
auto packet = pendingPacket.packet;
std::string interfaceName = pendingPacket.iface;
const Interface *interface = findIfaceByName(interfaceName);
Buffer icmpPacket(sizeof(ethernet_hdr) + sizeof(ip_hdr) + sizeof(icmp_t3_hdr));
uint8_t *data = icmpPacket.data();
// Get destination IP address
ip_hdr *originalIPHeader = reinterpret_cast<ip_hdr *>(packet.data() + sizeof(ethernet_hdr));
uint32_t destinationIP = originalIPHeader->ip_src;
auto arpEntry = m_arp.lookup(destinationIP);
if (!arpEntry)
{
std::cerr << "ARP entry not found for destination IP: "
<< ipToString(destinationIP) << std::endl;
return;
}
Buffer destinationMAC = arpEntry->mac;
// Construct Ethernet header
ethernet_hdr *ethernetHeader = reinterpret_cast<ethernet_hdr *>(data);
std::copy(interface->addr.data(), interface->addr.data() + ETHER_ADDR_LEN, ethernetHeader->ether_shost);
std::copy(destinationMAC.data(), destinationMAC.data() + ETHER_ADDR_LEN, ethernetHeader->ether_dhost);
ethernetHeader->ether_type = htons(ethertype_ip);
// Construct IP header
ip_hdr *ipHeader = reinterpret_cast<ip_hdr *>(data + sizeof(ethernet_hdr));
ipHeader->ip_v = 0x04;
ipHeader->ip_hl = 0x05;
ipHeader->ip_tos = 0x00;
ipHeader->ip_p = ip_protocol_icmp;
ipHeader->ip_src = interface->ip;
ipHeader->ip_dst = originalIPHeader->ip_src;
ipHeader->ip_len = htons(sizeof(icmp_t3_hdr) + sizeof(ip_hdr));
ipHeader->ip_id = static_cast<uint16_t>(rand());
ipHeader->ip_off = htons(IP_DF);
ipHeader->ip_ttl = 0x40;
ipHeader->ip_sum = 0;
ipHeader->ip_sum = cksum(ipHeader, sizeof(ip_hdr));
// Construct ICMP type 3 error header
icmp_t3_hdr *icmpHeader = reinterpret_cast<icmp_t3_hdr *>(data + sizeof(ethernet_hdr) + sizeof(ip_hdr));
icmpHeader->icmp_type = type;
icmpHeader->icmp_code = code;
icmpHeader->icmp_sum = 0;
icmpHeader->unused = 0;
icmpHeader->next_mtu = 0;
std::memcpy(icmpHeader->data, originalIPHeader, ICMP_DATA_SIZE);
icmpHeader->icmp_sum = cksum(icmpHeader, sizeof(icmp_t3_hdr));
// Send packet
sendPacket(icmpPacket, interfaceName);
}
void SimpleRouter::SendICMPUnreachable(const PendingPacket &pendingPacket, uint8_t type, uint8_t code)
{
auto packet = pendingPacket.packet;
std::string interfaceName = pendingPacket.iface;
const Interface *interface = findIfaceByName(interfaceName);
Buffer icmpPacket(sizeof(ethernet_hdr) + sizeof(ip_hdr) + sizeof(icmp_t3_hdr));
uint8_t *data = icmpPacket.data();
// Get destination IP address
ip_hdr *originalIPHeader = reinterpret_cast<ip_hdr *>(packet.data() + sizeof(ethernet_hdr));
uint32_t destinationIP = originalIPHeader->ip_src;
std::cerr << "Unreachable Host: " << ipToString(originalIPHeader->ip_dst) << std::endl;
auto arpEntry = m_arp.lookupForce(destinationIP);
Buffer destinationMAC = Buffer(ETHER_ADDR_LEN, 0);
if (arpEntry)
{
destinationMAC = arpEntry->mac;
}
// Construct Ethernet header
ethernet_hdr *ethernetHeader = reinterpret_cast<ethernet_hdr *>(data);
std::copy(interface->addr.data(), interface->addr.data() + ETHER_ADDR_LEN, ethernetHeader->ether_shost);
std::copy(destinationMAC.data(), destinationMAC.data() + ETHER_ADDR_LEN, ethernetHeader->ether_dhost);
ethernetHeader->ether_type = htons(ethertype_ip);
// Construct IPv4 header
ip_hdr *ipHeader = reinterpret_cast<ip_hdr *>(data + sizeof(ethernet_hdr));
ipHeader->ip_p = ip_protocol_icmp;
ipHeader->ip_src = interface->ip;
ipHeader->ip_dst = originalIPHeader->ip_src;
ipHeader->ip_v = 0x04;
ipHeader->ip_hl = 0x05;
ipHeader->ip_tos = 0x00;
ipHeader->ip_id = static_cast<uint16_t>(rand());
ipHeader->ip_off = htons(IP_DF);
ipHeader->ip_ttl = 0x40;
ipHeader->ip_len = htons(sizeof(icmp_t3_hdr) + sizeof(ip_hdr));
ipHeader->ip_sum = 0;
ipHeader->ip_sum = cksum(ipHeader, sizeof(ip_hdr));
// Construct ICMP header
icmp_t3_hdr *icmpHeader = reinterpret_cast<icmp_t3_hdr *>(data + sizeof(ethernet_hdr) + sizeof(ip_hdr));
icmpHeader->next_mtu = 0;
icmpHeader->icmp_code = code;
icmpHeader->icmp_type = type;
icmpHeader->icmp_sum = 0;
icmpHeader->unused = 0;
std::memcpy(icmpHeader->data, originalIPHeader, ICMP_DATA_SIZE);
icmpHeader->icmp_sum = cksum(icmpHeader, sizeof(icmp_t3_hdr));
// Send packet
if (!arpEntry)
{
m_arp.queueRequestForce(originalIPHeader->ip_src, icmpPacket, interfaceName);
}
else
{
sendPacket(icmpPacket, interfaceName);
}
}
void SimpleRouter::SendArpRequest(const uint32_t &destinationIp)
{
std::cout << "Sending ARP request to " << ipToString(destinationIp) << std::endl;
// Create a buffer for the ARP request packet
Buffer arpRequestPacket(sizeof(ethernet_hdr) + sizeof(arp_hdr));
unsigned char *packetData = arpRequestPacket.data();
// Retrieve interface information
std::string interfaceName = m_routingTable.lookup(destinationIp).ifName;
const Interface *interface = findIfaceByName(interfaceName);
if (interface == nullptr)
{
std::cerr << "Interface not found for " << interfaceName << std::endl;
return;
}
// Construct the Ethernet header
ethernet_hdr *ethernetHeader = reinterpret_cast<ethernet_hdr *>(packetData);
std::copy(interface->addr.begin(), interface->addr.begin() + ETHER_ADDR_LEN, ethernetHeader->ether_shost);
std::fill(ethernetHeader->ether_dhost, ethernetHeader->ether_dhost + ETHER_ADDR_LEN, 0xff); // Broadcast address
ethernetHeader->ether_type = htons(ethertype_arp);
// Construct the ARP header
arp_hdr *arpHeader = reinterpret_cast<arp_hdr *>(packetData + sizeof(ethernet_hdr));
arpHeader->arp_hrd = htons(arp_hrd_ethernet);
arpHeader->arp_pro = htons(ethertype_ip);
arpHeader->arp_hln = ETHER_ADDR_LEN;
arpHeader->arp_pln = sizeof(uint32_t);
arpHeader->arp_op = htons(arp_op_request);
std::copy(interface->addr.begin(), interface->addr.begin() + ETHER_ADDR_LEN, arpHeader->arp_sha);
arpHeader->arp_sip = interface->ip;
std::fill(arpHeader->arp_tha, arpHeader->arp_tha + ETHER_ADDR_LEN, 0); // Target MAC address is unknown
arpHeader->arp_tip = destinationIp;
// Send the ARP request packet
sendPacket(arpRequestPacket, interfaceName);
}
void SimpleRouter::SendArpReply(const uint32_t &senderIp, const uint32_t &targetIp,
const Buffer &senderMac, const Buffer &targetMac,
const std::string &interfaceName)
{
// Create a buffer for the ARP reply packet
Buffer arpReplyPacket(sizeof(ethernet_hdr) + sizeof(arp_hdr));
unsigned char *packetData = arpReplyPacket.data();
// Construct the Ethernet header
ethernet_hdr *ethernetHeader = reinterpret_cast<ethernet_hdr *>(packetData);
std::copy(senderMac.begin(), senderMac.begin() + ETHER_ADDR_LEN, ethernetHeader->ether_shost);
std::copy(targetMac.begin(), targetMac.begin() + ETHER_ADDR_LEN, ethernetHeader->ether_dhost);
ethernetHeader->ether_type = htons(ethertype_arp);
// Construct the ARP reply header
arp_hdr *arpHeader = reinterpret_cast<arp_hdr *>(packetData + sizeof(ethernet_hdr));
arpHeader->arp_hrd = htons(arp_hrd_ethernet);
arpHeader->arp_pro = htons(ethertype_ip);
arpHeader->arp_hln = ETHER_ADDR_LEN;
arpHeader->arp_pln = sizeof(uint32_t);
arpHeader->arp_op = htons(arp_op_reply);
std::copy(senderMac.begin(), senderMac.begin() + ETHER_ADDR_LEN, arpHeader->arp_sha);
arpHeader->arp_sip = senderIp;
std::copy(targetMac.begin(), targetMac.begin() + ETHER_ADDR_LEN, arpHeader->arp_tha);
arpHeader->arp_tip = targetIp;
// Output debug information
std::cout << "Sending ARP reply to " << ipToString(targetIp) << " from interface " << interfaceName << std::endl;
// Send the ARP reply packet
sendPacket(arpReplyPacket, interfaceName);
}
void SimpleRouter::sendIpv4(const Buffer &packet, const Interface *interface, const Buffer &dst_mac)
{
// 使用自动管理内存的Buffer对象
Buffer forward_pkt(packet);
uint8_t *data = forward_pkt.data();
// 构造以太网头
ethernet_hdr *eth = reinterpret_cast<ethernet_hdr *>(data);
memcpy(eth->ether_shost, interface->addr.data(), ETHER_ADDR_LEN);
memcpy(eth->ether_dhost, dst_mac.data(), ETHER_ADDR_LEN);
// 构造IP头并更新TTL和校验和
ip_hdr *ip = reinterpret_cast<ip_hdr *>(data + sizeof(ethernet_hdr));
ip->ip_ttl--;
ip->ip_sum = 0;
ip->ip_sum = cksum(ip, sizeof(ip_hdr));
// 发送数据包
sendPacket(forward_pkt, interface->name);
// 输出调试信息
std::cout << "send ipv4 to: " << ipToString(ip->ip_dst) << " via: " << interface->name << std::endl;
}
void SimpleRouter::handlePacket(const Buffer &packet, const std::string &inIface)
{
std::cout << "handlePacket" << std::endl;
const Interface *iface = findIfaceByName(inIface);
if (!iface)
{
std::cerr << "Unknown interface: " << inIface << std::endl;
return;
}
if (packet.size() < sizeof(ethernet_hdr))
{
std::cerr << "Invalid Ethernet header!" << std::endl;
return;
}
const unsigned char *data = packet.data();
ethernet_hdr *ethernet = (ethernet_hdr *)data;
uint16_t eth_type = ntohs(ethernet->ether_type);
switch (eth_type)
{
case ethertype_arp:
handleArp(data, iface, inIface);
break;
case ethertype_ip:
handleIpv4(data, packet, inIface);
break;
default:
std::cerr << "Unsupported Ethernet type: " << eth_type << std::endl;
}
}
void SimpleRouter::handleArp(const unsigned char *data, const Interface *iface, const std::string &inIface)
{
arp_hdr *arp = (arp_hdr *)(data + sizeof(ethernet_hdr));
uint16_t arp_type = ntohs(arp->arp_op);
ethernet_hdr *ethernet = (ethernet_hdr *)data;
Buffer src_mac(ETHER_ADDR_LEN);
memcpy(src_mac.data(), ethernet->ether_dhost, ETHER_ADDR_LEN);
Buffer dst_mac(ETHER_ADDR_LEN);
memcpy(dst_mac.data(), ethernet->ether_shost, ETHER_ADDR_LEN);
if (arp_type == arp_op_request)
{ // 处理 ARP 请求
if (arp->arp_tip == iface->ip)
{ // 目标是当前接口
std::cout << "Received ARP request from " << ipToString(arp->arp_sip) << std::endl;
SendArpReply(arp->arp_tip, arp->arp_sip, iface->addr,
src_mac, inIface);
}
}
else if (arp_type == arp_op_reply)
{ // 处理 ARP 回复
std::cout << "Received ARP reply from " << macToString(src_mac) << std::endl;
// 如果 ARP 缓存中没有该 IP,更新缓存并处理挂起请求
if (!m_arp.lookup(arp->arp_sip))
{
auto request = m_arp.insertArpEntry(dst_mac, arp->arp_sip);
if (!request)
return;
// 处理挂起的包
for (auto &pending_packet : request->packets)
{
handlePacket(pending_packet.packet, pending_packet.iface);
}
m_arp.removeRequest(request);
}
}
else
{
std::cerr << "Unsupported ARP operation: " << arp_type << std::endl;
}
}
void SimpleRouter::handleIpv4(const unsigned char *data, const Buffer &packet, const std::string &inIface)
{
ip_hdr *ip = (ip_hdr *)(data + sizeof(ethernet_hdr));
std::cout << "Received IPv4 packet from " << ipToString(ip->ip_src) << std::endl;
// 检查 IP 校验和
uint16_t sum = cksum(ip, sizeof(ip_hdr));
if (sum != 0xffff)
{
std::cerr << "Invalid IP checksum" << std::endl;
return;
}
// 如果目标 IP 是路由器自身
if (findIfaceByIp(ip->ip_dst))
{
if (ip->ip_p == ip_protocol_icmp)
{ // 如果是 ICMP
if (!handleIpSource(ip->ip_src, packet, inIface))
return;
SendICMPEchoReply(packet, inIface);
}
else
{ // 非 ICMP 协议 (如 TCP/UDP)
if (!handleIpSource(ip->ip_src, packet, inIface))
return;
SendICMPType3(PendingPacket({packet, inIface}), 3, 3); // Type 3, Code 3: Port unreachable
}
}
else
{ // 如果目标 IP 不是路由器自身
// TTL 检查
if (ip->ip_ttl <= 1)
{
if (!handleIpSource(ip->ip_src, packet, inIface))
return;
SendICMPType3(PendingPacket({packet, inIface}), 11, 0); // Type 11, Code 0: Time Exceeded
}
else
{ // 转发数据包
if (!forwardPacket(ip, packet, inIface))
return;
}
}
}
bool SimpleRouter::handleIpSource(uint32_t src_ip, const Buffer &packet, const std::string &inIface)
{
auto entry = m_arp.lookup(src_ip);
if (!entry)
{
m_arp.queueRequest(src_ip, packet, inIface);
return false;
}
return true;
}
bool SimpleRouter::forwardPacket(const ip_hdr *ip, const Buffer &packet, const std::string &inIface)
{
RoutingTableEntry forwardEntry = m_routingTable.lookup(ip->ip_dst);
if (forwardEntry.ifName.empty())
{
std::cerr << "No route to " << ipToString(ip->ip_dst) << std::endl;
return false;
}
const Interface *outIface = findIfaceByName(forwardEntry.ifName);
auto arpEntry = m_arp.lookup(ip->ip_dst);
if (!arpEntry)
{
m_arp.queueRequest(ip->ip_dst, packet, inIface);
return false;
}
// 转发数据包
sendIpv4(packet, outIface, arpEntry->mac);
return true;
}
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// You should not need to touch the rest of this code.
SimpleRouter::SimpleRouter()
: m_arp(*this)
{
}
void
SimpleRouter::sendPacket(const Buffer &packet, const std::string &outIface)
{
m_pox->begin_sendPacket(packet, outIface);
}
bool
SimpleRouter::loadRoutingTable(const std::string &rtConfig)
{
return m_routingTable.load(rtConfig);
}
void
SimpleRouter::loadIfconfig(const std::string &ifconfig)
{
std::ifstream iff(ifconfig.c_str());
std::string line;
while (std::getline(iff, line))
{
std::istringstream ifLine(line);
std::string iface, ip;
ifLine >> iface >> ip;
in_addr ip_addr;
if (inet_aton(ip.c_str(), &ip_addr) == 0)
{
throw std::runtime_error("Invalid IP address `" + ip + "` for interface `" + iface + "`");
}
m_ifNameToIpMap[iface] = ip_addr.s_addr;
}
}
void
SimpleRouter::printIfaces(std::ostream &os)
{
if (m_ifaces.empty())
{
os << " Interface list empty " << std::endl;
return;
}
for (const auto &iface : m_ifaces)
{
os << iface << "\n";
}
os.flush();
}
const Interface *
SimpleRouter::findIfaceByIp(uint32_t ip) const
{
auto iface = std::find_if(m_ifaces.begin(), m_ifaces.end(), [ip](const Interface &iface)
{ return iface.ip == ip; });
if (iface == m_ifaces.end())
{
return nullptr;
}
return &*iface;
}
const Interface *
SimpleRouter::findIfaceByMac(const Buffer &mac) const
{
auto iface = std::find_if(m_ifaces.begin(), m_ifaces.end(), [mac](const Interface &iface)
{ return iface.addr == mac; });
if (iface == m_ifaces.end())
{
return nullptr;
}
return &*iface;
}
const Interface *
SimpleRouter::findIfaceByName(const std::string &name) const
{
auto iface = std::find_if(m_ifaces.begin(), m_ifaces.end(), [name](const Interface &iface)
{ return iface.name == name; });
if (iface == m_ifaces.end())
{
return nullptr;
}
return &*iface;
}
void
SimpleRouter::reset(const pox::Ifaces &ports)
{
std::cerr << "Resetting SimpleRouter with " << ports.size() << " ports" << std::endl;
m_arp.clear();
m_ifaces.clear();
for (const auto &iface : ports)
{
auto ip = m_ifNameToIpMap.find(iface.name);
if (ip == m_ifNameToIpMap.end())
{
std::cerr << "IP_CONFIG missing information about interface `" + iface.name + "`. Skipping it" << std::endl;
continue;
}
m_ifaces.insert(Interface(iface.name, iface.mac, ip->second));
}
printIfaces(std::cerr);
}
} // namespace simple_router {