Libpcap使用总结

类别:编程语言 点击:0 评论:0 推荐:

Libpcap使用总结
1、打开、读取设备,设置过滤器部分。
1.1 pcap_read()
1.2 pcap_t * pcap_open_live ( char * device, int snaplen, int promisc,
                          int to_ms, char * errbuf );

该函数用于获取一个抽象的包捕捉句柄,后续很多libpcap函数将使用该句柄,类似
文件操作函数频繁使用文件句柄。device指定网络接口设备名,比如"eth0。snaplen
指定单包最大捕捉字节数,为了保证包捕捉不至于太低效率,snaplen尽量适中,以
恰能获得所需协议层数据为准。promisc指定网络接口是否进入混杂模式,注意即使
该参数为false(0),网络接口仍然有可能因为其他原因处在混杂模式。to_ms指定毫
秒级读超时,man手册上并没有指明什么值意味着永不超时,测试下来的结论,0可能
代表永不超时。如果调用失败返回NULL,errbuf包含失败原因。                         
/usr/include/pcap.h
typedef struct pcap pcap_t;
pcap-int.h里定义了struct pcap {}
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;
    int                bufsize;  /* Read buffer                                       */
    u_char *           buffer;
    u_char *           bp;
    int                cc;
    u_char *           pkt;      /* Place holder for pcap_next()                      */
    struct bpf_program fcode;    /* Placeholder for filter code if bpf not in kernel. */
    char               errbuf[PCAP_ERRBUF_SIZE];
};
                         
1.3 int pcap_setfilter ( pcap_t * p, struct bpf_program * fp );
该函数用于设置pcap_compile()解析完毕的过滤规则
2、编译、优化、调试过滤规则表达式部分
2.1 int pcap_compile ( pcap_t * p, struct bpf_program * fp, char * str,
                   int optimize, bpf_u_int32 netmask );
该函数用于解析过滤规则串,填写bpf_program结构。str指向过滤规则串,格式参看
tcpdump的man手册,比如:
tcpdump -x -vv -n -t ip proto \\tcp and dst 192.168.8.90 and tcp[13] \& 2 = 2
这条过滤规则将捕捉所有携带SYN标志的到192.168.8.90的TCP报文。过滤规则串可以
是空串(""),表示抓取所有过路的报文。
optimize为1表示对过滤规则进行优化处理。netmask指定子网掩码,一般从
pcap_lookupnet()调用中获取。返回值小于零表示调用失败。
这个函数可能比较难于理解,涉及的概念源自BPF,Linux系统没有这种概念,但是
libpcap采用pcap_compile()和pcap_setfilter()结合的办法屏蔽了各种链路层支持
的不同,无论是SOCK_PACKET、DLPI。                  
3、脱机方式监听部分。
3.1 pcap_open_offline()
3.2 pcap_offline_read ()

4、本地网络设置检测部分。
4.1 char * pcap_lookupdev ( char * errbuf );
该函数返回一个网络设备接口名,类似libnet_select_device(),对于Linux就是
"eth0"一类的名字.失败时返回NULL,errbuf包含了失败原因。errbuf一般定义如下:
/usr/include/pcap.h
#define PCAP_ERRBUF_SIZE 256
char errbuf[ PCAP_ERRBUF_SIZE ];

4.2 int pcap_lookupnet ( char * device, bpf_u_int32 * netp,
                     bpf_u_int32 * maskp, char * errbuf );
该函数用于获取指定网络接口的IP地址、子网掩码。不要被netp的名字所迷惑,它对
应的就是IP地址,maskp对应子网掩码。
/usr/include/pcap.h
typedef u_int bpf_u_int32;
显然简单理解成32-bit即可。如果调用失败则返回-1,errbuf包含失败原因。                    
                    
5、主控程序及版本部分。
5.1 pcap_next()
6、
6.1 int pcap_dispatch ( pcap_t * p, int cnt, pcap_handler callback, u_char * user );

该函数用于捕捉报文、分发报文到预先指定好的处理函数(回调函数)。
pcap_dispatch()接收够cnt个报文便返回,如果cnt为-1意味着所有报文集中在一个
缓冲区中。如果cnt为0,仅当发生错误、读取到EOF或者读超时到了(pcap_open_live
中指定)才停止捕捉报文并返回。callback指定如下类型的回调函数,用于处理
pcap_dispatch()所捕获的报文:

typedef void ( *pcap_handler ) ( u_char *, const struct pcap_pkthdr *, const u_char * );

pcap_dispatch()返回捕捉到的报文个数,如果在读取静态文件(以前包捕捉过程中存
储下来的)时碰到EOF则返回0。返回-1表示发生错误,此时可以用pcap_perror()、
pcap_geterr()显示错误信息。

下面来看看那个回调函数,总共有三个参数,第一个形参来自pcap_dispatch()的第
三个形参,一般我们自己的包捕捉程序不需要提供它,总是为NULL。第二个形参指向
pcap_pkthdr结构,该结构位于真正的物理帧前面,用于消除不同链路层支持的差异。
最后的形参指向所捕获报文的物理帧。

--------------------------------------------------------------------------
/usr/include/pcap.h

/*
* Each packet in the dump file is prepended with this generic header.
* This gets around the problem of different headers for different
* packet interfaces.
*/
struct pcap_pkthdr
{
    struct timeval ts;      /* time stamp                    */
    bpf_u_int32    caplen;  /* length of portion present     */
    bpf_u_int32    len;     /* length this packet (off wire) */
};

/usr/include/net/bpf.h

/*
* Structure prepended to each packet.
*/
struct bpf_hdr
{
    struct timeval bh_tstamp;   /* time stamp                 */
    bpf_u_int32    bh_caplen;   /* length of captured portion */
    bpf_u_int32    bh_datalen;  /* original length of packet  */
    u_short        bh_hdrlen;   /* length of bpf header (this struct
                                   plus alignment padding)    */
};
/*
* Because the structure above is not a multiple of 4 bytes, some compilers
* will insist on inserting padding; hence, sizeof(struct bpf_hdr) won't work.
* Only the kernel needs to know about it; applications use bh_hdrlen.
*/
#ifdef KERNEL
#define SIZEOF_BPF_HDR 18
#endif

6.2 void pcap_close ( pcap_t * p );
该函数用于关闭pcap_open_live()获取的包捕捉句柄,释放相关资源。
void pcap_perror ( pcap_t * p, char * prefix );
第一形参来自pcap_open_live(),第二行参的作用类似perror()的形参,指定错误信
息的前缀,与perror()一样,结尾自动输出一个换行。
pcap_perror( p, "pcap_compile" )的输出类似这个效果:
pcap_compile: unknown ip proto ...
pcap_perror并不自动exit(),与perror()一样,如果需要,应该显式调用exit()。

本文地址:http://com.8s8s.com/it/it22256.htm