循序渐进学习使用WINPCAP文档格式.docx
《循序渐进学习使用WINPCAP文档格式.docx》由会员分享,可在线阅读,更多相关《循序渐进学习使用WINPCAP文档格式.docx(34页珍藏版)》请在冰豆网上搜索。
一些需要知道的细节描述(前言):
这一部分展示了如何使用WINPCAP-API的不同的功能,它作为一个使用指南被划分为一系列的课时来带领读者循序渐进的体会PCAP的程序设计的
魅力:
从简单的基本功能(如获取网卡的列表,数据包的捕获等)到统计和收集网络流量等高级功能。
在这里将提供一些简单但完整的代码作为参考:
所有的这些原代码都有和它相关的详细信息的连接以便单击这些功能和数据结构时能够即使跳转到相关的文献。
这些例子是用C语言写的,所以在学习之前首先要有一定的C语言的基础,当然PCAP作为一个网络底层的驱动,要想学好它也必须具备一定的网络方面的知识。
(一)得到网络驱动列表
用PCAP写应用程序的第一件事往往就是要获得本地的网卡列表。
PCAP提供了pcap_findalldevs()这个函数来实现此功能,这个API返回一个pcap_if结构的连表,连表的每项内容都含有全面的网卡信息:
尤其是字段名字和含有名字的描述以及有关驱动器的易读信息。
得到网络驱动列表的程序如下:
#include"
pcap.h"
main()
{
pcap_if_t*alldevs;
pcap_if_t*d;
inti=0;
charerrbuf[PCAP_ERRBUF_SIZE];
/*这个API用来获得网卡的列表*/
if(pcap_findalldevs(&
alldevs,errbuf)==-1)
fprintf(stderr,"
Errorinpcap_findalldevs:
%s\n"
errbuf);
exit
(1);
}
/*显示列表的响应字段的内容*/
for(d=alldevs;
d;
d=d->
next)
printf("
%d.%s"
++i,d->
name);
if(d->
description)
(%s)\n"
d->
description);
else
(Nodescriptionavailable)\n"
);
if(i==0)
\nNointerfacesfound!
MakesureWinPcapisinstalled.\n"
return;
/*Wedon'
tneedanymorethedevicelist.Freeit*/
pcap_freealldevs(alldevs);
有关这段程序的一些说明:
首先pcap_findalldevs()同其他的libpca函数一样有一个errbuf参数,当有异常情况发生时,这个参数会被PCAP填充为某个特定的错误字串。
再次,UNIX也同样提供pcap_findalldevs()这个函数,但是请注意并非所有的系统都支持libpcap提供的网络程序接口。
所以我门要想写出合适
的程序就必须考虑到这些情况(系统不能够返回一些字段的描述信息),在这种情况下我门应该给出类似"
Nodescriptionavailable"
这样的
提示。
最后结束时别忘了用pcap_freealldevs()释放掉内存资源。
原文如下:
Obtainingthedevicelist
ThefirstthingthatusuallyaWinPcapbasedapplicationneedsisalistofsuitablenetworkadapters.Libpcapprovidesthepcap_findalldevs()functionforthispurpose:
thisfunctionreturnsalinkedlistofpcap_ifstructures,eachofwhichcontainscomprehensiveinformationaboutanadapter.Inparticularthefieldsnameanddescriptioncontainthenameandahumanreadabledescriptionofthedevice.
Thefollowingcoderetrievestheadapterlistandshowsitonthescreen,printinganerrorifnoadaptersarefound.
/*Retrievethedevicelist*/
/*Printthelist*/
Somecommentsaboutthiscode.
Firstofall,pcap_findalldevs(),likeotherlibpcapfunctions,hasanerrbufparameter.Thisparameterpointstoastringfilledbylibpcapwithadescriptionoftheerrorifsomethinggoeswrong.
Second,notethatpcap_findalldevs()isprovidedbylibpcapunderUnixaswell,butrememberthatnotalltheOSessupportedbylibpcapprovideadescriptionofthenetworkinterfaces,thereforeifwewanttowriteaportableapplication,wemustconsiderthecaseinwhichdescriptionisnull:
weprintthestring"
inthatsituation.
Notefinallythatwefreethelistwithpcap_freealldevs()oncewhenwehavefinishedwithit.
Let'
strytocompileandrunthecodeofthisfirstsample.InordertocompileitunderUnixorCygwin,simplyissuea:
gcc-otestaprogtestprog.c-lpcap
OnWindows,youwillneedtocreateaproject,followingtheinstructionsinthe"
UsingWinPcapinyourprograms"
sectionofthismanual.However,IsuggestyoutousetheWinPcapdeveloper'
spack(availableattheWinPcapwebsite,http:
//winpcap.polito.it),thatprovidesalotofproperlyconfiguredexampleapps,allthecodepresentedinthistutorialandalltheprojects,includesandlibrariesneededtocompileandrunthesamples.
Assumingwehavecompiledtheprogram,let'
strytorunit.OnmyWinXPworkstation,theresultis
1.{4E273621-5161-46C8-895A-48D0E52A0B83}(RealtekRTL8029(AS)EthernetAdapter)
2.{5D24AE04-C486-4A96-83FB-8B5EC6C7F430}(3ComEtherLinkPCI)
Asyoucansee,thenameofthenetworkadapters(thatwillbepassedtolibpcapwhenopeningthedevices)underWindowsarequiteunreadable,sothedescriptionnearthemcanbeveryusefultotheuser.
循序渐进学习使用WINPCAP
(二)
获得已安装网络驱动器的高级信息
在第一章中演示了如何获得已存在适配器的静态信息。
实际上WinPcap同样也提供其他的高级信息,特别是pcap_findalldevs()这个函数返回的每个pcap_if结构体都同样包含一个pcap_addr结构的列表,他包含:
一个地址列表,一个掩码列表,一个广播地址列表和一个目的地址列表。
下面的例子通过一个ifprint()函数打印出了pcap_if结构的的所有字段信息,该程序对每一个pcap_findalldevs()所返回的pcap_if结构循环调用ifprint()来显示详细的字段信息。
#ifndefWIN32
#include<
sys/socket.h>
netinet/in.h>
#else
winsock.h>
#endif
voidifprint(pcap_if_t*d);
char*iptos(u_longin);
intmain()
charerrbuf[PCAP_ERRBUF_SIZE+1];
/*获得网卡的列表*/
errbuf);
/*循环调用ifprint()来显示pcap_if结构的信息*/
ifprint(d);
return1;
/*Printalltheavailableinformationonthegiveninterface*/
voidifprint(pcap_if_t*d)
pcap_addr_t*a;
/*Name*/
%s\n"
d->
/*Description*/
\tDescription:
/*LoopbackAddress*/
\tLoopback:
(d->
flags&
PCAP_IF_LOOPBACK)?
"
yes"
:
no"
/*IPaddresses*/
for(a=d->
addresses;
a;
a=a->
next){
\tAddressFamily:
#%d\n"
a->
addr->
sa_family);
/*关于sockaddr_in结构请参考其他的网络编程书*/
switch(a->
sa_family)
caseAF_INET:
\tAddressFamilyName:
AF_INET\n"
//打印网络地址类型
if(a->
addr)//打印IP地址
\tAddress:
iptos(((structsockaddr_in*)a->
addr)->
sin_addr.s_addr));
netmask)//打印掩码
\tNetmask:
netmask)->
broadaddr)//打印广播地址
\tBroadcastAddress:
broadaddr)->
dstaddr)//目的地址
\tDestinationAddress:
dstaddr)->
break;
default:
Unknown\n"
\n"
/*
将一个unsignedlong型的IP转换为字符串类型的IP*/
#defineIPTOSBUFFERS
12
char*iptos(u_longin)
staticcharoutput[IPTOSBUFFERS][3*4+3+1];
staticshortwhich;
u_char*p;
p=(u_char*)&
in;
which=(which+1==IPTOSBUFFERS?
0:
which+1);
sprintf(output[which],"
%d.%d.%d.%d"
p[0],p[1],p[2],p[3]);
returnoutput[which];
循序渐进学习使用WINPCAP(三)
-------打开网卡捕获数据包
现在我门已经知道了如何去获得网卡的信息现在就让我们开始真正的工作:
打开网卡并捕获数据流。
在这一节
里我们将写一个打印流经网络的每个数据包信息的程序。
打开网卡的功能是通过pcap_open_live()来实现的它
有三个参数snaplen
promisc
to_ms。
snaplen用于指定所捕获包的特定部分,在一些系统上(象xBSDandWin32等)驱动只给出所捕获数据包
的一部分而不是全部,这样就减少了拷贝数据的数量从而提高了包捕获的效率。
promisc指明网卡处于混杂模式,在正常情况下网卡只接受去往它的包而去往其他主机的数据包则被忽略
。
相反当网卡处于混杂模式时他将接收所有的流经它的数据包:
这就意味着在共享介质的情况下我门可以捕获
到其它主机的数据包。
大部分的包捕获程序都将混杂模式设为默认,所有我们在下面的例子里也将网卡设为混杂模式。
to_ms参数指定读数据的超时控制,超时以毫秒计算。
当在超时时间内网卡上没有数据到来时对网卡的读
操作将返回(如pcap_dispatch()orpcap_next_ex()等函数)。
还有,如果网卡处于统计模式下(请查看“统计和收集网络数据流一节”)to_ms还定义了统计的时间间隔。
如果该参数为0那么意味着没有超时控制,对网卡的读操作在没有数据到来是将永远堵塞。
如果为-1那么对网卡的读操作将立即返回不管有没有数据可读。
/*prototypeofthepackethandler*/
voidpacket_handler(u_char*param,conststructpcap_pkthdr*header,constu_char*pkt_data);
intinum;
pcap_t*adhandle;
/*打印网卡信息*/
d;
d=d->
else
return-1;
Entertheinterfacenumber(1-%d):
i);
scanf("
%d"
&
inum);
//输入要选择打开的网卡号
if(inum<
1||inum>
i)
//判断号的合法性
\nInterfacenumberoutofrange.\n"
/*Freethedevicelist*/
/*找到要选择的网卡结构*/
for(d=alldevs,i=0;