简单路由器设计与实现
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
简单路由器设计与实现-CAL-FENGHAI-(2020YEAR-YICAI)_JINGBIAN
简单路由器设计与实现(选做)
1.实验目的
熟悉并掌握路由器的工作原理;熟悉Winpcap编程,掌握数据包捕获与发送的方法;在理解IP协议、ICMP协议与路由基本工作原理的基础上,完成一个(软件)路由器基本功能的设计与编程实现。
2.实验环境
接入局域网的PC机;
操作系统:Windows xp, Windows 7等;
推荐开发工具:Visual Studio 2010;
推荐编程语言:C++/ Visual C++;
函数库/开发包: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的分组,使得两个子网中的主机能够互相访问
图5-1 实
验验证环境配置示意图
4.实验方式
每位同学独立上机编程实验,实验指导教师现场指导。
5.参考内容
(1) WinPcap简介
Winpcap (windows packet capture)是windows平台下一个免费、公共的网络访问系统。开发Winpcap项目的目的在于为win32应用程序提供访问网络底层的能力。它用于windows系统下的直接的网络编程。关于Winpcap的介绍请参阅
。
Winpcap开发包可以在上下载,Winpcap的技术文档可以从下载。附录中给出了主要函数的说明文档。
(2) Winpcap主要函数及功能介绍
Winpcap部分主要函数及其功能介绍如下:
(1).int pcap_findalldevs_ex(char * source, struct pcap_rmtauth * auth, pcap_if_t ** alldevs, char * errbuf)
函数功能:
Create a list of network devices that can be opened with pcap_open().
参数说明:
source:a char* buffer that keeps the 'source localtion', according to the new WinPcap syntax. This source will be examined looking for adapters (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 authenticate 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 element 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 returned 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().
(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 include 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
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 filter, 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_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