循序渐进学习使用WINPCAP(七)

来源:互联网 发布:淘宝店铺能换支付宝吗 编辑:程序博客网 时间:2024/06/06 01:04

处理脱机的转储文件

在这一章,我们将要学习如何处理数据包,继而将它转储到文件中。WinPcap提供了丰富的函数来将流经网络的数据包保存到一个转储文件并且读取转储文件的内容。这一章将讲述如何使用这些函数。我们也将看到如何使用WinPcap的内核转储功能来得到高性能的转储(注意:目前,由于新内核缓存的一些问题,这个功能暂时被关闭)。

转储文件的格式很简单,是libpcap的一种。它包含了所捕捉的数据报的二进制内容,这种格式也是很多网络工具的标准,如WinDump,Ethereal和Snort。

  • 关于如何将数据报保存到转储文件

首先,让我们看看如何以libpcap的格式书写数据报。下面的例子从所选的接口捕捉数据报并将它们存储到一个用户指定的文件。

 

正如你所看到的那样,程序的结构与前几章的非常相似。不同在于:

  1. 一旦打开网卡就调用pcap_dump_open()来打开一个文件,该调用将文件与某个网卡相关联
  2. packet_handler()内部通过调用pcap_dump()来将捕捉到的数据报存储到文件。pcap_dump()的参数和pcap_handler()的参数一一对应的。
  • 从转储文件中读取数据报

现在,我们已经拥有了一个可用的转储文件,我们来试着读取它的内容。下面的代码打开一个WinPcap/libpcap的转储文件,并且显示文件中每个数据报的内容。文件是用pcap_open_offline()打开的,之后用pcap_loop()来循环从文件中读取数据。你就会发现,所读取的离线的数据几乎和从物理网卡上读取的一模一样。

这个例子中引入了另一个函数:pcap_createsrcsrc()。这个函数需要创建以一个标记开头的源字符串,这个标记是用来告诉WinPcap源的类型。例如,如果我们要打开一个适配器,标记就为“rpcap://”;如果我们要打开一个文件,就标记“file://”。如果用了函数pcap_findalldevs_ex(),这一步就不需要了,因为此函数的返回值以已经包含了那个源字符串了。然而,在这个例子中这一步是需要的,因为文件的名字是用户输入的。

下面的例子与上面的功能一样,只不过pcap_next_ex()用来代替pcap_loop()循环读取数据而已。

 

  • 用pcap_live_dump将数据写入到转储文件

注意:就目前而言,由于新内核缓存的一些问题,这项功能被关闭。

当前的WinPcap版本提供更进一步的方法保存网络数据到磁盘,该方法为pcap_live_dump()函数。pcap_live_dump()有三个参数:文件名,文件所允许达到的最大的大小(byte)以及该文件最大允许容纳的最大的数据报的数量。后两个参数为0意味着没有限制。该程序内可以设置一个过滤器(使用pcap_setfilter(),可参见过滤数据那章)在调用pcap_live_dump()之前,来定义要保存的子数据。

pcap_live_dump()是非阻塞的,所以它会一开始转储就立刻返回:数据的转储过程会异步进行,直到文件到达指定的最大长度或最大数据报的数目为止。

应用程序能够用pcap_live_dump_ended()来等待或检查数据是否转储完毕。如果指定的最大长度参数和数据报数量为0,则该操作将永远阻塞。

pcap_live_dump()和pcap_dump()的不同之处,撇开设置的限制不说就是性能的问题。pcap_live_dump()采用WinPcap NPF驱动来从内核级的层次上向文件中写数据,从而使内存拷贝的最小化。

显然,这些特点当前在其他操作系统下是不能够实现的,pcap_live_dump()是WinPcap所特有的,而且只能够应用于Win32环境。

附原文:

In this lession we are going to learn how to handle packet capture to a file (dump to file). WinPcap offers a wide range of functions to save the network traffic to a file and to read the content of dumps -- this lesson will teach how to use all of these functions. We'll see also how to use the kernel dump feature of WinPcap to obtain high-performance dumps (NOTE: At the moment, due to some problems with the new kernel buffer, this feature has been disabled).

The format for dump files is the libpcap one. This format contains the data of the captured packets in binary form and is a standard used by many network tools including WinDump, Ethereal and Snort.

  • Saving packets to a dump file

First of all, let's see how to write packets in libpcap format.

The following example captures the packets from the selected interface and saves them on a file whose name is provided by the user.

/* codes */

As you can see, the structure of the program is very similar to the ones we have seen in the previous lessons. The differences are:

  1. a call to pcap_dump_open() is issued once the interface is opened. This call opens a dump file and associates it with the interface.
  2. the packets are written to this file with a pcap_dump() from the packet_handler() callback. The parameters of pcap_dump() are in 1-1 correspondence with the parameters of pcap_handler().
  • Reading packets from a dump file

Now that we have a dump file available, we can try to read its content. The following code opens a WinPcap/libpcap dump file and displays every packet contained in the file. The file is opened with pcap_open_offline(), then the usual pcap_loop() is used to sequence through the packets. As you can see, reading packets from an offline capture is nearly identical to receiving them from a physical interface.

This example introduces another function: pcap_createsrcsrc(). This function is required to create a source string that begins with a marker used to tell WinPcap the type of the source, e.g. "rpcap://" if we are going to open an adapter, or "file://" if we are going to open a file. This step is not required when pcap_findalldevs_ex() is used (the returned values already contain these strings). However, it is required in this example because the name of the file is read from the user input.

/* codes */

The following example has the same purpose of the last one, but pcap_next_ex() is used instead of the pcap_loop() callback method.

/* codes */

  • Writing packets to a dump file with pcap_live_dump

NOTE: At the moment, due to some problems with the new kernel buffer, this feature has been disabled.

Recent versions of WinPcap provide a further way to save network traffic to disk, the pcap_live_dump() function. pcap_live_dump() takes three parameters: a file name, the maximum size (in bytes) that this file is allowed to reach and the maximum amount of packets that the file is allowed to contain. Zero means no limit for both these values. Notice that the program can set a filter (with pcap_setfilter(), see the tutorial Filtering the traffic) before calling pcap_live_dump() to define the subset of the traffic that will be saved.

pcap_live_dump() is non-blocking, therefore it starts the dump and returns immediately: The dump process goes on asynchronously until the maximum file size or the maximum amount of packets has been reached.

The application can wait or check the end of the dump with pcap_live_dump_ended(). Beware that if the sync parameter is nonzero, this function will block your application forever if the limits are both 0.

/* codes */

The difference between pcap_live_dump() and pcap_dump(), apart from the possibility to set limits, is performance. pcap_live_dump() exploits the ability of the WinPcap NPF driver (see NPF driver internals manual) to write dumps from kernel level, minimizing the number of context switches and memory copies.

Obviously, since this feature is currently not available on other operating systems, pcap_live_dump() is WinPcap specific and is present only under Win32.

#end

原创粉丝点击