WinPcap编程常用函数和数据结构

来源:互联网 发布:mac地址怎么查苹果手机 编辑:程序博客网 时间:2024/05/21 02:19

  WinPcap编程实质上就是对各种函数的熟悉和调用,因此本节对前面稍微做一下小结,对所用到的函数及数据类型进行归纳和总结,一是为了回顾所掌握的知识,二是加深印象,便于后面更好地学习。

常用函数和结构体

1.  pcap_if_t结构体,表示适配器列表中的一项

复制代码
/*
* Item in a list of interfaces.
*/
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;
bpf_u_int32 flags; /* PCAP_IF_ interface flags */
};
typedef struct pcap_if pcap_if_t;
复制代码


2.  pcap_findalldevs_ex() 获取适配器列表,返回0表示正常,-1表示出错

int pcap_findalldevs_ex(char *source, struct pcap_rmtauth *auth, pcap_if_t **alldevs, char *errbuf);


3. pcap_freealldevs() 释放适配器链表空间

void    pcap_freealldevs(pcap_if_t *);


4.  pcap_addr_t结构体,接口地址的表示形式

复制代码
/*
* Representation of an interface address.
*/
struct pcap_addr {
struct pcap_addr *next;
struct sockaddr *addr; /* address */
struct sockaddr *netmask; /* netmask for that address */
struct sockaddr *broadaddr; /* broadcast address for that address */
struct sockaddr *dstaddr; /* P2P destination address for that address */
};
typedef struct pcap_addr pcap_addr_t;
复制代码


5.  sockaddr_in结构体,接口地址的表示形式

复制代码
struct sockaddr_in {
short sin_family; //协议族
u_short sin_port; //协议端口
struct in_addr sin_addr; //
char sin_zero[8];
};
struct sockaddr {
u_short sa_family;
char sa_data[14];

}; //通用的socket地址

struct in_addr {
union {
struct { u_char s_b1,s_b2,s_b3,s_b4; } S_un_b;
struct { u_short s_w1,s_w2; } S_un_w;
u_long S_addr;
} S_un;

          #define s_addr S_un.S_addr
          #define s_host S_un.S_un_b.s_b2
          #define s_net S_un.S_un_b.s_b1
          #define s_imp S_un.S_un_w.s_w2
          #define s_impno S_un.S_un_b.s_b4
          #define s_lh S_un.S_un_b.s_b3

        };  
复制代码

      如上所示,上面定义了三种结构体:sockaddr_in、sockaddr和in_addr。简单说一下这三个结构体,sockaddr是通用的socket地址,而sockaddr_in是具体到internet的socket地址,两者之间可以进行类型转换。而in_addr结构体就是32位IP地址。因为这些其实都是socket编程中的知识,所以只是稍微提一下,感兴趣的可以细心研究一下。


6.  pcap_open() 打开适配器

pcap_t *pcap_open(const char *source, int snaplen, int flags, int read_timeout, struct pcap_rmtauth *auth, char *errbuf);


7.  pcap_loop() 捕获数据包,其中pcap_handler为回调函数指针

int pcap_loop  ( pcap_t *  p,  
int cnt,
pcap_handler callback,
u_char * user
)
typedef void (*pcap_handler)(u_char *, const struct pcap_pkthdr *,
const u_char *);


8.  pcap_next_ex() 直接获得一个数据包,非回调方法; pcap_pkthdr结构体表示dump文件中数据包首部

int pcap_next_ex  ( pcap_t *  p,  
struct pcap_pkthdr ** pkt_header,
const u_char ** pkt_data
)
复制代码
/*
* Generic per-packet information, as supplied by libpcap.
*
* The time stamp can and should be a "struct timeval", regardless of
* whether your system supports 32-bit tv_sec in "struct timeval",
* 64-bit tv_sec in "struct timeval", or both if it supports both 32-bit
* and 64-bit applications. The on-disk format of savefiles uses 32-bit
* tv_sec (and tv_usec); this structure is irrelevant to that. 32-bit
* and 64-bit versions of libpcap, even if they're on the same platform,
* should supply the appropriate version of "struct timeval", even if
* that's not what the underlying packet capture mechanism supplies.
*/
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) */
};

  struct timeval {
      long tv_sec;
      long tv_usec;
  };

