snmp.cpp
来自「HP公司的SNMP++的Win32版本源码」· C++ 代码 · 共 1,767 行 · 第 1/5 页
CPP
1,767 行
// resume
if ( pdu_ref_count < 0) pdu_ref_count = 0;
unreg_status = UnregisterClass( SzSnmpClass,NULL);
if ( unreg_status == FALSE) {
SNMPDEBUG("-- SNMP++, UnRegister Class Fail \n");
}
#ifndef _DLL_ATTACH_OPTION
SnmpCleanup();
#endif
return SNMP_CLASS_SUCCESS;
};
//----------------------------------------------------------------------
//------[ Snmp Class Constructor ]--------------------------------------
//----------------------------------------------------------------------
Snmp::Snmp( int &status, unsigned short agent_port) // status of construction
{
SNMPDEBUG("++ SNMP++, SNMP Constructor \n");
SNMPAPI_STATUS WinSnmpStatus; // return status for snmp calls
SNMP_WAIT;
// call transport start up if it hasn't been called before
construct_status = SNMP_CLASS_SUCCESS;
if ( snmp_ref_count == 0)
{
status = transport_start_up();
construct_status = status;
if ( status != SNMP_CLASS_SUCCESS)
{
SNMPDEBUG("-- SNMP++, Transport Start Up Failure \n");
SNMP_SIGNAL;
return;
}
}
snmp_ref_count++; // bump up the ref count
pdu_handler = CreateWindow( SzSnmpClass, // name of class
NULL, // window text
WS_POPUP, // window style
0, // xpos
0, // ypos
0, // width
0, // hieght
0, // parent window handle, none for popup
0 , // window id
0, // parent instance
0); // pointer to construct data
if ( pdu_handler == NULL)
{
status = SNMP_CLASS_RESOURCE_UNAVAIL;
SNMPDEBUG("-- SNMP++, Hidden Window Create Failure \n");
construct_status = status;
SNMP_SIGNAL;
return;
}
else
{
// create a winsnmp session
iv_snmp_session = (SNMPHANDLE) SnmpOpen( pdu_handler , SNMP_MSG);
if ( iv_snmp_session == SNMPAPI_FAILURE) {
WinSnmpStatus = SnmpGetLastError( (HSNMP_SESSION) iv_snmp_session);
if (SNMPAPI_ALLOC_ERROR == WinSnmpStatus)
status = SNMP_CLASS_RESOURCE_UNAVAIL;
else if (SNMPAPI_OTHER_ERROR == WinSnmpStatus)
status = SNMP_CLASS_ERROR;
else
status = SNMP_CLASS_INTERNAL_ERROR;
SNMPDEBUG("-- SNMP++, WinSNMP Session Create Failure \n");
construct_status = status;
SNMP_SIGNAL;
return;
}
else {
// set the pdu handler extra data to the session handle value
// WinSnmp session
SetWindowLong( pdu_handler,
0,
(LONG) iv_snmp_session);
// snmp++ session
SetWindowLong( pdu_handler,
0 + sizeof( HWND),
(LONG) this);
// intialize all the trap receiving member variables
notify_targets = 0;
notify_ids = 0;
listen_addresses = 0;
notifycallback = 0;
notifycallback_data = 0;
// success
status = SNMP_CLASS_SUCCESS;
SNMP_SIGNAL;
return;
}
}
};
//--------------------------------------------------------------------
//---------[ Snmp Class Destructor ]----------------------------------
//--------------------------------------------------------------------
// destroys instance
Snmp::~Snmp()
{
SNMPAPI_STATUS status;
SNMP_WAIT;
// if we failed during construction then don't try
// to free stuff up that was not allocated
if ( construct_status != SNMP_CLASS_SUCCESS) {
SNMP_SIGNAL;
return;
}
// shut down trap reception if used
notify_unregister();
// terminate the session
pdu_container->search_and_destroy( (HSNMP_SESSION) iv_snmp_session, this);
status = SnmpClose( (HSNMP_SESSION) iv_snmp_session);
status = DestroyWindow( (HWND) pdu_handler);
snmp_ref_count--;
if (snmp_ref_count <0) snmp_ref_count = 0;
if ( snmp_ref_count == 0)
transport_shut_down( );
SNMPDEBUG("++ SNMP++, Exit From ~Snmp \n");
SNMP_SIGNAL;
};
//-----------------------[ access the trap reception info ]---------------
snmp_callback Snmp::get_notify_callback()
{ return notifycallback; };
void * Snmp::get_notify_callback_data()
{ return notifycallback_data; };
//-----------------------[ get notify filters ]---------------------------
// return back the currently set notification filters
// if they are not defined then return back empty collections
//
int Snmp::get_notify_filter( OidCollection &trapids,
TargetCollection &targets)
{
if (notify_ids != 0)
trapids = *notify_ids;
if ( notify_targets != 0)
targets = *notify_targets;
return SNMP_CLASS_SUCCESS;
};
//-----------------------[ register to get traps]-------------------------
// alternate form for local listen specification
int Snmp::notify_register( const OidCollection &trapids,
const TargetCollection &targets,
const AddressCollection &listen_addresses,
const snmp_callback callback,
const void *callback_data)
{
return notify_register( trapids,
targets,
callback,
callback_data);
};
//-----------------------[ register to get traps]-------------------------
int Snmp::notify_register( const OidCollection &trapids,
const TargetCollection &targets,
const snmp_callback callback,
const void *callback_data)
{
SNMPAPI_STATUS status;
SNMPAPI_STATUS WinSnmpStatus; // return status for snmp calls
int return_status = 0;
int already_on = FALSE;
// free up local collections
if ( notify_targets != 0)
delete notify_targets;
if ( notify_ids != 0) {
delete notify_ids;
already_on = TRUE;
}
// null out callback information
notifycallback = 0;
notifycallback_data = 0;
//-------------------------------------------------------------------------
//-----------[ assign new trap filtering info ]----------------------------
//-------------------------------------------------------------------------
// create a new collection using param passed in
notify_targets = new TargetCollection ( targets);
// delete current trapid collection if defined
// create a new collection using param passed in
notify_ids = new OidCollection( trapids);
// assign callback and callback data info
notifycallback = callback;
notifycallback_data = (void*) callback_data;
//--------------------------------------------------------------------
//-----------[ Turn the traps on using new info ]---------------------
//--------------------------------------------------------------------
if ( !already_on) {
// listen for everything
status = SnmpRegister( (HSNMP_SESSION)iv_snmp_session,// session to use
NULL, // source entity
NULL, // destination entity, don't care where its directed
NULL, // handle send context, don't care what the community name is
NULL, // oid mask
SNMPAPI_ON);
}
else
return SNMP_CLASS_SUCCESS;
if ( status == SNMPAPI_FAILURE) {
WinSnmpStatus = SnmpGetLastError( (HSNMP_SESSION) iv_snmp_session);
if (SNMPAPI_ALLOC_ERROR == WinSnmpStatus) {
return SNMP_CLASS_RESOURCE_UNAVAIL;
} else if (SNMPAPI_OTHER_ERROR == WinSnmpStatus) {
return SNMP_CLASS_ERROR;
} else if (SNMPAPI_TL_IN_USE == WinSnmpStatus) {
return SNMP_CLASS_TL_IN_USE;
} else if (SNMPAPI_TL_NOT_AVAILABLE == WinSnmpStatus) {
return SNMP_CLASS_TL_UNSUPPORTED;
} else {
return SNMP_CLASS_INTERNAL_ERROR;
}
}
return SNMP_CLASS_SUCCESS;
};
//-----------------------[ un-register to get traps]----------------------
int Snmp::notify_unregister()
{
SNMPAPI_STATUS Wstatus;
// free up local collections
if ( notify_targets != 0)
delete notify_targets;
if ( notify_ids != 0)
delete notify_ids;
// null out callback information
notifycallback = 0;
notifycallback_data = 0;
// invoke WinSNMP register to turn it off
Wstatus = SnmpRegister((HSNMP_SESSION)iv_snmp_session, // session to use
NULL, // source entity
NULL, // destination entity, don't care where its directed
NULL, // handle send context, don't care what the community name is
NULL, // oid mask
SNMPAPI_OFF);
if ( Wstatus == SNMPAPI_FAILURE) {
Wstatus = SnmpGetLastError( (HSNMP_SESSION) iv_snmp_session);
if (SNMPAPI_ALLOC_ERROR == Wstatus) {
return SNMP_CLASS_RESOURCE_UNAVAIL;
} else {
return SNMP_CLASS_INTERNAL_ERROR;
}
}
return SNMP_CLASS_SUCCESS;
};
//----------------------[ cancel a pending request ]--------------------
// cancel a pending request
// only cancels async requests
// returns true is canceled and false if nothing to cancel
int Snmp::cancel( const unsigned long rid)
{
snmpcallback callback;
SNMP_WAIT;
if ( pdu_container->check_if_pending( (long int) rid, &callback))
{
if ( callback != 0)
{
pdu_container->clear_request_id( rid);
SNMP_SIGNAL;
return SNMP_CLASS_SUCCESS;
}
}
SNMP_SIGNAL;
return SNMP_CLASS_INVALID_REQID; // no requests pending for this rid
};
//------------------------[ get ]---------------------------------------
int Snmp::get( Pdu &pdu, // pdu to use
SnmpTarget &target) // get target
{
pdu.set_type( sNMP_PDU_GET);
return snmp_engine( pdu, // get pdu
0, // max repeaters
0, // non repeaters
target, // target
NULL, // callback for async only
0); // callback data
};
//------------------------[ get async ]----------------------------------
int Snmp::get( Pdu &pdu, // pdu to use
SnmpTarget &target, // destination target
const snmp_callback callback, // async callback
const void * callback_data) // callback data
{
if ( ! callback )
return SNMP_CLASS_INVALID_CALLBACK;
pdu.set_type( sNMP_PDU_GET);
return snmp_engine( pdu, // get async pdu
0, // max repeaters
0, // non repeaters
target, // target
callback, // callback to use
callback_data); // callback data
};
//------------------------[ get next ]-----------------------------------
int Snmp::get_next( Pdu &pdu, // pdu to use
SnmpTarget &target) // get target
{
pdu.set_type( sNMP_PDU_GETNEXT);
return snmp_engine( pdu, // pdu to get next
0, // max repeaters
0, // non repeaters
target, // target
NULL, // callback for async only
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?