www.pudn.com > snmp.rar > snmp.c
#include "stdafx.h" ///////////////////////////////////////////////////////////////////////////// #include "..\include\ASocketConstants.h" #include "..\include\ASocket_i.c" #include "..\include\ASocket.h" ///////////////////////////////////////////////////////////////////////////// #include "ActiveSocket Demo.h" #include "DlgSnmpGet.h" ///////////////////////////////////////////////////////////////////////////// #include///////////////////////////////////////////////////////////////////////////// #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// CDlgSnmpGet::CDlgSnmpGet(CWnd* pParent) : CDialog(CDlgSnmpGet::IDD, pParent) { CHAR szComputerName[ 255 + 1 ] = { 0 }; DWORD dwSize = 255; GetComputerName( szComputerName, &dwSize ); //{{AFX_DATA_INIT(CDlgSnmpGet) m_strGetOID = _T("system.sysName.0"); m_strSessionAgent = _T(szComputerName ); m_strSessionCommunity = _T("public"); m_strGetValue = _T(""); m_strGetDataType = _T(""); m_strSetValue = _T(""); m_strTranslationMode = _T("SNMP v2c"); m_lPort = 161; m_strLogFile = _T(""); m_strResult = _T(""); //}}AFX_DATA_INIT m_pManager = NULL; m_bSessionOpened = FALSE; m_lDisplayMode = 0L; // ASCII by default } ///////////////////////////////////////////////////////////////////////////// CDlgSnmpGet::~CDlgSnmpGet() { if ( m_pManager ) { m_pManager->Shutdown (); m_pManager->Release (); m_pManager = NULL; } m_bSessionOpened = FALSE; } ///////////////////////////////////////////////////////////////////////////// void CDlgSnmpGet::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CDlgSnmpGet) DDX_Text(pDX, IDC_EDIT_GETOID, m_strGetOID); DDX_Text(pDX, IDC_EDIT_SESSIONAGENT, m_strSessionAgent); DDX_Text(pDX, IDC_EDIT_SESSIONCOMMUNITY, m_strSessionCommunity); DDX_Text(pDX, IDC_EDIT_GETVALUE, m_strGetValue); DDX_Text(pDX, IDC_EDIT_GETDATATYPE, m_strGetDataType); DDX_Text(pDX, IDC_EDIT_SETVALUE, m_strSetValue); DDX_CBString(pDX, IDC_COMBO_TRANSLATIONMODE, m_strTranslationMode); DDX_Text(pDX, IDC_EDIT_SESSIONPORT, m_lPort); DDX_Text(pDX, IDC_EDIT_LOGFILE, m_strLogFile); DDX_Text(pDX, IDC_EDIT_RESULT, m_strResult); //}}AFX_DATA_MAP } ///////////////////////////////////////////////////////////////////////////// BEGIN_MESSAGE_MAP(CDlgSnmpGet, CDialog) //{{AFX_MSG_MAP(CDlgSnmpGet) ON_BN_CLICKED(IDC_BUTTON_GET, OnButtonGet) ON_BN_CLICKED(IDC_BUTTON_SESSIONOPEN, OnButtonSessionopen) ON_BN_CLICKED(IDC_BUTTON_SESSIONCLOSE, OnButtonSessionclose) ON_BN_CLICKED(IDC_BUTTON_GETNEXT, OnButtonGetnext) ON_BN_CLICKED(IDC_BUTTON_SET, OnButtonSet) ON_BN_CLICKED(IDC_BUTTON_VIEW, OnButtonView) ON_BN_CLICKED(IDC_RADIO1, OnRadio1) ON_BN_CLICKED(IDC_RADIO2, OnRadio2) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// BOOL CDlgSnmpGet::OnInitDialog() { CDialog::OnInitDialog(); TCHAR szTempPath [ MAX_PATH + 1 ]; CoInitialize( NULL ); HRESULT hr = CoCreateInstance(CLSID_SnmpManager, NULL, CLSCTX_INPROC_SERVER, IID_ISnmpManager, (void**) &m_pManager); if( ! SUCCEEDED( hr ) ) { m_pManager = NULL; m_strResult = "Unable to load the ActiveSocket:SnmpManager object"; UpdateData( FALSE ); AfxMessageBox( m_strResult, MB_OK | MB_ICONERROR ); return TRUE; } GetTempPath( MAX_PATH - 14, szTempPath ); if( szTempPath[ 0 ] == '\0' || szTempPath[ lstrlen( szTempPath ) - 1 ] != '\\' ) _tcscat( szTempPath, _T("\\") ); m_strLogFile = szTempPath; m_strLogFile += "SnmpLog.txt"; UpdateData ( FALSE ); m_pManager->put_LogFile ( _bstr_t ( ( LPCSTR ) m_strLogFile ) ); m_pManager->Initialize(); ( ( CButton* ) GetDlgItem ( IDC_RADIO1 ) )->EnableWindow ( IsString () ); ( ( CButton* ) GetDlgItem ( IDC_RADIO1 ) )->SetCheck ( BST_CHECKED ); ( ( CButton* ) GetDlgItem ( IDC_RADIO2 ) )->EnableWindow ( IsString () ); UpdateControls(); return TRUE; } ///////////////////////////////////////////////////////////////////////////// void CDlgSnmpGet::OnButtonSessionopen() { LONG lLastError; UpdateData( TRUE ); if( m_pManager == NULL ) return; m_pManager->put_LogFile ( _bstr_t ( ( LPCSTR ) m_strLogFile ) ); if( m_strTranslationMode == "SNMP v2c" ) m_pManager->put_ProtocolVersion( asSNMP_VERSION_V2C ); else m_pManager->put_ProtocolVersion( asSNMP_VERSION_V1 ); AfxGetApp( )->DoWaitCursor( 1 ); // turn on the wait cursor m_pManager->Open( _bstr_t( ( LPCTSTR ) m_strSessionAgent ), _bstr_t( ( LPCTSTR ) m_strSessionCommunity ), m_lPort ); AfxGetApp( )->DoWaitCursor( 0 ); // turn off the wait cursor m_pManager->get_LastError( &lLastError ); UpdateResult( lLastError ); if( lLastError == 0 ) m_bSessionOpened = TRUE; UpdateControls(); } ///////////////////////////////////////////////////////////////////////////// void CDlgSnmpGet::OnButtonSessionclose() { UpdateData( TRUE ); if( m_pManager == NULL ) return; m_pManager->Close(); UpdateResult( 0L ); // Close is always successfull m_bSessionOpened = FALSE; UpdateControls(); } ///////////////////////////////////////////////////////////////////////////// void CDlgSnmpGet::OnButtonGet() { LONG lLastError = 0L; LONG lDataType = 0L; BSTR bstrValue = NULL; BSTR bstrOID = NULL; VARIANT vtVar; VariantInit ( &vtVar ); if( m_pManager == NULL ) return; UpdateData( TRUE ); AfxGetApp( )->DoWaitCursor( 1 ); // turn on the wait cursor m_pManager->Get( _bstr_t( ( LPCTSTR ) m_strGetOID ), &vtVar ); AfxGetApp( )->DoWaitCursor( 0 ); // turn off the wait cursor m_pManager->get_LastError( &lLastError ); UpdateResult( lLastError ); if( lLastError == 0L ) { if ( vtVar.vt == VT_DISPATCH ) { ISnmpObject * pSnmpObject = ( ISnmpObject * ) vtVar.pdispVal; if ( pSnmpObject ) { pSnmpObject->get_Type( &lDataType ); m_strGetDataType = GetDataType( lDataType ); m_lCurrentType = lDataType; pSnmpObject->get_Value ( &bstrValue ); DisplayValue ( bstrValue ); SysFreeString( bstrValue ); pSnmpObject->get_OID ( &bstrOID ); m_strGetOID = bstrOID; SysFreeString ( bstrOID ); VariantClear ( &vtVar ); } } ( ( CWnd* ) GetDlgItem ( IDC_RADIO1 ) )->EnableWindow ( IsString () ); ( ( CWnd* ) GetDlgItem ( IDC_RADIO2 ) )->EnableWindow ( IsString () ); } UpdateData( FALSE ); UpdateControls(); } ///////////////////////////////////////////////////////////////////////////// void CDlgSnmpGet::OnButtonGetnext() { LONG lLastError = 0L; LONG lDataType = 0L; BSTR bstrValue = NULL; BSTR bstrOID = NULL; VARIANT vtVar; VariantInit ( &vtVar ); if( m_pManager == NULL ) return; UpdateData( TRUE ); AfxGetApp( )->DoWaitCursor( 1 ); // turn on the wait cursor m_pManager->GetNext ( &vtVar ); AfxGetApp( )->DoWaitCursor( 0 ); // turn off the wait cursor m_pManager->get_LastError( &lLastError ); UpdateResult( lLastError ); if( lLastError == 0L ) { if ( vtVar.vt == VT_DISPATCH ) { ISnmpObject * pSnmpObject = ( ISnmpObject * ) vtVar.pdispVal; if ( pSnmpObject ) { pSnmpObject->get_Type( &lDataType ); m_strGetDataType = GetDataType( lDataType ); m_lCurrentType = lDataType; pSnmpObject->get_Value ( &bstrValue ); DisplayValue ( bstrValue ); SysFreeString( bstrValue ); pSnmpObject->get_OID ( &bstrOID ); m_strGetOID = bstrOID; SysFreeString ( bstrOID ); VariantClear ( &vtVar ); UpdateData( FALSE ); } } ( ( CWnd* ) GetDlgItem ( IDC_RADIO1 ) )->EnableWindow ( IsString () ); ( ( CWnd* ) GetDlgItem ( IDC_RADIO2 ) )->EnableWindow ( IsString () ); } } ///////////////////////////////////////////////////////////////////////////// void CDlgSnmpGet::OnButtonSet() { LONG lLastError; if( m_pManager == NULL ) return; UpdateData( TRUE ); ISnmpObject * pSnmpObject = NULL; HRESULT hr = CoCreateInstance(CLSID_SnmpObject, NULL, CLSCTX_INPROC_SERVER, IID_ISnmpObject, (void**) &pSnmpObject ); if( ! SUCCEEDED( hr ) ) { m_strResult = "Unable to load the ActiveSocket:SnmpData object"; UpdateData( FALSE ); AfxMessageBox( m_strResult, MB_OK | MB_ICONERROR ); return; } AfxGetApp( )->DoWaitCursor( 1 ); // turn on the wait cursor pSnmpObject->Clear (); pSnmpObject->put_OID ( _bstr_t( ( LPCTSTR ) m_strGetOID ) ); pSnmpObject->put_Value ( _bstr_t( ( LPCTSTR ) m_strSetValue ) ); pSnmpObject->put_Type ( m_lCurrentType ); m_pManager->Set ( &_variant_t ( ( IDispatch* ) pSnmpObject ) ); AfxGetApp()->DoWaitCursor( 0 ); // turn off the wait cursor m_pManager->get_LastError( &lLastError ); UpdateResult( lLastError ); if( lLastError == 0L ) { UpdateData( FALSE ); OnButtonGet(); } UpdateControls(); } ///////////////////////////////////////////////////////////////////////////// VOID CDlgSnmpGet::UpdateResult( LONG lLastError ) { UpdateData ( TRUE ); if ( m_pManager ) { BSTR bstrTemp = NULL; m_pManager->GetErrorDescription ( lLastError, &bstrTemp ); m_strResult.Format ( "%ld : %ls", lLastError, bstrTemp ); SysFreeString( bstrTemp ); } UpdateData( FALSE ); } ///////////////////////////////////////////////////////////////////////////// VOID CDlgSnmpGet::UpdateControls() { LONG lDataType = m_lCurrentType; ( ( CWnd * ) GetDlgItem( IDC_BUTTON_SESSIONOPEN ) )->EnableWindow( m_pManager != NULL && ! m_bSessionOpened ); ( ( CWnd * ) GetDlgItem( IDC_BUTTON_SESSIONCLOSE ) )->EnableWindow( m_pManager != NULL && m_bSessionOpened); ( ( CWnd * ) GetDlgItem( IDC_EDIT_SESSIONAGENT ) )->EnableWindow( m_pManager != NULL && ! m_bSessionOpened ); ( ( CWnd * ) GetDlgItem( IDC_EDIT_SESSIONPORT ) )->EnableWindow( m_pManager != NULL && ! m_bSessionOpened ); ( ( CWnd * ) GetDlgItem( IDC_EDIT_SESSIONCOMMUNITY ) )->EnableWindow( m_pManager != NULL && ! m_bSessionOpened ); ( ( CWnd * ) GetDlgItem( IDC_COMBO_TRANSLATIONMODE ) )->EnableWindow( m_pManager != NULL && ! m_bSessionOpened ); ( ( CWnd * ) GetDlgItem( IDC_EDIT_GETOID ) )->EnableWindow( m_pManager != NULL && m_bSessionOpened ); ( ( CWnd * ) GetDlgItem( IDC_BUTTON_GET ) )->EnableWindow( m_pManager != NULL && m_bSessionOpened ); ( ( CWnd * ) GetDlgItem( IDC_BUTTON_GETNEXT ) )->EnableWindow( m_pManager != NULL && m_bSessionOpened ); ( ( CWnd * ) GetDlgItem( IDC_EDIT_GETVALUE ) )->EnableWindow( m_pManager != NULL && m_bSessionOpened); ( ( CWnd * ) GetDlgItem( IDC_EDIT_GETDATATYPE ) )->EnableWindow( m_pManager != NULL && m_bSessionOpened); ( ( CWnd * ) GetDlgItem( IDC_EDIT_SETVALUE ) )->EnableWindow( m_pManager != NULL && m_bSessionOpened && lDataType != 0L ); ( ( CWnd * ) GetDlgItem( IDC_BUTTON_SET ) )->EnableWindow( m_pManager != NULL && m_bSessionOpened && lDataType != 0L ); // Remove a previous queried value that is not up-to-date anymore if( lDataType == 0 ) { UpdateData ( TRUE ); m_strGetValue = _T(""); m_strGetDataType = _T(""); m_strSetValue = _T(""); UpdateData ( FALSE ); } } ///////////////////////////////////////////////////////////////////////////// LPSTR CDlgSnmpGet::GetDataType( LONG lType ) { LPSTR lpszRetVal = ""; switch( lType ) { case asSNMP_TYPE_INTEGER: lpszRetVal = "ASN_INTEGER"; break; case asSNMP_TYPE_BITS: lpszRetVal = "ASN_BITS"; break; case asSNMP_TYPE_OCTETSTRING: lpszRetVal = "ASN_OCTETSTRING"; break; case asSNMP_TYPE_NULL: lpszRetVal = "ASN_NULL"; break; case asSNMP_TYPE_OBJECTIDENTIFIER: lpszRetVal = "ASN_OBJECTIDENTIFIER"; break; case asSNMP_TYPE_SEQUENCE: lpszRetVal = "ASN_SEQUENCE"; break; case asSNMP_TYPE_IPADDRESS: lpszRetVal = "ASN_IPADDRESS"; break; case asSNMP_TYPE_COUNTER32: lpszRetVal = "ASN_COUNTER32"; break; case asSNMP_TYPE_GAUGE32: lpszRetVal = "ASN_GAUGE32"; break; case asSNMP_TYPE_TIMETICKS: lpszRetVal = "ASN_TIMETICKS"; break; case asSNMP_TYPE_OPAQUE: lpszRetVal = "ASN_OPAQUE"; break; case asSNMP_TYPE_COUNTER64: lpszRetVal = "ASN_COUNTER64"; break; case asSNMP_TYPE_UNSIGNED32: lpszRetVal = "ASN_UNSIGNED32"; break; default: lpszRetVal = "Unknown"; break; } return lpszRetVal; } ///////////////////////////////////////////////////////////////////////////// void CDlgSnmpGet::OnButtonView() { UpdateData(); if (!m_strLogFile.GetLength()) return; if (ShellExecute(NULL,_T("open"),m_strLogFile,NULL,NULL,SW_SHOWDEFAULT ) == INVALID_HANDLE_VALUE) { AfxMessageBox (_T("Can't run viewer"),MB_OK); } } ///////////////////////////////////////////////////////////////////////////// void CDlgSnmpGet::OnRadio1() { m_lDisplayMode = 0; OnButtonGet (); } ///////////////////////////////////////////////////////////////////////////// void CDlgSnmpGet::OnRadio2() { m_lDisplayMode = 1; OnButtonGet (); } ///////////////////////////////////////////////////////////////////////////// BOOL CDlgSnmpGet::IsString() { BOOL bIsString = FALSE; switch( m_lCurrentType ) { case asSNMP_TYPE_BITS: bIsString = TRUE; break; case asSNMP_TYPE_OCTETSTRING: bIsString = TRUE; break; case asSNMP_TYPE_SEQUENCE: bIsString = TRUE; break; case asSNMP_TYPE_OPAQUE: bIsString = TRUE; break; } return bIsString; } ///////////////////////////////////////////////////////////////////////////// VOID CDlgSnmpGet::DisplayValue(BSTR bstrVal) { if ( IsString () ) { if ( m_lDisplayMode == 1 ) // HEX dump ? { INT nLength = SysStringLen ( bstrVal ); m_strGetValue = ""; for ( INT n = 0 ; n < nLength ; n++ ) { CHAR szTemp [ 4 ]; sprintf ( szTemp, "%02X ", bstrVal [ n ] ); m_strGetValue += szTemp; } UpdateData ( FALSE ); return; } } m_strGetValue = bstrVal; UpdateData ( FALSE ); } /////////////////////////////////////////////////////////////////////////////