Serenity Operating System
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
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
38uint16_t internet_checksum(const void* ptr, size_t count)
39{
40 uint32_t checksum = 0;
41 auto* w = (const uint16_t*)ptr;
42 while (count > 1) {
43 checksum += ntohs(*w++);
44 if (checksum & 0x80000000)
45 checksum = (checksum & 0xffff) | (checksum >> 16);
46 count -= 2;
47 }
48 while (checksum >> 16)
49 checksum = (checksum & 0xffff) + (checksum >> 16);
50 return htons(~checksum);
51}
52
53int main(int argc, char** argv)
54{
55 if (pledge("stdio id inet dns", nullptr) < 0) {
56 perror("pledge");
57 return 1;
58 }
59
60 if (argc != 2) {
61 printf("usage: ping <host>\n");
62 return 0;
63 }
64
65 int fd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
66 if (fd < 0) {
67 perror("socket");
68 return 1;
69 }
70
71 if (setgid(getgid()) || setuid(getuid())) {
72 fprintf(stderr, "Failed to drop privileges.\n");
73 return 1;
74 }
75
76 if (pledge("stdio inet dns", nullptr) < 0) {
77 perror("pledge");
78 return 1;
79 }
80
81 struct timeval timeout {
82 1, 0
83 };
84 int rc = setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
85 if (rc < 0) {
86 perror("setsockopt");
87 return 1;
88 }
89
90 auto* hostent = gethostbyname(argv[1]);
91 if (!hostent) {
92 printf("Lookup failed for '%s'\n", argv[1]);
93 return 1;
94 }
95
96 if (pledge("stdio inet", nullptr) < 0) {
97 perror("pledge");
98 return 1;
99 }
100
101 pid_t pid = getpid();
102
103 sockaddr_in peer_address;
104 memset(&peer_address, 0, sizeof(peer_address));
105 peer_address.sin_family = AF_INET;
106 peer_address.sin_port = 0;
107
108 peer_address.sin_addr.s_addr = *(const in_addr_t*)hostent->h_addr_list[0];
109
110 struct PingPacket {
111 struct icmphdr header;
112 char msg[64 - sizeof(struct icmphdr)];
113 };
114
115 uint16_t seq = 1;
116
117 for (;;) {
118 PingPacket ping_packet;
119 PingPacket pong_packet;
120 memset(&ping_packet, 0, sizeof(PingPacket));
121
122 ping_packet.header.type = 8; // Echo request
123 ping_packet.header.code = 0;
124 ping_packet.header.un.echo.id = htons(pid);
125 ping_packet.header.un.echo.sequence = htons(seq++);
126 strcpy(ping_packet.msg, "Hello there!\n");
127
128 ping_packet.header.checksum = internet_checksum(&ping_packet, sizeof(PingPacket));
129
130 struct timeval tv_send;
131 gettimeofday(&tv_send, nullptr);
132
133 rc = sendto(fd, &ping_packet, sizeof(PingPacket), 0, (const struct sockaddr*)&peer_address, sizeof(sockaddr_in));
134 if (rc < 0) {
135 perror("sendto");
136 return 1;
137 }
138
139 for (;;) {
140 socklen_t peer_address_size = sizeof(peer_address);
141 rc = recvfrom(fd, &pong_packet, sizeof(PingPacket), 0, (struct sockaddr*)&peer_address, &peer_address_size);
142 if (rc < 0) {
143 if (errno == EAGAIN) {
144 printf("Request (seq=%u) timed out.\n", ntohs(ping_packet.header.un.echo.sequence));
145 break;
146 }
147 perror("recvfrom");
148 return 1;
149 }
150
151 if (pong_packet.header.type != 0)
152 continue;
153 if (pong_packet.header.code != 0)
154 continue;
155 if (ntohs(pong_packet.header.un.echo.id) != pid)
156 continue;
157
158 struct timeval tv_receive;
159 gettimeofday(&tv_receive, nullptr);
160
161 struct timeval tv_diff;
162 timersub(&tv_receive, &tv_send, &tv_diff);
163
164 int ms = tv_diff.tv_sec * 1000 + tv_diff.tv_usec / 1000;
165
166 char addr_buf[64];
167 printf("Pong from %s: id=%u, seq=%u%s, time=%dms\n",
168 inet_ntop(AF_INET, &peer_address.sin_addr, addr_buf, sizeof(addr_buf)),
169 ntohs(pong_packet.header.un.echo.id),
170 ntohs(pong_packet.header.un.echo.sequence),
171 pong_packet.header.un.echo.sequence != ping_packet.header.un.echo.sequence ? "(!)" : "",
172 ms);
173
174 // If this was a response to an earlier packet, we still need to wait for the current one.
175 if (pong_packet.header.un.echo.sequence != ping_packet.header.un.echo.sequence)
176 continue;
177 break;
178 }
179
180 sleep(1);
181 }
182
183 return 0;
184}