打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
winpcap中文资料(不全)
 
(很全的文章)
 
1. int pcap_findalldevs(pcap_if_t **, char *)
                   说明:用来获得网卡的列表
                  入口参数:
                  指向pcap_if_t**类型的列表的指针的指针
pcap_if_t 是pcap_if 重命名而来:typedef struct pcap_if pcap_if_t;
pcap_if结构体如下:
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;
/*以下是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 */
};*/
bpf_u_int32 flags; /* PCAP_IF_ interface flags */
};
char型指针
当打开列表错误时返回错误信息
出口参数:
为int型,当显示列表失败时返回-1
举例:
pcap_if_t *alldevs;
pcap_if_t *d;
char errbuf[64];
/* 这个API用来获得网卡 的列表 */
if (pcap_findalldevs(&alldevs, errbuf) == -1)
{
fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
exit(1);
}
/* 显示列表的响应字段的内容 */
for(d=alldevs;d;d=d->next)
{
printf("%d. %s", ++i, d->name);
if (d->description)
printf(" (%s)\n", d->description);
else
printf(" (No description available)\n");
}
2. void pcap_freealldevs(pcap_if_t *)
说明:与int pcap_findalldevs(pcap_if_t **, char *)配套使用,当不再需要网卡列表时,用此函数free释放空间
入口参数:打开网卡列表时申请的pcap_if_t型的指针
出口参数:无
举例:
/* We don't need any more the device list. Free it */
pcap_freealldevs(alldevs);
3. pcap_t *pcap_open_live(const char * device, int snaplen, int promisc, int to_ms, char ebuf *)
说明:被用来得到一个包抓取得描述符
入口参数:
device是一个指出要抓取的网络设备的字符串。
snaplen指明最大可抓取的字节长度。
promisc置位表明该接口要被设置成混杂模式。
to_ms以毫秒为单位设置超时时间。当在超时时间内网卡上没有数据到来时对网卡的读操作将返回(如pcap_dispatch() or pcap_next_ex()等函数)。
ebuf被用来存放当pcap_open_live()调用失败时,返回的错误字符串。
出口参数:
pcap_t型的指针,供pcap_dispatch() or pcap_next_ex()等函数调用。
pcap_t的结构体:
struct pcap {
#ifdef WIN32
ADAPTER *adapter;
LPPACKET Packet;
int timeout;
int nonblock;
#else
int fd;
#endif /* WIN32 */
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 + 1];
int dlt_count;
int *dlt_list;
#ifdef REMOTE
/*! \brief '1' if we're the network client; needed by several functions (like pcap_setfilter() ) to know if
they have to use the socket or they have to open the local adapter. */
int rmt_clientside;
SOCKET rmt_sockctrl; //!< socket ID of the socket used for the control connection
SOCKET rmt_sockdata; //!< socket ID of the socket used for the data connection
pthread_t rmt_threaddata; //!< handle to the receiving thread, we need to kill it in case of 'pcap_close()'
int rmt_flags; //!< we have to save flags, since they are passed by the pcap_open_live(), but they are used by the pcap_startcapture()
int rmt_capstarted; //!< 'true' if the capture is already started (needed to knoe if we have to call the pcap_startcapture()
struct pcap_pkthdr pcap_header; //!< In Linux, you have to copy the packet headers another time before giving them to the user
#endif /* REMOTE */
};
举例:
/* Open the adapter */
if ( (adhandle= pcap_open_live(d->name, // name of the device
65536, // portion of the packet to capture.
// 65536 grants that the whole packet will be captured on all the MACs.
1, // promiscuous mode
1000, // read timeout
errbuf // error buffer
) ) == NULL)
{
fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n");
/* Free the device list */
pcap_freealldevs(alldevs);
return -1;
}






