Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <winsock2.h>
- #include <iphlpapi.h>
- #include <icmpapi.h>
- #include <stdio.h>
- #include <chrono>
- #pragma comment(lib, "iphlpapi.lib")
- #pragma comment(lib, "ws2_32.lib")
- #pragma warning(disable : 4996)
- using namespace std;
- using namespace std::chrono;
- const int N = 512;
- const size_t timeout = 1000;
- int MinMaxMed(int* arr, int size, int flag)
- {
- if (flag == 1)
- {
- int min = timeout;
- for (int i = 0; i < size; i++)
- if (arr[i] < min)
- min = arr[i];
- return min;
- }
- else if (flag == 2)
- {
- int max = 0;
- for (int i = 0; i < size; i++)
- if (arr[i] > max)
- max = arr[i];
- return max;
- }
- else
- {
- int med = 0;
- for (int i = 0; i < size; i++)
- med += arr[i];
- med /= size;
- return med;
- }
- }
- int main() {
- WORD ver = MAKEWORD(2, 2);
- WSADATA wsaData;
- int err = 0;
- if (err = WSAStartup(ver, &wsaData))
- cout << "Error while WSAStartup";
- HANDLE IcmpHandle;
- char ReqData[N];
- LPVOID ReplyBuffer;
- WORD ReplySize;
- char HostName[N];
- unsigned long HostAddr;
- DWORD dwRetVal;
- int TrCount = 0;
- int* RecTimeArr;
- cout << "Enter address to ping:\n";
- cin >> HostName;
- cout << "Enter tries count:\n";
- cin >> TrCount;
- cout << "Enter request message:\n";
- cin >> ReqData;
- RecTimeArr = new int[TrCount] {};
- ReplySize = sizeof(ICMP_ECHO_REPLY) + sizeof(ReqData) + 8;
- ReplyBuffer = (VOID*)malloc(ReplySize);
- if (ReplyBuffer == NULL) {
- cout << "Unable to allocate memory\n";
- return 1;
- }
- IcmpHandle = IcmpCreateFile();
- if (IcmpHandle == INVALID_HANDLE_VALUE) {
- cout << "Error while opening handle\n";
- return 1;
- }
- struct hostent* host = gethostbyname(HostName);
- if (host == NULL) {
- cout << "Can't get host by name\n";
- return 1;
- }
- HostAddr = inet_addr(inet_ntoa(*((in_addr*)host->h_addr_list[0])));
- for (int i = TrCount; i > 0; i--) {
- auto start = high_resolution_clock::now();
- dwRetVal = IcmpSendEcho2(IcmpHandle, nullptr, nullptr, nullptr, HostAddr, ReqData, sizeof(ReqData), nullptr, ReplyBuffer, ReplySize, timeout);
- auto end = high_resolution_clock::now();
- auto duration = duration_cast<milliseconds>(end - start);
- if (dwRetVal) {
- PICMP_ECHO_REPLY pEchoReply = (PICMP_ECHO_REPLY)ReplyBuffer;
- struct in_addr ReplyAddr;
- ReplyAddr.S_un.S_addr = pEchoReply->Address;
- if (i == 1)
- cout << "Received " << dwRetVal << " icmp message from " << inet_ntoa(ReplyAddr) << " time: " << duration.count() << " ms\n";
- else
- cout << "Received " << dwRetVal << " icmp messages from " << inet_ntoa(ReplyAddr) << " time: " << duration.count() << " ms\n";
- RecTimeArr[TrCount - i] = duration.count();
- }
- else {
- cout << "Call to IcmpSendEcho2 failed.\n";
- }
- }
- if (RecTimeArr[0] != 0)
- cout << "min - " << MinMaxMed(RecTimeArr, TrCount, 1) << " ms, max - " << MinMaxMed(RecTimeArr, TrCount, 2) << " ms, med - " << MinMaxMed(RecTimeArr, TrCount, 3) << " ms\n";
- free(ReplyBuffer);
- IcmpCloseHandle(IcmpHandle);
- WSACleanup();
- delete[] RecTimeArr;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement