WinpkFilter wwwcensor in Delphi problem

Home Forums Discussions Support Portal WinpkFilter wwwcensor in Delphi problem

This topic contains 1 reply, has 2 voices, and was last updated by  Vadim Smirnov 7 years, 1 month ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #5326

    cristighenea
    Participant

    Hi.
    I am trying to implement the wwwcensor example in Delphi 2007, windows application.
    I have a problem with WaitForMultipleObjects: it returns all the time -1
    Can someone help me?
    The code is:


    procedure TForm1.block;
    begin
    //block!

    //Initialize NDISAPI
    InitNDISAPI();

    //Create driver object
    hFilt := OpenFilterDriver('NDISRD');

    //Check if driver is loaded
    if IsDriverLoaded(hFilt) then
    begin
    Memo1.Lines.add('driver is loaded!');

    //get pattern to upper case
    pattern:=Edit1.Text;
    pattern:=UpperCase(pattern);

    // Get system installed network interfaces
    GetTcpipBoundAdaptersInfo (hFilt, @AdList);

    //just checking the numbers of network interfaces
    showmessage(inttostr(Adlist.m_nAdapterCount));

    // Initialize common ADAPTER_MODE structure (all network interfaces will operate in the same mode)
    Mode.dwFlags := MSTCP_FLAG_SENT_TUNNEL xor MSTCP_FLAG_RECV_TUNNEL;

    // Create notification events and initialize the driver to pass packets thru us
    for dwAdIndex := 1 to AdList.m_nAdapterCount do
    begin

    Memo1.Lines.add('set adapter no '+inttostr(dwAdindex));
    Memo1.Lines.add(AdList.m_szAdapterNameList[dwAdindex]);
    showmessage('hhh');

    hEvent[dwAdIndex] := CreateEvent(nil, TRUE, FALSE, nil);

    // modify this for error
    if hEvent[dwAdIndex] = 0 then begin showmessage('hEvent error');halt;end;

    Mode.hAdapterHandle := AdList.m_nAdapterHandle[dwAdIndex];
    // Set MSTCP_FLAG_SENT_TUNNEL|MSTCP_FLAG_RECV_TUNNEL for the network interface
    SetAdapterMode ( hFilt, @Mode );

    // Set packet notification event for the network interface
    if SetPacketEvent(hFilt,AdList.m_nAdapterHandle[dwAdIndex], hEvent[dwAdIndex]) = 0 then begin showmessage('SetPacketEvent error');halt;end;
    end;

    // Initialize common part of ETH_REQUEST
    ReadRequest.EthPacket.Buffer := @Buffer;


    while true do
    begin
    Application.ProcessMessages;
    // Wait before any of the interfaces is ready to indicate the packet
    dwAdIndex := WaitForMultipleObjects ( AdList.m_nAdapterCount, @hEvent, FALSE, INFINITE ) -WAIT_OBJECT_0;
    if dwAdIndex<>-1 then
    begin
    Memo1.Lines.Add('act on '+inttostr(dwAdIndex));
    //dwAdIndex := WaitForSingleObject ( hEvent[1], INFINITE );
    // Complete initialization of ETH_REQUEST
    ReadRequest.hAdapterHandle := AdList.m_nAdapterHandle[dwAdIndex];

    // Read packet from the interface until there are any
    while ReadPacket (hFilt, @ReadRequest) <> 0 do
    begin
    Application.ProcessMessages;
    // Get Ethernet header
    pEthHeader := TEtherHeaderPtr (@Buffer.m_IBuffer);
    if ntohs(pEthHeader.h_proto) = ETH_P_IP then
    begin
    // Get IP header
    pIPHeader := TIPHeaderPtr(Integer(pEthHeader) + SizeOf(TEtherHeader));//1);//SizeOf(TEtherHeader));

    // Check if IP packet contains TCP packet
    if pIpHeader.Protocol = IPPROTO_TCP then
    begin
    // Get TCP header pointer
    pTcpHeader := TTCPHeaderPtr(Integer(pIPHeader) + (pIPHeader.VerLen and $F) * 4);

    // Check if this HTTP packet is destined to remote system port 80, or received from it
    if (((pTcpHeader.DestPort = htons (80))and(Buffer.m_dwDeviceFlags = PACKET_FLAG_ON_SEND))or
    ((pTcpHeader.SourcePort = htons (80))and(Buffer.m_dwDeviceFlags = PACKET_FLAG_ON_RECEIVE)))
    then
    begin

    // Get data size in the packet and pointer to the data
    dwDataLength := Buffer.m_Length - (sizeof(pEthHeader) + pIpHeader.VerLen*4 + pTcpHeader.Offset*4);
    pData := pchar(pEthHeader) + (sizeof(pEthHeader) + pIpHeader.VerLen*4 + pTcpHeader.Offset*4);

    // If packet contains any data - process it
    if (dwDataLength>0) then
    begin
    Memo1.Lines.Add('something');
    end;
    end;


    end;


    end;



    end
    end;
    end;


    end
    else
    begin
    Memo1.Lines.add('driver is not loaded!');
    end;
    end;

    The variables are:


    hFilt: THANDLE;
    AdList: TCP_AdapterList;
    Mode:ADAPTER_MODE;
    hEvent: array[0..255] of THANDLE;
    ReadRequest : ETH_REQUEST;
    Buffer: INTERMEDIATE_BUFFER;
    pEthHeader: TEtherHeaderPtr;
    pIPHeader: TIPHeaderPtr;
    pTcpHeader: TTCPHeaderPtr;
    dwDataLength: word;
    pData : pchar;
    pattern: string;
    dwAdIndex:integer;

    What am I doing wrong? 🙁

    Thanks

    #6940

    Vadim Smirnov
    Moderator

    -1 means WAIT_FAILED and the problem is in hEvent[0] value which is NULL

    You defined hEvent array indexed from 0, as below

    hEvent: array[0..255] of THANDLE;

    But you fill it with event handles starting from index 1 (see below), so for index 0 you have an invalid event handle.

    for dwAdIndex := 1 to AdList.m_nAdapterCount do
    begin
    ...
    hEvent[dwAdIndex] := CreateEvent(nil, TRUE, FALSE, nil);
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.