🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.internet.socket
Article #3862

Developing a Sniffer

#3862
From: Patrick Wong
Date: Fri, 10 Jun 2005 02:50
67 lines
2543 bytes
Dear all,

I am thinking of developing a sniffer program to monitor packets on two
servers.  I referenced some samples on the web and found several
interesting.  One of which was developed in Delphi so changing it into BCB
would not be hard.

I did some porting and have a trial, and found it has a short-coming.  The
program uses raw socket to capture packets between two machines.  It does
the capture in a thread:

//--------------------------------------------------------------------------
-
void __fastcall TSnifferThread::Execute()
{
  //---- Place thread code here ----
  int         addr, iRet;

  iMySocketState = InitSocket(); /* Initialize the SOCKET */

  while(!Terminated)
  {
    // Save the received packet into a buffer
    iRet = recv(RawSocket, buf, sizeof(buf), 0);

    if(iRet == -1)
      continue;

    pipheader = PIP_Header(buf); // Transform the received packet into a
defined structure

    if((pipheader->ip_destaddr != FDestIp) || (pipheader->ip_srcaddr !=
FSrcIp))
      continue;

    // Display the packet information on a main thread's ListView
    try
    {
      pBuf = new char[iRet];
      CopyMemory(pBuf, buf, iRet);
      Synchronize(AddListItem);
    }
    __finally
    {
      if(pBuf != (char *)NULL)
      {
        delete pBuf;
        pBuf = (char *)NULL;
      }
    }

    Application->ProcessMessages();
  }

  if(ip != (char *)NULL)
  {
    delete ip;
    ip = (char *)NULL;
  }
}

The problem is, the thread keeps looping tightly to capture packets, causing
almost 100% CPU usage.  Are there other means to capture packets without a
tight loop, while packets would not be missed?

Many thanks,
Patrick
Technical Details
Message-ID: <42a8ffee$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!news-out.nntp.be!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,8706ab62d569f7f0,start
X-Google-Attributes: gid11508e,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Patrick Wong" <patrickwong@computer.org>
Newsgroups: borland.public.cppbuilder.internet.socket
Subject: Developing a Sniffer
Date: Fri, 10 Jun 2005 10:50:24 +0800
Lines: 69
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1437
X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1441
NNTP-Posting-Host: 203.82.252.252
Message-ID: <42a8ffee$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1118371822 203.82.252.252 (9 Jun 2005 19:50:22 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!news-out.nntp.be!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail