HostDnsServiceWin.cpp revision 63fdb92b88f31446f6ea53241c0d127677c71c7e
/* $Id$ */
/** @file
* Host DNS listener for Windows.
*/
/*
* Copyright (C) 2014 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
#include <Windows.h>
#include <string>
#include <sstream>
#include <vector>
#include "../HostDnsService.h"
/* In order to monitor DNS setting updates we need to receive notification about
* Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\* keys changes.
* Since it is not possible to use patterns when subscribing key changes, we need to find valid paths for all such
* keys manually and subscribe to changes one by one (see enumerateSubTree()). */
const TCHAR HostDnsServiceWin::m_pwcKeyRoot[] = _T("SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces");
: HostDnsMonitor(true),
m_aWarehouse(),
m_fInitialized(false)
{
/* Add monitor destroy event.
* This event should have index '0' at the events array in order to separate it from
* DNS and tree change events. When this event occurs all resources should be released. */
{
/* Add registry tree change event and corresponding key.
* This event should have index '1' at the events array in order to separate it from DNS events.
* When this event occurs it means there are changes in the list of available network interfaces.
* Network interfaces should be re-enumerated, all DNS events and keys data should re-initialized. */
{
/* Enumerate all available network interfaces, create events and corresponding keys and perform subscription. */
if (enumerateSubTree())
{
m_fInitialized = true;
return;
}
else
LogRel(("WARNING: cannot set up monitor properly (3); monitor now disabled.\n"));
}
else
/* Too bad we can't even subscribe to notifications about network interfaces changes. */
LogRel(("WARNING: cannot set up monitor properly (2); monitor now disabled.\n"));
}
else
/* Too bad we can't even subscribe to destroy event. */
LogRel(("WARNING: cannot set up monitor properly (1); monitor now disabled.\n"));
}
{
m_fInitialized = false;
}
{
bool rc = true;
/* We do not check if idxItem is in valid range of m_aWarehouse here
* (a bit of performance optimization), so make sure you provided a valid value! */
/* Non-zero return code means ResetEvent() succeeded. */
if (!rc) LogRel(("Failed to reset event (idxItem=%d); monitor unstable (rc=%d).\n", idxItem, GetLastError()));
return rc;
}
{
bool rc = true;
/* Any sub-tree events we subscribed? */
/* Going from the end to the beginning. */
{
LogRel(("DNS monitor unstable; %d events left after dropping.\n", (int)cElementsLeft - VBOX_OFFSET_SUBTREE_EVENTS));
return false;
}
return rc;
}
void HostDnsServiceWin::releaseResources()
{
/* First, drop notifications subscription for sub-tree keys. */
/* Then release notification about tree structure changes. */
/* Release shutdown event. */
}
{
/* Do not add more than MAXIMUM_WAIT_OBJECTS items to the array due to WaitForMultipleObjects() limitation. */
if ((m_aWarehouse.size() + 1 /* the array size if we would add an extra item */ ) > MAXIMUM_WAIT_OBJECTS)
{
LogRel(("Too many items to monitor.\n"));
return false;
}
if (!hTmpEvent)
return false;
/* wcsPath might not be specified if we want to subscribe to the termination event. In this case
* it is assumed that this is the first issued subscription request (i.e., m_aWarehouse.size() == 0). */
if (wcsPath)
{
/* Open registry key itself. */
if (rc == ERROR_SUCCESS)
{
/* Link registry key and notification event. */
if (rc != ERROR_SUCCESS)
{
/* Don't leak! */
}
}
else
/* All good so far? */
if (rc != ERROR_SUCCESS)
{
return false;
}
}
else if (m_aWarehouse.size() > 0)
{
LogRel(("Subscription to termination event already established.\n"));
return false;
}
/* Finally, construct array item and queue it. */
/* Sub-tree keys should provide interface name (UUID). This is needed in order to
* collect all useful network settings to HostDnsInformation storage object to provide it to parent class. */
if (wcsInterface)
if (wcsPath)
return true;
}
bool HostDnsServiceWin::enumerateSubTree()
{
DWORD cbSubKeyNameMax = 0;
/* Enumerate all the available interfaces. */
if (rc == ERROR_SUCCESS)
{
/* Get info about amount of available network interfaces. */
rc = RegQueryInfoKey(hTmpKey, NULL, NULL, NULL, &cSubKeys, &cbSubKeyNameMax, NULL, NULL, NULL, NULL, NULL, NULL);
if (rc == ERROR_SUCCESS)
{
/* Now iterate over interfaces if:
* 1) there are interfaces available and
* 2) maximum length of an interface name conforms to our buffer allocation size. */
{
{
if (rc == ERROR_SUCCESS)
{
/* Since we already know interface name (actually UUID), construct full registry path here. */
if (rc == 0)
}
else
LogRel(("Unable to open interfaces list (1).\n"));
}
return true;
}
else
LogRel(("Unable to open interfaces list (2).\n"));
}
else
LogRel(("Unable to open interfaces list (3).\n"));
}
else
LogRel(("Unable to open interfaces list (4).\n"));
return false;
}
{
return updateInfo(NULL);
}
{
}
void HostDnsServiceWin::extendVectorWithStrings(std::vector<std::wstring>& pVectorToExtend, std::wstring &wcsParameter)
{
}
#ifdef DEBUG
{
{
}
}
#endif /* DEBUG */
{
/* Any interfaces available? */
{
/* Walk across all the available interfaces and collect network configuration data:
* domain name, name servers and search list. */
{
/* Get number of key values. */
rc = RegQueryInfoKey(m_aWarehouse[idxKey].hKey, NULL, NULL, NULL, NULL, NULL, NULL, &cValues, NULL, NULL, NULL, NULL);
if (rc == ERROR_SUCCESS)
{
{
/* Walk across all the properties of given interface. */
rc = RegEnumValue(m_aWarehouse[idxKey].hKey, idxValue, wcsValueName, &cbValueName, 0, NULL, (LPBYTE)wcsData, &cbData);
if (rc == ERROR_SUCCESS)
{
{
/* We rely on that fact that Windows host cannot be a member of more than one domain in the same time! */
}
{
}
{
}
}
}
}
}
/* Compare cached network settings and newly obtained ones. */
{
#ifdef DEBUG
LogRel(("Servers changed from:\n"));
LogRel(("to:\n"));
#endif /* DEBUG */
}
{
#ifdef DEBUG
LogRel(("Domain changed: [%ls]->[%ls].\n",
m_hostInfoCache.domain.empty() ? _T("NONE") : (const unsigned short *)m_hostInfoCache.domain.data(),
#endif /* DEBUG */
}
{
#ifdef DEBUG
LogRel(("SearchList changed from:\n"));
LogRel(("to:\n"));
#endif /* DEBUG */
}
/* Provide info about changes if requested. */
if (fWhatsChanged)
/* Update host network configuration cache. */
}
return S_OK;
}
{
}
int HostDnsServiceWin::monitorWorker()
{
if (!m_fInitialized)
{
LogRel(("Host DNS monitor was not initialized properly.\n"));
return VERR_INTERNAL_ERROR;
}
while (true)
{
/* Each new iteration we need to update event handles list we monitor. */
("WaitForMultipleObjects failed (%d) to wait! Please debug",
/* Shutdown requested. */
/* Interfaces amount changed. */
{
Log2(("Network interfaces amount changed.\n"));
/* Drop interface events. */
if (dropSubTreeNotifications())
{
/* Drop event which is corresponds to interfaces tree changes. */
{
/* Restart interface tree monitoring. */
{
/* Restart interface events. */
if (enumerateSubTree())
{
Log2(("Monitor restarted successfully.\n"));
{
LogRel(("Notification sent (1).\n"));
notifyAll();
}
continue;
}
else
LogRel(("Monitor unstable: failed to subscribe network configuration changes.\n"));
}
else
LogRel(("Monitor unstable: failed to subscribe interface changes.\n"));
}
else
LogRel(("Monitor unstable: failed to unsubscribe from interfaces amount changes.\n"));
}
else
LogRel(("Monitor unstable: failed to unsubscribe from previous notifications.\n"));
/* If something went wrong, we break monitoring. */
break;
}
/* DNS update events range. */
{
Log2(("Network setting has changed at interface %ls.\n", m_aWarehouse[rc - WAIT_OBJECT_0].wcsInterface));
/* Drop previous notifications first. */
if (dropSubTreeNotifications())
{
/* Re-subscribe. */
if (enumerateSubTree())
{
Log2(("Restart monitoring.\n"));
{
LogRel(("Notification sent (2).\n"));
notifyAll();
}
continue;
}
else
LogRel(("WARNING: Monitor unstable: unable to re-subscribe to notifications.\n"));
}
else
LogRel(("WARNING: Monitor unstable: failed to unsubscribe from previous notifications.\n"));
/* If something went wrong, we stop monitoring. */
break;
}
else
AssertMsgFailedReturn(("WaitForMultipleObjects returns out of bound (%d) index %d. Please debug!", m_aWarehouse.size(), rc), VERR_INTERNAL_ERROR);
}
LogRel(("Monitor thread exited.\n"));
return VINF_SUCCESS;
}