Winpcap常用函数.doc_第1页
Winpcap常用函数.doc_第2页
Winpcap常用函数.doc_第3页
Winpcap常用函数.doc_第4页
Winpcap常用函数.doc_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Winpcap常用函数最近在看WINPCAP,将其库函数总结如下1. int pcap_findalldevs(pcap_if_t *, char *) 说明:用来获得网卡的列表 参数:指向pcap_if_t*类型的列表的指针的指针; char型指针,当打开列表错误时返回错误信息 返回值: 为int型,当显示列表失败时返回-1pcap_if_t 是pcap_if 重命名而来:typedef struct pcap_if pcap_if_t;pcap_if结构体如下:struct pcap_if struct pcap_if *next; char *name; char *description; struct pcap_addr *addresses; /pcap_addr 结构体 bpf_u_int32 flags;pcap_addr 结构体如下:struct pcap_addr struct pcap_addr *next; struct sockaddr *addr; struct sockaddr *netmask; struct sockaddr *broadaddr; struct sockaddr *dstaddr;举例: pcap_if_t *alldevs; pcap_if_t *d; char errbuf64; if (pcap_findalldevs(&alldevs, errbuf) = -1) fprintf(stderr,Error in pcap_findalldevs: %sn, 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);用pcap_findalldevs不能获得网卡的MAC,有两种方法可以实现,一、向自己发送arp包,二、使用IPHelp的API可以获得。2. void pcap_freealldevs(pcap_if_t *)说明:与int pcap_findalldevs(pcap_if_t *, char *)配套使用,当不再需要网卡列表时,用此函数free释放空间参数:打开网卡列表时申请的pcap_if_t型的指针举例: 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 int snapshot; int linktype; int tzoff; int offset; struct pcap_sf sf; struct pcap_md md; 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 errbufPCAP_ERRBUF_SIZE + 1; int dlt_count; int *dlt_list; #ifdef REMOTE 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_clos() int rmt_flags; /! we have to save flags, since they are passed by the pcap_open_live(), but they are used by the pcap_start capture() 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; /!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, / 混杂模式 1000, / 设置超时时间,亳秒为单位 errbuf / 发生错误时存放错误内容的缓冲区 ) ) = NULL) fprintf(stderr,nUnable to open the adapter. %s is not supported by WinPcapn); 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 timestr16;ltime=localtime(&header-ts.tv_sec);strftime( timestr, sizeof timestr, %H:%M:%S, ltime);printf(%s,%.6d len:%dn, timestr, header-ts.tv_usec, header-len);5. int pcap_dispatch(pcap_t * p, int cnt, pcap_handler, u_char *user)说明:捕获数据包。可以不被阻塞参数:与pcap_loop()相同pcap_dispatch(.)和pcap_loop(.)的比较: 一旦网卡被打开,旧可以调用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型的结构体,存储时间,包的长度 pkt_data存储数据包的内容,为一个char型数组struct pcap_pkthdr struct timeval ts; bpf_u_int32 caplen; bpf_u_int32 len;返回值:当等于1时成功;等于0时超时;等于-1时说明发生错误,错误信息用pcap_geterr(adhandle)获得举例:(最重要的一段代码)#define LINE_LEN 16main(int argc, char *argv) pcap_if_t *alldevs, *d; pcap_t *fp; u_int inum, i=0; char errbufPCAP_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_namenn); if(argc 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 i) /选择网卡 printf(nInterface number out of range.n); pcap_freealldevs(alldevs); return -1; for(d=alldevs, i=0; inext, i+); /跳转到指定的网卡 if ( (fp= pcap_open_live(d-name, 100, 1, 20, errbuf) ) = NULL) fprintf(stderr,nError opening adaptern); return -1; else switch (argv1 1) case n: if ( (fp= pcap_open_live(argv2, 100, 1, 20, errbuf) ) = NULL) /Open a physical device fprintf(stderr,nError opening adaptern); return -1; break; case f: if ( (fp = pcap_open_offline(argv2, errbuf) ) = NULL) fprintf(stderr,nError opening dump filen); return -1; break; while(res = pcap_next_ex( fp, &header, &pkt_data) = 0) if(res = 0) continue; printf(%ld:%ld (%ld)n, header-ts.tv_sec, header-ts.tv_usec, header-len); for (i=1; (i caplen + 1 ) ; i+) printf(%.2x , pkt_datai-1); if ( (i % LINE_LEN) = 0) printf(n); printf(nn); if(res = -1) printf(Error reading the packets: %sn, 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指明是否需要优化编译结果。 netmask子网掩码返回值: 发生错误是返回-1举例:if(d-addresses != NULL) netmask=(struct sockaddr_in *)(d-addresses-netmask)-sin_addr.S_addr;else netmask=0xffffff; if(pcap_compile(adhandle, &fcode, ip and tcp, 1, netmask) 0 ) fprintf(stderr,nUnable to compile the packet filter. Check the syntax.n); pcap_freealldevs(alldevs); return -1;if(pcap_setfilter(adhandle, &fcode)0) fprintf(stderr,nError setting the filter.n); pcap_freealldevs(alldevs); return -1;8. int pcap_setfilter ( pcap_t *p, struct bpf_program * fp ) 说明:pcap_setfilter() 用来联系一个在内核驱动上过滤的过滤器,这时所有网络数据包都将流经过滤器,并拷贝到应用程序中。参数: p是打开网卡时返回的网卡指针 fp是pcap_compile()传递过来的参数返回值: 错误时返回-19. int pcap_sendpacket(pcap_t *p, u_char *buf, int size) 说明:手工发送一个数据包了。这个函数需要的参数:一个装有要发送数据的缓冲区,要发送的长度,和一个适配器。注意缓冲区中的数据将不被内核协议处理,只是作为最原始的数据流被发送,所以我门必须填充好正确的协议头以便正确的将数据发送。参数: p是打开网卡时返回的网卡指针 buf是发送数据包的内容缓冲区首地址 size是发送数据包的大小举例:void usage();void main(int argc, char *argv)pcap_t *fp;char errorPCAP_ERRBUF_SIZE;u_char packet100;int i;if (argc != 2) printf(usage: %s inerface, argv0); return;if(fp = pcap_open_live(argv1, 100, 1, 1000, error) ) = NULL) fprintf(stderr,nError opening adapter: %sn, error); return;packet0=1;packet1=1;packet2=1;packet3=1;packet4=1;packet5=1;packet6=2;packet7=2;packet8=2;packet9=2;packet10=2;packet11=2;for(i=12;i100;i+) packeti=i%256;pcap_sendpacket(fp, packet, 100); /发送原始的数据包return;10.发送数据包有关的几个函数pcap_send_queue* pcap_sendqueue_alloc(u_int memsize);说明:给数据包队列分配空间参数: memsize队列缓冲区的大小返回值: pcap_send_queue指针struct pcap_send_queue u_int maxlen; u_int len; / Current size of the queue, in bytes. char *buffer; / Buffer containing the packets to be sent.;举例:squeue = pcap_sendqueue_allo

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论