| 73 | |
| 74 | #ifdef _WIN32 |
| 75 | AdapterUtils::Adapter* AdapterUtils::GetAllAdapters(AdapterBuffer* buffer, bool includeHidden) |
| 76 | { |
| 77 | // It is recommend to pre-allocate enough space to be able to call GetAdaptersAddresses just once. |
| 78 | // Also provide extra space if we are including hidden adapters. |
| 79 | int neededSize = includeHidden ? 100000 : 50000; |
| 80 | // Each IP_ADAPTER_ADDRESSES will have pointers other structures which are also copied into this buffer. |
| 81 | // Subsequent IP_ADAPTER_ADDRESSES (accessed via .Next) are also not aligned to sizeof(IP_ADAPTER_ADDRESSES) boundaries. |
| 82 | std::unique_ptr<std::byte[]> adapterInfo = std::make_unique_for_overwrite<std::byte[]>(neededSize); |
| 83 | ULONG dwBufLen = neededSize; |
| 84 | |
| 85 | DWORD dwStatus = GetAdaptersAddresses( |
| 86 | AF_UNSPEC, |
| 87 | GAA_FLAG_INCLUDE_PREFIX | GAA_FLAG_INCLUDE_GATEWAYS | (includeHidden ? GAA_FLAG_INCLUDE_ALL_INTERFACES : 0), |
| 88 | NULL, |
| 89 | reinterpret_cast<PIP_ADAPTER_ADDRESSES>(adapterInfo.get()), |
| 90 | &dwBufLen); |
| 91 | |
| 92 | if (dwStatus == ERROR_BUFFER_OVERFLOW) |
| 93 | { |
| 94 | DevCon.WriteLn("DEV9: GetWin32Adapter() buffer too small, resizing"); |
| 95 | neededSize = dwBufLen + 500; |
| 96 | adapterInfo = std::make_unique_for_overwrite<std::byte[]>(neededSize); |
| 97 | dwBufLen = neededSize; |
| 98 | DevCon.WriteLn("DEV9: New size %i", neededSize); |
| 99 | |
| 100 | dwStatus = GetAdaptersAddresses( |
| 101 | AF_UNSPEC, |
| 102 | GAA_FLAG_INCLUDE_PREFIX | GAA_FLAG_INCLUDE_GATEWAYS | (includeHidden ? GAA_FLAG_INCLUDE_ALL_INTERFACES : 0), |
| 103 | NULL, |
| 104 | reinterpret_cast<PIP_ADAPTER_ADDRESSES>(adapterInfo.get()), |
| 105 | &dwBufLen); |
| 106 | } |
| 107 | if (dwStatus != ERROR_SUCCESS) |
| 108 | return nullptr; |
| 109 | |
| 110 | buffer->swap(adapterInfo); |
| 111 | |
| 112 | // Trigger implicit object creation. |
| 113 | return std::launder(reinterpret_cast<PIP_ADAPTER_ADDRESSES>(buffer->get())); |
| 114 | } |
| 115 | bool AdapterUtils::GetAdapter(const std::string& name, Adapter* adapter, AdapterBuffer* buffer) |
| 116 | { |
| 117 | std::unique_ptr<std::byte[]> adapterInfo; |