Packet Sniffer SDK VCL Edition

HNFileAdapter :: Events :: OnPacketReceive

 Previous Next

Fires immediately after reading a packet from the CAP file.

Syntax:

TOnPacketReceive = procedure (Sender : TObject;
                              ThParam: Pointer;
                              hPacket: Pointer;
                          pPacketData: Pointer;
                        IncPacketSize: DWORD) of object;

Parameters:

ThParam

[in] User defined thread parameter (see HNFileAdapter.OnThreadBegin).

hPacket

[in] HNPacket object handle.

pPacketData

[in] A pointer to the packet data (see also PacketData).

IncPacketSize

[in] Packet size (see also IncPacketSize).

Description:

This event handler is called immediately after a packet is read from a CAP file, and if you have set a BPF filter, this event handler is executed only for packets, passed by BPF filter.

Please use statistics to make sure that your application does not lose any packets.

For example, when you use a BPF filter, and if your application is in time to process all packets, then the value returned by HNFileAdapter.Get_ProcessCount is equal to the HNFileAdapter.Get_AcceptCount one.

If you do not use a filter, you should compare HNFileAdapter.Get_ProcessCount and HNFileAdapter.Get_RecvCount returned values.

Also, you should take into account that OnPacketReceive event handler is called in the context of a thread, created by Packet Sniffer SDK library.

We strongly recommend you to do nothing related to the data displaying in this event handler. If the traffic is high, such actions may lead to the hanging of the application and packets loss.

OnPacketReceive event handler example:

procedure TForm1.HNAdapterOnPacketReceive(Sender: TObject;
  ThParam, hPacket, pPacketData: Pointer; IncPacketSize: DWORD);
 var
  pEth:    PETHERNET_HEADER;
  EthType: Word;
  pArp:    PARP_HEADER;
begin
   HNPacket1.Handle := hPacket;
   if (HNPacket1.MediumType <> atEthernet) then Exit;

   pEth  := PETHERNET_HEADER(pPacketData);
   EthType := htons(pEth^.EthType);

   ...

   if(EthType = ETHERTYPE_ARP) then
    begin
      pArp := PARP_HEADER(@PByteArray(pPacketData)^[sizeof(ETHERNET_HEADER)]);
      MoveMemory(@DestMacAddr[0],@pArp^.Sndr_Hw_Addr[0],6);
      SetEvent(RecvArpPacket);
      Exit;
    end;

   ...
end;
See also:HNFileAdapter, HNFileAdapter.OpenFile, HNFileAdapter.Get_ProcessCount, HNFileAdapter.Get_AcceptCount, HNFileAdapter.Get_ProcessCount, HNFileAdapter.Get_RecvCount, HNFileAdapter.Get_OpenTime, HNPacket