Bug 1840882 - Remove unused code nsNetworkInfoService r=necko-reviewers,kershaw,jesup

Differential Revision: https://phabricator.services.mozilla.com/D182285
This commit is contained in:
Jake Senne 2023-06-30 12:04:25 +00:00
parent 5ed9d9fcb5
commit 9cd4a028b6
7 changed files with 0 additions and 416 deletions

View File

@ -1,100 +0,0 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <net/if.h>
#include <netdb.h>
#include "mozilla/DebugOnly.h"
#include "mozilla/ScopeExit.h"
#include "NetworkInfoServiceImpl.h"
namespace mozilla {
namespace net {
static nsresult ListInterfaceAddresses(int aFd, const char* aIface,
AddrMapType& aAddrMap);
nsresult DoListAddresses(AddrMapType& aAddrMap) {
int fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
return NS_ERROR_FAILURE;
}
auto autoCloseSocket = MakeScopeExit([&] { close(fd); });
struct ifconf ifconf;
/* 16k of space should be enough to list all interfaces. Worst case, if it's
* not then we will error out and fail to list addresses. This should only
* happen on pathological machines with way too many interfaces.
*/
char buf[16384];
ifconf.ifc_len = sizeof(buf);
ifconf.ifc_buf = buf;
if (ioctl(fd, SIOCGIFCONF, &ifconf) != 0) {
return NS_ERROR_FAILURE;
}
struct ifreq* ifreq = ifconf.ifc_req;
int i = 0;
while (i < ifconf.ifc_len) {
size_t len = IFNAMSIZ + ifreq->ifr_addr.sa_len;
DebugOnly<nsresult> rv =
ListInterfaceAddresses(fd, ifreq->ifr_name, aAddrMap);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "ListInterfaceAddresses failed");
ifreq = (struct ifreq*)((char*)ifreq + len);
i += len;
}
autoCloseSocket.release();
return NS_OK;
}
static nsresult ListInterfaceAddresses(int aFd, const char* aInterface,
AddrMapType& aAddrMap) {
struct ifreq ifreq;
memset(&ifreq, 0, sizeof(struct ifreq));
strncpy(ifreq.ifr_name, aInterface, IFNAMSIZ - 1);
if (ioctl(aFd, SIOCGIFADDR, &ifreq) != 0) {
return NS_ERROR_FAILURE;
}
char host[128];
int family;
switch (family = ifreq.ifr_addr.sa_family) {
case AF_INET:
case AF_INET6:
getnameinfo(&ifreq.ifr_addr, sizeof(ifreq.ifr_addr), host, sizeof(host),
nullptr, 0, NI_NUMERICHOST);
break;
case AF_UNSPEC:
return NS_OK;
default:
// Unknown family.
return NS_OK;
}
nsCString ifaceStr;
ifaceStr.AssignASCII(aInterface);
nsCString addrStr;
addrStr.AssignASCII(host);
aAddrMap.InsertOrUpdate(ifaceStr, addrStr);
return NS_OK;
}
} // namespace net
} // namespace mozilla

View File

