|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "network_interface.hh" |
| 4 | + |
| 5 | +#include <optional> |
| 6 | +#include <queue> |
| 7 | + |
| 8 | +// A wrapper for NetworkInterface that makes the host-side |
| 9 | +// interface asynchronous: instead of returning received datagrams |
| 10 | +// immediately (from the `recv_frame` method), it stores them for |
| 11 | +// later retrieval. Otherwise, behaves identically to the underlying |
| 12 | +// implementation of NetworkInterface. |
| 13 | +class AsyncNetworkInterface : public NetworkInterface |
| 14 | +{ |
| 15 | + std::queue<InternetDatagram> datagrams_in_ {}; |
| 16 | + |
| 17 | +public: |
| 18 | + using NetworkInterface::NetworkInterface; |
| 19 | + |
| 20 | + // Construct from a NetworkInterface |
| 21 | + explicit AsyncNetworkInterface( NetworkInterface&& interface ) : NetworkInterface( interface ) {} |
| 22 | + |
| 23 | + // \brief Receives and Ethernet frame and responds appropriately. |
| 24 | + |
| 25 | + // - If type is IPv4, pushes to the `datagrams_out` queue for later retrieval by the owner. |
| 26 | + // - If type is ARP request, learn a mapping from the "sender" fields, and send an ARP reply. |
| 27 | + // - If type is ARP reply, learn a mapping from the "target" fields. |
| 28 | + // |
| 29 | + // \param[in] frame the incoming Ethernet frame |
| 30 | + void recv_frame( const EthernetFrame& frame ) |
| 31 | + { |
| 32 | + auto optional_dgram = NetworkInterface::recv_frame( frame ); |
| 33 | + if ( optional_dgram.has_value() ) { |
| 34 | + datagrams_in_.push( std::move( optional_dgram.value() ) ); |
| 35 | + } |
| 36 | + }; |
| 37 | + |
| 38 | + // Access queue of Internet datagrams that have been received |
| 39 | + std::optional<InternetDatagram> maybe_receive() |
| 40 | + { |
| 41 | + if ( datagrams_in_.empty() ) { |
| 42 | + return {}; |
| 43 | + } |
| 44 | + |
| 45 | + InternetDatagram datagram = std::move( datagrams_in_.front() ); |
| 46 | + datagrams_in_.pop(); |
| 47 | + return datagram; |
| 48 | + } |
| 49 | +}; |
| 50 | + |
| 51 | +// A router that has multiple network interfaces and |
| 52 | +// performs longest-prefix-match routing between them. |
| 53 | +class Router |
| 54 | +{ |
| 55 | + // The router's collection of network interfaces |
| 56 | + std::vector<AsyncNetworkInterface> interfaces_ {}; |
| 57 | + |
| 58 | +public: |
| 59 | + // Add an interface to the router |
| 60 | + // interface: an already-constructed network interface |
| 61 | + // returns the index of the interface after it has been added to the router |
| 62 | + size_t add_interface( AsyncNetworkInterface&& interface ) |
| 63 | + { |
| 64 | + interfaces_.push_back( std::move( interface ) ); |
| 65 | + return interfaces_.size() - 1; |
| 66 | + } |
| 67 | + |
| 68 | + // Access an interface by index |
| 69 | + AsyncNetworkInterface& interface( size_t N ) { return interfaces_.at( N ); } |
| 70 | + |
| 71 | + // Add a route (a forwarding rule) |
| 72 | + void add_route( uint32_t route_prefix, |
| 73 | + uint8_t prefix_length, |
| 74 | + std::optional<Address> next_hop, |
| 75 | + size_t interface_num ); |
| 76 | + |
| 77 | + // Route packets between the interfaces. For each interface, use the |
| 78 | + // maybe_receive() method to consume every incoming datagram and |
| 79 | + // send it on one of interfaces to the correct next hop. The router |
| 80 | + // chooses the outbound interface and next-hop as specified by the |
| 81 | + // route with the longest prefix_length that matches the datagram's |
| 82 | + // destination address. |
| 83 | + void route(); |
| 84 | +}; |
0 commit comments