// Steps: // 1. Open ICMP handle // 2. Sends an IPv4 ICMP echo request and returns any echo response replies. // 3. Display the infomation for response echo. // 4. Close ICMP handle /* Syntax: IPHLPAPI_DLL_LINKAGE DWORD IcmpSendEcho( [input] HANDLE IcmpHandle, [input] IPAddr DestinationAddress, [input] LPVOID RequestData, [input] WORD RequestSize, [output] LPVOID ReplyBuffer, [input] DWORD ReplySize, [input] DWORD Timeout ); */ #include #include // IP helper function library: <<<< https://docs.microsoft.com/en-us/windows/win32/iphlp/ip-helper-functions?redirectedfrom=MSDN >>>> #include #include #include using namespace std; #pragma comment(lib, "iphlpapi.lib") //The IcmpSendEcho function is exported from the Icmp.dll on Windows 2000. The IcmpSendEcho function is exported from the Iphlpapi.dll which provides all the functions of the ICMP protocol #pragma comment(lib, "ws2_32.lib") bool CheckIPAddr (unsigned long ipaddr) { cout << "Checking IP address using CheckIPAddr function" << endl; if (ipaddr == INADDR_NONE) { return false; } else { return true; } } int main(int argc, char **argv) { // Express and initialize variables HANDLE IcmpHandle; //// Handle from IcmpCreateFile(). File handling in C++ is a mechanism to store the output of a program in a file and help perform various operations on it. Files help store these data permanently on a storage device. unsigned long ipaddr = INADDR_NONE; // the destination IP address to be pinged DWORD dwRetVal = 0; // for calling function of IcmpSendEcho and DWORD means unsigned "double word. // Payload to send //************************************************************************************************************************************************************************************************************ /* because you get the size of the array including a null terminator. with '\0' */ char SendData[32] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o', /* -->C strings don't store their own length, so a 'null terminator' (the character \0) is used to mark the end of the string. */ 'p','q','r','s','t','u','v','w','a','b','c','d','e','f','g','h','i'}; /* ---> Sorun Hakkında: https://stackoverflow.com/questions/72270575/hidden-byte-in-unsigned-char */ /* alternatif biçim: char SendData[32] = "abcdefghijklmnopqrstuvwabcdefghi"; */ //********************************************************************************************************************************************************************************************************************************* // char* cpPayload = PreparePayload(); LPVOID ReplyBuffer = NULL; DWORD ReplySize = 0; // Size of the ReplyBuffer, bytes DWORD Timeout = 10000; // Wait for response time, milliseconds // Validate the parameters if (argc != 2) { cout<<'\n'; cout< https://mskb.pkisolutions.com/kb/2384321) if (IcmpHandle == INVALID_HANDLE_VALUE) { cout<<"Unable to open handle.\n"; cout<<"IcmpCreatefile returned error code:\n"<< GetLastError(); // Return value of 1 indicates failure, try to get error info. return 1; } // Reply buffer size for exactly 1 echo reply, payload data, and 8 bytes for ICMP error message. //The allocated size, in bytes, of the reply buffer. The buffer should be large enough to hold at least one ICMP_ECHO_REPLY structure plus RequestSize bytes of data. //This buffer should also be large enough to also hold 8 more bytes of data (the size of an ICMP error message). ReplySize = sizeof(ICMP_ECHO_REPLY) + sizeof(SendData) + 8; // Allocating memory space ReplyBuffer = (void*) malloc(ReplySize); // A buffer to hold any replies to the echo request and LPVOID means is void*. if (ReplyBuffer == NULL) { cout<<"Unable to allocate memory for reply buffer\n"; return 1; } for (int nCount = 0; nCount < 4; ++nCount) // Keep looping for sending ping request for 4 times. { // Call the function of IcmpSendEcho to make echo request dwRetVal = IcmpSendEcho(IcmpHandle, ipaddr, SendData, sizeof(SendData), NULL, ReplyBuffer, ReplySize, Timeout); if (dwRetVal != 0) { PICMP_ECHO_REPLY pEchoReply = (PICMP_ECHO_REPLY)ReplyBuffer; struct in_addr ipAddr; ipAddr.S_un.S_addr = pEchoReply->Address; // It converts an IP address as a string to an integer value in network byte order cout<<'\n'; cout<<" Sent ICMP echo request to "<< argv[1]<<" with "<< sizeof(SendData) <<" Bytes data"<<'\n'; if (dwRetVal > 1) { cout<<" successfully received ICMP response"<< dwRetVal<< '\n'; } else { cout<<" Received "<< dwRetVal<<" ICMP response successfully"<< '\n'; } cout<<" Roundtrip time: "<< pEchoReply->RoundTripTime<< "ms"<< '\n'; cout<<" Status: "<< pEchoReply->Status; // The function runs correctly when Status is 0) cout<<'\n'; } else { cout<<" \nCall to IcmpSendEcho failed."<< '\n'; cout<<"IcmpSendEcho returned error code: "< Due to the dynamic linking. "IP" stands for "Internet Protocol", "API" stands for "Application Programming Interface", and "HLP" stands for "Helper". // app 8.8.8.8 etc.