」 feat: added the verbose mode

This commit is contained in:
2025-08-17 22:30:53 +02:00
parent 3c02e153f7
commit 8c1c83229b
4 changed files with 26 additions and 8 deletions

View File

@ -17,9 +17,9 @@ extern int rx_count;
extern char *address; extern char *address;
extern double *times; extern double *times;
extern int sock; extern int sock;
extern bool verbose;
typedef struct { typedef struct {
bool verbose;
uint32_t count; uint32_t count;
uint16_t size; uint16_t size;
uint16_t timeout; uint16_t timeout;
@ -34,7 +34,6 @@ typedef struct {
#define MAX_WAIT_TIME 5 #define MAX_WAIT_TIME 5
// TODO: implement the verbose mode !!!
#define DEFAULT_VERBOSE false #define DEFAULT_VERBOSE false
#define DEFAULT_SIZE 64 #define DEFAULT_SIZE 64

View File

@ -10,6 +10,7 @@ char *exec_name;
double *times = NULL; double *times = NULL;
int rx_count = 0; int rx_count = 0;
int tx_count = 0; int tx_count = 0;
bool verbose = DEFAULT_VERBOSE;
void init_args_t(args_t *args) { void init_args_t(args_t *args) {
OPT_WHILE { OPT_WHILE {

View File

@ -15,6 +15,10 @@
#include <unistd.h> #include <unistd.h>
int send_icmp(int socket, char *buf, struct sockaddr_in *addr, uint32_t size) { int send_icmp(int socket, char *buf, struct sockaddr_in *addr, uint32_t size) {
if (verbose) {
int seq = ntohs(((struct icmphdr *)buf)->un.echo.sequence);
printf("Sending packet: seq=%d, size=%d bytes\n", seq, size);
}
int bytes_send = sendto(socket, buf, (unsigned long)size, 0, int bytes_send = sendto(socket, buf, (unsigned long)size, 0,
(struct sockaddr *)addr, sizeof(*addr)); (struct sockaddr *)addr, sizeof(*addr));
@ -36,6 +40,17 @@ int receive_icmp(int socket, char *buf, uint32_t size,
bytes = bytes =
recvfrom(socket, buf, size, 0, (struct sockaddr *)addr, &addr_len); recvfrom(socket, buf, size, 0, (struct sockaddr *)addr, &addr_len);
if (verbose) {
printf("Received packet: seq=%d, size=%d bytes, TTL=%d\n", icmp->un.echo.sequence,
bytes - (((struct ip *)buf)->ip_hl << 2), ((struct ip *)buf)->ip_ttl);
// Print raw packet data (hex dump)
printf("Packet data: ");
for (int i = 0; i < bytes; i++) {
printf("%02x ", (unsigned char)buf[i]);
}
printf("\n");
}
return bytes; return bytes;
} }
@ -44,6 +59,7 @@ void process_icmp(char *buf, int bytes, struct sockaddr_in *addr, int seq,
struct ip *ip = (struct ip *)buf; struct ip *ip = (struct ip *)buf;
int len = ip->ip_hl << 2; int len = ip->ip_hl << 2;
if (len < 20) { if (len < 20) {
if (verbose)
fprintf(stderr, "Invalid IP header length\n"); fprintf(stderr, "Invalid IP header length\n");
return; return;
} }

View File

@ -33,7 +33,7 @@ void sigint(int sig) {
options_t init_opt(args_t *args) { options_t init_opt(args_t *args) {
options_t opt; options_t opt;
opt.verbose = args->opts[VERBOSE] != false ? true : DEFAULT_VERBOSE; verbose = args->opts[VERBOSE] != false ? true : DEFAULT_VERBOSE;
opt.count = args->arg[COUNT] != -1 ? args->arg[COUNT] : DEFAULT_COUNT; opt.count = args->arg[COUNT] != -1 ? args->arg[COUNT] : DEFAULT_COUNT;
opt.size = args->arg[SIZE] != -1 ? args->arg[SIZE] : DEFAULT_SIZE; opt.size = args->arg[SIZE] != -1 ? args->arg[SIZE] : DEFAULT_SIZE;
opt.timeout = opt.timeout =
@ -68,11 +68,13 @@ int send_ping(int socket, struct sockaddr_in *dest, options_t opt) {
gettimeofday(&pkt_end, NULL); gettimeofday(&pkt_end, NULL);
if (bytes <= 0) { if (bytes <= 0) {
if (verbose) {
if (errno == EAGAIN || errno == EWOULDBLOCK) { if (errno == EAGAIN || errno == EWOULDBLOCK) {
printf("Request timeout for icmp_seq=%d\n", seq); printf("Request timeout for icmp_seq=%d\n", seq);
} else { } else {
fprintf(stderr, "recv failed: %s\n", strerror(errno)); fprintf(stderr, "recv failed: %s\n", strerror(errno));
} }
}
} else } else
process_icmp(recvbuf, bytes, &addr, seq, &pkt_start, process_icmp(recvbuf, bytes, &addr, seq, &pkt_start,
&pkt_end); &pkt_end);