宿州市监察局领导班子:libpcap库函数

来源:百度文库 编辑:偶看新闻 时间:2024/04/29 16:44:47

libpcap库函数介绍(附源代码)


libpcap的英文意思是 Packet Capture library,即数据包捕获函数库。该库提供的C函数接口可用于需要捕获经过网络接口(只要经过该接口,目标地址不一定为本机)数据包的系统开发上。由 Berkeley大学Lawrence Berkeley National Laboratory研究院的Van Jacobson、Craig Leres和Steven McCanne编写。该函数库支持Linux、Solaris和*BSD系统平台。

  主要接口函数说明如下:

pcap_t *pcap_open_live(char *device, int snaplen,
int promisc, int to_ms, char *ebuf)

获得用于捕获网络数据包的数据包捕获描述字。device参数为指定打开
的网络设备名。snaplen参数定义捕获数据的最大字节数。promisc指定
是否将网络接口置于混杂模式。to_ms参数指定超时时间(毫秒)。
ebuf参数则仅在pcap_open_live()函数出错返回NULL时用于传递错误消
息。

pcap_t *pcap_open_offline(char *fname, char *ebuf)

打开以前保存捕获数据包的文件,用于读取。fname参数指定打开的文
件名。该文件中的数据格式与tcpdump和tcpslice兼容。"-"为标准输
入。ebuf参数则仅在pcap_open_offline()函数出错返回NULL时用于传
递错误消息。

pcap_dumper_t *pcap_dump_open(pcap_t *p, char *fname)

打开用于保存捕获数据包的文件,用于写入。fname参数为"-"时表示
标准输出。出错时返回NULL。p参数为调用pcap_open_offline()或
pcap_open_live()函数后返回的pcap结构指针。fname参数指定打开
的文件名。如果返回NULL,则可调用pcap_geterr()函数获取错误消
息。

char *pcap_lookupdev(char *errbuf)

用于返回可被pcap_open_live()或pcap_lookupnet()函数调用的网络
设备名指针。如果函数出错,则返回NULL,同时errbuf中存放相关的
错误消息。

int pcap_lookupnet(char *device, bpf_u_int32 *netp,
bpf_u_int32 *maskp, char *errbuf)

获得指定网络设备的网络号和掩码。netp参数和maskp参数都是
bpf_u_int32指针。如果函数出错,则返回-1,同时errbuf中存放相
关的错误消息。

int pcap_dispatch(pcap_t *p, int cnt,
pcap_handler callback, u_char *user)

捕获并处理数据包。cnt参数指定函数返回前所处理数据包的最大值。
cnt=-1表示在一个缓冲区中处理所有的数据包。cnt=0表示处理所有
数据包,直到产生以下错误之一:读取到EOF;超时读取。callback
参数指定一个带有三个参数的回调函数,这三个参数为:一个从
pcap_dispatch()函数传递过来的u_char指针,一个pcap_pkthdr结构
的指针,和一个数据包大小的u_char指针。如果成功则返回读取到的
字节数。读取到EOF时则返回零值。出错时则返回-1,此时可调用
pcap_perror()或pcap_geterr()函数获取错误消息。

int pcap_loop(pcap_t *p, int cnt,
pcap_handler callback, u_char *user)

功能基本与pcap_dispatch()函数相同,只不过此函数在cnt个数据包
被处理或出现错误时才返回,但读取超时不会返回。而如果为
pcap_open_live()函数指定了一个非零值的超时设置,然后调用
pcap_dispatch()函数,则当超时发生时pcap_dispatch()函数会返回。
cnt参数为负值时pcap_loop()函数将始终循环运行,除非出现错误。

void pcap_dump(u_char *user, struct pcap_pkthdr *h,
u_char *sp)

向调用pcap_dump_open()函数打开的文件输出一个数据包。该函数可
作为pcap_dispatch()函数的回调函数。

int pcap_compile(pcap_t *p, struct bpf_program *fp,
char *str, int optimize, bpf_u_int32 netmask)

将str参数指定的字符串编译到过滤程序中。fp是一个bpf_program结
构的指针,在pcap_compile()函数中被赋值。optimize参数控制结果
代码的优化。netmask参数指定本地网络的网络掩码。

