Pcap::Pcap()
{
alldevs = new pcap_if[MAX_DEVICE_NUM];
for( int i = 0;i < MAX_DEVICE_NUM;i++ )
{
device_name[i] = new char[MAX_DEVICE_NAME_LENGTH];
device_description[i] = new char[MAX_DEVICE_DESCRIPTION_LENGTH];
}
}
Pcap::~Pcap()
{
delete []alldevs;
for( int i = 0;i < MAX_DEVICE_NUM;i++ )
delete []device_name[i];
for( int i = 0;i < MAX_DEVICE_NUM;i++ )
delete []device_description[i];
outfile.close();
}
int Pcap::show_info_of_device( void )
{
pcap_if *temp;
char *Pstr;
int i = 0;
if( ( pcap_findalldevs( &alldevs,errbuf ) ) == -1 ) // error in find device
{
cerr <<" error in fine device! " <<endl;
exit(1);
}
else // no error,print the info
{
for( temp = alldevs;temp != NULL;temp = temp->next )
{
// name
if( temp->name )
{
cout << i <<":" <<" device'name is " <<temp->name <<endl;
device_name[i] = temp->name;
}
// description
if( temp->description )
{
cout << i <<":" <<" device'description is " <<temp->description <<endl;
device_description[i] = temp->description;
}
else
cout << i <<":" <<" no descripton avalable! " <<endl;
// flag
if( temp->flags )
cout << i <<":" <<" device'flag is " <<temp->flags <<endl;
else
cout << i <<":" <<" no flag avalable! " <<endl;
#ifdef PRINT_ADDRESS_OPEN
if( i != 0 )
{
// addr
if( temp->addresses->addr )
{
Pstr = inet_ntoa( temp->addresses->addr->sin_addr );
device_addr[i] = Pstr;
cout << i <<":" <<" device's address is " <<Pstr <<endl;
}
if( temp->addresses->broadaddr )
{
Pstr = inet_ntoa( temp->addresses->broadaddr->sin_addr );
device_broadaddr[i] = Pstr;
cout << i <<":" <<" device's broadcast address is " <<Pstr <<endl;
}
if( temp->addresses->dstaddr )
{
Pstr = inet_ntoa( temp->addresses->dstaddr->sin_addr );
device_dstaddr[i] = Pstr;
cout << i <<":" <<" device's destination address for p2p is " <<Pstr <<endl;
}
if( temp->addresses->netmask )
{
Pstr = inet_ntoa( temp->addresses->netmask->sin_addr );
device_netmask[i] = Pstr;
cout << i <<":" <<" device's netmask is " <<Pstr <<endl;
}
}
#endif
i++;
} // end of for
if( i == 0 )
{
cerr <<" there is no net device in your computer! ";
exit(1);
}
else
device_num = i; // obtain the device number in the machine
} // end of else
return(0);
}
int Pcap::select_device( void )
{
cout <<endl;
for( int i = 0;i < device_num;i++ )
cout <<" 设备 " <<i <<" 的名字是 " <<device_name[i] <<endl;
//cout <<" please input the devise youn want to choose: " <<endl;
cout <<" 请输入你希望选择的设备: " <<endl;
cin >>operating_flag; // indicate the device bing used
if( operating_flag < 0 || operating_flag > MAX_DEVICE_NUM )
{
//cerr <<" the num you input is not exist !" <<endl;
cerr <<" 你输入的号码并不存在,请核对后重新输入 !" <<endl;
pcap_freealldevs( alldevs );
exit(1);
}
return 0;
}
int Pcap::capture_packet( void )
{
int error_flag;
// no error
cout <<" 正在监听 " <<device_description[operating_flag] <<endl <<endl;
//pcap_freealldevs( alldevs ); // now,we don't need any more the device list
pcap_setmintocopy( caphandle,PACKET_NUM_KERNEL_TO_USER ); // kernel is forced to wait 32 packets and then copy the data to the user
error_flag = pcap_loop( caphandle,PACKET_NUM,packet_handler,NULL ); // start capturing
The following are the only two type specifications supported for Windows Sockets 1.1:
SOCK_STREAM
Provides sequenced, reliable, two-way, connection-based byte streams with an OOB data transmission mechanism. Uses TCP for the Internet address family.
SOCK_DGRAM
Supports datagrams, which are connectionless, unreliable buffers of a fixed (typically small) maximum length. Uses UDP for the Internet address family.
In Windows Sockets 2, many new socket types will be introduced and no longer need to be specified, since an application can dynamically discover the attributes of each available transport protocol through the WSAEnumProtocols function. Socket type definitions appear in Winsock2.h, which will be periodically updated as new socket types, address families, and protocols are defined.