4. int pcap_loop(pcap_t *p, int cnt, pcap_handler callback, u_char *user);
说明:捕获数据包;不会响应pcap_open_live()中设置的超时时间
入口参数:
p是由pcap_open_live()返回的所打开网卡的指针
cnt用于设置所捕获数据包的个数
packet_handler是与void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data)配合使用的一个参数。回调函数。
user值一般为NULL
出口参数:
举例:
pcap_loop(adhandle, 0, packet_handler, NULL);
//…
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data)
{
struct tm *ltime;
char timestr[16];

/* 将时间戳转变为易读的标准格式*/
ltime=localtime(&header->ts.tv_sec);
strftime( timestr, sizeof timestr, "%H:%M:%S", ltime);

printf("%s,%.6d len:%d\n", timestr, header->ts.tv_usec, header->len);

}
5. int pcap_dispatch(pcap_t * p, int cnt, pcap_handler, u_char *user)
说明:捕获数据包。可以不被阻塞???????????????????
入口参数:与pcap_loop()相同
出口参数:???????????????????
举例:NULL
几点说明:
一旦网卡被打开,旧可以调用pcap_dispatch() 或pcap_loop()进行数据的捕获,这两个函数的功能十分相似,不同的是pcap_ dispatch()可以不被阻塞,而pcap_loop()在没有数据流到达时将阻塞。在这个简单的例子里用
pcap_loop()就足够了,而在一些复杂的程序里往往用pcap_dispatch()。这两个函数都有返回的参数,一个指向某个函数(该函数用来接受数据如该程序中的packet_handler)的指针
,libpcap调用该函数对每个从网上到来的数据包进行处理和接收数据包。另一个参数是带有时间戳和包长等信息的头部,最后一个是含有所有协议头部数据报的实际数据。注意MAC的冗余校验码一般不出现,因为当一个桢到达并被确认后网卡就把它删除了,同样需要注意的是大多数网卡会丢掉冗余码出错的数据包,所以WinPcap一般不能够捕获这些出错的数据报。
6. int pcap_next_ex(pcap_t *p, struct pcap_pkthdr **pkt_header, u_char **pkt_data);
说明:捕获数据包,与pcap_ dispatch() pcap_loop()很相似。pcap_next_ex()允许直接调用来接收包,它的参数和pcap_loop()相同:有一个网卡描述副,和两个指针,这两个指针会被初始化并返回给用户,一个是pcap_pkthdr结构,另一个是接收数据的缓冲区。
入口参数:
p是由pcap_open_live()返回的所打开网卡的指针
pcap_pkthdr型的结构体,存储时间,包的长度
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) */
};
pkt_data存储数据包的内容,为一个char型数组
出口参数:
当等于1时成功;等于0时超时;等于-1时说明发生错误,错误信息用pcap_geterr(adhandle)获得
举例:(最重要的一段代码)
#include
#include
#include
#define LINE_LEN 16
main(int argc, char **argv) {

pcap_if_t *alldevs, *d;
pcap_t *fp;
u_int inum, i=0;
char errbuf[PCAP_ERRBUF_SIZE];
int res;
struct pcap_pkthdr *header;
u_char *pkt_data;
printf("pktdump_ex: prints the packets of the network using WinPcap.\n");
printf("\t Usage: pktdump_ex [-n adapter] | [-f file_name]\n\n");
if(argc < 3){
/* The user didn't provide a packet source: Retrieve the device list */
if (pcap_findalldevs(&alldevs, errbuf) == -1)
{
fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
exit(1);
}

/* Print the list */
for(d=alldevs; d; d=d->next)
{
printf("%d. %s", ++i, d->name);
if (d->description)
printf(" (%s)\n", d->description);
else
printf(" (No description available)\n");
}

if(i==0)
{
printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
return -1;
}

printf("Enter the interface number (1-%d):",i);
scanf("%d", &inum);

if(inum < 1 || inum > i)
{
printf("\nInterface number out of range.\n");
/* Free the device list */
pcap_freealldevs(alldevs);
return -1;
}

/* Jump to the selected adapter */
for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);

/* Open the device */
if ( (fp= pcap_open_live(d->name, 100, 1, 20, errbuf) ) == NULL)
{
fprintf(stderr,"\nError opening adapter\n");
return -1;
}
}
else{

/* The user provided a packet source: open it */
switch (argv[1] [1])
{

case 'n':
{
/* Open a physical device */
if ( (fp= pcap_open_live(argv[2], 100, 1, 20, errbuf) ) == NULL)
{
fprintf(stderr,"\nError opening adapter\n");
return -1;
}
};
break;

case 'f':
{
/* Open a capture file */
if ( (fp = pcap_open_offline(argv[2], errbuf) ) == NULL)
{
fprintf(stderr,"\nError opening dump file\n");
return -1;
}
};
break;
}
}
/* Read the packets */
while((res = pcap_next_ex( fp, &header, &pkt_data)) >= 0){
if(res == 0)
/* Timeout elapsed */
continue;
/* print pkt timestamp and pkt len */
printf("%ld:%ld (%ld)\n", header->ts.tv_sec, header->ts.tv_usec, header->len);

/* Print the packet */
for (i=1; (i < header->caplen + 1 ) ; i++)
{
printf("%.2x ", pkt_data[i-1]);
if ( (i % LINE_LEN) == 0) printf("\n");
}

printf("\n\n");
}
if(res == -1){
printf("Error reading the packets: %s\n", pcap_geterr(fp));
return -1;
}
return 0;
}


7. int pcap_compile(pcap_t *p, struct bpf_program *fp, char *str, int optimize,bpf_u_int32 netmask)
说明:编译一个过滤设备,它通过一个高层的boolean型变量和字串产生一系列的能够被底层驱动所解释的二进制编码。boolean表示语法能够在这个文件的过滤表示语法中找到。与pcap_setfilter()配合使用。
入口参数:
p是打开网卡时返回的网卡指针
fp用于与pcap_setfilter()传递过滤信息。
str是一个字符串。
optimize指明是否需要优化编译结果。
子网掩码
出口参数:
发生错误是返回-1
举例:
if(d->addresses != NULL)
/* 获得第一个接口地址的掩码 */
netmask=((struct sockaddr_in *)(d->addresses->netmask))->sin_addr.S_addr;
else
/* 如果这个接口没有地址那么我们假设他为C类地址 */
netmask=0xffffff;


