Sponge
CS144's user-space TCP library
router.hh
Go to the documentation of this file.
1 #ifndef SPONGE_LIBSPONGE_ROUTER_HH
2 #define SPONGE_LIBSPONGE_ROUTER_HH
3 
4 #include "network_interface.hh"
5 
6 #include <optional>
7 #include <queue>
8 
16 
17  public:
19 
22 
24 
30  void recv_frame(const EthernetFrame &frame) {
31  auto optional_dgram = NetworkInterface::recv_frame(frame);
32  if (optional_dgram.has_value()) {
33  _datagrams_out.push(std::move(optional_dgram.value()));
34  }
35  };
36 
39 };
40 
43 class Router {
46 
51 
52  public:
58  return _interfaces.size() - 1;
59  }
60 
62  AsyncNetworkInterface &interface(const size_t N) { return _interfaces.at(N); }
63 
65  void add_route(const uint32_t route_prefix,
66  const uint8_t prefix_length,
67  const std::optional<Address> next_hop,
68  const size_t interface_num);
69 
71  void route();
72 };
73 
74 #endif // SPONGE_LIBSPONGE_ROUTER_HH
NetworkInterface::recv_frame
std::optional< InternetDatagram > recv_frame(const EthernetFrame &frame)
Receives an Ethernet frame and responds appropriately.
Definition: network_interface.cc:40
AsyncNetworkInterface::recv_frame
void recv_frame(const EthernetFrame &frame)
Receives and Ethernet frame and responds appropriately.
Definition: router.hh:30
std::move
T move(T... args)
std::vector< AsyncNetworkInterface >
std::vector::size
T size(T... args)
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
EthernetFrame
Ethernet frame.
Definition: ethernet_frame.hh:8
std::queue
Router
A router that has multiple network interfaces and performs longest-prefix-match routing between them.
Definition: router.hh:43
IPv4Datagram
IPv4 Internet datagram
Definition: ipv4_datagram.hh:8
AsyncNetworkInterface::datagrams_out
std::queue< InternetDatagram > & datagrams_out()
Access queue of Internet datagrams that have been received.
Definition: router.hh:38
std::vector::push_back
T push_back(T... args)
NetworkInterface
A "network interface" that connects IP (the internet layer, or network layer) with Ethernet (the netw...
Definition: network_interface.hh:32
Router::route_one_datagram
void route_one_datagram(InternetDatagram &dgram)
Definition: router.cc:37
std::vector::at
T at(T... args)
Router::_interfaces
std::vector< AsyncNetworkInterface > _interfaces
The router's collection of network interfaces.
Definition: router.hh:45
Router::add_interface
size_t add_interface(AsyncNetworkInterface &&interface)
Definition: router.hh:56
AsyncNetworkInterface::_datagrams_out
std::queue< InternetDatagram > _datagrams_out
Definition: router.hh:15
AsyncNetworkInterface
A wrapper for NetworkInterface that makes the host-side interface asynchronous: instead of returning ...
Definition: router.hh:14
Router::interface
AsyncNetworkInterface & interface(const size_t N)
Access an interface by index.
Definition: router.hh:62
AsyncNetworkInterface::AsyncNetworkInterface
AsyncNetworkInterface(NetworkInterface &&interface)
Construct from a NetworkInterface.
Definition: router.hh:21
NetworkInterface::NetworkInterface
NetworkInterface(const EthernetAddress &ethernet_address, const Address &ip_address)
Construct a network interface with given Ethernet (network-access-layer) and IP (internet-layer) addr...
Definition: network_interface.cc:23
Router::route
void route()
Route packets between the interfaces.
Definition: router.cc:42
network_interface.hh