📄 uxsnmp.cpp
字号:
SnmpTarget &target, // destination target const snmp_callback callback, // callback to use const void * callback_data) // callback data{ pdu.set_type( sNMP_PDU_SET_ASYNC); return snmp_engine( pdu, // pdu to set 0, // max repeaters 0, // non repeaters target, // target callback, // callback function callback_data); // callback data};//-----------------------[ get bulk ]------------------------------------int Snmp::get_bulk( Pdu &pdu, // pdu to use SnmpTarget &target, // destination target const int non_repeaters, // number of non repeaters const int max_reps) // maximum number of repetitions{ pdu.set_type( sNMP_PDU_GETBULK); return snmp_engine( pdu, // pdu to use non_repeaters, // # of non repeaters max_reps, // max repititions target, // target NULL, // callback for async only 0); // callback data};//-----------------------[ get bulk async ]------------------------------int Snmp::get_bulk( Pdu &pdu, // pdu to use SnmpTarget &target, // destination target const int non_repeaters, // number of non repeaters const int max_reps, // maximum number of repetitions const snmp_callback callback, // callback to use const void * callback_data) // callback data{ pdu.set_type( sNMP_PDU_GETBULK_ASYNC); return snmp_engine( pdu, // pdu to use non_repeaters, // # of non repeaters max_reps, // max repititions target, // target callback, // callback function callback_data); // callback data};//-----------------------[ cancel ]--------------------------------------int Snmp::cancel( const unsigned long request_id){ int status = snmpEventList->DeleteEntry(request_id); return(status);};//----------------------[ blocking inform, V2 only]------------------------int Snmp::inform( Pdu &pdu, // pdu to send SnmpTarget &target) // destination target{ pdu.set_type( sNMP_PDU_INFORM); return snmp_engine( pdu, 0, 0, target, NULL, 0); };//----------------------[ asynch inform, V2 only]------------------------int Snmp::inform( Pdu &pdu, // pdu to send SnmpTarget &target, // destination target const snmp_callback callback, // callback function const void * callback_data) // callback data{ pdu.set_type( sNMP_PDU_INFORM); return snmp_engine( pdu, 0, 0, target, callback, callback_data);};//---------------------[ send a trap ]-----------------------------------int Snmp::trap( Pdu &pdu, // pdu to send SnmpTarget &target) // destination target{ OctetStr my_get_community; OctetStr my_set_community; GenAddress address; unsigned long my_timeout; int my_retry; unsigned char version; int status; SNMPDEBUG("++ SNMP++, Send a Trap\n"); //---------[ make sure pdu is valid ]--------------------------------- if ( !pdu.valid()) { SNMPDEBUG("-- SNMP++, PDU Object Invalid\n"); return SNMP_CLASS_INVALID_PDU; } //---------[ make sure target is valid ]------------------------------ if ( !target.valid()) { SNMPDEBUG("-- SNMP++, Target Object Invalid\n"); return SNMP_CLASS_INVALID_TARGET; } //---------[ try to resolve it as a v1 target ]------------------------ if ( !target.resolve_to_C( my_get_community, my_set_community, address, my_timeout, my_retry, version)) { SNMPDEBUG("-- SNMP++, Resolve Fail\n"); return SNMP_CLASS_UNSUPPORTED; } //--------[ determine request id to use ]------------------------------ set_request_id(&pdu, MyMakeReqId()); //--------[ check timestamp, if null use system time ]----------------- TimeTicks timestamp; pdu.get_notify_timestamp( timestamp); if (timestamp <= 0) { struct timeval tp; gettimeofday(&tp, NULL); tp.tv_sec -= 642816000; // knock off 20years worth of seconds timestamp = (tp.tv_sec * 100) + (tp.tv_usec / 10000); pdu.set_notify_timestamp( timestamp); } //------[ validate address to use ]------------------------------------- if (!address.valid()) { SNMPDEBUG("-- SNMP++, Bad address\n"); return SNMP_CLASS_INVALID_TARGET; } if ((address.get_type() != type_ip) && (address.get_type() != type_udp) ) { SNMPDEBUG("-- SNMP++, Bad address type\n"); return SNMP_CLASS_TL_UNSUPPORTED; } UdpAddress udp_address(address); if (!udp_address.valid()) { SNMPDEBUG("-- SNMP++, copy address failed\n"); return SNMP_CLASS_RESOURCE_UNAVAIL; } //----------[ choose the target address port ]----------------------- if ((address.get_type() == type_ip) || !udp_address.get_port()) udp_address.set_port(SNMP_TRAP_PORT); //----------[ based on the target type, choose v1 or v1 trap type ]----- if ( version == version1) pdu.set_type( sNMP_PDU_V1TRAP); else pdu.set_type( sNMP_PDU_TRAP); //------[ build the snmp message ]---------------------------------------- SnmpMessage snmpmsg; status = snmpmsg.load( pdu, my_get_community, (snmp_version) version); if ( status != SNMP_CLASS_SUCCESS) return status; //------[ send the trap ] status = send_snmp_request((int) iv_snmp_session, snmpmsg.data(), (size_t)snmpmsg.len(), udp_address); if (status != 0) return SNMP_CLASS_TL_FAILED; return SNMP_CLASS_SUCCESS;};//-----------------------[ access the trap reception info ]---------------snmp_callback Snmp::get_notify_callback(){ return notifycallback; };void * Snmp::get_notify_callback_data(){ return notifycallback_data; };//-----------------------[ read the notification filters ]----------------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]-------------------------int Snmp::notify_register( const OidCollection &trapids, const TargetCollection &targets, const AddressCollection &addresses, const snmp_callback callback, const void *callback_data) { int status; //------------------------------------------------------------------------- //-----------[ assign new trap filtering info ]---------------------------- //-------------------------------------------------------------------------#if 0// TM: Note since the Snmp class does not allow me to get access to// the notify_targets and notify_ids I carry them in the notify queue// instead of adding them into the Snmp object. This should be// changed when Snmp allows extraction of these values. // delete current target collection if defined if (notify_targets != 0) delete notify_targets; // create a new collection using param passed in notify_targets = new TargetCollection ( targets); // delete current trapid collection if defined if (notify_ids != 0) delete notify_ids; // create a new collection using param passed in notify_ids = new OidCollection( trapids);#endif // 0 // assign callback and callback data info notifycallback = callback; notifycallback_data = (void *)callback_data; // remove any previous filters for this session notifyEventList->DeleteEntry(this); // add to the notify queue status = notifyEventList->AddEntry(this, trapids, targets, addresses); return status;};// alternate form for local listen specification//-----------------------[ register to get traps]-------------------------int Snmp::notify_register( const OidCollection &trapids, const TargetCollection &targets, const snmp_callback callback, const void *callback_data) { AddressCollection addresses; return notify_register(trapids, targets, addresses, callback, callback_data);}//-----------------------[ un-register to get traps]----------------------int Snmp::notify_unregister() { #if 0//TM: see comment in notify_register // free up local collections if ( notify_targets != 0) delete notify_targets; if ( notify_ids != 0) delete notify_ids;#endif // 0 // null out callback information notifycallback = 0; notifycallback_data = 0; // remove from the notify queue notifyEventList->DeleteEntry(this); return SNMP_CLASS_SUCCESS; };//---------[ get / set engine ]-----------------------------------------// The main snmp engine used for all requests// async requests return out early and don't wait in here for// the responseint Snmp::snmp_engine( Pdu &pdu, // pdu to use long int non_reps, // # of non repititions long int max_reps, // # of max repititions SnmpTarget &target, // from this target const snmp_callback cb, // callback for async calls const void * cbd) // callback data{ long req_id; // pdu request id int status; // send status unsigned short pdu_action; // type of pdu to build unsigned short action; // type of pdu to build unsigned long my_timeout; // target specific timeout int my_retry; // target specific retry unsigned char send_buf[MAX_SNMP_PACKET]; size_t send_len = MAX_SNMP_PACKET; OctetStr my_get_community; OctetStr my_set_community; GenAddress address; unsigned char version; //---------[ make sure pdu is valid ]-------------------------- if ( !pdu.valid()) return SNMP_CLASS_INVALID_PDU; //---------[ depending on user action, map the correct pdu action] action = pdu.get_type(); map_action(action, pdu_action); //---------[ check for correct mode ]--------------------------- // if the class was constructed as a blocked model, callback=0 // and async calls are attempted, an error is returned if (( cb == 0) && ((action == sNMP_PDU_GET_ASYNC) || (action == sNMP_PDU_SET_ASYNC) || (action == sNMP_PDU_GETNEXT_ASYNC) || (action == sNMP_PDU_GETBULK_ASYNC))) return SNMP_CLASS_INVALID_CALLBACK; //---------[ more mode checking ]-------------------------------- // if the class was constructed as an async model, callback = something // and blocked calls are attempted, an error is returned if (( cb != 0) && ((action == sNMP_PDU_GET) || (action == sNMP_PDU_SET) || (action == sNMP_PDU_GETNEXT) || (action == sNMP_PDU_GETBULK))) return SNMP_CLASS_INVALID_CALLBACK; //---------[ make sure target is valid ]------------------------- // make sure that the target is valid if ( ! target.valid()) return SNMP_CLASS_INVALID_TARGET; // try to resolve it as a v1 target instead if ( !target.resolve_to_C( my_get_community, my_set_community, address, my_timeout, my_retry, version)) { SNMPDEBUG("-- SNMP++, Target is not Community-based\n"); return SNMP_CLASS_INVALID_TARGET; } if (!address.valid()) { SNMPDEBUG("-- SNMP++, Target contains invalid address\n"); return SNMP_CLASS_INVALID_TARGET; } //----------[ validate the target address ]-------------------------- if ((address.get_type() != type_ip) && (address.get_type() != type_udp) ) { SNMPDEBUG("-- SNMP++, Bad address type\n"); return SNMP_CLASS_TL_UNSUPPORTED; } UdpAddress udp_address(address); if (!udp_address.valid()) { SNMPDEBUG("-- SNMP++, Bad address\n"); return SNMP_CLASS_RESOURCE_UNAVAIL; } //----------[ choose the target address port ]----------------------- if ((address.get_type() == type_ip) || !udp_address.get_port()) { udp_address.set_port(SNMP_PORT); } // otherwise port was already set OctetStr community_string; //----------[ use the appropriate community string ]----------------- if (( action == sNMP_PDU_GET) || ( action == sNMP_PDU_GET_ASYNC) || ( action == sNMP_PDU_GETNEXT) || ( action == sNMP_PDU_GETNEXT_ASYNC) || ( action == sNMP_PDU_GETBULK) || ( action == sNMP_PDU_GETBULK_ASYNC)) { community_string = my_get_community; } else // got to be a set { community_string = my_set_community; } // set error index to none set_error_index(&pdu, 0); // determine request id to use req_id = MyMakeReqId(); set_request_id(&pdu, req_id); //---------[ map GetBulk over v1 to GetNext ]------------------------- if (( pdu_action == sNMP_PDU_GETBULK)&&( (snmp_version)version== version1)) pdu_action = sNMP_PDU_GETNEXT; if ( pdu_action == sNMP_PDU_GETBULK) { set_error_status( &pdu,(int) non_reps); set_error_index( &pdu,(int) max_reps); } pdu.set_type( pdu_action); SnmpMessage snmpmsg; status = snmpmsg.load( pdu, community_string,(snmp_version) version); if ( status != SNMP_CLASS_SUCCESS) { printf("snmp message load error !\n"); return status; } //------[ send the request ] status = send_snmp_request((int) iv_snmp_session, // socket descriptor snmpmsg.data(), (size_t) snmpmsg.len(),// pdu to send udp_address); // address to send to if ( status != 0) { return SNMP_CLASS_TL_FAILED; } // Add the message to the message queue snmpEventList->AddEntry( req_id, this, (int)iv_snmp_session, target, pdu, snmpmsg.data(), (size_t) snmpmsg.len(), udp_address, cb, (void *)cbd); //----[ if an async mode request then return success ]----- if (( action == sNMP_PDU_GET_ASYNC) || ( action == sNMP_PDU_SET_ASYNC) || ( action == sNMP_PDU_GETNEXT_ASYNC) || ( action == sNMP_PDU_GETBULK_ASYNC)) { // call the async handler in case someone is // firing multiple requests without yielding // TM seems kind of unfriendly... // SNMPProcessPendingEvents(); return SNMP_CLASS_SUCCESS; } // Now wait for the response (or timeout) for our message. // This handles any necessary retries. status = SNMPBlockForResponse(req_id, pdu); return status;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -