
Firewall and Filtering Architecture
Before I get to the discussion of the sample code, there are two other items of business to address. Firstly, I want to provide pointers to existing documentation and background information that every programmer should peruse, prior to using the Windows Firewall API:
The second item of business to address before I get to the sample code is to explain briefly the difference between the Windows Filtering Platform and the Windows Firewall APIs.
My sample demonstrates the use of two closely related Windows Vista APIs: those for the Windows Filtering Platform and those for the Windows Firewall. The Windows Filtering Platform API is new in Windows Vista. While some of the Windows Firewall API functionality is available in Microsoft Windows XP, it has been greatly enhanced in Windows Vista. And, while some of the capabilities of the Windows Filtering Platform and Windows Firewall APIs overlap in Windows Vista, it's important to understand the scenarios to which each applies.
I learned this lesson the hard way; my first attempt to add firewall rules programmatically for my sample TCP server was based on the Windows Filtering Platform API. This was partly because I first found a sample in the latest SDK that demonstrates adding Windows Filtering Platform–based rules for MSN Messenger. Because I had no reason to suspect that this wouldn't be a good starting point for my own work, I used it as a baseline. Using the Windows Filtering Platform API instead of the Windows Firewall API was also initially attractive, because I found the Windows Filtering Platform API to be more intuitive.
However, while testing my first Windows Filtering Platform–based solution, I found that, even after adding my filters, I was still getting prompted by the Windows Firewall when the TCP socket–based test-server program started listening on its port. Windows Vista displayed the following screen shot when I was testing my first Windows Filtering Platform–based solution:
Figure 1. Windows Security Alert dialog box displayed in Windows Vista
Furthermore, my Windows Filtering Platform rules seemed to be ignored. If I clicked the Keep blocking button, the client could never connect, regardless of my own filter. If I clicked the Unblock button, the server was able to receive all traffic, regardless of the client address—for example, even though my filter was more restrictive. In other words, despite the presence of my filter, I was still observing the default per-application behavior of the Windows Firewall.
This was contrary to my goals for this demo, as I wanted to demonstrate the proper way to install very specific firewall rules and avoid the preceding potentially confusing dialog box.
Subsequent conversation with the engineering team at Microsoft clarified that the Windows Filtering Platform API is not intended to be used in the way in which I was using it. In scenarios in which the Windows Firewall is enabled, and a standard network-based application needs to open one or more ports, the Windows Firewall API should be used. In contrast, the primary purpose of the Windows Filtering Platform API is to facilitate development of rule-based network-infrastructure technologies, such as third-party firewalls. Indeed, the Windows Firewall is based on the Windows Filtering Platform API, as I'll demonstrate later, and non-Microsoft firewalls also are intended to be so.
We can infer from this situation that it's Microsoft's intent that the average network application should not interact with the Windows Filtering Platform API in any way. Instead, such applications should call into the higher-level Windows Firewall APIs during installation and un-installation. This is best practice, as the Windows Firewall is enabled by default. However, application developers should also provide documentation that details the expected firewall-rules configuration for proper and secure operation. This is important, as the Windows Firewall may have been replaced by a non-Microsoft solution (and, therefore, a different programmatic interface). Finally, this should serve as a gentle reminder that Windows should always have a software firewall enabled and correctly configured!