snmpmsg.cpp

来自「JdonFramework need above jdk 1.4.0 This」· C++ 代码 · 共 832 行 · 第 1/2 页

CPP
832
字号
/*_############################################################################  _##   _##  snmpmsg.cpp    _##  _##  SNMP++v3.2.21a  _##  -----------------------------------------------  _##  Copyright (c) 2001-2006 Jochen Katz, Frank Fock  _##  _##  This software is based on SNMP++2.6 from Hewlett Packard:  _##    _##    Copyright (c) 1996  _##    Hewlett-Packard Company  _##    _##  ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.  _##  Permission to use, copy, modify, distribute and/or sell this software   _##  and/or its documentation is hereby granted without fee. User agrees   _##  to display the above copyright notice and this license notice in all   _##  copies of the software and any documentation of the software. User   _##  agrees to assume all liability for the use of the software;   _##  Hewlett-Packard and Jochen Katz make no representations about the   _##  suitability of this software for any purpose. It is provided   _##  "AS-IS" without warranty of any kind, either express or implied. User   _##  hereby grants a royalty-free license to any and all derivatives based  _##  upon this software code base.   _##    _##  Stuttgart, Germany, Tue Nov 21 22:12:16 CET 2006   _##    _##########################################################################*//*===================================================================  Copyright (c) 1996  Hewlett-Packard Company  ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.  Permission to use, copy, modify, distribute and/or sell this software  and/or its documentation is hereby granted without fee. User agrees  to display the above copyright notice and this license notice in all  copies of the software and any documentation of the software. User  agrees to assume all liability for the use of the software; Hewlett-Packard  makes no representations about the suitability of this software for any  purpose. It is provided "AS-IS" without warranty of any kind,either express  or implied. User hereby grants a royalty-free license to any and all  derivatives based upon this software code base.  SNMP++ S N M P M S G . C P P  SNMPMESSAGE CLASS DEFINITION  DESIGN + AUTHOR:  Peter E Mellquist  LANGUAGE:         ANSI C++  DESCRIPTION:      ASN.1	encoding / decoding class=====================================================================*/char snmpmsg_cpp_version[]="#(@) SNMP++ $Id: snmpmsg.cpp,v 1.16 2006/10/12 18:11:48 katz Exp $";#ifdef WIN32#include <winsock.h>#elif _AIX#include <unistd.h>#endif#include <stdio.h>                      // standard io file#include "snmp_pp/snmpmsg.h"                    // header file for SnmpMessage#include "snmp_pp/oid_def.h"                    // changed (Frank Fock)#include "snmp_pp/log.h"#include "snmp_pp/vb.h"#ifdef SNMP_PP_NAMESPACEnamespace Snmp_pp {#endif#define MAX_LEN_COMMUNITY 254const coldStartOid coldStart;const warmStartOid warmStart;const linkDownOid linkDown;const linkUpOid linkUp;const authenticationFailureOid authenticationFailure;const egpNeighborLossOid egpNeighborLoss;const snmpTrapEnterpriseOid snmpTrapEnterprise;//------------[ convert SNMP++ VB to WinSNMP smiVALUE ]----------------int convertVbToSmival( const Vb &tempvb, SmiVALUE *smival ){  smival->syntax = tempvb.get_syntax();  switch ( smival->syntax ) {    // case sNMP_SYNTAX_NULL  case sNMP_SYNTAX_NULL:  case sNMP_SYNTAX_NOSUCHOBJECT:  case sNMP_SYNTAX_NOSUCHINSTANCE:  case sNMP_SYNTAX_ENDOFMIBVIEW:    break;    // case sNMP_SYNTAX_INT32:  case sNMP_SYNTAX_INT:    tempvb.get_value(smival->value.sNumber);    break;    //    case sNMP_SYNTAX_UINT32:  case sNMP_SYNTAX_GAUGE32:  case sNMP_SYNTAX_CNTR32:  case sNMP_SYNTAX_TIMETICKS:    //  case sNMP_SYNTAX_UINT32:    tempvb.get_value(smival->value.uNumber);    break;    // case Counter64  case sNMP_SYNTAX_CNTR64:    {      Counter64 c64;      tempvb.get_value(c64);      smival->value.hNumber.hipart = c64.high();      smival->value.hNumber.lopart = c64.low();    }    break;  case sNMP_SYNTAX_BITS:  case sNMP_SYNTAX_OCTETS:  case sNMP_SYNTAX_OPAQUE:  case sNMP_SYNTAX_IPADDR:    {      OctetStr os;      tempvb.get_value(os);      smival->value.string.ptr = NULL;      smival->value.string.len = os.len();      if ( smival->value.string.len > 0 )      {        smival->value.string.ptr          = (SmiLPBYTE) new  unsigned char [smival->value.string.len];        if ( smival->value.string.ptr )        {          for (int i=0; i<(int) smival->value.string.len ; i++)            smival->value.string.ptr[i] = os[i];        }        else        {          smival->syntax = sNMP_SYNTAX_NULL;  // invalidate the smival          return SNMP_CLASS_RESOURCE_UNAVAIL;        }      }    }    break;  case sNMP_SYNTAX_OID:    {      Oid oid;      tempvb.get_value(oid);      smival->value.oid.ptr = NULL;      smival->value.oid.len = oid.len();      if ( smival->value.oid.len > 0 )      {        smival->value.oid.ptr          = (SmiLPUINT32) new unsigned long [ smival->value.oid.len];        if ( smival->value.oid.ptr )        {          for (int i=0; i<(int)smival->value.oid.len ; i++)            smival->value.oid.ptr[i] = oid[i];        }        else        {          smival->syntax = sNMP_SYNTAX_NULL;  // invalidate the smival          return SNMP_CLASS_RESOURCE_UNAVAIL;        }      }    }    break;  default:    return SNMP_CLASS_INTERNAL_ERROR;  }  return SNMP_CLASS_SUCCESS;}// free a SMI valuevoid freeSmivalDescriptor( SmiVALUE *smival ){  switch ( smival->syntax ) {  case sNMP_SYNTAX_OCTETS:  case sNMP_SYNTAX_OPAQUE:  case sNMP_SYNTAX_IPADDR:  case sNMP_SYNTAX_BITS:		    // obsoleted in SNMPv2 Draft Std    delete [] smival->value.string.ptr;    break;  case sNMP_SYNTAX_OID:    delete [] smival->value.oid.ptr;    break;  }  smival->syntax = sNMP_SYNTAX_NULL;}#ifdef _SNMPv3int SnmpMessage::unloadv3( Pdu &pdu,                // Pdu returned                           snmp_version &version,   // version                           OctetStr &engine_id,     // optional v3                           OctetStr &security_name, // optional v3                           long int &security_model,                           UdpAddress &from_addr,			   Snmp &snmp_session){  OctetStr tmp;  return unload(pdu, tmp, version, &engine_id,                &security_name, &security_model, &from_addr, &snmp_session);}#endifint SnmpMessage::load(const Pdu &cpdu,                      const OctetStr &community,                      const snmp_version version,                      const OctetStr* engine_id,                      const OctetStr* security_name,                      const int security_model){  int status;  const Pdu *pdu = &cpdu;  Pdu temppdu;  // make sure pdu is valid  if ( !pdu->valid())    return SNMP_CLASS_INVALID_PDU;  // create a raw pdu  snmp_pdu *raw_pdu;  raw_pdu = snmp_pdu_create( (int) pdu->get_type());  Oid enterprise;  // load it up  raw_pdu->reqid = pdu->get_request_id();#ifdef _SNMPv3  raw_pdu->msgid = pdu->get_message_id();#endif  raw_pdu->errstat= (unsigned long) pdu->get_error_status();  raw_pdu->errindex= (unsigned long) pdu->get_error_index();  // if its a V1 trap then load up other values  // for v2, use normal pdu format  if (raw_pdu->command == sNMP_PDU_V1TRAP)  {    // DON'T forget about the v1 trap agent address (changed by Frank Fock)    GenAddress gen_addr;    IpAddress ip_addr;    int addr_set = FALSE;    if (pdu->get_v1_trap_address(gen_addr))    {      /* User did set the v1 trap address */      if ((gen_addr.get_type() != Address::type_ip) &&          (gen_addr.get_type() != Address::type_udp) )      {	LOG_BEGIN(ERROR_LOG | 4);	LOG("SNMPMessage: Bad v1 trap address type in pdu");	LOG(gen_addr.get_type());	LOG_END;        snmp_free_pdu( raw_pdu);        return SNMP_CLASS_INVALID_PDU;      }      ip_addr = gen_addr;      if (!ip_addr.valid())      {	LOG_BEGIN(ERROR_LOG | 1);	LOG("SNMPMessage: Copied v1 trap address not valid");	LOG_END;        snmp_free_pdu( raw_pdu);        return SNMP_CLASS_RESOURCE_UNAVAIL;      }      addr_set = TRUE;    }    else    {      /* User did not set the v1 trap address */      char addrString[256];      if (gethostname(addrString, 255) == 0)      {          ip_addr = addrString;          addr_set = TRUE;      }    }    struct sockaddr_in agent_addr;  // agent address socket struct    // prepare the agent address    memset(&agent_addr, 0, sizeof(agent_addr));    agent_addr.sin_family = AF_INET;    if (addr_set)    {      agent_addr.sin_addr.s_addr        = inet_addr(((IpAddress &)ip_addr).IpAddress::get_printable());      LOG_BEGIN(INFO_LOG | 7);      LOG("SNMPMessage: Setting v1 trap address");      LOG(((IpAddress &)ip_addr).IpAddress::get_printable());      LOG_END;    }    raw_pdu->agent_addr = agent_addr;    //-----[ compute generic trap value ]-------------------------------    // determine the generic value    // 0 - cold start    // 1 - warm start    // 2 - link down    // 3 - link up    // 4 - authentication failure    // 5 - egpneighborloss    // 6 - enterprise specific    Oid trapid;    pdu->get_notify_id( trapid);    if ( !trapid.valid() || trapid.len() < 2 )      {        snmp_free_pdu( raw_pdu);        return SNMP_CLASS_INVALID_NOTIFYID;      }    raw_pdu->specific_type=0;    if ( trapid == coldStart)      raw_pdu->trap_type = 0;  // cold start    else if ( trapid == warmStart)      raw_pdu->trap_type = 1;  // warm start    else if( trapid == linkDown)      raw_pdu->trap_type = 2;  // link down    else if ( trapid == linkUp)      raw_pdu->trap_type = 3;  // link up    else if ( trapid == authenticationFailure )      raw_pdu->trap_type = 4;  // authentication failure    else if ( trapid == egpNeighborLoss)      raw_pdu->trap_type = 5;  // egp neighbor loss    else {      raw_pdu->trap_type = 6;     // enterprise specific      // last oid subid is the specific value      // if 2nd to last subid is "0", remove it      // enterprise is always the notify oid prefix      raw_pdu->specific_type = (int) trapid[(int) (trapid.len()-1)];      trapid.trim(1);      if ( trapid[(int)(trapid.len()-1)] == 0 )        trapid.trim(1);      enterprise = trapid;    }    if ( raw_pdu->trap_type !=6)      pdu->get_notify_enterprise( enterprise);    if ( enterprise.len() >0) {      // note!!      // these are hooks into an SNMP++ oid      // and therefor the raw_pdu enterprise      // should not free them. null them out!!      SmiLPOID rawOid;      rawOid = enterprise.oidval();      raw_pdu->enterprise = rawOid->ptr;      raw_pdu->enterprise_length = (int) rawOid->len;    }    // timestamp    TimeTicks timestamp;    pdu->get_notify_timestamp( timestamp);    raw_pdu->time = ( unsigned long) timestamp;  }  // if its a v2 trap then we need to make a few adjustments  // vb #1 is the timestamp  // vb #2 is the id, represented as an Oid  if (( raw_pdu->command == sNMP_PDU_TRAP) ||      ( raw_pdu->command == sNMP_PDU_INFORM))  {    Vb tempvb;    temppdu = *pdu;    temppdu.trim(temppdu.get_vb_count());    // vb #1 is the timestamp    TimeTicks timestamp;    tempvb.set_oid(SNMP_MSG_OID_SYSUPTIME);   // sysuptime    pdu->get_notify_timestamp( timestamp);    tempvb.set_value ( timestamp);    temppdu += tempvb;    // vb #2 is the id    Oid trapid;    tempvb.set_oid(SNMP_MSG_OID_TRAPID);    pdu->get_notify_id( trapid);    tempvb.set_value( trapid);    temppdu += tempvb;    // append the remaining vbs    for (int z=0; z<pdu->get_vb_count(); z++) {      pdu->get_vb( tempvb,z);      temppdu += tempvb;    }    pdu = &temppdu;          // reassign the pdu to the temp one  }  // load up the payload  // for all Vbs in list, add them to the pdu  int vb_count;  Vb tempvb;  Oid tempoid;  SmiLPOID smioid;  SmiVALUE smival;  vb_count = pdu->get_vb_count();  for (int z=0;z<vb_count;z++) {    pdu->get_vb( tempvb,z);    tempvb.get_oid( tempoid);    smioid = tempoid.oidval();    // clear the value portion, in case its    // not already been done so by the app writer    // only do it in the case its a get,next or bulk    if ((raw_pdu->command == sNMP_PDU_GET) ||        (raw_pdu->command == sNMP_PDU_GETNEXT) ||        (raw_pdu->command == sNMP_PDU_GETBULK))

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?