看了网上很多代码用的是vector维护路由表,这样在查找目的地址的时候使用的是遍历的方法,因此自己写了一个用map维护的路由表,这样可以加快查找的速度。当然了,对于实验平台上的测试样例,性能并不会提升多少,但是在路由表项非常多的情况下,使用map的查找可以达到logN的时间复杂度,速度是令人满意的。
/*
* THIS FILE IS FOR IP FORWARD TEST
*/
#include "sysInclude.h"
#include<map>
// system support
extern void fwd_LocalRcv(char *pBuffer, int length);
extern void fwd_SendtoLower(char *pBuffer, int length, unsigned int nexthop);
extern void fwd_DiscardPkt(char *pBuffer, int type);
extern unsigned int getIpv4Address( );
// implemented by students
//define my own route table, using map set
map<unsigned int,unsigned int> myroute_table;
//initialization my route table
void stud_Route_Init()
{
myroute_table.clear();
return;
}
void stud_route_add(stud_route_msg *proute)
{
unsigned int dest_add = (ntohl(proute->dest))&(0xffffffff<<(32-htonl(proute->masklen)));
unsigned int next_hop = (ntohl(proute->