From e389fb7924c51822e83b2abadf5c22ec7cb46692 Mon Sep 17 00:00:00 2001 From: Dmitry Fleytman Date: Mon, 26 Jan 2015 14:54:25 +0200 Subject: UsbDkHelper: Introduce hider API Signed-off-by: Kirill Moizik Signed-off-by: Dmitry Fleytman --- UsbDkController/UsbDkController.cpp | 2 + UsbDkHelper/DriverAccess.cpp | 11 +++- UsbDkHelper/DriverAccess.h | 12 ++++ UsbDkHelper/UsbDkHelper.cpp | 53 ++++++++++++++++ UsbDkHelper/UsbDkHelper.vcxproj | 1 + UsbDkHelper/UsbDkHelper.vcxproj.filters | 3 + UsbDkHelper/UsbDkHelperHider.h | 105 ++++++++++++++++++++++++++++++++ 7 files changed, 186 insertions(+), 1 deletion(-) create mode 100644 UsbDkHelper/UsbDkHelperHider.h diff --git a/UsbDkController/UsbDkController.cpp b/UsbDkController/UsbDkController.cpp index 6df7bc8..59b3b91 100644 --- a/UsbDkController/UsbDkController.cpp +++ b/UsbDkController/UsbDkController.cpp @@ -24,6 +24,8 @@ #include "stdafx.h" #include "UsbDkHelper.h" +#include "UsbDkHelperHider.h" +#include "UsbDkDataHider.h" using namespace std; //------------------------------------------------------------------------------- diff --git a/UsbDkHelper/DriverAccess.cpp b/UsbDkHelper/DriverAccess.cpp index f0f8b7d..c1f99b9 100644 --- a/UsbDkHelper/DriverAccess.cpp +++ b/UsbDkHelper/DriverAccess.cpp @@ -92,7 +92,16 @@ HANDLE UsbDkDriverAccess::AddRedirect(USB_DK_DEVICE_ID &DeviceID) { ULONG64 RedirectorHandle; SendIoctlWithDeviceId(IOCTL_USBDK_ADD_REDIRECT, DeviceID, &RedirectorHandle); - return reinterpret_cast(RedirectorHandle); } //------------------------------------------------------------------------------------------------ + +void UsbDkHiderAccess::AddHideRule(const USB_DK_HIDE_RULE &Rule) +{ + Ioctl(IOCTL_USBDK_ADD_HIDE_RULE, false, const_cast(&Rule), sizeof(Rule)); +} + +void UsbDkHiderAccess::ClearHideRules() +{ + Ioctl(IOCTL_USBDK_CLEAR_HIDE_RULES); +} diff --git a/UsbDkHelper/DriverAccess.h b/UsbDkHelper/DriverAccess.h index f7a7ca5..7771929 100644 --- a/UsbDkHelper/DriverAccess.h +++ b/UsbDkHelper/DriverAccess.h @@ -24,6 +24,7 @@ #pragma once #include "UsbDkData.h" +#include "UsbDkDataHider.h" #include "DriverFile.h" #include "UsbDkNames.h" @@ -63,3 +64,14 @@ private: } }; //----------------------------------------------------------------------------------- + +class UsbDkHiderAccess : public UsbDkDriverFile +{ +public: + UsbDkHiderAccess() + : UsbDkDriverFile(USBDK_USERMODE_HIDER_NAME) + {} + + void AddHideRule(const USB_DK_HIDE_RULE &Rule); + void ClearHideRules(); +}; diff --git a/UsbDkHelper/UsbDkHelper.cpp b/UsbDkHelper/UsbDkHelper.cpp index c81720a..06fd941 100644 --- a/UsbDkHelper/UsbDkHelper.cpp +++ b/UsbDkHelper/UsbDkHelper.cpp @@ -25,6 +25,8 @@ #include "stdafx.h" #include "UsbDkHelper.h" +#include "UsbDkHelperHider.h" +#include "UsbDkDataHider.h" #include "Installer.h" #include "DriverAccess.h" #include "RedirectorAccess.h" @@ -262,3 +264,54 @@ HANDLE UsbDk_GetRedirectorSystemHandle(HANDLE DeviceHandle) return deviceHandle->RedirectorAccess->GetSystemHandle(); } //------------------------------------------------------------------------------------------- + +HANDLE UsbDk_CreateHiderHandle() +{ + try + { + unique_ptr hiderAccess(new UsbDkHiderAccess); + return reinterpret_cast(hiderAccess.release()); + } + catch (const exception &e) + { + printExceptionString(e.what()); + return INVALID_HANDLE_VALUE; + } +} + +BOOL UsbDk_AddHideRule(HANDLE HiderHandle, PUSB_DK_HIDE_RULE Rule) +{ + auto HiderAccess = reinterpret_cast(HiderHandle); + + try + { + HiderAccess->AddHideRule(*Rule); + return TRUE; + } + catch (const exception &e) + { + printExceptionString(e.what()); + return FALSE; + } +} + +BOOL UsbDk_ClearHideRules(HANDLE HiderHandle) +{ + auto HiderAccess = reinterpret_cast(HiderHandle); + + try + { + HiderAccess->ClearHideRules(); + return TRUE; + } + catch (const exception &e) + { + printExceptionString(e.what()); + return FALSE; + } +} + +void UsbDk_CloseHiderHandle(HANDLE HiderHandle) +{ + delete reinterpret_cast(HiderHandle); +} diff --git a/UsbDkHelper/UsbDkHelper.vcxproj b/UsbDkHelper/UsbDkHelper.vcxproj index 8e45476..08779a8 100644 --- a/UsbDkHelper/UsbDkHelper.vcxproj +++ b/UsbDkHelper/UsbDkHelper.vcxproj @@ -974,6 +974,7 @@ + diff --git a/UsbDkHelper/UsbDkHelper.vcxproj.filters b/UsbDkHelper/UsbDkHelper.vcxproj.filters index b9bea0d..356e346 100644 --- a/UsbDkHelper/UsbDkHelper.vcxproj.filters +++ b/UsbDkHelper/UsbDkHelper.vcxproj.filters @@ -104,6 +104,9 @@ Header Files + + Header Files + diff --git a/UsbDkHelper/UsbDkHelperHider.h b/UsbDkHelper/UsbDkHelperHider.h new file mode 100644 index 0000000..309943f --- /dev/null +++ b/UsbDkHelper/UsbDkHelperHider.h @@ -0,0 +1,105 @@ +/********************************************************************** +* Copyright (c) 2013-2014 Red Hat, Inc. +* +* Developed by Daynix Computing LTD. +* +* Authors: +* Dmitry Fleytman +* Kirill Moizik +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* +**********************************************************************/ + +#pragma once + +// UsbDkHelper C-interface + +#ifdef BUILD_DLL +#define DLL __declspec(dllexport) +#else +#ifdef _MSC_VER +#define DLL __declspec(dllimport) +#else +#define DLL +#endif +#endif + +#include "UsbDkDataHider.h" +#ifdef __cplusplus +extern "C" { +#endif + +// UsbDk hider API provides hide device feature that allows to detach USB device from the hosting system. +// It can be useful in various scenarios, for example when there is no drivers for USB device on hosting machine +// and user would like to avoid dealing with "New Hardware" wizard. + + /* Create handle to hider interface of UsbDk driver + * + * @params + * IN - None + * OUT - None + * + * @return + * Handle to hider interface of UsbDk driver + * + * @note + * When this handle closes UsbDk clears all rules set by UsbDk_AddHideRule() + * + */ + DLL HANDLE UsbDk_CreateHiderHandle(void); + + /* Add a rule to determine which USB devices + * to be hidden when plugged in, a rule contains: + * + * class, vendor, product, version, allow + * + * Use -1 for @class/@vendor/@product/@version to accept any value. + * + * @params + * IN - HiderHandle Handle to UsbDk driver + - rule - pointer to hiding rule + * OUT - None + * + * @return + * TRUE if function succeeds + * + */ + DLL BOOL UsbDk_AddHideRule(HANDLE HiderHandle, PUSB_DK_HIDE_RULE Rule); + + /* Clear all hider rules + * + * @params + * IN - HiderHandle Handle to UsbDk driver + * OUT - None + * + * @return + * TRUE if function succeeds + * + */ + DLL BOOL UsbDk_ClearHideRules(HANDLE HiderHandle); + + /* Close Handle to UsbDk hider interface + * + * @params + * IN - HiderHandle Handle to UsbDk driver + * OUT - None + * + * @return + * None + * + */ + DLL void UsbDk_CloseHiderHandle(HANDLE HiderHandle); +#ifdef __cplusplus +} +#endif -- cgit v1.2.3