int pcap_setfilter(pcap_t *p, struct bpf_program *fp)

指定一个过滤程序。fp参数是bpf_program结构指针,通常取自
pcap_compile()函数调用。出错时返回-1;成功时返回0。

u_char *pcap_next(pcap_t *p, struct pcap_pkthdr *h)

返回指向下一个数据包的u_char指针。

int pcap_datalink(pcap_t *p)

返回数据链路层类型,例如DLT_EN10MB。

int pcap_snapshot(pcap_t *p)

返回pcap_open_live被调用后的snapshot参数值。

int pcap_is_swapped(pcap_t *p)

返回当前系统主机字节与被打开文件的字节顺序是否不同。

int pcap_major_version(pcap_t *p)

返回写入被打开文件所使用的pcap函数的主版本号。

int pcap_minor_version(pcap_t *p)

返回写入被打开文件所使用的pcap函数的辅版本号。

int pcap_stats(pcap_t *p, struct pcap_stat *ps)

向pcap_stat结构赋值。成功时返回0。这些数值包括了从开始
捕获数据以来至今共捕获到的数据包统计。如果出错或不支持
数据包统计,则返回-1,且可调用pcap_perror()或
pcap_geterr()函数来获取错误消息。

FILE *pcap_file(pcap_t *p)

返回被打开文件的文件名。

int pcap_fileno(pcap_t *p)

返回被打开文件的文件描述字号码。

void pcap_perror(pcap_t *p, char *prefix)

在标准输出设备上显示最后一个pcap库错误消息。以prefix参
数指定的字符串为消息头。

char *pcap_geterr(pcap_t *p)

返回最后一个pcap库错误消息。

char *pcap_strerror(int error)

如果strerror()函数不可用,则可调用pcap_strerror函数替代。

void pcap_close(pcap_t *p)

关闭p参数相应的文件,并释放资源。

void pcap_dump_close(pcap_dumper_t *p)

关闭相应的被打开文件。

ling:
libpcap的数据类型定义:

struct pcap_addr:网卡地址描述
{
pcap_addr * next;
sockaddr * addr;
sockaddr * netmask;
sockaddr *broadaddr;
sockaddr *dstaddr;
};
pcap_addr * next;
如果非空,指向链表中一个元素的指针;空表示链表中的最后一个元素。
sockaddr * addr;
指向包含一个地址的sockaddr的结构的指针。
sockaddr * netmask;
如果非空,指向包含相对于addr指向的地址的一个网络掩码的结构。
sockaddr * broadaddr;
如果非空,指向包含相对于addr指向的地址的一个广播地址,如果网络不支持广播可能为空。
sockaddr * dstaddr;
如果非空,指向一个相对于addr指向的源地址的目的地址,如果网络不支持点对点通讯,则为空。

struct pcap_file_header {
bpf_u_int32 magic;
u_short version_major;
u_short version_minor;
bpf_int32 thiszone; /* gmt to local correction */
bpf_u_int32 sigfigs; /* accuracy of timestamps */
bpf_u_int32 snaplen; /* max length saved portion of each pkt */
bpf_u_int32 linktype; /* data link type (LINKTYPE_*) */
};

bpf_u_int32 magic;

u_short version_major;
Libpcap的主版本号。
u_shart version_minor;
Libpcap的从版本号。
bpf_u_int32 sigfigs;
时间戳描述。
bpf_u_int32 snaplen;
保存的每个pkt的分片号的最大值。
bpf_u_int32 linktype;
数据链的类型。
细节说明:
libpcap dump文件头;
libpcap dump文件中的第一个记录包含了一些标志的保存值,这些标志在打印阶段用到。这儿的很多域都是32位的int,所以compilers不用进行转化;这些文件需要具有跨层次的可交换性。
无论如何不要改变结构的层次(包括仅仅改变这个结构中域的长度);


struct pcap_if { /*网卡数据链的一个元素*/
struct pcap_if *next;
char *name; /* name to hand to "pcap_open_live()" */
char *description; /* textual description of interface, or NULL */
struct pcap_addr *addresses;
u_int flags; /* PCAP_IF_ interface flags */
};

