huyc

导航

pcap(3)

PCAP(3PCAP) 

NAME
pcap —— 数据包抓取库

SYNOPSIS

#include <pcap/pcap.h>

DESCRIPTION
数据包抓取库提供数据包抓取系统的高层接口。所有网络上的数据包,即便他们目标是其他主机,也能通过此机制访问到。它还支持将抓取的数据包保存到“savefile”,并可以从这个“savefile”读取出来。

调用pcap_create()可以为一个活动的抓取器打开一个句柄,为其设置适当的选项并调用pcap_activate()激活它。调用pcap_open_offline()可以打开并获取存有离线数据的“savefile”文件的句柄。pcap_create()和pcap_open_offline()都返回一个pcap_t指针,这便是用于从数据流中读取数据包的句柄,同样它也用于获取数据流的信息。

抓包器的可用选项包括:

快照长度
比如说抓包时,你抓取整个数据包,这需要更多的CPU时间以将其拷贝到你的应用程序,更多的磁盘和可能的网络带宽以将其数据写入文件,和更多的磁盘空间来保存该数据包。如果你不需要数据包的整个内容——例如,你只对其中的TCP报头感兴趣——你可以将该抓包器的“快照长度”设置为合适的长度。如果快照长度被设置为snaplen,而snaplen比抓到的数据包长度要短,则只有前面的snaplen个字节被捕获并作为数据包的数据。

对于抓取整个数据包而言,65535的长度对于绝大多数的网络都足够了。

通过pcap_set_snaplen()以设置快照长度。

混杂模式
On broadcast LANs such as Ethernet, if the network isn't switched, or if the adapter is connected to a "mirror port" on a switch to which all packets passing through the switch are sent, a network adapter receives all packets on the LAN, including unicast or multicast packets not sent to a network address that the network adapter isn't configured to recognize.

正常情况下,该适配器会忽略这些数据包,然而,许多网络适配器支持“混杂模式”,该模式下适配器将接受所有数据包,即便那不是发到该适配器的。这有利于捕获其他的主机进行分析。

记住,即便应用程序不设置混杂模式,适配器也会因为某些原因处在混杂模式。

现在而言,这不能在“any”设备下工作,如果提供了“any”和NULL,混杂模式将被忽略。

通过pcap_set_promisc()可以设置混杂模式。

监视模式
在IEEE 802.11无线局域网,即便适配器工作在混杂模式,它也只在它所在的网络中获取传递到该主机的帧。它也许只提供数据帧,而非管理或控制帧,也许不提供这些帧的802.11头部或无线信息的伪头部。

在“监视模式”下,有时叫做“rfmon模式”(“Radio Frequency MONitor”),适配器将提供收到的所有帧,包括802.11头部,以及可能的无线信息伪头部。

记住,监视模式下的适配器也许会从它所在的网络脱离开来,以致于你无法在该适配器上使用任何无线网络。这使得无法访问网络服务器上的文件,或解析域名以及网络地址,如果你在监视模式下抓包,并且没有使用另外一个适配器连接到另外的网络。

通过pcap_set_rfmon()设置监视模式,pcap_can_set_rfmon()可以用于确定适配器是否可以进入监视模式。

超时读取
假设抓包时,数据包在它到达之后就立刻交付,抓包的应用程序会在每个数据包到达时被唤醒,并执行一个或多个调用以取出每个数据包。

假如又说,数据包在它们到达之后不立刻交付,而是在一小段延时之后交付(叫做“超时读取”),在交付之前,多个数据包可以累积在一起,于是一次唤醒可以处理多个数据包,每个调用可以处理操作系统提供的多个数据包而非单个。如果数据包频繁到达的话,这减少了每个数据包的CPU开销,提高了每秒可以捕获的数据包数量。

超时读取是必要的,如此则应用程序不用在数据包交付之前等待操作系统填满捕获缓冲区,如果数据包很慢到达,这样的等待可以持续任意长的时间。

不是所有平台都支持超时读取,如果该平台不支持,则超时读取被乎略。对于0超时值来说,在支持超时读取的平台上,将引起无限期等待,直至足够多的数据包到达,永不超时。

NOTE:超时读取不能用于触发调用,这些调用读取数据包并在一段时间内返回,因为在一些平台上,超时读取是不被支持的,并且,另外一些平台上,在至少一个数据包到来之前定时器不会启动。这意味着超时读取不应该被利用。例如,在一个交互式程序中允许抓包循环由用户周期性输入来驱动,但实际上无法确保读取数据包的调用会在期望的时间返回,即便没有数据包到达。

通过pcap_set_timeout()设置读取超时。

缓冲大小
抓包器抓下的包被存放在缓冲区,因而它们不需要在到达时就被应用程序读走。一些平台上,缓冲大小是可以设置的,太小的缓冲意味着,如果捕捉到太多数据包,而且快照长度没有限制缓冲的数据量,当缓冲区在应用程序来读走数据包前被填满,则数据包将被丢弃;而太大的缓冲意味着将占用过多不可分页的OS内存。

通过pcap_set_buffer_size()设置缓冲大小。

从网络接口中读取数据包要求具有特定的特权:

Under SunOS 3.x or 4.x with NIT or BPF:
你必须具有/dev/nit或/dev/bpf*的读权限。

Under Solaris with DLPI:
你必须具有对网络虚拟设备的读写权限,比如/dev/le。在至少一些版本的Solaris上,这还不足以保证tcpdump工作在混杂模式。这些版本的Solaris上,你必须是root,或者有效uid是root,才能启用混杂模式。记住,对于许多(也许是所有)接口,如果你不启用混杂模式抓包,你将看不到任何外出数据包,因此抓包时不能启用混杂模式将十分不利。

In newer versions of Solaris, you must have been given the net_rawaccess privilege; this is both necessary and
sufficient to give you access to the network pseudo-device - there is no need to change the privileges on that
device. A user can be given that privilege by, for example, adding that privilege to the user's defaultpriv key
with the usermod (1M) command.

Under HP-UX with DLPI:
你必须是root,或者应用程序的有效uid是root。

Under IRIX with snoop:
你必须是root,或者应用程序的有效uid是root。

Under Linux:
你必须是root或者有效uid是root。(unless your distribution has a kernel that supports capability bits such as CAP_NET_RAW and code to allow those capability bits to be given to particular accounts and to cause those bits to be set on a user's initial processes when they log in, in which case you must have CAP_NET_RAW in order to capture and CAP_NET_ADMIN to enumerate network devices with, for example, the -D flag).

Under ULTRIX and Digital UNIX/Tru64 UNIX:
Any user may capture network traffic. However, no user (not even the super-user) can capture in promiscuous mode
on an interface unless the super-user has enabled promiscuous-mode operation on that interface using pfconfig(8),
and no user (not even the super-user) can capture unicast traffic received by or sent by the machine on an inter‐
face unless the super-user has enabled copy-all-mode operation on that interface using pfconfig, so useful packet
capture on an interface probably requires that either promiscuous-mode or copy-all-mode operation, or both modes
of operation, be enabled on that interface.

Under BSD (this includes Mac OS X):
You must have read access to /dev/bpf* on systems that don't have a cloning BPF device, or to /dev/bpf on systems
that do. On BSDs with a devfs (this includes Mac OS X), this might involve more than just having somebody with
super-user access setting the ownership or permissions on the BPF devices - it might involve configuring devfs to
set the ownership or permissions every time the system is booted, if the system even supports that; if it doesn't
support that, you might have to find some other way to make that happen at boot time.

读取存好的数据包文件不需要特殊权限。

调用pcap_dump_open()打开“savefile”以写入数据包。它返回一个pcap_dumper_t指针,它是用于向“savefile”写数据包的句柄。

通过pcap_dispatch()或pcap_loop()以读取数据包,它们处理一个或多个数据包,为每个数据包调用回调例程,或者用pcap_next()或pcap_next_ex(),它们返回下一个数据包。pcap_dispatch()和pcap_loop()的回调参数是一个struct pcap_pkthdr指针,它包含以下成员:

  ts    一个timeval结构,包含数据包捕获的时间戳

  caplen  一个bpf_u_int32,给出数据包被捕获下来的可用长度

  len    一个bpf_u_int32,给出数据包的原始长度,以字节为单位(这可能比caplen要长一些,如果数据包的长度要大于捕捉到的最大长度)

pcap_next_ex()通过一个指针参数传递此指针,pcap_next()传递一个指向struct pcap_pkthdr结构的指针,填写该结构。

回掉还提供一个const u_char指针指向前caplen(由传递给回调例程的struct pcap_pkthdr指针给出)字节数据包数据。这并不一定是整个数据包。为了抓取整个数据包,你需要在调用pcap_open_live()时提供一个snaplen的值,此值足够大到获取数据包的所有数据(65535对于绝大多数的网络来说足够了)。当从“savefile”读时,抓包时指定的快照长度会限制数据包可用数据的数量。pcap_next()返回该指针,pcap_next_ex()通过参数提供该指针。

BACKWARDS COMPATIBILITY
libpcap v1.0之前,绝大多数平台的pcap.h并不在pcap目录中,如果你的应用必须工作在libpcap1.0之前的版本,包含<pcap.h>,它会为你包含<pcap/pcap.h>。

pcap_create()和pcap_activate()在1.0之前的版本是不可用的,如果你写的应用必须工作在1.0之前的版本,要么用pcap_open_live()获取句柄,或者希望用pcap_create()和pcap_activate()提供的额外功能,使用autoconf(1)脚本或一些其他的配置脚本检查libpcap 1.0的API是否可用,并在可用时使用它们。

SEE ALSO
autoconf(1), tcpdump(8), tcpslice(1), pcap-filter(7), pfconfig(8), usermod(1M)

AUTHORS
The original authors of libpcap are:

Van Jacobson, Craig Leres and Steven McCanne, all of the Lawrence Berkeley National Laboratory, University of California,
Berkeley, CA.

The current version is available from "The Tcpdump Group"'s Web site at

http://www.tcpdump.org/

BUGS
Please send problems, bugs, questions, desirable enhancements, etc. to:

tcpdump-workers@lists.tcpdump.org

 

posted on 2011-12-24 22:25  huyc  阅读(5510)  评论(0编辑  收藏  举报