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(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/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
WinPcap编程常用函数和数据结构相关推荐
- 笔试编程常用函数(Java)
笔试编程常用函数(Java) 处理对象为String 数组相关 集合相关 (今天太晚了,以后再慢慢写) 处理对象为String 将String对象按某规则分割: String[] split(Stri ...
- VxWorks编程常用函数说明
VxWorks编程常用函数说明 一.官方的Program Guide 位于安装目录下:\docs\vxworks\guide\index.html 二.常用的库: #include "tas ...
- linux网络编程常用函数详解与实例(socket--bind--listen--accept)
常用的网络命令: netstat 命令netstat是用来显示网络的连接,路由表和接口统计等网络的信息.netstat有许多的选项我们常用的选项是 -an 用来显示详细的网络状态.至于其它的选项我们可 ...
- 基于树莓派的python GPIO编程-常用函数综合整理
Hello,朋友们大家好,欢迎大家来到LIUSE网络. 随着Mini型PC越来越多,与之而来的创客也丰富起来,比如说Arduino就是一个非常好里例子. 不过,Arduino毕竟是一块适合入门型的基础 ...
- Linux socket 网络编程常用函数总结
1.字节序函数 #include <netinet.h> uint16_t htons(uint16_t host16bitvalue); uint32_t htonl(uint32_t ...
- Linux文件编程常用函数详解——lseek()函数
lseek()函数的头文件和形式: #include <sys/types.h> #include <unistd.h> off_t lseek(int filedes, of ...
- WinPcap编程入门实践
转自:http://www.cnblogs.com/blacksword/archive/2012/03/19/2406098.html WinPcap可能对大多数人都很陌生,我在这里就先简单介绍一下 ...
- socket网络编程常用C语言api函数(Linux)
参考<Linux高性能服务器编程> 网络编程常用函数 字节序 ip转换 地址结构体 socket相关函数 数据读写 获取地址信息 获取或者设置套接字属性 通过域名或地址获取主机信息 字节序 ...
- Python机器视觉编程常用数据结构与示例
Python机器视觉编程常用数据结构与示例 本文总结了使用Python进行机器视觉(图像处理)编程时常用的数据结构,主要包括以下内容: 数据结构 通用序列操作:索引(indexing).分片(slic ...
最新文章
- 2W+好评,这个python数据分析课程免费开放3天!
- 深度学习核心技术精讲100篇(八)-keras 实战系列之深度学习中的多任务学习(Multi-task learning)
- ElementUI中el-table-column的type为selection时选择框旁边有个点
- TPYBoard:一款可以发挥无限创意的MicroPython开发板
- 技术案例分享:WIPTEC采用Aruba边缘服务平台,实现物流配送生产自动化、精简生产力
- RedisConf2018记录--Day 1 sessions
- 从VSS到SVN再到Git 记Git的基本操作
- mysql单实例安装
- 编写python代码估算sin(x)的值
- android配置多个url,Retrofit中使用多个baseUrl
- 多线程——java练习题
- 83 项开源视觉 SLAM 方案够你用了吗?
- 微信小程序Java登录流程(ssm实现具体功能和加解密隐私信息问题解决方案)...
- windows7电脑备份系统教程
- 鼠标式光流传感器与多传感器融合
- html5怎么设置路由器,无线路由器
- 【JavaMap接口】HashMap源码解读实例
- 迅为RK3399开发板Linux系统TFTP传输文件服务器测试
- 查询是否有公网IP的方法
- cu3er 3D幻灯切换效果 div被遮住的解决方法