-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added DNS resolution for host addresses
- Loading branch information
Showing
3 changed files
with
77 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#include "address_utils.h" | ||
|
||
#include "../util/logger.h" | ||
|
||
namespace Ardos { | ||
|
||
std::string AddressUtils::resolve_host(const std::shared_ptr<uvw::loop> &loop, | ||
const std::string &host, | ||
unsigned int port) { | ||
// First, test if we have a valid IPv4 address. | ||
sockaddr_in sockaddr; | ||
if (uv_ip4_addr(host.c_str(), port, &sockaddr) == 0) { | ||
return host; | ||
} | ||
|
||
// Next, test if we have a valid IPv6 address. | ||
sockaddr_in6 sockaddr6; | ||
if (uv_ip6_addr(host.c_str(), port, &sockaddr6) == 0) { | ||
return host; | ||
} | ||
|
||
// We don't, lets try to resolve an IP via DNS. | ||
auto dnsRequest = loop->resource<uvw::get_addr_info_req>(); | ||
auto dnsResult = dnsRequest->addr_info_sync(host, std::to_string(port)); | ||
if (!dnsResult.first) { | ||
Logger::Error( | ||
std::format("[NET] Failed to resolve host address: {}:{}", host, port)); | ||
exit(1); | ||
} | ||
|
||
char addr[INET6_ADDRSTRLEN]; | ||
if (inet_ntop(dnsResult.second->ai_family, | ||
get_in_addr(dnsResult.second->ai_addr), addr, | ||
dnsResult.second->ai_addrlen)) { | ||
return std::string(addr); | ||
} | ||
|
||
Logger::Error( | ||
std::format("[NET] Failed to parse host address: {}:{}", host, port)); | ||
exit(1); | ||
} | ||
|
||
void *AddressUtils::get_in_addr(struct sockaddr *sa) { | ||
if (sa->sa_family == AF_INET) { | ||
return &(((struct sockaddr_in *)sa)->sin_addr); | ||
} | ||
|
||
return &(((struct sockaddr_in6 *)sa)->sin6_addr); | ||
} | ||
|
||
} // namespace Ardos |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#ifndef ARDOS_ADDRESS_UTILS_H | ||
#define ARDOS_ADDRESS_UTILS_H | ||
|
||
#include <memory> | ||
#include <string> | ||
|
||
#include <uvw.hpp> | ||
|
||
namespace Ardos { | ||
|
||
class AddressUtils { | ||
public: | ||
static std::string resolve_host(const std::shared_ptr<uvw::loop> &loop, | ||
const std::string &host, | ||
unsigned int port = 0); | ||
|
||
private: | ||
static void *get_in_addr(struct sockaddr *sa); | ||
}; | ||
|
||
} // namespace Ardos | ||
|
||
#endif // ARDOS_ADDRESS_UTILS_H |