----- Original Message -----
Sent: Monday, July 07, 2003 10:20
AM
Subject: [WinPcap-users] how to receive
an ICMP echo reply
I'm trying to write something like a ping
program. I send an ICMP echo request with the thread
SendICMPPacket.
Sending works fine, but I'm not capable of capturing the
reply and recognize it as the reply. The program captures packets, but not
all the packets, how can I change that?
If somebody could check my code
and give an answer why I don't capture everything and if IsICMPReply()
should work.
The code is written in Visual C++ and OnBICMP is the
starting point.
Thank u.
void CPIGDlg::OnBICMP()
{
if(initAdapter()){
//begin
listening
AfxBeginThread(ListenThread,this);
Sleep(500);
//send the
packet
AfxBeginThread(SendICMPPacket,this);
Sleep(6000); //wait
for listenthread to finish
}
}
UINT CPIGDlg::ListenThread(LPVOID
pParam)
{
CPIGDlg * dlg = (CPIGDlg *)
pParam;
dlg->ListenThread();
return
0;
}
void
CPIGDlg::ListenThread()
{
LPPACKET
lpPacket;
char buffer[256000];
if (!m_lpAdapter ||
(m_lpAdapter->hFile ==
INVALID_HANDLE_VALUE))
{
AfxMessageBox("Unable to
open the adapter");
}
// set the network adapter in promiscuous
mode
if(PacketSetHwFilter(m_lpAdapter,NDIS_PACKET_TYPE_PROMISCUOUS)==FALSE){
AfxMessageBox("Warning:
unable to set promiscuous mode!");
}
// set a 512K buffer in the
driver
if(PacketSetBuff(m_lpAdapter,512000)==FALSE){
AfxMessageBox("Unable
to set the kernel buffer!");
}
// set a 1 second read
timeout
if(PacketSetReadTimeout(m_lpAdapter,1000)==FALSE){
AfxMessageBox("Warning:
unable to set the read timeout!");
}
//allocate and initialize a packet
structure that will be used to
//receive the
packets.
if((lpPacket =
PacketAllocatePacket())==NULL){
AfxMessageBox("Error: failed
to allocate the LPPACKET
structure.");
}
PacketInitPacket(lpPacket,(char*)buffer,256000);
//main
capture loop
while(true)
{
//
capture the
packets
if(PacketReceivePacket(m_lpAdapter,lpPacket,TRUE)==FALSE){
MessageBox("Error:
PacketReceivePacket
failed");
}
else{
if(lpPacket->ulBytesReceived!=0){
AfxMessageBox("packet
captured");
(void)IsICMPReply(lpPacket);
}
}
}
PacketFreePacket(lpPacket);
// close the
adapter and exit
PacketCloseAdapter(m_lpAdapter);
}
BOOL CPIGDlg::IsICMPReply(LPPACKET
lpPacket)
{
BOOL bFlag = FALSE;
PIPHEADER
pIPHeader;
PICMPPACKET
pICMPPacket;
char *buf;
u_int
off=0;
struct bpf_hdr *hdr;
buf =
(char*)lpPacket->Buffer;
off=0;
hdr=(struct bpf_hdr
*)(buf+off);
off+=hdr->bh_hdrlen;
pICMPPacket = (PICMPPACKET) (buf +
off); //cast the received lpPacket to a ICMPPacket
if (pICMPPacket->ehhdr.eh_type ==
htons(0x0800)){ //if the ethernet type = IP,
can be icmp
if
(strcmp((char*)(pICMPPacket->ehhdr.eh_dmac),m_myMAC) ==
0
&& pICMPPacket->icmphead.i_id ==
(USHORT)GetCurrentProcessId()) //if m_myMac =
destination MAC and id = current process
id
{
char
szTemp[10];
pIPHeader = (PIPHEADER)(buf + off +
sizeof(EHHDR));
memcpy(szTemp,
&pIPHeader->sourceIP,
sizeof(pIPHeader->sourceIP));
AfxMessageBox((char*)inet_ntoa(*((struct
in_addr *)(szTemp))));
return
TRUE;
}
}
return
FALSE;
}