ImageVerifierCode 换一换
格式:DOCX , 页数:21 ,大小:702.95KB ,
资源ID:9455135      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/9455135.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(简单路由器设计与实现.docx)为本站会员(b****7)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

简单路由器设计与实现.docx

1、简单路由器设计与实现简单路由器设计与实现简单路由器设计与实现(选做)1.实验目的熟悉并掌握路由器的工作原理;熟悉Winpcap编程,掌握数据包捕获与发送的方法;在理解IP协议、ICMP协议与路由基本工作原理的基础上,完成一个(软件)路由器基本功能的设计与编程实现。2.实验环境 接入局域网的PC机; 操作系统:Windows xp, Windows 7等; 推荐开发工具:Visual Studio 2010; 推荐编程语言:C+/ Visual C+; 函数库/开发包:Winpcap。3.实验内容(1)学习并掌握winpcap编程:自行完成WinPcap教程中的内容,从而掌握winpcap编程。

2、内容包括但不限于:a)获取设备列表;b)打开一个适配器并抓包(分组);c)解析分组;d)发送分组;学习内容及源程序参见WinPcap官方教程:。(2)利用winpcap开发包实现简单路由程序,该路由程序应该至少包括以下功能:a)IP数据包捕获和转发;b)ARP请求与解析;c)重新计算IP数据包的头部校验和;d)处理IP数据包的头部校验和;处理IP数据包的TTL值;e)静态路由表维护。在下图所示的完了过拓扑结构中,当所开发的路由器程序部署并运行在计算机C上时,它将(作为一个路由器)能够连通两个子网,正确地捕获、转发来自计算机A和计算机B的分组,使得两个子网中的主机能够互相访问图5-1 实验验证环

3、境配置示意图4.实验方式每位同学独立上机编程实验,实验指导教师现场指导。5.参考内容(1) WinPcap简介Winpcap (windows packet capture)是windows平台下一个免费、公共的网络访问系统。开发Winpcap项目的目的在于为win32应用程序提供访问网络底层的能力。它用于windows系统下的直接的网络编程。关于Winpcap的介绍请参阅。Winpcap开发包可以在上下载,Winpcap的技术文档可以从下载。附录中给出了主要函数的说明文档。(2) Winpcap主要函数及功能介绍Winpcap部分主要函数及其功能介绍如下:(1).int pcap_finda

4、lldevs_ex(char * source, struct pcap_rmtauth * auth, pcap_if_t * alldevs, char * errbuf)函数功能:Create a list of network devices that can be opened withpcap_open().参数说明:source:a char* buffer that keeps the source localtion, according to the new WinPcap syntax. This source will be examined looking for a

5、dapters (local or remote) or pcap files,The strings that must be prepended to the source in order to define if we want local/remote adapters or files is defined in the new Source Specification Syntax .auth:a pointer to a pcap_rmtauth structure. This pointer keeps the information required to authenti

6、cate the RPCAP connection to the remote host. This parameter is not meaningful in case of a query to the local host: in that case it can be NULL.alldevs:a struct pcap_if_t pointer, which will be properly allocated inside this function. When the function returns, it is set to point to the first eleme

7、nt of the interface list; each element of the list is of type struct pcap_if_t.errbuf: a pointer to a user-allocated buffer (of size PCAP_ERRBUF_SIZE) that will contain the error message (in case there is one).返回值:0 if everything is fine, -1 if some errors occurred. The list of the devices is return

8、ed in the alldevs variable. When the function returns correctly, alldevs cannot be NULL. In other words, this function returns -1 also in case the system does not have any interface to list.(2).void pcap_freealldevs_ex(pcap_if_t * alldevsp)函数功能:Free an interface list returned by pcap_findalldevs().(

9、3).pcap_t* pcap_open(const char * source, int snaplen, int flags, int read_timeout, struct pcap_rmtauth * auth, char * errbuf)函数功能:Open a generic source in order to capture / send (WinPcap only) traffic.参数说明:source:zero-terminated string containing the source name to open. The source name has to inc

10、lude the format prefix according to the new Source Specification Syntax and it cannot be on Linux systems with or later kernels, a device argument of any can be used to capture packets from all interfaces. In order to makes the source syntax easier, please remember that:the adapters returned by the

11、pcap_findalldevs_ex() can be used immediately by the pcap_open()in case the user wants to pass its own source string to the pcap_open(), the pcap_createsrcstr() helps in creating the correct source identifier.snaplen:length of the packet that has to be retained. For each packet received by the filte

12、r, only the first snaplen bytes are stored in the buffer and passed to the user application. For instance, snaplen equal to 100 means that only the first 100 bytes of each packet are stored.flags:keeps several flags that can be needed for capturing packets. The allowed flags are defined in the pcap_

13、open() flags .read_timeout:read timeout in milliseconds. The read timeout is used to arrange that the read not necessarily return immediately when a packet is seen, but that it waits for some amount of time to allow more packets to arrive and to read multiple packets from the OS kernel in one operat

14、ion. Not all platforms support a read timeout; on platforms that dont, the read timeout is ignored.auth:a pointer to a struct pcap_rmtauth that keeps the information required to authenticate the user on a remote machine. In case this is not a remote capture, this pointer can be set to NULL.errbuf:a

15、pointer to a user-allocated buffer which will contain the error in case this function fails. The pcap_open() and findalldevs() are the only two functions which have this parameter, since they do not have (yet) a pointer to a pcap_t structure, which reserves space for the error string. Since these fu

16、nctions do not have (yet) a pcap_t pointer (the pcap_t pointer is NULL in case of errors), they need an explicit errbuf variable. errbuf may also be set to warning text when pcap_open_live() succeds; to detect this case the caller should store a zero-length string in errbuf before calling pcap_open_

17、live() and display the warning to the user if errbuf is no longer a zero-length string.返回值: A pointer to a pcap_t which can be used as a parameter to the following calls (pcap_compile()and so on) and that specifies an opened WinPcap session. In case of problems, it returns NULL and the errbuf variab

18、le keeps the error message.(4). int pcap_next_ex(pcap_t * p, struct pcap_pkthdr * pkt_header, const u_char * pkt_data)函数功能:Read a packet from an interface or from an offline function is used to retrieve the next available packet, bypassing the callback method traditionally provided by libpcap. pcap_

