VirtualBox

Ignore:
Timestamp:
Mar 26, 2019 8:11:25 AM (6 years ago)
Author:
vboxsync
Message:

NetAdp/Win: (bugref:9409) Work around host-only adapter naming issue after Windows upgrade.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/HostDrivers/VBoxNetFlt/win/cfg/VBoxNetCfg.cpp

    r77804 r77875  
    4949#include <iprt/win/iphlpapi.h>
    5050
     51#include <set>
    5152
    5253#ifndef Assert   /** @todo r=bird: where would this be defined? */
     
    29512952}
    29522953
     2954/* We assume the following name matches the device description in vboxnetadp6.inf */
     2955#define HOSTONLY_ADAPTER_NAME "VirtualBox Host-Only Ethernet Adapter"
     2956
     2957/*
     2958 * Enumerate all host-only adapters collecting their names into a set, then
     2959 * come up with the next available name by taking the first unoccupied index.
     2960 */
     2961static HRESULT vboxNetCfgWinNextAvailableDevName(bstr_t& bstrName)
     2962{
     2963    SP_DEVINFO_DATA DeviceInfoData;
     2964    /* initialize the structure size */
     2965    DeviceInfoData.cbSize = sizeof (SP_DEVINFO_DATA);
     2966
     2967    HDEVINFO DeviceInfoSet = SetupDiGetClassDevs(&GUID_DEVCLASS_NET, NULL, NULL, DIGCF_PRESENT);
     2968    if (DeviceInfoSet == INVALID_HANDLE_VALUE)
     2969        return GetLastError();
     2970
     2971    DWORD i;
     2972    std::set<bstr_t> aExistingNames;
     2973    for (i = 0; SetupDiEnumDeviceInfo(DeviceInfoSet, i, &DeviceInfoData); ++i)
     2974    {
     2975        /* Should be more than enough for both our device id and our device name, we do not care about the rest */
     2976        WCHAR wszDevName[64];
     2977        if (!SetupDiGetDeviceRegistryProperty(DeviceInfoSet, &DeviceInfoData, SPDRP_HARDWAREID,
     2978                                              NULL, (PBYTE)wszDevName, sizeof(wszDevName), NULL))
     2979            continue;
     2980        /* Ignore everything except our host-only adapters */
     2981        if (_wcsicmp(wszDevName, DRIVERHWID))
     2982            continue;
     2983        if (   SetupDiGetDeviceRegistryProperty(DeviceInfoSet, &DeviceInfoData, SPDRP_FRIENDLYNAME,
     2984                                                NULL, (PBYTE)wszDevName, sizeof(wszDevName), NULL)
     2985            || SetupDiGetDeviceRegistryProperty(DeviceInfoSet, &DeviceInfoData, SPDRP_DEVICEDESC,
     2986                                                NULL, (PBYTE)wszDevName, sizeof(wszDevName), NULL))
     2987            aExistingNames.insert(bstr_t(wszDevName));
     2988    }
     2989    /* Try the name without index first */
     2990    bstrName = HOSTONLY_ADAPTER_NAME;
     2991    if (aExistingNames.find(bstrName) != aExistingNames.end())
     2992    {
     2993        WCHAR wszSuffix[16];
     2994        /* Try indexed names until we find unused one */
     2995        for (i = 2;; ++i)
     2996        {
     2997            wsprintf(wszSuffix, L" #%u", i);
     2998            if (aExistingNames.find(bstrName + wszSuffix) == aExistingNames.end())
     2999            {
     3000                bstrName += wszSuffix;
     3001                break;
     3002            }
     3003        }
     3004    }
     3005
     3006    if (DeviceInfoSet)
     3007        SetupDiDestroyDeviceInfoList(DeviceInfoSet);
     3008    return S_OK;
     3009}
     3010
    29533011static HRESULT vboxNetCfgWinCreateHostOnlyNetworkInterface(IN LPCWSTR pInfPath, IN bool bIsInfPathFile, IN BSTR bstrDesiredName,
    29543012                                                           OUT GUID *pGuid, OUT BSTR *lppszName, OUT BSTR *pErrMsg)
     
    29663024    HKEY hkey = (HKEY)INVALID_HANDLE_VALUE;
    29673025    bstr_t bstrError;
     3026    bstr_t bstrNewInterfaceName;
     3027
     3028    if (SysStringLen(bstrDesiredName) != 0)
     3029        bstrNewInterfaceName = bstrDesiredName;
     3030    else
     3031    {
     3032        hrc = vboxNetCfgWinNextAvailableDevName(bstrNewInterfaceName);
     3033        if (FAILED(hrc))
     3034            NonStandardLogFlow(("vboxNetCfgWinNextAvailableDevName failed with 0x%x\n", hrc));
     3035    }
    29683036
    29693037    do
     
    33243392
    33253393
    3326 #ifndef VBOXNETCFG_DELAYEDRENAME
    33273394        /*
    33283395         * We need to query the device name after we have succeeded in querying its
     
    33563423            }
    33573424        }
    3358 #else /* !VBOXNETCFG_DELAYEDRENAME */
     3425        /* No need to rename the device if the names match. */
     3426        if (!wcscmp(bstrNewInterfaceName.GetBSTR(), DevName))
     3427            bstrNewInterfaceName.Assign(NULL);
     3428#ifdef VBOXNETCFG_DELAYEDRENAME
    33593429        /* Re-use DevName for device instance id retrieval. */
    33603430        if (!SetupDiGetDeviceInstanceId(hDeviceInfo, &DeviceInfoData, DevName, RT_ELEMENTS(DevName), &cbSize))
    33613431            SetErrBreak (("SetupDiGetDeviceInstanceId failed (0x%08X)",
    33623432                          GetLastError()));
    3363 #endif /* !VBOXNETCFG_DELAYEDRENAME */
     3433#endif /* VBOXNETCFG_DELAYEDRENAME */
    33643434    }
    33653435    while (0);
     
    34133483        if (hr == S_OK)
    34143484        {
    3415             if (SysStringLen(bstrDesiredName) != 0)
    3416             {
    3417                 /* Rename only if the desired name has been provided */
    3418                 context.bstrName = bstrDesiredName;
     3485            if (!!bstrNewInterfaceName)
     3486            {
     3487                /* The assigned name does not match the desired one, rename the device */
     3488                context.bstrName = bstrNewInterfaceName.GetBSTR();
    34193489                context.pGuid = pGuid;
    34203490                hr = vboxNetCfgWinEnumNetCfgComponents(pNetCfg,
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette