July 2007
Intermediate to advanced
332 pages
10h 4m
English
The filter class forwarding (Example 11-48) performs a very simple packet forwarding operation. If the packet is inbound, it uses the network_table to select a NIC. Outbound packets can go to only one place, which is the NIC connected to the outbound Ethernet link. (In the NIC IP bindings in the input file, we made the simplifying assumption that the first pair was the outward link.) See Example 11-49 for the packet processing main program.
Example 11-48. Forwarding filter
class forwarding : public tbb::filter { const ip_t outgoing_ip; const nic_t outgoing_nic; network_table& network_config; public: forwarding (ip_t& _outgoing_ip, nic_t& _outgoing_nic, network_table& _network_config) : outgoing_ip(_outgoing_ip), outgoing_nic(_outgoing_nic), network_config(_network_config), filter (false /* is_serial*/) { } void* operator() (void* item) { packet_trace* packet = static_cast<packet_trace*> (item); if (packet->packetNic == outgoing_nic) { // packet is inbound, so translate it to the target Mac nic_t nextNic; if (find_next_hop (packet->packetDestIp, nextNic)) packet->packetNic = nextNic; else cerr << "No next hop found" << endl; } else { // packet is outbound, only one place it can go packet->packetSrcIp = outgoing_ip; packet->packetNic = outgoing_nic; } return packet; } bool find_next_hop (ip_t& ip, nic_t& nic) { network_table::const_accessor a; // acquires reader lock if (network_config.find (a, ip)) { nic = a->second; return true; } return false; ...