icmp.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include "icmp.h"
  2. #include "netinet.h"
  3. #include "hdef.h"
  4. #include "hsocket.h"
  5. #include "htime.h"
  6. #define PING_TIMEOUT 1000 // ms
  7. int ping(const char* host, int cnt) {
  8. static uint16_t seq = 0;
  9. uint16_t pid16 = (uint16_t)getpid();
  10. char ip[64] = {0};
  11. uint32_t start_tick, end_tick;
  12. uint64_t start_hrtime, end_hrtime;
  13. int timeout = 0;
  14. int sendbytes = 64;
  15. char sendbuf[64];
  16. char recvbuf[128]; // iphdr + icmp = 84 at least
  17. icmp_t* icmp_req = (icmp_t*)sendbuf;
  18. iphdr_t* ipheader = (iphdr_t*)recvbuf;
  19. icmp_t* icmp_res;
  20. // ping stat
  21. int send_cnt = 0;
  22. int recv_cnt = 0;
  23. int ok_cnt = 0;
  24. float rtt, min_rtt, max_rtt, total_rtt;
  25. rtt = max_rtt = total_rtt = 0.0f;
  26. min_rtt = 1000000.0f;
  27. //min_rtt = MIN(rtt, min_rtt);
  28. //max_rtt = MAX(rtt, max_rtt);
  29. // gethostbyname -> socket -> setsockopt -> sendto -> recvfrom -> closesocket
  30. sockaddr_u peeraddr;
  31. socklen_t addrlen = sizeof(peeraddr);
  32. memset(&peeraddr, 0, addrlen);
  33. int ret = Resolver(host, &peeraddr);
  34. if (ret != 0) return ret;
  35. sockaddr_ip(&peeraddr, ip, sizeof(ip));
  36. int sockfd = socket(peeraddr.sa.sa_family, SOCK_RAW, IPPROTO_ICMP);
  37. if (sockfd < 0) {
  38. perror("socket");
  39. if (errno == EPERM) {
  40. fprintf(stderr, "please use root or sudo to create a raw socket.\n");
  41. }
  42. return -socket_errno();
  43. }
  44. timeout = PING_TIMEOUT;
  45. ret = so_sndtimeo(sockfd, timeout);
  46. if (ret < 0) {
  47. perror("setsockopt");
  48. goto error;
  49. }
  50. timeout = PING_TIMEOUT;
  51. ret = so_rcvtimeo(sockfd, timeout);
  52. if (ret < 0) {
  53. perror("setsockopt");
  54. goto error;
  55. }
  56. icmp_req->icmp_type = ICMP_ECHO;
  57. icmp_req->icmp_code = 0;
  58. icmp_req->icmp_id = pid16;
  59. for (int i = 0; i < sendbytes - sizeof(icmphdr_t); ++i) {
  60. icmp_req->icmp_data[i] = i;
  61. }
  62. start_tick = gettick();
  63. while (cnt-- > 0) {
  64. // NOTE: checksum
  65. icmp_req->icmp_seq = ++seq;
  66. icmp_req->icmp_cksum = 0;
  67. icmp_req->icmp_cksum = checksum((uint8_t*)icmp_req, sendbytes);
  68. start_hrtime = gethrtime_us();
  69. addrlen = sockaddr_len(&peeraddr);
  70. int nsend = sendto(sockfd, sendbuf, sendbytes, 0, &peeraddr.sa, addrlen);
  71. if (nsend < 0) {
  72. perror("sendto");
  73. continue;
  74. }
  75. ++send_cnt;
  76. addrlen = sizeof(peeraddr);
  77. int nrecv = recvfrom(sockfd, recvbuf, sizeof(recvbuf), 0, &peeraddr.sa, &addrlen);
  78. if (nrecv < 0) {
  79. perror("recvfrom");
  80. continue;
  81. }
  82. ++recv_cnt;
  83. end_hrtime = gethrtime_us();
  84. // check valid
  85. bool valid = false;
  86. int iphdr_len = ipheader->ihl * 4;
  87. int icmp_len = nrecv - iphdr_len;
  88. if (icmp_len == sendbytes) {
  89. icmp_res = (icmp_t*)(recvbuf + ipheader->ihl*4);
  90. if (icmp_res->icmp_type == ICMP_ECHOREPLY &&
  91. icmp_res->icmp_id == pid16 &&
  92. icmp_res->icmp_seq == seq) {
  93. valid = true;
  94. }
  95. }
  96. if (valid == false) {
  97. printd("recv invalid icmp packet!\n");
  98. continue;
  99. }
  100. rtt = (end_hrtime-start_hrtime) / 1000.0f;
  101. min_rtt = MIN(rtt, min_rtt);
  102. max_rtt = MAX(rtt, max_rtt);
  103. total_rtt += rtt;
  104. printd("%d bytes from %s: icmp_seq=%u ttl=%u time=%.1f ms\n", icmp_len, ip, seq, ipheader->ttl, rtt);
  105. fflush(stdout);
  106. ++ok_cnt;
  107. if (cnt > 0) sleep(1); // sleep a while, then agian
  108. }
  109. end_tick = gettick();
  110. printd("--- %s ping statistics ---\n", host);
  111. printd("%d packets transmitted, %d received, %d%% packet loss, time %d ms\n",
  112. send_cnt, recv_cnt, (send_cnt-recv_cnt)*100/(send_cnt==0?1:send_cnt), end_tick-start_tick);
  113. printd("rtt min/avg/max = %.3f/%.3f/%.3f ms\n",
  114. min_rtt, total_rtt/(ok_cnt==0?1:ok_cnt), max_rtt);
  115. closesocket(sockfd);
  116. return ok_cnt;
  117. error:
  118. closesocket(sockfd);
  119. return socket_errno() > 0 ? -socket_errno() : -1;
  120. }