⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 notifysend.cpp

📁 HP公司的SNMP++的Win32版本源码
💻 CPP
字号:
/*============================================================================
  Copyright (c) 1996
  Hewlett-Packard Company

  ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
  Permission to use, copy, modify, distribute and/or sell this software 
  and/or its documentation is hereby granted without fee. User agrees 
  to display the above copyright notice and this license notice in all 
  copies of the software and any documentation of the software. User 
  agrees to assume all liability for the use of the software; Hewlett-Packard 
  makes no representations about the suitability of this software for any 
  purpose. It is provided "AS-IS without warranty of any kind,either express 
  or implied. User hereby grants a royalty-free license to any and all 
  derivatives based upon this software code base. 

=============================================================================*/

#include "stdafx.h"
#include "browser.h"
#include "NotifySend.h"
#include "db_cls.h"
#include "tarfact.h"
#include "vbopts.h"
#include "tarfact.h"


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

/////////////////////////////////////////////////////////////////////////////
// NotifySend dialog


NotifySend::NotifySend(CWnd* pParent /*=NULL*/)
	: CDialog(NotifySend::IDD, pParent)
{
	//{{AFX_DATA_INIT(NotifySend)
	m_read_community = _T("");
	m_write_community = _T("");
	m_retries = _T("");
	m_timeouts = _T("");
	m_mgmt_proto = _T("");
	m_net_proto = _T("");
	m_oid = _T("");
	m_customid = _T("");
	m_enterprise = _T("");
	m_output = _T("");
	//}}AFX_DATA_INIT

	
	int cstatus = Create(IDD, pParent);

	// create a snmp++ object
	int status;
	snmp = new Snmp( status);
	if ( status != SNMP_CLASS_SUCCESS)
	{
		AfxMessageBox("Unable To Create Snmp Object!");
		snmp = NULL;
	}

    CheckRadioButton( IDC_RCOLDSTART, IDC_REGPLOSS,IDC_RCOLDSTART); 

	// load up the target list
    CComboBox * target_cb = ( CComboBox *) GetDlgItem( IDC_TARGETS);
    target_cb->ResetContent();

    // get the db file name from the ini file
    CString filename = theApp.GetProfileString( BROWSER_VALUE,DB_NAME,DEF_DB_NAME);

	// access the target database
    Db target_db;
	TargetDb_Rec db_rec;
    target_db.set_attributtes( filename, sizeof( TargetDb_Rec));
    int nr = target_db.get_num_recs();
	if ( nr==0)
		AfxMessageBox("You Have No Targets Defined\n Please Define a Target");



	// for all records, get and display
    for (int i=1;i<=nr;i++)
    {
       if ((status = target_db.retrieve((long int) (i-1), &db_rec))!= DB_OK)
       {
          MessageBox("Unable to Read Target DB Record", ERR_MSG,MB_ICONSTOP);
          return;
       }

	   CString target_name;
	   if ( strcmp( db_rec.alias,"") ==0)
         target_cb->AddString((char*)  db_rec.key);
	   else
	   {
		 target_name = db_rec.alias;
		 target_name += " @ ";
		 target_name += db_rec.key;
		 target_cb->AddString(target_name);
	   }

       if ( i==1)
       {
          target_cb->SetCurSel(0);
          load_target_attribs( db_rec);
       }
	}
}


// load the current target view
void NotifySend::load_target_attribs( TargetDb_Rec db_rec)
{
   char buffer[20];
   m_read_community = db_rec.read_community;
   m_write_community = db_rec.write_community;
   sprintf( buffer,"%d",db_rec.retries);
   m_retries = buffer;
   sprintf( buffer,"%d",db_rec.timeout);
   m_timeouts = buffer;
   m_mgmt_proto = (db_rec.snmp_type == SNMPV1) ? V1 :V2C;
   m_net_proto =  (db_rec.address_type == IP_TYPE) ? "IP" : "IPX";
   UpdateData( FALSE);
};


void NotifySend::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(NotifySend)
	DDX_Text(pDX, IDC_READ_COMMUNITY, m_read_community);
	DDX_Text(pDX, IDC_WRITE_COMMUNITY, m_write_community);
	DDX_Text(pDX, IDC_RETRIES, m_retries);
	DDX_Text(pDX, IDC_TIMEOUTS, m_timeouts);
	DDX_Text(pDX, IDC_MGMTPROTO, m_mgmt_proto);
	DDX_Text(pDX, IDC_NETPROTO, m_net_proto);
	DDX_Text(pDX, IDC_OID, m_oid);
	DDX_Text(pDX, IDC_CUSTOMID, m_customid);
	DDX_Text(pDX, IDC_ENTERPRISE, m_enterprise);
	DDX_Text(pDX, IDC_OUTPUT, m_output);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(NotifySend, CDialog)
	//{{AFX_MSG_MAP(NotifySend)
	ON_CBN_SELCHANGE(IDC_TARGETS, OnSelchangeTargets)
	ON_BN_CLICKED(IDC_ADDOID, OnAddoid)
	ON_BN_CLICKED(IDC_EDITPAY, OnEditpay)
	ON_BN_CLICKED(IDC_DELPAY, OnDelpay)
	ON_LBN_DBLCLK(IDC_PDU, OnDblclkPdu)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// NotifySend message handlers

BOOL NotifySend::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	// clear the pdu table
	CListBox *lb;
	lb= ( CListBox*) GetDlgItem( IDC_PDU);
	lb->ResetContent();	
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void NotifySend::OnSelchangeTargets() 
{
	// TODO: Add your control notification handler code here
		// access the target database
    Db target_db;
	TargetDb_Rec db_rec;
	char key[80];
	char *ptr,*address;
	int status;
 
    CComboBox * target_cb = ( CComboBox *) GetDlgItem( IDC_TARGETS);
	target_cb->GetLBText( target_cb->GetCurSel() , key);

	// trim off alias, if present
	address = key;
	ptr = key;
	while (*ptr != 0)
	{
		if ( *ptr == '@')
		   address = ptr+2;
	   ptr++;
	}


    // get the db file name from the ini file
    CString filename = theApp.GetProfileString( BROWSER_VALUE,DB_NAME,DEF_DB_NAME);
	target_db.set_attributtes( filename, sizeof( TargetDb_Rec));
    strcpy( db_rec.key,address);
    status = target_db.read( &db_rec);
    if ( status != DB_OK)
    {
       MessageBox( "Unable to Read Target Record",ERR_MSG,MB_ICONSTOP);
       return;
    }
    load_target_attribs( db_rec);


}

void NotifySend::OnAddoid() 
{
	// TODO: Add your control notification handler code here
	UpdateData( TRUE);
	Oid oid( m_oid);
	if ( oid.valid())
	{
	   VbOpts vb_options;
	   vb_options.m_vboid = oid.get_printable();
	   int status;
	   status = vb_options.DoModal();

	   if ( vb_options.my_vb.valid()) {
	      CListBox *lb;
	      lb = ( CListBox*) GetDlgItem( IDC_PDU);
	      CString item;
	      item += oid.get_printable();
          int spot = lb->InsertString( lb->GetCount(),item);
		  if ( spot >= MAX_TRAP_VBS)
			  AfxMessageBox("Exceeded Max trap Vbs!");
		  else
		     trap_vbs[spot] = vb_options.my_vb;
	   }
	}
	else
		AfxMessageBox("Invalid Custom MIB Object Identifier");
}

void NotifySend::OnEditpay() 
{
	// TODO: Add your control notification handler code here
	CListBox *lb;
	int selection;
	char item_data[100];
	lb = ( CListBox*) GetDlgItem( IDC_PDU);

	UpdateData( TRUE);

	// make sure we have an item selected
	if((selection = lb->GetCurSel()) == LB_ERR)
	{
		AfxMessageBox("You Must Select an Item to Edit");
	    return;
	}

	// get the item text
	if ( (lb->GetText( selection, item_data)) == LB_ERR)
    {
       AfxMessageBox("Can Not Obtain List Box Selection");
       return;
    }

	Oid addoid( item_data);
	VbOpts vb_options;
	vb_options.m_vboid = addoid.get_printable();
	vb_options.m_vbvalue =  trap_vbs[selection].get_printable_value();
	int status;
	status = vb_options.DoModal();
	
	if ( vb_options.my_vb.valid())
	{
	   CString scalar;
	   scalar += addoid.get_printable();
	   lb->DeleteString( selection);
	   lb->InsertString( selection,scalar);
	   trap_vbs[selection] =  vb_options.my_vb;
	}

}