pcap_if *next;
如果非空,指向链的下一个元素。如果为空是链的最后一个元素。
char * name;
指向一个字符串,该字符串是传给pcap_open_live()函数的设备名;
char * description;
如果非空,指向一个对设备的人性化的描述字符串。
pcap_addr *addresses;
指向网卡地址链中的第一个元素。
u_int flags;
PCAP_IF_ 网卡的标志。现在唯一可用的标识是PCAP_IF_LOOKBACK,它被用来标识网卡是不是lookback网卡。

struct pcap_pkthdr { /*dump 文件中的数据包头*/
struct timeval ts; /* time stamp */
bpf_u_int32 caplen; /* length of portion present */
bpf_u_int32 len; /* length this packet (off wire) */
};
timeval ts;
数据报时间戳;
bpf_u_int32 caplen;
当前分片的长度;
dpf_u_int32 len;
这个数据报的长度;
细节描述:
在dump文件中的每个数据报都有这样一个报头。它用来处理不同数据报网卡的不同报头问题。

struct pcap_stat { /*用来保存网卡静态变量的结构*/
u_int ps_recv; /* number of packets received */
u_int ps_drop; /* number of packets dropped */
u_int ps_ifdrop; /* drops by interface XXX not yet supported */
};
u_int ps_recv;
接受数据报的数目;
u_int ps_drop;
被驱动程序丢弃的数据报的数目;
u_int ps_ifdrop;
被网卡丢弃的数据报的数目;


struct pcap_sf { //pacap的savefile结构 定义
FILE *rfile; //该指针指向savefile文件
int swapped; //?
int hdrsize; //头大小吗?
int version_major;//主版本号
int version_minor;//从版本号
u_char *base;//?
};
struct pcap_md { //?
struct pcap_stat stat;
/*XXX*/
int use_bpf; /* using kernel filter */
u_long TotPkts; /* can't oflow for 79 hrs on ether */
u_long TotAccepted; /* count accepted by filter */
u_long TotDrops; /* count of dropped packets */
long TotMissed; /* missed by i/f during this run */
long OrigMissed; /* missed by i/f before this run */
#ifdef linux
int sock_packet; /* using Linux 2.0 compatible interface */
int timeout; /* timeout specified to pcap_open_live */
int clear_promisc; /* must clear promiscuous mode when we close */
int cooked; /* using SOCK_DGRAM rather than SOCK_RAW */
int lo_ifindex; /* interface index of the loopback device */
char *device; /* device name */
struct pcap *next; /* list of open promiscuous sock_packet pcaps */
#endif
};

struct pcap { //这个结构很重要
int fd;
int snapshot;
int linktype;
int tzoff; /* timezone offset */
int offset; /* offset for proper alignment */

struct pcap_sf sf;
struct pcap_md md;

/*
* Read buffer.
*/
int bufsize;
u_char *buffer;
u_char *bp;
int cc;

/*
* Place holder for pcap_next().
*/
u_char *pkt;


/*
* Placeholder for filter code if bpf not in kernel.
*/
struct bpf_program fcode;

char errbuf[PCAP_ERRBUF_SIZE];
};

lipcap的声明:
#define PCAP_VERSION_MAJOR 2
libpcap dump文件的主版本号;
#define PCAP_VERSION_MINOR 4
libpcap dump文件的从版本号;
#define PCAP_ERRBUF_SIZE 256
用来存放libpcap出错信息的缓冲区的大小;
#define PCAP_IF_LOOPBACK 0x00000001
网卡是回环网卡;
#define MODE_CAPT 0
抓报模式,在调用pcap_setmode()时使用;
#define MODE_STAT 1
静态模式,在调用pcap_setmode()时使用;

libpcap的类型定义:
typedef int bpf_int32
32bit 的整形;
typedef u_int bpf_u_int32
32bit 的无类型整形;
typedef pcap pcap_t
Descriptor of an open capture instance(一个打开的捕获实例的描述符?)这个结构对用户是不透明的。
typedef pcap_dumper pcap_dumper_t
libpcap保存文件的描述符。
typedef pcap_if pcap_if_t
网卡链表的一个元素;
typedef pcap_addr pcap_addr_t
网卡地址的表示;

