将上下文与数据流相关联
对于在支持数据流的筛选层处理数据的标注,标注驱动程序可以将上下文与每个数据流相关联。 此类上下文对筛选器引擎是不透明的。 标注的 classifyFn 标注函数可以使用此上下文保存特定于数据流的状态信息,以便下次该数据流的筛选器引擎调用该数据流。 筛选器引擎通过 flowContext 参数将此上下文传递给标注的 classifyFn 标注函数。 如果没有与数据流关联的上下文,则 flowContext 参数为零。
若要将上下文与数据流相关联,标注的 classifyFn 标注函数调用 FwpsFlowAssociateContext0 函数。 例如:
// Context structure to be associated with data flows
typedef struct FLOW_CONTEXT_ {
.
. // Driver-specific content
.
} FLOW_CONTEXT, *PFLOW_CONTEXT;
#define FLOW_CONTEXT_POOL_TAG 'fcpt'
// classifyFn callout function
VOID NTAPI
ClassifyFn(
IN const FWPS_INCOMING_VALUES0 *inFixedValues,
IN const FWPS_INCOMING_METADATA_VALUES0 *inMetaValues,
IN OUT VOID *layerData,
IN const FWPS_FILTER0 *filter,
IN UINT64 flowContext,
IN OUT FWPS_CLASSIFY_OUT *classifyOut
)
{
PFLOW_CONTEXT context;
UINT64 flowHandle;
NTSTATUS status;
...
// Check for the flow handle in the metadata
if (FWPS_IS_METADATA_FIELD_PRESENT(
inMetaValues,
FWPS_METADATA_FIELD_FLOW_HANDLE))
{
// Get the flow handle
flowHandle = inMetaValues->flowHandle;
// Allocate the flow context structure
context =
(PFLOW_CONTEXT)ExAllocatePoolWithTag(
NonPagedPool,
sizeof(FLOW_CONTEXT),
FLOW_CONTEXT_POOL_TAG
);
// Check the result of the memory allocation
if (context == NULL)
{
// Handle memory allocation error
...
}
else
{
// Initialize the flow context structure
...
// Associate the flow context structure with the data flow
status = FwpsFlowAssociateContext0(
flowHandle,
FWPS_LAYER_STREAM_V4,
calloutId,
(UINT64)context
);
// Check the result
if (status != STATUS_SUCCESS)
{
// Handle error
...
}
}
}
...
}
如果上下文已与数据流关联,必须先删除该上下文,然后才能将新上下文与数据流关联。 若要从数据流中删除上下文,标注的 classifyFn 标注函数调用 FwpsFlowRemoveContext0 函数。 例如:
// Context structure to be associated with data flows
typedef struct FLOW_CONTEXT_ {
...
} FLOW_CONTEXT, *PFLOW_CONTEXT;
#define FLOW_CONTEXT_POOL_TAG 'fcpt'
// classifyFn callout function
VOID NTAPI
ClassifyFn(
IN const FWPS_INCOMING_VALUES0 *inFixedValues,
IN const FWPS_INCOMING_METADATA_VALUES0 *inMetaValues,
IN OUT VOID *layerData,
IN const FWPS_FILTER0 *filter,
IN UINT64 flowContext,
OUT FWPS_CLASSIFY_OUT *classifyOut
)
{
PFLOW_CONTEXT context;
UINT64 flowHandle;
NTSTATUS status;
...
// Check for the flow handle in the metadata
if (FWPS_IS_METADATA_FIELD_PRESENT(
inMetaValues,
FWPS_METADATA_FIELD_FLOW_HANDLE))
{
// Get the flow handle
flowHandle = inMetaValues->flowHandle;
// Check whether there is a context associated with the data flow
if (flowHandle != 0)
{
// Get a pointer to the flow context structure
context = (PFLOW_CONTEXT)flowContext;
// Remove the flow context structure from the data flow
status = FwpsFlowRemoveContext0(
flowHandle,
FWPS_LAYER_STREAM_V4,
calloutId
);
// Check the result
if (status != STATUS_SUCCESS)
{
// Handle error
...
}
// Cleanup the flow context structure
...
// Free the memory for the flow context structure
ExFreePoolWithTag(
context,
FLOW_CONTEXT_POOL_TAG
);
}
}
...
}
在前面的示例中, calloutId 变量包含标注的运行时标识符。 运行时标识符与标注驱动程序向筛选器引擎注册标注时返回到标注驱动程序的标识符相同。