2021-09-17 06:39:04 +03:00
|
|
|
/**
|
|
|
|
* @brief Send ICMP pings
|
|
|
|
*
|
|
|
|
* @copyright
|
|
|
|
* This file is part of ToaruOS and is released under the terms
|
|
|
|
* of the NCSA / University of Illinois License - see LICENSE.md
|
|
|
|
* Copyright (C) 2021 K. Lange
|
|
|
|
*/
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <poll.h>
|
2021-09-18 03:55:37 +03:00
|
|
|
#include <signal.h>
|
2023-01-05 17:21:17 +03:00
|
|
|
#include <sys/time.h>
|
2021-09-17 06:39:04 +03:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
2024-02-11 01:41:52 +03:00
|
|
|
#define BYTES_TO_SEND 64
|
2021-09-17 06:39:04 +03:00
|
|
|
|
|
|
|
struct ICMP_Header {
|
|
|
|
uint8_t type, code;
|
|
|
|
uint16_t checksum;
|
|
|
|
uint16_t identifier;
|
|
|
|
uint16_t sequence_number;
|
|
|
|
uint8_t payload[];
|
|
|
|
};
|
|
|
|
|
2023-01-05 17:21:17 +03:00
|
|
|
static unsigned long clocktime(void) {
|
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv,NULL);
|
|
|
|
return tv.tv_sec * 1000000 + tv.tv_usec;
|
|
|
|
}
|
|
|
|
|
2021-09-17 06:39:04 +03:00
|
|
|
static uint16_t icmp_checksum(char * payload, size_t len) {
|
|
|
|
uint32_t sum = 0;
|
|
|
|
uint16_t * s = (uint16_t *)payload;
|
|
|
|
for (size_t i = 0; i < (len) / 2; ++i) {
|
|
|
|
sum += ntohs(s[i]);
|
|
|
|
}
|
|
|
|
if (sum > 0xFFFF) {
|
|
|
|
sum = (sum >> 16) + (sum & 0xFFFF);
|
|
|
|
}
|
|
|
|
return ~(sum & 0xFFFF) & 0xFFFF;
|
|
|
|
}
|
|
|
|
|
2021-09-18 03:55:37 +03:00
|
|
|
static int break_from_loop = 0;
|
|
|
|
|
|
|
|
static void sig_break_loop(int sig) {
|
|
|
|
(void)sig;
|
|
|
|
break_from_loop = 1;
|
|
|
|
}
|
|
|
|
|
2021-09-17 06:39:04 +03:00
|
|
|
int main(int argc, char * argv[]) {
|
|
|
|
if (argc < 2) return 1;
|
|
|
|
|
2021-09-18 03:55:37 +03:00
|
|
|
int pings_sent = 0;
|
2021-09-17 06:39:04 +03:00
|
|
|
|
|
|
|
struct hostent * host = gethostbyname(argv[1]);
|
|
|
|
|
|
|
|
if (!host) {
|
|
|
|
fprintf(stderr, "%s: not found\n", argv[1]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
char * addr = inet_ntoa(*(struct in_addr*)host->h_addr_list[0]);
|
|
|
|
|
|
|
|
int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP);
|
|
|
|
|
|
|
|
if (sock < 0) {
|
|
|
|
fprintf(stderr, "%s: No socket: %s\n", argv[1], strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-02-09 08:54:07 +03:00
|
|
|
int yes = 1;
|
|
|
|
setsockopt(sock, IPPROTO_IP, IP_RECVTTL, &yes, sizeof(int));
|
|
|
|
|
2021-09-18 03:55:37 +03:00
|
|
|
signal(SIGINT, sig_break_loop);
|
|
|
|
|
2021-09-17 06:39:04 +03:00
|
|
|
struct sockaddr_in dest;
|
|
|
|
dest.sin_family = AF_INET;
|
|
|
|
memcpy(&dest.sin_addr.s_addr, host->h_addr, host->h_length);
|
|
|
|
|
2024-02-11 01:41:52 +03:00
|
|
|
printf("PING %s (%s) %d data bytes\n", argv[1], addr, BYTES_TO_SEND - 8);
|
2021-09-17 06:39:04 +03:00
|
|
|
|
|
|
|
struct ICMP_Header * ping = malloc(BYTES_TO_SEND);
|
|
|
|
ping->type = 8; /* request */
|
|
|
|
ping->code = 0;
|
|
|
|
ping->identifier = 0;
|
|
|
|
ping->sequence_number = 0;
|
|
|
|
/* Fill in data */
|
|
|
|
for (int i = 0; i < BYTES_TO_SEND - 8; ++i) {
|
|
|
|
ping->payload[i] = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
int responses_received = 0;
|
|
|
|
|
2021-09-18 03:55:37 +03:00
|
|
|
while (!break_from_loop) {
|
|
|
|
ping->sequence_number = htons(pings_sent+1);
|
2021-09-17 06:39:04 +03:00
|
|
|
ping->checksum = 0;
|
|
|
|
ping->checksum = htons(icmp_checksum((void*)ping, BYTES_TO_SEND));
|
|
|
|
|
|
|
|
/* Send it and wait */
|
2023-01-05 17:21:17 +03:00
|
|
|
unsigned long sent_at = clocktime();
|
2021-09-17 06:39:04 +03:00
|
|
|
if (sendto(sock, (void*)ping, BYTES_TO_SEND, 0, (struct sockaddr*)&dest, sizeof(struct sockaddr_in)) < 0) {
|
|
|
|
fprintf(stderr, "sendto: %s\n", strerror(errno));
|
|
|
|
}
|
|
|
|
|
2021-09-18 03:55:37 +03:00
|
|
|
pings_sent++;
|
|
|
|
|
2021-09-17 06:39:04 +03:00
|
|
|
struct pollfd fds[1];
|
|
|
|
fds[0].fd = sock;
|
|
|
|
fds[0].events = POLLIN;
|
|
|
|
int ret = poll(fds,1,1000);
|
|
|
|
|
|
|
|
if (ret > 0) {
|
|
|
|
char data[4096];
|
2024-02-09 08:54:07 +03:00
|
|
|
char control[4096];
|
2022-03-22 09:32:58 +03:00
|
|
|
struct sockaddr_in source;
|
|
|
|
socklen_t source_size = sizeof(struct sockaddr_in);
|
2024-02-09 08:54:07 +03:00
|
|
|
struct iovec _iovec = {
|
|
|
|
data, 4096
|
|
|
|
};
|
|
|
|
struct msghdr msg = {
|
|
|
|
&source,
|
|
|
|
source_size,
|
|
|
|
&_iovec,
|
|
|
|
1,
|
|
|
|
control,
|
|
|
|
4096,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
ssize_t len = recvmsg(sock, &msg, 0);
|
2023-01-05 17:21:17 +03:00
|
|
|
unsigned long rcvd_at = clocktime();
|
2021-09-17 06:39:04 +03:00
|
|
|
if (len > 0) {
|
|
|
|
/* Is it actually a PING response ? */
|
|
|
|
|
2022-03-22 09:32:58 +03:00
|
|
|
struct ICMP_Header * icmp = (void*)data;
|
2024-02-09 08:54:07 +03:00
|
|
|
unsigned char ttl = 0;
|
|
|
|
|
|
|
|
if (msg.msg_controllen) {
|
|
|
|
char * control_msg = control;
|
|
|
|
while (control_msg - control + sizeof(struct cmsghdr) <= msg.msg_controllen) {
|
|
|
|
struct cmsghdr * cmsg = (void*)control_msg;
|
|
|
|
if (cmsg->cmsg_level == IPPROTO_IP && (cmsg->cmsg_type == IP_RECVTTL || cmsg->cmsg_type == IP_TTL)) {
|
|
|
|
memcpy(&ttl, CMSG_DATA(cmsg), 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
control_msg += cmsg->cmsg_len;
|
|
|
|
}
|
|
|
|
}
|
2021-09-17 06:39:04 +03:00
|
|
|
|
|
|
|
if (icmp->type == 0) {
|
|
|
|
/* How much data, minus the header? */
|
|
|
|
/* Get the address */
|
2022-03-22 09:32:58 +03:00
|
|
|
char * from = inet_ntoa(source.sin_addr);
|
2021-09-18 04:10:53 +03:00
|
|
|
int time_taken = (rcvd_at - sent_at);
|
|
|
|
printf("%zd bytes from %s: icmp_seq=%d ttl=%d time=%d",
|
2024-02-09 08:54:07 +03:00
|
|
|
len, from, ntohs(icmp->sequence_number), (unsigned char)ttl,
|
2021-09-18 04:10:53 +03:00
|
|
|
time_taken / 1000);
|
|
|
|
if (time_taken < 1000) {
|
|
|
|
printf(".%03d", time_taken % 1000);
|
|
|
|
} else if (time_taken < 10000) {
|
|
|
|
printf(".%02d", (time_taken / 10) % 100);
|
|
|
|
} else if (time_taken < 100000) {
|
|
|
|
printf(".%01d", (time_taken / 100) % 10);
|
|
|
|
}
|
|
|
|
printf(" ms\n");
|
2021-09-17 06:39:04 +03:00
|
|
|
responses_received++;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-18 03:55:37 +03:00
|
|
|
if (!break_from_loop) {
|
2022-03-22 09:32:58 +03:00
|
|
|
sleep(1);
|
2021-09-17 06:39:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-18 03:55:37 +03:00
|
|
|
printf("--- %s statistics ---\n", argv[1]);
|
2021-09-17 06:39:04 +03:00
|
|
|
printf("%d packets transmitted, %d received, %d%% packet loss\n",
|
2021-09-18 03:55:37 +03:00
|
|
|
pings_sent, responses_received, 100*(pings_sent-responses_received)/pings_sent);
|
2021-09-17 06:39:04 +03:00
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|