print at end of program
This commit is contained in:
13
ft_ping.h
13
ft_ping.h
@@ -1,18 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <stddef.h>
|
||||
#include <signal.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/ip_icmp.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
@@ -25,10 +21,3 @@ struct icmp_header
|
||||
uint16_t id;
|
||||
uint16_t seq;
|
||||
};
|
||||
|
||||
struct t_socket
|
||||
{
|
||||
struct sockaddr_in dst, src;
|
||||
int fd;
|
||||
int len;
|
||||
};
|
||||
|
||||
23
main.c
23
main.c
@@ -1,5 +1,10 @@
|
||||
#include "ft_ping.h"
|
||||
|
||||
int n_packet_sent = 0;
|
||||
int n_packet_recv = 0;
|
||||
char *ip;
|
||||
char loop = 1;
|
||||
|
||||
uint16_t calculate_checksum(uint16_t *data, int len)
|
||||
{
|
||||
uint32_t checksum = 0;
|
||||
@@ -76,15 +81,23 @@ char *get_ip_by_hostname(char *hostname)
|
||||
return (inet_ntoa(*addr_list[0]));
|
||||
}
|
||||
|
||||
void handler(int code)
|
||||
{
|
||||
(void)code;
|
||||
loop = 0;
|
||||
}
|
||||
|
||||
void init_signal()
|
||||
{
|
||||
signal(SIGINT, handler);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int sock;
|
||||
struct sockaddr_in dst;
|
||||
int seq = 1;
|
||||
char *ip;
|
||||
double start;
|
||||
int n_packet_sent = 0;
|
||||
int n_packet_recv = 0;
|
||||
|
||||
if (argc != 2 || argv[1] == NULL || argv[1][0] == 0)
|
||||
{
|
||||
@@ -105,7 +118,7 @@ int main(int argc, char **argv)
|
||||
fprintf(stderr, "ERROR : socket() failed\n");
|
||||
return (0);
|
||||
}
|
||||
while (1)
|
||||
while (loop)
|
||||
{
|
||||
start = get_timestamp();
|
||||
ft_ping(sock, seq, dst);
|
||||
@@ -113,6 +126,8 @@ int main(int argc, char **argv)
|
||||
seq++;
|
||||
sleep(1);
|
||||
}
|
||||
printf("--- %s ping statistics ---\n", ip);
|
||||
printf("%d packed transmitted %d received, %5.1f%% packet loss", n_packet_sent, n_packet_recv, (double)(n_packet_sent / n_packet_sent) * 100);
|
||||
close(sock);
|
||||
return (0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user