//compile the filter
if(pcap_compile(adhandle, &fcode, "ip and tcp", 1, netmask) <0 ){
fprintf(stderr,"\nUnable to compile the packet filter. Check the syntax.\n");
/* Free the device list */
pcap_freealldevs(alldevs);
return -1;
}

//set the filter
if(pcap_setfilter(adhandle, &fcode)<0){
fprintf(stderr,"\nError setting the filter.\n");
/* Free the device list */
pcap_freealldevs(alldevs);
return -1;
}
8. int pcap_setfilter ( pcap_t *p, struct bpf_program * fp )
说明:pcap_setfilter() 用来联系一个在内核驱动上过滤的过滤器,这时所有网络数据包都将流经过滤器,并拷贝到应用程序中。
入口参数:
p是打开网卡时返回的网卡指针
fp是pcap_compile()传递过来的参数
出口参数:
错误时返回-1
举例:同上
9. int pcap_sendpacket(pcap_t *p, u_char *buf, int size)
说明:手工发送一个数据包了。这个函数需要的参数:一个装有要发送数据的缓冲区,要发送的长度,和一个适配器。注意缓冲区中的数据将不被内核协议处理,只是作为最原始的数据流被发送,所以我门必须填充好正确的协议头以便正确的将数据发送。
入口参数:
p是打开网卡时返回的网卡指针
buf是发送数据包的内容缓冲区首地址
size是发送数据包的大小
出口参数:
举例:
#include
#include
#include
void usage();
void main(int argc, char **argv) {
pcap_t *fp;
char error[PCAP_ERRBUF_SIZE];
u_char packet[100];
int i;
/* Check the validity of the command line */
if (argc != 2)
{
printf("usage: %s inerface", argv[0]);
return;
}
/* 打开指定网卡 */
if((fp = pcap_open_live(argv[1], 100, 1, 1000, error) ) == NULL)
{
fprintf(stderr,"\nError opening adapter: %s\n", error);
return;
}
/* 假设网络环境为ethernet,我门把目的MAC设为1:1:1:1:1:1*/
packet[0]=1;
packet[1]=1;
packet[2]=1;
packet[3]=1;
packet[4]=1;
packet[5]=1;

/* 假设源MAC为 2:2:2:2:2:2 */
packet[6]=2;
packet[7]=2;
packet[8]=2;
packet[9]=2;
packet[10]=2;
packet[11]=2;

/* 填充发送包的剩余部分 */
for(i=12;i<100;i++){
packet=i%256;
}
/* 发送包 */
pcap_sendpacket(fp, packet, 100); //发送原始的数据包
return;
}
10.发送数据包有关的几个函数
10.1 pcap_send_queue* pcap_sendqueue_alloc(u_int memsize);
说明:给数据包队列分配空间
入口参数:
memsize队列缓冲区的大小
出口参数:
pcap_send_queue指针
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.
};
举例:
squeue = pcap_sendqueue_alloc(caplen);
10.2 int pcap_sendqueue_queue(pcap_send_queue* queue, const struct pcap_pkthdr *pkt_header, const u_char *pkt_data)
说明:填充队列
入口参数:
queue 是由pcap_sendqueue_alloc()返回的指针
pkt_header是数据包头
pkt_data是数据包内容缓冲区指针
出口参数:错误是返回-1
举例:
if(pcap_sendqueue_queue(squeue, pktheader, pktdata) == -1){
printf("Warning: packet buffer too small, not all the packets will be sent.\n");
break;
}
10.3 u_int pcap_sendqueue_transmit(pcap_t *p, pcap_send_queue* queue, int sync)
说明:发送队列数据
入口参数:
pcap_t是 pcap_open_live()函数打开的网卡指针
queue 是由pcap_sendqueue_alloc()返回的指针
sync是同步设置。如果非零,那么发送将是同步的,这将站用很大的CPU资源,因为发生在内核驱动的同步发送是通过"brute force" loops的,但是一般情况下能够精确到微秒。
出口参数:错误是返回-1
举例:
if((res = pcap_sendqueue_transmit(outdesc, squeue, sync)) < squeue->len)
{
printf("An error occurred sending the packets: %s. Only %d bytes were sent\n", error,res);
}
10.4 void pcap_sendqueue_destroy(pcap_send_queue* queue);
说明:释放队列
入口参数:
queue 是由pcap_sendqueue_alloc()返回的指针
出口参数:NULL
举例:
pcap_sendqueue_destroy(squeue);
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
libpcap-第四课:不用回调函数来捕捉数据包 - 我的文章 - 汪汪学D
网络数据包捕获函数库Libpcap安装与使用
循序渐进学习使用WINPCAP
使用WinPcap编程(4)——把网络数据包存储到一个文件中
wimpcap基础知识 good
Winpcap主要数据结构及函数
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服