复制代码


9.  pcap_compile() 编译数据包过滤器,将程序中高级的过滤表达式,转换成能被内核级的过滤引擎所处理的东西。

     对于bpf_program结构体我们只需要知道它是pcap_compile()最终要得到用来过滤的东西。

int    pcap_compile(pcap_t *, struct bpf_program *, const char *, int,
bpf_u_int32);
复制代码
/*
* Structure for "pcap_compile()", "pcap_setfilter()", etc..
*/
struct bpf_program {
u_int bf_len;
struct bpf_insn *bf_insns;
};
复制代码
复制代码
/*
* The instruction data structure.
*/
struct bpf_insn {
u_short code;
u_char jt;
u_char jf;
bpf_u_int32 k;
};
复制代码


10.  pcap_setfilter() 在捕获过程中绑定一个过滤器。

     至于pcap结构体,它是一个已打开的捕捉实例的描述符。这个结构体对用户来说是不透明的,它通过wpcap.dll提供的函数,维护了它的内容。

int    pcap_setfilter(pcap_t *, struct bpf_program *);
typedef struct pcap pcap_t;


11.  pcap_datalink() 返回适配器的链路层

int    pcap_datalink(pcap_t *);


12.  pcap_dump_open() 打开一个文件来写入数据包;而pcap_dumper结构体表示libpcap存储文件的描述符

pcap_dumper_t *pcap_dump_open(pcap_t *, const char *);
typedef struct pcap_dumper pcap_dumper_t;


13.  pcap_dump() 将数据包保存到磁盘 

void    pcap_dump(u_char *, const struct pcap_pkthdr *, const u_char *);


14.  pcap_createsrcstr() 接收一组字符串(hot name,port,...),并根据新的格式,返回一个完整的源字符串(比如:'rpcap://1.2.3.4/eth0') 

int pcap_createsrcstr(char *source, int type, const char *host, const char *port, const char *name, char *errbuf);


15.  pcap_live_dump() 将捕获保存到文件,它和pcap_dump()之间的区别在“处理脱机dump文件(下)”中有说明

int pcap_live_dump(pcap_t *p, char *filename, int maxsize, int maxpacks);


16.  pcap_sendpacket() 发送raw数据包

int pcap_sendpacket(pcap_t *, const u_char *, int);


17.  pcap_sendqueue_alloc() 创建发送队列

       pcap_sendqueue_queue() 向队列加入要发送的数据包

       pcap_sendqueue_transmit() 发送数据队列

       pcap_sendqueue_destroy() 释放数据队列

pcap_send_queue* pcap_sendqueue_alloc(u_int memsize);
int pcap_sendqueue_queue(pcap_send_queue* queue, const struct pcap_pkthdr *pkt_header, const u_char *pkt_data);
u_int pcap_sendqueue_transmit(pcap_t *p, pcap_send_queue* queue, int sync);
void pcap_sendqueue_destroy(pcap_send_queue* queue);


18.  pcap_send_queue结构体,发送数据报队列的数据结构

复制代码
/*!
\brief A queue of raw packets that will be sent to the network with pcap_sendqueue_transmit().
*/
struct pcap_send_queue
{
u_int maxlen; ///< Maximum size of the the queue, in bytes. This variable contains the size of the buffer field.
u_int len; ///< Current size of the queue, in bytes.
char *buffer; ///< Buffer containing the packets to be sent.
};

typedef struct pcap_send_queue pcap_send_queue;
复制代码


19. pcap_setmode() 设置适配器工作模式

int pcap_setmode(pcap_t *p, int mode);


常用字段含义

PCAP_ERRBUF_SIZE            libpcap错误信息缓冲的大小,值为256
PCAP_BUF_SIZE               最大缓冲大小,值为1024
PCAP_SRC_IF_STRING          rpcap源字符串格式,值为“rpcap://”
PCAP_IF_LOOPBACK            用来判断回环地址,值为0x00000001 
PCAP_OPENFLAG_PROMISCUOUS   混杂模式,值为1
DLT_EN10MB                  链路层为以太网             

作者:黑剑 
出处:http://www.cnblogs.com/blacksword/ 
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

0 0
原创粉丝点击