@ -1,95 +0,0 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netdb.h>
#include "mozilla/DebugOnly.h"
#include "mozilla/ScopeExit.h"
#include "NetworkInfoServiceImpl.h"
namespace mozilla::net {
static nsresult ListInterfaceAddresses(int aFd, const char* aInterface,
AddrMapType& aAddrMap);
nsresult DoListAddresses(AddrMapType& aAddrMap) {
int fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
return NS_ERROR_FAILURE;
}
auto autoCloseSocket = MakeScopeExit([&] { close(fd); });
struct ifconf ifconf {};
/* 16k of space should be enough to list all interfaces. Worst case, if it's
* not then we will error out and fail to list addresses. This should only
* happen on pathological machines with way too many interfaces.
*/
char buf[16384];
ifconf.ifc_len = sizeof(buf);
ifconf.ifc_buf = buf;
if (ioctl(fd, SIOCGIFCONF, &ifconf) != 0) {
return NS_ERROR_FAILURE;
}
struct ifreq* ifreq = ifconf.ifc_req;
int i = 0;
while (i < ifconf.ifc_len) {
size_t len = sizeof(struct ifreq);
DebugOnly<nsresult> rv =
ListInterfaceAddresses(fd, ifreq->ifr_name, aAddrMap);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "ListInterfaceAddresses failed");
ifreq = (struct ifreq*)((char*)ifreq + len);
i += len;
}
return NS_OK;
}
static nsresult ListInterfaceAddresses(int aFd, const char* aInterface,
AddrMapType& aAddrMap) {
struct ifreq ifreq {};
memset(&ifreq, 0, sizeof(struct ifreq));
strncpy(ifreq.ifr_name, aInterface, IFNAMSIZ - 1);
if (ioctl(aFd, SIOCGIFADDR, &ifreq) != 0) {
return NS_ERROR_FAILURE;
}
char host[128];
switch (ifreq.ifr_addr.sa_family) {
case AF_INET:
case AF_INET6:
getnameinfo(&ifreq.ifr_addr, sizeof(ifreq.ifr_addr), host, sizeof(host),
nullptr, 0, NI_NUMERICHOST);
break;
case AF_UNSPEC:
return NS_OK;
default:
// Unknown family.
return NS_OK;
}
nsCString ifaceStr;
ifaceStr.AssignASCII(aInterface);
nsCString addrStr;
addrStr.AssignASCII(host);
aAddrMap.InsertOrUpdate(ifaceStr, addrStr);
return NS_OK;
}
} // namespace mozilla::net

View File

@ -1,58 +0,0 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <winsock2.h>
#include <ws2ipdef.h>
#include <iphlpapi.h>
#include "mozilla/UniquePtr.h"
#include "NetworkInfoServiceImpl.h"
namespace mozilla {
namespace net {
nsresult DoListAddresses(AddrMapType& aAddrMap) {
UniquePtr<MIB_IPADDRTABLE> ipAddrTable;
DWORD size = sizeof(MIB_IPADDRTABLE);
ipAddrTable.reset((MIB_IPADDRTABLE*)malloc(size));
if (!ipAddrTable) {
return NS_ERROR_FAILURE;
}
DWORD retVal = GetIpAddrTable(ipAddrTable.get(), &size, 0);
if (retVal == ERROR_INSUFFICIENT_BUFFER) {
ipAddrTable.reset((MIB_IPADDRTABLE*)malloc(size));
if (!ipAddrTable) {
return NS_ERROR_FAILURE;
}
retVal = GetIpAddrTable(ipAddrTable.get(), &size, 0);
}
if (retVal != NO_ERROR) {
return NS_ERROR_FAILURE;
}
for (DWORD i = 0; i < ipAddrTable->dwNumEntries; i++) {
int index = ipAddrTable->table[i].dwIndex;
uint32_t addrVal = (uint32_t)ipAddrTable->table[i].dwAddr;
nsCString indexString;
indexString.AppendInt(index, 10);
nsCString addrString;
addrString.AppendPrintf("%d.%d.%d.%d", (addrVal >> 0) & 0xff,
(addrVal >> 8) & 0xff, (addrVal >> 16) & 0xff,
(addrVal >> 24) & 0xff);
aAddrMap.InsertOrUpdate(indexString, addrString);
}
return NS_OK;
}
} // namespace net
} // namespace mozilla

View File

@ -285,23 +285,6 @@ else:
"nsURLHelperUnix.cpp",
]
# nsINetworkInfoService support.
if CONFIG["MOZ_WIDGET_TOOLKIT"] == "windows":
SOURCES += [
"NetworkInfoServiceWindows.cpp",
"nsNetworkInfoService.cpp",
]
elif CONFIG["MOZ_WIDGET_TOOLKIT"] == "cocoa":
SOURCES += [
"NetworkInfoServiceCocoa.cpp",
"nsNetworkInfoService.cpp",
]
elif CONFIG["OS_ARCH"] == "Linux":
SOURCES += [
"NetworkInfoServiceLinux.cpp",
"nsNetworkInfoService.cpp",
]
EXTRA_JS_MODULES += [
"NetUtil.sys.mjs",
]

View File

