ping_main.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * ping program
  3. *
  4. * Copyright (C) 2010 Trey Hunner
  5. * Copyright (C) 2018 Isira Seneviratne
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  20. */
  21. #include "winsock2.h"
  22. #include "ws2tcpip.h"
  23. #include "iphlpapi.h"
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <icmpapi.h>
  28. #include <limits.h>
  29. #include <windows.h>
  30. #include "wine/debug.h"
  31. #include "wine/heap.h"
  32. WINE_DEFAULT_DEBUG_CHANNEL(ping);
  33. static void usage(void)
  34. {
  35. printf("Usage: ping [-n count] [-w timeout] [-l buffer_length] target_name\n\n"
  36. "Options:\n"
  37. " -n Number of echo requests to send.\n"
  38. " -w Timeout in milliseconds to wait for each reply.\n"
  39. " -l Length of send buffer.\n");
  40. }
  41. int __cdecl main(int argc, char** argv)
  42. {
  43. unsigned int n = 4, i, w = 4000, l = 32;
  44. int res;
  45. int rec = 0, lost = 0, min = INT_MAX, max = 0;
  46. WSADATA wsa;
  47. HANDLE icmp_file;
  48. unsigned long ipaddr;
  49. DWORD retval, reply_size;
  50. char *send_data, ip[100], *hostname = NULL, rtt[16];
  51. void *reply_buffer;
  52. struct in_addr addr;
  53. ICMP_ECHO_REPLY *reply;
  54. float avg = 0;
  55. struct hostent *remote_host;
  56. if (argc == 1)
  57. {
  58. usage();
  59. exit(1);
  60. }
  61. for (i = 1; i < argc; i++)
  62. {
  63. if (argv[i][0] == '-' || argv[i][0] == '/')
  64. {
  65. switch (argv[i][1])
  66. {
  67. case 'n':
  68. if (i == argc - 1)
  69. {
  70. printf( "Missing value for option %s\n", argv[i] );
  71. exit(1);
  72. }
  73. n = atoi(argv[++i]);
  74. if (n == 0)
  75. {
  76. printf("Bad value for option -n, valid range is from 1 to 4294967295.\n");
  77. exit(1);
  78. }
  79. break;
  80. case 'w':
  81. if (i == argc - 1)
  82. {
  83. printf( "Missing value for option %s\n", argv[i] );
  84. exit(1);
  85. }
  86. w = atoi(argv[++i]);
  87. if (w == 0)
  88. {
  89. printf("Bad value for option -w.\n");
  90. exit(1);
  91. }
  92. break;
  93. case 'l':
  94. if (i == argc - 1)
  95. {
  96. printf( "Missing value for option %s\n", argv[i] );
  97. exit(1);
  98. }
  99. l = atoi(argv[++i]);
  100. if (l == 0)
  101. {
  102. printf("Bad value for option -l.\n");
  103. exit(1);
  104. }
  105. break;
  106. case '?':
  107. usage();
  108. exit(1);
  109. default:
  110. usage();
  111. WINE_FIXME( "this command currently only supports the -n, -w and -l parameters.\n" );
  112. exit(1);
  113. }
  114. }
  115. else
  116. {
  117. if (hostname)
  118. {
  119. printf( "Bad argument %s\n", argv[i] );
  120. exit(1);
  121. }
  122. hostname = argv[i];
  123. }
  124. }
  125. if (!hostname)
  126. {
  127. printf("Pass a host name.\n");
  128. return 1;
  129. }
  130. res = WSAStartup(MAKEWORD(2, 2), &wsa);
  131. if (res != 0)
  132. {
  133. printf("WSAStartup failed: %d\n", res);
  134. return 1;
  135. }
  136. remote_host = gethostbyname(hostname);
  137. if (remote_host == NULL)
  138. {
  139. printf("Ping request could not find host %s. Please check the name and try again.\n",
  140. hostname);
  141. return 1;
  142. }
  143. addr.s_addr = *(u_long *) remote_host->h_addr_list[0];
  144. strcpy(ip, inet_ntoa(addr));
  145. ipaddr = inet_addr(ip);
  146. if (ipaddr == INADDR_NONE)
  147. {
  148. printf("Could not get IP address of host %s.", hostname);
  149. return 1;
  150. }
  151. icmp_file = IcmpCreateFile();
  152. send_data = heap_alloc_zero(l);
  153. reply_size = sizeof(ICMP_ECHO_REPLY) + l + 8;
  154. /* The buffer has to hold 8 more bytes of data (the size of an ICMP error message). */
  155. reply_buffer = heap_alloc(reply_size);
  156. if (reply_buffer == NULL)
  157. {
  158. printf("Unable to allocate memory to reply buffer.\n");
  159. return 1;
  160. }
  161. printf("Pinging %s [%s] with %d bytes of data:\n", hostname, ip, l);
  162. for (i = 0; i < n; i++)
  163. {
  164. SetLastError(0);
  165. retval = IcmpSendEcho(icmp_file, ipaddr, send_data, l,
  166. NULL, reply_buffer, reply_size, w);
  167. if (retval != 0)
  168. {
  169. reply = (ICMP_ECHO_REPLY *) reply_buffer;
  170. if (reply->RoundTripTime >= 1)
  171. sprintf(rtt, "=%d", reply->RoundTripTime);
  172. else
  173. strcpy(rtt, "<1");
  174. printf("Reply from %s: bytes=%d time%sms TTL=%d\n", ip, l,
  175. rtt, reply->Options.Ttl);
  176. if (reply->RoundTripTime > max)
  177. max = reply->RoundTripTime;
  178. if (reply->RoundTripTime < min)
  179. min = reply->RoundTripTime;
  180. avg += reply->RoundTripTime;
  181. rec++;
  182. }
  183. else
  184. {
  185. if (GetLastError() == IP_REQ_TIMED_OUT)
  186. puts("Request timed out.");
  187. else
  188. puts("PING: transmit failed. General failure.");
  189. lost++;
  190. }
  191. if (i < n - 1) Sleep(1000);
  192. }
  193. printf("\nPing statistics for %s\n", ip);
  194. printf("\tPackets: Sent = %d, Received = %d, Lost = %d (%.0f%% loss)\n",
  195. n, rec, lost, (float) lost / n * 100);
  196. if (rec != 0)
  197. {
  198. avg /= rec;
  199. printf("Approximate round trip times in milli-seconds:\n");
  200. printf("\tMinimum = %dms, Maximum = %dms, Average = %.0fms\n",
  201. min, max, avg);
  202. }
  203. heap_free(reply_buffer);
  204. return 0;
  205. }