classNetworkInterface { ... private: // Human-readable name of the interface std::string name_;
// The physical output port (+ a helper function `transmit` that uses it to send an Ethernet frame) std::shared_ptr<OutputPort> port_; voidtransmit( const EthernetFrame& frame )const{ port_->transmit( *this, frame ); }
// Ethernet (known as hardware, network-access-layer, or link-layer) address of the interface EthernetAddress ethernet_address_;
// IP (known as internet-layer or network-layer) address of the interface Address ip_address_;
// Datagrams that have been received std::queue<InternetDatagram> datagrams_received_ {};
// Mapping between next-hop IP address and Ethernet address // recording entry's exist time, delete entry if exceed 30s structentry{ EthernetAddress eaddr; size_t time; }; std::unordered_map<uint32_t, entry> ip_map_ {};
// Dategrams that wait to be sent structdgram_buffered{ InternetDatagram dgram; uint32_t ip; }; std::vector<dgram_buffered> datagrams_buffered_ {};
// send the buffered datagrams to dst after receive ARP voiddatagrams_clear( uint32_t dst );
// Mapping between request IP and the time the ARP has been sent // if its time exceed 5 seconds, it should be discarded. std::unordered_map<uint32_t, size_t> ip_request_ {}; };
voidNetworkInterface::tick( constsize_t ms_since_last_tick ) { // check the time of the request ARP for(auto it = ip_request_.begin(); it != ip_request_.end();) { it->second += ms_since_last_tick; if(it->second >= 5000) it = ip_request_.erase(it); else it++; }
// check the time of the mapping between IP address and Ethernet address for(auto it = ip_map_.begin(); it != ip_map_.end();) { (it->second).time += ms_since_last_tick; if((it->second).time >= 30000) it = ip_map_.erase(it); else it++; } }