GetPacketListView.cpp
资源名称:ntpacket.zip [点击查看]
上传用户:qxsjcl
上传日期:2007-01-08
资源大小:263k
文件大小:11k
源码类别:
网络截获/分析
开发平台:
Visual C++
- // GetPacketListView.cpp : implementation file
- //
- #include "stdafx.h"
- #include "GetPacket.h"
- #include "GetPacketListView.h"
- #include "mainfrm.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- //global variables
- CONTROL_BLOCK cbAdapter;
- PVOID pPacket;
- BOOL bSniff=FALSE;
- GetPacketListView* pThreadParam;
- DWORD WINAPI GetPacketThread(LPVOID param)
- {
- //wsastartup , I don't know why
- WSADATA wData;
- WORD wVersionRequested = MAKEWORD(2, 0);
- if (WSAStartup(wVersionRequested, &wData) != 0)
- {
- MessageBox(NULL,"WSAStartup","Error",MB_OK);
- return -1;
- }
- //get adapter's name
- ULONG NameLength=64;
- PacketGetAdapterNames(cbAdapter.AdapterName,&NameLength);
- cbAdapter.BufferSize=1514;
- cbAdapter.hMem=GlobalAlloc(GMEM_MOVEABLE,1514);
- if(cbAdapter.hMem==NULL)
- {
- MessageBox(NULL,"GlobalAlloc","Error",MB_OK);
- return -1;
- }
- cbAdapter.lpMem=(LPBYTE)GlobalLock(cbAdapter.hMem);
- //open adapter
- cbAdapter.hFile=PacketOpenAdapter(cbAdapter.AdapterName);
- if (cbAdapter.hFile == NULL)
- {
- MessageBox(NULL,"PacketOpenAdapter","Error",MB_OK);
- return -1;
- }
- //set NIC to PROMISCUOUS status
- PacketSetFilter((LPADAPTER)cbAdapter.hFile,NDIS_PACKET_TYPE_PROMISCUOUS);
- //allocate packet
- pPacket=PacketAllocatePacket((LPADAPTER)cbAdapter.hFile);
- if(!pPacket)
- {
- MessageBox(NULL,"PacketAllocatePacket","Error",MB_OK);
- return -1;
- }
- PacketInitPacket((LPPACKET)pPacket,cbAdapter.lpMem,1514);
- bSniff=TRUE;
- while(bSniff)
- {
- PacketReceivePacket((LPADAPTER)cbAdapter.hFile,(LPPACKET)pPacket,TRUE,&cbAdapter.PacketLength);
- //the packet is valid***total frame size should exceeds 14+20+20=54
- if (cbAdapter.PacketLength > (sizeof(ETH_HEADER) + sizeof(IP_HEADER) + sizeof(TCP_HEADER)))
- {
- ETHER_PACKET *pEthPkt = (ETHER_PACKET *) cbAdapter.lpMem;
- ETH_HEADER *pEthHdr = (ETH_HEADER *) cbAdapter.lpMem;
- IP_HEADER *pIpHdr = (IP_HEADER *) ((char *) pEthPkt + sizeof(ETH_HEADER));
- TCP_HEADER *pTcpHdr = (TCP_HEADER *) ((char *) pIpHdr + sizeof(IP_HEADER));
- int i_IPH_Size = pIpHdr->ihl * 4;
- int i_TCPH_Size = pTcpHdr->doff * 4;
- //determine if this is a valid IP packet
- if(pEthPkt->ETH.h_proto!=8)
- continue;
- if (pIpHdr->protocol != 6)
- continue;
- int iDataSize = ((int) ntohs(pIpHdr->tot_len) - i_IPH_Size - i_TCPH_Size);
- if(!iDataSize)
- continue;
- BYTE *pPktData = (BYTE *) pTcpHdr + i_TCPH_Size;
- *(pPktData+iDataSize)=' ';
- pThreadParam->SetData(pIpHdr->saddr,pTcpHdr->source,pIpHdr->daddr,pTcpHdr->dest,pPktData,iDataSize);
- }
- }//end while
- PacketFreePacket((LPPACKET)pPacket);
- PacketCloseAdapter((LPADAPTER)cbAdapter.hFile);
- GlobalUnlock(cbAdapter.hMem);
- GlobalFree(cbAdapter.hMem);
- WSACleanup();
- return 0;
- }
- /////////////////////////////////////////////////////////////////////////////
- // GetPacketListView
- IMPLEMENT_DYNCREATE(GetPacketListView, CListView)
- GetPacketListView::GetPacketListView()
- {
- pThreadParam=this;
- //bIsSniffing=FALSE;
- }
- GetPacketListView::~GetPacketListView()
- {
- }
- BEGIN_MESSAGE_MAP(GetPacketListView, CListView)
- //{{AFX_MSG_MAP(GetPacketListView)
- ON_COMMAND(ID_FILE_START, OnFileStart)
- ON_COMMAND(ID_FILE_STOP, OnFileStop)
- ON_COMMAND(ID_EDIT_FILTER, OnEditFilter)
- ON_UPDATE_COMMAND_UI(ID_FILE_START, OnUpdateFileStart)
- ON_UPDATE_COMMAND_UI(ID_FILE_STOP, OnUpdateFileStop)
- ON_COMMAND(ID_EDIT_CLEARALL, OnEditClearall)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // GetPacketListView drawing
- void GetPacketListView::OnDraw(CDC* pDC)
- {
- CDocument* pDoc = GetDocument();
- // TODO: add draw code here
- }
- /////////////////////////////////////////////////////////////////////////////
- // GetPacketListView diagnostics
- #ifdef _DEBUG
- void GetPacketListView::AssertValid() const
- {
- CListView::AssertValid();
- }
- void GetPacketListView::Dump(CDumpContext& dc) const
- {
- CListView::Dump(dc);
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // GetPacketListView message handlers
- void GetPacketListView::OnInitialUpdate()
- {
- CListView::OnInitialUpdate();
- /*CWnd *pmain;
- pmain=AfxGetMainWnd();
- pmain->SetWindowText("GetPacket1.0");*/
- //set list control's style
- LONG dwLong = GetWindowLong(GetListCtrl().m_hWnd, GWL_STYLE);
- dwLong ^= LVS_REPORT|LVS_SINGLESEL ;
- LONG err=SetWindowLong(GetListCtrl().m_hWnd, GWL_STYLE, dwLong);
- ListView_SetExtendedListViewStyle(GetListCtrl(), LVS_EX_FULLROWSELECT);
- //insert 5 columns
- LV_COLUMN lv_column;
- lv_column.mask=LVCF_TEXT|LVCF_WIDTH|LVCF_SUBITEM ;
- lv_column.cx=110;
- lv_column.pszText=" Source IP";
- GetListCtrl().InsertColumn( 0, &lv_column);
- lv_column.cx=80;
- lv_column.pszText="Source Port";
- GetListCtrl().InsertColumn( 1, &lv_column);
- lv_column.cx=110;
- lv_column.pszText=" Dest IP";
- GetListCtrl().InsertColumn( 2, &lv_column);
- lv_column.cx=80;
- lv_column.pszText=" Dest Port";
- GetListCtrl().InsertColumn( 3, &lv_column);
- lv_column.cx=370;
- lv_column.pszText=" packet content";
- GetListCtrl().InsertColumn( 4, &lv_column);
- }
- void GetPacketListView::OnFileStart()
- {
- DWORD dwThreadID;
- hThread=CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)GetPacketThread,
- (LPVOID)NULL,(DWORD)0,&dwThreadID);
- ((CMainFrame*)AfxGetMainWnd())->m_wndStatusBar.SetPaneText( 0, "START RETRIEVING PACKETS...", TRUE );
- /*//if(bIsSniffing)
- // return;
- //bIsSniffing=TRUE;
- //wsastartup , I don't know why
- WORD wVersionRequested = MAKEWORD(2, 0);
- if (WSAStartup(wVersionRequested, &wsaData) != 0)
- {
- MessageBox("WSAStartup error","",MB_OK);
- return;
- }
- //get adapter's name
- ULONG NameLength=64;
- PacketGetAdapterNames(Adapter.AdapterName,&NameLength);
- Adapter.BufferSize=1514;
- Adapter.hMem=GlobalAlloc(GMEM_MOVEABLE,1514);
- Adapter.lpMem=(LPBYTE)GlobalLock(Adapter.hMem);
- //open adapter
- Adapter.hFile=PacketOpenAdapter(Adapter.AdapterName);
- if (Adapter.hFile == NULL)
- {
- MessageBox("PacketOpenAdapter error",Adapter.AdapterName,MB_OK);
- return;
- }
- //set NIC to PROMISCUOUS status
- PacketSetFilter((LPADAPTER)Adapter.hFile,NDIS_PACKET_TYPE_PROMISCUOUS);
- //allocate packet
- Packet=PacketAllocatePacket((LPADAPTER)Adapter.hFile);
- if (Packet != NULL)
- {
- PacketInitPacket((LPPACKET)Packet,Adapter.lpMem,1514);
- int i=0;
- while(bIsSniffing)
- {
- PacketReceivePacket((LPADAPTER)Adapter.hFile,(LPPACKET)Packet,TRUE,&Adapter.PacketLength);
- //the packet is valid***total frame size should exceeds 14+20+20=54
- if (Adapter.PacketLength > (sizeof(ETH_HEADER) + sizeof(IP_HEADER) + sizeof(TCP_HEADER)))
- {
- ETHER_PACKET *pEthPkt = (ETHER_PACKET *) Adapter.lpMem;
- ETH_HEADER *pEthHdr = (ETH_HEADER *) Adapter.lpMem;
- IP_HEADER *pIpHdr = (IP_HEADER *) ((char *) pEthPkt + sizeof(ETH_HEADER));
- TCP_HEADER *pTcpHdr = (TCP_HEADER *) ((char *) pIpHdr + sizeof(IP_HEADER));
- int i_IPH_Size = pIpHdr->ihl * 4;
- int i_TCPH_Size = pTcpHdr->doff * 4;
- //determine if this is a valid IP packet
- if(pEthPkt->ETH.h_proto!=8)
- continue;
- if (pIpHdr->protocol != 6)
- continue;
- {
- int iDataSize = ((int) ntohs(pIpHdr->tot_len) - i_IPH_Size - i_TCPH_Size);
- if(!iDataSize)
- continue;
- BYTE *pPktData = (BYTE *) pTcpHdr + i_TCPH_Size;
- *(pPktData+iDataSize)=' ';
- SetData(pIpHdr->saddr,pTcpHdr->source,pIpHdr->daddr,pTcpHdr->dest,pPktData,iDataSize);
- }
- i++;
- if(i==1000)
- return;
- }
- }//end while
- }*/
- }
- void GetPacketListView::OnFileStop()
- {
- bSniff=FALSE;
- //TerminateThread(hThread,0);
- /*if(!bIsSniffing)
- return;
- bIsSniffing=FALSE;
- WSACleanup();
- PacketCloseAdapter((LPADAPTER)Adapter.hFile);
- if(Packet!=NULL)
- PacketFreePacket((LPPACKET)Packet);
- PacketSetFilter((LPADAPTER)Adapter.hFile,NDIS_PACKET_TYPE_DIRECTED);*/
- }
- void GetPacketListView::SetData(__u32 saddr, __u16 source, __u32 daddr, __u16 dest, BYTE * pPktData, int iDataSize)
- {
- static char SourceAddress[512] = "";
- static char DestAddress[512] = "";
- char buffer[1024],data[2048],temp[512],*dot;
- struct in_addr iaddr;
- int iSourcePort,iDestPort,i;
- CGetPacketApp *pApp;
- pApp=(CGetPacketApp*)AfxGetApp();
- //save packet data
- strcpy(data,(char*)pPktData);
- memset(&iaddr,0,sizeof(struct in_addr));
- iaddr.s_addr = saddr;
- strcpy(SourceAddress, inet_ntoa(iaddr));//source IP address
- if(pApp->SourceIP.GetSize()>0)
- {
- for(i=0;i<pApp->SourceIP.GetSize();i++)
- if(!strcmp(SourceAddress,pApp->SourceIP.GetAt(i)))
- break;
- if(i==pApp->SourceIP.GetSize())
- return;
- }
- memset(&iaddr,0,sizeof(struct in_addr));
- iaddr.s_addr = daddr;
- strcpy(DestAddress,inet_ntoa(iaddr)); //dest IP address
- if(pApp->DestIP.GetSize()>0)
- {
- for(i=0;i<pApp->DestIP.GetSize();i++)
- if(!strcmp(DestAddress,pApp->DestIP.GetAt(i)))
- break;
- if(i==pApp->DestIP.GetSize())
- return;
- }
- iSourcePort=(int) ntohs(source); //source port no
- if(pApp->SourcePort.GetSize()>0)
- {
- for(i=0;i<pApp->SourcePort.GetSize();i++)
- if((unsigned int)iSourcePort==pApp->SourcePort.GetAt(i))
- break;
- if(i==pApp->SourcePort.GetSize())
- return;
- }
- iDestPort =(int) ntohs(dest ); //destination port no
- if(pApp->DestPort.GetSize()>0)
- {
- for(i=0;i<pApp->DestPort.GetSize();i++)
- if((unsigned int)iDestPort==pApp->DestPort.GetAt(i))
- break;
- if(i==pApp->DestPort.GetSize())
- return;
- }
- #ifndef _DEBUG //filter "172.28.9.*"
- strcpy(temp,SourceAddress);
- dot=strrchr( temp, '.' );
- *dot=' ';
- if(!strcmp(temp,"172.28.9"))
- return;
- strcpy(temp,DestAddress);
- dot=strrchr( temp, '.' );
- *dot=' ';
- if(!strcmp(temp,"172.28.9"))
- return;
- #endif
- //have been filtered, can be insert into listview.
- int index;
- index=GetListCtrl().InsertItem(0,SourceAddress);
- if(index!=-1)
- {
- GetListCtrl().SetItem( index, 1, LVIF_TEXT , _itoa( iSourcePort, buffer, 10 ), 0, 0, 0,0);
- GetListCtrl().SetItem( index, 2, LVIF_TEXT , DestAddress , 0, 0, 0,0);
- GetListCtrl().SetItem( index, 3, LVIF_TEXT , _itoa( iDestPort, buffer, 10 ), 0, 0, 0,0);
- GetListCtrl().SetItem( index, 4, LVIF_TEXT , data, 0, 0, 0,0);
- }
- return ;
- }
- void GetPacketListView::OnEditFilter()
- {
- CFileterDlg FilterDlg;
- int ret=FilterDlg.DoModal();
- if(ret==IDOK)
- {
- }
- }
- void GetPacketListView::OnUpdateFileStart(CCmdUI* pCmdUI)
- {
- if(bSniff)
- pCmdUI->Enable(FALSE);
- else
- pCmdUI->Enable(TRUE);
- }
- void GetPacketListView::OnUpdateFileStop(CCmdUI* pCmdUI)
- {
- if(!bSniff)
- pCmdUI->Enable(FALSE);
- else
- pCmdUI->Enable(TRUE);
- }
- void GetPacketListView::OnEditClearall()
- {
- GetListCtrl().DeleteAllItems();
- }