snmp.cpp
来自「HP公司的SNMP++的Win32版本源码」· C++ 代码 · 共 1,767 行 · 第 1/5 页
CPP
1,767 行
&hDstRecvEnt,
&hRecvContext,
&hRecvPdu);
// if successful process it, else throw it away
if ( Wstatus != SNMPAPI_FAILURE)
{
SNMPDEBUG("++ SNMP++, Get PDU Data \n");
Wstatus = SnmpGetPduData( hRecvPdu,
&lnRecvPduType,
&lnRecvRequestId,
&lnErrorStatus,
&lnErrorIndex,
&hRecvVbl );
// if the decode was successful stuff it
// into the pdu container else throw it away
if ( Wstatus == SNMPAPI_SUCCESS)
{
switch( lnRecvPduType)
{
//----[ response ]-----------------
case SNMP_PDU_RESPONSE:
SNMP_WAIT;
status = pdu_container->check_if_pending( (long int) lnRecvRequestId,
&callback);
SNMP_SIGNAL;
if ( status)
{
if ( callback ==0)
{ // blocking response
// memory is freed when extracted using get_slot
SNMPDEBUG("++ SNMP++, Process Blocked MSG \n");
SNMP_WAIT;
pdu_container->set_slot( (long int) lnRecvRequestId,
session_handle,
hSrcRecvEnt,
hDstRecvEnt,
hRecvContext,
lnRecvPduType,
lnRecvRequestId,
lnErrorStatus,
lnErrorIndex,
hRecvVbl);
SNMPDEBUG("++ SNMP++, Free PDU \n");
SnmpFreePdu( hRecvPdu);
// update the pdu count
pdu_container->pdu_received();
SNMP_SIGNAL;
}
else
{ // async response
SNMPDEBUG("++ SNMP++, Process Async Msg \n");
// if we can find the request in the pdu container
// then get the callback information and process it
SNMP_WAIT;
status = pdu_container->get_cb_info( (long int) lnRecvRequestId,
&callback,
&personality);
SNMP_SIGNAL;
if ( status)
process_async_event( (int) SNMP_CLASS_ASYNC_RESPONSE,
lnRecvRequestId,
session_handle,
lnErrorStatus,
lnErrorIndex,
hRecvVbl,
hRecvContext,
hSrcRecvEnt,
spp_session,
callback,
personality);
else {
SNMP_WAIT;
pdu_container->stray_response();
SNMP_SIGNAL;
}
SNMPDEBUG("++ SNMP++, Free Vars After process_async\n");
SnmpFreeVbl( hRecvVbl);
SnmpFreeEntity( hSrcRecvEnt);
SnmpFreeContext( hRecvContext);
SnmpFreeEntity( hDstRecvEnt); // don't need
SnmpFreePdu( hRecvPdu); // don't need
// update the pdu count
SNMP_WAIT;
pdu_container->pdu_received();
SNMP_SIGNAL;
}
}
else // not a pending response, toss it
{
SNMPDEBUG("++ SNMP++, Stray Message \n");
SnmpFreeEntity( hSrcRecvEnt);
SnmpFreeEntity( hDstRecvEnt);
SnmpFreeContext( hRecvContext);
SnmpFreePdu( hRecvPdu);
SnmpFreeVbl( hRecvVbl);
SNMP_WAIT;
pdu_container->stray_response();
SNMP_SIGNAL;
}
break;
//---[ trap ]----------------------
case SNMP_PDU_TRAP:
SNMP_WAIT;
pdu_container->trap_received();
SNMP_SIGNAL;
SNMPDEBUG("++ SNMP++, TRAP Received \n");
if ( spp_session->get_notify_callback() != 0)
process_trap_event( (int) SNMP_CLASS_NOTIFICATION,
lnRecvRequestId,
session_handle,
lnErrorStatus,
lnErrorIndex,
hRecvVbl,
hRecvContext,
hSrcRecvEnt,
spp_session,
spp_session->get_notify_callback(),
spp_session->get_notify_callback_data());
SNMPDEBUG("++ SNMP++, Free Entity and PDU \n");
SnmpFreeEntity( hSrcRecvEnt);
SnmpFreeEntity( hDstRecvEnt);
SnmpFreeContext( hRecvContext);
SnmpFreePdu( hRecvPdu);
SnmpFreeVbl( hRecvVbl);
break;
//----[ unidentified message ]---------------
default:
{
SNMPDEBUG("++ SNMP++, unidentified Message \n");
SNMP_WAIT;
pdu_container->stray_response();
SnmpFreeEntity( hSrcRecvEnt);
SnmpFreeEntity( hDstRecvEnt);
SnmpFreeContext( hRecvContext);
SnmpFreePdu( hRecvPdu);
SnmpFreeVbl( hRecvVbl);
pdu_container->invalid_resp_pdu();
SNMP_SIGNAL;
}
}; // end switch
}
else //error getting pdu data, so we need to free the vars
{
SNMPDEBUG("++ SNMP++, GET PDU ERROR \n");
SNMP_WAIT;
SnmpFreeEntity( hSrcRecvEnt);
SnmpFreeEntity( hDstRecvEnt);
SnmpFreeContext( hRecvContext);
SnmpFreePdu( hRecvPdu);
pdu_container->decode_err();
SNMP_SIGNAL;
}
} // end if recv success
else
{
SNMPDEBUG("++ SNMP++, Receive MSG Fail \n");
// receive message fail, look for timeout case
last_error = SnmpGetLastError( session_handle);
if ( last_error == SNMPAPI_TL_TIMEOUT)
{
SNMPDEBUG("++ SNMP++, WinSNMP TimeOut Notify \n");
// process a async timeout event
// we only care about async ones
// handles from receive message are valid
SNMPDEBUG("++ SNMP++, Get PDU Data \n");
Wstatus = SnmpGetPduData( hRecvPdu,
&lnRecvPduType,
&lnRecvRequestId,
&lnErrorStatus,
&lnErrorIndex,
&hRecvVbl );
if ( Wstatus == SNMPAPI_SUCCESS)
{
SNMP_WAIT;
pdu_container->timeout();
// check to make sure that this was a
// pending async request
status = pdu_container->get_cb_info( (long int) lnRecvRequestId,
&callback,
&personality);
SNMP_SIGNAL;
if ( status)
{
// clear the request from the pdu container
SNMP_WAIT;
pdu_container->clear_request_id( (long int) lnRecvRequestId);
SNMP_SIGNAL;
// inform the users callback of the timeout
// this was the initial request
process_async_event( (int) SNMP_CLASS_TIMEOUT,
lnRecvRequestId,
session_handle,
0, // error status
0, // error index
hRecvVbl,
hRecvContext,
hSrcRecvEnt,
spp_session,
callback,
personality);
}
SnmpFreeVbl( hRecvVbl);
}
// free up the get message vars
// these are valid on a timeout
SnmpFreeEntity( hSrcRecvEnt);
SnmpFreeEntity( hDstRecvEnt);
SnmpFreeContext( hRecvContext);
SnmpFreePdu( hRecvPdu);
}
else {
SNMP_WAIT;
pdu_container->receive_err();
SNMP_SIGNAL;
}
}
return 0;
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
};
//----------------------------------------------------------------------
//--------[ transport layer start up ]----------------------------------
//----------------------------------------------------------------------
// Private function, not exposed to the outside
int transport_start_up( )
{
SNMPAPI_STATUS status;
WNDCLASS snmpc;
#ifndef _DLL_ATTACH_OPTION
smiUINT32 nMajVer, nMinVer, nLevel, nTrans, nReTrans;
#endif
SNMPDEBUG("++ SNMP++, Transport Start UP \n");
// set translation mode to v1
SnmpSetTranslateMode( SNMPAPI_UNTRANSLATED_V1);
// register the snmp class
snmpc.style = 0;
snmpc.lpfnWndProc = SnmpWndProc;
snmpc.cbClsExtra = 0;
snmpc.cbWndExtra = sizeof( HWND) + // WinSnmp Session
sizeof( Snmp*); // Snmp++ session
snmpc.hInstance = NULL;
snmpc.hIcon = NULL;
snmpc.hCursor = NULL;
snmpc.hbrBackground = NULL;
snmpc.lpszMenuName = NULL;
snmpc.lpszClassName = SzSnmpClass;
// register will fail if the class
// already exists, in this case
// we don't care
// This may happen is someone inadvertently
// calls transport_start_up more than once!
status = RegisterClass( &snmpc);
if ( status == 0)
{
SNMPDEBUG("-- SNMP++, CLASS REGISTER FAIL \n");
return SNMP_CLASS_RESOURCE_UNAVAIL;
}
SNMPDEBUG("++ SNMP++, Done Xport Start Up \n");
// create the pdu container class
// here, multiple pdu container objects per app
// should not be created
pdu_ref_count++; // up the ref count
if ( pdu_ref_count ==1)
{
SNMPDEBUG("++ SNMP++, New PDU Container \n");
pdu_container = new Pdu_Container();
if ( pdu_container == NULL)
{
SNMPDEBUG("-- SNMP++, Pdu Container Create Fail \n");
return SNMP_CLASS_RESOURCE_UNAVAIL;
}
else
{
#ifndef _DLL_ATTACH_OPTION
// start up WinSNMP
status = SnmpStartup(&nMajVer,
&nMinVer,
&nLevel,
&nTrans,
&nReTrans);
if ( status != SNMPAPI_SUCCESS ) {
SNMPDEBUG("Unable to Startup WinSNMP!\n");
return 0;
}
SNMPDEBUG("Startup WinSNMP OK\n");
#endif
return SNMP_CLASS_SUCCESS;
}
}
else
{
#ifndef _DLL_ATTACH_OPTION
// start up WinSNMP
status = SnmpStartup(&nMajVer,
&nMinVer,
&nLevel,
&nTrans,
&nReTrans);
if ( status != SNMPAPI_SUCCESS ) {
SNMPDEBUG("Unable to Startup WinSNMP!\n");
return 0;
}
SNMPDEBUG("Startup WinSNMP OK\n");
#endif
return SNMP_CLASS_SUCCESS; // we already have a good pdu object
}
};
//-----------------------------------------------------------------------
//----------[ transport layer shut down ]--------------------------------
//-----------------------------------------------------------------------
// Private function
int transport_shut_down( )
{
int unreg_status;
SNMPDEBUG("++ SNMP++, Transport Shut Down \n");
// delete the pdu container only when the ref
// count reaches 0
pdu_ref_count--;
if ( pdu_ref_count == 0)
{
SNMPDEBUG("++ SNMP++, Destroy PDU Container \n");
delete pdu_container;
}
// protect against running counter < 0
// this allows a new transport_start_up to
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?