Implement ResolveDNS

This commit is contained in:
2024-04-13 00:56:58 -04:00
parent 879cea30c6
commit cec6132acc
4 changed files with 202 additions and 51 deletions

View File

@@ -1,68 +1,92 @@
//
// Created by dawsh on 4/9/24.
//
#pragma once
#ifndef SOCKETS_DNSRESOLVER_H
#define SOCKETS_DNSRESOLVER_H
#endif //SOCKETS_DNSRESOLVER_H
/// DNS Resolver sample program from ChatGPT
#include <vector>
#include <iostream>
#include <string>
#include <netdb.h>
#include <arpa/inet.h>
#include <cstring>
#include <optional>
int main(int argc, char *argv[])
namespace Sockets
{
if (argc != 2)
/// Internet Protocol Version
/// @see https://www.geeksforgeeks.org/differences-between-ipv4-and-ipv6/
enum class InternetProtocol
{
std::cerr << "Usage: " << argv[0] << " <hostname>" << std::endl;
return 1;
}
IPv4, IPv6
};
const char* hostname = argv[1];
struct addrinfo hints, *res, *p;
int status;
char ipstr[INET6_ADDRSTRLEN];
std::memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if ((status = getaddrinfo(hostname, NULL, &hints, &res)) != 0)
struct Hostname
{
std::cerr << "getaddrinfo: " << gai_strerror(status) << std::endl;
return 2;
}
InternetProtocol ProtocolVersion;
std::string Host;
};
std::cout << "IP addreses for " << hostname << ":" << std::endl;
for (p = res; p != NULL; p = p->ai_next)
struct DNSResult
{
void* addr;
std::string ipver;
bool Success;
std::vector<Hostname> ResolvedHostnames;
};
if (p->ai_family == AF_INET) // IPv4
/// Resolve DNS for a given hostname.
/// This function resolves the DNS for a given hostname and returns found IPaddress strings contained in a DNSResult struct.
static DNSResult ResolveDNS(const std::string& hostname)
{
DNSResult result;
result.ResolvedHostnames = std::vector<Hostname>();
std::vector<std::string> resolved_hostnames;
const char *cstr_hostname = hostname.c_str();
struct addrinfo hints, *res, *p;
int status;
char ipstr[INET6_ADDRSTRLEN];
std::memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if ((status = getaddrinfo(cstr_hostname, NULL, &hints, &res)) != 0)
{
struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
addr = &(ipv4->sin_addr);
ipver = "IPv4";
} else {
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
addr = &(ipv6->sin6_addr);
ipver = "IPv6";
std::cerr << "getaddrinfo: " << gai_strerror(status) << std::endl;
result.Success = false;
return result;
}
// Convert the IP to a string and print it
inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
std::cout << " " << ipver << ": " << ipstr << std::endl;
std::cout << "IP addreses for " << hostname << ":" << std::endl;
for (p = res; p != NULL; p = p->ai_next)
{
void* addr;
std::string ipver;
Hostname hostdata;
if (p->ai_family == AF_INET) // IPv4
{
struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
addr = &(ipv4->sin_addr);
hostdata.ProtocolVersion = InternetProtocol::IPv4;
} else {
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
addr = &(ipv6->sin6_addr);
hostdata.ProtocolVersion = InternetProtocol::IPv6;
}
// Convert the IP to a string and print it
inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
hostdata.Host = ipstr;
result.ResolvedHostnames.push_back(hostdata);
}
freeaddrinfo(res); // free the linked list
return result;
}
freeaddrinfo(res); // free the linked list
return 0;
}
// Compile this code and run it with a hostname as an argument, like so:
// ./dns_resolver www.google.com
// This program will resolve the IP addresses associated with the given hostname and print them to the console.
}

View File

@@ -1,3 +1,4 @@
/*
#include <Sockets/TcpSocket.hpp>
#include <iostream>
@@ -71,3 +72,64 @@ int main(int argc, char *argv[])
UDP_Roundtrip();
return 0;
}
*/
/// DNS Resolver sample program from ChatGPT
#include <iostream>
#include <netdb.h>
#include <arpa/inet.h>
#include <cstring>
int main(int argc, char *argv[])
{
if (argc != 2)
{
std::cerr << "Usage: " << argv[0] << " <hostname>" << std::endl;
return 1;
}
const char* hostname = argv[1];
struct addrinfo hints, *res, *p;
int status;
char ipstr[INET6_ADDRSTRLEN];
std::memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if ((status = getaddrinfo(hostname, NULL, &hints, &res)) != 0)
{
std::cerr << "getaddrinfo: " << gai_strerror(status) << std::endl;
return 2;
}
std::cout << "IP addreses for " << hostname << ":" << std::endl;
for (p = res; p != NULL; p = p->ai_next)
{
void* addr;
std::string ipver;
if (p->ai_family == AF_INET) // IPv4
{
struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
addr = &(ipv4->sin_addr);
ipver = "IPv4";
} else {
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
addr = &(ipv6->sin6_addr);
ipver = "IPv6";
}
// Convert the IP to a string and print it
inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
std::cout << " " << ipver << ": " << ipstr << std::endl;
}
freeaddrinfo(res); // free the linked list
return 0;
}
// Compile this code and run it with a hostname as an argument, like so:
// ./dns_resolver www.google.com
// This program will resolve the IP addresses associated with the given hostname and print them to the console.

View File

@@ -0,0 +1,59 @@
/// DNS Resolver sample program from ChatGPT
#include <iostream>
#include <netdb.h>
#include <arpa/inet.h>
#include <cstring>
int main(int argc, char *argv[])
{
if (argc != 2)
{
std::cerr << "Usage: " << argv[0] << " <hostname>" << std::endl;
return 1;
}
const char* hostname = argv[1];
struct addrinfo hints, *res, *p;
int status;
char ipstr[INET6_ADDRSTRLEN];
std::memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if ((status = getaddrinfo(hostname, NULL, &hints, &res)) != 0)
{
std::cerr << "getaddrinfo: " << gai_strerror(status) << std::endl;
return 2;
}
std::cout << "IP addreses for " << hostname << ":" << std::endl;
for (p = res; p != NULL; p = p->ai_next)
{
void* addr;
std::string ipver;
if (p->ai_family == AF_INET) // IPv4
{
struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
addr = &(ipv4->sin_addr);
ipver = "IPv4";
} else {
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
addr = &(ipv6->sin6_addr);
ipver = "IPv6";
}
// Convert the IP to a string and print it
inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
std::cout << " " << ipver << ": " << ipstr << std::endl;
}
freeaddrinfo(res); // free the linked list
return 0;
}
// Compile this code and run it with a hostname as an argument, like so:
// ./dns_resolver www.google.com
// This program will resolve the IP addresses associated with the given hostname and print them to the console.

View File

@@ -0,0 +1,6 @@
#include <Sockets/DNSResolver.hpp>
namespace Sockets
{
}