GetPacketListView.cpp
上传用户:qxsjcl
上传日期:2007-01-08
资源大小:263k
文件大小:11k
源码类别:

网络截获/分析

开发平台:

Visual C++

  1. // GetPacketListView.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "GetPacket.h"
  5. #include "GetPacketListView.h"
  6. #include "mainfrm.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. //global variables
  13. CONTROL_BLOCK cbAdapter;
  14. PVOID    pPacket;
  15. BOOL     bSniff=FALSE;
  16. GetPacketListView* pThreadParam;
  17. DWORD WINAPI GetPacketThread(LPVOID param)
  18. {
  19. //wsastartup , I don't know why
  20. WSADATA wData;
  21. WORD   wVersionRequested = MAKEWORD(2, 0);
  22.     if (WSAStartup(wVersionRequested, &wData) != 0)
  23. {
  24. MessageBox(NULL,"WSAStartup","Error",MB_OK);
  25. return -1;
  26. }
  27. //get adapter's name
  28. ULONG NameLength=64;
  29. PacketGetAdapterNames(cbAdapter.AdapterName,&NameLength);
  30. cbAdapter.BufferSize=1514;
  31. cbAdapter.hMem=GlobalAlloc(GMEM_MOVEABLE,1514);
  32. if(cbAdapter.hMem==NULL)
  33. {
  34. MessageBox(NULL,"GlobalAlloc","Error",MB_OK);
  35. return -1;
  36. }
  37. cbAdapter.lpMem=(LPBYTE)GlobalLock(cbAdapter.hMem);
  38.     //open adapter
  39. cbAdapter.hFile=PacketOpenAdapter(cbAdapter.AdapterName);
  40.     if (cbAdapter.hFile == NULL)
  41. {
  42. MessageBox(NULL,"PacketOpenAdapter","Error",MB_OK);
  43. return -1;
  44. }
  45. //set NIC to PROMISCUOUS status
  46. PacketSetFilter((LPADAPTER)cbAdapter.hFile,NDIS_PACKET_TYPE_PROMISCUOUS);
  47. //allocate packet
  48.     pPacket=PacketAllocatePacket((LPADAPTER)cbAdapter.hFile);
  49. if(!pPacket)
  50. {
  51. MessageBox(NULL,"PacketAllocatePacket","Error",MB_OK);
  52. return -1;
  53. }
  54. PacketInitPacket((LPPACKET)pPacket,cbAdapter.lpMem,1514);
  55. bSniff=TRUE;
  56. while(bSniff)
  57. {
  58. PacketReceivePacket((LPADAPTER)cbAdapter.hFile,(LPPACKET)pPacket,TRUE,&cbAdapter.PacketLength);
  59. //the packet is valid***total frame size should exceeds 14+20+20=54
  60. if (cbAdapter.PacketLength > (sizeof(ETH_HEADER) + sizeof(IP_HEADER) + sizeof(TCP_HEADER)))
  61. {
  62. ETHER_PACKET   *pEthPkt = (ETHER_PACKET *) cbAdapter.lpMem;
  63. ETH_HEADER     *pEthHdr = (ETH_HEADER *) cbAdapter.lpMem;
  64. IP_HEADER      *pIpHdr = (IP_HEADER *) ((char *) pEthPkt + sizeof(ETH_HEADER));
  65. TCP_HEADER     *pTcpHdr = (TCP_HEADER *) ((char *) pIpHdr + sizeof(IP_HEADER));
  66. int             i_IPH_Size = pIpHdr->ihl * 4;
  67. int             i_TCPH_Size = pTcpHdr->doff * 4;
  68. //determine if this is a valid IP packet
  69. if(pEthPkt->ETH.h_proto!=8)
  70. continue;
  71. if (pIpHdr->protocol != 6)
  72. continue;
  73. int  iDataSize = ((int) ntohs(pIpHdr->tot_len) -  i_IPH_Size - i_TCPH_Size);
  74. if(!iDataSize)
  75. continue;
  76. BYTE *pPktData = (BYTE *) pTcpHdr + i_TCPH_Size;
  77. *(pPktData+iDataSize)='';
  78. pThreadParam->SetData(pIpHdr->saddr,pTcpHdr->source,pIpHdr->daddr,pTcpHdr->dest,pPktData,iDataSize);
  79. }
  80. }//end while
  81. PacketFreePacket((LPPACKET)pPacket);
  82. PacketCloseAdapter((LPADAPTER)cbAdapter.hFile);
  83. GlobalUnlock(cbAdapter.hMem);
  84. GlobalFree(cbAdapter.hMem);
  85. WSACleanup();
  86. return 0;
  87. }
  88. /////////////////////////////////////////////////////////////////////////////
  89. // GetPacketListView
  90. IMPLEMENT_DYNCREATE(GetPacketListView, CListView)
  91. GetPacketListView::GetPacketListView()
  92. {
  93. pThreadParam=this;
  94. //bIsSniffing=FALSE;
  95. }
  96. GetPacketListView::~GetPacketListView()
  97. {
  98. }
  99. BEGIN_MESSAGE_MAP(GetPacketListView, CListView)
  100. //{{AFX_MSG_MAP(GetPacketListView)
  101. ON_COMMAND(ID_FILE_START, OnFileStart)
  102. ON_COMMAND(ID_FILE_STOP, OnFileStop)
  103. ON_COMMAND(ID_EDIT_FILTER, OnEditFilter)
  104. ON_UPDATE_COMMAND_UI(ID_FILE_START, OnUpdateFileStart)
  105. ON_UPDATE_COMMAND_UI(ID_FILE_STOP, OnUpdateFileStop)
  106. ON_COMMAND(ID_EDIT_CLEARALL, OnEditClearall)
  107. //}}AFX_MSG_MAP
  108. END_MESSAGE_MAP()
  109. /////////////////////////////////////////////////////////////////////////////
  110. // GetPacketListView drawing
  111. void GetPacketListView::OnDraw(CDC* pDC)
  112. {
  113. CDocument* pDoc = GetDocument();
  114. // TODO: add draw code here
  115. }
  116. /////////////////////////////////////////////////////////////////////////////
  117. // GetPacketListView diagnostics
  118. #ifdef _DEBUG
  119. void GetPacketListView::AssertValid() const
  120. {
  121. CListView::AssertValid();
  122. }
  123. void GetPacketListView::Dump(CDumpContext& dc) const
  124. {
  125. CListView::Dump(dc);
  126. }
  127. #endif //_DEBUG
  128. /////////////////////////////////////////////////////////////////////////////
  129. // GetPacketListView message handlers
  130. void GetPacketListView::OnInitialUpdate() 
  131. {
  132. CListView::OnInitialUpdate();
  133. /*CWnd *pmain;
  134. pmain=AfxGetMainWnd();
  135. pmain->SetWindowText("GetPacket1.0");*/
  136. //set list control's style
  137. LONG dwLong = GetWindowLong(GetListCtrl().m_hWnd, GWL_STYLE);
  138. dwLong ^= LVS_REPORT|LVS_SINGLESEL ;
  139. LONG err=SetWindowLong(GetListCtrl().m_hWnd, GWL_STYLE, dwLong);
  140. ListView_SetExtendedListViewStyle(GetListCtrl(), LVS_EX_FULLROWSELECT); 
  141. //insert 5 columns
  142. LV_COLUMN lv_column;
  143. lv_column.mask=LVCF_TEXT|LVCF_WIDTH|LVCF_SUBITEM ;
  144. lv_column.cx=110;
  145. lv_column.pszText="  Source IP";
  146. GetListCtrl().InsertColumn( 0, &lv_column);
  147. lv_column.cx=80;
  148. lv_column.pszText="Source Port";
  149. GetListCtrl().InsertColumn( 1, &lv_column);
  150. lv_column.cx=110;
  151. lv_column.pszText="    Dest IP";
  152. GetListCtrl().InsertColumn( 2, &lv_column);
  153. lv_column.cx=80;
  154. lv_column.pszText="  Dest Port";
  155. GetListCtrl().InsertColumn( 3, &lv_column);
  156. lv_column.cx=370;
  157. lv_column.pszText="            packet content";
  158. GetListCtrl().InsertColumn( 4, &lv_column);
  159. }
  160. void GetPacketListView::OnFileStart() 
  161. {
  162. DWORD dwThreadID;
  163. hThread=CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)GetPacketThread,
  164.                 (LPVOID)NULL,(DWORD)0,&dwThreadID);
  165. ((CMainFrame*)AfxGetMainWnd())->m_wndStatusBar.SetPaneText( 0, "START RETRIEVING PACKETS...", TRUE );
  166. /*//if(bIsSniffing)
  167. // return;
  168. //bIsSniffing=TRUE;
  169. //wsastartup , I don't know why
  170. WORD   wVersionRequested = MAKEWORD(2, 0);
  171.     if (WSAStartup(wVersionRequested, &wsaData) != 0)
  172. {
  173. MessageBox("WSAStartup error","",MB_OK);
  174. return;
  175. }
  176. //get adapter's name
  177. ULONG NameLength=64;
  178. PacketGetAdapterNames(Adapter.AdapterName,&NameLength);
  179. Adapter.BufferSize=1514;
  180. Adapter.hMem=GlobalAlloc(GMEM_MOVEABLE,1514);
  181. Adapter.lpMem=(LPBYTE)GlobalLock(Adapter.hMem);
  182.     //open adapter
  183. Adapter.hFile=PacketOpenAdapter(Adapter.AdapterName);
  184.     if (Adapter.hFile == NULL)
  185. {
  186. MessageBox("PacketOpenAdapter error",Adapter.AdapterName,MB_OK);
  187. return;
  188. }
  189. //set NIC to PROMISCUOUS status
  190. PacketSetFilter((LPADAPTER)Adapter.hFile,NDIS_PACKET_TYPE_PROMISCUOUS);
  191. //allocate packet
  192.     Packet=PacketAllocatePacket((LPADAPTER)Adapter.hFile);
  193.     if (Packet != NULL) 
  194. {
  195. PacketInitPacket((LPPACKET)Packet,Adapter.lpMem,1514);
  196. int i=0;
  197. while(bIsSniffing)
  198. {
  199. PacketReceivePacket((LPADAPTER)Adapter.hFile,(LPPACKET)Packet,TRUE,&Adapter.PacketLength);
  200. //the packet is valid***total frame size should exceeds 14+20+20=54
  201. if (Adapter.PacketLength > (sizeof(ETH_HEADER) + sizeof(IP_HEADER) + sizeof(TCP_HEADER)))
  202. {
  203. ETHER_PACKET   *pEthPkt = (ETHER_PACKET *) Adapter.lpMem;
  204.             
  205. ETH_HEADER     *pEthHdr = (ETH_HEADER *) Adapter.lpMem;
  206.     IP_HEADER      *pIpHdr = (IP_HEADER *) ((char *) pEthPkt + sizeof(ETH_HEADER));
  207. TCP_HEADER     *pTcpHdr = (TCP_HEADER *) ((char *) pIpHdr + sizeof(IP_HEADER));
  208. int             i_IPH_Size = pIpHdr->ihl * 4;
  209. int             i_TCPH_Size = pTcpHdr->doff * 4;
  210. //determine if this is a valid IP packet
  211. if(pEthPkt->ETH.h_proto!=8)
  212. continue;
  213. if (pIpHdr->protocol != 6)
  214.         continue;
  215.         {
  216.         int  iDataSize = ((int) ntohs(pIpHdr->tot_len) -  i_IPH_Size - i_TCPH_Size);
  217. if(!iDataSize)
  218. continue;
  219.     BYTE *pPktData = (BYTE *) pTcpHdr + i_TCPH_Size;
  220. *(pPktData+iDataSize)='';
  221. SetData(pIpHdr->saddr,pTcpHdr->source,pIpHdr->daddr,pTcpHdr->dest,pPktData,iDataSize);
  222. }
  223. i++;
  224. if(i==1000)
  225. return;
  226. }
  227. }//end while
  228. }*/
  229. }
  230. void GetPacketListView::OnFileStop() 
  231. {
  232. bSniff=FALSE;
  233. //TerminateThread(hThread,0);
  234. /*if(!bIsSniffing)
  235. return;
  236. bIsSniffing=FALSE;
  237. WSACleanup();
  238.     PacketCloseAdapter((LPADAPTER)Adapter.hFile);
  239. if(Packet!=NULL)
  240. PacketFreePacket((LPPACKET)Packet);
  241. PacketSetFilter((LPADAPTER)Adapter.hFile,NDIS_PACKET_TYPE_DIRECTED);*/
  242. }
  243. void GetPacketListView::SetData(__u32 saddr, __u16 source, __u32 daddr, __u16 dest, BYTE * pPktData, int iDataSize)
  244. {
  245. static char     SourceAddress[512] = "";
  246.   static char     DestAddress[512] = "";
  247. char buffer[1024],data[2048],temp[512],*dot;
  248.     struct in_addr  iaddr;
  249. int iSourcePort,iDestPort,i;
  250. CGetPacketApp *pApp;
  251. pApp=(CGetPacketApp*)AfxGetApp();
  252. //save packet data
  253. strcpy(data,(char*)pPktData);
  254. memset(&iaddr,0,sizeof(struct in_addr));
  255.     iaddr.s_addr = saddr;
  256.     strcpy(SourceAddress, inet_ntoa(iaddr));//source IP address
  257. if(pApp->SourceIP.GetSize()>0)
  258. {
  259. for(i=0;i<pApp->SourceIP.GetSize();i++)
  260. if(!strcmp(SourceAddress,pApp->SourceIP.GetAt(i)))
  261. break;
  262. if(i==pApp->SourceIP.GetSize())
  263. return;
  264. }
  265. memset(&iaddr,0,sizeof(struct in_addr));
  266.     iaddr.s_addr = daddr;
  267. strcpy(DestAddress,inet_ntoa(iaddr));   //dest IP address
  268. if(pApp->DestIP.GetSize()>0)
  269. {
  270. for(i=0;i<pApp->DestIP.GetSize();i++)
  271. if(!strcmp(DestAddress,pApp->DestIP.GetAt(i)))
  272. break;
  273. if(i==pApp->DestIP.GetSize())
  274. return;
  275. }
  276. iSourcePort=(int) ntohs(source);   //source port no
  277. if(pApp->SourcePort.GetSize()>0)
  278. {
  279. for(i=0;i<pApp->SourcePort.GetSize();i++)
  280. if((unsigned int)iSourcePort==pApp->SourcePort.GetAt(i))
  281. break;
  282. if(i==pApp->SourcePort.GetSize())
  283. return;
  284. }
  285. iDestPort  =(int) ntohs(dest  );   //destination port no
  286. if(pApp->DestPort.GetSize()>0)
  287. {
  288. for(i=0;i<pApp->DestPort.GetSize();i++)
  289. if((unsigned int)iDestPort==pApp->DestPort.GetAt(i))
  290. break;
  291. if(i==pApp->DestPort.GetSize())
  292. return;
  293. }
  294. #ifndef _DEBUG //filter "172.28.9.*"
  295. strcpy(temp,SourceAddress);
  296. dot=strrchr( temp, '.' );
  297. *dot='';
  298. if(!strcmp(temp,"172.28.9"))
  299. return;
  300. strcpy(temp,DestAddress);
  301. dot=strrchr( temp, '.' );
  302. *dot='';
  303. if(!strcmp(temp,"172.28.9"))
  304. return;
  305. #endif
  306. //have been filtered, can be insert into listview.
  307. int index;
  308. index=GetListCtrl().InsertItem(0,SourceAddress);
  309. if(index!=-1)
  310. {
  311. GetListCtrl().SetItem( index, 1, LVIF_TEXT , _itoa( iSourcePort, buffer, 10 ), 0, 0, 0,0);
  312. GetListCtrl().SetItem( index, 2, LVIF_TEXT , DestAddress     , 0, 0, 0,0);
  313. GetListCtrl().SetItem( index, 3, LVIF_TEXT , _itoa( iDestPort, buffer, 10 ), 0, 0, 0,0);
  314. GetListCtrl().SetItem( index, 4, LVIF_TEXT , data, 0, 0, 0,0);
  315. }
  316.     return ;
  317. }
  318. void GetPacketListView::OnEditFilter() 
  319. {
  320. CFileterDlg FilterDlg;
  321. int ret=FilterDlg.DoModal();
  322. if(ret==IDOK)
  323. {
  324. }
  325. }
  326. void GetPacketListView::OnUpdateFileStart(CCmdUI* pCmdUI) 
  327. {
  328. if(bSniff)
  329. pCmdUI->Enable(FALSE);
  330. else
  331. pCmdUI->Enable(TRUE);
  332. }
  333. void GetPacketListView::OnUpdateFileStop(CCmdUI* pCmdUI) 
  334. {
  335. if(!bSniff)
  336. pCmdUI->Enable(FALSE);
  337. else
  338. pCmdUI->Enable(TRUE);
  339. }
  340. void GetPacketListView::OnEditClearall() 
  341. {
  342. GetListCtrl().DeleteAllItems();
  343. }