Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2012839
  • 博文数量: 356
  • 博客积分: 8284
  • 博客等级: 中将
  • 技术积分: 4580
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-15 20:25
个人简介

天行健,君子以自强不息

文章分类

全部博文(356)

文章存档

2018年(1)

2016年(4)

2015年(13)

2014年(14)

2013年(2)

2012年(25)

2011年(43)

2010年(65)

2009年(189)

分类: C/C++

2015-10-17 19:44:40


点击(此处)折叠或打开

  1. // PacketsDisplayDlg.cpp : implementation file
  2. //

  3. #include "stdafx.h"
  4. #include "PacketsDisplay.h"
  5. #include "PacketsDisplayDlg.h"
  6. #include "time.h"

  7. int m_nWidth,m_nHeight;
  8. int m_nDx,m_nDy;
  9. int m_nDx1,m_nDy1;

  10. int g_intbuf[100];
  11. int *g_pBuf;

  12. HWND hStatusWindow;

  13. #ifdef _DEBUG
  14. #define new DEBUG_NEW
  15. #undef THIS_FILE
  16. static char THIS_FILE[] = __FILE__;
  17. #endif


  18. #pragma comment(lib,"Packet.lib")
  19. #pragma comment(lib,"wpcap.lib")
  20. #pragma comment(lib,"ws2_32.lib")
  21. //use printf
  22. #pragma comment(linker, "/subsystem:\"CONSOLE\" /entry:\"WinMainCRTStartup\"")


  23. pcap_if_t *tempdev; //临时网卡变量,用来记录用户选中的网卡
  24. int AdapterNumber = 0; //记录计算机中的网卡数
  25. char DeviceAdapterName[200];//保存用户选定的网卡名字
  26. long double NMaxSec;
  27. long double NMaxNsec;
  28. long double NMinSec;
  29. long double NMinNsec;
  30. long double NAvgSec;
  31. long double NAvgNsec;
  32. long double TotalSec;
  33. long double TotalNsec;
  34. UINT32 PacketNum;

  35. /////////////////////////////////////////////////////////////////////////////
  36. // CAboutDlg dialog used for App About

  37. class CAboutDlg : public CDialog
  38. {
  39. public:
  40.     CAboutDlg();

  41. // Dialog Data
  42.     //{{AFX_DATA(CAboutDlg)
  43.     enum { IDD = IDD_ABOUTBOX };
  44.     //}}AFX_DATA

  45.     // ClassWizard generated virtual function overrides
  46.     //{{AFX_VIRTUAL(CAboutDlg)
  47.     protected:
  48.     virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
  49.     //}}AFX_VIRTUAL

  50. // Implementation
  51. protected:
  52.     //{{AFX_MSG(CAboutDlg)
  53.     //}}AFX_MSG
  54.     DECLARE_MESSAGE_MAP()
  55. };

  56. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  57. {
  58.     //{{AFX_DATA_INIT(CAboutDlg)
  59.     //}}AFX_DATA_INIT
  60. }

  61. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  62. {
  63.     CDialog::DoDataExchange(pDX);
  64.     //{{AFX_DATA_MAP(CAboutDlg)
  65.     //}}AFX_DATA_MAP
  66. }

  67. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  68.     //{{AFX_MSG_MAP(CAboutDlg)
  69.         // No message handlers
  70.     //}}AFX_MSG_MAP

  71. END_MESSAGE_MAP()

  72. /////////////////////////////////////////////////////////////////////////////
  73. // CPacketsDisplayDlg dialog

  74. CPacketsDisplayDlg::CPacketsDisplayDlg(CWnd* pParent /*=NULL*/)
  75.     : CDialog(CPacketsDisplayDlg::IDD, pParent)
  76. {
  77.     //{{AFX_DATA_INIT(CPacketsDisplayDlg)
  78.         // NOTE: the ClassWizard will add member initialization here
  79.     //}}AFX_DATA_INIT
  80.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  81.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  82. }

  83. void CPacketsDisplayDlg::DoDataExchange(CDataExchange* pDX)
  84. {
  85.     CDialog::DoDataExchange(pDX);
  86.     //{{AFX_DATA_MAP(CPacketsDisplayDlg)
  87.     DDX_Control(pDX, IDC_COMBO1, m_SelectAdapterCtrl);
  88.     //}}AFX_DATA_MAP
  89. }

  90. BEGIN_MESSAGE_MAP(CPacketsDisplayDlg, CDialog)
  91.     //{{AFX_MSG_MAP(CPacketsDisplayDlg)
  92.     ON_WM_SYSCOMMAND()
  93.     ON_WM_PAINT()
  94.     ON_WM_QUERYDRAGICON()
  95.     ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
  96.     ON_CBN_SELCHANGE(IDC_COMBO1, OnSelchangeCombo1)
  97.     ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
  98.     ON_WM_CLOSE()
  99.     ON_WM_CANCELMODE()
  100.     ON_WM_TIMER()
  101.     ON_WM_CAPTURECHANGED()
  102.     ON_MESSAGE(WM_GETRECEIVEPACKET,OnGetReceivePacket)
  103.     ON_MESSAGE(WM_DISSERVERTIME,OnDisPlayServerTime)
  104.     //ON_MESSAGE(SB_SETPARTS,)

  105.     //}}AFX_MSG_MAP
  106. END_MESSAGE_MAP()

  107. /////////////////////////////////////////////////////////////////////////////
  108. // CPacketsDisplayDlg message handlers

  109. BOOL CPacketsDisplayDlg::OnInitDialog()
  110. {
  111.     CDialog::OnInitDialog();

  112.     // Add "About..." menu item to system menu.

  113.     // IDM_ABOUTBOX must be in the system command range.
  114.     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  115.     ASSERT(IDM_ABOUTBOX < 0xF000);

  116.     CMenu* pSysMenu = GetSystemMenu(FALSE);
  117.     if (pSysMenu != NULL)
  118.     {
  119.         CString strAboutMenu;
  120.         strAboutMenu.LoadString(IDS_ABOUTBOX);
  121.         if (!strAboutMenu.IsEmpty())
  122.         {
  123.             pSysMenu->AppendMenu(MF_SEPARATOR);
  124.             pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  125.         }
  126.     }

  127.     // Set the icon for this dialog. The framework does this automatically
  128.     // when the application's main window is not a dialog
  129.     SetIcon(m_hIcon, TRUE);            // Set big icon
  130.     SetIcon(m_hIcon, FALSE);        // Set small icon
  131.     
  132.     // TODO: Add extra initialization here

  133.     //初始化memmap

  134.     InitSharedMem();
  135.     CreateSharedMem(100*4);

  136.     //skin


  137.   /*
  138.     CPacketsDisplayDlg dlg;

  139.     //HWND m_pMainWnd=&dlg;
  140.     VERIFY( 1 == SetWindowSkin(AfxGetMainWnd()->m_hWnd , "MainFrame" ));
  141.     AfxGetMainWnd()->ShowWindow(SW_SHOW);
  142.     AfxGetMainWnd()->UpdateWindow();
  143.     VERIFY(1 == SetDialogSkin("Dialog"));//设置对话框的皮肤
  144.     dlg.DoModal();

  145.     AutoBootUp();*/
  146.     InitStatusBar();

  147.     m_pNICHandle = NULL;//把网卡指针的初始值设置为NULL
  148.     m_pEthCapThread = NULL;//把以太网抓包线程指针设置为NULL


  149.     //////////////////////////////////////////////////////////////////////////
  150.     

  151.     //dynamic window
  152.     CRect dlgRect;
  153.     GetWindowRect(dlgRect);
  154.     CRect desktopRect;
  155.     GetDesktopWindow()->GetWindowRect(desktopRect);
  156.     MoveWindow((desktopRect.Width()-dlgRect.Width())/2,(desktopRect.Height()-dlgRect.Height())/2,0,0);
  157.     m_nWidth=dlgRect.Width();
  158.     m_nHeight=dlgRect.Height();
  159.     m_nDx=6;
  160.     m_nDy=8;
  161.     m_nDx1=6;
  162.     m_nDy1=6;
  163.     SetTimer(2,2,NULL);//关闭动画效果
  164.     //endl

  165.     //注册表操作,自动选中上次最后使用的网卡
  166.     m_DstIP = 0xC0A80000;//临时定义的一个地址变量,看日后是否有用
  167.     CString mLastUsedNIC ="";//上次选中的网卡名称
  168.     long result;
  169.     HKEY mHKEY;
  170.     DWORD dwFlag;        
  171.     try
  172.     {
  173.         result = RegCreateKeyEx(HKEY_CURRENT_USER,"SoftWare\\XXXX\\PacketsDisplay\\",
  174.             0,NULL,REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL,&mHKEY,&dwFlag);
  175.         if(result == ERROR_SUCCESS)
  176.         {
  177.             BYTE mData[120];
  178.             DWORD mDataType;
  179.             DWORD mLen = 4;
  180.             DWORD mLenCopy = mLen;
  181.             result = RegQueryValueEx(mHKEY,"DstIP",NULL,&mDataType,mData,&mLen);
  182.             if(result==ERROR_SUCCESS && mDataType==REG_BINARY && mLen==mLenCopy)
  183.             {
  184.                 m_DstIP = *(UINT*)mData;
  185.             }    
  186.             mLen = 120;
  187.             result = RegQueryValueEx(mHKEY,"NICName",0,&mDataType,mData,&mLen);
  188.             if(result==ERROR_SUCCESS && mDataType==REG_SZ)
  189.             {
  190.                 mLastUsedNIC = mData;
  191.             }
  192.             RegCloseKey(mHKEY);
  193.             result = ERROR_CANCELLED;
  194.         }
  195.     }
  196.     catch (...)
  197.     {
  198.         if (result == ERROR_SUCCESS)
  199.         {
  200.             RegCloseKey(mHKEY);
  201.         }
  202.     }    

  203.     //检索本地计算机中的网卡,并且开启上次最后使用的网卡
  204.     pcap_if_t *d;
  205.     char errbuf[PCAP_ERRBUF_SIZE]; //256
  206.     //检索本地计算机中的网卡,如果没有找到,则返回
  207.     if (pcap_findalldevs(&alldevs, errbuf) == -1)
  208.     {
  209.         AfxMessageBox("在本地计算机中没有找到网卡\n");
  210.     }
  211.     AdapterList.RemoveAll();//清空列表
  212.     //把网卡名称和适配器列表名添加到AdapterList列表中
  213.     for(tempdev=alldevs; tempdev; tempdev=tempdev->next)
  214.     {
  215.         CString temp1; //网卡名称
  216.         CString temp2; //适配器名称
  217.         pcap_addr_t *pAddr = tempdev->addresses;//只取一个IP,(网卡可能有多个IP)
  218.         temp1.Format("%d.",++AdapterNumber);
  219.         if (pAddr)//有IP地址
  220.         {
  221.             switch(pAddr->addr->sa_family)
  222.             {
  223.             case AF_INET: //2
  224.                 {
  225.                     if (pAddr->addr)
  226.                         temp1.Format("%d. %s,%s",AdapterNumber, inet_ntoa(((struct sockaddr_in *)pAddr->addr)->sin_addr),tempdev->name); //序号+IP地址+网卡物理号
  227.                     //The inet_ntoa function converts an (Ipv4) Internet network address into a string in Internet standard dotted-decimal format.
  228.                     break;
  229.                 }
  230.             default:
  231.                 break;//未知协议
  232.             }
  233.         }
  234.         if (tempdev->description)
  235.         {
  236.             temp2.Format(" (%s)\n", tempdev->description); //适配器名称
  237.         }
  238.         else
  239.         {
  240.             temp2.Format(" (No description available)\n");
  241.         }
  242.         CString *ptemp3 = new CString; //记录网卡名称+适配器名称
  243.         ptemp3->Format("%s",temp1 + temp2);
  244.         AdapterList.AddTail(ptemp3); //添加至链表
  245.     }

  246.     //把网卡信息添加至下拉框中
  247.     POSITION pos1;
  248.     pos1 = AdapterList.GetHeadPosition();
  249.     while (pos1 != NULL)
  250.     {
  251.         CString *pString = NULL;
  252.         pString = (CString *)AdapterList.GetNext(pos1);
  253.         CString temp;
  254.         temp = *pString;
  255.         m_SelectAdapterCtrl.AddString(temp);
  256.     }

  257.     int index=0;
  258.     //设置下拉框的值,设置为上次选中的网卡
  259.     if (alldevs != NULL)
  260.     {
  261.         if (mLastUsedNIC != "")
  262.         {
  263.             for (d=alldevs; NULL;d=d->next,index++)
  264.             {
  265.                 CString nicName(d->name);
  266.                 if (nicName == mLastUsedNIC)
  267.                 {
  268.                     m_SelectAdapterCtrl.SetCurSel(index);
  269.                     break;
  270.                 }
  271.             }
  272.         }
  273.     }

  274.     //释放链表里的内存
  275.     while(AdapterList.IsEmpty()==0)
  276.     {
  277.         CString* prec = (CString*)AdapterList.RemoveHead();
  278.         delete prec;
  279.     }

  280.     int a;
  281.     for(tempdev=alldevs, a=0; a< index ;tempdev=tempdev->next, a++);//把tempdev设置为当前选中的网卡
  282.     ZeroMemory(DeviceAdapterName,100);
  283.     memcpy(DeviceAdapterName,tempdev->name,strlen(tempdev->name));//Copies bytes between buffers
  284.     StartCap();//开启网卡和对应的线程

  285.     
  286.     return TRUE; // return TRUE unless you set the focus to a control
  287. }

  288. void CPacketsDisplayDlg::OnSysCommand(UINT nID, LPARAM lParam)
  289. {
  290.     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  291.     {
  292.         CAboutDlg dlgAbout;
  293.         dlgAbout.DoModal();
  294.     }
  295.     else
  296.     {
  297.         CDialog::OnSysCommand(nID, lParam);
  298.     }
  299. }

  300. // If you add a minimize button to your dialog, you will need the code below
  301. // to draw the icon. For MFC applications using the document/view model,
  302. // this is automatically done for you by the framework.

  303. void CPacketsDisplayDlg::OnPaint()
  304. {
  305.     if (IsIconic())
  306.     {
  307.         CPaintDC dc(this); // device context for painting

  308.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

  309.         // Center icon in client rectangle
  310.         int cxIcon = GetSystemMetrics(SM_CXICON);
  311.         int cyIcon = GetSystemMetrics(SM_CYICON);
  312.         CRect rect;
  313.         GetClientRect(&rect);
  314.         int x = (rect.Width() - cxIcon + 1) / 2;
  315.         int y = (rect.Height() - cyIcon + 1) / 2;

  316.         // Draw the icon
  317.         dc.DrawIcon(x, y, m_hIcon);
  318.     }
  319.     else
  320.     {
  321.         CDialog::OnPaint();
  322.     }
  323. }

  324. // The system calls this to obtain the cursor to display while the user drags
  325. // the minimized window.
  326. HCURSOR CPacketsDisplayDlg::OnQueryDragIcon()
  327. {
  328.     return (HCURSOR) m_hIcon;
  329. }



  330. //=================以太网抓包线程函数================================
  331. //线程函数
  332. UINT EthernetCAP_FRT(LPVOID lp)
  333. {
  334.     EthernetCAPParam* p_capparam = (EthernetCAPParam*)lp;
  335.     int capret = 0;
  336.     HRESULT AddRet = 0;
  337.     unsigned char* pbuff = NULL;
  338.     pcap_pkthdr* h = NULL;
  339. //SendData(cardtype,command,data,ip,datalength,SNO);
  340.     while(p_capparam->m_bCapOn)
  341.     {
  342.         //读取报文
  343.         capret = pcap_next_ex(p_capparam->p_adapterHandle, &h, (const u_char**)&pbuff);     //读取报文
  344.         //capret=1
  345.         if (capret != 1)
  346.         {
  347.             continue;
  348.         }    
  349.         // 匹配报文,匹配自己从sniffer中发出的报文,位置偏移13位和14位(20110624 by CHEN )
  350.     //    if (*(pbuff + 16) == 0x55 && *(pbuff + 17) == 0x55 ) //协议类型和响应报文的IRTTYPE都匹配上
  351.         {
  352.             ::SendMessage(AfxGetMainWnd()->m_hWnd,WM_GETRECEIVEPACKET,(WPARAM)pbuff,NULL);//向消息窗口发送一条消息并立即返回
  353.         }
  354.         
  355.         
  356.     }
  357.     return S_OK;
  358. }
  359. ///////自己添加的程序
  360. // 发送数据 cardtype是卡件类型 command是报文中的命令字段,datlength是发送的数据长度 SNO是发送数据时的序号
  361. bool SendData(UINT16 cardtype, UINT32 command, UINT16 *data, UINT ip,UINT32 datalength,UINT32 SNO) //0x0600 0x44444444 "" 0xffffffff 0 0
  362. {
  363.     //数据打包
  364.     SendPacket buffer; //packet结构体(报文)
  365.     ZeroMemory(&buffer,sizeof(buffer)); //清空数据,把内存数据置为零
  366.     memset(&buffer,0xff,12);//dmac全部设置成0xff,设置某一段内存的显示格式(设置目标地址DMAC)
  367.     buffer.SMAC[0] = 0x00;//SMAC的值可以设置为默认值,对结果没有任何影响
  368.     buffer.SMAC[1] = 0x01;
  369.     buffer.SMAC[2] = 0x02;
  370.     buffer.SMAC[3] = 0x03;
  371.     buffer.SMAC[4] = 0x04;
  372.     buffer.SMAC[5] = 0x05;
  373.     buffer.TYPE = 0x0789;//设置为默认值0x0789
  374.     //buffer.CardType = cardtype;//卡件类型 0x0600
  375.     //buffer.DestIP = ip;//目的ip 0xffffffff

  376.     //数据crc校验
  377.     //buffer.packCheckSum = 0;
  378.     //buffer.command = command;//0x44444444
  379.     //buffer.SNO = SNO;//0000000
  380.     buffer.DataLength = datalength;
  381.     //memcpy(buffer.data,data,DATALEN*2);//在缓冲区之间拷贝字符
  382.     //////////////////////////////////////////////if (command == UPPROEND)//最后一个
  383.     {
  384.         //    buffer.dataCRC = DATACRC;
  385.         ///////////buffer.DataLength = FileDataLen;
  386.         buffer.DataLength = 60;
  387.     }
  388.     //////////////////////////////////////////////else //如果不是最后一个,则要进行CRC校验
  389.     {
  390.         //buffer.dataCRC = crc32_check((UINT8*)data,datalength,RALATIONKEY); //进行一次CRC校验
  391.     }
  392.     //buffer.packCheckSum = FlashProChksum((UINT16*)(&buffer),48);//数据头校验

  393.     //发送数据
  394.     pcap_t *adhandle;
  395.     char errbuf[PCAP_ERRBUF_SIZE]; //256
  396.     // * Open the adapter * /
  397.     if ((adhandle= pcap_open_live(DeviceAdapterName, // name of the device //保存用户选定的网卡名字
  398.         65535, // portion of the packet to capture.
  399.         // 65536 grants that the whole packet will be captured on all the MACs.
  400.         1, // promiscuous mode
  401.         1000, // read timeout
  402.         errbuf // error buffer
  403.         )) == NULL)
  404.     {
  405.         AfxMessageBox("\nUnable to open the adapter.It is not supported by WinPcap\n");
  406.         // * Free the device list * /
  407.         //pcap_freealldevs(alldevs);
  408.         return false;
  409.     }
  410.     int a = pcap_sendpacket(adhandle,(u_char*)&buffer,sizeof(buffer));//发送包
  411.     // * At this point, we don't need any more the device list. Free it * /
  412.     pcap_close(adhandle);
  413.     return true;
  414. }
  415. bool MySendData(void)
  416. {
  417.     //数据打包
  418.     MySendPacket buffer; //packet结构体(报文)
  419.     ZeroMemory(&buffer,sizeof(buffer)); //清空数据,把内存数据置为零
  420.     memset(&buffer,0xff,6);//dmac全部设置成0xff,设置某一段内存的显示格式(设置目标地址DMAC)
  421.     buffer.smac[0] = 0x00;//SMAC的值可以设置为默认值,对结果没有任何影响
  422.     buffer.smac[1] = 0x01;
  423.     buffer.smac[2] = 0x02;
  424.     buffer.smac[3] = 0x03;
  425.     buffer.smac[4] = 0x04;
  426.     buffer.smac[5] = 0x05;
  427.     buffer.type = 0x0081;//设置为默认值0x0789
  428.     buffer.msgid=0x5555;
  429.     buffer.pri=0x70;
  430.     buffer.vlan=0x00;
  431.     buffer.rsv=0;
  432.     buffer.len = 0x40;

  433.     pcap_t *adhandle;
  434.     char errbuf[PCAP_ERRBUF_SIZE]; //256
  435.     // * Open the adapter * /
  436.     
  437.     if ((adhandle= pcap_open_live(DeviceAdapterName, // name of the device //保存用户选定的网卡名字
  438.         65535, // portion of the packet to capture.
  439.         // 65536 grants that the whole packet will be captured on all the MACs.
  440.         1, // promiscuous mode
  441.         1000, // read timeout
  442.         errbuf // error buffer
  443.         )) == NULL)
  444.     {
  445.         AfxMessageBox("\nUnable to open the adapter.It is not supported by WinPcap\n");
  446.         // * Free the device list * /
  447.         //pcap_freealldevs(alldevs);
  448.         return false;
  449.     }
  450.     
  451.     int a = pcap_sendpacket(adhandle,(u_char*)&buffer,sizeof(buffer));//发送包
  452.     // * At this point, we don't need any more the device list. Free it * /
  453.     pcap_close(adhandle);
  454.     return true;
  455. }

  456. BOOL SendFolowUp(DWORD sec,DWORD nsec)
  457. {
  458.     BYTE bydata[60];
  459.     ZeroMemory(bydata,sizeof(bydata));
  460.     FolowUpHead_t *pFolowUp=(FolowUpHead_t *)bydata;
  461.     DWORD dwSec=htonl(sec);
  462.     DWORD dwNsec=htonl(nsec);

  463.     pFolowUp->bydmac[0]=0x00;
  464.     pFolowUp->bydmac[1]=0x1b;
  465.     pFolowUp->bydmac[2]=0x19;
  466.     pFolowUp->bydmac[3]=0x00;
  467.     pFolowUp->bydmac[4]=0x00;
  468.     pFolowUp->bydmac[5]=0x00;

  469.     pFolowUp->bysmac[0]=0x00;
  470.     pFolowUp->bysmac[1]=0x1e;
  471.     pFolowUp->bysmac[2]=0xcd;
  472.     pFolowUp->bysmac[3]=0x00;
  473.     pFolowUp->bysmac[4]=0x00;
  474.     pFolowUp->bysmac[5]=0x83;
  475.     pFolowUp->wtype=htons(0x88f7);
  476.     pFolowUp->wproctol=htons(0x0802);

  477.     memcpy(bydata+50,&dwSec,4);
  478.     memcpy(bydata+54,&dwNsec,4);

  479.     pcap_t *phandle;
  480.     char errbuf[PCAP_ERRBUF_SIZE];

  481.     if ((phandle=pcap_open_live(DeviceAdapterName,
  482.         65535,
  483.         1,
  484.         1000,
  485.         errbuf))==NULL
  486.      )
  487.     {
  488.         AfxMessageBox("err");
  489.         return FALSE;
  490.     }
  491.     int ret=pcap_sendpacket(phandle,bydata,sizeof(bydata));
  492.     pcap_close(phandle);

  493.     return TRUE;
  494. }



  495. //开始抓包函数
  496. int CPacketsDisplayDlg::StartCap()
  497. {
  498.     //开启网卡
  499.     if(m_pNICHandle != NULL)
  500.     {
  501.         return 1;
  502.     }
  503.     char errbuf[PCAP_ERRBUF_SIZE];
  504.     m_pNICHandle = pcap_open_live(tempdev->name, // name of the device
  505.         65535, // portion of the packet to capture.65536 grants that the whole packet will be captured on all the MACs.
  506.         1, // promiscuous mode
  507.         10, // read timeout
  508.         errbuf); // error buffer        
  509.     if (m_pNICHandle == NULL)
  510.     {
  511.         return 2;
  512.     }

  513.     //设置抓包过滤
  514.     //char* pFilter = "ether proto 0x5555";
  515.     //bpf_program fcode;
  516.     //if (pcap_compile(m_pNICHandle, &fcode,pFilter, 1, 0x00ffffff) < 0)
  517.     //{
  518.     //    return 3;//pcap_compile 错误
  519.     //}
  520.     //if (pcap_setfilter(m_pNICHandle, &fcode) < 0)
  521.     //{
  522.     //    return 4;//pcap_setfilter 错误
  523.     //}

  524.     //启动抓包线程
  525.     m_EthCapParam.m_bCapOn = true;
  526.     m_EthCapParam.p_adapterHandle = m_pNICHandle;//winpcap中用到的网卡指针 网卡
  527.     if (m_pEthCapThread != NULL)
  528.     {
  529.         //抓包线程未关闭
  530.         return 5;
  531.     }
  532.     m_pEthCapThread = AfxBeginThread(EthernetCAP_FRT,(LPVOID)&m_EthCapParam);//开启抓包线程
  533.     if (m_pEthCapThread == NULL)
  534.     {
  535.         //抓包线程开启失败
  536.         return 6;
  537.     }

  538.     ///SetTimer(1,1000,NULL);///定时发送时间信息
  539.     SetTimer(5,1000,NULL);
  540.     return S_OK;
  541. }

  542. //结束抓包函数
  543. int CPacketsDisplayDlg::StopCap()
  544. {
  545.     int retval = 0;
  546.     // 关闭抓包线程
  547.     if (m_pEthCapThread != NULL)
  548.     {
  549.         m_EthCapParam.m_bCapOn = false;//以太网抓包函数线程参数
  550.         DWORD ret = WaitForSingleObject(m_pEthCapThread->m_hThread,5000);//以太网抓包线程指针
  551.         if (ret != WAIT_OBJECT_0)
  552.         {
  553.             TerminateThread(m_pEthCapThread->m_hThread,0);
  554.             delete m_pEthCapThread;
  555.             retval = 1;
  556.         }
  557.         m_pEthCapThread = NULL;
  558.     }
  559.     // 关闭网卡
  560.     if (m_pNICHandle != NULL)
  561.     {
  562.         pcap_close(m_pNICHandle);//关闭网卡
  563.         m_pNICHandle = NULL;
  564.     }
  565.     return retval;
  566. }
  567. LRESULT CPacketsDisplayDlg::OnDisPlayServerTime(WPARAM wParam,LPARAM lParam)
  568. {


  569.     ptp_date_time_t *p=(ptp_date_time_t *)wParam;
  570.     CString strDis,strTemp;
  571.     strDis="";
  572.     strTemp.Format("%4d-",p->year);
  573.     strDis+=strTemp;
  574.     strTemp.Format("%2d-",p->mon);
  575.     strDis+=strTemp;
  576.     strTemp.Format("%2d ",p->day);
  577.     strDis+=strTemp;
  578.     strTemp.Format("%2d;",p->hour);
  579.     strDis+=strTemp;
  580.     strTemp.Format("%2d;",p->min);
  581.     strDis+=strTemp;
  582.     strTemp.Format("%2d",p->sec);
  583.     strDis+=strTemp;


  584.     GetDlgItem(IDC_STATIC_TIME)->SetWindowText(strDis);
  585.     
  586.     return 0;
  587. }
  588. LRESULT CPacketsDisplayDlg::OnGetReceivePacket(WPARAM wParam,LPARAM lParam)
  589. {
  590.     PacketNum++;
  591.     


  592.     WORD wtype=*((WORD *)(wParam+12));
  593.     DWORD dwsec;
  594.     DWORD dwnsec;

  595.     wtype=htons(wtype);

  596.     if (wtype==0x88f7 )
  597.     {
  598.         printf("ptp ");
  599.         printf("%4x\r\n",wtype);
  600.         memcpy(&dwsec ,(char *)(wParam)+50,4);
  601.         memcpy(&dwnsec,(char *)(wParam)+54,4);
  602.         dwsec=htonl(dwsec);
  603.         dwnsec=htonl(dwnsec);

  604.         struct tm *p=gmtime((time_t *)&dwsec);
  605.         ptp_date_time_t ptp;
  606.         ptp.year=p->tm_year+1900;
  607.         ptp.mon=p->tm_mon+1;
  608.         ptp.day=p->tm_mday;
  609.         ptp.hour=p->tm_hour+8;
  610.         ptp.min=p->tm_min;
  611.         ptp.sec=p->tm_sec;

  612.             //::SendMessage(AfxGetMainWnd()->m_hWnd,WM_GETRECEIVEPACKET,(WPARAM)pbuff,NULL);//向消息窗口发送一条消息并立即返回
  613.         ::SendMessage(AfxGetMainWnd()->m_hWnd,WM_DISSERVERTIME,(WPARAM)&ptp,NULL);
  614.         printf("sec is %d,nsec is %d,%4d-%2d-%2d,%2d:%2d:%2d\r\n",dwsec,dwnsec,p->tm_year+1900,p->tm_mon+1,p->tm_mday,
  615.             p->tm_hour+8,p->tm_min,p->tm_sec);
  616.     }



  617.     HWND    hRed=::GetDlgItem(m_hWnd,IDC_RED);
  618.     if(::IsWindowVisible(hRed))
  619.     {
  620.         ::ShowWindow(hRed,SW_HIDE);
  621.     }
  622.     else
  623.     {
  624.         ::ShowWindow(hRed,SW_SHOW);
  625.     }


  626.  
  627.     return 0;
  628. }

  629. void CPacketsDisplayDlg::OnClose()
  630. {
  631.     // TODO: 在此添加消息处理程序代码和/或调用默认值
  632.     long result;
  633.     HKEY mHKEY;
  634.     DWORD dwFlag;        
  635.     try
  636.     {
  637.         result = RegCreateKeyEx(HKEY_CURRENT_USER,"SoftWare\\XXXX\\PacketsDisplay\\",
  638.             0,NULL,REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL,&mHKEY,&dwFlag);
  639.         if(result == ERROR_SUCCESS)
  640.         {
  641.             BYTE mData[24];
  642.             memcpy(mData,(BYTE*)&m_DstIP,4);
  643.             RegSetValueEx(mHKEY,"DstIP",0,REG_BINARY,mData,4);
  644.             RegSetValueEx(mHKEY,"NICName",0,REG_SZ,(const BYTE*)DeviceAdapterName,strlen(DeviceAdapterName));                                    
  645.             RegCloseKey(mHKEY);
  646.             result = ERROR_CANCELLED;
  647.         }
  648.     }
  649.     catch (...)
  650.     {
  651.         if (result == ERROR_SUCCESS)
  652.         {
  653.             RegCloseKey(mHKEY);
  654.         }
  655.     }    
  656.     StopCap();//关闭抓包线程
  657.     pcap_freealldevs(alldevs);

  658.     SetTimer(3,2,NULL);//启动动画效果
  659.     //CDialog::OnClose();
  660. }

  661. void CPacketsDisplayDlg::OnSelchangeCombo1()
  662. {
  663.     // TODO: 在此添加控件通知处理程序代码
  664.     int SelectedAdapterIndex=0;//记录选择网卡界面中选中的网卡序号
  665.     //之前如有网卡开启,则关闭该网卡对应的抓包线程,并且关闭该网卡
  666.     while(m_pNICHandle !=NULL)
  667.     {
  668.         StopCap();//关闭线程
  669.     }
  670.     //在下拉框中选择网卡,并记录下选择的网卡序号
  671.     if(AdapterNumber >0)//多块网卡
  672.     {
  673.         SelectedAdapterIndex =m_SelectAdapterCtrl.GetCurSel()+1;
  674.     }

  675.     //异常情况:没有选择网卡或者选中的网卡序号大于网卡数
  676.     if(SelectedAdapterIndex<1 || SelectedAdapterIndex>AdapterNumber)
  677.     {
  678.         AfxMessageBox("Interface number out of range.");
  679.         pcap_freealldevs(alldevs);
  680.         return;
  681.     }
  682.     int a;
  683.     for(tempdev=alldevs, a=0; a< SelectedAdapterIndex - 1 ;tempdev=tempdev->next, a++);//把tempdev设置为当前选中的网卡

  684.     ZeroMemory(DeviceAdapterName,100);
  685.     memcpy(DeviceAdapterName,tempdev->name,strlen(tempdev->name));//将选中的网卡的名字,作为全局变量保存在DeviceAdapterName中
  686.     StartCap();//开启网卡
  687. }


  688. void CPacketsDisplayDlg::OnButton1()
  689. {
  690.     //SendData(cardtype,command,data,ip,datalength,SNO);
  691.     // TODO: 在此添加控件通知处理程序代码
  692.     MySendData();
  693.     ::SendMessage(hStatusWindow,SB_SETTEXT,2,(LPARAM)TEXT("FUCK"));
  694.     memset((void*)g_pBuf,0x11,100);
  695. }

  696. void CPacketsDisplayDlg::OnButton2()
  697. {
  698.     // TODO: Add your control notification handler code here
  699.     static char tick=1;
  700.     tick++;
  701.     time_t t=time(NULL);//把当前时间转换成秒
  702.     struct tm p=*gmtime(&t);//将秒转换成当前时间
  703.     DWORD year=p.tm_year;
  704.     DWORD mon=p.tm_mon;
  705.     DWORD day=p.tm_mday;
  706.     DWORD hour=p.tm_hour;
  707.     DWORD minute=p.tm_min;
  708.     DWORD sec=p.tm_sec;
  709.     memset((void*)g_pBuf,0x55,100);
  710.     SendFolowUp(134,0x146fa418);
  711. }



  712. void CPacketsDisplayDlg::OnCancelMode()
  713. {
  714.     CDialog::OnCancelMode();
  715.     
  716.     // TODO: Add your message handler code here
  717.     
  718. }


  719. void CPacketsDisplayDlg::AutoBootUp()
  720. {
  721.     HKEY hKey;
  722.     CString strRegPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
  723.     if (RegOpenKeyEx(HKEY_CURRENT_USER, strRegPath, 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS) //打开启动项
  724.     {
  725.         TCHAR szModule[_MAX_PATH];
  726.         GetModuleFileName(NULL, szModule, _MAX_PATH);//得到本程序自身的全路径
  727.         RegSetValueEx(hKey,"PacketsDisplay", 0, REG_SZ, (const BYTE*)(LPCSTR)szModule, strlen(szModule)); //添加一个子Key,并设置值,"Client"是应用程序名字(不加后缀.exe)
  728.         RegCloseKey(hKey); //关闭注册表
  729.     }
  730.     else
  731.     {
  732.         AfxMessageBox("系统参数错误,不能随系统启动");
  733.     }
  734. }


  735. void CPacketsDisplayDlg::OnTimer(UINT nIDEvent)
  736. {
  737.     // TODO: Add your message handler code here and/or call default
  738.     static BYTE byTick=0;

  739.     if (nIDEvent==1)
  740.     {
  741.         time_t t=time(NULL);//把当前时间转换成秒
  742.         struct tm p=*gmtime(&t);//将秒转换成当前时间
  743.         ptp_date_time_t ptp;
  744.         ptp.year=p.tm_year;
  745.         ptp.mon=p.tm_mon;
  746.         ptp.day=p.tm_mday;
  747.         ptp.hour=p.tm_hour;
  748.         ptp.min=p.tm_min;
  749.         ptp.sec=p.tm_sec;
  750.         printf("date: %4d-%2d-%2d , time: %02d:%02d:%02d\r\n",ptp.year+1900,ptp.mon+1,ptp.day,ptp.hour+8,ptp.min,ptp.sec);
  751.         SendFolowUp(t,123);
  752.         
  753.         HWND hGreen=::GetDlgItem(m_hWnd,IDC_GREEN);

  754.         if (::IsWindowVisible(hGreen))
  755.         {
  756.             ::ShowWindow(hGreen,SW_HIDE);
  757.         }
  758.         else
  759.         {
  760.             ::ShowWindow(hGreen,SW_SHOW);
  761.         
  762.         }

  763.         
  764.         
  765.     }
  766.     if (nIDEvent==5)
  767.     {
  768.         byTick++;
  769.         if (byTick>127)
  770.         {
  771.             byTick=0;
  772.         }
  773.         memset((void*)g_pBuf,byTick,100);
  774.     }





  775.   CRect dlgRect;
  776.   GetWindowRect(dlgRect);
  777.   CRect desktopRect;
  778.   GetDesktopWindow()->GetWindowRect(desktopRect);
  779.   if(nIDEvent==2)
  780.   {
  781.     MoveWindow((-m_nDx+desktopRect.Width()-dlgRect.Width())/2,(-m_nDy+desktopRect.Height()-dlgRect.Height())/2,+m_nDx+dlgRect.Width(),+m_nDy+dlgRect.Height());
  782.     if(dlgRect.Width()>=m_nWidth)
  783.       m_nDx=0;
  784.     if(dlgRect.Height()>=m_nHeight)
  785.       m_nDy=0;
  786.     if((dlgRect.Width()>=m_nWidth)&&(dlgRect.Height()>=m_nHeight))
  787.       KillTimer(2);
  788.   }
  789.   if((dlgRect.Width()>=m_nWidth)&&(dlgRect.Height()>=m_nHeight))
  790.     KillTimer(2);
  791.   if(nIDEvent==3)
  792.   {
  793.     MoveWindow((+m_nDx+desktopRect.Width()-dlgRect.Width())/2,(+m_nDy+desktopRect.Height()-dlgRect.Height())/2,-m_nDx1+dlgRect.Width(),-m_nDy1+dlgRect.Height());
  794.     if(dlgRect.Width()<=0)
  795.         m_nDx1=0;
  796.     if(dlgRect.Height()<=0)
  797.         m_nDy1=0;
  798.     if((dlgRect.Width()<=0)&&(dlgRect.Height()<=0))
  799.     {
  800.         KillTimer(3);
  801.         CDialog::OnOK();
  802.     }
  803.   }



  804.     CDialog::OnTimer(nIDEvent);
  805. }

  806. void CPacketsDisplayDlg::OnCaptureChanged(CWnd *pWnd)
  807. {
  808.     // TODO: Add your message handler code here
  809.     
  810.     CDialog::OnCaptureChanged(pWnd);
  811. }

  812. void CPacketsDisplayDlg::InitStatusBar()
  813. {
  814.     HWND hDlg=GetSafeHwnd();
  815.     hStatusWindow=CreateStatusWindow(WS_CHILD|WS_VISIBLE|WS_BORDER,
  816.         TEXT("状态栏"),
  817.         hDlg,IDS_TOOLBAR);
  818.         int pint[4]={110,250,300,-1};//110,250,300设定间隔
  819.         ::SendMessage(hStatusWindow,SB_SETPARTS,4,(LPARAM)pint);
  820. }

  821. int CPacketsDisplayDlg::InitSharedMem()
  822. {
  823.     m_hMapFile=NULL;
  824.     m_pBaseMapFile=NULL;
  825.     return 0;
  826. }

  827. int CPacketsDisplayDlg::DestroySharedMem()
  828. {
  829.     if (m_hMapFile)
  830.     {
  831.         CloseHandle(m_hMapFile);
  832.         m_hMapFile=NULL;    
  833.     }
  834.     if (m_pBaseMapFile)
  835.     {
  836.         UnmapViewOfFile(m_pBaseMapFile);
  837.         m_pBaseMapFile=NULL;
  838.     }
  839.     return 0;
  840. }

  841. int CPacketsDisplayDlg::CreateSharedMem(int size)
  842. {
  843.     m_hMapFile=CreateFileMapping(
  844.         (HANDLE)0xffffffff,
  845.         NULL,
  846.         PAGE_READWRITE,
  847.         0,
  848.         size,
  849.         "fuck");

  850.     if (m_hMapFile)
  851.     {
  852.         m_pBaseMapFile=MapViewOfFile(
  853.             m_hMapFile,FILE_MAP_READ|FILE_MAP_WRITE,
  854.             0,
  855.             0,
  856.             0);

  857.         if (m_pBaseMapFile)
  858.         {
  859.             g_pBuf=(int *)m_pBaseMapFile;    
  860.         }
  861.     }
  862.     else
  863.     {
  864.         m_pBaseMapFile=NULL;
  865.     }
  866.     return 0;
  867. }

阅读(797) | 评论(0) | 转发(0) |
0

上一篇:NTGraph控件绘图

下一篇:CSerial串口类

给主人留下些什么吧!~~