add: improved print
This commit is contained in:
8
src/host.h
Normal file
8
src/host.h
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <netinet/in.h>
|
||||||
|
|
||||||
|
struct host {
|
||||||
|
char ip[INET_ADDRSTRLEN];
|
||||||
|
char *hostname;
|
||||||
|
};
|
||||||
15
src/main.c
15
src/main.c
@ -1,3 +1,5 @@
|
|||||||
|
#include "dns.h"
|
||||||
|
#include "host.h"
|
||||||
#include "packet.h"
|
#include "packet.h"
|
||||||
#include "print.h"
|
#include "print.h"
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
@ -32,21 +34,21 @@ int main(int ac, char **av)
|
|||||||
{
|
{
|
||||||
(void) ac;
|
(void) ac;
|
||||||
(void) av;
|
(void) av;
|
||||||
|
struct host hostname;
|
||||||
|
|
||||||
size_t payload_size = 48;
|
size_t payload_size = 48;
|
||||||
size_t packet_size = sizeof(struct icmphdr) + payload_size;
|
size_t packet_size = sizeof(struct icmphdr) + payload_size;
|
||||||
|
|
||||||
signal(SIGINT, signal_handler);
|
signal(SIGINT, signal_handler);
|
||||||
|
|
||||||
char *ipstr = dns_lookup("google.com");
|
if (dns_lookup("localhost", hostname.ip))
|
||||||
if (ipstr == NULL)
|
|
||||||
return 5;
|
return 5;
|
||||||
|
|
||||||
struct sockaddr_in dest;
|
struct sockaddr_in dest;
|
||||||
|
|
||||||
dest.sin_family = AF_INET;
|
dest.sin_family = AF_INET;
|
||||||
dest.sin_port = htons(0);
|
dest.sin_port = htons(0);
|
||||||
inet_pton(AF_INET, ipstr, &dest.sin_addr);
|
inet_pton(AF_INET, hostname.ip, &dest.sin_addr);
|
||||||
free(ipstr);
|
|
||||||
|
|
||||||
int sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
|
int sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
|
||||||
|
|
||||||
@ -102,8 +104,9 @@ int main(int ac, char **av)
|
|||||||
}
|
}
|
||||||
while (len == packet_size && packet_check(buffer, packet_size) == 0 && packet_compare(packet, buffer, packet_size) == 0);
|
while (len == packet_size && packet_check(buffer, packet_size) == 0 && packet_compare(packet, buffer, packet_size) == 0);
|
||||||
|
|
||||||
double time_interval = ((stop.tv_sec - start.tv_sec) * 1000 + (stop.tv_usec - start.tv_usec)) / 1000;
|
double time_interval = ((stop.tv_sec - start.tv_sec) * 1000 + ((double)stop.tv_usec - (double) start.tv_usec) / 1000);
|
||||||
printf("icmp reply received %fms\n", time_interval);
|
struct icmphdr *hdr = (struct icmphdr *) packet;
|
||||||
|
printf("%zu bytes from %p (%s): icmp_seq=%d, ttl=%u, time=%fms\n", payload_size, NULL, hostname.ip, htons(hdr->un.echo.sequence), 116, time_interval);
|
||||||
|
|
||||||
sleep(1);
|
sleep(1);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user