@ -1,94 +0,0 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#if defined(XP_MACOSX) || defined(XP_LINUX)
# include <unistd.h>
#elif defined(XP_WIN)
# include <winsock2.h>
#endif
#include "nsNetworkInfoService.h"
#if defined(XP_MACOSX) || defined(XP_WIN) || defined(XP_LINUX)
# include "NetworkInfoServiceImpl.h"
#else
# error "Unsupported platform for nsNetworkInfoService! Check moz.build"
#endif
namespace mozilla::net {
NS_IMPL_ISUPPORTS(nsNetworkInfoService, nsINetworkInfoService)
nsNetworkInfoService::nsNetworkInfoService() = default;
nsresult nsNetworkInfoService::Init() { return NS_OK; }
nsresult nsNetworkInfoService::ListNetworkAddresses(
nsIListNetworkAddressesListener* aListener) {
nsresult rv;
AddrMapType addrMap;
rv = DoListAddresses(addrMap);
if (NS_WARN_IF(NS_FAILED(rv))) {
aListener->OnListNetworkAddressesFailed();
return NS_OK;
}
uint32_t addrCount = addrMap.Count();
nsTArray<nsCString> addrStrings;
if (!addrStrings.SetCapacity(addrCount, fallible)) {
aListener->OnListNetworkAddressesFailed();
return NS_OK;
}
for (const auto& data : addrMap.Values()) {
addrStrings.AppendElement(data);
}
aListener->OnListedNetworkAddresses(addrStrings);
return NS_OK;
}
// TODO: Bug 1275373: https://bugzilla.mozilla.org/show_bug.cgi?id=1275373
// Use platform-specific implementation of DoGetHostname on Cocoa and Windows.
static nsresult DoGetHostname(nsACString& aHostname) {
char hostnameBuf[256];
int result = gethostname(hostnameBuf, 256);
if (result == -1) {
return NS_ERROR_FAILURE;
}
// Ensure that there is always a terminating NUL byte.
hostnameBuf[255] = '\0';
// Find the first '.', terminate string there.
char* dotLocation = strchr(hostnameBuf, '.');
if (dotLocation) {
*dotLocation = '\0';
}
if (strlen(hostnameBuf) == 0) {
return NS_ERROR_FAILURE;
}
aHostname.AssignASCII(hostnameBuf);
return NS_OK;
}
nsresult nsNetworkInfoService::GetHostname(nsIGetHostnameListener* aListener) {
nsresult rv;
nsCString hostnameStr;
rv = DoGetHostname(hostnameStr);
if (NS_FAILED(rv)) {
aListener->OnGetHostnameFailed();
return NS_OK;
}
aListener->OnGotHostname(hostnameStr);
return NS_OK;
}
} // namespace mozilla::net

View File

@ -1,40 +0,0 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef mozilla_net_nsNetworkInfoService_h
#define mozilla_net_nsNetworkInfoService_h
#include "nsISupportsImpl.h"
#include "nsINetworkInfoService.h"
#define NETWORKINFOSERVICE_CID \
{ \
0x296d0900, 0xf8ef, 0x4df0, { \
0x9c, 0x35, 0xdb, 0x58, 0x62, 0xab, 0xc5, 0x8d \
} \
}
namespace mozilla {
namespace net {
class nsNetworkInfoService final : public nsINetworkInfoService {
public:
NS_DECL_ISUPPORTS
NS_DECL_NSINETWORKINFOSERVICE
nsresult Init();
explicit nsNetworkInfoService();
private:
virtual ~nsNetworkInfoService() = default;
};
} // namespace net
} // namespace mozilla
#endif // mozilla_dom_nsNetworkInfoService_h

View File

@ -755,18 +755,6 @@ if defined('NECKO_WIFI'):
},
]
if buildconfig.substs['OS_ARCH'] in ('WINNT', 'Darwin', 'Linux'):
Classes += [
{
'cid': '{296d0900-f8ef-4df0-9c35-db5862abc58d}',
'contract_ids': ['@mozilla.org/network-info-service;1'],
'type': 'mozilla::net::nsNetworkInfoService',
'headers': ['/netwerk/base/nsNetworkInfoService.h'],
'init_method': 'Init',
},
]
toolkit = buildconfig.substs['MOZ_WIDGET_TOOLKIT']
link_service = None
if toolkit == 'windows':