WinPcap文档翻译第三章

本文档翻译自WinPcap开发包,重点介绍了如何打开网络适配器并捕获数据包。讲解了pcap_open()函数的参数含义,如snaplen、flags和to_ms,并提供了一个示例程序,该程序打印每个通过适配器传输的包的信息。程序展示了如何处理捕获的包,包括获取时间戳和长度。
摘要由CSDN通过智能技术生成

<!-- @page { margin: 2cm } P { margin-bottom: 0.21cm } -->

WinPcap 导引:使用WinPcap 简明指导

WinPcap tutorial:a step by step guide to using WinPcap

翻译自WinPcap 开发包的帮助文档- 俞凌峰

 

第三篇: 打开网络适配器和捕获数据包

Opening an adapter and capturing the pcakets

Now that we've seen how to obtain an adapter to play with,let's start the real job,opening an adapter and capturing some traffic.In this lesson we'll write a program that prints some information about each packet flowing through the adapter.

到现在为止,我们已经知道了如何获得一个网络适配器,那就让我们开始真正的工作吧!开打一个网络适配器并捕获一些网络数据。在本章中,我们将会编写一个能打印通过一个特定网络适配器传输的网络数据包的某些信息的程序。

The function that opens a capture device is pcap_open().The parameters,snaplen,flags and to_ms deserve some explanation.

snaplen specifies the portion of the pcaket to capture.On some Oses(like xBSD and Win32),the packet driver can be configured to capture only the initial part of any packet:this decreases the amount of data to copy to the application and therefore improves the efficiency of the capture.In this case we use the value 65536 which is higher than the greatest MTU that we could encounter.In this manner we ensure that the application will always receive the whole packet.

flags: the most important flag is the one that indicates if the adapter will be put in promiscuous mode.In normal operation,an adapter only captures packets from the network that are destined to it;the pcakets exchanged by other hosts are therefore ignored.Instead,when the adapter is in promiscuous mode it captures all packets whether they are destined to it or not.This means that on shared media(like non-switched Ethernet),WinPcap will be able to capture the packets of other hosts.Promiscuous mode is the default for most capture applications,so we enable it in the following example.

to_ms specifies the read timeout,in millseconds.A read on the adapter(for example,with pcap_dispatch() or pcap_next_ex())will always return after to_ms millseconds,enen if no pcakets are available from the network.to_ms also defines the interval between statistical reports if the adapter is in statistical mode(see the lesson “/ref wpcap_tut9”for information about statistical mode).Setting to_ms to 0 means no timeout,a read on the adapter never returns if no packets arrive.A -1 timeout on the other side causes a read on the adapter to always return immediately.

打开一个捕获设备的函数是pcap_open() 。该函数参数是snaplen,flags,to_ms, 下面分别是各个参数的解释说明。

snaplen 指明了捕获数据包的哪一部分。在有些操作系统中(如xBSD 系统和Win32 系统),包捕获驱动可以配置成只捕获每个数据包的初始化部分:这能减少网络拷贝到应用程序中的数据从而提高了捕获的效率。在本例中我们设置该参数值为65536 ,这个参数值大大超过了链路层MTUMTU 既最大传输单元,为某些链路的最大传输上限值) 。这样做

1> 捕获原始数据包,包括在共享网络上各主机发送/接收的以及相互之间交换的数据winpcap结构包;   2> 在数据包发往应用程序之前,按照自定义的规则将某些特殊的数据包过滤掉;   3> 在网络上发送原始的数据包;   4> 收集网络通信过程中的统计信息。   winpcap的主要功能在于独立于主机协议(如TCP-IP)而发送和接收原始数据包。也就是说,winpcap不能阻塞,过滤或控制其他应用程序数据包的发收,它仅仅只是监听共享网络上传送的数据包。因此,它不能用于QoS调度程序或个人防火墙。目前,winpcap开发的主要对象是windows NT/2000/XP,这主要是因为在使用winpcap的用户中只有一小部分是仅使用windows 95/98/Me,并且M$也已经放弃了对win9x的开发。因此本文相关的程序T-ARP也是面向NT/2000/XP用户的。其实winpcap中的面向9x系统的概念和NT系统的非常相似,只是在某些实现上有点差异,比如说9x只支持ANSI编码,而NT系统则提倡使用Unicode编码。有个软件叫sniffer pro.可以作网管软件用,有很多功能,可监视网络运行情况,每台网内机器的数据流量,实时反映每台机器所访问IP以及它们之间的数据流通情况,可以抓包,可对过滤器进行设置,以便只抓取想要的包,比如POP3包,smtp包,ftp包等,并可从中找到邮箱用户名和密码,还有ftp用户名和密码。它还可以在使用交换机的网络上监听,不过要在交换机上装它的一个软件。还有一个简单的监听软件叫Passwordsniffer,可截获邮箱用户名和密码,还有ftp用户名和密码,它只能用在HUB网络上。著名软件tcpdump及ids snort都是基于libpcap编写的,此外Nmap扫描器也是基于libpcap来捕获目标主机返回的数据包的。   winpcap提供给用户两个不同级别的编程接口:一个基于libpcap的wpcap.dll,另一个是较底层的packet.dll。对于一般的要与unix平台上libpcap兼容的开发来说,使用wpcap.dll是当然的选择。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值