FQDN done
This commit is contained in:
38
srcs/main.c
38
srcs/main.c
@ -63,7 +63,7 @@ static bool stop = false;
|
|||||||
|
|
||||||
int sock = 0;
|
int sock = 0;
|
||||||
|
|
||||||
static struct sockaddr_in destination;
|
static struct sockaddr_in destination = {0};
|
||||||
|
|
||||||
struct timespec sendt;
|
struct timespec sendt;
|
||||||
|
|
||||||
@ -146,25 +146,41 @@ int main(int argc, char** argv) {
|
|||||||
struct arguments arguments;
|
struct arguments arguments;
|
||||||
int timeout = 3000;
|
int timeout = 3000;
|
||||||
struct timespec recvt;
|
struct timespec recvt;
|
||||||
|
struct addrinfo hints = {0};
|
||||||
|
struct addrinfo *res = NULL;
|
||||||
struct pollfd fds[1];
|
struct pollfd fds[1];
|
||||||
double min = INT32_MAX;
|
double min = INT32_MAX;
|
||||||
double max = 0;
|
double max = 0;
|
||||||
double avg = 0;
|
double avg = 0;
|
||||||
double stddev = 0;
|
double stddev = 0;
|
||||||
|
|
||||||
|
hints.ai_family = AF_INET;
|
||||||
|
hints.ai_socktype = SOCK_STREAM;
|
||||||
|
|
||||||
arguments.verbose = 0;
|
arguments.verbose = 0;
|
||||||
arguments.ttl = 64;
|
arguments.ttl = 64;
|
||||||
argp_parse(&argp, argc, argv, 0, 0, &arguments);
|
argp_parse(&argp, argc, argv, 0, 0, &arguments);
|
||||||
|
|
||||||
memset(&destination, 0, sizeof(destination));
|
// memset(&destination, 0, sizeof(destination));
|
||||||
|
|
||||||
destination.sin_family = AF_INET;
|
char ip_str[INET_ADDRSTRLEN] = {0};
|
||||||
destination.sin_addr.s_addr = inet_addr(arguments.args[0]);
|
|
||||||
if (destination.sin_addr.s_addr == INADDR_NONE) {
|
int status = getaddrinfo(arguments.args[0], NULL, &hints, &res);
|
||||||
dprintf(sock, "%s: unknown host\n", argv[0]);
|
if (status != 0) {
|
||||||
exit(1);
|
destination.sin_addr.s_addr = inet_addr(arguments.args[0]);
|
||||||
|
if (destination.sin_addr.s_addr == INADDR_NONE) {
|
||||||
|
dprintf(STDERR_FILENO, "%s: unknown host\n", argv[0]);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
destination.sin_family = AF_INET;
|
||||||
|
destination.sin_port = htons(IPPORT_ECHO);
|
||||||
|
memcpy(ip_str, arguments.args[0], INET_ADDRSTRLEN);
|
||||||
|
} else {
|
||||||
|
memcpy(&destination, res->ai_addr, sizeof(struct sockaddr_in));
|
||||||
|
destination.sin_family = AF_INET;
|
||||||
|
destination.sin_port = htons(IPPORT_ECHO);
|
||||||
|
inet_ntop(res->ai_family, &destination.sin_addr, ip_str, INET_ADDRSTRLEN);
|
||||||
}
|
}
|
||||||
destination.sin_port = htons(IPPORT_ECHO);
|
|
||||||
|
|
||||||
sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
|
sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
|
||||||
|
|
||||||
@ -189,7 +205,7 @@ int main(int argc, char** argv) {
|
|||||||
sigaction(SIGINT, &sig_handle, 0);
|
sigaction(SIGINT, &sig_handle, 0);
|
||||||
sigaction(SIGALRM, &sig_handle, 0);
|
sigaction(SIGALRM, &sig_handle, 0);
|
||||||
|
|
||||||
printf("PING %s (%s): %ld data bytes", arguments.args[0], arguments.args[0], 64 - sizeof(struct icmphdr));
|
printf("PING %s (%s): %ld data bytes", arguments.args[0], ip_str, 64 - sizeof(struct icmphdr));
|
||||||
if (arguments.verbose) {
|
if (arguments.verbose) {
|
||||||
printf(", id 0x%04x = %u", getpid(), getpid());
|
printf(", id 0x%04x = %u", getpid(), getpid());
|
||||||
}
|
}
|
||||||
@ -225,7 +241,7 @@ int main(int argc, char** argv) {
|
|||||||
icmp_reply->checksum = 0;
|
icmp_reply->checksum = 0;
|
||||||
|
|
||||||
if (old_checksum != calc_checksum(icmp_reply, bytes_received - (ip_header->ip_hl << 2))) {
|
if (old_checksum != calc_checksum(icmp_reply, bytes_received - (ip_header->ip_hl << 2))) {
|
||||||
dprintf(STDERR_FILENO, "checksum mismatch from %s\n", arguments.args[0]);
|
dprintf(STDERR_FILENO, "checksum mismatch from %s\n", ip_str);
|
||||||
rept++;
|
rept++;
|
||||||
pause();
|
pause();
|
||||||
continue;
|
continue;
|
||||||
@ -238,7 +254,7 @@ int main(int argc, char** argv) {
|
|||||||
tmp += ((double)((recvt.tv_nsec - sendt.tv_nsec) % 1000000) / 1000000.0);
|
tmp += ((double)((recvt.tv_nsec - sendt.tv_nsec) % 1000000) / 1000000.0);
|
||||||
|
|
||||||
printf("%zd bytes from %s: icmp_seq=%d ttl=%d time=%.3f ms \n", bytes_received - sizeof(struct ip),
|
printf("%zd bytes from %s: icmp_seq=%d ttl=%d time=%.3f ms \n", bytes_received - sizeof(struct ip),
|
||||||
arguments.args[0], icmp_reply->un.echo.sequence, ip_header->ip_ttl, tmp);
|
ip_str, icmp_reply->un.echo.sequence, ip_header->ip_ttl, tmp);
|
||||||
|
|
||||||
min = (((min) < (tmp)) ? (min) : (tmp));
|
min = (((min) < (tmp)) ? (min) : (tmp));
|
||||||
max = (((max) > (tmp)) ? (max) : (tmp));
|
max = (((max) > (tmp)) ? (max) : (tmp));
|
||||||
|
Reference in New Issue
Block a user