예제 #1
0
QueryData genInterfaceDetails(QueryContext& context) {
  QueryData results;
  WmiRequest request("SELECT * FROM Win32_NetworkAdapter");
  if (request.getStatus().ok()) {
    auto& wmi_results = request.results();
    for (const auto& adapter : wmi_results) {
      genInterfaceDetail(adapter, results);
    }
  }
  return results;
}
예제 #2
0
QueryData genInterfaceDetails(QueryContext& context) {
  QueryData results;

  DWORD buffSize = kWorkingBufferSize;
  auto alloc_attempts = 0;
  size_t alloc_result = 0;
  const auto addrFamily = AF_UNSPEC;
  const auto addrFlags =
      GAA_FLAG_INCLUDE_PREFIX | GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST;
  std::unique_ptr<IP_ADAPTER_ADDRESSES> adapters(nullptr);

  // Buffer size can change between the query and malloc (if adapters are
  // added/removed), so shenanigans are required
  do {
    adapters.reset(static_cast<PIP_ADAPTER_ADDRESSES>(malloc(buffSize)));
    if (adapters == nullptr) {
      return results;
    }
    alloc_result = GetAdaptersAddresses(
        addrFamily, addrFlags, nullptr, adapters.get(), &buffSize);
    alloc_attempts++;
  } while (alloc_result == ERROR_BUFFER_OVERFLOW &&
           alloc_attempts < kMaxBufferAllocRetries);
  if (alloc_result != NO_ERROR) {
    return results;
  }

  const IP_ADAPTER_ADDRESSES* currAdapter = adapters.get();
  while (currAdapter != nullptr) {
    Row r;
    genInterfaceDetail(currAdapter, r);
    currAdapter = currAdapter->Next;
    results.push_back(r);
  }
  return results;
}