读书人

请问一个用WinPcap编写的例子

发布时间: 2012-03-28 15:40:03 作者: rapoo

请教一个用WinPcap编写的例子
在网上找了一个调用WinPcap的例子,但是用VS2005编译得到下面的错误
1> netflow.cpp
1> c:\try\netflow\netflow\netflow.cpp(210) : error C2664: 'localtime ' : cannot convert parameter 1 from 'const long *__w64 ' to 'const time_t * '
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1> ws2_32.lib
1> c1xx : fatal error C1083: Cannot open source file: 'ws2_32.lib ': No such file or directory
1> wpcap.lib
1> c1xx : fatal error C1083: Cannot open source file: 'wpcap.lib ': No such file or directory
1> Generating Code...
1> Build log was saved at "file://c:\try\netflow\netflow\Debug\BuildLog.htm "
1> netflow - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

2005用的不熟,好像是语法不支持VC6.0的,请问该怎么该啊?
还有,就是这个程序用VC6.0编译成功后,在VC6.0的那台电脑(XP HOME)上只能显示猫的网络适配器,100M和无线网卡都不显示。但是在另外一台电脑上(XP PRO+2005NET)却能显示所有网络适配器。请问这是为什么啊?

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//下面是一个应用winpcap写的一个网络流量统计的例子,

// nettraffic.cpp : Defines the entry point for the console application.

//

#include <pcap.h>

#include <remote-ext.h>

#include <iostream>

#include <winsock2.h>


using namespace std;

//------------------------------------


//------------------------------------

void dispatcher_handler(u_char* user_data, const struct pcap_pkthdr * pkthdr, const u_char *pktdata);


//------------------------------------

int main(int argc, char* argv[])

{

int i;

pcap_if_t* alldevs;

pcap_if_t* dev;

char errorbuf[PCAP_ERRBUF_SIZE];



int choice;

pcap_t* stathandle;

WSADATA wsadata;

struct timeval timestamp;



if( WSAStartup( MAKEWORD(2,2), &wsadata) != 0 )

{

cerr < < "WSAStartup failed [ " < <WSAGetLastError() < < " ] " < <endl;

return (-1);

}



//enum all device

if( pcap_findalldevs_ex( PCAP_SRC_IF_STRING, NULL, &alldevs, errorbuf ) == -1 )

{

WSACleanup();

cerr < < "pcap_findalldevs_ex failed! ( " < <errorbuf < < ") " < <endl;

return (-1);

}



for( i=0,dev = alldevs; dev != NULL; dev = dev-> next )

{

cout < <++i < < '\t ' < <dev-> name < <endl;

}



if( i== 0 )



{

WSACleanup();

cerr < < "no device found! " < <endl;

return (-2);

}



//let user choice

while( 1)

{

cout < < "please choice a device: ";

cin> > choice;

if( choice > = 1 && choice <=i )

break;



cerr < < "input error, you shall choice a device from upon list " < <endl;

}



//move to the choice device

for( i=0, dev = alldevs; i <choice-1; i++,dev = dev-> next );

if( (stathandle = pcap_open( dev-> name,

100,

PCAP_OPENFLAG_PROMISCUOUS,

500,



NULL, errorbuf ) ) == NULL )

{

cerr < < "open device failed! [device: " < <dev-> name < < "] "

< <errorbuf < <endl;



pcap_freealldevs( alldevs );

WSACleanup();

return (-3);

}



cout < < "is Stat " < <dev-> name < < " ... " < <endl;

pcap_freealldevs( alldevs );



pcap_setmode( stathandle, MODE_STAT );

timestamp.tv_sec = 0;

timestamp.tv_usec = 0;

pcap_loop( stathandle, 0, dispatcher_handler,(unsigned char*)&timestamp );

pcap_close( stathandle );


return 0;

}

//------------------------------------

void dispatcher_handler(u_char* user_data, const struct pcap_pkthdr * pkthdr, const u_char *pktdata)

{

static struct timeval tstamp = *( (struct timeval*)user_data );

LARGE_INTEGER Bps,Pps;

unsigned long delay;



char strtime[32];



delay = (pkthdr-> ts.tv_sec - tstamp.tv_sec)*1000000 - tstamp.tv_usec + pkthdr-> ts.tv_usec;



Pps.QuadPart = ((*(LONGLONG*)(pktdata)) * 1000000 ) / delay;

Bps.QuadPart = ((*(LONGLONG*)(pktdata + 8)) * 1000000 ) / delay;


struct tm* ltime = localtime( &(pkthdr-> ts.tv_sec) );

strftime( strtime, sizeof(strtime), "%H:%M:%S ", ltime);



printf( "%s: ", strtime );

printf( "\tPps=%I64u\tBps=%I64u\r\n ",Pps.QuadPart,Bps.QuadPart);



tstamp = pkthdr-> ts;

}




[解决办法]
需要将参数localtime的类型转换一下,另外新程序中缺少库文件ws2_32.lib和wpcap.lib,或者已经有了但没在项目属性中设置

读书人网 >VC

热点推荐