Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.84 KB | None | 0 0
  1. #define _WINSOCK_DEPRECATED_NO_WARNINGS
  2. #include <winsock2.h>
  3. #include <iphlpapi.h>
  4. #include <icmpapi.h>
  5. #include <ctime>
  6. #include <stdio.h>
  7.  
  8. #pragma comment(lib, "iphlpapi.lib")
  9. #pragma comment(lib, "ws2_32.lib")
  10.  
  11. int __cdecl main( int argc , char **argv )
  12. {
  13.  
  14.     // Declare and initialize variables
  15.  
  16.     HANDLE hIcmpFile;
  17.     unsigned long ipaddr = INADDR_NONE;
  18.     DWORD dwRetVal = 0;
  19.     char SendData [ 32 ] = "Data Buffer";
  20.     LPVOID ReplyBuffer = NULL;
  21.     DWORD ReplySize = 0;
  22.  
  23.     // Validate the parameters
  24.     if ( argc != 2 ) {
  25.         printf( "usage: %s IP address\n" , argv [ 0 ] );
  26.         system( "pause" );
  27.         return 1;
  28.     }
  29.  
  30.     ipaddr = inet_addr( argv [ 1 ] );
  31.     if ( ipaddr == INADDR_NONE ) {
  32.         printf( "usage: %s IP address\n" , argv [ 0 ] );
  33.         system( "pause" );
  34.         return 1;
  35.     }
  36.  
  37.     hIcmpFile = IcmpCreateFile( );
  38.     if ( hIcmpFile == INVALID_HANDLE_VALUE ) {
  39.         printf( "\tUnable to open handle.\n" );
  40.         printf( "IcmpCreatefile returned error: %ld\n" , GetLastError( ) );
  41.         system( "pause" );
  42.         return 1;
  43.     }
  44.  
  45.     ReplySize = sizeof( ICMP_ECHO_REPLY ) + sizeof( SendData );
  46.     ReplyBuffer = ( VOID* ) malloc( ReplySize );
  47.     if ( ReplyBuffer == NULL ) {
  48.         printf( "\tUnable to allocate memory\n" );
  49.         system( "pause" );
  50.         return 1;
  51.     }
  52.  
  53.     while ( true )
  54.     {
  55.         dwRetVal = IcmpSendEcho( hIcmpFile , ipaddr , SendData , sizeof( SendData ) , NULL , ReplyBuffer , ReplySize , 1000 );
  56.         if ( dwRetVal != 0 )
  57.         {
  58.             PICMP_ECHO_REPLY pEchoReply = ( PICMP_ECHO_REPLY ) ReplyBuffer;
  59.             struct in_addr ReplyAddr;
  60.             ReplyAddr.S_un.S_addr = pEchoReply->Address;
  61.         }
  62.         else
  63.         {
  64.             auto t = time( 0 );
  65.             struct tm now;
  66.             localtime_s( &now , &t );
  67.             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 );
  68.             system( "pause" );
  69.             return 1;
  70.         }
  71.         Sleep( 500 );
  72.     }
  73.     return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement