Sample Code

Windows Driver Samples/ Windows Filtering Platform Sample/ C++/ svc/ Framework_WindowsFirewall.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
////////////////////////////////////////////////////////////////////////////////////////////////////
//
//   Copyright (c) 2012 Microsoft Corporation.  All Rights Reserved.
//
//   Module Name:
//      Framework_WindowsFirewall.cpp
//
//   Abstract:
//      This module contains functions for interoperating with the Microsoft Windows Firewall
//
//   Author:
//      Dusty Harper      (DHarper)
//
//   Revision History:
//
//      [ Month ][Day] [Year] - [Revision]-[ Comments ]
//      May       01,   2010  -     1.0   -  Creation
//
////////////////////////////////////////////////////////////////////////////////////////////////////
 
#include "Framework_WFPSamplerService.h"
 
/// Module's Global variables
namespace
{
   IUnknown*    pFirewallRegistrationHandle = 0;
   THREAD_DATA* pWFNotifyThread             = 0;
   SC_HANDLE    scmHandle                   = 0;
   SC_HANDLE    mpsSvcHandle                = 0;
};
 
///
 
/// Functions implemented in Framework_WFPSamplerSvc.cpp
 
_Success_(return == NO_ERROR)
UINT32 ServiceStatusReportToSCM(_In_ UINT32 currentState,
                                _In_ UINT32 exitCode,
                                _In_ UINT32 waitHint);
 
VOID ServiceEventLogError(_In_ PCWSTR pFunctionName,
                          _In_ UINT32 errorCode);
 
///
 
/// Forward Declarations
 
_Success_(return == NO_ERROR)
UINT32 WindowsFirewallAcquireFirewallCategory();
 
///
 
/**
 @framework_function="WindowsFirewallNotification"
  
   Purpose:  Notification used to take firewall categories from Windows Firewall when MPSSvc
             transitions to a running                                                           <br>
                                                                                                <br>
   Notes:                                                                                       <br>
                                                                                                <br>
*/
VOID CALLBACK WindowsFirewallNotification(_In_ VOID* pContext)
{
   ASSERT(pContext);
 
   SERVICE_NOTIFY* pServiceNotify = (SERVICE_NOTIFY*)pContext;
   THREAD_DATA*    pThreadData    = (THREAD_DATA*)pServiceNotify->pContext;
 
   if(pServiceNotify->dwNotificationStatus == NO_ERROR)
   {
      if(pServiceNotify->ServiceStatus.dwCurrentState == SERVICE_RUNNING)
      {
         WindowsFirewallAcquireFirewallCategory();
 
         HlprThreadStop(&pThreadData);
      }
   }
   else if(pServiceNotify->dwNotificationStatus == ERROR_SERVICE_MARKED_FOR_DELETE)
      HlprThreadStop(&pThreadData);
 
   return;
}
 
/**
 @framework_function="WindowsFirewallNotifyThreadStartRoutine"
 
   Purpose:  New thread which waits for a notification that MPSSvc has started.                 <br>
                                                                                                <br>
   Notes:                                                                                       <br>
                                                                                                <br>
*/
_Success_(return == NO_ERROR)
DWORD WindowsFirewallNotifyThreadStartRoutine(_In_ LPVOID lpThreadParameter)
{
   ASSERT(lpThreadParameter);
 
   UINT32         status      = NO_ERROR;
   THREAD_DATA*   pThreadData = (THREAD_DATA*)lpThreadParameter;
   SERVICE_NOTIFY svcNotify   = {0};
 
   HlprEventSet(pThreadData->threadStartEvent);
 
   svcNotify.dwVersion         = SERVICE_NOTIFY_STATUS_CHANGE;
   svcNotify.pfnNotifyCallback = WindowsFirewallNotification;
   svcNotify.pContext          = pWFNotifyThread;
 
   status = HlprServiceNotificationStateChangeRegister(L"MPSSvc",
                                                       &svcNotify,
                                                       SERVICE_NOTIFY_RUNNING,
                                                       &scmHandle,
                                                       &mpsSvcHandle);
   HLPR_BAIL_ON_FAILURE(status);
 
   status = WaitForSingleObjectEx(mpsSvcHandle,
                                  INFINITE,
                                  TRUE);
   if(status != WAIT_IO_COMPLETION)
   {
      if(status == WAIT_FAILED)
         status = GetLastError();
 
      HlprLogError(L"WindowsFirewallNotifyThreadStartRoutine : WaitForSingleObjectEx() [status: %#x]",
                   status);
   }
 
   HLPR_BAIL_LABEL:
 
   HLPR_CLOSE_SERVICE_HANDLE(mpsSvcHandle);
 
   HLPR_CLOSE_SERVICE_HANDLE(scmHandle);
 
   return status;
}
 
/**
 @framework_function="WindowsFirewallReleaseFirewallCategory"
  
   Purpose:  Relinquish ownership of the Firewall Category back to Windows Firewall and
             unregister as a Firewall product.                                                  <br>
                                                                                                <br>
   Notes:                                                                                       <br>
                                                                                                <br>
*/
VOID WindowsFirewallReleaseFirewallCategory()
{
   if(pFirewallRegistrationHandle)
   {
      pFirewallRegistrationHandle->Release();
 
      pFirewallRegistrationHandle = 0;
   }
 
   HLPR_CLOSE_SERVICE_HANDLE(mpsSvcHandle);
 
   HLPR_CLOSE_SERVICE_HANDLE(scmHandle);
 
   HlprThreadStop(&pWFNotifyThread);
 
   return;
}
 
/**
 @framework_function="WindowsFirewallAcquireFirewallCategory"
  
   Purpose:  Take ownership from Windows Firewall of the Firewall Category and register as a
             Firewall product so as to be displayed in the Windows Firewall User Interface.     <br>
                                                                                                <br>
   Notes:                                                                                       <br>
                                                                                                <br>
*/
_Success_(return == NO_ERROR)
UINT32 WindowsFirewallAcquireFirewallCategory()
{
   UINT32 status = NO_ERROR;
 
   if(HlprServiceQueryState(L"MPSSvc") == SERVICE_RUNNING)
   {
      BSTR            pProductName      = SysAllocString(g_pServiceName);
      INetFwProduct*  pNetFwProduct     = 0;
      INetFwProducts* pNetFwProducts    = 0;
      SAFEARRAY*      pFirewallCategory = 0;
      SAFEARRAY*      pBootTimeCategory = 0;
      SAFEARRAYBOUND  boundary          = {0};
      VARIANT         firewallCategory  = {0};
      VARIANT         boottimeCategory  = {0};
 
      if(pProductName == 0)
      {
         status = ERROR_GEN_FAILURE;
 
         ServiceEventLogError(L"WindowsFirewallAcquireFirewallCategory : SysAllocString()",
                              status);
 
         HLPR_BAIL;
      }
 
      boundary.lLbound   = 0;
      boundary.cElements = 1;
 
      pBootTimeCategory = SafeArrayCreate(VT_VARIANT,
                                          1,
                                          &boundary);
      if(pBootTimeCategory == 0)
      {
         status = ERROR_GEN_FAILURE;
 
         ServiceEventLogError(L"WindowsFirewallAcquireFirewallCategory : SafeArrayCreate()",
                              status);
 
         HLPR_BAIL;
      }
 
      V_VT((VARIANT*)pBootTimeCategory->pvData) = VT_I4;
      V_I4((VARIANT*)pBootTimeCategory->pvData) = NET_FW_RULE_CATEGORY_BOOT;
 
      VariantInit(&firewallCategory);
 
      V_VT(&boottimeCategory)    = VT_ARRAY | VT_VARIANT;
      V_ARRAY(&boottimeCategory) = pBootTimeCategory;
 
      pFirewallCategory = SafeArrayCreate(VT_VARIANT,
                                          1,
                                          &boundary);
      if(pFirewallCategory == 0)
      {
         status = ERROR_GEN_FAILURE;
 
         ServiceEventLogError(L"WindowsFirewallAcquireFirewallCategory : SafeArrayCreate()",
                              status);
 
         HLPR_BAIL;
      }
 
      V_VT((VARIANT*)pFirewallCategory->pvData) = VT_I4;
      V_I4((VARIANT*)pFirewallCategory->pvData) = NET_FW_RULE_CATEGORY_FIREWALL;
 
      VariantInit(&firewallCategory);
 
      V_VT(&firewallCategory)    = VT_ARRAY | VT_VARIANT;
      V_ARRAY(&firewallCategory) = pFirewallCategory;
 
      /// Initialize the COM library
      if(!g_isCOMInitialized)
      {
         status = CoInitializeEx(0,
                                 COINIT_MULTITHREADED);
         if(FAILED(status))
         {
            ServiceEventLogError(L"WindowsFirewallAcquireFirewallCategory : CoInitializeEx()",
                                 status);
 
            HLPR_BAIL;
         }
 
         g_isCOMInitialized = TRUE;
      }
 
      /// Create an instance of the NetFwProduct class
      status = CoCreateInstance(__uuidof(NetFwProduct),
                                0,
                                CLSCTX_INPROC_SERVER,
                                __uuidof(INetFwProduct),
                                (LPVOID*)&pNetFwProduct);
      if(FAILED(status))
      {
         ServiceEventLogError(L"WindowsFirewallAcquireFirewallCategory : CoCreateInstance()",
                              status);
 
         HLPR_BAIL;
      }
 
      /// Set the DisplayName
      status = pNetFwProduct->put_DisplayName(pProductName);
      if(FAILED(status))
      {
         ServiceEventLogError(L"WindowsFirewallAcquireFirewallCategory : INetFwProduct::put_DisplayName()",
                              status);
 
         HLPR_BAIL;
      }
 
      /// Take the category
      status = pNetFwProduct->put_RuleCategories(boottimeCategory);
      if(FAILED(status))
      {
         ServiceEventLogError(L"WindowsFirewallAcquireFirewallCategory : INetFwProduct::put_RuleCategories()",
                              status);
 
         HLPR_BAIL;
      }
 
      status = pNetFwProduct->put_RuleCategories(firewallCategory);
      if(FAILED(status))
      {
         ServiceEventLogError(L"WindowsFirewallAcquireFirewallCategory : INetFwProduct::put_RuleCategories()",
                              status);
 
         HLPR_BAIL;
      }
 
      /// Create an instance of the NetFwProducts class
      status = CoCreateInstance(__uuidof(NetFwProducts),
                                0,
                                CLSCTX_INPROC_SERVER,
                                __uuidof(INetFwProducts),
                                (LPVOID*)&pNetFwProducts);
      if(FAILED(status))
      {
         ServiceEventLogError(L"WindowsFirewallAcquireFirewallCategory : CoCreateInstance()",
                              status);
 
         HLPR_BAIL;
      }
 
      /// Register as a Firewall Product
      if(pFirewallRegistrationHandle == 0)
      {
         status = pNetFwProducts->Register(pNetFwProduct,
                                           &pFirewallRegistrationHandle);
         if(FAILED(status))
         {
            ServiceEventLogError(L"WindowsFirewallAcquireFirewallCategory : INetFwProducts::Register()",
                                 status);
 
            HLPR_BAIL;
         }
      }
 
      status = NO_ERROR;
 
      HLPR_BAIL_LABEL:
 
      if(FAILED(status))
         WindowsFirewallReleaseFirewallCategory();
 
      if(pNetFwProducts)
         pNetFwProducts->Release();
 
      if(pNetFwProduct)
         pNetFwProduct->Release();
 
      if(pProductName)
         SysFreeString(pProductName);
   }
   else
   {
      HLPR_NEW(pWFNotifyThread,
               THREAD_DATA);
      if(pWFNotifyThread)
      {
         pWFNotifyThread->threadStartRoutine = (LPTHREAD_START_ROUTINE)WindowsFirewallNotifyThreadStartRoutine;
 
         status = HlprThreadStart(pWFNotifyThread);
      }
      else
         status = ERROR_OUTOFMEMORY;
   }
 
   if(HlprServiceQueryState(L"WFPSampler") == SERVICE_START_PENDING)
      ServiceStatusReportToSCM(SERVICE_START_PENDING,
                               status,
                               2500);
 
   return status;
}

Our Services

  • What our customers say about us?

© 2011-2025 All Rights Reserved. Joya Systems. 4425 South Mopac Building II Suite 101 Austin, TX 78735 Tel: 800-DEV-KERNEL

Privacy Policy. Terms of use. Valid XHTML & CSS