「🔨」 fix: fixed some things.
This commit is contained in:
@ -17,6 +17,7 @@ extern int rx_count;
|
|||||||
extern char *address;
|
extern char *address;
|
||||||
extern double *times;
|
extern double *times;
|
||||||
extern int sock;
|
extern int sock;
|
||||||
|
extern char **hosts;
|
||||||
extern bool verbose;
|
extern bool verbose;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
@ -11,6 +11,7 @@ double *times = NULL;
|
|||||||
int rx_count = 0;
|
int rx_count = 0;
|
||||||
int tx_count = 0;
|
int tx_count = 0;
|
||||||
bool verbose = DEFAULT_VERBOSE;
|
bool verbose = DEFAULT_VERBOSE;
|
||||||
|
char **hosts;
|
||||||
|
|
||||||
void init_args_t(args_t *args) {
|
void init_args_t(args_t *args) {
|
||||||
OPT_WHILE {
|
OPT_WHILE {
|
||||||
|
@ -28,11 +28,15 @@ void sigint(int sig) {
|
|||||||
close(sock);
|
close(sock);
|
||||||
print_stats();
|
print_stats();
|
||||||
|
|
||||||
|
// free(times);
|
||||||
|
// free(hosts);
|
||||||
|
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
options_t init_opt(args_t *args) {
|
options_t init_opt(args_t *args) {
|
||||||
options_t opt;
|
options_t opt;
|
||||||
|
hosts = args->hosts;
|
||||||
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;
|
||||||
if (args->arg[SIZE] > sizeof(struct icmphdr) || args->arg[SIZE] == -1) {
|
if (args->arg[SIZE] > sizeof(struct icmphdr) || args->arg[SIZE] == -1) {
|
||||||
@ -84,12 +88,16 @@ int send_ping(int socket, struct sockaddr_in *dest, options_t opt) {
|
|||||||
process_icmp(recvbuf, bytes, &addr, seq, &pkt_start, &pkt_end);
|
process_icmp(recvbuf, bytes, &addr, seq, &pkt_start, &pkt_end);
|
||||||
if (check_for_count(opt)) {
|
if (check_for_count(opt)) {
|
||||||
print_stats();
|
print_stats();
|
||||||
|
free(times);
|
||||||
|
times = NULL;
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
sleep(opt.interval);
|
sleep(opt.interval);
|
||||||
if (check_for_timeout(loop_start, opt)) {
|
if (check_for_timeout(loop_start, opt)) {
|
||||||
print_stats();
|
print_stats();
|
||||||
|
free(times);
|
||||||
|
times = NULL;
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -100,35 +108,32 @@ int ping(args_t *args) {
|
|||||||
int ret;
|
int ret;
|
||||||
options_t opt = init_opt(args);
|
options_t opt = init_opt(args);
|
||||||
signal(SIGINT, &sigint);
|
signal(SIGINT, &sigint);
|
||||||
for (int i = 0; args->hosts[i] != NULL; i++) {
|
address = args->hosts[0];
|
||||||
address = args->hosts[i];
|
struct sockaddr_in addr;
|
||||||
struct sockaddr_in addr;
|
struct addrinfo hints, *res;
|
||||||
struct addrinfo hints, *res;
|
|
||||||
|
|
||||||
memset(&hints, 0, sizeof(hints));
|
memset(&hints, 0, sizeof(hints));
|
||||||
hints.ai_family = AF_INET;
|
hints.ai_family = AF_INET;
|
||||||
hints.ai_socktype = SOCK_RAW;
|
hints.ai_socktype = SOCK_RAW;
|
||||||
hints.ai_protocol = IPPROTO_ICMP;
|
hints.ai_protocol = IPPROTO_ICMP;
|
||||||
|
|
||||||
int status = getaddrinfo(address, NULL, &hints, &res);
|
int status = getaddrinfo(address, NULL, &hints, &res);
|
||||||
if (status != 0) {
|
if (status != 0) {
|
||||||
fprintf(stderr, "Bad address: %s\n", address);
|
fprintf(stderr, "Bad address: %s\n", address);
|
||||||
ret = EXIT_FAILURE;
|
ret = EXIT_FAILURE;
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
memcpy(&addr, res->ai_addr, res->ai_addrlen);
|
|
||||||
freeaddrinfo(res);
|
|
||||||
|
|
||||||
sock = init_socket(opt);
|
|
||||||
if (sock < 0) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("PING %s (%s): %d data bytes\n", address,
|
|
||||||
inet_ntoa(addr.sin_addr), opt.size);
|
|
||||||
|
|
||||||
ret = send_ping(sock, &addr, opt);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
memcpy(&addr, res->ai_addr, res->ai_addrlen);
|
||||||
|
freeaddrinfo(res);
|
||||||
|
|
||||||
|
sock = init_socket(opt);
|
||||||
|
if (sock < 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("PING %s (%s): %d data bytes\n", address, inet_ntoa(addr.sin_addr),
|
||||||
|
opt.size);
|
||||||
|
|
||||||
|
ret = send_ping(sock, &addr, opt);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
@ -61,8 +61,9 @@ bool check_for_timeout(struct timeval start, options_t opt) {
|
|||||||
|
|
||||||
double span =
|
double span =
|
||||||
(end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec) / 1000000.0;
|
(end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec) / 1000000.0;
|
||||||
if (span > opt.timeout)
|
if (span > opt.timeout) {
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,7 +78,8 @@ void print_stats(void) {
|
|||||||
bool check_for_count(options_t opt) {
|
bool check_for_count(options_t opt) {
|
||||||
if (opt.count == -1)
|
if (opt.count == -1)
|
||||||
return false;
|
return false;
|
||||||
if (tx_count >= opt.count)
|
if (tx_count >= opt.count) {
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user