Repository navigation
Expand file tree
/
Copy pathrouting-table.cpp
More file actions
135 lines (117 loc) · 3.49 KB
/
Copy pathrouting-table.cpp
File metadata and controls
135 lines (117 loc) · 3.49 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
/* -*- 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 "routing-table.hpp"
#include "core/utils.hpp"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <unistd.h>
namespace simple_router {
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// IMPLEMENT THIS METHOD
RoutingTableEntry
RoutingTable::lookup(uint32_t ip) const
{
const RoutingTableEntry *longest_entry = nullptr;
uint32_t longest_mask = 0;
for (auto &entry : m_entries)
{
auto masked_ip = ip & entry.mask;
if ((entry.dest & entry.mask) == masked_ip)
{
if (longest_entry == nullptr || ntohl(entry.mask) > longest_mask)
{
longest_mask = ntohl(entry.mask);
longest_entry = &entry;
}
}
}
if (longest_entry)
{
return *longest_entry;
}
throw std::runtime_error("Routing entry not found");
}
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// You should not need to touch the rest of this code.
bool
RoutingTable::load(const std::string& file)
{
FILE* fp;
char line[BUFSIZ];
char dest[32];
char gw[32];
char mask[32];
char iface[32];
struct in_addr dest_addr;
struct in_addr gw_addr;
struct in_addr mask_addr;
if (access(file.c_str(), R_OK) != 0) {
perror("access");
return false;
}
fp = fopen(file.c_str(), "r");
while (fgets(line, BUFSIZ, fp) != 0) {
sscanf(line,"%s %s %s %s", dest, gw, mask, iface);
if (inet_aton(dest, &dest_addr) == 0) {
fprintf(stderr,
"Error loading routing table, cannot convert %s to valid IP\n",
dest);
return false;
}
if (inet_aton(gw, &gw_addr) == 0) {
fprintf(stderr,
"Error loading routing table, cannot convert %s to valid IP\n",
gw);
return false;
}
if (inet_aton(mask, &mask_addr) == 0) {
fprintf(stderr,
"Error loading routing table, cannot convert %s to valid IP\n",
mask);
return false;
}
addEntry({dest_addr.s_addr, gw_addr.s_addr, mask_addr.s_addr, iface});
}
return true;
}
void
RoutingTable::addEntry(RoutingTableEntry entry)
{
m_entries.push_back(std::move(entry));
}
std::ostream&
operator<<(std::ostream& os, const RoutingTableEntry& entry)
{
os << ipToString(entry.dest) << "\t\t"
<< ipToString(entry.gw) << "\t"
<< ipToString(entry.mask) << "\t"
<< entry.ifName;
return os;
}
std::ostream&
operator<<(std::ostream& os, const RoutingTable& table)
{
os << "Destination\tGateway\t\tMask\tIface\n";
for (const auto& entry : table.m_entries) {
os << entry << "\n";
}
return os;
}
} // namespace simple_router