HostDnsServiceWin.cpp revision 735076496e14763dcdef4b43202e7665d4a8da43
735076496e14763dcdef4b43202e7665d4a8da43vboxsync * Host DNS listener for Windows.
735076496e14763dcdef4b43202e7665d4a8da43vboxsync * Copyright (C) 2014 Oracle Corporation
735076496e14763dcdef4b43202e7665d4a8da43vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
735076496e14763dcdef4b43202e7665d4a8da43vboxsync * available from http://www.virtualbox.org. This file is free software;
735076496e14763dcdef4b43202e7665d4a8da43vboxsync * you can redistribute it and/or modify it under the terms of the GNU
735076496e14763dcdef4b43202e7665d4a8da43vboxsync * General Public License (GPL) as published by the Free Software
735076496e14763dcdef4b43202e7665d4a8da43vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
735076496e14763dcdef4b43202e7665d4a8da43vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
735076496e14763dcdef4b43202e7665d4a8da43vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
735076496e14763dcdef4b43202e7665d4a8da43vboxsync/* In order to monitor DNS setting updates we need to receive notification about
735076496e14763dcdef4b43202e7665d4a8da43vboxsync * Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\* keys changes.
735076496e14763dcdef4b43202e7665d4a8da43vboxsync * Since it is not possible to use patterns when subscribing key changes, we need to find valid paths for all such
735076496e14763dcdef4b43202e7665d4a8da43vboxsync * keys manually and subscribe to changes one by one (see enumerateSubTree()). */
735076496e14763dcdef4b43202e7665d4a8da43vboxsyncconst wchar_t HostDnsServiceWin::pwcKeyRoot[] = L"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces";
735076496e14763dcdef4b43202e7665d4a8da43vboxsync /* Add monitor destroy event.
735076496e14763dcdef4b43202e7665d4a8da43vboxsync * This event should have index '0' at the events array in order to separate it from
735076496e14763dcdef4b43202e7665d4a8da43vboxsync * DNS and tree change events. When this event occurs all resources should be released. */
735076496e14763dcdef4b43202e7665d4a8da43vboxsync /* Add registry tree change event and corresponding key.
735076496e14763dcdef4b43202e7665d4a8da43vboxsync * This event should have index '1' at the events array in order to separate it from DNS events.
735076496e14763dcdef4b43202e7665d4a8da43vboxsync * When this event occurs it means there are changes in the list of available network interfaces.
735076496e14763dcdef4b43202e7665d4a8da43vboxsync * Network interfaces should be re-enumerated, all DNS events and keys data should re-initialized. */
735076496e14763dcdef4b43202e7665d4a8da43vboxsync if (subscribeTo(pwcKeyRoot, NULL, REG_NOTIFY_CHANGE_NAME))
735076496e14763dcdef4b43202e7665d4a8da43vboxsync /* Enumerate all available network interfaces, create events and corresponding keys and perform subscription. */
735076496e14763dcdef4b43202e7665d4a8da43vboxsync LogRel(("WARNING: cannot set up monitor properly (3); monitor now disabled.\n"));
735076496e14763dcdef4b43202e7665d4a8da43vboxsync /* Too bad we can't even subscribe to notifications about network interfaces changes. */
735076496e14763dcdef4b43202e7665d4a8da43vboxsync LogRel(("WARNING: cannot set up monitor properly (2); monitor now disabled.\n"));
735076496e14763dcdef4b43202e7665d4a8da43vboxsync /* Too bad we can't even subscribe to destroy event. */
735076496e14763dcdef4b43202e7665d4a8da43vboxsync LogRel(("WARNING: cannot set up monitor properly (1); monitor now disabled.\n"));
735076496e14763dcdef4b43202e7665d4a8da43vboxsyncbool HostDnsServiceWin::releaseWarehouseItem(int idxItem)
735076496e14763dcdef4b43202e7665d4a8da43vboxsync bool rc = true;
735076496e14763dcdef4b43202e7665d4a8da43vboxsync /* We do not check if idxItem is in valid range of m_aWarehouse here
735076496e14763dcdef4b43202e7665d4a8da43vboxsync * (a bit of performance optimization), so make sure you provided a valid value! */
735076496e14763dcdef4b43202e7665d4a8da43vboxsync /* Non-zero return code means ResetEvent() succeeded. */
735076496e14763dcdef4b43202e7665d4a8da43vboxsync if (!rc) LogRel(("Failed to reset event (idxItem=%d); monitor unstable (rc=%d).\n", idxItem, GetLastError()));
735076496e14763dcdef4b43202e7665d4a8da43vboxsync Log2(("Unsubscribed from %ls notifications\n", oTmpItem.wcsInterface));
735076496e14763dcdef4b43202e7665d4a8da43vboxsync m_aWarehouse.erase(m_aWarehouse.begin() + idxItem);
735076496e14763dcdef4b43202e7665d4a8da43vboxsync bool rc = true;
735076496e14763dcdef4b43202e7665d4a8da43vboxsync /* Any sub-tree events we subscribed? */
735076496e14763dcdef4b43202e7665d4a8da43vboxsync if (m_aWarehouse.size() > VBOX_OFFSET_SUBTREE_EVENTS)
735076496e14763dcdef4b43202e7665d4a8da43vboxsync /* Going from the end to the beginning. */
735076496e14763dcdef4b43202e7665d4a8da43vboxsync for (int idxItem = (int)m_aWarehouse.size() - 1; idxItem >= VBOX_OFFSET_SUBTREE_EVENTS; idxItem--)
735076496e14763dcdef4b43202e7665d4a8da43vboxsync LogRel(("DNS monitor unstable; %d events left after dropping.\n", (int)cElementsLeft - VBOX_OFFSET_SUBTREE_EVENTS));
735076496e14763dcdef4b43202e7665d4a8da43vboxsync return false;
735076496e14763dcdef4b43202e7665d4a8da43vboxsync /* First, drop notifications subscription for sub-tree keys. */
735076496e14763dcdef4b43202e7665d4a8da43vboxsync /* Then release notification about tree structure changes. */
735076496e14763dcdef4b43202e7665d4a8da43vboxsync /* Release shutdown event. */
735076496e14763dcdef4b43202e7665d4a8da43vboxsync if (m_aWarehouse.size() > VBOX_OFFSET_SHUTDOWN_EVENT)
735076496e14763dcdef4b43202e7665d4a8da43vboxsyncbool HostDnsServiceWin::subscribeTo(const wchar_t *wcsPath, const wchar_t *wcsInterface, DWORD fFilter)
735076496e14763dcdef4b43202e7665d4a8da43vboxsync /* Do not add more than MAXIMUM_WAIT_OBJECTS items to the array due to WaitForMultipleObjects() limitation. */
735076496e14763dcdef4b43202e7665d4a8da43vboxsync if ((m_aWarehouse.size() + 1 /* the array size if we would add an extra item */ ) > MAXIMUM_WAIT_OBJECTS)
735076496e14763dcdef4b43202e7665d4a8da43vboxsync return false;
735076496e14763dcdef4b43202e7665d4a8da43vboxsync return false;
735076496e14763dcdef4b43202e7665d4a8da43vboxsync /* wcsPath might not be specified if we want to subscribe to the termination event. In this case
735076496e14763dcdef4b43202e7665d4a8da43vboxsync * it is assumed that this is the first issued subscription request (i.e., m_aWarehouse.size() == 0). */
735076496e14763dcdef4b43202e7665d4a8da43vboxsync /* Open registry key itself. */
735076496e14763dcdef4b43202e7665d4a8da43vboxsync rc = RegOpenKeyExW(HKEY_LOCAL_MACHINE, wcsPath, 0, KEY_READ | KEY_NOTIFY, &hTmpKey);
735076496e14763dcdef4b43202e7665d4a8da43vboxsync /* Link registry key and notification event. */
735076496e14763dcdef4b43202e7665d4a8da43vboxsync rc = RegNotifyChangeKeyValue(hTmpKey, TRUE, fFilter, hTmpEvent, TRUE);
735076496e14763dcdef4b43202e7665d4a8da43vboxsync /* Don't leak! */
735076496e14763dcdef4b43202e7665d4a8da43vboxsync LogRel(("Unable to register key notification (rc=0x%X).\n", rc));
735076496e14763dcdef4b43202e7665d4a8da43vboxsync /* All good so far? */
735076496e14763dcdef4b43202e7665d4a8da43vboxsync LogRel(("WARNING: unable to set up %ls registry key notifications.\n", wcsPath));
735076496e14763dcdef4b43202e7665d4a8da43vboxsync return false;
735076496e14763dcdef4b43202e7665d4a8da43vboxsync LogRel(("Subscription to termination event already established.\n"));
735076496e14763dcdef4b43202e7665d4a8da43vboxsync return false;
735076496e14763dcdef4b43202e7665d4a8da43vboxsync /* Finally, construct array item and queue it. */
735076496e14763dcdef4b43202e7665d4a8da43vboxsync struct Item oTmpItem = { hTmpKey, hTmpEvent, NULL };
735076496e14763dcdef4b43202e7665d4a8da43vboxsync /* Sub-tree keys should provide interface name (UUID). This is needed in order to
735076496e14763dcdef4b43202e7665d4a8da43vboxsync * collect all useful network settings to HostDnsInformation storage object to provide it to parent class. */
735076496e14763dcdef4b43202e7665d4a8da43vboxsync Log2(("Subscription to %ls established.\n", wcsPath));
735076496e14763dcdef4b43202e7665d4a8da43vboxsync return true;
735076496e14763dcdef4b43202e7665d4a8da43vboxsync /* Enumerate all the available interfaces. */
735076496e14763dcdef4b43202e7665d4a8da43vboxsync rc = RegOpenKeyExW(HKEY_LOCAL_MACHINE, pwcKeyRoot, 0, KEY_READ, &hTmpKey);
735076496e14763dcdef4b43202e7665d4a8da43vboxsync /* Get info about amount of available network interfaces. */
735076496e14763dcdef4b43202e7665d4a8da43vboxsync rc = RegQueryInfoKeyW(hTmpKey, NULL, NULL, NULL, &cSubKeys, &cbSubKeyNameMax, NULL, NULL, NULL, NULL, NULL, NULL);
735076496e14763dcdef4b43202e7665d4a8da43vboxsync /* Now iterate over interfaces if:
735076496e14763dcdef4b43202e7665d4a8da43vboxsync * 1) there are interfaces available and
735076496e14763dcdef4b43202e7665d4a8da43vboxsync * 2) maximum length of an interface name conforms to our buffer allocation size. */
735076496e14763dcdef4b43202e7665d4a8da43vboxsync if (cSubKeys > 0 && cbSubKeyNameMax <= VBOX_KEY_NAME_LEN_MAX)
735076496e14763dcdef4b43202e7665d4a8da43vboxsync for (DWORD idxSubKey = 0; idxSubKey < cSubKeys; idxSubKey++)
735076496e14763dcdef4b43202e7665d4a8da43vboxsync rc = RegEnumKeyW(hTmpKey, idxSubKey, sSubKeyName, VBOX_KEY_NAME_LEN_MAX);
735076496e14763dcdef4b43202e7665d4a8da43vboxsync /* Since we already know interface name (actually UUID), construct full registry path here. */
735076496e14763dcdef4b43202e7665d4a8da43vboxsync rc = wcscat_s(sSubKeyFullPath, VBOX_KEY_NAME_LEN_MAX, L"\\");
735076496e14763dcdef4b43202e7665d4a8da43vboxsync rc |= wcscat_s(sSubKeyFullPath, VBOX_KEY_NAME_LEN_MAX, sSubKeyName);
735076496e14763dcdef4b43202e7665d4a8da43vboxsync subscribeTo(sSubKeyFullPath, sSubKeyName, REG_NOTIFY_CHANGE_LAST_SET);
735076496e14763dcdef4b43202e7665d4a8da43vboxsync return true;
735076496e14763dcdef4b43202e7665d4a8da43vboxsync return false;
735076496e14763dcdef4b43202e7665d4a8da43vboxsync AssertReturnVoid(m_aWarehouse.size() > VBOX_OFFSET_SHUTDOWN_EVENT);
735076496e14763dcdef4b43202e7665d4a8da43vboxsync SetEvent(m_aWarehouse[VBOX_OFFSET_SHUTDOWN_EVENT].hEvent);
735076496e14763dcdef4b43202e7665d4a8da43vboxsyncvoid HostDnsServiceWin::extendVectorWithStrings(std::vector<std::string> &pVectorToExtend, const std::wstring &wcsParameter)
735076496e14763dcdef4b43202e7665d4a8da43vboxsync while (std::getline(wcsStream, wcsSubString, L' '))
735076496e14763dcdef4b43202e7665d4a8da43vboxsyncstatic void hostDnsWinDumpList(const std::vector<std::string> &awcszValues)
735076496e14763dcdef4b43202e7665d4a8da43vboxsync for (int idxItem = 0; idxItem < awcszValues.size(); idxItem++)
735076496e14763dcdef4b43202e7665d4a8da43vboxsync#endif /* DEBUG */
735076496e14763dcdef4b43202e7665d4a8da43vboxsyncHRESULT HostDnsServiceWin::updateInfo(uint8_t *fWhatsChanged)
735076496e14763dcdef4b43202e7665d4a8da43vboxsync /* Any interfaces available? */
735076496e14763dcdef4b43202e7665d4a8da43vboxsync if (m_aWarehouse.size() > VBOX_OFFSET_SUBTREE_EVENTS)
735076496e14763dcdef4b43202e7665d4a8da43vboxsync /* Walk across all the available interfaces and collect network configuration data:
735076496e14763dcdef4b43202e7665d4a8da43vboxsync * domain name, name servers and search list. */
735076496e14763dcdef4b43202e7665d4a8da43vboxsync for (int idxKey = VBOX_OFFSET_SUBTREE_EVENTS; idxKey < m_aWarehouse.size(); idxKey++)
735076496e14763dcdef4b43202e7665d4a8da43vboxsync /* Get number of key values. */
735076496e14763dcdef4b43202e7665d4a8da43vboxsync rc = RegQueryInfoKeyW(m_aWarehouse[idxKey].hKey, NULL, NULL, NULL, NULL, NULL, NULL, &cValues, NULL, NULL, NULL, NULL);
735076496e14763dcdef4b43202e7665d4a8da43vboxsync for (DWORD idxValue = 0; idxValue < cValues; idxValue++)
735076496e14763dcdef4b43202e7665d4a8da43vboxsync /* Walk across all the properties of given interface. */
735076496e14763dcdef4b43202e7665d4a8da43vboxsync rc = RegEnumValueW(m_aWarehouse[idxKey].hKey, idxValue, wcsValueName, &cbValueName, 0, NULL, (LPBYTE)wcsData, &cbData);
735076496e14763dcdef4b43202e7665d4a8da43vboxsync /* We rely on that fact that Windows host cannot be a member of more than one domain in the same time! */
735076496e14763dcdef4b43202e7665d4a8da43vboxsync pHostDnsInfo.domain = std::_Narrow_str(std::wstring(wcsData));
735076496e14763dcdef4b43202e7665d4a8da43vboxsync else if (( wcscmp(wcsValueName, L"NameServer") == 0
735076496e14763dcdef4b43202e7665d4a8da43vboxsync extendVectorWithStrings(pHostDnsInfo.servers, std::wstring(wcsData));
735076496e14763dcdef4b43202e7665d4a8da43vboxsync extendVectorWithStrings(pHostDnsInfo.searchList, std::wstring(wcsData));
735076496e14763dcdef4b43202e7665d4a8da43vboxsync /* Compare cached network settings and newly obtained ones. */
735076496e14763dcdef4b43202e7665d4a8da43vboxsync if (pHostDnsInfo.servers != m_hostInfoCache.servers)
735076496e14763dcdef4b43202e7665d4a8da43vboxsync#endif /* DEBUG */
735076496e14763dcdef4b43202e7665d4a8da43vboxsync m_hostInfoCache.domain.empty() ? "NONE" : m_hostInfoCache.domain.c_str(),
735076496e14763dcdef4b43202e7665d4a8da43vboxsync#endif /* DEBUG */
735076496e14763dcdef4b43202e7665d4a8da43vboxsync if (pHostDnsInfo.searchList != m_hostInfoCache.searchList)
735076496e14763dcdef4b43202e7665d4a8da43vboxsync#endif /* DEBUG */
735076496e14763dcdef4b43202e7665d4a8da43vboxsync /* Provide info about changes if requested. */
735076496e14763dcdef4b43202e7665d4a8da43vboxsync /* Update host network configuration cache. */
735076496e14763dcdef4b43202e7665d4a8da43vboxsync m_hostInfoCache.domain.assign(pHostDnsInfo.domain);
735076496e14763dcdef4b43202e7665d4a8da43vboxsync m_hostInfoCache.searchList = pHostDnsInfo.searchList;
735076496e14763dcdef4b43202e7665d4a8da43vboxsyncvoid HostDnsServiceWin::getEventHandles(HANDLE *ahEvents)
735076496e14763dcdef4b43202e7665d4a8da43vboxsync for (int idxHandle = 0; idxHandle < m_aWarehouse.size(); idxHandle++)
735076496e14763dcdef4b43202e7665d4a8da43vboxsync ahEvents[idxHandle] = m_aWarehouse[idxHandle].hEvent;
735076496e14763dcdef4b43202e7665d4a8da43vboxsync LogRel(("Host DNS monitor was not initialized properly.\n"));
735076496e14763dcdef4b43202e7665d4a8da43vboxsync while (true)
735076496e14763dcdef4b43202e7665d4a8da43vboxsync /* Each new iteration we need to update event handles list we monitor. */
735076496e14763dcdef4b43202e7665d4a8da43vboxsync DWORD rc = WaitForMultipleObjects((DWORD)m_aWarehouse.size(), ahEvents, FALSE, INFINITE);
735076496e14763dcdef4b43202e7665d4a8da43vboxsync ("WaitForMultipleObjects failed (%d) to wait! Please debug",
735076496e14763dcdef4b43202e7665d4a8da43vboxsync /* Shutdown requested. */
735076496e14763dcdef4b43202e7665d4a8da43vboxsync if (rc == (WAIT_OBJECT_0 + VBOX_OFFSET_SHUTDOWN_EVENT)) break;
735076496e14763dcdef4b43202e7665d4a8da43vboxsync /* Interfaces amount changed. */
735076496e14763dcdef4b43202e7665d4a8da43vboxsync else if (rc == (WAIT_OBJECT_0 + VBOX_OFFSET_TREE_EVENT))
735076496e14763dcdef4b43202e7665d4a8da43vboxsync /* Drop interface events. */
735076496e14763dcdef4b43202e7665d4a8da43vboxsync /* Drop event which is corresponds to interfaces tree changes. */
735076496e14763dcdef4b43202e7665d4a8da43vboxsync /* Restart interface tree monitoring. */
735076496e14763dcdef4b43202e7665d4a8da43vboxsync if (subscribeTo(pwcKeyRoot, NULL, REG_NOTIFY_CHANGE_NAME))
735076496e14763dcdef4b43202e7665d4a8da43vboxsync /* Restart interface events. */
735076496e14763dcdef4b43202e7665d4a8da43vboxsync LogRel(("Monitor unstable: failed to subscribe network configuration changes.\n"));
735076496e14763dcdef4b43202e7665d4a8da43vboxsync LogRel(("Monitor unstable: failed to subscribe interface changes.\n"));
735076496e14763dcdef4b43202e7665d4a8da43vboxsync LogRel(("Monitor unstable: failed to unsubscribe from interfaces amount changes.\n"));
735076496e14763dcdef4b43202e7665d4a8da43vboxsync LogRel(("Monitor unstable: failed to unsubscribe from previous notifications.\n"));
735076496e14763dcdef4b43202e7665d4a8da43vboxsync /* If something went wrong, we break monitoring. */
735076496e14763dcdef4b43202e7665d4a8da43vboxsync /* DNS update events range. */
735076496e14763dcdef4b43202e7665d4a8da43vboxsync else if (rc >= (WAIT_OBJECT_0 + VBOX_OFFSET_SUBTREE_EVENTS) &&
735076496e14763dcdef4b43202e7665d4a8da43vboxsync Log2(("Network setting has changed at interface %ls.\n", m_aWarehouse[rc - WAIT_OBJECT_0].wcsInterface));
735076496e14763dcdef4b43202e7665d4a8da43vboxsync /* Drop previous notifications first. */
735076496e14763dcdef4b43202e7665d4a8da43vboxsync /* Re-subscribe. */
735076496e14763dcdef4b43202e7665d4a8da43vboxsync LogRel(("WARNING: Monitor unstable: unable to re-subscribe to notifications.\n"));
735076496e14763dcdef4b43202e7665d4a8da43vboxsync LogRel(("WARNING: Monitor unstable: failed to unsubscribe from previous notifications.\n"));
735076496e14763dcdef4b43202e7665d4a8da43vboxsync /* If something went wrong, we stop monitoring. */
735076496e14763dcdef4b43202e7665d4a8da43vboxsync AssertMsgFailedReturn(("WaitForMultipleObjects returns out of bound (%d) index %d. Please debug!\n", m_aWarehouse.size(), rc), VERR_INTERNAL_ERROR);