Advertisement
Guest User

Untitled

a guest
Oct 20th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.88 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <winsock2.h>
  3. #include <iphlpapi.h>
  4. #include <icmpapi.h>
  5. #include <iostream>
  6. #include <Windows.h>
  7. #pragma warning(disable: 4996)
  8. using std::cout;
  9. using std::endl;
  10. using std::atoi;
  11.  
  12. #pragma comment(lib, "iphlpapi.lib")
  13. #pragma comment(lib, "ws2_32.lib")
  14.  
  15. #define IP_STATUS_BASE 11000
  16. #define IP_SUCCESS 0
  17. #define IP_DEST_NET_UNREACHABLE 11002
  18. #define IP_DEST_HOST_UNREACHABLE 11003
  19. #define IP_DEST_PROT_UNREACHABLE 11004
  20. #define IP_DEST_PORT_UNREACHABLE 11005
  21. #define IP_REQ_TIMED_OUT 11010
  22. #define IP_BAD_REQ 11011
  23. #define IP_BAD_ROUTE 11012
  24. #define IP_TTL_EXPIRED_TRANSIT 11013
  25.  
  26. void Ping(const char* cHost, unsigned int Timeout, unsigned int RequestCount)
  27. {
  28. HANDLE hIP = IcmpCreateFile();
  29.  
  30. if (hIP == INVALID_HANDLE_VALUE)
  31. {
  32. WSACleanup();
  33. return;
  34. }
  35.  
  36. char SendData[32] = "Data for ping";//буфер для передачи
  37. int LostPacketsCount = 0; // кол-во потерянных пакетов unsigned
  38. int MaxMS = 0;// максимальное время ответа (мс)
  39. int MinMS = -1; // минимальное время ответа (мс)
  40.  
  41. // Выделяем память под пакет – буфер ответа
  42. PICMP_ECHO_REPLY pIpe = (PICMP_ECHO_REPLY)GlobalAlloc(GHND, sizeof(ICMP_ECHO_REPLY) + sizeof(SendData));
  43. if (pIpe == 0) {
  44. IcmpCloseHandle(hIP);
  45. WSACleanup();
  46. return;
  47. }
  48. pIpe->Data = SendData;
  49. pIpe->DataSize = sizeof(SendData);
  50. unsigned long ipaddr = inet_addr(cHost);
  51.  
  52. IP_OPTION_INFORMATION option = IP_OPTION_INFORMATION();
  53. option.Ttl = 100;
  54. option.Tos = 0;
  55. option.Flags = IP_FLAG_DF;
  56. option.OptionsSize = 0;
  57. option.OptionsData = 0;
  58.  
  59. for (unsigned int c = 0; c < RequestCount; c++) {
  60. int dwStatus = IcmpSendEcho(hIP, ipaddr, SendData, sizeof(SendData), &option, pIpe, sizeof(ICMP_ECHO_REPLY) + sizeof(SendData), Timeout);
  61. if (dwStatus > 0) {
  62. for (int i = 0; i< dwStatus; i++) {
  63. unsigned char* pIpPtr = (unsigned char*)&pIpe->Address;
  64. cout << "Ответ от " << (int)*(pIpPtr)
  65. << "." << (int)*(pIpPtr + 1)
  66. << "." << (int)*(pIpPtr + 2)
  67. << "." << (int) *(pIpPtr + 3)
  68. << ": число байт = " << pIpe->DataSize
  69. << " время = " << pIpe->RoundTripTime
  70. << "мс TTL = " << (int)pIpe->Options.Ttl;
  71.  
  72. MaxMS = (MaxMS > pIpe->RoundTripTime) ? MaxMS : pIpe->RoundTripTime;
  73.  
  74. MinMS = (MinMS < (int)pIpe->RoundTripTime && MinMS >= 0) ? MinMS : pIpe->RoundTripTime;
  75.  
  76. cout << endl;
  77. }
  78. }
  79. else {
  80. if (pIpe->Status) {
  81. LostPacketsCount++;
  82. switch (pIpe->Status) {
  83. case IP_DEST_NET_UNREACHABLE:
  84. case IP_DEST_HOST_UNREACHABLE:
  85. case IP_DEST_PROT_UNREACHABLE:
  86. case IP_DEST_PORT_UNREACHABLE:
  87. cout << "Remote host may be down." << endl;
  88. break;
  89. case IP_REQ_TIMED_OUT:
  90. cout << "Request timed out." << endl;
  91. break;
  92. case IP_TTL_EXPIRED_TRANSIT:
  93. cout << "TTL expired in transit." << endl;
  94. break;
  95. default:
  96. cout << "Error code: %ld" << pIpe->Status << endl;
  97. break;
  98. }
  99. }
  100. }
  101. }
  102.  
  103. IcmpCloseHandle(hIP);
  104. WSACleanup();
  105.  
  106. if (MinMS<0) MinMS = 0;
  107. unsigned char* pByte = (unsigned char*)&pIpe->Address;
  108. cout << "Статистика Ping " << (int)*(pByte)
  109. << "." << (int)*(pByte + 1)
  110. << "." << (int)*(pByte + 2)
  111. << "." << (int)*(pByte + 3) << endl;
  112.  
  113. cout << "\tПакетов: отправлено = " << RequestCount
  114. << ", получено = " << RequestCount - LostPacketsCount
  115. << ", потеряно = " << LostPacketsCount
  116. << "<" << (int)(100 / (float)RequestCount)* LostPacketsCount
  117. << " % потерь>, " << endl;
  118.  
  119. cout << "Приблизительное время приема-передачи:" << endl
  120. << "Минимальное = " << MinMS
  121. << "мс, Максимальное = " << MaxMS
  122. << "мс, Среднее = " << (MaxMS + MinMS) / 2 << "мс" << endl;
  123. }
  124.  
  125. int main(int argc, char* argv[])
  126. {
  127. SetConsoleCP(1251);
  128. SetConsoleOutputCP(1251);
  129.  
  130. /*cout << argc << endl;
  131. for (int i = 0; i < argc; i++) {
  132. cout << argv[i] << endl;
  133. }*/
  134.  
  135. switch (argc) {
  136. case 1: {
  137. Ping("127.0.0.1", 60, 10);
  138. break;
  139. }
  140. case 2: {
  141. Ping(argv[1], 60, 10);
  142. break;
  143. }
  144. case 3: {
  145. int timeout = atoi(argv[2]);
  146.  
  147. if (timeout == 0) {
  148. cout << "INCORRECT SECOND PARAMETR" << endl;
  149. return 1;
  150. }
  151.  
  152. Ping(argv[1], timeout, 10);
  153. break;
  154. }
  155. case 4: {
  156. int timeout = atoi(argv[2]);
  157. int request_count = atoi(argv[3]);
  158.  
  159. if (timeout == 0) {
  160. cout << "THE SECOND PARAMETR IS INCORRECT" << endl;
  161. return 1;
  162. }
  163. if (request_count == 0) {
  164. cout << "THE THIRD PARAMETR IS INCORRECT" << endl;
  165. return 1;
  166. }
  167.  
  168. Ping(argv[1], timeout, request_count);
  169. break;
  170. }
  171. default: {
  172. cout << "INCORRECT ARGUMENTS" << endl
  173. << "ALLOWED: \nmyPing\nmyPing ip\nmyPing ip timeout\nmyPing ip request_count\n";
  174. }
  175. }
  176.  
  177. return 0;
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement