🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Developing a Sniffer

#3863
From: Eelke
Date: Fri, 10 Jun 2005 06:34
78 lines
3131 bytes
Patrick Wong wrote:
> 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
> 
> 
I do not know if it works with raw sockets but normally you can use 
select to wait for data to become available.

Also I guess the code has put the socket in non-blocking mode. You could 
try to use blocking mode in which case recv would wait for data to 
become available.

Eelke
Technical Details
Message-ID: <42a93417$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news2.google.com!proxad.net!212.101.4.254.MISMATCH!solnet.ch!solnet.ch!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <42a8ffee$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,8706ab62d569f7f0
X-Google-Attributes: gid11508e,public
X-Google-Language: ENGLISH,ASCII-7-bit
Date: Fri, 10 Jun 2005 08:34:05 +0200
From: Eelke <e.klein@mplussoftware.nl>
User-Agent: Mozilla Thunderbird 1.0 (Windows/20041206)
X-Accept-Language: en-us, en
MIME-Version: 1.0
Newsgroups: borland.public.cppbuilder.internet.socket
Subject: Re: Developing a Sniffer
References: <42a8ffee$1@newsgroups.borland.com>
In-Reply-To: <42a8ffee$1@newsgroups.borland.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 81.58.1.162
Message-ID: <42a93417$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1118385175 81.58.1.162 (9 Jun 2005 23:32:55 -0700)
Lines: 78
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news2.google.com!proxad.net!212.101.4.254.MISMATCH!solnet.ch!solnet.ch!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail