add: reverse dns
This commit is contained in:
29
src/dns.c
29
src/dns.c
@ -4,6 +4,8 @@
|
|||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
#include <strings.h>
|
#include <strings.h>
|
||||||
|
|
||||||
int dns_lookup(const char *hostname, char *ipstr)
|
int dns_lookup(const char *hostname, char *ipstr)
|
||||||
@ -30,7 +32,32 @@ int dns_lookup(const char *hostname, char *ipstr)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int dns_reverse_lookup(const char *ipstr, char *hostname);
|
char *dns_reverse_lookup(const char *ipstr)
|
||||||
{
|
{
|
||||||
|
struct sockaddr_in sa;
|
||||||
|
char host[NI_MAXHOST];
|
||||||
|
|
||||||
|
bzero(&sa, sizeof(sa));
|
||||||
|
sa.sin_family = AF_INET;
|
||||||
|
|
||||||
|
printf("%s\n", ipstr);
|
||||||
|
|
||||||
|
if (inet_pton(AF_INET, ipstr, &sa.sin_addr) <= 0)
|
||||||
|
{
|
||||||
|
print_err("inet_pton failed");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
int ret = getnameinfo(
|
||||||
|
(struct sockaddr *)&sa, sizeof(sa),
|
||||||
|
host, sizeof(host),
|
||||||
|
NULL, 0, NI_NAMEREQD);
|
||||||
|
|
||||||
|
if (ret != 0)
|
||||||
|
{
|
||||||
|
print_err("reverse dns failed.");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return strdup(host);
|
||||||
}
|
}
|
||||||
@ -1,4 +1,4 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
int dns_lookup(const char *hostname, char *ipstr);
|
int dns_lookup(const char *hostname, char *ipstr);
|
||||||
int dns_reverse_lookup(const char *ipstr, char *hostname);
|
char *dns_reverse_lookup(const char *ipstr);
|
||||||
@ -5,4 +5,5 @@
|
|||||||
struct host {
|
struct host {
|
||||||
char ip[INET_ADDRSTRLEN];
|
char ip[INET_ADDRSTRLEN];
|
||||||
char *hostname;
|
char *hostname;
|
||||||
|
char *reverse_dns;
|
||||||
};
|
};
|
||||||
17
src/main.c
17
src/main.c
@ -2,6 +2,7 @@
|
|||||||
#include "host.h"
|
#include "host.h"
|
||||||
#include "packet.h"
|
#include "packet.h"
|
||||||
#include "print.h"
|
#include "print.h"
|
||||||
|
#include <netinet/ip.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
@ -21,6 +22,7 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
|
#include <net/ethernet.h>
|
||||||
|
|
||||||
bool loop = true;
|
bool loop = true;
|
||||||
|
|
||||||
@ -34,21 +36,26 @@ int main(int ac, char **av)
|
|||||||
{
|
{
|
||||||
(void) ac;
|
(void) ac;
|
||||||
(void) av;
|
(void) av;
|
||||||
struct host hostname;
|
|
||||||
|
|
||||||
size_t payload_size = 48;
|
struct host host;
|
||||||
|
|
||||||
|
host.hostname = "localhost";
|
||||||
|
|
||||||
|
size_t payload_size = 20;
|
||||||
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);
|
||||||
|
|
||||||
if (dns_lookup("localhost", hostname.ip))
|
if (dns_lookup(host.hostname, host.ip))
|
||||||
return 5;
|
return 5;
|
||||||
|
|
||||||
|
host.reverse_dns = dns_reverse_lookup(host.ip);
|
||||||
|
|
||||||
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, hostname.ip, &dest.sin_addr);
|
inet_pton(AF_INET, host.ip, &dest.sin_addr);
|
||||||
|
|
||||||
int sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
|
int sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
|
||||||
|
|
||||||
@ -106,7 +113,7 @@ int main(int ac, char **av)
|
|||||||
|
|
||||||
double time_interval = ((stop.tv_sec - start.tv_sec) * 1000 + ((double)stop.tv_usec - (double) start.tv_usec) / 1000);
|
double time_interval = ((stop.tv_sec - start.tv_sec) * 1000 + ((double)stop.tv_usec - (double) start.tv_usec) / 1000);
|
||||||
struct icmphdr *hdr = (struct icmphdr *) packet;
|
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);
|
printf("%zu bytes from %s (%s): icmp_seq=%d, ttl=%u, time=%fms\n", payload_size + sizeof(struct icmphdr) + sizeof(struct iphdr) + sizeof(struct ether_header), host.reverse_dns, host.ip, htons(hdr->un.echo.sequence), 116, time_interval);
|
||||||
|
|
||||||
sleep(1);
|
sleep(1);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user