diff --git a/Makefile b/Makefile index 04bb494..36bef0e 100644 --- a/Makefile +++ b/Makefile @@ -24,7 +24,9 @@ SUBDIRECTORIES = # -- Make Recipes -- # DO NOT TOUCH BELOW UNLESS YOU KNOW WHAT YOU ARE DOING :) -all: $(NAME) +all: $(BUILD_DIR)/$(NAME) + +install: $(NAME) re: fclean all diff --git a/srcs/main.c b/srcs/main.c index 0d02638..8226498 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -147,7 +147,7 @@ int main(int argc, char** argv) { int timeout = 3000; struct timespec recvt; struct addrinfo hints = {0}; - struct addrinfo *res = NULL; + struct addrinfo* res = NULL; struct pollfd fds[1]; double min = INT32_MAX; double max = 0; @@ -182,10 +182,20 @@ int main(int argc, char** argv) { inet_ntop(res->ai_family, &destination.sin_addr, ip_str, INET_ADDRSTRLEN); } - sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); + struct protoent *proto = getprotobyname("icmp"); + if (!proto) { + dprintf (STDERR_FILENO, "%s: unknown protocol icmp.\n", argv[0]); + exit(1); + } + + sock = socket(AF_INET, SOCK_RAW, proto->p_proto); if (sock < 0) { - dprintf(STDERR_FILENO, "%s: socket() failed: %s\n", argv[0], strerror(errno)); + if (errno == EPERM || errno == EACCES || errno == EPROTONOSUPPORT) { + dprintf(STDERR_FILENO, "%s: Lacking privilege for icmp socket.\n", argv[0]); + } else { + dprintf(STDERR_FILENO, "%s: %s\n", argv[0], strerror(errno)); + } exit(1); } @@ -268,10 +278,9 @@ int main(int argc, char** argv) { } } else if (ret == 0) { printf("No reply received within the timeout period\n"); + } else { + perror("Poll failed"); } - // else { - // perror("Poll failed"); - // } pause(); } @@ -281,7 +290,13 @@ int main(int argc, char** argv) { if (rept) { printf("+%zu duplicates, ", rept); } - printf("%zu%% packet loss\n", (xmit - recvd) / xmit * 100); + if (xmit) { + if (recvd > xmit) { + printf("-- somebody is printing forged packets!\n"); + } else { + printf("%zu%% packet loss\n", (xmit - recvd) / xmit * 100); + } + } double total = recvd + rept; avg /= total; stddev = stddev / total - avg * avg; diff --git a/srcs/util.c b/srcs/util.c deleted file mode 100644 index 83851f6..0000000 --- a/srcs/util.c +++ /dev/null @@ -1,5 +0,0 @@ -// -// Created by jrathelo on 2/24/25. -// - -#include