Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _WINSOCK_DEPRECATED_NO_WARNINGS
- #include <winsock2.h>
- #include <iphlpapi.h>
- #include <icmpapi.h>
- #include <ctime>
- #include <stdio.h>
- #pragma comment(lib, "iphlpapi.lib")
- #pragma comment(lib, "ws2_32.lib")
- int __cdecl main( int argc , char **argv )
- {
- // Declare and initialize variables
- HANDLE hIcmpFile;
- unsigned long ipaddr = INADDR_NONE;
- DWORD dwRetVal = 0;
- char SendData [ 32 ] = "Data Buffer";
- LPVOID ReplyBuffer = NULL;
- DWORD ReplySize = 0;
- // Validate the parameters
- if ( argc != 2 ) {
- printf( "usage: %s IP address\n" , argv [ 0 ] );
- system( "pause" );
- return 1;
- }
- ipaddr = inet_addr( argv [ 1 ] );
- if ( ipaddr == INADDR_NONE ) {
- printf( "usage: %s IP address\n" , argv [ 0 ] );
- system( "pause" );
- return 1;
- }
- hIcmpFile = IcmpCreateFile( );
- if ( hIcmpFile == INVALID_HANDLE_VALUE ) {
- printf( "\tUnable to open handle.\n" );
- printf( "IcmpCreatefile returned error: %ld\n" , GetLastError( ) );
- system( "pause" );
- return 1;
- }
- ReplySize = sizeof( ICMP_ECHO_REPLY ) + sizeof( SendData );
- ReplyBuffer = ( VOID* ) malloc( ReplySize );
- if ( ReplyBuffer == NULL ) {
- printf( "\tUnable to allocate memory\n" );
- system( "pause" );
- return 1;
- }
- while ( true )
- {
- dwRetVal = IcmpSendEcho( hIcmpFile , ipaddr , SendData , sizeof( SendData ) , NULL , ReplyBuffer , ReplySize , 1000 );
- if ( dwRetVal != 0 )
- {
- PICMP_ECHO_REPLY pEchoReply = ( PICMP_ECHO_REPLY ) ReplyBuffer;
- struct in_addr ReplyAddr;
- ReplyAddr.S_un.S_addr = pEchoReply->Address;
- }
- else
- {
- auto t = time( 0 );
- struct tm now;
- localtime_s( &now , &t );
- printf( "%d:%d:%d %d-%d-%d: ping failed\n" , now.tm_hour , now.tm_min , now.tm_sec , now.tm_year + 1900 , now.tm_mon + 1 , now.tm_mday );
- system( "pause" );
- return 1;
- }
- Sleep( 500 );
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement