check lab6 ok
This commit is contained in:
parent
00611e4880
commit
d1461afdda
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,2 +1,4 @@
|
|||||||
/build
|
/build
|
||||||
/.ccls-cache
|
/.ccls-cache
|
||||||
|
/.vscode
|
||||||
|
/.cache
|
||||||
@ -1,6 +1,7 @@
|
|||||||
#include "router.hh"
|
#include "router.hh"
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <tuple>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
@ -28,15 +29,37 @@ void Router::add_route(const uint32_t route_prefix,
|
|||||||
const size_t interface_num) {
|
const size_t interface_num) {
|
||||||
cerr << "DEBUG: adding route " << Address::from_ipv4_numeric(route_prefix).ip() << "/" << int(prefix_length)
|
cerr << "DEBUG: adding route " << Address::from_ipv4_numeric(route_prefix).ip() << "/" << int(prefix_length)
|
||||||
<< " => " << (next_hop.has_value() ? next_hop->ip() : "(direct)") << " on interface " << interface_num << "\n";
|
<< " => " << (next_hop.has_value() ? next_hop->ip() : "(direct)") << " on interface " << interface_num << "\n";
|
||||||
|
uint32_t mask = 0;
|
||||||
DUMMY_CODE(route_prefix, prefix_length, next_hop, interface_num);
|
if (prefix_length > 0) mask = (1 << 31) >> (prefix_length - 1);
|
||||||
// Your code here.
|
_route_table.push_back(std::make_tuple(route_prefix, prefix_length, next_hop, interface_num, mask));
|
||||||
}
|
}
|
||||||
|
|
||||||
//! \param[in] dgram The datagram to be routed
|
//! \param[in] dgram The datagram to be routed
|
||||||
void Router::route_one_datagram(InternetDatagram &dgram) {
|
void Router::route_one_datagram(InternetDatagram &dgram) {
|
||||||
DUMMY_CODE(dgram);
|
auto dst_ip = dgram.header().dst;
|
||||||
// Your code here.
|
auto best_match = _route_table.end();
|
||||||
|
int longest_prefix = 0;
|
||||||
|
for (auto rule = _route_table.begin(); rule != _route_table.end(); ++ rule) {
|
||||||
|
auto route_prefix = std::get<0>(*rule);
|
||||||
|
auto prefix_length = std::get<1>(*rule);
|
||||||
|
auto mask = std::get<4>(*rule);
|
||||||
|
if ((mask & route_prefix) == (mask & dst_ip)) {
|
||||||
|
if (longest_prefix <= prefix_length) {
|
||||||
|
longest_prefix = prefix_length;
|
||||||
|
best_match = rule;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (best_match != _route_table.end()) {
|
||||||
|
if (dgram.header().ttl <= 1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
dgram.header().ttl --;
|
||||||
|
}
|
||||||
|
auto next_hop = std::get<2>(*best_match).value_or(Address::from_ipv4_numeric(dgram.header().dst));
|
||||||
|
_interfaces[std::get<3>(*best_match)].send_datagram(dgram, next_hop);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Router::route() {
|
void Router::route() {
|
||||||
|
|||||||
@ -1,10 +1,13 @@
|
|||||||
#ifndef SPONGE_LIBSPONGE_ROUTER_HH
|
#ifndef SPONGE_LIBSPONGE_ROUTER_HH
|
||||||
#define SPONGE_LIBSPONGE_ROUTER_HH
|
#define SPONGE_LIBSPONGE_ROUTER_HH
|
||||||
|
|
||||||
|
#include "address.hh"
|
||||||
#include "network_interface.hh"
|
#include "network_interface.hh"
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <queue>
|
#include <queue>
|
||||||
|
#include <tuple>
|
||||||
|
|
||||||
//! \brief A wrapper for NetworkInterface that makes the host-side
|
//! \brief A wrapper for NetworkInterface that makes the host-side
|
||||||
//! interface asynchronous: instead of returning received datagrams
|
//! interface asynchronous: instead of returning received datagrams
|
||||||
@ -44,6 +47,9 @@ class Router {
|
|||||||
//! The router's collection of network interfaces
|
//! The router's collection of network interfaces
|
||||||
std::vector<AsyncNetworkInterface> _interfaces{};
|
std::vector<AsyncNetworkInterface> _interfaces{};
|
||||||
|
|
||||||
|
//! Internal routing table. Tuple(prefix, prefix_length, next_hop, interface, mask)
|
||||||
|
std::vector<std::tuple<uint32_t, uint8_t,std::optional<Address>, size_t, uint32_t>> _route_table {};
|
||||||
|
|
||||||
//! Send a single datagram from the appropriate outbound interface to the next hop,
|
//! Send a single datagram from the appropriate outbound interface to the next hop,
|
||||||
//! as specified by the route with the longest prefix_length that matches the
|
//! as specified by the route with the longest prefix_length that matches the
|
||||||
//! datagram's destination address.
|
//! datagram's destination address.
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user