ping.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibCore/ArgsParser.h>
  27. #include <arpa/inet.h>
  28. #include <netdb.h>
  29. #include <netinet/in.h>
  30. #include <netinet/ip_icmp.h>
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <sys/socket.h>
  34. #include <sys/time.h>
  35. #include <time.h>
  36. #include <unistd.h>
  37. static uint16_t internet_checksum(const void* ptr, size_t count)
  38. {
  39. uint32_t checksum = 0;
  40. auto* w = (const uint16_t*)ptr;
  41. while (count > 1) {
  42. checksum += ntohs(*w++);
  43. if (checksum & 0x80000000)
  44. checksum = (checksum & 0xffff) | (checksum >> 16);
  45. count -= 2;
  46. }
  47. while (checksum >> 16)
  48. checksum = (checksum & 0xffff) + (checksum >> 16);
  49. return htons(~checksum);
  50. }
  51. int main(int argc, char** argv)
  52. {
  53. if (pledge("stdio id inet dns", nullptr) < 0) {
  54. perror("pledge");
  55. return 1;
  56. }
  57. const char* host = nullptr;
  58. Core::ArgsParser args_parser;
  59. args_parser.add_positional_argument(host, "Host to ping", "host");
  60. args_parser.parse(argc, argv);
  61. int fd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
  62. if (fd < 0) {
  63. perror("socket");
  64. return 1;
  65. }
  66. if (setgid(getgid()) || setuid(getuid())) {
  67. fprintf(stderr, "Failed to drop privileges.\n");
  68. return 1;
  69. }
  70. if (pledge("stdio inet dns", nullptr) < 0) {
  71. perror("pledge");
  72. return 1;
  73. }
  74. struct timeval timeout {
  75. 1, 0
  76. };
  77. int rc = setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
  78. if (rc < 0) {
  79. perror("setsockopt");
  80. return 1;
  81. }
  82. auto* hostent = gethostbyname(host);
  83. if (!hostent) {
  84. printf("Lookup failed for '%s'\n", host);
  85. return 1;
  86. }
  87. if (pledge("stdio inet", nullptr) < 0) {
  88. perror("pledge");
  89. return 1;
  90. }
  91. pid_t pid = getpid();
  92. sockaddr_in peer_address;
  93. memset(&peer_address, 0, sizeof(peer_address));
  94. peer_address.sin_family = AF_INET;
  95. peer_address.sin_port = 0;
  96. peer_address.sin_addr.s_addr = *(const in_addr_t*)hostent->h_addr_list[0];
  97. struct PingPacket {
  98. struct icmphdr header;
  99. char msg[64 - sizeof(struct icmphdr)];
  100. };
  101. uint16_t seq = 1;
  102. for (;;) {
  103. PingPacket ping_packet;
  104. PingPacket pong_packet;
  105. memset(&ping_packet, 0, sizeof(PingPacket));
  106. ping_packet.header.type = 8; // Echo request
  107. ping_packet.header.code = 0;
  108. ping_packet.header.un.echo.id = htons(pid);
  109. ping_packet.header.un.echo.sequence = htons(seq++);
  110. bool fits = String("Hello there!\n").copy_characters_to_buffer(ping_packet.msg, sizeof(ping_packet.msg));
  111. // It's a constant string, we can be sure that it fits.
  112. ASSERT(fits);
  113. ping_packet.header.checksum = internet_checksum(&ping_packet, sizeof(PingPacket));
  114. struct timeval tv_send;
  115. gettimeofday(&tv_send, nullptr);
  116. rc = sendto(fd, &ping_packet, sizeof(PingPacket), 0, (const struct sockaddr*)&peer_address, sizeof(sockaddr_in));
  117. if (rc < 0) {
  118. perror("sendto");
  119. return 1;
  120. }
  121. for (;;) {
  122. socklen_t peer_address_size = sizeof(peer_address);
  123. rc = recvfrom(fd, &pong_packet, sizeof(PingPacket), 0, (struct sockaddr*)&peer_address, &peer_address_size);
  124. if (rc < 0) {
  125. if (errno == EAGAIN) {
  126. printf("Request (seq=%u) timed out.\n", ntohs(ping_packet.header.un.echo.sequence));
  127. break;
  128. }
  129. perror("recvfrom");
  130. return 1;
  131. }
  132. if (pong_packet.header.type != 0)
  133. continue;
  134. if (pong_packet.header.code != 0)
  135. continue;
  136. if (ntohs(pong_packet.header.un.echo.id) != pid)
  137. continue;
  138. struct timeval tv_receive;
  139. gettimeofday(&tv_receive, nullptr);
  140. struct timeval tv_diff;
  141. timersub(&tv_receive, &tv_send, &tv_diff);
  142. int ms = tv_diff.tv_sec * 1000 + tv_diff.tv_usec / 1000;
  143. char addr_buf[64];
  144. printf("Pong from %s: id=%u, seq=%u%s, time=%dms\n",
  145. inet_ntop(AF_INET, &peer_address.sin_addr, addr_buf, sizeof(addr_buf)),
  146. ntohs(pong_packet.header.un.echo.id),
  147. ntohs(pong_packet.header.un.echo.sequence),
  148. pong_packet.header.un.echo.sequence != ping_packet.header.un.echo.sequence ? "(!)" : "",
  149. ms);
  150. // If this was a response to an earlier packet, we still need to wait for the current one.
  151. if (pong_packet.header.un.echo.sequence != ping_packet.header.un.echo.sequence)
  152. continue;
  153. break;
  154. }
  155. sleep(1);
  156. }
  157. return 0;
  158. }