libpcap函数描述:


char *pcap_lookupdev(char * errbuf);
描述: 这个函数用于获取一个合适的网卡描述,以供pcap_open_liver函数和pcap_lookupnet函数使用。如果找不到网卡或者所有网卡为 off,则返回null。如果一个系统中有多个网卡,那么该函数返回找到的第一个on的网卡。最后才是回环接口。回环网卡一直被忽略;
参数:
char * errbuf 存放pcap_lookupdev函数的出错信息,只有在pcap_lookup失败是才有值。
返回值: 如果函数执行成功,则返回一个用于描述系统上的一个网卡的描述符的指针。如果失败,返回null,errbuf中存放出错信息。


int pcap_lookupnet(char * device, bpf_u_int32 * netp, bpf_u_int32 * maskp,char * errbuf);
描述:该函数用于监测网卡所在网络的网络地址和子网掩码。
参数:
char *devic:网卡的描述符指针,由pcap_looupdev函数获取;
bpf_u_int32 *netp:存放网络地址;
bpf_u_int32 *maskp:存放子网掩码;
char * errbuf: 存放出错信息;
返回值:如果函数执行成功,则返回值为0,否则返回值为-1,并在errbuf中存放出错信息。


pcap_t *pcap_open_live(char * device, int snaplen,int promisc, int to_ms, char * ebuf);
描述:该函数用于打开网卡用于捕获数据报。单词live的意思就是表示一个运行的网卡(相对于offline而言)被打开了,如同一个保存有被抓数据报的 文件被打开一样。在捕获数据报之前这个函数必须被执行。所有的其他的用于处理数据报捕获的函数用到的捕获数据报的描述符由该函数产生。查看 pcap_open_offlin()函数的定义,了解如何打开一个预先保存的包含数据报的文件的细节。
参数:
char *device:网卡的描述符指针,由pcap_looupdev函数获取;
int snaplen:规定捕获的每个数据报的最大字节数;
int promisc:1为混杂模式;0为非混杂模式;
int to_ms:规定读超时的微秒(milliseconds)数;
char *ebuf:存放错误信息,只有在pcap_open_live失败时才被设置;
返回值:如果函数成功执行,则返回一个指向数据报捕获的指针;如果错误,返回null,ebuf存放出错信息;


int pcap_compile(pcap_t * p, struct bpf_ program *fp, char * str,int optimize, bpf_u_int32 netmask);
描述:该函数用于将str指定的规则整合到fp过滤程序中去,并生成过滤程序入口地址,用于过滤选择期望的数据报;
参数:
pcap_t *p:pcap_open_live返回的数据报捕获的指针;
struct bpf_program *fp:指向一个子函数用于过滤,在pcap_compile()函数中被赋值;
char *str:该字符串规定过滤规则;
int optimize:规定了在结果代码上的选择是否被执行;
bpf_u_int32 netmask:该网卡的子网掩码,可以通过pcap_lookupnet()获取;
返回值:
如果成功执行,返回0,否则返回-1;

int pcap_loop(pcap_t * p, int cnt, pcap_handler callback,u_char * user);
描述:
该函数用于读取和处理数据报。既可以用来处理事先捕获的保存在文件中的数据报,也可以用来处理实时捕获的数据报;
这个函数类似于pcap_dispatch函数,除了它继续读取数据报直至完成cnt个报的处理,或者文件处理完(在offline情况下),或者有错误 发生为止。它不会在实时读超时时返回(而如果为pcap_open_live()函数指定了一个非零值的超时设置,然后调用
pcap_dispatch()函数,则当超时发生时pcap_dispatch()函数会返回。)
注意第三个参数,callback是pcap_handler类型的变量。这是一个用户提供的有着三个参数的子函数。定义为:
void user_routine(u_char *user, struct pcap_pkthdr *phrd, u_char *pdata)
这三个参数中,user,是传递给pcap_dispatch()的那个参数;phdr,是个pcap_pkthdr类型的指针,是savefile中的数据报的头指针,pdata,指向数据报数据;这个函数允许用户定义子集的数据报过滤程序;
参数:
pcap_t * p:pcap_open_live返回的数据报捕获的指针;
int cnt:规定了函数返回前应处理的数据报数目;
pcap_handler callback:指向一个用户自定义的函数,在处理每个报后自动调用该函数进行再处理;
u_char *user:该指针用于传递给callback.(不知道有什么用?)
返回值:
如果函数成功执行(包括读文件时读到EOF),则返回0.否则返回-1,那么错误信息将由函数pcap_geterr或pcap_perror给出;
补充:callback函数:
The concept behind a callback function is fairly simple. Suppose I have a program that is waiting for an event of some sort. For the purpose of this example, lets pretend that my program wants a user to press a key on the keyboard. Every time they press a key, I want to call a function which then will determine that to do. The function I am utilizing is a callback function.
pcap_open_dead()
is used for creating a pcap_t structure to use when calling the other functions in libpcap. It is typically used when just using libpcap for compiling BPF code.
pcap_dump_open()
is called to open a ``savefile'' for writing. The name "-" in a synonym for stdout. NULL is returned on failure. p is a pcap struct as returned by pcap_open_offline() or pcap_open_live(). fname specifies the name of the file to open. If NULL is returned, pcap_geterr() can be used to get the error text.
pcap_setnonblock()
puts a capture descriptor, opened with pcap_open_live(), into ``non-blocking'' mode, or takes it out of ``non-blocking'' mode, depending on whether the nonblock argument is non-zero or zero. It has no effect on ``savefiles''. If there is an error, -1 is returned and errbuf is filled in with an appropriate error message; otherwise, 0 is returned. In ``non-blocking'' mode, an attempt to read from the capture descriptor with pcap_dispatch() will, if no packets are currently available to be read, return 0 immediately rather than blocking waiting for packets to arrive. pcap_loop() and pcap_next() will not work in ``non-blocking'' mode.
libpcap文件选读:
ethernet.c:定义了三个内联函数:
static inline int xdtoi(int):该函数将十六进值数转化为整数;
static inline int skip_space(FILE *):该函数定义了在一个文件中含有空格时,返回第一个不是'
'的字符。
static inline int skip_line(FILE *):
struct pcap_etherent {
u_char addr[6];
char name[122];
};
ethertype.h定义了各个协议的值。如#define ETHERTYPE_IP 0x0800 /* IP protocol */



#include
#include

#define CAP_LEN 2048
#define FILENAME "rh"
#define _DEBUG_


int main()
{
bpf_u_int32 localnet,netmask;
struct bpf_program fcode;
pcap_handler printer;


pcap_dumper_t*p;
char ebuf[PCAP_ERRBUF_SIZE];
char * device;
u_char * pcap_userdata;
pcap_t * pd;
int dev_flag=1;
int cap_len=CAP_LEN;
int i;


device=pcap_lookupdev(ebuf);
if (device == NULL)
exit(printf("%s n", ebuf));
#ifdef _DEBUG_
printf("device is %s /n",device);
#endif
pd=pcap_open_live(device,cap_len,dev_flag,1000,ebuf);
if(pd == NULL)
exit(printf("%s/n",ebuf));


i=pcap_snapshot(pd);
if(cap_len < i) {

printf("snaplen raised from %d to %d /n", cap_len, i);

cap_len=i;
}
if(pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0) {

localnet=0;

netmask=0;

printf("%s/n", ebuf);
}
if (pcap_compile(pd, &fcode, "", 1, netmask) < 0)

exit(printf("Error %s/n","pcap_compile"));
if (pcap_setfilter(pd,&fcode) < 0)

exit(printf("Error %s/n","pcap_setfilter"));


p=pcap_dump_open(pd,FILENAME);
if(p == NULL)

exit(printf("Error:%s/n","pcap_dump_open"));
printer=pcap_dump;
pcap_userdata=(u_char *)p;


if(pcap_loop(pd, -1, printer, pcap_userdata) < 0)

exit(printf("Error, %s/n","pcap_loop"));


pcap_close(pd);
exit(0);
}