more stuff
This commit is contained in:
parent
462f8062db
commit
76a06c4fc9
1 changed files with 37 additions and 28 deletions
|
|
@ -11,8 +11,7 @@
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "ping.h"
|
#include "ping.h"
|
||||||
#define TIME_START 0
|
#define TIME_START 0 #define TIME_END 1
|
||||||
#define TIME_END 1
|
|
||||||
#define TIME_TRIPTIME 2
|
#define TIME_TRIPTIME 2
|
||||||
|
|
||||||
t_ping g_ping =
|
t_ping g_ping =
|
||||||
|
|
@ -22,14 +21,35 @@ t_ping g_ping =
|
||||||
.pkt_recv = 0,
|
.pkt_recv = 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
void display(void *buf, int bytes, struct sockaddr_in *addr)
|
int resolve_host(char *node, t_ping *ping)
|
||||||
|
{
|
||||||
|
struct addrinfo *result;
|
||||||
|
struct addrinfo hints;
|
||||||
|
|
||||||
|
memset(&hints, 0, sizeof(hints));
|
||||||
|
hints.ai_family = PF_INET;
|
||||||
|
hints.ai_socktype = SOCK_STREAM;
|
||||||
|
hints.ai_flags |= AI_CANONNAME;
|
||||||
|
if (getaddrinfo(node, NULL, &hints, &result) != 0)
|
||||||
|
{
|
||||||
|
perror("getaddrinfo");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
ping->sa = result;
|
||||||
|
inet_ntop(AF_INET, &(((struct sockaddr_in*)ping->sa->ai_addr)->sin_addr), ping->ip4, INET_ADDRSTRLEN);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void display(void *buf, int bytes, struct sockaddr *addr)
|
||||||
{
|
{
|
||||||
struct ip *ip;
|
struct ip *ip;
|
||||||
struct icmp *icmp;
|
struct icmp *icmp;
|
||||||
struct s_packet *pkt;
|
struct s_packet *pkt;
|
||||||
int hlen;
|
int hlen;
|
||||||
double triptime;
|
double triptime;
|
||||||
char strbuf[INET_ADDRSTRLEN];
|
char hbuf[NI_MAXHOST];
|
||||||
|
char ipbuf[INET_ADDRSTRLEN];
|
||||||
|
|
||||||
ip = buf;
|
ip = buf;
|
||||||
(void)bytes;
|
(void)bytes;
|
||||||
|
|
@ -41,10 +61,17 @@ void display(void *buf, int bytes, struct sockaddr_in *addr)
|
||||||
triptime = time_milli() - *(double*)&pkt->msg;
|
triptime = time_milli() - *(double*)&pkt->msg;
|
||||||
rs_push(&g_ping.rs, triptime);
|
rs_push(&g_ping.rs, triptime);
|
||||||
g_ping.pkt_recv++;
|
g_ping.pkt_recv++;
|
||||||
printf("%d bytes from %s: icmp_seq=%d ttl=%i time=%.3f ms\n",
|
|
||||||
ip->ip_len,
|
bzero(hbuf, NI_MAXHOST);
|
||||||
inet_ntop(AF_INET, &(addr->sin_addr), strbuf, INET_ADDRSTRLEN),
|
bzero(ipbuf, INET_ADDRSTRLEN);
|
||||||
icmp->icmp_seq, ip->ip_ttl, triptime);
|
getnameinfo(addr, sizeof(struct sockaddr_in), ipbuf, INET_ADDRSTRLEN, NULL, 0, NI_NUMERICHOST);
|
||||||
|
if (getnameinfo(addr, sizeof(struct sockaddr_in), hbuf, NI_MAXHOST, NULL, 0, 0) == 0)
|
||||||
|
printf("%d bytes from %s (%s): icmp_seq=%d ttl=%i time=%.1f ms\n",
|
||||||
|
hlen, hbuf, ipbuf, icmp->icmp_seq, ip->ip_ttl, triptime);
|
||||||
|
else
|
||||||
|
printf("%d bytes from %s: icmp_seq=%d ttl=%i time=%.1f ms\n",
|
||||||
|
hlen, ipbuf, icmp->icmp_seq, ip->ip_ttl, triptime);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ping(int signo)
|
void ping(int signo)
|
||||||
|
|
@ -58,10 +85,11 @@ void ping(int signo)
|
||||||
pkt.hdr.icmp_id = g_ping.pid;
|
pkt.hdr.icmp_id = g_ping.pid;
|
||||||
pkt.hdr.icmp_seq = ++g_ping.pkt_sent;
|
pkt.hdr.icmp_seq = ++g_ping.pkt_sent;
|
||||||
epoch = time_milli();
|
epoch = time_milli();
|
||||||
ft_memcpy(pkt.msg, (void*)&epoch, sizeof(epoch));
|
ft_memcpy(pkt.msg, (void*)&epoch, sizeof(double));
|
||||||
pkt.hdr.icmp_cksum = cksum(&pkt, sizeof(pkt));
|
pkt.hdr.icmp_cksum = cksum(&pkt, sizeof(pkt));
|
||||||
sendto(g_ping.sock, &pkt, sizeof(pkt), 0, g_ping.sa->ai_addr,
|
sendto(g_ping.sock, &pkt, sizeof(pkt), 0, g_ping.sa->ai_addr,
|
||||||
sizeof(struct sockaddr));
|
sizeof(struct sockaddr));
|
||||||
|
alarm(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void stats_recap(int signo)
|
void stats_recap(int signo)
|
||||||
|
|
@ -80,25 +108,6 @@ void stats_recap(int signo)
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int resolve_host(char *hostname, t_ping *ping)
|
|
||||||
{
|
|
||||||
struct addrinfo *result;
|
|
||||||
struct addrinfo hints;
|
|
||||||
|
|
||||||
memset(&hints, 0, sizeof(hints));
|
|
||||||
hints.ai_family = PF_INET;
|
|
||||||
hints.ai_socktype = SOCK_STREAM;
|
|
||||||
hints.ai_flags |= AI_CANONNAME;
|
|
||||||
if (getaddrinfo(hostname, NULL, &hints, &result) != 0)
|
|
||||||
{
|
|
||||||
perror("getaddrinfo");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
ping->sa = result;
|
|
||||||
inet_ntop(AF_INET, &(((struct sockaddr_in*)ping->sa->ai_addr)->sin_addr), ping->ip4, INET_ADDRSTRLEN);
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int ac, char **av)
|
int main(int ac, char **av)
|
||||||
{
|
{
|
||||||
if (ac != 2)
|
if (ac != 2)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue