Trap receiver

// TrapReceiverDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TrapReceiver.h"
#include "TrapReceiverDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
 CAboutDlg();

// Dialog Data
 //{{AFX_DATA(CAboutDlg)
 enum { IDD = IDD_ABOUTBOX };
 //}}AFX_DATA

 // ClassWizard generated virtual function overrides
 //{{AFX_VIRTUAL(CAboutDlg)
 protected:
 virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
 //}}AFX_VIRTUAL

// Implementation
protected:
 //{{AFX_MSG(CAboutDlg)
 //}}AFX_MSG
 DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
 //{{AFX_DATA_INIT(CAboutDlg)
 //}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
 CDialog::DoDataExchange(pDX);
 //{{AFX_DATA_MAP(CAboutDlg)
 //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
 //{{AFX_MSG_MAP(CAboutDlg)
  // No message handlers
 //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/
// CTrapReceiverDlg dialog

CTrapReceiverDlg::CTrapReceiverDlg(CWnd* pParent /*=NULL*/)
 : CDialog(CTrapReceiverDlg::IDD, pParent)
{
 //{{AFX_DATA_INIT(CTrapReceiverDlg)
 //}}AFX_DATA_INIT
 // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
 m_bRecvTrap=false;
}

void CTrapReceiverDlg::DoDataExchange(CDataExchange* pDX)
{
 CDialog::DoDataExchange(pDX);
 //{{AFX_DATA_MAP(CTrapReceiverDlg)
 DDX_Control(pDX, IDC_EDIT_PORT, m_edit_port);
 DDX_Control(pDX, IDC_EDIT_OID, m_edit_oid);
 DDX_Control(pDX, IDC_IPADDRESS2, m_host_ip);
 DDX_Control(pDX, IDC_EDIT_EVENT, m_edit_event);
 DDX_Control(pDX, IDC_EDIT1, m_edit);
 //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTrapReceiverDlg, CDialog)
 //{{AFX_MSG_MAP(CTrapReceiverDlg)
 ON_WM_SYSCOMMAND()
 ON_WM_PAINT()
 ON_WM_QUERYDRAGICON()
 ON_BN_CLICKED(IDTRAP, OnTrap)
 ON_BN_CLICKED(IDC_SEND_TRAP, OnSendTrap)
 ON_BN_CLICKED(IDC_V1, OnV1)
 ON_BN_CLICKED(IDC_V2, OnV2)
 //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/
// CTrapReceiverDlg message handlers

BOOL CTrapReceiverDlg::OnInitDialog()
{
 CDialog::OnInitDialog();

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

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

 CMenu* pSysMenu = GetSystemMenu(FALSE);
 if (pSysMenu != NULL)
 {
  CString strAboutMenu;
  strAboutMenu.LoadString(IDS_ABOUTBOX);
  if (!strAboutMenu.IsEmpty())
  {
   pSysMenu->AppendMenu(MF_SEPARATOR);
   pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  }
 }
 this->SetWindowText("TrapReceiver                                 QQ:40700167  浪");

 // Set the icon for this dialog.  The framework does this automatically
 //  when the application's main window is not a dialog
 SetIcon(m_hIcon, TRUE);   // Set big icon
 SetIcon(m_hIcon, FALSE);
 
 m_host_ip.SetWindowText("127.0.0.1");
 m_edit_oid.SetWindowText("1.3.6.1.6.3.1.1.5.3");
 m_edit_event.SetWindowText("Link down");
 m_edit_port.SetWindowText("162");
 ((CButton *)GetDlgItem(IDC_V1))->SetCheck(TRUE);//选上
 ((CButton *)GetDlgItem(IDC_V2))->SetCheck(FALSE);//选上
 


 OnTrap();


 
 // TODO: Add extra initialization here
 
 return TRUE;  // return TRUE  unless you set the focus to a control
}

void CTrapReceiverDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
 if ((nID & 0xFFF0) == IDM_ABOUTBOX)
 {
  CAboutDlg dlgAbout;
  dlgAbout.DoModal();
 }
 else
 {
  CDialog::OnSysCommand(nID, lParam);
 }
}

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

void CTrapReceiverDlg::OnPaint()
{
 if (IsIconic())
 {
  CPaintDC dc(this); // device context for painting

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

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

  // Draw the icon
  dc.DrawIcon(x, y, m_hIcon);
 }
 else
 {
  CDialog::OnPaint();
 }
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CTrapReceiverDlg::OnQueryDragIcon()
{
 return (HCURSOR) m_hIcon;
}

void CTrapReceiverDlg::OnTrap()
{
 // TODO: Add your control notification handler code here
 CButton *pBt;
 
 pBt=(CButton *)GetDlgItem(IDTRAP);
 
 if(m_bRecvTrap==false)
 {
  GetDlgItem(IDC_SEND_TRAP)->EnableWindow(false);;
 
  
 /*   if(pSnmp.sessionID==FALSE) 
 
   {
  
  pSnmp.CreateSession(CSnmpMgrDlg::m_hWnd,wMsg);
  
    pSnmp.sessionID=TRUE;
   
   }    
  */
  m_bRecvTrap=true;
  
  AfxBeginThread(WorkerThreadProc,this,THREAD_PRIORITY_NORMAL,0,0,NULL);           ///创建线程, 线程函数为WorkerThreadProc 参数为this指针。
  
  pBt->SetWindowText("停止接收TRAP");              //修改按钮文字
  
 }else{
  
  m_bRecvTrap=false;
  
  GetDlgItem(IDC_SEND_TRAP)->EnableWindow(true);;
 

  Snmp::socket_startup(); 
  
  UdpAddress address("127.0.0.1:162");     
  
  int status;
  
  Snmp snmp(status, 0) ;
  
  Pdu pdu;                              
  
  pdu.set_notify_id("1.3.6.1");              
  
  pdu.set_notify_enterprise( "atesta");
  
  CTarget ctarget(address);            
  
  ctarget.set_version( version1);       
  
        ctarget.set_readcommunity("public");
       
        SnmpTarget *target=&ctarget;
       
        status = snmp.trap(pdu,*target);        //向本机发送Trap以标志退出监听。
       
        Snmp::socket_cleanup();
       
        pBt->SetWindowText("开始接收TRAP");
  
 }
}


UINT CTrapReceiverDlg::WorkerThreadProc(LPVOID Param)
{
 

 CTrapReceiverDlg *pCV;
 
 pCV=(CTrapReceiverDlg *)Param; //将指针类型转换为CSnmpMgrDlg *类型指针;
 
    int trap_port=162;      //指定监听端口号;
 
 OidCollection oidc;
 
 TargetCollection targetc;   //创建过滤TRAP的对象;
 
 // CNotifyEventQueue::set_listen_port(trap_port);     //设置端口号;
 
 int status;
 
 Snmp::socket_startup(); 
 
 Snmp *snmp = new Snmp(status);       // 创建Snmp对象;
 //Snmp snmp(status);
 
 if ( status != SNMP_CLASS_SUCCESS)
 {
  AfxMessageBox("出现错误!");
  return TRUE;
 }
 snmp->notify_set_listen_port(trap_port);
 status=snmp->notify_register(oidc, targetc,callme,Param);
 
 
 if (status != SNMP_CLASS_SUCCESS)
 {
  AfxMessageBox("出现错误!");
  return TRUE;
 }
 
 pCV->m_edit.ReplaceSel("开始接收/r/n");
 while(pCV->m_bRecvTrap)
 {
  snmp->eventListHolder->SNMPProcessEvents();//进入SNMPProcessEvents()函数,等待TRAP触发回调用函数;
  Sleep(1000);
 }
 Snmp::socket_cleanup(); 
 pCV->m_bRecvTrap=false;
 //AfxMessageBox("已停止!");
 pCV->m_edit.ReplaceSel("已停止接收/r/n");
 
 return TRUE;
 
}


//回调函数,用来处理接收到的trap

void CTrapReceiverDlg::callme(int reason, Snmp *snmp, Pdu &pdu, SnmpTarget &target, void *cd)

 
 
 CTrapReceiverDlg *p;
 p=(CTrapReceiverDlg *)cd;
 
 CString str="";
 
 CTime time=CTime::GetCurrentTime();
 
 CString time_str=time.Format("%Y-%m-%d:%H:%M:%S");  //计算接收的时间
 
 Vb nextVb;
 int pdu_error;
 GenAddress addr;
 
 target.get_address(addr);
 UdpAddress from(addr);
 
 /* cout << "reason: " << reason << endl
 << "msg: " << snmp->error_msg(reason) << endl
 << "from: " << from.get_printable() << endl;
 */
 str.Format("reason:%d,msg:%s,from:%s",reason,snmp->error_msg(reason),from.get_printable());
 TRACE(str);
 p->m_edit.ReplaceSel(str+"/r/n");
 
 pdu_error = pdu.get_error_status();
 if (pdu_error){
  // cout << "Response contains error: "
  //  << snmp->error_msg(pdu_error)<< endl;
  str.Format("Response contains error:%s/n/r",snmp->error_msg(pdu_error));
  TRACE(str); 
  p->m_edit.ReplaceSel(str+"/r/n");
 }
 
 Oid id;
 pdu.get_notify_id(id);
 //cout << "ID:  " << id.get_printable() << endl;
 // cout << "Type:" << pdu.get_type() << endl;
 str.Format("ID:%s/n/r",id.get_printable());
 TRACE(str);
 p->m_edit.ReplaceSel(str+"/r/n");
 
 str.Format("Type%d/n/r",pdu.get_type());
 TRACE(str);
 p->m_edit.ReplaceSel(str+"/r/n");
 
 
 for (int i=0; i<pdu.get_vb_count(); i++)
 {
  pdu.get_vb(nextVb, i);
  
  //cout << "Oid: " << nextVb.get_printable_oid() << endl
  // << "Val: " <<  nextVb.get_printable_value() << endl;
  str.Format("Oid:%s/n/r",nextVb.get_printable_oid());
  TRACE(str);
  p->m_edit.ReplaceSel(str+"/r/n");
  
  str.Format("Val:%s/n/r",nextVb.get_printable_value());
  TRACE(str);
  p->m_edit.ReplaceSel(str+"/r/n");
  
  
  
 }
 if (pdu.get_type() == sNMP_PDU_INFORM) {
  //cout << "pdu type: " << pdu.get_type() << endl;
  //cout << "sending response to inform: " << endl;
  
  str.Format("pdu type:%d/n/r",pdu.get_type());
  TRACE(str);
  p->m_edit.ReplaceSel(str+"/r/n");
  
  str.Format("sending response to inform/n/r");
  TRACE(str);
  p->m_edit.ReplaceSel(str+"/r/n");
  
  
  nextVb.set_value("This is the response.");
  pdu.set_vb(nextVb, 0);
  snmp->response(pdu, target);
 }
 
 int iLineNum=p->m_edit.GetLineCount();
 if(iLineNum<=100)
 {
  // p->m_edit.SetSel(-1, -1);
  // p->m_edit.ReplaceSel(strDate+" "+strTime+" "+"----"+str+"/r/n/r/n");
 }else{
  p->m_edit.SetSel(0, -1);
  p->m_edit.Clear();
 }
 
}


void CTrapReceiverDlg::OnCancel()
{
 // TODO: Add extra cleanup here
 m_bRecvTrap=false;
 CDialog::OnCancel();
}

void CTrapReceiverDlg::OnSendTrap()
{
 // TODO: Add your control notification handler code here
 CString Ipstr,Port,addr,Oidstr,EntPrs;

 m_host_ip.GetWindowText(Ipstr);
 m_edit_port.GetWindowText(Port);
 m_edit_oid.GetWindowText(Oidstr);
 m_edit_event.GetWindowText(EntPrs);

 addr.Format("%s:%s",Ipstr,Port);

 UdpAddress address(addr);     
 
 int status;
 
 Snmp snmp(status, 0) ;
 
 Pdu pdu;                              
 Oid oidc(Oidstr);
 pdu.set_notify_id(oidc);              
 
 pdu.set_notify_enterprise(oidc);
 
 CTarget ctarget(address);            
 
 if(versel==1)
  ctarget.set_version( version1);
 else
  ctarget.set_version( version2c);

 ctarget.set_readcommunity("public");
 
 SnmpTarget *target=&ctarget;
 
 status = snmp.trap(pdu,*target);        //向本机发送Trap以标志退出监听。
 
 Snmp::socket_cleanup();
 
}

void CTrapReceiverDlg::OnV1()
{
 // TODO: Add your control notification handler code here
 versel=1;
}

void CTrapReceiverDlg::OnV2()
{
 // TODO: Add your control notification handler code here
 versel=2;
 
}

 

 

==============================

// TrapReceiverDlg.h : header file
//

#if !defined(AFX_TRAPRECEIVERDLG_H__25D965B8_4C10_404B_8CBA_74F37A57AFEA__INCLUDED_)
#define AFX_TRAPRECEIVERDLG_H__25D965B8_4C10_404B_8CBA_74F37A57AFEA__INCLUDED_

#include "snmp_pp.h"
#include "snmp_pp/collect.h"
#include "snmp_pp/notifyqueue.h"

//#include <stdlib.h>
//#include <stdio.h>
//#include   "stdafx.h" 


/*
#ifdef SNMP_PP_NAMESPACE
using namespace Snmp_pp;
#endif

#if (__GNUC__ > 2)
#include <iostream>
using std::cerr;
using std::cout;
using std::endl;
using std::flush;
#else
#include <iostream.h>
#endif
*/
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

/
// CTrapReceiverDlg dialog

class CTrapReceiverDlg : public CDialog
{
// Construction
public:


// Snmp snmp;

// Dialog Data
 //{{AFX_DATA(CTrapReceiverDlg)
 enum { IDD = IDD_TRAPRECEIVER_DIALOG };
 CEdit m_edit_port;
 CEdit m_edit_oid;
 CIPAddressCtrl m_host_ip;
 CEdit m_edit_event;
 CEdit m_edit;
 //}}AFX_DATA

 // ClassWizard generated virtual function overrides
 //{{AFX_VIRTUAL(CTrapReceiverDlg)
 protected:
 virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
 //}}AFX_VIRTUAL

// Implementation
protected:
 HICON m_hIcon;

 // Generated message map functions
 //{{AFX_MSG(CTrapReceiverDlg)
 virtual BOOL OnInitDialog();
 afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
 afx_msg void OnPaint();
 afx_msg HCURSOR OnQueryDragIcon();
 afx_msg void OnTrap();
 virtual void OnCancel();
 afx_msg void OnSendTrap();
 afx_msg void OnV1();
 afx_msg void OnV2();
 //}}AFX_MSG
 DECLARE_MESSAGE_MAP()

public:
 int versel;
 static void callme(int reason, Snmp *snmp, Pdu &pdu, SnmpTarget &target, void *cd);
 bool m_bRecvTrap;
 CTrapReceiverDlg(CWnd* pParent = NULL); // standard constructor
 static UINT WorkerThreadProc(LPVOID Param);
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_TRAPRECEIVERDLG_H__25D965B8_4C10_404B_8CBA_74F37A57AFEA__INCLUDED_)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值