Sponge
CS144's user-space TCP library
router.cc
Go to the documentation of this file.
1 #include "router.hh"
2 
3 #include <iostream>
4 
5 using namespace std;
6 
7 // Dummy implementation of an IP router
8 
9 // Given an incoming Internet datagram, the router decides
10 // (1) which interface to send it out on, and
11 // (2) what next hop address to send it to.
12 
13 // For Lab 6, please replace with a real implementation that passes the
14 // automated checks run by `make check_lab6`.
15 
16 // You will need to add private members to the class declaration in `router.hh`
17 
18 template <typename... Targs>
19 void DUMMY_CODE(Targs &&... /* unused */) {}
20 
25 void Router::add_route(const uint32_t route_prefix,
26  const uint8_t prefix_length,
27  const optional<Address> next_hop,
28  const size_t interface_num) {
29  cerr << "DEBUG: adding route " << Address::from_ipv4_numeric(route_prefix).ip() << "/" << int(prefix_length)
30  << " => " << (next_hop.has_value() ? next_hop->ip() : "(direct)") << " on interface " << interface_num << "\n";
31 
32  DUMMY_CODE(route_prefix, prefix_length, next_hop, interface_num);
33  // Your code here.
34 }
35 
38  DUMMY_CODE(dgram);
39  // Your code here.
40 }
41 
42 void Router::route() {
43  // Go through all the interfaces, and route every incoming datagram to its proper outgoing interface.
44  for (auto &interface : _interfaces) {
45  auto &queue = interface.datagrams_out();
46  while (not queue.empty()) {
47  route_one_datagram(queue.front());
48  queue.pop();
49  }
50  }
51 }
Router::add_route
void add_route(const uint32_t route_prefix, const uint8_t prefix_length, const std::optional< Address > next_hop, const size_t interface_num)
Add a route (a forwarding rule)
Definition: router.cc:25
router.hh
std::queue
std::queue::front
T front(T... args)
IPv4Datagram
IPv4 Internet datagram
Definition: ipv4_datagram.hh:8
Address::from_ipv4_numeric
static Address from_ipv4_numeric(const uint32_t ip_address)
Create an Address from a 32-bit raw numeric IP address.
Definition: address.cc:119
Router::route_one_datagram
void route_one_datagram(InternetDatagram &dgram)
Definition: router.cc:37
std::cerr
std::queue::pop
T pop(T... args)
std::uint32_t
std
std::queue::empty
T empty(T... args)
DUMMY_CODE
void DUMMY_CODE(Targs &&...)
Definition: router.cc:19
Address::ip
std::string ip() const
Dotted-quad IP address string ("18.243.0.1").
Definition: address.hh:51
Router::route
void route()
Route packets between the interfaces.
Definition: router.cc:42