nsNetworkInfoService.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #if defined(XP_MACOSX) || defined(XP_LINUX)
  6. #include <unistd.h>
  7. #elif defined(XP_WIN)
  8. #include <winsock2.h>
  9. #endif
  10. #include "nsNetworkInfoService.h"
  11. #include "mozilla/ScopeExit.h"
  12. #if defined(XP_MACOSX) || defined(XP_WIN) || defined(XP_LINUX)
  13. #include "NetworkInfoServiceImpl.h"
  14. #else
  15. #error "Unsupported platform for nsNetworkInfoService! Check moz.build"
  16. #endif
  17. namespace mozilla {
  18. namespace net {
  19. NS_IMPL_ISUPPORTS(nsNetworkInfoService,
  20. nsINetworkInfoService)
  21. nsNetworkInfoService::nsNetworkInfoService()
  22. {
  23. }
  24. nsresult
  25. nsNetworkInfoService::Init()
  26. {
  27. return NS_OK;
  28. }
  29. nsresult
  30. nsNetworkInfoService::ListNetworkAddresses(nsIListNetworkAddressesListener* aListener)
  31. {
  32. nsresult rv;
  33. AddrMapType addrMap;
  34. rv = DoListAddresses(addrMap);
  35. if (NS_WARN_IF(NS_FAILED(rv))) {
  36. aListener->OnListNetworkAddressesFailed();
  37. return NS_OK;
  38. }
  39. uint32_t addrCount = addrMap.Count();
  40. const char** addrStrings = (const char**) malloc(sizeof(*addrStrings) * addrCount);
  41. if (!addrStrings) {
  42. aListener->OnListNetworkAddressesFailed();
  43. return NS_OK;
  44. }
  45. auto autoFreeAddrStrings = MakeScopeExit([&] {
  46. free(addrStrings);
  47. });
  48. uint32_t idx = 0;
  49. for (auto iter = addrMap.Iter(); !iter.Done(); iter.Next()) {
  50. addrStrings[idx++] = iter.Data().get();
  51. }
  52. aListener->OnListedNetworkAddresses(addrStrings, addrCount);
  53. return NS_OK;
  54. }
  55. // TODO: Bug 1275373: https://bugzilla.mozilla.org/show_bug.cgi?id=1275373
  56. // Use platform-specific implementation of DoGetHostname on Cocoa and Windows.
  57. static nsresult
  58. DoGetHostname(nsACString& aHostname)
  59. {
  60. char hostnameBuf[256];
  61. int result = gethostname(hostnameBuf, 256);
  62. if (result == -1) {
  63. return NS_ERROR_FAILURE;
  64. }
  65. // Ensure that there is always a terminating NUL byte.
  66. hostnameBuf[255] = '\0';
  67. // Find the first '.', terminate string there.
  68. char* dotLocation = strchr(hostnameBuf, '.');
  69. if (dotLocation) {
  70. *dotLocation = '\0';
  71. }
  72. if (strlen(hostnameBuf) == 0) {
  73. return NS_ERROR_FAILURE;
  74. }
  75. aHostname.AssignASCII(hostnameBuf);
  76. return NS_OK;
  77. }
  78. nsresult
  79. nsNetworkInfoService::GetHostname(nsIGetHostnameListener* aListener)
  80. {
  81. nsresult rv;
  82. nsCString hostnameStr;
  83. rv = DoGetHostname(hostnameStr);
  84. if (NS_FAILED(rv)) {
  85. aListener->OnGetHostnameFailed();
  86. return NS_OK;
  87. }
  88. aListener->OnGotHostname(hostnameStr);
  89. return NS_OK;
  90. }
  91. } // namespace net
  92. } // namespace mozilla