19、next_ex fills the pkt_header and pkt_data parameters (see pcap_handler() with the pointers to the header and to the data of the next captured packet.返回值:The return value can be:1 if the packet has been read without problems0 if the timeout set with pcap_open_live() has elapsed. In this case pkt_head

20、er and pkt_data dont point to a valid packet-1 if an error occurred-2 if EOF was reached reading from an offline capture (5). int pcap_compile(pcap_t * p, struct bpf_program * fp, char * str, int optimize, bpf_u_int32 netmask)函数功能:Compile a packet filter, converting an high level filtering expressio

21、n (see Filtering expression syntax) in a program that can be interpreted by the kernel-level filtering engine.(6). int pcap_setfilter(pcap_t * p, struct bpf_program * fp)函数功能:Associate a filter to a capture. 此函数是要和函数pcap_compile()配合使用。(7). int pcap_sendpacket(pcap_t * p, u_char * buf, int size)函数功能:

22、 Send a raw packet.6.实验步骤 WinPcap开发包安装与配置下载、安装WinPcap开发包,配置VisualStudio 2010开发环境。具体步骤请同学们参阅网上资料自行完成。 用户界面设计(1).首先打开vs2010,新建工程,选择MFC Application,输入工程名,点击OK,图5-2点击next,选择Dialog based,点击Next跳到最后一个页面选择.Dlg,点击finish。图5-3图5-4到此为止工程新建完毕,运行会出现以下页面,下面就为这个对话框拖拽控件。图5-5首先将对话框上面的三个控件删除,并拖拽相应的控件得到如下效果:图5-6图5-7其中

23、对话框可在Resource View打开,控件在Toolbox中可以找到,图5-8其中启动、退出、添加路由、删除路由为button控件,两个空白区为List Box,日志、路由表、子网掩码、目的地址、下一跳步为Static Text,三个输入IP的控件为IP Address Control,将控件拖拽到相应的位置并且在控件的属性框中修改控件的Caption以及ID。启动ONSTART_BUTTON退出ONSTOP_BUTTON日志IDC_STATIC第一个空白区LOGGER_LIST路由表IDC_STATIC第二个空白区ROUTER_LIST子网掩码IDC_STATIC子网掩码对应的IP输入区

24、IDC_NETMASK目的地址IDC_STATIC目的地址对应的IP输入区IDC_IPADDRESS下一跳步IDC_STATIC下一跳步对应的IP输入区IDC_NEXTHOP添加路由ADD_ROUTER_BUTTON删除路由DELETE_ROUTER_BUTTON 代码实现(1). 事件响应函数及主流程控件添加完成后为Button添加相应的单击事件并修改单击事件名称,对应关系如下:启动OnStartClickedButton()退出OnBnClickedButton()添加路由OnAddRouterButton()删除路由OnDeleteRouterButton()VC程序首先执行程序初始化函

25、数OnInitDialog(),在此函数中我们获取了MFC窗体指针。当我们点击“启动”按钮时,程序会执行OnStartClickedButton()函数,此函数流程图如下图所示:图5-9当我们点击结束按钮时程序会执行OnBnClickedButton(),程序自动退出。当程序退出时会触发ON_WM_DESTROY()消息,从而会执行OnDestroy()函数,并且程序在执行过程中每隔一段时间会执行OnTimer()函数。(2). 主要数据结构和函数声明首先将程序所用变量以及函数复制到文件中:#define MAX_IF 5 函数代码实现PAddr) cpyMAC(pIfInfo-MACAddr

26、, ARPFrame-SendHa); return 0; void setMAC(UCHAR *MAC, UCHAR ch) for (int i=0; iname; IfInfoi.Description = d-description; for(a = d-addresses; a; a = a-next) if (a-addr-sa_family = AF_INET) = (struct sockaddr_in *)a-addr)-; = (struct sockaddr_in *)a-netmask)-; IfInfoi.(ipaddr); j+; if (i=MAX_IF) dha

27、ndle = pcap_open(IfInfoi.DeviceName, eviceName); MessageBox(strbuf); PostMessage(WM_QUIT, 0, 0); ACAddr, 0); dhandle, srcMAC, srcIP, IfInfoi.ip0.IPAddr); ACAddr, srcMAC) k+; continue; else break; while (!(j+ 10) | (k = IfCount); if (k != IfCount) MessageBox(至少有一个接口的MAC地址没能得到!); PostMessage(WM_QUIT,

28、0, 0); eviceName); (-1, 设备描述: + IfInfoi.Description); (-1,(MAC地址:+ MACntoa(IfInfoi.MACAddr); for (j = 0; j IfInfoi.(); j+) (-1,(IP地址:+IPntoa(IfInfoi.ipj.IPAddr); ; j+) = i; = IfInfoi.ipj.IPAddr & IfInfoi.ipj.IPMask; = IfInfoi.ipj.IPMask; = 0; ACAddr) + ); for (j = 0; j IfInfoi.(); j+) Filter1 += (ip dst host + IPntoa(IfInfoi.ipj.IPAddr) + ); if (j = (IfInfoi.() -1) & (i = (IfCount-1) Filter1 += ); else Filter1 += or ; if (i = (IfCount-1) Filter0 += ); else Filter0 += or ; Filter = Filter0 + and (arp and (ether21=0x2) or (not + Filter1 + ); sprintf_s(strbuf, %s, Filter); dhandl

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1