void NotifySend::OnDelpay() 
{
	// TODO: Add your control notification handler code here
	CListBox *lb;
	int selection;
	lb = ( CListBox*) GetDlgItem( IDC_PDU);
	int count= lb->GetCount();

	// delete the item from the list box
	if((selection = lb->GetCurSel()) != LB_ERR)
		lb->DeleteString( selection);

	// relink the set vb list if needed
	for (int x=selection;x<(count-1);x++)
		trap_vbs[x] = trap_vbs[x+1];

}

void NotifySend::OnDblclkPdu() 
{
	// TODO: Add your control notification handler code here
	OnEditpay();
}

void NotifySend::OnOK() 
{
   // TODO: Add extra validation here
   Oid trapid;
   Pdu trap_pdu;
   char buffer[200];

   Oid coldStart("1.3.6.1.6.3.1.1.5.1");
   Oid warmStart("1.3.6.1.6.3.1.1.5.2");
   Oid linkUp("1.3.6.1.6.3.1.1.5.4");
   Oid linkDown("1.3.6.1.6.3.1.1.5.3");
   Oid authenticationFailure("1.3.6.1.6.3.1.1.5.5");
   Oid egpNeighborLoss("1.3.6.1.6.3.1.1.5.6");

   // update MFC variables
   UpdateData( TRUE);




   // determine the trap id
   int trap_type;
   trap_type = GetCheckedRadioButton( IDC_RCOLDSTART, IDC_RCUSTOM);

   switch( trap_type)
   {

      case IDC_RCOLDSTART:
      trapid = coldStart;
      break;

      case IDC_RWARMSTART:
      trapid = warmStart;
      break;

      case IDC_RLINKUP:
      trapid = linkUp;
      break;

      case IDC_RLINKDOWN:
      trapid = linkDown;
      break;

      case IDC_RAUTHFAIL:
      trapid = authenticationFailure;
      break;

      case IDC_REGPLOSS:
      trapid = egpNeighborLoss; 
      break;

      case IDC_RCUSTOM:
      {
		 strcpy( buffer, m_customid);
         Oid coid( buffer);
         if ( !coid.valid())
         {
            AfxMessageBox("Invalid Custom Oid");
            return;
         }
         trapid =  coid;
      }
   }

   // attach the id to the pdu
   trap_pdu.set_notify_id( trapid);

   // use the default timestamp!

   // determine the trap enterprise
   strcpy( buffer, m_enterprise);
   Oid enterprise_oid( buffer);
   trap_pdu.set_notify_enterprise( enterprise_oid);


   // make a SnmpTarget using the Target_Factory
   CComboBox *cb = ( CComboBox *) GetDlgItem( IDC_TARGETS);
   char key[80];
   if ( cb->GetCurSel() == CB_ERR)
   {
      AfxMessageBox("No Target Selected!");
      return;
   }
   cb->GetLBText( cb->GetCurSel(), key); 


   // get a target from the target factory
   SnmpTarget *target = target_factory( key);
   if ( target == NULL)
   {
   	 AfxMessageBox("Unable To find Target");
	 return;
   }

   // build up the payload
   CListBox *lb;
   lb = ( CListBox*) GetDlgItem( IDC_PDU);
   for (int x=0;x<lb->GetCount();x++)
	   trap_pdu += trap_vbs[x];

   // make sure we have at least one vb in the payload
   if ( trap_pdu.get_vb_count() == 0) {
	   AfxMessageBox("The Trap Payload Must have at least One Variable Binding");
	   delete target;
	   return;
   }


   // clear the output display
   m_output = "SNMP++ Trap Send In Progress...";
   UpdateData( FALSE);


   int status = snmp->trap( trap_pdu, *target);

   // display trap send status
   m_output = snmp->error_msg( status);
   UpdateData( FALSE);

   // look for the continous mode
   if ( status == SNMP_CLASS_SUCCESS)
   {
	  CButton * continue_mode = (CButton*) GetDlgItem( IDC_CONTINUOS);
	  if ( continue_mode->GetCheck())
	     SetTimer(100,500,NULL);
   }

   delete target;
}

void NotifySend::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	KillTimer( nIDEvent);
	OnOK();
}

void NotifySend::OnCancel() 
{
	// TODO: Add extra cleanup here
	DestroyWindow();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -