简单路由器设计与实现(选做)1.实验目的熟悉并掌握路由器的工作原理;熟悉Winpcap编程,掌握数据包捕获与发送的方法;在理解IP协议、ICMP协议与路由基本工作原理的基础上,完成一个(软件)路由器基本功能的设计与编程实现。2.实验环境接入局域网的PC机;操作系统:Windowsxp,Windows7等;推荐开发工具:VisualStudio2010;推荐编程语言:C++/VisualC++;函数库/开发包:Winpcap。3.实验内容(1)学习并掌握winpcap编程:自行完成WinPcap教程中的内容,从而掌握winpcap编程。内容包括但不限于:a)获取设备列表;b)打开一个适配器并抓包(分组);c)解析分组;d)发送分组;学习内容及源程序参见WinPcap官方教程:。(2)利用winpcap开发包实现简单路由程序,该路由程序应该至少包括以下功能:a)IP数据包捕获和转发;b)ARP请求与解析;c)重新计算IP数据包的头部校验和;d)处理IP数据包的头部校验和;处理IP数据包的TTL值;e)静态路由表维护。在下图所示的完了过拓扑结构中,当所开发的路由器程序部署并运行在计算机C上时,它将(作为一个路由器)能够连通两个子网,正确地捕获、转发来自计算机A和计算机B的分组,使得两个子网中的主机能够互相访问计算机A计算机C192.168.1.92默认网关设为:192.168.1.44该机器设置为双IP:192.168.1.4410.10.1.44计算机B10.10.1.10默认网关设为:10.10.1.44图5-1实验验证环境配置示意图4.实验方式每位同学独立上机编程实验,实验指导教师现场指导。5.参考内容(1)WinPcap简介Winpcap(windowspacketcapture)是windows平台下一个免费、公共的网络访问系统。开发Winpcap项目的目的在于为win32应用程序提供访问网络底层的能力。它用于windows系统下的直接的网络编程。关于Winpcap的介绍请参阅=rQt7NLzLCFGDO8Fd2XoM3yMvpqRhA4NU6xjLdWtoS_JkMLVfgQ9mspXhMceA5RVYIl9CnA2w66uEPrSlUXKq3_。Winpcap开发包可以在上下载,Winpcap的技术文档可以从下载。附录中给出了主要函数的说明文档。(2)Winpcap主要函数及功能介绍Winpcap部分主要函数及其功能介绍如下:(1).intpcap_findalldevs_ex(char*source,structpcap_rmtauth*auth,pcap_if_t**alldevs,char*errbuf)函数功能:Createalistofnetworkdevicesthatcanbeopenedwithpcap_open().参数说明:source:achar*bufferthatkeepsthe'sourcelocaltion',accordingtothenewWinPcapsyntax.Thissourcewillbeexaminedlookingforadapters(localorremote)orpcapfiles,Thestringsthatmustbeprependedtothe'source'inordertodefineifwewantlocal/remoteadaptersorfilesisdefinedinthenewSourceSpecificationSyntax.auth:apointertoapcap_rmtauthstructure.ThispointerkeepstheinformationrequiredtoauthenticatetheRPCAPconnectiontotheremotehost.Thisparameterisnotmeaningfulincaseofaquerytothelocalhost:inthatcaseitcanbeNULL.alldevs:a'structpcap_if_t'pointer,whichwillbeproperlyallocatedinsidethisfunction.Whenthefunctionreturns,itissettopointtothefirstelementoftheinterfacelist;eachelementofthelistisoftype'structpcap_if_t'.errbuf:apointertoauser-allocatedbuffer(ofsizePCAP_ERRBUF_SIZE)thatwillcontaintheerrormessage(incasethereisone).返回值:'0'ifeverythingisfine,'-1'ifsomeerrorsoccurred.Thelistofthedevicesisreturnedinthe'alldevs'variable.Whenthefunctionreturnscorrectly,'alldevs'cannotbeNULL.Inotherwords,thisfunctionreturns'-1'alsoincasethesystemdoesnothaveanyinterfacetolist.(2).voidpcap_freealldevs_ex(pcap_if_t*alldevsp)函数功能:Freeaninterfacelistreturnedbypcap_findalldevs().(3).pcap_t*pcap_open(constchar*source,intsnaplen,intflags,intread_timeout,structpcap_rmtauth*auth,char*errbuf)函数功能:Openagenericsourceinordertocapture/send(WinPcaponly)traffic.参数说明:source:zero-terminatedstringcontainingthesourcenametoopen.ThesourcenamehastoincludetheformatprefixaccordingtothenewSourceSpecificationSyntaxanditcannotbeNULL.OnonLinuxsystemswith2.2orlaterkernels,adeviceargumentofanycanbeusedtocapturepacketsfromallinterfaces.Inordertomakesthesourcesyntaxeasier,pleaserememberthat:theadaptersreturnedbythepcap_findalldevs_ex()canbeusedimmediatelybythepcap_open()incasetheuserwantstopassitsownsourcestringtothepcap_open(),thepcap_createsrcstr()helpsincreatingthecorrectsourceidentifier.snaplen:lengthofthepacketthathastoberetained.Foreachpacketreceivedbythefilter,onlythefirst'snaplen'bytesarestoredinthebufferandpassedtotheuserapplication.Forinstance,snaplenequalto100meansthatonlythefirst100bytesofeachpacketarestored.flags:keepsseveralflagsthatcanbeneededforcapturingpackets.Theallowedflagsaredefinedinthepcap_open()flags.read_timeout:readtimeoutinmilliseconds.Thereadtimeoutisusedtoarrangethatthereadnotnecessarilyreturnimmediatelywhenapacketisseen,butthatitwaitsforsomeamountoftimetoallowmorepacketstoarriveandtoreadmultiplepacketsfromtheOSkernelinoneoperation.Notallplatformssupportareadtimeout;onplatformsthatdon't,thereadtimeoutisignored.auth:apointertoa'structpcap_rmtauth'thatkeepstheinformationrequiredtoauthenticatetheuseronaremotemachine.Incasethisisnotaremotecapture,thispointercanbesettoNULL.errbuf:apointertoauser-allocatedbufferwhichwillcontaintheerrorincasethisfunctionfails.Thepcap_open()andfindalldevs()aretheonlytwofunctionswhichhavethisparameter,sincetheydonothave(yet)apointertoapcap_tstructure,whichreservesspacefortheerrorstring.Sincethesefunctionsdonothave(yet)apcap_tpointer(thepcap_tpointerisNULLincaseoferrors),theyneedanexplicit'errbuf'variable.'errbuf'mayalsobesettowarningtextwhenpcap_open_live()succeds;todetectthiscasethecallershouldstoreazero-lengthstringin'errbuf'beforecallingpcap_open_live()anddisplaythewarningtotheuserif'errbuf'isnolongerazero-lengthstring.返回值:Apointertoa'pcap_t'whichcanbeusedasaparametertothefollowingcalls(pcap_compile()andsoon)andthatspecifiesanopenedWinPcapsession.Incaseofproblems,itreturnsNULLandthe'errbuf'variablekeepstheerrormessage.(4).intpcap_next_ex(pcap_t*p,structpcap_pkthdr**pkt_header,constu_char**pkt_data)函数功能:Readapacketfromaninterfaceorfromanofflinecapture.Thisfunctionisusedtoretrievethenextavailablepacket,bypas