winpcap模擬發包
#define HAVE_REMOTE
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <pcap.h>
bool get_file_size(const std::wstring& filename, unsigned long long& size)
{
HANDLE file = CreateFileW(filename.c_str(),
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (file == INVALID_HANDLE_VALUE)
{
return false;
}
LARGE_INTEGER file_size = { 0 };
GetFileSizeEx(file, &file_size);
size = file_size.QuadPart;
::CloseHandle(file);
return true;
}
bool read_file(const std::wstring& filename, std::string& data)
{
HANDLE file = CreateFileW(filename.c_str(),
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (file == INVALID_HANDLE_VALUE)
{
return false;
}
unsigned long long file_size = 0;
get_file_size(filename, file_size);
if (0 == file_size)
{
::CloseHandle(file);
return true;
}
data.resize(file_size);
DWORD count_of_bytes = 0;
bool ret = ::ReadFile(file, (LPVOID)data.c_str(), file_size, &count_of_bytes, NULL)
&& (count_of_bytes == file_size);
::CloseHandle(file);
return ret;
}
int main()
{
pcap_if_t * allAdapters;
pcap_if_t * adapter;
pcap_t * adapterHandle;
char errorBuffer[ PCAP_ERRBUF_SIZE ];
// retrieve the adapters from the computer
if( pcap_findalldevs_ex( PCAP_SRC_IF_STRING, NULL,
&allAdapters, errorBuffer ) == -1 )
{
fprintf( stderr, "Error in pcap_findalldevs_ex function: %s\n",
errorBuffer );
return -1;
}
// if there are no adapters, print an error
if( allAdapters == NULL )
{
printf( "\nNo adapters found! Make sure WinPcap is installed.\n" );
return 0;
}
// print the list of adapters along with basic information about an adapter
int crtAdapter = 0;
for( adapter = allAdapters; adapter != NULL; adapter = adapter->next)
{
printf( "\n%d.%s ", ++crtAdapter, adapter->name );
printf( "-- %s\n", adapter->description );
}
printf( "\n" );
int adapterNumber = 1;
if( adapterNumber < 1 || adapterNumber > crtAdapter )
{
printf( "\nAdapter number out of range.\n" );
// Free the adapter list
pcap_freealldevs( allAdapters );
return -1;
}
// parse the list until we reach the desired adapter
adapter = allAdapters;
for( crtAdapter = 0; crtAdapter < adapterNumber - 1; crtAdapter++ )
adapter = adapter->next;
// open the adapter
adapterHandle = pcap_open( adapter->name, // name of the adapter
65536, // portion of the packet to capture
// 65536 guarantees that the whole
// packet will be captured
PCAP_OPENFLAG_PROMISCUOUS, // promiscuous mode
1000, // read timeout - 1 millisecond
NULL, // authentication on the remote machine
errorBuffer // error buffer
);
if( adapterHandle == NULL )
{
fprintf( stderr, "\nUnable to open the adapter\n", adapter->name );
// Free the adapter list
pcap_freealldevs( allAdapters );
return -1;
}
// free the adapter list
pcap_freealldevs( allAdapters );
// this is the most important part of the application
// here we send the packet
std::wstring file1 = L"e:\\1.dat";
std::string sData1;
if (!read_file(file1,sData1))
{
return 0;
}
std::wstring file2 = L"e:\\2.dat";
std::string sData2;
if (!read_file(file2,sData2))
{
return 0;
}
std::wstring file3 = L"e:\\3.dat";
std::string sData3;
if (!read_file(file3,sData3))
{
return 0;
}
// send the packet
for (int nRuntimes = 0;nRuntimes < 100;nRuntimes++)
{
if( pcap_sendpacket( adapterHandle,(const u_char *)sData1.data(),sData1.size()) != 0 )
{
fprintf( stderr,"\nError sending the packet: \n", pcap_geterr( adapterHandle ) );
return -1;
}
Sleep(1000);
if( pcap_sendpacket( adapterHandle,(const u_char *)sData2.data(),sData2.size()) != 0 )
{
fprintf( stderr,"\nError sending the packet: \n", pcap_geterr( adapterHandle ) );
return -1;
}
Sleep(400);
if( pcap_sendpacket( adapterHandle,(const u_char *)sData3.data(),sData3.size()) != 0 )
{
fprintf( stderr,"\nError sending the packet: \n", pcap_geterr( adapterHandle ) );
return -1;
}
Sleep(10000);
}
//system( "PAUSE" );
return 0;
}
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <pcap.h>
bool get_file_size(const std::wstring& filename, unsigned long long& size)
{
HANDLE file = CreateFileW(filename.c_str(),
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (file == INVALID_HANDLE_VALUE)
{
return false;
}
LARGE_INTEGER file_size = { 0 };
GetFileSizeEx(file, &file_size);
size = file_size.QuadPart;
::CloseHandle(file);
return true;
}
bool read_file(const std::wstring& filename, std::string& data)
{
HANDLE file = CreateFileW(filename.c_str(),
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (file == INVALID_HANDLE_VALUE)
{
return false;
}
unsigned long long file_size = 0;
get_file_size(filename, file_size);
if (0 == file_size)
{
::CloseHandle(file);
return true;
}
data.resize(file_size);
DWORD count_of_bytes = 0;
bool ret = ::ReadFile(file, (LPVOID)data.c_str(), file_size, &count_of_bytes, NULL)
&& (count_of_bytes == file_size);
::CloseHandle(file);
return ret;
}
int main()
{
pcap_if_t * allAdapters;
pcap_if_t * adapter;
pcap_t * adapterHandle;
char errorBuffer[ PCAP_ERRBUF_SIZE ];
// retrieve the adapters from the computer
if( pcap_findalldevs_ex( PCAP_SRC_IF_STRING, NULL,
&allAdapters, errorBuffer ) == -1 )
{
fprintf( stderr, "Error in pcap_findalldevs_ex function: %s\n",
errorBuffer );
return -1;
}
// if there are no adapters, print an error
if( allAdapters == NULL )
{
printf( "\nNo adapters found! Make sure WinPcap is installed.\n" );
return 0;
}
// print the list of adapters along with basic information about an adapter
int crtAdapter = 0;
for( adapter = allAdapters; adapter != NULL; adapter = adapter->next)
{
printf( "\n%d.%s ", ++crtAdapter, adapter->name );
printf( "-- %s\n", adapter->description );
}
printf( "\n" );
int adapterNumber = 1;
if( adapterNumber < 1 || adapterNumber > crtAdapter )
{
printf( "\nAdapter number out of range.\n" );
// Free the adapter list
pcap_freealldevs( allAdapters );
return -1;
}
// parse the list until we reach the desired adapter
adapter = allAdapters;
for( crtAdapter = 0; crtAdapter < adapterNumber - 1; crtAdapter++ )
adapter = adapter->next;
// open the adapter
adapterHandle = pcap_open( adapter->name, // name of the adapter
65536, // portion of the packet to capture
// 65536 guarantees that the whole
// packet will be captured
PCAP_OPENFLAG_PROMISCUOUS, // promiscuous mode
1000, // read timeout - 1 millisecond
NULL, // authentication on the remote machine
errorBuffer // error buffer
);
if( adapterHandle == NULL )
{
fprintf( stderr, "\nUnable to open the adapter\n", adapter->name );
// Free the adapter list
pcap_freealldevs( allAdapters );
return -1;
}
// free the adapter list
pcap_freealldevs( allAdapters );
// this is the most important part of the application
// here we send the packet
std::wstring file1 = L"e:\\1.dat";
std::string sData1;
if (!read_file(file1,sData1))
{
return 0;
}
std::wstring file2 = L"e:\\2.dat";
std::string sData2;
if (!read_file(file2,sData2))
{
return 0;
}
std::wstring file3 = L"e:\\3.dat";
std::string sData3;
if (!read_file(file3,sData3))
{
return 0;
}
// send the packet
for (int nRuntimes = 0;nRuntimes < 100;nRuntimes++)
{
if( pcap_sendpacket( adapterHandle,(const u_char *)sData1.data(),sData1.size()) != 0 )
{
fprintf( stderr,"\nError sending the packet: \n", pcap_geterr( adapterHandle ) );
return -1;
}
Sleep(1000);
if( pcap_sendpacket( adapterHandle,(const u_char *)sData2.data(),sData2.size()) != 0 )
{
fprintf( stderr,"\nError sending the packet: \n", pcap_geterr( adapterHandle ) );
return -1;
}
Sleep(400);
if( pcap_sendpacket( adapterHandle,(const u_char *)sData3.data(),sData3.size()) != 0 )
{
fprintf( stderr,"\nError sending the packet: \n", pcap_geterr( adapterHandle ) );
return -1;
}
Sleep(10000);
}
//system( "PAUSE" );
return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.