简单路由器设计与实现Word格式文档下载.doc

上传人:聆听****声音 文档编号:470259 上传时间:2023-04-29 格式:DOC 页数:38 大小:1.40MB
下载 相关 举报
简单路由器设计与实现Word格式文档下载.doc_第1页
第1页 / 共38页
简单路由器设计与实现Word格式文档下载.doc_第2页
第2页 / 共38页
简单路由器设计与实现Word格式文档下载.doc_第3页
第3页 / 共38页
简单路由器设计与实现Word格式文档下载.doc_第4页
第4页 / 共38页
简单路由器设计与实现Word格式文档下载.doc_第5页
第5页 / 共38页
简单路由器设计与实现Word格式文档下载.doc_第6页
第6页 / 共38页
简单路由器设计与实现Word格式文档下载.doc_第7页
第7页 / 共38页
简单路由器设计与实现Word格式文档下载.doc_第8页
第8页 / 共38页
简单路由器设计与实现Word格式文档下载.doc_第9页
第9页 / 共38页
简单路由器设计与实现Word格式文档下载.doc_第10页
第10页 / 共38页
简单路由器设计与实现Word格式文档下载.doc_第11页
第11页 / 共38页
简单路由器设计与实现Word格式文档下载.doc_第12页
第12页 / 共38页
简单路由器设计与实现Word格式文档下载.doc_第13页
第13页 / 共38页
简单路由器设计与实现Word格式文档下载.doc_第14页
第14页 / 共38页
简单路由器设计与实现Word格式文档下载.doc_第15页
第15页 / 共38页
简单路由器设计与实现Word格式文档下载.doc_第16页
第16页 / 共38页
简单路由器设计与实现Word格式文档下载.doc_第17页
第17页 / 共38页
简单路由器设计与实现Word格式文档下载.doc_第18页
第18页 / 共38页
简单路由器设计与实现Word格式文档下载.doc_第19页
第19页 / 共38页
简单路由器设计与实现Word格式文档下载.doc_第20页
第20页 / 共38页
亲,该文档总共38页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

简单路由器设计与实现Word格式文档下载.doc

《简单路由器设计与实现Word格式文档下载.doc》由会员分享,可在线阅读,更多相关《简单路由器设计与实现Word格式文档下载.doc(38页珍藏版)》请在冰点文库上搜索。

简单路由器设计与实现Word格式文档下载.doc

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),theyneedanexplicit'

errbuf'

variable.'

mayalsobesettowarningtextwhenpcap_open_live()succeds;

todetectthiscasethecallershouldstoreazero-lengthstringin'

beforecallingpcap_open_live()anddisplaythewarningtotheuserif'

isnolongerazero-lengthstring.

Apointertoa'

pcap_t'

whichcanbeusedasaparametertothefollowingcalls(pcap_compile() 

andsoon)andthatspecifiesanopenedWinPcapsession.Incaseofproblems,itreturnsNULLandthe'

variablekeepstheerrormessage.

(4).intpcap_next_ex(pcap_t*p,structpcap_pkthdr**pkt_header,constu_char**pkt_data)

Readapacketfromaninterfaceorfromanofflinecapture.Thisfunctionisusedtoretrievethenextavailablepacket,bypassingthecallbackmethodtraditionallyprovidedbylibpcap.pcap_next_exfillsthepkt_headerandpkt_dataparameters(seepcap_handler())withthepointerstotheheaderandtothedataofthenextcapturedpacket.

Thereturnvaluecanbe:

1ifthepackethasbeenreadwithoutproblems

0ifthetimeoutsetwithpcap_open_live()haselapsed.Inthiscasepkt_headerandpkt_datadon'

tpointtoavalidpacket

-1ifanerroroccurred

-2ifEOFwasreachedreadingfromanofflinecapture

(5).intpcap_compile(pcap_t*p,structbpf_program*fp,char*str,intoptimize,bpf_u_int32netmask)

Compileapacketfilter,convertinganhighlevelfilteringexpression(seeFilteringexpressionsyntax)inaprogramthatcanbeinterpretedbythekernel-levelfilteringengine.

(6).intpcap_setfilter(pcap_t*p,structbpf_program*fp)

Associateafiltertoacapture.此函数是要和函数pcap_compile()配合使用。

(7).intpcap_sendpacket(pcap_t*p,u_char*buf,intsize)

Sendarawpacket.

6.实验步骤

6.1WinPcap开发包安装与配置

下载、安装WinPcap开发包,配置VisualStudio2010开发环境。

具体步骤请同学们参阅网上资料自行完成。

6.2用户界面设计

