uxsnmp.cpp
来自「JdonFramework need above jdk 1.4.0 This」· C++ 代码 · 共 2,128 行 · 第 1/5 页
CPP
2,128 行
debugprintf(4,"Snmp::trap:"); debugprintf(4," engineID (%s), securityName (%s)\n securityModel (%i) security_level (%i)", engine_id.get_printable(), security_name.get_printable(), security_model, pdu.get_security_level()); debugprintf(4," Addr/Port (%s)",udp_address.get_printable()); status = snmpmsg.loadv3( pdu, engine_id, security_name, security_model, (snmp_version)version); } else#endif status = snmpmsg.load( pdu, my_get_community, (snmp_version) version); if ( status != SNMP_CLASS_SUCCESS) { debugprintf(0, "snmp message load error!"); return status; } lock(); //------[ send the trap ] if (udp_address.get_ip_version() == Address::version_ipv4) { if (iv_snmp_session != INVALID_SOCKET) status = send_snmp_request(iv_snmp_session, snmpmsg.data(), (size_t)snmpmsg.len(), udp_address); else { udp_address.map_to_ipv6(); status = send_snmp_request(iv_snmp_session_ipv6, snmpmsg.data(), (size_t)snmpmsg.len(), udp_address); } } else status = send_snmp_request(iv_snmp_session_ipv6, snmpmsg.data(), (size_t)snmpmsg.len(), udp_address); unlock(); if (status != 0) return SNMP_CLASS_TL_FAILED; return SNMP_CLASS_SUCCESS;}//----------------[ set notify_timestamp if it is null ]-------------#if defined (CPU) && CPU == PPC603 struct SCommTimer { unsigned long NumMS; unsigned long FractMS; }; extern "C" { void GetTime (struct SCommTimer * Time); }#endifvoid Snmp::check_notify_timestamp(Pdu &pdu){ // As we don't know, when the application was started, // use a continuously increasing notify_timestamp TimeTicks timestamp; pdu.get_notify_timestamp( timestamp); if (timestamp <= 0) {#ifdef WIN32 struct _timeb timebuffer; _ftime( &timebuffer ); timebuffer.time -= 1103760000; // knock off 35 years worth of seconds timestamp = SAFE_ULONG_CAST((timebuffer.time * 100) + (timebuffer.millitm / 10));#elif defined (CPU) && CPU == PPC603 SCommTimer theTime; GetTime(&theTime); // This function must be defined by the application timestamp = theTime.NumMS/10;#else struct timeval tp; gettimeofday(&tp, NULL); tp.tv_sec -= 1103760000; // knock off 35 years worth of seconds timestamp = (tp.tv_sec * 100) + (tp.tv_usec / 10000);#endif pdu.set_notify_timestamp( timestamp); }}//-----------------------[ read the notification filters ]----------------int Snmp::get_notify_filter( OidCollection &trapids, TargetCollection &targets, AddressCollection &listen_addresses){ CNotifyEvent *e = eventListHolder->notifyEventList()->GetEntry(this); if (!e) return SNMP_CLASS_INVALID; e->get_filter(trapids, targets, listen_addresses); return SNMP_CLASS_SUCCESS;}// Set the port for listening to traps and informs.void Snmp::notify_set_listen_port(const int port){ eventListHolder->notifyEventList()->set_listen_port(port);}// Get the port that is used for listening to traps and informs.int Snmp::notify_get_listen_port(){ return eventListHolder->notifyEventList()->get_listen_port();}//-----------------------[ 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 callback and callback data info notifycallback = callback; notifycallback_data = (void *)callback_data; // remove any previous filters for this session eventListHolder->notifyEventList()->DeleteEntry(this); // add to the notify queue status = eventListHolder->notifyEventList()->AddEntry(this, trapids, targets, addresses); iv_notify_fd = eventListHolder->notifyEventList()->get_notify_fd(); // DLD 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(){ /// @todo Return error if not registered before! // remove from the notify queue eventListHolder->notifyEventList()->DeleteEntry(this); // null out callback information notifycallback = 0; notifycallback_data = 0; iv_notify_fd = INVALID_SOCKET; 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 const SnmpTarget &target, // from this target const snmp_callback cb,// callback for async calls const void *cbd, // callback data SnmpSocket fd, int reports_received){ long req_id; // pdu request id int status; // send status#ifdef _SNMPv3 // save original PDU for later reference Pdu backupPdu = pdu; for (int maxloops=0; maxloops<3; maxloops++) {#endif 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 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) || (action == sNMP_PDU_INFORM_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) || (action == sNMP_PDU_INFORM))) 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; OctetStr community_string; OctetStr security_name; int security_model; const CTarget* ctarget = NULL; const UTarget* utarget = NULL; switch (target.get_type()) { case SnmpTarget::type_ctarget: ctarget = (CTarget*)(&target); break; case SnmpTarget::type_utarget: utarget = (UTarget*)(&target); break; case SnmpTarget::type_base: debugprintf(0, "-- SNMP++, do not use SnmpTarget," "use a CTarget or UTarget"); return SNMP_CLASS_INVALID_TARGET; default: /* target is not known */ debugprintf(0, "-- SNMP++, type of target is unknown!"); return SNMP_CLASS_UNSUPPORTED; } if (ctarget) /* Is is a CTarget? */ { debugprintf(3, "snmp_engine called with CTarget"); if (!ctarget->resolve_to_C( my_get_community, my_set_community, address, my_timeout, my_retry, version)) { debugprintf(0, "-- SNMP++, Resolve Fail (CTarget)"); return SNMP_CLASS_UNSUPPORTED; }#ifdef _SNMPv3 if ((version == version3) || (action == sNMP_PDU_REPORT)) { debugprintf(0, "-- SNMP++, use UTarget for SNMPv3"); return SNMP_CLASS_INVALID_TARGET; }#endif //----------[ 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) || ( action == sNMP_PDU_INFORM) || ( action == sNMP_PDU_INFORM_ASYNC) || ( action == sNMP_PDU_RESPONSE)) community_string = my_get_community; else /* got to be a set */ community_string = my_set_community; } else if (utarget) /* Is is a UTarget? */ { debugprintf(3, "snmp_engine called with UTarget"); if (!utarget->resolve_to_U( security_name, security_model, address, my_timeout, my_retry, version)) { debugprintf(0, "-- SNMP++, Resolve Fail (UTarget)"); return SNMP_CLASS_UNSUPPORTED; }#ifdef _SNMPv3 if (version != version3) {#endif community_string = security_name; if (((version == version1) && (security_model != SecurityModel_v1)) || ((version == version2c) && (security_model != SecurityModel_v2))) { LOG_BEGIN(ERROR_LOG | 1); LOG("Snmp: Target does not match SNMP version: (security model) (version)"); LOG(security_model); LOG(version); LOG_END; return SNMP_CLASS_INVALID_TARGET; }#ifdef _SNMPv3 } // end if (version != version3)#endif } else { // target is neither CTarget nor UTarget (should not happen) debugprintf(0, "-- SNMP++, Resolve Fail"); return SNMP_CLASS_INVALID_TARGET; } if (!address.valid()) { debugprintf(0, "-- SNMP++, Target contains invalid address"); return SNMP_CLASS_INVALID_TARGET; } //----------[ validate the target address ]-------------------------- if ((address.get_type() != Address::type_ip) && (address.get_type() != Address::type_udp) ) { debugprintf(0, "-- SNMP++, Bad address type"); return SNMP_CLASS_TL_UNSUPPORTED; } UdpAddress udp_address(address); if (!udp_address.valid()) { debugprintf(0, "-- SNMP++, Bad address"); return SNMP_CLASS_RESOURCE_UNAVAIL; } //----------[ choose the target address port ]----------------------- if ((address.get_type() == Address::type_ip) || !udp_address.get_port()) { if (pdu_action == sNMP_PDU_INFORM) udp_address.set_port(SNMP_TRAP_PORT); else udp_address.set_port(SNMP_PORT); } // otherwise port was already set // check socket to use SnmpSocket iv_session_used = fd; if (fd == INVALID_SOCKET) { if (udp_address.get_ip_version() == Address::version_ipv4) { if (iv_snmp_session != INVALID_SOCKET) iv_session_used = iv_snmp_session; else { udp_address.map_to_ipv6(); iv_session_used = iv_snmp_session_ipv6; } } else iv_session_used = iv_snmp_session_ipv6; } if ((pdu_action != sNMP_PDU_RESPONSE) && (pdu_action != sNMP_PDU_REPORT)) { // set error index to none pdu.set_error_index(0); // determine request id to use req_id = MyMakeReqId(); pdu.set_request_id(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) { pdu.set_error_status((int) non_reps); pdu.set_error_index((int) max_reps); } pdu.set_type( pdu_action); SnmpMessage snmpmsg;#ifdef _SNMPv3 if (version == version3) { if (!utarget) { debugprintf(0, "-- SNMP++, need UTarget to send SNMPv3 message!"); return SNMP_CLASS_INVALID_TARGET; } OctetStr engine_id; utarget->get_engine_id(engine_id); if (engine_id.len() == 0) { if (v3MP::I->get_from_engine_id_table(engine_id, (char*)udp_address.get_printable()) == SNMPv3_MP_OK ) { // Override const here
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?