简单路由器设计与实现Word文件下载.doc
《简单路由器设计与实现Word文件下载.doc》由会员分享,可在线阅读,更多相关《简单路由器设计与实现Word文件下载.doc(38页珍藏版)》请在冰豆网上搜索。
![简单路由器设计与实现Word文件下载.doc](https://file1.bdocx.com/fileroot1/2022-10/3/89a87fbd-7cf6-4830-80d0-f590f40447e6/89a87fbd-7cf6-4830-80d0-f590f40447e61.gif)
推荐开发工具:
VisualStudio2010;
推荐编程语言:
C++/VisualC++;
函数库/开发包:
Winpcap。
3.实验内容
(1)学习并掌握winpcap编程:
自行完成WinPcap教程中的内容,从而掌握winpcap编程。
内容包括但不限于:
a)获取设备列表;
b)打开一个适配器并抓包(分组);
c)解析分组;
d)发送分组;
学习内容及源程序参见WinPcap官方教程:
http:
//www.winpcap.org/docs/docs_412/html/group_wpcap_tut.html。
(2)利用winpcap开发包实现简单路由程序,该路由程序应该至少包括以下功能:
a)IP数据包捕获和转发;
b)ARP请求与解析;
c)重新计算IP数据包的头部校验和;
d)处理IP数据包的头部校验和;
处理IP数据包的TTL值;
e)静态路由表维护。
在下图所示的完了过拓扑结构中,当所开发的路由器程序部署并运行在计算机C上时,它将(作为一个路由器)能够连通两个子网,正确地捕获、转发来自计算机A和计算机B的分组,使得两个子网中的主机能够互相访问
图5-1实验验证环境配置示意图
4.实验方式
每位同学独立上机编程实验,实验指导教师现场指导。
5.参考内容
(1)WinPcap简介
Winpcap(windowspacketcapture)是windows平台下一个免费、公共的网络访问系统。
开发Winpcap项目的目的在于为win32应用程序提供访问网络底层的能力。
它用于windows系统下的直接的网络编程。
关于Winpcap的介绍请参阅
Winpcap开发包可以在http:
//www.winpcap.org/上下载,Winpcap的技术文档可以从http:
//www.winpcap.org/docs/default.htm下载。
附录中给出了主要函数的说明文档。
(2)Winpcap主要函数及功能介绍
Winpcap部分主要函数及其功能介绍如下:
(1).intpcap_findalldevs_ex(char*source,structpcap_rmtauth*auth,pcap_if_t**alldevs,char*errbuf)
函数功能:
Createalistofnetworkdevicesthatcanbeopenedwith
pcap_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'
.
errbuf:
apointertoauser-allocatedbuffer(ofsizePCAP_ERRBUF_SIZE)thatwillcontaintheerrormessage(incasethereisone).
返回值:
'
0'
ifeverythingisfine,'
-1'
ifsomeerrorsoccurred.Thelistofthedevicesisreturnedinthe'
alldevs'
variable.Whenthefunctionreturnscorrectly,'
cannotbeNULL.Inotherwords,thisfunctionreturns'
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.
zero-terminatedstringcontainingthesourcenametoopen.ThesourcenamehastoincludetheformatprefixaccordingtothenewSourceSpecificationSyntaxanditcannotbeNULL.OnonLinuxsystemswith2.2orlaterkernels,adeviceargumentof"
any"
canbeusedtocapturepacketsfromallinterfaces.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.
apointertoa'
structpcap_rmtauth'
thatkeepstheinformationrequiredtoauthenticatetheuseronaremotemachine.Incasethisisnotaremotecapture,thispointercanbesettoNULL.
apointertoauser-allocatedbufferwhichwillcontaintheerrorincasethisfunctionfails.Thepcap_open()andfindalldevs()aretheonlytwofunctionswhichhavethisparameter,sincetheydonothave(yet)apointertoapcap_tstructure,whichreservesspacefortheerrorstring.Sincethesefunctionsdonothave(yet)apcap_tpointer(thepcap_tpointerisNULLincaseoferrors),theyneedanexpl