用C#开发网络防火墙技术分析

http://tech.ddvip.com   2006年08月01日    社区交流

本文详细介绍用C#开发网络防火墙技术分析

  既然在IoCtrl类中获得了这些信息,但是需要把它们封装成主程序容易处理的数据类型,这样,用C#实现了InfoEvent类用来封装这些信息:

  //本类封装了数据包的详细信息,可以通过事件实现对它的模块间传递。

  public class InfoEvent:EventArgs
{
 string sInfo; //用来存放输出信息的私有成员
 public int pLength; //CommonFunction.sPort数组的长度
 public ushort protocol; //网络通信协议类型
 public uint sourceIp; //数据包的源IP
 public uint destinationIp; //数据包的目的IP
 public ushort sourcePort; //数据包的源端口
 public ushort destinationPort; //数据包的目的端口
 ………………………………
}

  下面在用托管C++实现的InfoProvider驱动程序信息提供者类中把个InfoEvent类的对象传递给主程序,需要使用一个委托生成一个事件:

  //声明委托事件,用来向主程序传递数据。
__delegate void DriverInfo(Object* sender, InfoEvent* e);
//声明响应事件函数。
__event DriverInfo* OnDriverInfo;

  然后在InfoProvider驱动程序信息提供者类中定义一个方法,在主程序中以线程的方式运行这个方法,在这个方法中使用了事件函数OnDriverInfo:

  //用来获得驱动程序信息的进程,在主程序中将开启该进程。
void GetInfoThreadProc()
{
 this->hEvent=OpenEvent(SYNCHRONIZE,FALSE,"NBEvent");
 if(!ic->GetDriverHandle())
 {
  return;
 }
 while(true)
 {
  f(!hEvent)
  ExitThread(0);
  WaitForSingleObject(this->hEvent,INFINITE);
  nPackets++;
  ic->GetAccessInfo();
  ic->ResetEvent();
  //定义一个主程序可以识别的对象,通过OnDriverInfo传给主程序。
  InfoEvent*ie=new InfoEvent(ic->protocol,ic->sourceIp,ic->destinationIp,ic->sourcePort,ic->destinationPort);
  OnDriverInfo(this,ie);
 }
 ic->CloseDriverHandle();
 return;
}

  在主程序中,会开启这个进程并定义了OnDriverInfo的处理函数DealWithInfo:

  pInfo=new InfoProvider();
//开启与驱动交换信息的进程
FilterThread=new Thread(new ThreadStart(pInfo.GetInfoThreadProc));
FilterThread.IsBackground=true;
FilterThread.Start();
pInfo.OnDriverInfo+=new InfoProvider.DriverInfo(DealWithInfo);

  这样主程序就可以在DealWithInfo函数中加入对InfoEvent对象的处理了。可见,通过中间模块IoCtrl的转换,便实现了.NET主程序对驱动程序中非托管数据类型的获取和处理。

作者:seafrog    责编:豆豆技术应用

正在加载评论...