From 8e33891593b2417ec94528f9b53b0df5b4bb820c Mon Sep 17 00:00:00 2001 From: zeroZshadow Date: Tue, 11 Aug 2015 23:34:49 +0200 Subject: [PATCH 1/2] Properly scan for OpenVPN TAP adapters Handle errors correctly in SendFrame --- Source/Core/Core/HW/BBA-TAP/TAP_Win32.cpp | 29 ++++++++--------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/Source/Core/Core/HW/BBA-TAP/TAP_Win32.cpp b/Source/Core/Core/HW/BBA-TAP/TAP_Win32.cpp index 696cf8b2e5..c27487337b 100644 --- a/Source/Core/Core/HW/BBA-TAP/TAP_Win32.cpp +++ b/Source/Core/Core/HW/BBA-TAP/TAP_Win32.cpp @@ -88,14 +88,13 @@ bool GetGUIDs(std::vector>& guids) HKEY control_net_key; DWORD len; int i = 0; - bool found_all = false; status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, NETWORK_CONNECTIONS_KEY, 0, KEY_READ, &control_net_key); if (status != ERROR_SUCCESS) return false; - while (!found_all) + for (;; i++) { TCHAR enum_name[256]; TCHAR connection_string[256]; @@ -111,7 +110,7 @@ bool GetGUIDs(std::vector>& guids) if (status == ERROR_NO_MORE_ITEMS) break; else if (status != ERROR_SUCCESS) - return false; + continue; _sntprintf(connection_string, sizeof(connection_string), _T("%s\\%s\\Connection"), NETWORK_CONNECTIONS_KEY, enum_name); @@ -127,28 +126,23 @@ bool GetGUIDs(std::vector>& guids) if (status != ERROR_SUCCESS || name_type != REG_SZ) { - return false; + continue; } else { if (IsTAPDevice(enum_name)) { guids.push_back(enum_name); - //found_all = true; } } RegCloseKey(connection_key); } - i++; } RegCloseKey(control_net_key); - //if (!found_all) - //return false; - - return true; + return !guids.empty(); } bool OpenTAP(HANDLE& adapter, const std::basic_string& device_guid) @@ -187,7 +181,7 @@ bool CEXIETHERNET::Activate() if (Win32TAPHelper::OpenTAP(mHAdapter, device_guids.at(i))) { INFO_LOG(SP1, "OPENED %s", device_guids.at(i).c_str()); - i = device_guids.size(); + break; } } if (mHAdapter == INVALID_HANDLE_VALUE) @@ -248,19 +242,16 @@ bool CEXIETHERNET::SendFrame(u8 *frame, u32 size) DEBUG_LOG(SP1, "SendFrame %x\n%s", size, ArrayToString(frame, size, 0x10).c_str()); - DWORD numBytesWrit; OVERLAPPED overlap; ZeroMemory(&overlap, sizeof(overlap)); - if (!WriteFile(mHAdapter, frame, size, &numBytesWrit, &overlap)) - { - DWORD res = GetLastError(); - ERROR_LOG(SP1, "Failed to send packet with error 0x%X", res); - } + //WriteFile will always return false because the TAP handle is async + WriteFile(mHAdapter, frame, size, NULL, &overlap); - if (numBytesWrit != size) + DWORD res = GetLastError(); + if (res != ERROR_IO_PENDING) { - ERROR_LOG(SP1, "BBA SendFrame %i only got %i bytes sent!", size, numBytesWrit); + ERROR_LOG(SP1, "Failed to send packet with error 0x%X", res); } // Always report the packet as being sent successfully, even though it might be a lie From 7ed894484e4ee632d014c73b0fec242f2106d45c Mon Sep 17 00:00:00 2001 From: zeroZshadow Date: Wed, 12 Aug 2015 01:18:27 +0200 Subject: [PATCH 2/2] Added for loop end condition --- Source/Core/Core/HW/BBA-TAP/TAP_Win32.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/Source/Core/Core/HW/BBA-TAP/TAP_Win32.cpp b/Source/Core/Core/HW/BBA-TAP/TAP_Win32.cpp index c27487337b..e9ffd400f4 100644 --- a/Source/Core/Core/HW/BBA-TAP/TAP_Win32.cpp +++ b/Source/Core/Core/HW/BBA-TAP/TAP_Win32.cpp @@ -87,14 +87,19 @@ bool GetGUIDs(std::vector>& guids) LONG status; HKEY control_net_key; DWORD len; - int i = 0; + DWORD cSubKeys = 0; - status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, NETWORK_CONNECTIONS_KEY, 0, KEY_READ, &control_net_key); + status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, NETWORK_CONNECTIONS_KEY, 0, KEY_READ | KEY_QUERY_VALUE, &control_net_key); if (status != ERROR_SUCCESS) return false; - for (;; i++) + status = RegQueryInfoKey(control_net_key, nullptr, nullptr, nullptr, &cSubKeys, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr); + + if (status != ERROR_SUCCESS) + return false; + + for (DWORD i = 0; i < cSubKeys; i++) { TCHAR enum_name[256]; TCHAR connection_string[256]; @@ -107,9 +112,7 @@ bool GetGUIDs(std::vector>& guids) status = RegEnumKeyEx(control_net_key, i, enum_name, &len, nullptr, nullptr, nullptr, nullptr); - if (status == ERROR_NO_MORE_ITEMS) - break; - else if (status != ERROR_SUCCESS) + if (status != ERROR_SUCCESS) continue; _sntprintf(connection_string, sizeof(connection_string),