Sample Code
Windows Driver Samples/ Windows Filtering Platform Sample/ C++/ svc/ Scenarios_BasicAction.cpp/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 | //////////////////////////////////////////////////////////////////////////////////////////////////// // // Copyright (c) 2012 Microsoft Corporation. All Rights Reserved. // // Module Name: // Scenarios_BasicAction.cpp // // Abstract: // This module contains functions which implements the BASIC_ACTION_* scenarios. // // Naming Convention: // // <Scope><Object><Action><Modifier> // <Scope><Object><Action> // // i.e. // // <Scope> // { // - Function is likely visible to other modules. // Prv - Function is private to this module. // } // <Object> // { // ScenarioBasicActionBlock - Function pertains to the Basic Action Block Scenario. // ScenarioBasicActionContinue - Function pertains to the Basic Action Continue Scenario. // ScenarioBasicActionPermit - Function pertains to the Basic Action Permit Scenario. // ScenarioBasicActionRandom - Function pertains to the Basic Action Random Scenario. // RPC - Function is and RPC entry point. // } // <Action> // { // Add - Function adds objects // Remove - Function removes objects // Invoke - Function implements the scenario based on parameters // passed from the commandline interface // (WFPSampler.exe). // } // <Modifier> // { // FwpmObjects - Function acts on WFP objects. // ScenarioBasicAction - Function pertains to all of the Basic Action Scenarios. // } // // Private Functions: // PrvScenarioBasicActionAddFwpmObjects(), // PrvScenarioBasicActionDeleteFwpmObjects(), // ScenarioBasicActionBlockRemove(), // ScenarioBasicActionBlockAdd(), // ScenarioBasicActionContinueRemove(), // ScenarioBasicActionContinueAdd(), // ScenarioBasicActionPermitRemove(), // ScenarioBasicActionPermitAdd(), // ScenarioBasicActionRandomRemove(), // ScenarioBasicActionRandomAdd(), // // Public Functions: // RPCInvokeScenarioBasicAction(). // // Author: // Dusty Harper (DHarper) // // Revision History: // // [ Month ][Day] [Year] - [Revision]-[ Comments ] // May 01, 2010 - 1.0 - Creation // //////////////////////////////////////////////////////////////////////////////////////////////////// #include "Framework_WFPSamplerService.h" /// . /** @private_function="PrvScenarioBasicActionDeleteFwpmObjects" Purpose: Function that disables the SCENARIO_BASIC_ACTION_* scenarios. <br> <br> Notes: Scenario removes the filters using specified filtering conditions at the specified layer. If no layer is provided, then a default layer is used. If associated, the callout and provider contexts are removed as well. <br> <br> MSDN_Ref: <br> */ _Success_( return == NO_ERROR) UINT32 PrvScenarioBasicActionDeleteFwpmObjects(_In_ const FWPM_FILTER* pFilter, _In_ FWP_ACTION_TYPE actionType) { ASSERT(pFilter); UINT32 status = NO_ERROR; HANDLE engineHandle = 0; HANDLE enumHandle = 0; UINT32 entryCount = 0; FWPM_FILTER** ppFilters = 0; FWPM_FILTER_ENUM_TEMPLATE filterEnumTemplate = {0}; filterEnumTemplate.providerKey = (GUID*)&WFPSAMPLER_PROVIDER; filterEnumTemplate.layerKey = pFilter->layerKey; filterEnumTemplate.enumType = FWP_FILTER_ENUM_FULLY_CONTAINED; filterEnumTemplate.flags = FWP_FILTER_ENUM_FLAG_INCLUDE_BOOTTIME | FWP_FILTER_ENUM_FLAG_INCLUDE_DISABLED; filterEnumTemplate.numFilterConditions = pFilter->numFilterConditions; filterEnumTemplate.filterCondition = pFilter->filterCondition; filterEnumTemplate.actionMask = 0xFFFFFFFF; status = HlprFwpmEngineOpen(&engineHandle); HLPR_BAIL_ON_FAILURE(status); status = HlprFwpmFilterCreateEnumHandle(engineHandle, &filterEnumTemplate, &enumHandle); HLPR_BAIL_ON_FAILURE(status); status = HlprFwpmFilterEnum(engineHandle, enumHandle, 0xFFFFFFFF, &ppFilters, &entryCount); HLPR_BAIL_ON_FAILURE(status); if (ppFilters) { for ( UINT32 filterIndex = 0; filterIndex < entryCount; filterIndex++) { if (ppFilters[filterIndex]->action.type == actionType || ppFilters[filterIndex]->action.type & FWP_ACTION_FLAG_CALLOUT) { HlprFwpmFilterDeleteByKey(engineHandle, &(ppFilters[filterIndex]->filterKey)); if (ppFilters[filterIndex]->flags & FWPM_FILTER_FLAG_HAS_PROVIDER_CONTEXT) HlprFwpmProviderContextDeleteByKey(engineHandle, &(ppFilters[filterIndex]->providerContextKey)); if (ppFilters[filterIndex]->action.type & FWP_ACTION_FLAG_CALLOUT) HlprFwpmCalloutDeleteByKey(engineHandle, &(ppFilters[filterIndex]->action.calloutKey)); } } FwpmFreeMemory(( void **)&ppFilters); } HLPR_BAIL_LABEL: if (engineHandle) { if (enumHandle) HlprFwpmFilterDestroyEnumHandle(engineHandle, &enumHandle); HlprFwpmEngineClose(&engineHandle); } return status; } /** @private_function="PrvScenarioBasicActionAddFwpmObjects" Purpose: Function that enables the SCENARIO_BASIC_ACTION_* scenarios. <br> <br> Notes: Scenario adds a filter using specified filtering conditions to the specified layer. If no layer is provided, then a default layer is used. This filter is associated with WFPSampler's default sublayer and provider. If required, the appropriate callout and provider context is added and associated with the filter. <br> <br> MSDN_Ref: <br> */ _Success_( return == NO_ERROR) UINT32 PrvScenarioBasicActionAddFwpmObjects(_In_ const FWPM_FILTER* pFilter, _In_ FWP_ACTION_TYPE actionType = 0, _In_opt_ const PC_BASIC_ACTION_DATA* pPCBasicActionData = 0) { ASSERT(pFilter); UINT32 status = NO_ERROR; HANDLE engineHandle = 0; FWPM_CALLOUT* pCallout = 0; FWPM_PROVIDER_CONTEXT* pProviderContext = 0; FWP_BYTE_BLOB* pByteBlob = 0; FWPM_FILTER filter = {0}; RtlCopyMemory(&filter, pFilter, sizeof (FWPM_FILTER)); status = HlprGUIDPopulate(&(filter.filterKey)); HLPR_BAIL_ON_FAILURE(status); filter.providerKey = (GUID*)&WFPSAMPLER_PROVIDER; filter.subLayerKey = WFPSAMPLER_SUBLAYER; filter.weight.type = FWP_UINT8; filter.weight.uint8 = 0xF; if (filter.action.type & FWP_ACTION_FLAG_CALLOUT) { FWP_ACTION_TYPE newActionType = FWP_ACTION_CALLOUT_TERMINATING; HLPR_NEW(pCallout, FWPM_CALLOUT); HLPR_BAIL_ON_ALLOC_FAILURE(pCallout, status); if (actionType == FWP_ACTION_BLOCK) { pCallout->calloutKey = WFPSAMPLER_CALLOUT_BASIC_ACTION_BLOCK; pCallout->displayData.name = L "WFPSampler's Basic Block Callout" ; pCallout->displayData.description = L "Causes callout invocation which returns FWP_ACTION_BLOCK" ; } else if (actionType == FWP_ACTION_CONTINUE) { pCallout->calloutKey = WFPSAMPLER_CALLOUT_BASIC_ACTION_CONTINUE; pCallout->displayData.name = L "WFPSampler's Basic Continue Callout" ; pCallout->displayData.description = L "Causes callout invocation which returns FWP_ACTION_CONTINUE" ; newActionType = FWP_ACTION_CALLOUT_UNKNOWN; } else if (actionType == FWP_ACTION_PERMIT) { pCallout->calloutKey = WFPSAMPLER_CALLOUT_BASIC_ACTION_PERMIT; pCallout->displayData.name = L "WFPSampler's Basic Permit Callout" ; pCallout->displayData.description = L "Causes callout invocation which returns FWP_ACTION_PERMIT" ; } else { HLPR_NEW(pProviderContext, FWPM_PROVIDER_CONTEXT); HLPR_BAIL_ON_ALLOC_FAILURE(pProviderContext, status); HLPR_NEW(pByteBlob, FWP_BYTE_BLOB); HLPR_BAIL_ON_ALLOC_FAILURE(pProviderContext, status); status = HlprGUIDPopulate(&(pProviderContext->providerContextKey)); HLPR_BAIL_ON_FAILURE(status); pProviderContext->displayData.name = L "WFPSampler's Basic Random Action Provider Context" ; pProviderContext->displayData.description = L "Testing Purposes Only!!!" ; pProviderContext->providerKey = (GUID*)&WFPSAMPLER_PROVIDER; pProviderContext->type = FWPM_GENERAL_CONTEXT; pProviderContext->dataBuffer = pByteBlob; pProviderContext->dataBuffer->size = sizeof (PC_BASIC_ACTION_DATA); pProviderContext->dataBuffer->data = ( BYTE *)pPCBasicActionData; pCallout->calloutKey = WFPSAMPLER_CALLOUT_BASIC_ACTION_RANDOM; pCallout->displayData.name = L "WFPSampler's Basic Random Action Callout" ; pCallout->displayData.description = L "Testing Purposes Only!!! Causes callout invocation which randomly chooses an action to return." ; pCallout->flags |= FWPM_CALLOUT_FLAG_USES_PROVIDER_CONTEXT; filter.flags |= FWPM_FILTER_FLAG_HAS_PROVIDER_CONTEXT; filter.providerContextKey = pProviderContext->providerContextKey; newActionType = FWP_ACTION_CALLOUT_UNKNOWN; } pCallout->calloutKey.Data4[7] = HlprFwpmLayerGetIDByKey(&(filter.layerKey)); /// Uniquely identifies the callout used pCallout->providerKey = (GUID*)&WFPSAMPLER_PROVIDER; pCallout->applicableLayer = filter.layerKey; if (filter.flags & FWPM_FILTER_FLAG_BOOTTIME || filter.flags & FWPM_FILTER_FLAG_PERSISTENT) { if (pProviderContext) pProviderContext->flags = FWPM_PROVIDER_CONTEXT_FLAG_PERSISTENT; pCallout->flags = FWPM_CALLOUT_FLAG_PERSISTENT; } filter.action.type = newActionType; filter.action.calloutKey = pCallout->calloutKey; } else { if (actionType & FWP_ACTION_FLAG_TERMINATING) filter.action.type = actionType; else filter.action.type = FWP_ACTION_BLOCK; } status = HlprFwpmEngineOpen(&engineHandle); HLPR_BAIL_ON_FAILURE(status); status = HlprFwpmTransactionBegin(engineHandle); HLPR_BAIL_ON_FAILURE(status); if (pCallout) { status = HlprFwpmCalloutAdd(engineHandle, pCallout); HLPR_BAIL_ON_FAILURE(status); } if (pProviderContext) { status = HlprFwpmProviderContextAdd(engineHandle, pProviderContext); HLPR_BAIL_ON_FAILURE(status); } status = HlprFwpmFilterAdd(engineHandle, &filter); HLPR_BAIL_ON_FAILURE(status); status = HlprFwpmTransactionCommit(engineHandle); HLPR_BAIL_ON_FAILURE(status); HLPR_BAIL_LABEL: if (engineHandle) { if (status != NO_ERROR) HlprFwpmTransactionAbort(engineHandle); HlprFwpmEngineClose(&engineHandle); } HLPR_DELETE(pByteBlob); HLPR_DELETE(pProviderContext); HLPR_DELETE(pCallout); return status; } /** @scenario_function="ScenarioBasicActionBlockRemove" Purpose: Function that removes corresponding objects for a previously added SCENARIO_BASIC_ACTION_BLOCK. <br> <br> Notes: <br> <br> MSDN_Ref: <br> */ _Success_( return == NO_ERROR) UINT32 ScenarioBasicActionBlockRemove(_In_ const FWPM_FILTER* pFilter) { ASSERT(pFilter); return PrvScenarioBasicActionDeleteFwpmObjects(pFilter, FWP_ACTION_BLOCK); } /** @scenario_function="ScenarioBasicActionBlockAdd" Purpose: Scenario which will return FWP_ACTION_BLOCK. <br> <br> Notes: Adds a filter. By default this will be a static terminating filter. If specified, this filter can be made to reference one of the WFPSAMPLER_CALLOUT_BASIC_ACTION_BLOCK callouts. <br> <br> MSDN_Ref: <br> */ _Success_( return == NO_ERROR) UINT32 ScenarioBasicActionBlockAdd(_In_ const FWPM_FILTER* pFilter) { ASSERT(pFilter); return PrvScenarioBasicActionAddFwpmObjects(pFilter, FWP_ACTION_BLOCK, 0); } /** @scenario_function="ScenarioBasicActionContinueRemove" Purpose: Function that removes corresponding objects for a previously added SCENARIO_BASIC_ACTION_CONTINUE. <br> <br> Notes: <br> <br> MSDN_Ref: <br> */ _Success_( return == NO_ERROR) UINT32 ScenarioBasicActionContinueRemove(_In_ const FWPM_FILTER* pFilter) { ASSERT(pFilter); return PrvScenarioBasicActionDeleteFwpmObjects(pFilter, FWP_ACTION_CONTINUE); } /** @scenario_function="ScenarioBasicActionContinueAdd" Purpose: Scenario which will return FWP_ACTION_CONTINUE. <br> <br> Notes: Adds a filter. By default this will be a static terminating filter. If specified, this filter can be made to reference one of the WFPSAMPLER_CALLOUT_BASIC_ACTION_CONTINUE callouts. <br> <br> MSDN_Ref: <br> */ _Success_( return == NO_ERROR) UINT32 ScenarioBasicActionContinueAdd(_In_ const FWPM_FILTER* pFilter) { ASSERT(pFilter); return PrvScenarioBasicActionAddFwpmObjects(pFilter, FWP_ACTION_CONTINUE, 0); } /** @scenario_function="ScenarioBasicActionPermitRemove" Purpose: Function that removes corresponding objects for a previously added SCENARIO_BASIC_ACTION_PERMIT. <br> <br> Notes: <br> <br> MSDN_Ref: <br> */ _Success_( return == NO_ERROR) UINT32 ScenarioBasicActionPermitRemove(_In_ const FWPM_FILTER* pFilter) { ASSERT(pFilter); return PrvScenarioBasicActionDeleteFwpmObjects(pFilter, FWP_ACTION_PERMIT); } /** @scenario_function="ScenarioBasicActionPermitAdd" Purpose: Scenario which will return FWP_ACTION_PERMIT. <br> <br> Notes: Adds a filter. By default this will be a static terminating filter. If specified, this filter can be made to reference one of the WFPSAMPLER_CALLOUT_BASIC_ACTION_PERMIT callouts. <br> <br> MSDN_Ref: <br> */ _Success_( return == NO_ERROR) UINT32 ScenarioBasicActionPermitAdd(_In_ const FWPM_FILTER* pFilter) { ASSERT(pFilter); return PrvScenarioBasicActionAddFwpmObjects(pFilter, FWP_ACTION_PERMIT, 0); } /** @scenario_function="ScenarioBasicActionRandomRemove" Purpose: Function that removes corresponding objects for a previously added SCENARIO_BASIC_ACTION_RANDOM. <br> <br> Notes: <br> <br> MSDN_Ref: <br> */ _Success_( return == NO_ERROR) UINT32 ScenarioBasicActionRandomRemove(_In_ const FWPM_FILTER* pFilter) { ASSERT(pFilter); return PrvScenarioBasicActionDeleteFwpmObjects(pFilter, FWP_ACTION_CALLOUT_UNKNOWN); } /** @scenario_function="ScenarioBasicActionRandomAdd" Purpose: Scenario which will randomly return FWP_ACTION_BLOCK, FWP_ACTION_CONTINUE, or FWP_ACTION_PERMIT. <br> <br> Notes: Adds a filter which references one of the WFPSAMPLER_CALLOUT_BASIC_ACTION_RANDOM callouts for the provided layer. <br> <br> The randomness of the action is configurable via the percentage values for this scenario. <br> <br> MSDN_Ref: <br> */ _Success_( return == NO_ERROR) UINT32 ScenarioBasicActionRandomAdd(_In_ const FWPM_FILTER* pFilter, _In_ const PC_BASIC_ACTION_DATA* pPCBasicActionData) { ASSERT(pFilter); ASSERT(pPCBasicActionData); return PrvScenarioBasicActionAddFwpmObjects(pFilter, 0, pPCBasicActionData); } /** @rpc_function="RPCInvokeScenarioBasicAction" Purpose: RPC exposed function used to dipatch the scenario routines for SCENARIO_BASIC_ACTION_*. <br> <br> Notes: <br> <br> MSDN_Ref: <br> */ /* [fault_status][comm_status] */ error_status_t RPCInvokeScenarioBasicAction( /* [in] */ handle_t rpcBindingHandle, /* [in] */ WFPSAMPLER_SCENARIO scenario, /* [in] */ FWPM_CHANGE_TYPE changeType, /* [ref][in] */ __RPC__in const FWPM_FILTER0* pFilter, /* [unique][in] */ __RPC__in_opt const PC_BASIC_ACTION_DATA* pPCBasicActionData) { UINT32 status = NO_ERROR; UNREFERENCED_PARAMETER(rpcBindingHandle); ASSERT(scenario == SCENARIO_BASIC_ACTION_BLOCK || scenario == SCENARIO_BASIC_ACTION_CONTINUE || scenario == SCENARIO_BASIC_ACTION_PERMIT || scenario == SCENARIO_BASIC_ACTION_RANDOM); ASSERT(changeType < FWPM_CHANGE_TYPE_MAX); ASSERT(pFilter); switch (scenario) { case SCENARIO_BASIC_ACTION_BLOCK: { if (changeType == FWPM_CHANGE_ADD) status = ScenarioBasicActionBlockAdd(pFilter); else status = ScenarioBasicActionBlockRemove(pFilter); break ; } case SCENARIO_BASIC_ACTION_CONTINUE: { if (changeType == FWPM_CHANGE_ADD) status = ScenarioBasicActionContinueAdd(pFilter); else status = ScenarioBasicActionContinueRemove(pFilter); break ; } case SCENARIO_BASIC_ACTION_PERMIT: { if (changeType == FWPM_CHANGE_ADD) status = ScenarioBasicActionPermitAdd(pFilter); else status = ScenarioBasicActionPermitRemove(pFilter); break ; } case SCENARIO_BASIC_ACTION_RANDOM: { if (changeType == FWPM_CHANGE_ADD) { if (pPCBasicActionData) status = ScenarioBasicActionRandomAdd(pFilter, pPCBasicActionData); else { status = ERROR_INVALID_PARAMETER; HlprLogError(L "RpcInvokeScenarioBasicAction() [status: %#x][pPCBasicActionData: %#p]" , status, pPCBasicActionData); } } else status = ScenarioBasicActionRandomRemove(pFilter); break ; } } return status; } |
Our Services
-
What our customers say about us?
Read our customer testimonials to find out why our clients keep returning for their projects.
View Testimonials