Serenity Operating System
at master 212 lines 7.4 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#include <AK/Assertions.h> 8#include <AK/ByteBuffer.h> 9#include <LibCore/ArgsParser.h> 10#include <LibCore/System.h> 11#include <LibMain/Main.h> 12#include <arpa/inet.h> 13#include <errno.h> 14#include <netdb.h> 15#include <netinet/in.h> 16#include <netinet/ip.h> 17#include <netinet/ip_icmp.h> 18#include <serenity.h> 19#include <signal.h> 20#include <stdio.h> 21#include <string.h> 22#include <sys/socket.h> 23#include <sys/time.h> 24#include <time.h> 25#include <unistd.h> 26 27static uint32_t total_pings; 28static int successful_pings; 29static Optional<size_t> count; 30static uint32_t total_ms; 31static int min_ms; 32static int max_ms; 33static DeprecatedString host; 34static int payload_size = -1; 35// variable part of header can be 0 to 40 bytes 36// https://datatracker.ietf.org/doc/html/rfc791#section-3.1 37static constexpr int max_optional_header_size_in_bytes = 40; 38static constexpr int min_header_size_in_bytes = 5; 39 40static void closing_statistics() 41{ 42 int packet_loss = 100; 43 44 outln(); 45 outln("--- {} ping statistics ---", host); 46 47 if (total_pings) 48 packet_loss -= 100.0f * successful_pings / total_pings; 49 outln("{} packets transmitted, {} received, {}% packet loss", 50 total_pings, successful_pings, packet_loss); 51 52 int average_ms = 0; 53 if (successful_pings) 54 average_ms = total_ms / successful_pings; 55 outln("rtt min/avg/max = {}/{}/{} ms", min_ms, average_ms, max_ms); 56 57 exit(0); 58}; 59 60ErrorOr<int> serenity_main(Main::Arguments arguments) 61{ 62 TRY(Core::System::pledge("stdio id inet unix sigaction")); 63 64 Core::ArgsParser args_parser; 65 args_parser.add_positional_argument(host, "Host to ping", "host"); 66 args_parser.add_option(count, "Stop after sending specified number of ECHO_REQUEST packets.", "count", 'c', "count"); 67 args_parser.add_option(payload_size, "Amount of bytes to send as payload in the ECHO_REQUEST packets.", "size", 's', "size"); 68 args_parser.parse(arguments); 69 70 if (count.has_value() && (count.value() < 1 || count.value() > UINT32_MAX)) { 71 warnln("invalid count argument: '{}': out of range: 1 <= value <= {}", count.value(), UINT32_MAX); 72 return 1; 73 } 74 75 if (payload_size < 0) { 76 // Use the default. 77 payload_size = 32 - sizeof(struct icmphdr); 78 } 79 80 int fd = TRY(Core::System::socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)); 81 82 TRY(Core::System::drop_privileges()); 83 TRY(Core::System::pledge("stdio inet unix sigaction")); 84 85 struct timeval timeout { 86 1, 0 87 }; 88 89 TRY(Core::System::setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout))); 90 91 auto* hostent = gethostbyname(host.characters()); 92 if (!hostent) { 93 warnln("Lookup failed for '{}'", host); 94 return 1; 95 } 96 97 TRY(Core::System::pledge("stdio inet sigaction")); 98 99 pid_t pid = getpid(); 100 101 sockaddr_in peer_address {}; 102 peer_address.sin_family = AF_INET; 103 peer_address.sin_port = 0; 104 peer_address.sin_addr.s_addr = *(in_addr_t const*)hostent->h_addr_list[0]; 105 106 uint16_t seq = 1; 107 108 TRY(Core::System::signal(SIGINT, [](int) { 109 closing_statistics(); 110 })); 111 112 for (;;) { 113 auto ping_packet_result = ByteBuffer::create_zeroed(sizeof(struct icmphdr) + payload_size); 114 if (ping_packet_result.is_error()) { 115 warnln("failed to allocate a large enough buffer for the ping packet"); 116 return 1; 117 } 118 auto& ping_packet = ping_packet_result.value(); 119 struct icmphdr* ping_hdr = reinterpret_cast<struct icmphdr*>(ping_packet.data()); 120 ping_hdr->type = ICMP_ECHO; 121 ping_hdr->code = 0; 122 ping_hdr->un.echo.id = htons(pid); 123 ping_hdr->un.echo.sequence = htons(seq++); 124 125 // Fill payload 126 for (int i = 0; i < payload_size; i++) { 127 ping_packet[i + sizeof(struct icmphdr)] = i & 0xFF; 128 } 129 130 ping_hdr->checksum = internet_checksum(ping_packet.data(), ping_packet.size()); 131 132 struct timeval tv_send; 133 gettimeofday(&tv_send, nullptr); 134 135 if (count.has_value() && total_pings == count.value()) 136 closing_statistics(); 137 else 138 total_pings++; 139 140 TRY(Core::System::sendto(fd, ping_packet.data(), ping_packet.size(), 0, (const struct sockaddr*)&peer_address, sizeof(sockaddr_in))); 141 142 for (;;) { 143 auto pong_packet_result = ByteBuffer::create_uninitialized( 144 sizeof(struct ip) + max_optional_header_size_in_bytes + sizeof(struct icmphdr) + payload_size); 145 if (pong_packet_result.is_error()) { 146 warnln("failed to allocate a large enough buffer for the pong packet"); 147 return 1; 148 } 149 auto& pong_packet = pong_packet_result.value(); 150 socklen_t peer_address_size = sizeof(peer_address); 151 auto result = Core::System::recvfrom(fd, pong_packet.data(), pong_packet.size(), 0, (struct sockaddr*)&peer_address, &peer_address_size); 152 if (result.is_error()) { 153 if (result.error().code() == EAGAIN) { 154 outln("Request (seq={}) timed out.", ntohs(ping_hdr->un.echo.sequence)); 155 break; 156 } 157 return result.release_error(); 158 } 159 160 i8 internet_header_length = *pong_packet.data() & 0x0F; 161 if (internet_header_length < min_header_size_in_bytes) { 162 outln("ping: illegal ihl field value {:x}", internet_header_length); 163 continue; 164 } 165 166 struct icmphdr* pong_hdr = reinterpret_cast<struct icmphdr*>(pong_packet.data() + (internet_header_length * 4)); 167 if (pong_hdr->type != ICMP_ECHOREPLY) 168 continue; 169 if (pong_hdr->code != 0) 170 continue; 171 if (ntohs(pong_hdr->un.echo.id) != pid) 172 continue; 173 174 struct timeval tv_receive; 175 gettimeofday(&tv_receive, nullptr); 176 177 struct timeval tv_diff; 178 timersub(&tv_receive, &tv_send, &tv_diff); 179 180 int ms = tv_diff.tv_sec * 1000 + tv_diff.tv_usec / 1000; 181 successful_pings++; 182 int seq_dif = ntohs(ping_hdr->un.echo.sequence) - ntohs(pong_hdr->un.echo.sequence); 183 184 // Approximation about the timeout of the out of order packet 185 if (seq_dif) 186 ms += seq_dif * 1000 * timeout.tv_sec; 187 188 total_ms += ms; 189 if (min_ms == 0) 190 min_ms = max_ms = ms; 191 else if (ms < min_ms) 192 min_ms = ms; 193 else if (ms > max_ms) 194 max_ms = ms; 195 196 char addr_buf[INET_ADDRSTRLEN]; 197 outln("Pong from {}: id={}, seq={}{}, time={}ms, size={}", 198 inet_ntop(AF_INET, &peer_address.sin_addr, addr_buf, sizeof(addr_buf)), 199 ntohs(pong_hdr->un.echo.id), 200 ntohs(pong_hdr->un.echo.sequence), 201 pong_hdr->un.echo.sequence != ping_hdr->un.echo.sequence ? "(!)" : "", 202 ms, result.value()); 203 204 // If this was a response to an earlier packet, we still need to wait for the current one. 205 if (pong_hdr->un.echo.sequence != ping_hdr->un.echo.sequence) 206 continue; 207 break; 208 } 209 210 sleep(1); 211 } 212}