「✨」 feat: added the verbose mode
This commit is contained in:
@ -17,9 +17,9 @@ extern int rx_count;
|
||||
extern char *address;
|
||||
extern double *times;
|
||||
extern int sock;
|
||||
extern bool verbose;
|
||||
|
||||
typedef struct {
|
||||
bool verbose;
|
||||
uint32_t count;
|
||||
uint16_t size;
|
||||
uint16_t timeout;
|
||||
@ -34,7 +34,6 @@ typedef struct {
|
||||
|
||||
#define MAX_WAIT_TIME 5
|
||||
|
||||
// TODO: implement the verbose mode !!!
|
||||
#define DEFAULT_VERBOSE false
|
||||
|
||||
#define DEFAULT_SIZE 64
|
||||
|
@ -10,6 +10,7 @@ char *exec_name;
|
||||
double *times = NULL;
|
||||
int rx_count = 0;
|
||||
int tx_count = 0;
|
||||
bool verbose = DEFAULT_VERBOSE;
|
||||
|
||||
void init_args_t(args_t *args) {
|
||||
OPT_WHILE {
|
||||
|
@ -15,6 +15,10 @@
|
||||
#include <unistd.h>
|
||||
|
||||
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,
|
||||
(struct sockaddr *)addr, sizeof(*addr));
|
||||
|
||||
@ -36,6 +40,17 @@ int receive_icmp(int socket, char *buf, uint32_t size,
|
||||
bytes =
|
||||
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;
|
||||
}
|
||||
|
||||
@ -44,7 +59,8 @@ void process_icmp(char *buf, int bytes, struct sockaddr_in *addr, int seq,
|
||||
struct ip *ip = (struct ip *)buf;
|
||||
int len = ip->ip_hl << 2;
|
||||
if (len < 20) {
|
||||
fprintf(stderr, "Invalid IP header length\n");
|
||||
if (verbose)
|
||||
fprintf(stderr, "Invalid IP header length\n");
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -33,7 +33,7 @@ void sigint(int sig) {
|
||||
|
||||
options_t init_opt(args_t *args) {
|
||||
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.size = args->arg[SIZE] != -1 ? args->arg[SIZE] : DEFAULT_SIZE;
|
||||
opt.timeout =
|
||||
@ -68,10 +68,12 @@ int send_ping(int socket, struct sockaddr_in *dest, options_t opt) {
|
||||
gettimeofday(&pkt_end, NULL);
|
||||
|
||||
if (bytes <= 0) {
|
||||
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
||||
printf("Request timeout for icmp_seq=%d\n", seq);
|
||||
} else {
|
||||
fprintf(stderr, "recv failed: %s\n", strerror(errno));
|
||||
if (verbose) {
|
||||
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
||||
printf("Request timeout for icmp_seq=%d\n", seq);
|
||||
} else {
|
||||
fprintf(stderr, "recv failed: %s\n", strerror(errno));
|
||||
}
|
||||
}
|
||||
} else
|
||||
process_icmp(recvbuf, bytes, &addr, seq, &pkt_start,
|
||||
|
Reference in New Issue
Block a user