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)
10 # include <winsock2.h>
13 #include "nsNetworkInfoService.h"
14 #include "mozilla/ScopeExit.h"
16 #if defined(XP_MACOSX) || defined(XP_WIN) || defined(XP_LINUX)
17 # include "NetworkInfoServiceImpl.h"
19 # error "Unsupported platform for nsNetworkInfoService! Check moz.build"
25 NS_IMPL_ISUPPORTS(nsNetworkInfoService
, nsINetworkInfoService
)
27 nsNetworkInfoService::nsNetworkInfoService() = default;
29 nsresult
nsNetworkInfoService::Init() { return NS_OK
; }
31 nsresult
nsNetworkInfoService::ListNetworkAddresses(
32 nsIListNetworkAddressesListener
* aListener
) {
36 rv
= DoListAddresses(addrMap
);
37 if (NS_WARN_IF(NS_FAILED(rv
))) {
38 aListener
->OnListNetworkAddressesFailed();
42 uint32_t addrCount
= addrMap
.Count();
43 const char** addrStrings
=
44 (const char**)malloc(sizeof(*addrStrings
) * addrCount
);
46 aListener
->OnListNetworkAddressesFailed();
49 auto autoFreeAddrStrings
= MakeScopeExit([&] { free(addrStrings
); });
52 for (auto iter
= addrMap
.Iter(); !iter
.Done(); iter
.Next()) {
53 addrStrings
[idx
++] = iter
.Data().get();
55 aListener
->OnListedNetworkAddresses(addrStrings
, addrCount
);
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);
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
, '.');
77 if (strlen(hostnameBuf
) == 0) {
78 return NS_ERROR_FAILURE
;
81 aHostname
.AssignASCII(hostnameBuf
);
85 nsresult
nsNetworkInfoService::GetHostname(nsIGetHostnameListener
* aListener
) {
87 nsCString hostnameStr
;
88 rv
= DoGetHostname(hostnameStr
);
90 aListener
->OnGetHostnameFailed();
94 aListener
->OnGotHostname(hostnameStr
);
100 } // namespace mozilla