当前位置:软件学习 > 其它软件 >>

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

  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(pc

补充:软件开发 , 其他 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,