Commit bf7fb0ba authored by Joseph Noir's avatar Joseph Noir

Clean up allocation and error handling

parent 74de7157
...@@ -150,20 +150,19 @@ std::string hostname() { ...@@ -150,20 +150,19 @@ std::string hostname() {
std::vector<ip_address> local_addresses(string_view host) { std::vector<ip_address> local_addresses(string_view host) {
using adapters_ptr = std::unique_ptr<IP_ADAPTER_ADDRESSES, void (*)(void*)>; using adapters_ptr = std::unique_ptr<IP_ADAPTER_ADDRESSES, void (*)(void*)>;
using wbuf_ptr = std::unique_ptr<IP_ADAPTER_ADDRESSES, void (*)(void*)>;
if (host == v4_any_addr) if (host == v4_any_addr)
return {ip_address{make_ipv4_address(0, 0, 0, 0)}}; return {ip_address{make_ipv4_address(0, 0, 0, 0)}};
if (host == v6_any_addr) if (host == v6_any_addr)
return {ip_address{}}; return {ip_address{}};
ULONG len = 0; ULONG len = 0;
auto err = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, nullptr, if (GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, nullptr, nullptr,
nullptr, &len); &len)
if (err != ERROR_BUFFER_OVERFLOW) { != ERROR_BUFFER_OVERFLOW) {
CAF_LOG_ERROR("failed to get adapter addresses buffer length"); CAF_LOG_ERROR("failed to get adapter addresses buffer length");
return {}; return {};
} }
auto adapters = adapters_ptr{reinterpret_cast<IP_ADAPTER_ADDRESSES*>( auto adapters = adapters_ptr{reinterpret_cast<IP_ADAPTER_ADDRESSES*>(
::new uint8_t[len]), ::malloc(len),
free}; free};
if (!adapters) { if (!adapters) {
CAF_LOG_ERROR("malloc failed"); CAF_LOG_ERROR("malloc failed");
...@@ -172,9 +171,9 @@ std::vector<ip_address> local_addresses(string_view host) { ...@@ -172,9 +171,9 @@ std::vector<ip_address> local_addresses(string_view host) {
// TODO: The Microsoft WIN32 API example propopses to try three times, other // TODO: The Microsoft WIN32 API example propopses to try three times, other
// examples online just perform the call once. If we notice the call to be // examples online just perform the call once. If we notice the call to be
// unreliable, we might adapt that behavior. // unreliable, we might adapt that behavior.
err = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, nullptr, if (GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, nullptr,
adapters.get(), &len); adapters.get(), &len)
if (err != ERROR_SUCCESS) { != ERROR_SUCCESS) {
CAF_LOG_ERROR("failed to get adapter addresses"); CAF_LOG_ERROR("failed to get adapter addresses");
return {}; return {};
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment