-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.cpp
68 lines (53 loc) · 1.9 KB
/
table.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
#include "table.h"
#include <fstream>
#include <string>
#include <arpa/inet.h>
#include <algorithm>
#include <iostream>
RoutingTable::RoutingTable(const char* filename) : tableFile(filename) {}
uint32_t RoutingTable::addr_stoh(std::string ipString) {
// Use sockaddr_in struct and inet funcitons to convert string
// representation of IP to int
struct sockaddr_in sa;
inet_pton(AF_INET, ipString.c_str(), &(sa.sin_addr));
return ntohl(sa.sin_addr.s_addr);
}
const char* RoutingTable::addr_htos(uint32_t ipInt) {
char str[INET_ADDRSTRLEN];
struct sockaddr_in sa;
// Use sockaddr_in struct and inet functions to convert host order
// int to string
sa.sin_addr.s_addr = htonl(ipInt);
inet_ntop(AF_INET, &(sa.sin_addr), str, INET_ADDRSTRLEN);
std::string output = str;
return output.c_str();
}
void RoutingTable::read() {
std::ifstream infile(tableFile, std::ios::in);
std::string sNetid, sNetmask, sNextHop;
// Get the IP Addresses from each row and store them in vector
while(infile >> sNetid >> sNetmask >> sNextHop) {
Route route;
route.netid = addr_stoh(sNetid);
route.netmask = addr_stoh(sNetmask);
route.nextHop = addr_stoh(sNextHop);
routes.push_back(route);
}
// Sort the vector so longer netids occur before smaller. Used
// for longest pattern matching
std::sort(routes.begin(), routes.end(), [] (Route a, Route b) {
return a.netmask > b.netmask;
} );
infile.close();
}
uint32_t RoutingTable::getNextHop(uint32_t destination) {
for( auto it : routes) {
// For each route, see if the destination address matches the netid
if( (it.netmask & destination) == it.netid ) {
return it.nextHop;
}
}
std::cout << "Error: Routing table does not have a case for this destination - "
<< destination << std::endl;
return -1;
}