Formatting for better readability

This commit is contained in:
2024-09-16 17:53:45 +00:00
parent 35c0263e1b
commit 9c661d16b6

7
main.c
View File

@@ -20,6 +20,7 @@ uint16_t calculate_checksum(uint16_t *data, int len)
double get_timestamp() double get_timestamp()
{ {
struct timeval timestamp; struct timeval timestamp;
gettimeofday(&timestamp, NULL); gettimeofday(&timestamp, NULL);
return (timestamp.tv_sec + (double)timestamp.tv_usec / 1000000); return (timestamp.tv_sec + (double)timestamp.tv_usec / 1000000);
} }
@@ -31,11 +32,13 @@ int ft_ping(int sock, int seq, struct sockaddr_in dst)
memset(data, 0, sizeof(data)); memset(data, 0, sizeof(data));
memset(icmp_hdr, 0, sizeof(*icmp_hdr)); memset(icmp_hdr, 0, sizeof(*icmp_hdr));
icmp_hdr->type = ICMP_ECHO; icmp_hdr->type = ICMP_ECHO;
icmp_hdr->code = 0; icmp_hdr->code = 0;
icmp_hdr->id = getpid(); icmp_hdr->id = getpid();
icmp_hdr->seq = seq; icmp_hdr->seq = seq;
icmp_hdr->checksum = calculate_checksum((uint16_t *)icmp_hdr, sizeof(icmp_hdr)); icmp_hdr->checksum = calculate_checksum((uint16_t *)icmp_hdr, sizeof(icmp_hdr));
if (sendto(sock, data, sizeof(data), 0, (struct sockaddr *)&dst, sizeof(dst)) == -1) if (sendto(sock, data, sizeof(data), 0, (struct sockaddr *)&dst, sizeof(dst)) == -1)
{ {
fprintf(stderr, "ERROR : sendto() failed\n"); fprintf(stderr, "ERROR : sendto() failed\n");
@@ -67,11 +70,12 @@ char *get_ip_by_hostname(char *hostname)
{ {
struct hostent *he; struct hostent *he;
struct in_addr **addr_list; struct in_addr **addr_list;
he = gethostbyname(hostname);
he = gethostbyname(hostname);
addr_list = (struct in_addr **)he->h_addr_list; addr_list = (struct in_addr **)he->h_addr_list;
return (inet_ntoa(*addr_list[0])); return (inet_ntoa(*addr_list[0]));
} }
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int sock; int sock;
@@ -81,6 +85,7 @@ int main(int argc, char **argv)
double start; double start;
int n_packet_sent = 0; int n_packet_sent = 0;
int n_packet_recv = 0; int n_packet_recv = 0;
if (argc != 2 || argv[1] == NULL || argv[1][0] == 0) if (argc != 2 || argv[1] == NULL || argv[1][0] == 0)
{ {
fprintf(stderr, "ERROR : usage : ping {-v?} [ADRESS]\n"); fprintf(stderr, "ERROR : usage : ping {-v?} [ADRESS]\n");