api: unify NetCfgInstanceId registry retrieval

Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
Simon Rozman 2021-02-01 10:41:36 +01:00
parent 49a106b13e
commit 7a6bab7f04

View File

@ -427,7 +427,23 @@ static _Return_type_success_(return != FALSE) BOOL
} }
static _Return_type_success_(return != FALSE) BOOL static _Return_type_success_(return != FALSE) BOOL
GetNetCfgInstanceId(_In_ HDEVINFO DevInfo, _In_ SP_DEVINFO_DATA *DevInfoData, _Out_ GUID *CfgInstanceID) GetNetCfgInstanceIdFromHKEY(_In_ HKEY Key, _Out_ GUID *CfgInstanceID)
{
WCHAR *ValueStr = RegistryQueryString(Key, L"NetCfgInstanceId", TRUE);
if (!ValueStr)
return RET_ERROR(TRUE, LOG(WINTUN_LOG_ERR, L"Failed to get NetCfgInstanceId"));
DWORD LastError = ERROR_SUCCESS;
if (FAILED(CLSIDFromString(ValueStr, CfgInstanceID)))
{
LOG(WINTUN_LOG_ERR, L"NetCfgInstanceId is not a GUID");
LastError = ERROR_INVALID_DATA;
}
Free(ValueStr);
return RET_ERROR(TRUE, LastError);
}
static _Return_type_success_(return != FALSE) BOOL
GetNetCfgInstanceIdFromDevInfo(_In_ HDEVINFO DevInfo, _In_ SP_DEVINFO_DATA *DevInfoData, _Out_ GUID *CfgInstanceID)
{ {
HKEY Key = SetupDiOpenDevRegKey(DevInfo, DevInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DRV, KEY_QUERY_VALUE); HKEY Key = SetupDiOpenDevRegKey(DevInfo, DevInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DRV, KEY_QUERY_VALUE);
if (Key == INVALID_HANDLE_VALUE) if (Key == INVALID_HANDLE_VALUE)
@ -436,19 +452,8 @@ static _Return_type_success_(return != FALSE) BOOL
return FALSE; return FALSE;
} }
DWORD LastError = ERROR_SUCCESS; DWORD LastError = ERROR_SUCCESS;
WCHAR *ValueStr = RegistryQueryString(Key, L"NetCfgInstanceId", TRUE); if (!GetNetCfgInstanceIdFromHKEY(Key, CfgInstanceID))
if (!ValueStr) LastError = GetLastError();
{
LastError = LOG(WINTUN_LOG_ERR, L"Failed to get NetCfgInstanceId");
goto cleanupKey;
}
if (FAILED(CLSIDFromString(ValueStr, CfgInstanceID)))
{
LOG(WINTUN_LOG_ERR, L"NetCfgInstanceId is not a GUID");
LastError = ERROR_INVALID_DATA;
}
Free(ValueStr);
cleanupKey:
RegCloseKey(Key); RegCloseKey(Key);
return RET_ERROR(TRUE, LastError); return RET_ERROR(TRUE, LastError);
} }
@ -472,7 +477,7 @@ static _Return_type_success_(return != FALSE) BOOL
continue; continue;
} }
GUID CfgInstanceID2; GUID CfgInstanceID2;
if (GetNetCfgInstanceId(*DevInfo, DevInfoData, &CfgInstanceID2) && if (GetNetCfgInstanceIdFromDevInfo(*DevInfo, DevInfoData, &CfgInstanceID2) &&
!memcmp(CfgInstanceID, &CfgInstanceID2, sizeof(GUID))) !memcmp(CfgInstanceID, &CfgInstanceID2, sizeof(GUID)))
return TRUE; return TRUE;
} }
@ -561,20 +566,11 @@ static _Return_type_success_(return != NULL) WINTUN_ADAPTER
goto cleanupKey; goto cleanupKey;
} }
WCHAR *ValueStr = RegistryQueryString(Key, L"NetCfgInstanceId", TRUE); if (!GetNetCfgInstanceIdFromHKEY(Key, &Adapter->CfgInstanceID))
if (!ValueStr)
{ {
LastError = LOG(WINTUN_LOG_ERR, L"Failed to get NetCfgInstanceId"); LastError = GetLastError();
goto cleanupAdapter; goto cleanupAdapter;
} }
if (FAILED(CLSIDFromString(ValueStr, &Adapter->CfgInstanceID)))
{
LOG(WINTUN_LOG_ERR, L"NetCfgInstanceId is not Adapter GUID");
Free(ValueStr);
LastError = ERROR_INVALID_DATA;
goto cleanupAdapter;
}
Free(ValueStr);
if (!RegistryQueryDWORD(Key, L"NetLuidIndex", &Adapter->LuidIndex, TRUE)) if (!RegistryQueryDWORD(Key, L"NetLuidIndex", &Adapter->LuidIndex, TRUE))
{ {
@ -672,7 +668,7 @@ _Return_type_success_(return != NULL) WINTUN_ADAPTER *WINAPI
} }
GUID CfgInstanceID; GUID CfgInstanceID;
if (!GetNetCfgInstanceId(DevInfo, &DevInfoData, &CfgInstanceID)) if (!GetNetCfgInstanceIdFromDevInfo(DevInfo, &DevInfoData, &CfgInstanceID))
continue; continue;
/* TODO: is there a better way than comparing ifnames? */ /* TODO: is there a better way than comparing ifnames? */