NetworkInfoServiceWindows.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include <winsock2.h>
  6. #include <ws2ipdef.h>
  7. #include <iphlpapi.h>
  8. #include "mozilla/UniquePtr.h"
  9. #include "NetworkInfoServiceImpl.h"
  10. namespace mozilla {
  11. namespace net {
  12. nsresult
  13. DoListAddresses(AddrMapType& aAddrMap)
  14. {
  15. UniquePtr<MIB_IPADDRTABLE> ipAddrTable;
  16. DWORD size = sizeof(MIB_IPADDRTABLE);
  17. ipAddrTable.reset((MIB_IPADDRTABLE*) malloc(size));
  18. if (!ipAddrTable) {
  19. return NS_ERROR_FAILURE;
  20. }
  21. DWORD retVal = GetIpAddrTable(ipAddrTable.get(), &size, 0);
  22. if (retVal == ERROR_INSUFFICIENT_BUFFER) {
  23. ipAddrTable.reset((MIB_IPADDRTABLE*) malloc(size));
  24. if (!ipAddrTable) {
  25. return NS_ERROR_FAILURE;
  26. }
  27. retVal = GetIpAddrTable(ipAddrTable.get(), &size, 0);
  28. }
  29. if (retVal != NO_ERROR) {
  30. return NS_ERROR_FAILURE;
  31. }
  32. for (DWORD i = 0; i < ipAddrTable->dwNumEntries; i++) {
  33. int index = ipAddrTable->table[i].dwIndex;
  34. uint32_t addrVal = (uint32_t) ipAddrTable->table[i].dwAddr;
  35. nsCString indexString;
  36. indexString.AppendInt(index, 10);
  37. nsCString addrString;
  38. addrString.AppendPrintf("%d.%d.%d.%d",
  39. (addrVal >> 0) & 0xff, (addrVal >> 8) & 0xff,
  40. (addrVal >> 16) & 0xff, (addrVal >> 24) & 0xff);
  41. aAddrMap.Put(indexString, addrString);
  42. }
  43. return NS_OK;
  44. }
  45. } // namespace net
  46. } // namespace mozilla