(1).首先打开vs2010,新建工程,选择MFCApplication,输入工程名,点击OK,

图5-2

点击next,选择Dialogbased,点击Next跳到最后一个页面选择...Dlg,点击finish。

图5-3

图5-4

到此为止工程新建完毕,运行会出现以下页面,下面就为这个对话框拖拽控件。

图5-5

首先将对话框上面的三个控件删除,并拖拽相应的控件得到如下效果:

图5-6

图5-7

其中对话框可在ResourceView打开,控件在Toolbox中可以找到,

图5-8

其中启动、退出、添加路由、删除路由为button控件,两个空白区为ListBox,日志、路由表、子网掩码、目的地址、下一跳步为StaticText,三个输入IP的控件为IPAddressControl,将控件拖拽到相应的位置并且在控件的属性框中修改控件的Caption以及ID。

启动

ONSTART_BUTTON

退出

ONSTOP_BUTTON

日志

IDC_STATIC

第一个空白区

LOGGER_LIST

路由表

第二个空白区

ROUTER_LIST

子网掩码

子网掩码对应的IP输入区

IDC_NETMASK

目的地址

目的地址对应的IP输入区

IDC_IPADDRESS

下一跳步

下一跳步对应的IP输入区

IDC_NEXTHOP

添加路由

ADD_ROUTER_BUTTON

删除路由

DELETE_ROUTER_BUTTON

6.3代码实现

(1).事件响应函数及主流程

控件添加完成后为Button添加相应的单击事件并修改单击事件名称,对应关系如下:

OnStartClickedButton()

OnBnClickedButton()

OnAddRouterButton()

OnDeleteRouterButton()

VC程序首先执行程序初始化函数OnInitDialog(),在此函数中我们获取了MFC窗体指针。

当我们点击“启动”按钮时,程序会执行OnStartClickedButton()函数,此函数流程图如下图所示:

图5-9

当我们点击结束按钮时程序会执行OnBnClickedButton(),程序自动退出。

当程序退出时会触发ON_WM_DESTROY()消息,从而会执行OnDestroy()函数,并且程序在执行过程中每隔一段时间会执行OnTimer()函数。

(2).主要数据结构和函数声明

首先将程序所用变量以及函数复制到VCRouterDlg.cpp文件中:

#define MAX_IF 5//最大接口数目

#pragmapack

(1)

typedefstructFrameHeader_t{ //帧首部

UCHAR DesMAC[6];

//目的地址

UCHAR SrcMAC[6];

//源地址

USHORT FrameType;

//帧类型

}FrameHeader_t;

typedefstructARPFrame_t{ //ARP帧

FrameHeader_t FrameHeader;

//帧首部

WORD HardwareType;

//硬件类型

WORD ProtocolType;

//协议类型

BYTE HLen;

//硬件地址长度

BYTE PLen;

//协议地址长度

WORD Operation;

//操作值

UCHAR SendHa[6];

//源MAC地址

ULONG SendIP;

//源IP地址

UCHAR RecvHa[6];

//目的MAC地址

ULONG RecvIP;

//目的IP地址

}ARPFrame_t;

typedefstructIPHeader_t{ //IP首部

BYTE Ver_HLen;

//版本+头部长度

BYTE TOS;

//服务类型

WORD TotalLen;

//总长度

WORD ID;

//标识

WORD Flag_Segment;

//标志+片偏移

BYTE TTL;

//生存时间

BYTE Protocol;

//协议

WORD Checksum;

//头部校验和

ULONG SrcIP;

//源IP地址

ULONG DstIP;

//目的IP地址

}IPHeader_t;

typedefstructICMPHeader_t{//ICMP首部

BYTEType;

//类型

BYTECode;

//代码

WORDChecksum;

//校验和

WORDId;

WORDSequence;

//序列号

}ICMPHeader_t;

typedefstructIPFrame_t{ //IP帧

IPHeader_t IPHeader;

//IP首部

}IPFrame_t;

typedefstructip_t{//网络地址

ULONG IPAddr;

//IP地址

ULONG IPMask;

//子网掩码

}ip_t;

typedefstructIfInfo_t{ //接口信息

CString DeviceName;

//设备名

CString Description;

//设备描述

UCHAR MACAddr[6];

//MAC地址

CArray<

ip_t,ip_t&

>

ip;

//IP地址列表

pcap_t *adhandle;

//pcap句柄

}IfInfo_t;

typedefstructSendPacket_t{ //发送数据包结构

int len;

//长度

BYTE PktData[2000];

//数据缓存

ULONG TargetIP;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 自然科学 > 物理

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2