add: print
This commit is contained in:
11
src/interval.c
Normal file
11
src/interval.c
Normal file
@ -0,0 +1,11 @@
|
||||
#include <bits/types/struct_timeval.h>
|
||||
|
||||
double interval_to_ms(struct timeval const *t)
|
||||
{
|
||||
return (double)t->tv_sec * 1000.0 + (double)t->tv_usec / 1000.0;
|
||||
}
|
||||
|
||||
double get_interval(struct timeval const *start, struct timeval const *stop)
|
||||
{
|
||||
return interval_to_ms(stop) - interval_to_ms(start);
|
||||
}
|
||||
5
src/interval.h
Normal file
5
src/interval.h
Normal file
@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include <sys/time.h>
|
||||
|
||||
double get_interval(struct timeval const *start, struct timeval const *stop);
|
||||
18
src/main.c
18
src/main.c
@ -4,6 +4,8 @@
|
||||
#include "parsing.h"
|
||||
#include "print.h"
|
||||
#include "setting.h"
|
||||
#include "statistics.h"
|
||||
|
||||
#include <netinet/ip.h>
|
||||
#include <signal.h>
|
||||
#include <stddef.h>
|
||||
@ -100,6 +102,7 @@ static int get_setting(char * const *av, struct setting *setting)
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
(void) ac;
|
||||
struct statistics stats;
|
||||
struct setting settings;
|
||||
|
||||
if (get_setting(av + 1, &settings))
|
||||
@ -116,7 +119,6 @@ int main(int ac, char **av)
|
||||
settings.dest.ip.sin_port = htons(0);
|
||||
inet_pton(AF_INET, settings.dest.ipstr, &settings.dest.ip.sin_addr);
|
||||
|
||||
|
||||
dns_reverse_lookup(&settings.dest);
|
||||
|
||||
int sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
|
||||
@ -146,6 +148,9 @@ int main(int ac, char **av)
|
||||
struct sockaddr_in sender;
|
||||
socklen_t len = sizeof(sender);
|
||||
|
||||
bzero(&stats, sizeof(struct statistics));
|
||||
print_header(&settings);
|
||||
|
||||
while (loop) {
|
||||
|
||||
struct timeval stop, start;
|
||||
@ -159,6 +164,8 @@ int main(int ac, char **av)
|
||||
return 2;
|
||||
}
|
||||
|
||||
stats.packets_sent++;
|
||||
|
||||
do
|
||||
{
|
||||
if (recvfrom(sockfd, buffer, packet_size, 0, (struct sockaddr *) &sender, &len) < 0)
|
||||
@ -173,9 +180,11 @@ int main(int ac, char **av)
|
||||
}
|
||||
while (len == packet_size && packet_check(buffer, packet_size) == 0 && packet_compare(packet, buffer, packet_size) == 0);
|
||||
|
||||
double time_interval = ((stop.tv_sec - start.tv_sec) * 1000 + ((double)stop.tv_usec - (double) start.tv_usec) / 1000);
|
||||
stats.packets_received++;
|
||||
|
||||
struct icmphdr *hdr = (struct icmphdr *) packet;
|
||||
printf("%zu bytes from %s (%s): icmp_seq=%d, ttl=%u, time=%fms\n", packet_size, settings.dest.reverse_dns, settings.dest.ipstr, htons(hdr->un.echo.sequence), 116, time_interval);
|
||||
|
||||
print_ping(&settings, hdr, &start, &stop);
|
||||
|
||||
sleep(1);
|
||||
|
||||
@ -186,6 +195,9 @@ int main(int ac, char **av)
|
||||
free(packet);
|
||||
}
|
||||
}
|
||||
|
||||
free(packet);
|
||||
free(buffer);
|
||||
|
||||
print_statistics(&stats, &settings);
|
||||
}
|
||||
46
src/print.c
46
src/print.c
@ -1,7 +1,15 @@
|
||||
|
||||
|
||||
#include "interval.h"
|
||||
#include "setting.h"
|
||||
#include "statistics.h"
|
||||
#include <bits/types/struct_timeval.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/ip.h>
|
||||
#include <netinet/ip_icmp.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
void print_err(const char *format, ...)
|
||||
@ -13,4 +21,42 @@ void print_err(const char *format, ...)
|
||||
vdprintf(2, format, args);
|
||||
va_end(args);
|
||||
write(2, "\n", 1);
|
||||
}
|
||||
|
||||
void print_header(const struct setting *settings)
|
||||
{
|
||||
printf("PING %s (%s) %zu(%zu) bytes of data",
|
||||
settings->dest.hostname,
|
||||
settings->dest.ipstr,
|
||||
settings->payload_size,
|
||||
settings->payload_size + sizeof(struct icmphdr) + sizeof(struct iphdr)
|
||||
);
|
||||
if (settings->verbose)
|
||||
{
|
||||
unsigned short pid = getpid();
|
||||
printf(", id 0x%x = %d", htons(getpid()), pid);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void print_statistics(struct statistics const *stats, struct setting const *settings)
|
||||
{
|
||||
printf("--- %s ping statistics ---\n", settings->dest.hostname);
|
||||
printf("%zu packets transmitted, %zu packets received, %.3f%% packet loss\n",
|
||||
stats->packets_sent,
|
||||
stats->packets_received,
|
||||
((double) stats->packets_sent) * 100 / (double) stats->packets_received
|
||||
);
|
||||
}
|
||||
|
||||
void print_ping(const struct setting *settings, const struct icmphdr *header, const struct timeval *start, const struct timeval *stop)
|
||||
{
|
||||
|
||||
printf("%zu bytes from %s: icmp_seq=%d ttl=%zu time=%.3f ms\n",
|
||||
settings->payload_size + sizeof(struct icmphdr),
|
||||
settings->dest.ipstr,
|
||||
htons(header->un.echo.sequence),
|
||||
settings->ttl,
|
||||
get_interval(start, stop)
|
||||
);
|
||||
}
|
||||
11
src/print.h
11
src/print.h
@ -1,3 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
void print_err(const char *format, ...);
|
||||
#include "setting.h"
|
||||
#include "statistics.h"
|
||||
#include <bits/types/struct_timeval.h>
|
||||
#include <netinet/ip_icmp.h>
|
||||
|
||||
void print_err(const char *format, ...);
|
||||
|
||||
void print_header(struct setting const *settings);
|
||||
void print_statistics(struct statistics const *stats, struct setting const *settings);
|
||||
void print_ping(struct setting const *settings, struct icmphdr const *header, struct timeval const *start, struct timeval const *stop);
|
||||
9
src/statistics.h
Normal file
9
src/statistics.h
Normal file
@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include <stddef.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
struct statistics {
|
||||
size_t packets_sent;
|
||||
size_t packets_received;
|
||||
};
|
||||
Reference in New Issue
Block a user