Ingusclass.pas
上传用户:sunrenlu
上传日期:2007-01-08
资源大小:199k
文件大小:10k
源码类别:

Internet/网络编程

开发平台:

Delphi

  1. {
  2.   IngusClass.Pas - Packet32 Helper
  3.   ----------------------------------
  4.   Writen by: Jagad (don@indo.net.id)
  5.   Updates by FP (francois.piette@pophost.eunet.be, http://www.rtfm.be/fpiette)
  6.   May 12, 1999  FP  Added UDP Support
  7.                     Changed TIngusSnifferThread.MacAddr property to PChar to be
  8.                     consistent with TIngusPacketBase.MacAddr property.
  9.                     Existing code may need to be changed.
  10. }
  11. unit Ingusclass;
  12. interface
  13. uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  14.      Packet32, Protohdr;
  15. const
  16.   MAX_ADAPTER_COUNT = 5;
  17. type
  18.   TIngusPacketBase = class;
  19.   TPacketDirection = (pdOutput, pdInput, pdPassThrough);
  20.   TParsePacketEvent = procedure( nPacketSeq: Longint;
  21.                                  uBuffer: PChar;
  22.                                  nRecvBytes: integer;
  23.                                  sPacket: TIngusPacketBase ) of object;
  24.   TIngusPacketBase = class(TObject)
  25.   private
  26.     FMacAddr: PChar;
  27.     FBuffer: PChar;
  28.     FEthernetHdr: PETHERNET_HDR;
  29.     FPacketDirection: TPacketDirection;
  30.     FProtocol: Integer;
  31.     FData: PChar;
  32.   protected
  33.   public
  34.     constructor Create(MacAddr: PChar; uBuffer: PChar); virtual;
  35.     destructor Destroy; override;
  36.     property EthernetProtocol: integer read FProtocol;
  37.     property PacketDirection: TPacketDirection read FPacketDirection;
  38.     property EthernetHeader: PETHERNET_HDR read FEthernetHdr;
  39.     property EthernetData: PChar read FData;
  40.   end;
  41.   TIngusSnifferThread = class(TThread)
  42.   private
  43.     FMacAddr: PChar;  // FPiette May 12, 1999
  44.     FpAdapter: LPADAPTER;
  45.     FpPacket: LPPACKET;
  46.     FdwRxBytes: DWORD;
  47.     FRxPacketSeq: Longint;
  48.     FParsePacketEvent: TParsePacketEvent;
  49.   protected
  50.   public
  51.     constructor Create; virtual;
  52.     destructor Destroy; override;
  53.     procedure Execute; override;
  54.     procedure SyncParseEvent;
  55.     property MacAddr: PChar read FMacAddr write FMacAddr;
  56.     property Adapter: LPADAPTER read FpAdapter write FpAdapter;
  57.     property Packet: LPPACKET read FpPacket write FpPacket;
  58.     property OnParsePacket: TParsePacketEvent read FParsePacketEvent write FParsePacketEvent;
  59.   end;
  60.   TAfterGetAdapterDesc = procedure(bStatus: Boolean; sAdapterDesc: string) of object;
  61.   TAfterGetMacAddress = procedure(bStatus: Boolean; pMacAddr: PChar) of object;
  62.   TAfterSetFilter = procedure(bStatus: Boolean; uFilter: ULONG) of object;
  63.   TIngusSniffer = class(TObject)
  64.   private
  65.     //FuBuffer: array[0..1520] of Char;
  66.     FuMac: array[0..5] of UCHAR;
  67.     FAdapterDescs: array[0..MAX_ADAPTER_COUNT-1] of ADAPTER_DESC;
  68.     FAdapterNames: TStringList;
  69.     FAdapterCount: integer;
  70.     FpPacket: LPPACKET;
  71.     FhAdapter: DWORD;
  72.     FbStartSnoop: Boolean;
  73.     FParsePacketEvent: TParsePacketEvent;
  74.     FIngusThread: TIngusSnifferThread;
  75.     FAfterGetAdapterDesc: TAfterGetAdapterDesc;
  76.     FAfterGetMacAddress: TAfterGetMacAddress;
  77.     FAfterSetFilter: TAfterSetFilter;
  78.   protected
  79.     procedure GetAdapterNameList;
  80.     function GetMacAddr: PChar;        // FP 12/05/99
  81.   public
  82.     constructor Create; virtual;
  83.     destructor Destroy; override;
  84.     //zero-based index
  85.     function StartSnoop(nAdapterIndex: integer): Boolean;
  86.     procedure StopSnoop;
  87.     property MacAddr: PChar read GetMacAddr;         // FP 12/05/99
  88.     property AdapterNameList: TStringList read FAdapterNames;
  89.     property AdapterCount: integer read FAdapterCount;
  90.     property OnParsePacket: TParsePacketEvent read FParsePacketEvent
  91.                                               write FParsePacketEvent;
  92.     property OnAfterGetAdapterDesc: TAfterGetAdapterDesc read FAfterGetAdapterDesc
  93.                                                          write FAfterGetAdapterDesc;
  94.     property OnAfterGetMacAddress: TAfterGetMacAddress read FAfterGetMacAddress
  95.                                                        write FAfterGetMacAddress;
  96.     property OnAfterSetFilter: TAfterSetFilter read FAfterSetFilter
  97.                                                  write FAfterSetFilter;
  98.   end;
  99. var
  100.    FuBuffer: array[0..1520] of Char;
  101. implementation
  102. uses IngusPacket;
  103. //----- TIngusPacketBase ------//
  104. constructor TIngusPacketBase.Create(MacAddr: PChar; uBuffer: PChar);
  105. begin
  106.   inherited Create;
  107.   FMacAddr := MacAddr;
  108.   FBuffer := uBuffer;
  109.   FEthernetHdr := PETHERNET_HDR(FBuffer);
  110.   FProtocol := TOUSHORT(@(FEthernetHdr^.Protocol[0]));
  111.   FData := @(FEthernetHdr^.Data[0]);
  112.   //Packet Direction
  113.   FPacketDirection := pdPassThrough;
  114.   if CompareMem(FMacAddr, @(FEthernetHdr^.Destination[0]), 6) then begin
  115.     //Input
  116.     FPacketDirection := pdInput;
  117.   end
  118.   else if CompareMem(FMacAddr, @(FEthernetHdr^.Source[0]), 6) then begin
  119.     //Output
  120.     FPacketDirection := pdOutput;
  121.   end;
  122. end;
  123. destructor TIngusPacketBase.Destroy;
  124. begin
  125.   //...
  126.   inherited Destroy;
  127. end;
  128. //----- TIngusSnifferThread ------//
  129. constructor TIngusSnifferThread.Create;
  130. begin
  131.   //Initiate vars
  132.   FpAdapter := nil;
  133.   FpPacket := nil;
  134.   inherited Create(True);
  135. end;
  136. destructor TIngusSnifferThread.Destroy;
  137. begin
  138.   //...
  139.   inherited Destroy;
  140. end;
  141. procedure TIngusSnifferThread.Execute;
  142. begin
  143.   if (FpAdapter = nil)or(FpPacket = nil) then exit;
  144.   FRxPacketSeq := 0;
  145.   while (not Terminated) do begin
  146.     PacketReceivePacket(FpAdapter, FpPacket, TRUE, @FdwRxBytes);
  147.     if (not Terminated) then
  148.       Synchronize(SyncParseEvent);
  149.     Inc(FRxPacketSeq);
  150.   end;
  151. end;
  152. procedure TIngusSnifferThread.SyncParseEvent;
  153. var
  154.   sPacket: TIngusPacketBase;
  155.   pEthernetHdr: PETHERNET_HDR;
  156.   pIPHeader: PIP_RHDR;
  157.   nProto, nIPProto: integer;
  158. begin
  159.   if (FpPacket = nil) then exit;
  160.   pEthernetHdr := PETHERNET_HDR(@(FpPacket^.Buffer[0]));
  161.   nProto := TOUSHORT(@(pEthernetHdr^.Protocol[0]));
  162.   case nProto of
  163.   //IP
  164.   PROTO_IP: begin
  165.               pIPHeader := PIP_RHDR(@(pEthernetHdr^.Data[0]));
  166.               nIPProto := pIPHeader^.Protocol;
  167.               case nIPProto of
  168.               //ICMP
  169.               1: sPacket := TIngusICMPPacket.Create( FMacAddr, @(FpPacket^.Buffer[0]) );
  170.               //TCP
  171.               6: sPacket := TIngusTCPPacket.Create(FMacAddr, @(FpPacket^.Buffer[0]));
  172.               //UDP
  173.               17: sPacket := TIngusUDPPacket.Create(FMacAddr, @(FpPacket^.Buffer[0]));
  174.               else
  175.                 sPacket := TIngusIPPacket.Create(FMacAddr, @(FpPacket^.Buffer[0]));
  176.               end;
  177.             end;
  178.   else
  179.     sPacket := TIngusPacketBase.Create( FMacAddr, @(FpPacket^.Buffer[0]) );
  180.   end;
  181.   //Call Event handler
  182.   if Assigned(FParsePacketEvent) then
  183.     FParsePacketEvent( FRxPacketSeq, @(FpPacket^.Buffer[0]), FdwRxBytes, sPacket );
  184.   sPacket.Free;
  185. end;
  186. //----- TIngusSniffer ------//
  187. procedure TIngusSniffer.GetAdapterNameList;
  188. var
  189.   i: integer;
  190. begin
  191.   //Get Adapter names
  192.   if (PacketGetAdapterNames(@FAdapterDescs[0], MAX_ADAPTER_COUNT, @FAdapterCount) = FALSE) then
  193.   begin
  194.     //Error....
  195.     exit;
  196.   end;
  197.   for i := 0 to FAdapterCount-1 do begin
  198.     FAdapterNames.Add(StrPas(FAdapterDescs[i].szAdapterDesc));
  199.   end;
  200. end;
  201. function TIngusSniffer.GetMacAddr: PChar;
  202. begin
  203.   Result := @FuMac;  // FP 12/05/99
  204. end;
  205. constructor TIngusSniffer.Create;
  206. begin
  207.   inherited Create;
  208.   FAdapterNames := TStringList.Create;
  209.   //Get Adapter Names
  210.   GetAdapterNameList;
  211.   FbStartSnoop := FALSE;
  212. end;
  213. destructor TIngusSniffer.Destroy;
  214. begin
  215.   if FbStartSnoop then StopSnoop; //Avoid Blue-Screen :)
  216.   FAdapterNames.Free;
  217.   inherited Destroy;
  218. end;
  219. function TIngusSniffer.StartSnoop(nAdapterIndex: integer): Boolean;
  220. var
  221.   i: integer;
  222. begin
  223.   Result := False;
  224.   if (FbStartSnoop) then exit;
  225.   if (nAdapterIndex >= FAdapterCount) then exit;
  226.   //Open Adapter
  227.   FhAdapter := PacketOpenAdapter(FAdapterDescs[nAdapterIndex].szAdapterName);
  228.   if (FhAdapter = 0) then begin
  229.     //Error in open adapter...
  230.     exit;
  231.   end;
  232.   //Get Adapter Description
  233.   if (PacketAdapterDesc(LPADAPTER(FhAdapter), @FuBuffer[0], sizeof(FuBuffer), @i) = TRUE) then
  234.   begin
  235.     if Assigned(FAfterGetAdapterDesc) then
  236.       FAfterGetAdapterDesc(TRUE, StrPas(@FuBuffer[0]));
  237.   end
  238.   else begin
  239.     //Error
  240.     if Assigned(FAfterGetAdapterDesc) then
  241.       FAfterGetAdapterDesc(FALSE, '');
  242.   end;
  243.   //Get Current Mac Address
  244.   if (PacketGetAddress(LPADAPTER(FhAdapter), @FuMac[0], 6, @i) = TRUE) then
  245.   begin
  246.     if Assigned(FAfterGetMacAddress) then
  247.       FAfterGetMacAddress(TRUE, @FuMac[0]);
  248.   end
  249.   else begin
  250.     //Error
  251.     if Assigned(FAfterGetMacAddress) then
  252.       FAfterGetMacAddress(FALSE, nil);
  253.   end;
  254.   //Select Filter mode
  255.   if (PacketSetFilter(LPADAPTER(FhAdapter), NDIS_PACKET_TYPE_PROMISCUOUS) = TRUE) then
  256.   begin
  257.     if Assigned(FAfterSetFilter) then
  258.       FAfterSetFilter(TRUE, NDIS_PACKET_TYPE_PROMISCUOUS);
  259.   end
  260.   else begin
  261.     //Error
  262.     if Assigned(FAfterSetFilter) then
  263.       FAfterSetFilter(FALSE, NDIS_PACKET_TYPE_PROMISCUOUS);
  264.   end;
  265.   //Allocate Packet
  266.   FpPacket := LPPACKET( PacketAllocatePacket(LPADAPTER(FhAdapter)) );
  267.   if (FpPacket = Nil) then begin
  268.     //Error...
  269.     PacketCloseAdapter(LPADAPTER(FhAdapter));
  270.     exit;
  271.   end;
  272.   //set the packet's buffer and its max. length
  273. PacketInitPacket(FpPacket, @FuBuffer[0], 1520);
  274.   FbStartSnoop := TRUE;
  275.   //Create Thread
  276.   FIngusThread := TIngusSnifferThread.Create; //Create Suspended Thread
  277.   FIngusThread.MacAddr := @FuMac[0];
  278.   FIngusThread.Adapter := LPADAPTER(FhAdapter);
  279.   FIngusThread.Packet := FpPacket;
  280.   FIngusThread.OnParsePacket := FParsePacketEvent;
  281.   FIngusThread.Resume; //Resume thread -> Execute
  282.   Result := TRUE;
  283. end;
  284. procedure TIngusSniffer.StopSnoop;
  285. begin
  286.   if not FbStartSnoop then exit;
  287.   FIngusThread.Terminate;
  288.   //close the underlying adapter
  289. PacketCloseAdapter(LPADAPTER(FhAdapter));
  290.     //free packet
  291. PacketFreePacket(FpPacket);
  292.   FbStartSnoop := FALSE;
  293.   FIngusThread.Free;
  294. end;
  295. end.