Bug 1526591 - Remove devtools.inspector.shapesHighlighter.enabled pref. r=rcaliman
[gecko.git] / netwerk / base / nsNetworkInfoService.cpp
blob348f0463362426ee1b7d0c7f99048d21558dbf39
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #if defined(XP_MACOSX) || defined(XP_LINUX)
8 # include <unistd.h>
9 #elif defined(XP_WIN)
10 # include <winsock2.h>
11 #endif
13 #include "nsNetworkInfoService.h"
14 #include "mozilla/ScopeExit.h"
16 #if defined(XP_MACOSX) || defined(XP_WIN) || defined(XP_LINUX)
17 # include "NetworkInfoServiceImpl.h"
18 #else
19 # error "Unsupported platform for nsNetworkInfoService! Check moz.build"
20 #endif
22 namespace mozilla {
23 namespace net {
25 NS_IMPL_ISUPPORTS(nsNetworkInfoService, nsINetworkInfoService)
27 nsNetworkInfoService::nsNetworkInfoService() = default;
29 nsresult nsNetworkInfoService::Init() { return NS_OK; }
31 nsresult nsNetworkInfoService::ListNetworkAddresses(
32 nsIListNetworkAddressesListener* aListener) {
33 nsresult rv;
35 AddrMapType addrMap;
36 rv = DoListAddresses(addrMap);
37 if (NS_WARN_IF(NS_FAILED(rv))) {
38 aListener->OnListNetworkAddressesFailed();
39 return NS_OK;
42 uint32_t addrCount = addrMap.Count();
43 const char** addrStrings =
44 (const char**)malloc(sizeof(*addrStrings) * addrCount);
45 if (!addrStrings) {
46 aListener->OnListNetworkAddressesFailed();
47 return NS_OK;
49 auto autoFreeAddrStrings = MakeScopeExit([&] { free(addrStrings); });
51 uint32_t idx = 0;
52 for (auto iter = addrMap.Iter(); !iter.Done(); iter.Next()) {
53 addrStrings[idx++] = iter.Data().get();
55 aListener->OnListedNetworkAddresses(addrStrings, addrCount);
56 return NS_OK;
59 // TODO: Bug 1275373: https://bugzilla.mozilla.org/show_bug.cgi?id=1275373
60 // Use platform-specific implementation of DoGetHostname on Cocoa and Windows.
61 static nsresult DoGetHostname(nsACString& aHostname) {
62 char hostnameBuf[256];
63 int result = gethostname(hostnameBuf, 256);
64 if (result == -1) {
65 return NS_ERROR_FAILURE;
68 // Ensure that there is always a terminating NUL byte.
69 hostnameBuf[255] = '\0';
71 // Find the first '.', terminate string there.
72 char* dotLocation = strchr(hostnameBuf, '.');
73 if (dotLocation) {
74 *dotLocation = '\0';
77 if (strlen(hostnameBuf) == 0) {
78 return NS_ERROR_FAILURE;
81 aHostname.AssignASCII(hostnameBuf);
82 return NS_OK;
85 nsresult nsNetworkInfoService::GetHostname(nsIGetHostnameListener* aListener) {
86 nsresult rv;
87 nsCString hostnameStr;
88 rv = DoGetHostname(hostnameStr);
89 if (NS_FAILED(rv)) {
90 aListener->OnGetHostnameFailed();
91 return NS_OK;
94 aListener->OnGotHostname(hostnameStr);
96 return NS_OK;
99 } // namespace net
100 } // namespace mozilla