117 lines
2.7 KiB
C
117 lines
2.7 KiB
C
#ifndef FT_PING_H
|
|
# define FT_PING_H
|
|
|
|
#include <stdio.h>
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
#include <sys/time.h>
|
|
#include <unistd.h>
|
|
#include <arpa/inet.h>
|
|
#include <sys/socket.h>
|
|
#include <netdb.h>
|
|
#include <byteswap.h>
|
|
#include <ctype.h>
|
|
#include <stdlib.h>
|
|
#include <errno.h>
|
|
#include <sys/param.h>
|
|
#include <math.h>
|
|
#include <netinet/ip.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#define PACKET_DATA_LENGTH 40
|
|
|
|
#define HELP_MESSAGE " [OPTION...] HOST ...\n\
|
|
Send ICMP ECHO_REQUEST packets to network hosts.\n\
|
|
\n\
|
|
-c, --count=N Stop after sending NUMBER packets\n\
|
|
--ttl=N Specify N as time-to-live\n\
|
|
-i, --interval=N Wait N seconds between sending each packet\n\
|
|
-w, --timeout=N Stop after N seconds\n\
|
|
-W, --linger=N Number of seconds to wait for response\n\
|
|
\n\
|
|
-?, --help Give this help list\n"
|
|
|
|
extern int g_stop;
|
|
|
|
typedef struct s_waitlist
|
|
{
|
|
struct timeval time;
|
|
uint16_t seq;
|
|
struct s_waitlist *next;
|
|
} t_waitlist;
|
|
|
|
typedef struct s_host_list
|
|
{
|
|
char *host;
|
|
struct s_host_list *next;
|
|
} t_host_list;
|
|
|
|
typedef struct s_settings
|
|
{
|
|
t_host_list *hosts;
|
|
int count; // -c (--count)
|
|
int timeout; // -w (--timeout)
|
|
double interval; //-i (--interval)
|
|
int linger; //-W (--linger)
|
|
int ttl; // --ttl
|
|
short setTtl; // is ttl set
|
|
|
|
int socket;
|
|
uint16_t id;
|
|
short stop;
|
|
short err;
|
|
char *name;
|
|
struct timeval last_send_time;
|
|
|
|
char current_ip[INET_ADDRSTRLEN];
|
|
} t_settings;
|
|
|
|
|
|
typedef struct s_icmp_echo
|
|
{
|
|
uint8_t type;
|
|
uint8_t code;
|
|
uint16_t checksum;
|
|
uint16_t identifier;
|
|
uint16_t sequence;
|
|
struct timeval time;
|
|
uint8_t data[40];
|
|
} t_icmp_echo;
|
|
|
|
typedef struct __attribute__((__packed__))
|
|
{
|
|
struct iphdr header;
|
|
t_icmp_echo icmp;
|
|
} t_icmp_ip_reply;
|
|
|
|
typedef struct s_stats
|
|
{
|
|
size_t sent;
|
|
size_t received;
|
|
|
|
double min;
|
|
double avg;
|
|
double max;
|
|
|
|
double sqr_diff;
|
|
double prev;
|
|
} t_stats;
|
|
|
|
t_icmp_echo prepare_icmp_echo(uint16_t sequence, uint16_t identifier);
|
|
int add_host(t_settings *res, char *host);
|
|
void free_hosts(t_host_list *list);
|
|
t_settings parse_args(int argc, char **argv);
|
|
void show_help(t_settings *set, char *name);
|
|
void send_pings(t_settings *set);
|
|
uint16_t calc_checksum(void *ptr, size_t len);
|
|
double timediff(struct timeval *from);
|
|
void waitlist_remove(t_waitlist **lst, uint16_t seq, t_settings *set, t_stats *stats, uint8_t ttl);
|
|
void show_ping_res_line(t_waitlist *cur, t_settings *set, uint16_t seq, t_stats *stats, uint8_t ttl);
|
|
void waitlist_free(t_waitlist *lst);
|
|
t_waitlist *send_icmp(t_settings *set, struct addrinfo *host, uint16_t *seq, struct timeval *last, t_stats *stats);
|
|
void waitlist_add(t_waitlist **waitlist, t_waitlist *elem);
|
|
void receive_icmp(t_settings *set, struct addrinfo *info, t_waitlist **wl, t_stats *stats);
|
|
|
|
#endif
|