Files
firefox/toolkit/system/windowsDHCPClient/nsWindowsDHCPClient.cpp
T
Polly Shaw 723759fe28 Bug 356831 - Proxy autodiscovery doesn't check DHCP (option 252) r=bagder,valentin
This patch addresses an issue with Firefox's proxy detection on networks which
do not have their a proxy auto-configuration (PAC) file hosted at
http://wpad/wpad.dat, and instead make use of DHCP option 252 for broadcasting
the address of the PAC file. See https://findproxyforurl.com/wpad-introduction/
for an introduction to the protocol.

Prior to this patch, proxy auto-detect missed out the DHCP query stage, and just
 looked for a PAC file at http://wpad/wpad.dat


This patch only addresses the issue for Firefox on Windows, although it defines a
DHCP client interface which could be implemented on other platforms.

The high-level components of this patch are:
 * nsIDHCPClient.idl - this is an interface which has been defined for querying the
   DHCP server.
 * nsPACMan.cpp - where previously when the PAC URL was simply set to a constant of
   http://wpad/wpad.dat, it now dispatches an asynchronous command to the proxy
   thread. The class ExecutePACThreadAction has been augmented to include an
   instruction to 'ConfigureWPAD' (Configure Web-proxy auto-detect), and a new class,
   'ConfigureWPADComplete' has been created to relay the result (the URL of the PAC
   file) back to the nsPACMan object.
 * nsProtocolProxyService.cpp
   Minor changes to reflect the fact that the PAC URL not being set does not always
   mean there is no PAC to be used; instead it could be in the process of being
   detected.
 * TestPACMan.cpp
   This is a new file, and tests only the DHCP auto-detect functionality.
   Some tests use multiple threads, as they test the non-blocking proxy detection.
 * DHCPUtils.cpp
   A class containing the main logic for querying DHCP.
 * WindowsNetworkFunctionsWrapper.cpp
   A very thin wrapper around the Windows API calls needed by DHCPUtils.
   This class was introduced so it could be mocked out in tests.
 * nsWindowsDHCPClient.cpp
 * An implementation of the interface defined in nsIDHCPClient.idl. Fairly thin:
   most logic is implemented in DHCPUtils.
 * TestDHCPUtils.cpp
   Tests for DHCPUtils and nsWindowsDHCPClient

MozReview-Commit-ID: 4xFQz3tOLEx
2018-06-07 23:07:28 +01:00

103 lines
3.0 KiB
C++

/* -*- 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 "nsWindowsDHCPClient.h"
#include <vector>
#include "DHCPUtils.h"
#include "nsNetCID.h"
#include "nsString.h"
#include "mozilla/Logging.h"
#include "mozilla/ModuleUtils.h"
namespace mozilla {
namespace toolkit {
namespace system {
namespace windowsDHCPClient {
LazyLogModule gDhcpLog("windowsDHCPClient");
#undef LOG
#define LOG(args) MOZ_LOG(gDhcpLog, LogLevel::Debug, args)
#define MOZ_MAX_DHCP_OPTION_LENGTH 255 // this is the maximum option length in DHCP V4 and 6
NS_IMPL_ISUPPORTS(nsWindowsDHCPClient, nsIDHCPClient)
nsresult
nsWindowsDHCPClient::Init()
{
return NS_OK;
}
NS_IMETHODIMP
nsWindowsDHCPClient::GetOption(uint8_t aOption, nsACString& aRetVal)
{
nsCString networkAdapterName;
nsresult rv;
rv = GetActiveDHCPNetworkAdapterName(networkAdapterName, mNetworkFunctions);
if (rv != NS_OK) {
LOG(("Failed to get network adapter name in nsWindowsDHCPClient::GetOption due to error %d", rv));
return rv;
}
uint32_t sizeOptionValue = MOZ_MAX_DHCP_OPTION_LENGTH;
std::vector<char> optionValue;
bool retryingAfterLossOfSignificantData = false;
do {
optionValue.resize(sizeOptionValue);
rv = RetrieveOption(networkAdapterName,
aOption,
optionValue,
&sizeOptionValue,
mNetworkFunctions);
if (rv == NS_ERROR_LOSS_OF_SIGNIFICANT_DATA) {
LOG(("In nsWindowsDHCPClient::GetOption, DHCP Option %d required %d bytes", aOption, sizeOptionValue));
if (retryingAfterLossOfSignificantData) {
break;
}
retryingAfterLossOfSignificantData = true;
}
} while (rv == NS_ERROR_LOSS_OF_SIGNIFICANT_DATA);
if (rv != NS_OK) {
LOG(("Failed to get DHCP Option %d nsWindowsDHCPClient::GetOption due to error %d", aOption, rv));
return rv;
}
aRetVal.Assign(optionValue.data(), sizeOptionValue);
return NS_OK;
}
#define NS_WINDOWSDHCPCLIENTSERVICE_CID /* {FEBF1D69-4D7D-4891-9524-045AD18B5592} */\
{ 0xFEBF1D69, 0x4D7D, 0x4891, \
{0x95, 0x24, 0x04, 0x5a, 0xd1, 0x8b, 0x55, 0x92 } }
NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsWindowsDHCPClient, Init)
NS_DEFINE_NAMED_CID(NS_WINDOWSDHCPCLIENTSERVICE_CID);
static const mozilla::Module::CIDEntry kSysDHCPClientCIDs[] = {
{ &kNS_WINDOWSDHCPCLIENTSERVICE_CID, false, nullptr, nsWindowsDHCPClientConstructor },
{ nullptr }
};
static const mozilla::Module::ContractIDEntry kSysDHCPClientContracts[] = {
{ NS_DHCPCLIENT_CONTRACTID, &kNS_WINDOWSDHCPCLIENTSERVICE_CID },
{ nullptr }
};
static const mozilla::Module kSysDHCPClientModule = {
mozilla::Module::kVersion,
kSysDHCPClientCIDs,
kSysDHCPClientContracts
};
NSMODULE_DEFN(nsDHCPClientModule) = &kSysDHCPClientModule;
} // namespace windowsDHCPClient
} // namespace system
} // namespace toolkit
} // namespace mozilla