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