add: dns lookup
This commit is contained in:
51
src/main.c
51
src/main.c
@ -4,6 +4,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <strings.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
@ -14,6 +15,8 @@
|
|||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <netdb.h>
|
||||||
|
|
||||||
bool loop = true;
|
bool loop = true;
|
||||||
|
|
||||||
@ -144,6 +147,36 @@ void signal_handler(int code)
|
|||||||
loop = false;
|
loop = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char *dns_lookup(const char *hostname)
|
||||||
|
{
|
||||||
|
printf("%s\n",hostname);
|
||||||
|
char *ipstr = malloc(sizeof(char) * INET_ADDRSTRLEN);
|
||||||
|
if (ipstr == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
struct addrinfo filter;
|
||||||
|
|
||||||
|
bzero(&filter, sizeof(filter));
|
||||||
|
filter.ai_family = AF_INET;
|
||||||
|
|
||||||
|
struct addrinfo *responses;
|
||||||
|
if (getaddrinfo(hostname, NULL, &filter, &responses) != 0)
|
||||||
|
{
|
||||||
|
print_err("dns lookup failed");
|
||||||
|
free(ipstr);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct addrinfo *first = responses;
|
||||||
|
struct in_addr *addr = &(((struct sockaddr_in *) responses->ai_addr)->sin_addr);
|
||||||
|
|
||||||
|
inet_ntop(first->ai_family, addr, ipstr, sizeof(char) * INET_ADDRSTRLEN);
|
||||||
|
|
||||||
|
freeaddrinfo(responses);
|
||||||
|
|
||||||
|
return ipstr;
|
||||||
|
}
|
||||||
|
|
||||||
int main(int ac, char **av)
|
int main(int ac, char **av)
|
||||||
{
|
{
|
||||||
(void) ac;
|
(void) ac;
|
||||||
@ -153,6 +186,17 @@ int main(int ac, char **av)
|
|||||||
|
|
||||||
signal(SIGINT, signal_handler);
|
signal(SIGINT, signal_handler);
|
||||||
|
|
||||||
|
char *ipstr = dns_lookup("google.com");
|
||||||
|
if (ipstr == NULL)
|
||||||
|
return 5;
|
||||||
|
|
||||||
|
struct sockaddr_in dest;
|
||||||
|
|
||||||
|
dest.sin_family = AF_INET;
|
||||||
|
dest.sin_port = htons(0);
|
||||||
|
inet_pton(AF_INET, ipstr, &dest.sin_addr);
|
||||||
|
free(ipstr);
|
||||||
|
|
||||||
int sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
|
int sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
|
||||||
|
|
||||||
if (sockfd == -1)
|
if (sockfd == -1)
|
||||||
@ -161,12 +205,6 @@ int main(int ac, char **av)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct sockaddr_in dest;
|
|
||||||
|
|
||||||
dest.sin_family = AF_INET;
|
|
||||||
dest.sin_port = htons(0);
|
|
||||||
inet_pton(AF_INET, "8.8.8.8", &dest.sin_addr);
|
|
||||||
|
|
||||||
char *packet = create_packet(payload_size);
|
char *packet = create_packet(payload_size);
|
||||||
if (packet == NULL)
|
if (packet == NULL)
|
||||||
{
|
{
|
||||||
@ -198,7 +236,6 @@ int main(int ac, char **av)
|
|||||||
close(sockfd);
|
close(sockfd);
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
printf("icmp request sent\n");
|
|
||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user