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
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 800f93e..b0e2f52 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -9,7 +9,7 @@
-
+
{
"useNewFormat": true
@@ -44,7 +44,7 @@
-
+
@@ -58,6 +58,9 @@
+
+
+
@@ -108,7 +111,7 @@
-
+
@@ -121,7 +124,7 @@
-
+
@@ -146,11 +149,6 @@
-
-
-
-
-
@@ -200,6 +198,9 @@
+
+
+
diff --git a/includes/main.h b/includes/main.h
index c53c9c7..e769fe2 100644
--- a/includes/main.h
+++ b/includes/main.h
@@ -12,6 +12,7 @@
#include
#include
#include
+#include
struct arguments {
char* args[1];
@@ -19,4 +20,9 @@ struct arguments {
unsigned int ttl;
};
+struct ping_pkt {
+ struct icmphdr hdr;
+ char msg[64 - sizeof(struct icmphdr)];
+};
+
#endif //MAIN_H
diff --git a/srcs/main.c b/srcs/main.c
index 99e0c83..7dbc7d4 100644
--- a/srcs/main.c
+++ b/srcs/main.c
@@ -10,9 +10,9 @@ static char args_doc[] = "HOST ...";
#define ARG_TTL 256
static struct argp_option options[] = {
- {0, 0, 0, 0, "Options valid for all request types:\n"},
- {"verbose", 'v', 0, 0, "verbose output"},
- {"ttl", ARG_TTL, "N", 0, "specify N as time-to-live"},
+ {0, 0, 0, 0, "Options valid for all request types:\n", 0},
+ {"verbose", 'v', 0, 0, "verbose output", 0},
+ {"ttl", ARG_TTL, "N", 0, "specify N as time-to-live", 0},
{0},
};
@@ -46,7 +46,42 @@ static bool stop = false;
int sock = 0;
-void send_ping() {}
+static struct sockaddr_in destination;
+
+unsigned long xmit = 0;
+unsigned long recvd = 0;
+
+double min = 0;
+double max = 0;
+double avg = 0;
+
+uint16_t calc_checksum(void * b, const size_t l) {
+ uint16_t sum = 0;
+
+ for (int i = 0; i < l; i++) {
+ sum += *((uint16_t*)b + i);
+ }
+
+ return ~sum;
+}
+
+void send_ping() {
+ struct ping_pkt packet = {0};
+
+ packet.hdr.type = ICMP_ECHO;
+ packet.hdr.un.echo.id = getpid();
+ packet.hdr.un.echo.sequence = xmit++;
+
+ for (int i = 0; i < sizeof(packet.msg) - 1; ++i) {
+ packet.msg[i] = (char)i;
+ }
+ packet.msg[sizeof(packet.msg) - 1] = '\0';
+
+ packet.hdr.checksum = calc_checksum(&packet, sizeof(packet));
+
+ sendto(sock, &packet, sizeof(packet), 0, (struct sockaddr*)&destination, sizeof(destination));
+ alarm(1);
+}
void sig_handler(int sig) {
if (sig == SIGINT) {
@@ -62,6 +97,16 @@ int main(int argc, char** argv) {
arguments.ttl = 64;
argp_parse(&argp, argc, argv, 0, 0, &arguments);
+ memset(&destination, 0, sizeof(destination));
+
+ destination.sin_family = AF_INET;
+ destination.sin_addr.s_addr = inet_addr(arguments.args[0]);
+ if (destination.sin_addr.s_addr == INADDR_NONE) {
+ dprintf(sock, "%s: unknown host\n", argv[0]);
+ exit(1);
+ }
+ destination.sin_port = htons(IPPORT_ECHO);
+
sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if (sock < 0) {
@@ -74,7 +119,7 @@ int main(int argc, char** argv) {
close(sock);
exit(1);
}
-
+
struct sigaction sig_handle;
sig_handle.sa_handler = sig_handler;
@@ -82,10 +127,7 @@ int main(int argc, char** argv) {
sigaction(SIGINT, &sig_handle, 0);
sigaction(SIGALRM, &sig_handle, 0);
- // alarm(1);
-
- unsigned long xmit = 0;
- unsigned long recv = 0;
+ alarm(1);
while (true) {
@@ -95,7 +137,7 @@ int main(int argc, char** argv) {
}
printf("---- %s ping statistics ----\n", arguments.args[0]);
printf("%zu packets transmitted, %zu packets received, %%zu%% packet loss\n", xmit,
- recv /*, (xmit - recv) / xmit * 100*/);
+ recvd /*, (xmit - recv) / xmit * 100*/);
// printf("round-trip min/avg/max/stddev = %f/%f/%f/%f ms\n");
close(sock);
}
\ No newline at end of file