network: don't use reinterpret_cast in GetAvailableNetworkInterfaces

This commit is contained in:
spholz 2021-08-13 11:58:34 +02:00
parent e660334a21
commit deb65a5717

View File

@ -23,20 +23,17 @@ namespace Network {
#ifdef _WIN32 #ifdef _WIN32
std::vector<NetworkInterface> GetAvailableNetworkInterfaces() { std::vector<NetworkInterface> GetAvailableNetworkInterfaces() {
std::vector<u8> adapter_addresses_raw; std::vector<IP_ADAPTER_ADDRESSES> adapter_addresses;
auto adapter_addresses = reinterpret_cast<PIP_ADAPTER_ADDRESSES>(adapter_addresses_raw.data());
DWORD ret = ERROR_BUFFER_OVERFLOW; DWORD ret = ERROR_BUFFER_OVERFLOW;
DWORD buf_size = 0; DWORD buf_size = 0;
// retry up to 5 times // retry up to 5 times
for (int i = 0; i < 5 && ret == ERROR_BUFFER_OVERFLOW; i++) { for (int i = 0; i < 5 && ret == ERROR_BUFFER_OVERFLOW; i++) {
ret = GetAdaptersAddresses(AF_INET, GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER, ret = GetAdaptersAddresses(AF_INET, GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER,
nullptr, adapter_addresses, &buf_size); nullptr, adapter_addresses.data(), &buf_size);
if (ret == ERROR_BUFFER_OVERFLOW) { if (ret == ERROR_BUFFER_OVERFLOW) {
adapter_addresses_raw.resize(buf_size); adapter_addresses.resize((buf_size / sizeof(IP_ADAPTER_ADDRESSES)) + 1);
adapter_addresses =
reinterpret_cast<PIP_ADAPTER_ADDRESSES>(adapter_addresses_raw.data());
} else { } else {
break; break;
} }
@ -45,7 +42,7 @@ std::vector<NetworkInterface> GetAvailableNetworkInterfaces() {
if (ret == NO_ERROR) { if (ret == NO_ERROR) {
std::vector<NetworkInterface> result; std::vector<NetworkInterface> result;
for (auto current_address = adapter_addresses; current_address != nullptr; for (auto current_address = adapter_addresses.data(); current_address != nullptr;
current_address = current_address->Next) { current_address = current_address->Next) {
if (current_address->FirstUnicastAddress == nullptr || if (current_address->FirstUnicastAddress == nullptr ||
current_address->FirstUnicastAddress->Address.lpSockaddr == nullptr) { current_address->FirstUnicastAddress->Address.lpSockaddr == nullptr) {