net/Resolver: add simple getaddrinfo() wrapper

This commit is contained in:
Max Kellermann 2021-01-12 14:47:33 +01:00
parent 866c87c65e
commit bcb7e954e9
2 changed files with 29 additions and 26 deletions

View File

@ -49,6 +49,21 @@
#include <stdio.h> #include <stdio.h>
AddressInfoList
Resolve(const char *node, const char *service,
const struct addrinfo *hints)
{
struct addrinfo *ai;
int error = getaddrinfo(node, service, hints, &ai);
if (error != 0)
throw FormatRuntimeError("Failed to resolve '%s':'%s': %s",
node == nullptr ? "" : node,
service == nullptr ? "" : service,
gai_strerror(error));
return AddressInfoList(ai);
}
static inline bool static inline bool
ai_is_passive(const struct addrinfo *ai) ai_is_passive(const struct addrinfo *ai)
{ {
@ -82,10 +97,9 @@ FindAndResolveInterfaceName(char *host, size_t size)
#endif #endif
static int AddressInfoList
Resolve(const char *host_and_port, int default_port, Resolve(const char *host_and_port, int default_port,
const struct addrinfo *hints, const struct addrinfo *hints)
struct addrinfo **ai_r)
{ {
const char *host, *port; const char *host, *port;
char buffer[256], port_string[16]; char buffer[256], port_string[16];
@ -93,16 +107,10 @@ Resolve(const char *host_and_port, int default_port,
if (host_and_port != nullptr) { if (host_and_port != nullptr) {
const auto eh = ExtractHost(host_and_port); const auto eh = ExtractHost(host_and_port);
if (eh.HasFailed()) if (eh.HasFailed())
return EAI_NONAME; throw std::runtime_error("Failed to extract host name");
if (eh.host.size >= sizeof(buffer)) { if (eh.host.size >= sizeof(buffer))
#ifdef _WIN32 throw std::runtime_error("Host name too long");
return EAI_MEMORY;
#else
errno = ENAMETOOLONG;
return EAI_SYSTEM;
#endif
}
memcpy(buffer, eh.host.data, eh.host.size); memcpy(buffer, eh.host.data, eh.host.size);
buffer[eh.host.size] = 0; buffer[eh.host.size] = 0;
@ -131,20 +139,7 @@ Resolve(const char *host_and_port, int default_port,
port = port_string; port = port_string;
} }
return getaddrinfo(host, port, hints, ai_r); return Resolve(host, port, hints);
}
AddressInfoList
Resolve(const char *host_and_port, int default_port,
const struct addrinfo *hints)
{
struct addrinfo *ai;
int result = Resolve(host_and_port, default_port, hints, &ai);
if (result != 0)
throw FormatRuntimeError("Failed to resolve '%s': %s",
host_and_port, gai_strerror(result));
return AddressInfoList(ai);
} }
AddressInfoList AddressInfoList

View File

@ -35,6 +35,14 @@
class AddressInfoList; class AddressInfoList;
/**
* Thin wrapper for getaddrinfo() which throws on error and returns a
* RAII object.
*/
AddressInfoList
Resolve(const char *node, const char *service,
const struct addrinfo *hints);
/** /**
* Resolve the given host name (which may include a port), and fall * Resolve the given host name (which may include a port), and fall
* back to the given default port. * back to the given default port.