2019-03-12 14:51:42 +00:00
|
|
|
#include <arpa/inet.h>
|
2019-06-07 09:49:31 +00:00
|
|
|
#include <netdb.h>
|
2019-03-20 02:38:09 +00:00
|
|
|
#include <netinet/in.h>
|
2019-06-07 09:49:31 +00:00
|
|
|
#include <netinet/ip_icmp.h>
|
2019-03-12 14:51:42 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2019-06-07 09:49:31 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/time.h>
|
2019-03-13 02:26:01 +00:00
|
|
|
#include <time.h>
|
2019-06-07 09:49:31 +00:00
|
|
|
#include <unistd.h>
|
2019-03-12 14:51:42 +00:00
|
|
|
|
2019-03-13 02:26:01 +00:00
|
|
|
uint16_t internet_checksum(const void* ptr, size_t count)
|
2019-03-12 14:51:42 +00:00
|
|
|
{
|
2019-06-07 09:49:31 +00:00
|
|
|
uint32_t checksum = 0;
|
2019-03-13 02:26:01 +00:00
|
|
|
auto* w = (const uint16_t*)ptr;
|
2019-03-12 14:51:42 +00:00
|
|
|
while (count > 1) {
|
2019-03-13 02:26:01 +00:00
|
|
|
checksum += ntohs(*w++);
|
2019-03-12 14:51:42 +00:00
|
|
|
if (checksum & 0x80000000)
|
|
|
|
checksum = (checksum & 0xffff) | (checksum >> 16);
|
|
|
|
count -= 2;
|
|
|
|
}
|
|
|
|
while (checksum >> 16)
|
|
|
|
checksum = (checksum & 0xffff) + (checksum >> 16);
|
2019-03-13 02:26:01 +00:00
|
|
|
return htons(~checksum);
|
2019-03-12 14:51:42 +00:00
|
|
|
}
|
|
|
|
|
2019-03-13 02:26:01 +00:00
|
|
|
inline void timersub(struct timeval* a, struct timeval* b, struct timeval* result)
|
|
|
|
{
|
|
|
|
result->tv_sec = a->tv_sec - b->tv_sec;
|
|
|
|
result->tv_usec = a->tv_usec - b->tv_usec;
|
|
|
|
if (result->tv_usec < 0) {
|
|
|
|
--result->tv_sec;
|
|
|
|
result->tv_usec += 1000000;
|
|
|
|
}
|
|
|
|
}
|
2019-03-12 14:51:42 +00:00
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2019-03-20 02:38:09 +00:00
|
|
|
if (argc != 2) {
|
|
|
|
printf("usage: ping <host>\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-03-12 14:51:42 +00:00
|
|
|
int fd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
|
|
|
|
if (fd < 0) {
|
|
|
|
perror("socket");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-06-07 09:49:31 +00:00
|
|
|
struct timeval timeout {
|
|
|
|
1, 0
|
|
|
|
};
|
2019-03-13 12:13:23 +00:00
|
|
|
int rc = setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
|
|
|
|
if (rc < 0) {
|
|
|
|
perror("setsockopt");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-03-20 02:38:09 +00:00
|
|
|
auto* hostent = gethostbyname(argv[1]);
|
|
|
|
if (!hostent) {
|
|
|
|
printf("Lookup failed for '%s'\n", argv[1]);
|
|
|
|
return 1;
|
|
|
|
}
|
2019-03-13 02:26:01 +00:00
|
|
|
|
|
|
|
pid_t pid = getpid();
|
|
|
|
|
2019-03-12 14:51:42 +00:00
|
|
|
sockaddr_in peer_address;
|
|
|
|
memset(&peer_address, 0, sizeof(peer_address));
|
|
|
|
peer_address.sin_family = AF_INET;
|
|
|
|
peer_address.sin_port = 0;
|
2019-03-13 02:26:01 +00:00
|
|
|
|
2019-03-20 02:38:09 +00:00
|
|
|
peer_address.sin_addr.s_addr = *(const in_addr_t*)hostent->h_addr_list[0];
|
2019-03-12 14:51:42 +00:00
|
|
|
|
|
|
|
struct PingPacket {
|
|
|
|
struct icmphdr header;
|
|
|
|
char msg[64 - sizeof(struct icmphdr)];
|
|
|
|
};
|
|
|
|
|
2019-03-13 02:26:01 +00:00
|
|
|
uint16_t seq = 1;
|
2019-03-12 14:51:42 +00:00
|
|
|
|
2019-03-13 02:26:01 +00:00
|
|
|
for (;;) {
|
|
|
|
PingPacket ping_packet;
|
|
|
|
PingPacket pong_packet;
|
|
|
|
memset(&ping_packet, 0, sizeof(PingPacket));
|
2019-03-12 14:51:42 +00:00
|
|
|
|
2019-03-13 02:26:01 +00:00
|
|
|
ping_packet.header.type = 8; // Echo request
|
|
|
|
ping_packet.header.code = 0;
|
|
|
|
ping_packet.header.un.echo.id = htons(pid);
|
|
|
|
ping_packet.header.un.echo.sequence = htons(seq++);
|
|
|
|
strcpy(ping_packet.msg, "Hello there!\n");
|
2019-03-12 14:51:42 +00:00
|
|
|
|
2019-03-13 02:26:01 +00:00
|
|
|
ping_packet.header.checksum = internet_checksum(&ping_packet, sizeof(PingPacket));
|
2019-03-12 14:51:42 +00:00
|
|
|
|
2019-03-13 02:26:01 +00:00
|
|
|
struct timeval tv_send;
|
|
|
|
gettimeofday(&tv_send, nullptr);
|
|
|
|
|
|
|
|
rc = sendto(fd, &ping_packet, sizeof(PingPacket), 0, (const struct sockaddr*)&peer_address, sizeof(sockaddr_in));
|
|
|
|
if (rc < 0) {
|
|
|
|
perror("sendto");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
2019-03-13 13:47:21 +00:00
|
|
|
socklen_t peer_address_size = sizeof(peer_address);
|
|
|
|
rc = recvfrom(fd, &pong_packet, sizeof(PingPacket), 0, (struct sockaddr*)&peer_address, &peer_address_size);
|
2019-03-13 02:26:01 +00:00
|
|
|
if (rc < 0) {
|
2019-03-13 12:13:23 +00:00
|
|
|
if (errno == EAGAIN) {
|
|
|
|
printf("Request (seq=%u) timed out.\n", ntohs(ping_packet.header.un.echo.sequence));
|
|
|
|
break;
|
|
|
|
}
|
2019-03-13 02:26:01 +00:00
|
|
|
perror("recvfrom");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pong_packet.header.type != 0)
|
|
|
|
continue;
|
|
|
|
if (pong_packet.header.code != 0)
|
|
|
|
continue;
|
|
|
|
if (ntohs(pong_packet.header.un.echo.id) != pid)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
struct timeval tv_receive;
|
|
|
|
gettimeofday(&tv_receive, nullptr);
|
2019-03-12 16:27:07 +00:00
|
|
|
|
2019-03-13 02:26:01 +00:00
|
|
|
struct timeval tv_diff;
|
|
|
|
timersub(&tv_receive, &tv_send, &tv_diff);
|
|
|
|
|
|
|
|
int ms = tv_diff.tv_sec * 1000 + tv_diff.tv_usec / 1000;
|
|
|
|
|
|
|
|
char addr_buf[64];
|
2019-03-13 12:13:23 +00:00
|
|
|
printf("Pong from %s: id=%u, seq=%u%s, time=%dms\n",
|
2019-03-13 02:26:01 +00:00
|
|
|
inet_ntop(AF_INET, &peer_address.sin_addr, addr_buf, sizeof(addr_buf)),
|
|
|
|
ntohs(pong_packet.header.un.echo.id),
|
|
|
|
ntohs(pong_packet.header.un.echo.sequence),
|
2019-03-13 12:13:23 +00:00
|
|
|
pong_packet.header.un.echo.sequence != ping_packet.header.un.echo.sequence ? "(!)" : "",
|
2019-06-07 09:49:31 +00:00
|
|
|
ms);
|
2019-03-13 12:13:23 +00:00
|
|
|
|
|
|
|
// If this was a response to an earlier packet, we still need to wait for the current one.
|
|
|
|
if (pong_packet.header.un.echo.sequence != ping_packet.header.un.echo.sequence)
|
|
|
|
continue;
|
2019-03-13 02:26:01 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
sleep(1);
|
|
|
|
}
|
2019-03-12 16:27:07 +00:00
|
|
|
|
2019-03-12 14:51:42 +00:00
|
|
|
return 0;
|
|
|
|
}
|