Coverage Report

Created: 2018-09-25 14:53

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