The sample uses a file (hostdb.txt) to keep track of authorized hostnames, but you might modify this sample to access a database which holds host information.
For each URL request, the filter first looks in a cache of recently allowed hosts, and when that fails, it looks in the hostdb.txt file. This shows an efficient way to authorize connections: a cache allows the filter to quickly allow or disallow host access, and because each request comes in through the filter, speed is critical.
The project is a standard appwizard generated ISAPI filter. There are 5 parameters that can be changed to fine tune the filter: the maximum number of cached hosts, the position after which a cached entry will be moved to the front of the list (to make the search time shorter!), the name of the file that contains the host list and the names of the HTML files that indicate to the user that its IP address could not be resolved to a host name or that the access was denied for this host. All these parameters are
#define directives in the hostnamefilter.h header file.
The filter could be improved in several ways: using a database instead of a file for host information (you should consider using stored procedures to search and/or to cache!), load parameters from registry, automatic selection of the number of cached hosts and the list reorder parameter, etc.
The full source code is provided, you will have to compile it in order to get a working filter. Once you have compiled the project, you will need to take the following steps to install:
- Run REGEDT32.EXE and modify the server's registry as follows. Select the Filter DLLs key in HKEY_LOCAL_MACHINE\CurrentControlSet\Services\W3SVC\Parameters. Add a local path to hostnamefilter.dll, usually C:\WinNT\System32\InetSrv\hostnamefilter.dll. The filter entries are separated by commas. The order is important, if you have other filters with the same priority, the first one listed will receive the requests first.
- Copy the hostnamefilter.dll file to the directory you specified in the registry.
- Make sure the System account has execute rights on the filter DLL file.
- Edit the hostdb.txt file so it contains valid hostnames. The format of the file is:
- Copy the hostdb.txt file to the directory you specified in the hostnamefilter.h header file for the host database.
- Copy the NoName.htm file to the directory you specified in the hostnamefilter.h header file for the page to indicate that the IP address could not be resolved to a name.
- Copy the NoAccess.htm file to the directory you specified in the hostnamefilter.h header file for the page to indicate that the access is denied for this host.
- Make sure the System account has read rights on the NoName.htm, NoAccess.htm and hostdb.txt files.
- Restart the WWW service.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.