add: print
This commit is contained in:
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)
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user