GetNext.cpp
资源名称:hp_snmp3.zip [点击查看]
上传用户:czjinwang
上传日期:2007-01-12
资源大小:2484k
文件大小:16k
源码类别:
SNMP编程
开发平台:
Visual C++
- /*============================================================================
- 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 "GetNext.h"
- #include "db_cls.h"
- #include "tarfact.h"
- #include "fstream.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // GetNext dialog
- GetNext::GetNext(CWnd* pParent /*=NULL*/)
- : CDialog(GetNext::IDD, pParent)
- {
- //{{AFX_DATA_INIT(GetNext)
- m_read_community = _T("");
- m_write_community = _T("");
- m_retries = _T("");
- m_timeouts = _T("");
- m_mgmt_proto = _T("");
- m_net_proto = _T("");
- m_output = _T("");
- m_oid = _T("");
- m_outfile = _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;
- }
- // 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 Definedn 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 GetNext::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 GetNext::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(GetNext)
- 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_OUTPUT, m_output);
- DDV_MaxChars(pDX, m_output, 3000);
- DDX_Text(pDX, IDC_OID, m_oid);
- DDV_MaxChars(pDX, m_oid, 200);
- DDX_Text(pDX, IDC_OUTFILE, m_outfile);
- DDV_MaxChars(pDX, m_outfile, 12);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(GetNext, CDialog)
- //{{AFX_MSG_MAP(GetNext)
- ON_WM_DESTROY()
- ON_NOTIFY(NM_DBLCLK, IDC_MIBTREE, OnDblclkMibtree)
- ON_BN_CLICKED(IDC_ADD, OnAdd)
- ON_BN_CLICKED(IDGETNEXT, OnGetnext)
- ON_BN_CLICKED(IDC_REMOVE, OnRemove)
- ON_BN_CLICKED(IDC_ADDOID, OnAddoid)
- ON_BN_CLICKED(IDC_STOP, OnStop)
- ON_CBN_SELCHANGE(IDC_TARGETS, OnSelchangeTargets)
- ON_BN_CLICKED(IDC_FILEOUT, OnFileout)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // GetNext message handlers
- void GetNext::OnCancel()
- {
- // TODO: Add extra cleanup here
- DestroyWindow();
- }
- // enable the controls
- void GetNext::enable_controls( int state)
- {
- CWnd *control;
- control = GetDlgItem( IDC_TARGETS);
- control->EnableWindow( state ? TRUE : FALSE);
- control = GetDlgItem( IDC_MIBTREE);
- control->EnableWindow( state ? TRUE : FALSE);
- control = GetDlgItem( IDC_ADD);
- control->EnableWindow( state ? TRUE : FALSE);
- control = GetDlgItem( IDC_REMOVE);
- control->EnableWindow( state ? TRUE : FALSE);
- control = GetDlgItem( IDC_PDU);
- control->EnableWindow( state ? TRUE : FALSE);
- control = GetDlgItem( IDC_OID);
- control->EnableWindow( state ? TRUE : FALSE);
- control = GetDlgItem( IDC_ADDOID);
- control->EnableWindow( state ? TRUE : FALSE);
- control = GetDlgItem( IDC_ALL_DETAILS);
- control->EnableWindow( state ? TRUE : FALSE);
- control = GetDlgItem( IDC_AUTOWALK);
- control->EnableWindow( state ? TRUE : FALSE);
- control = GetDlgItem( IDC_FILEOUT);
- control->EnableWindow( state ? TRUE : FALSE);
- };
- //====================================================================
- // SNMP++ callback function for async events
- // this callback is used for all async requests
- void my_getnextcallback( int reason,
- Snmp* snmp,
- Pdu &pdu,
- SnmpTarget &target,
- void * callback_data)
- {
- // resolve the callback data to a MFC Get Window Handle
- GetNext * getnext_window = (GetNext *) callback_data;
- CButton * mib_walk = (CButton*) getnext_window->GetDlgItem( IDC_AUTOWALK);
- // based on the reason handle the reason
- switch ( reason)
- {
- // an asynchronous response has arrived
- case SNMP_CLASS_ASYNC_RESPONSE:
- getnext_window->display_response( pdu, target, snmp);
- // look for continuos mode
- if (( mib_walk->GetCheck()) &&(pdu.get_error_status()==0))
- getnext_window->getnext_another();
- else
- getnext_window->enable_controls( TRUE);
- break;
- // a timeout has occured
- case SNMP_CLASS_TIMEOUT:
- getnext_window->display_timeout( target);
- getnext_window->enable_controls( TRUE);
- break;
- } // end switch
- };
- // display the response value
- void GetNext::display_response( Pdu &pdu, SnmpTarget &target, Snmp *snmp)
- {
- GenAddress address; // SNMP++ generic Address
- char buffer[150];
- Vb vb;
- int error_status = pdu.get_error_status();
- int fileio = FALSE;
- // determine if an output file is specified
- // if so open up the file and write to it
- ofstream fo(m_outfile,010);
- // show the result status
- m_output = "Status = ";
- m_output += snmp->error_msg( error_status);
- m_output += "rn";
- // show the details if check box is selected
- CButton * all_details = (CButton*) GetDlgItem( IDC_ALL_DETAILS);
- if ( all_details->GetCheck())
- {
- target.get_address( address);
- m_output += "Response from ";
- m_output += address.get_printable();
- m_output += "rn";
- sprintf( buffer,"Error Status = %d, %srn",pdu.get_error_status(), snmp->error_msg(pdu.get_error_status()));
- m_output += buffer;
- sprintf( buffer,"Error Index = %drn", pdu.get_error_index());
- m_output += buffer;
- sprintf( buffer,"Request ID = %ldrn",pdu.get_request_id());
- m_output += buffer;
- sprintf( buffer,"Variable Binding Count = %drn",pdu.get_vb_count());
- m_output += buffer;
- }
- // clear the pdu selection box
- CListBox *lb;
- lb = ( CListBox*) GetDlgItem( IDC_PDU);
- lb->ResetContent();
- // show the Vbs
- for ( int x=0;x<pdu.get_vb_count();x++)
- {
- pdu.get_vb( vb,x);
- sprintf( buffer,"Vb #%drn",x+1);
- m_output += buffer;
- m_output += " Oid = ";
- m_output += vb.get_printable_oid();
- m_output += "rn";
- m_output += " Value = ";
- m_output += vb.get_printable_value();
- m_output += "rn";
- // update each vb in the Pdu selection box
- CString nextoid;
- nextoid = "next ";
- nextoid += vb.get_printable_oid();
- lb->InsertString( lb->GetCount(),nextoid);
- }
- // update the output display
- UpdateData( FALSE);
- if ( fo)
- fo << m_output;
- // close the file if it was opened
- if ( fileio )
- fo.close();
- };
- // display a timeout message
- void GetNext::display_timeout( SnmpTarget &target)
- {
- m_output = "";
- UpdateData( FALSE);
- m_output = "Request Timed Out";
- UpdateData( FALSE);
- };
- // load up the Ctree control with the MIB table
- void GetNext::load_mib_view( CTreeCtrl *tree)
- {
- CString name;
- Oid current_oid, parent_oid, next_oid,prev_parent_oid,*oid_ptr;
- HTREEITEM new_node, parent_node, previous_parent;
- parent_node = previous_parent = TVI_ROOT;
- for (int x=0;x<MAXMIBVALS; x++)
- {
- name = MIBVals[x][0];
- current_oid = MIBVals[x][2];
- next_oid = ((x+1)==MAXMIBVALS)?"":MIBVals[x+1][2];
- if ( current_oid.len() <= parent_oid.len())
- {
- parent_node = previous_parent;
- parent_oid = prev_parent_oid;
- }
- // add the node
- new_node = tree->InsertItem( name,parent_node);
- oid_ptr = new Oid( current_oid);
- if ( strcmp(MIBVals[x][1],SCALAR)==0)
- (*oid_ptr)+="0";
- tree->SetItemData( new_node, (DWORD) oid_ptr);
- if ( name == "sysDescr")
- tree->SelectItem( new_node);
- if (( current_oid.len() > parent_oid.len()) && ( current_oid.len() < next_oid.len()))
- {
- previous_parent = parent_node;
- parent_node = new_node;
- prev_parent_oid = parent_oid;
- parent_oid = current_oid;
- }
- };
- };
- BOOL GetNext::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // TODO: Add extra initialization here
- CenterWindow();
- // load up the MIB tree control
- CTreeCtrl *tree;
- tree = (CTreeCtrl *) GetDlgItem( IDC_MIBTREE);
- load_mib_view( tree);
- // 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
- }
- // recursive delete of CtreeCtrl Items
- void GetNext::delete_nodes( CTreeCtrl *tree, HTREEITEM node)
- {
- HTREEITEM next_node;
- Oid * oid_ptr;
- next_node = tree->GetChildItem( node);
- if ( next_node != NULL)
- delete_nodes( tree, next_node);
- next_node = tree->GetNextSiblingItem( node);
- if ( next_node != NULL)
- delete_nodes( tree, next_node);
- oid_ptr = (Oid *) tree->GetItemData( node);
- if ( oid_ptr != NULL)
- delete oid_ptr;
- }
- void GetNext::OnDestroy()
- {
- CDialog::OnDestroy();
- // TODO: Add your message handler code here
- // need to free up CtreeCtrl's Oids
- CTreeCtrl *tree;
- tree = (CTreeCtrl *) GetDlgItem( IDC_MIBTREE);
- delete_nodes( tree, tree->GetRootItem());
- }
- void GetNext::OnDblclkMibtree(NMHDR* pNMHDR, LRESULT* pResult)
- {
- // TODO: Add your control notification handler code here
- OnAdd();
- *pResult = 0;
- }
- void GetNext::OnAdd()
- {
- // TODO: Add your control notification handler code here
- CTreeCtrl *tree;
- HTREEITEM node;
- Oid *oid_ptr;
- tree = (CTreeCtrl *) GetDlgItem( IDC_MIBTREE);
- node = tree->GetSelectedItem();
- oid_ptr = (Oid *) tree->GetItemData( node);
- Oid addoid( oid_ptr->get_printable());
- CListBox *lb;
- lb = ( CListBox*) GetDlgItem( IDC_PDU);
- CString scalar;
- scalar = tree->GetItemText( node);
- scalar += " ";
- scalar += addoid.get_printable();
- lb->InsertString( lb->GetCount(),scalar);
- }
- void GetNext::OnGetnext()
- {
- // TODO: Add your control notification handler code here
- // TODO: Add your control notification handler code here
- //------------------------------------------------------
- // Here is the real SNMP++ code !
- //
- // Algorithm:
- // - build a Pdu using the Oids from the Pdu list box
- // - create a CTarget
- // - response will come to specified callback
- CListBox *lb;
- int count;
- char buffer[80],*ptr;
- Pdu pdu; // SNMP++ Pdu object
- UpdateData( TRUE);
- // get the ListBox id
- lb = ( CListBox*) GetDlgItem( IDC_PDU);
- // make sure we have something to get
- if ( (count=lb->GetCount()) <1)
- {
- AfxMessageBox("There are No PDU Variables to GetNext");
- return;
- }
- // build up a SNMP++ Pdu with all the Vbs requested
- for ( int z =0;z<count;z++)
- {
- // get the text from the listbox
- lb->GetText(z,buffer);
- // get rid of the text part
- ptr = buffer;
- while (*ptr != ' ') ptr++;
- ptr++;
- // make a SNMP++ Oid with the dotted notation
- Oid oid( ptr);
- // make a SNMP++ Vb with the oid
- Vb vb( oid);
- // add the vb to the Pdu
- pdu += vb;
- }
- // 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;
- }
- // Invoke a SNMP++ Get for the Pdu requested
- // from the target created
- int status = snmp->get_next( pdu,
- *target,
- (snmp_callback) &my_getnextcallback,
- (void *) this);
- if ( status != SNMP_CLASS_SUCCESS)
- AfxMessageBox( snmp->error_msg( status));
- m_output = "SNMP++ GetNext In Progress...";
- UpdateData( FALSE);
- // free up the target when complete
- delete target;
- // disable all controls while get is pending
- enable_controls( FALSE);
- }
- void GetNext::OnRemove()
- {
- // TODO: Add your control notification handler code here
- CListBox *lb;
- int selection;
- lb = ( CListBox*) GetDlgItem( IDC_PDU);
- if((selection = lb->GetCurSel()) != LB_ERR)
- lb->DeleteString( selection);
- }
- void GetNext::OnAddoid()
- {
- // TODO: Add your control notification handler code here
- UpdateData( TRUE);
- Oid oid( m_oid);
- if ( oid.valid())
- {
- CListBox *lb;
- lb = ( CListBox*) GetDlgItem( IDC_PDU);
- CString item;
- item = "Custom ";
- item += oid.get_printable();
- lb->InsertString( lb->GetCount(),item);
- }
- else
- AfxMessageBox("Invalid Custom MIB Object Identifier");
- }
- void GetNext::getnext_another()
- {
- OnGetnext();
- }
- void GetNext::OnStop()
- {
- // TODO: Add your control notification handler code here
- CButton * mib_walk = (CButton*) GetDlgItem( IDC_AUTOWALK);
- mib_walk->SetCheck(0);
- }
- void GetNext::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 GetNext::OnFileout()
- {
- CFileDialog io(FALSE);
- if ( io.DoModal() == IDOK) {
- m_outfile = io.GetFileName();
- UpdateData( FALSE);
- }
- }