Develop a Server Filter


This tutorial demonstrates how to create a server filter with Visual C++ in step-by-step. This filter is used to reject all TCP/IP connection from ip address "192.168.0.1" to SMTP service.

Create a server filter project in Visual C++ 6.0

First of all, open Visual C++ IDE, choose menu "File" -> "New" -> choose "Projects" tab and create a "Win32 Dynamic-Link Library" project named "myconnfilter". And choose "An empty DLL project" in the next step and click "Finish". After "myconnfilter" project is created, choose "FileView" tab at the right of the IDE, right click mouse under "Source Files" and choose "Add Files to Folder", and add two files to this folder, one is named "myconnfilter.cpp" another is named "myconnfilter.def", then the server filter project framework is finished.

Add filter header file

All server filter declaration is included in a header file "svrfilter.h" which locates at the sdk installation path\inc. You must place this include directive at the beginning of your filter cpp file.

#include "..\..\..\inc\svrfilter.h"

Add entry-points functions

In filter dll, two functions must be implemented, they are GetFilterVersion function and SvrFilterProc function. Here these functions are added to myconnfilter.cpp.

#include "..\..\..\inc\svrfilter.h"

/////////////////////////////////////////////////////////////////////
// GetFilterVersion
/////////////////////////////////////////////////////////////////////
BOOL	WINAPI 
GetFilterVersion( DWORD* pNotification )
{
  return TRUE;
}

/////////////////////////////////////////////////////////////////////
// SvrFilterProc
/////////////////////////////////////////////////////////////////////
DWORD	WINAPI 
SvrFilterProc(  
	DWORD               NotificationType,
	SVRFILTERCONTEXT    *pCtx
)
{

  return	SVR_FILTER_NEXT_NOTIFICATION;
}

We also need to add the following content into myconnfilter.def so that compiler exports these functions in myconnfilter.dll.

LIBRARY	"myconnfilter.dll"
EXPORTS
  GetFilterVersion
  SvrFilterProc
	

Implementation in GetFilterVersion function

As myconnfilter is used to reject TCP/IP connection from specified IP address, we should register SVR_FILTER_CONNECTION event.

/////////////////////////////////////////////////////////////////////
// GetFilterVersion
/////////////////////////////////////////////////////////////////////
BOOL	WINAPI 
GetFilterVersion( DWORD* pNotification )
{
  *pNotification = SVR_FILTER_CONNECTION; //register event here
  return TRUE;
}

Implementation in SvrFilterProc function

Once SVR_FILTER_CONNECTION event occurs, SvrFilterProc will be called by EAS. SvrFilterProc then returns a value to notify EAS to accept or reject this TCP/IP connection. Here we demonstrate how SvrFilterProc rejects the TCP/IP connection from "192.168.0.1".

/////////////////////////////////////////////////////////////////////
// SvrFilterProc
/////////////////////////////////////////////////////////////////////
DWORD	WINAPI 
SvrFilterProc(  
	DWORD             NotificationType,
	SVRFILTERCONTEXT  *pCtx
)
{
  CONNECTIONFILTERCONTEXT	*pSvrCtxt = (CONNECTIONFILTERCONTEXT*)pCtx->pCtxt;

  /*
  block all connections from 192.168.0.1 to SMTP service
  */
  if( pSvrCtxt->nLocalPort == 25 ) //this connection is to SMTP service.
  {
    if( stricmp( pSvrCtxt->pszRemoteIP, "192.168.0.1" ) == 0 )
      pCtx->action = enRefuseConnection; //reject connection.
  }
  return	SVR_FILTER_NEXT_NOTIFICATION;
}

Now you can compile the whole project and you will get "myconnfilter.dll".

See Also

Install Server Filter
Debug Server Filter
Server Filter with .NET languages