⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vb.cpp

📁 SNMP++程序源码 for ll .8snmp++2_8.tar.Z 嵌入式linux环境下的SNMP开发代码
💻 CPP
字号:
/*===================================================================  Copyright (c) 1999  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.   V B . C P P  VARIABLE BINDING CLASS IMPLEMENTATION  DESCRIPTION:  This module contains the class implementation of the VB class.  The Vb class is an encapsulation of the snmp variable binding.  DESIGN:  Peter E Mellquist  AUTHOR:  Peter E Mellquist  LANGAUGE:  ANSI C++  OPERATING SYSTEMS:  MS-Windows Win32  BSD UNIX=====================================================================*/char vb_cpp_version[]="#(@) SNMP++ 2.8 $Header: vb.cpp,v 1.19 96/09/11 14:02:18 hmgr Exp $";#include "oid.h"	       // include oid class defs#include "vb.h"		       // include vb class defs#include "snmperrs.h"      // error codes#define IP_ADDR_SIZE  4#define IPX_ADDR_SIZE 10#define MAC_ADDR_SIZE 6//---------------[ Vb::Vb( void) ]--------------------------------------// constructor with no arguments// makes an vb, unitializedVb::Vb( void)  { iv_vb_value = NULL;    exception_status = SNMP_CLASS_SUCCESS; };//---------------[ Vb::Vb( const Oid &oid) ]-----------------------------// constructor to initialize the oid// makes a vb with oid portion initializedVb::Vb( const Oid &oid){ iv_vb_oid = oid;  iv_vb_value = NULL;  exception_status = SNMP_CLASS_SUCCESS;  };//---------------[ Vb::Vb( const Vb &vb) ]-----------------------------// copy constructorVb::Vb( const Vb &vb){  iv_vb_value = NULL;   *this = vb; };//--------------[ Vb::valid() ]-----------------------------------------// returns validity of a Vb object// must have a valid oid and valueint Vb::valid() const{#ifdef WHEN_WE_HAVE_SNMPNULL_CLASS   if ( iv_vb_oid.valid() && iv_vb_value && iv_vb_value->valid() )#else  // WHEN_WE_HAVE_SNMPNULL_CLASS   if ( iv_vb_oid.valid() && 	((iv_vb_value == NULL) || (iv_vb_value && iv_vb_value->valid())) )#endif // WHEN_WE_HAVE_SNMPNULL_CLASS     return TRUE;   else     return FALSE;}//---------------[ Vb::~Vb() ]------------------------------------------// destructor// if the vb has a oid or an octect string then// the associated memory needs to be freedVb::~Vb(){ free_vb(); };//---------------[ Vb& Vb::operator=( const Vb &vb) ]--------------------// overloaded assignment allows assigning one Vb to another// this involves deep memory thus target vb needs to be freed// before assigning sourceVb& Vb::operator=( const Vb &vb){   free_vb();	 // free up target to begin with   //-----[ reassign the Oid portion 1st ]   vb.get_oid( iv_vb_oid);   //-----[ next set the vb value portion ]   if (vb.iv_vb_value == NULL){     iv_vb_value = NULL;   }   else{     iv_vb_value = vb.iv_vb_value->clone();   }   exception_status = vb.exception_status;   return *this; // return self reference};//---------------[ Vb::set_oid( const Oid oid ) ]-----------------------// set value oid only with another oidvoid Vb::set_oid( const Oid oid){ iv_vb_oid = oid;};//---------------[ Vb::get_oid( Oid &oid) ]-----------------------------// get oid portionvoid Vb::get_oid( Oid &oid) const{ oid = iv_vb_oid;};//----------------[ void Vb::free_vb() ]--------------------------------// protected method to free memory// this methos is used to free memory when assigning new vbs// or destructing// in the case of oids and octets, we need to do a deep freevoid Vb::free_vb(){  if (iv_vb_value)    delete iv_vb_value;  exception_status = SNMP_CLASS_SUCCESS;  iv_vb_value = NULL;};//---------------[ Vb::set_value( int i) ]-------------------------------// set the value with an int// C++ int maps to SMI intvoid Vb::set_value( const int i){  free_vb();  iv_vb_value = new SnmpInt32(i);};//----------------[ Vb::set_value( long int i) ]--------------------------// set the value with a long signed int// C++ long int maps to SMI int 32void Vb::set_value( const long int i){  free_vb();  iv_vb_value = new SnmpInt32(i);}; // set a Vb null, if its not already void Vb::set_null() {    free_vb(); };//-----------------[ Vb::set_value( unsigned long int i) ]------------------// set the value with an unsigned long int// C++ unsigned long int maps to SMI UINT32void Vb::set_value( const unsigned long int i){  free_vb();  iv_vb_value = new SnmpUInt32(i);};//------------[Vb::set_value ( general value) ]----------------------------void Vb::set_value ( const SnmpSyntax &val){  free_vb();  iv_vb_value = val.clone();};//--------------[ Vb::set_value( char WINFAR * ptr) ]-------------------// set value on a string// makes the string an octet// this must be a null terminates stringvoid Vb::set_value( const char WINFAR * ptr){  free_vb();  iv_vb_value = new OctetStr(ptr);};//---------------------[ Vb::get_value( int &i) ]----------------------// get value int// returns 0 on success and valueint Vb::get_value( int &i){   if (iv_vb_value &&       iv_vb_value->valid() &&       (iv_vb_value->get_syntax() == sNMP_SYNTAX_INT ))   {     long lval;     lval = *((SnmpInt32 *)iv_vb_value);// SnmpInt32 includes cast to long,     i = (int) lval;				    // but not to int.     return SNMP_CLASS_SUCCESS;   }   else     return SNMP_CLASS_INVALID;};//--------------[ Vb::get_value( long int &i) ]-------------------------// get the signed long int// returns 0 on success and a valueint Vb::get_value( long int &i){   if (iv_vb_value &&       iv_vb_value->valid() &&       (iv_vb_value->get_syntax() == sNMP_SYNTAX_INT32 ))   {     i = *((SnmpInt32 *)iv_vb_value);	// SnmpInt32 includes cast to long     return SNMP_CLASS_SUCCESS;   }   else    return SNMP_CLASS_INVALID;};//-----------------[  Vb::get_value( unsigned long int &i) ]--------------// get the unsigned long int// returns 0 on success and a valueint Vb::get_value( unsigned long int &i){   if (iv_vb_value &&       iv_vb_value->valid() &&       ((iv_vb_value->get_syntax() == sNMP_SYNTAX_UINT32 ) ||	(iv_vb_value->get_syntax() == sNMP_SYNTAX_CNTR32 ) ||	(iv_vb_value->get_syntax() == sNMP_SYNTAX_GAUGE32 ) ||	(iv_vb_value->get_syntax() == sNMP_SYNTAX_TIMETICKS )))   {     i = *((SnmpUInt32 *)iv_vb_value);	// SnmpUint32 has includes to ulong     return SNMP_CLASS_SUCCESS;   }   else     return SNMP_CLASS_INVALID;};//--------------[ Vb::get_value( unsigned char WINFAR * ptr, unsigned long &len)// get a unsigned char string value// destructive, copies into given ptr// also returned is the length//// Note!! The user must provide a target string// which is big enough to hold the stringint Vb::get_value( unsigned char WINFAR * ptr, unsigned long &len){   if (iv_vb_value &&       iv_vb_value->valid() &&       ( iv_vb_value->get_syntax() == sNMP_SYNTAX_OCTETS))   {     OctetStr *p_os = (OctetStr *)iv_vb_value;     len = p_os->len();     for ( int i=0; i<(int)len ; i++ ) {	   ptr[i] = (*p_os)[i];		// use OctetStr[] for "raw" access     }     return SNMP_CLASS_SUCCESS;   }   else   {     ptr[0] = '0';     len = 0;     return SNMP_CLASS_INVALID;   }};//---------------[ Vb::get_value ]-------------------------------------// get an unsigned char array// caller specifies max len of target spaceint Vb::get_value( unsigned char WINFAR * ptr,	// pointer to target space		   unsigned long &len,		// returned len		   unsigned long maxlen)	// max len of target space{   if ( iv_vb_value &&        iv_vb_value->valid() &&	(iv_vb_value->get_syntax() == sNMP_SYNTAX_OCTETS))   {     OctetStr *p_os = (OctetStr *)iv_vb_value;     len = p_os->len();     if (len > maxlen) len = maxlen;     for ( int i=0; i<(int)len ; i++ ) {	   ptr[i] = (*p_os)[i];		// use OctetStr[] for "raw" access     }     return SNMP_CLASS_SUCCESS;   }   else   {     ptr[0] = '0';     len = 0;     return SNMP_CLASS_INVALID;   }};//---------------[ Vb::get_value( Value &val) ]--------int Vb::get_value( SnmpSyntax &val){  if (iv_vb_value){    val = *iv_vb_value;    if (val.valid())	return SNMP_CLASS_SUCCESS;    else	return SNMP_CLASS_INVALID;  }  else  {//TM: should set val to be invalid    return SNMP_CLASS_INVALID;  }};//--------------[ Vb::get_value( char WINFAR *ptr) ]-------------------// get a char * from an octet string// the user must provide space or// memory will be stepped onint Vb::get_value( char WINFAR *ptr){   if (iv_vb_value &&       iv_vb_value->valid() &&       ( iv_vb_value->get_syntax() == sNMP_SYNTAX_OCTETS))   {     OctetStr *p_os = (OctetStr *)iv_vb_value;     unsigned long len = p_os->len();     for ( int i=0; i<(int)len ; i++ ) {	   ptr[i] = (*p_os)[i];		// use OctetStr[] for "raw" access     }     ptr[len] = 0;     return SNMP_CLASS_SUCCESS;   }   else     return SNMP_CLASS_INVALID;};//-----[ misc]--------------------------------------------------------// return the current syntax// This method violates Object Orientation but may be useful if// the caller has a vb object and does not know what it is.// This would be useful in the implementation of a browser.SmiUINT32 Vb::get_syntax(){ 	if ( exception_status != SNMP_CLASS_SUCCESS)		return exception_status;	else        return ( iv_vb_value ? iv_vb_value->get_syntax() : sNMP_SYNTAX_NULL);};static char blank_string[] = "";// return the printabel valuechar WINFAR *Vb::get_printable_value(){  if (iv_vb_value)    return iv_vb_value->get_printable();  else    return blank_string;}// return the printable oidchar WINFAR *Vb::get_printable_oid(){ return iv_vb_oid.get_printable(); };// friend function to set exception statusvoid set_exception_status( Vb *vb, const SmiUINT32 status){   vb->exception_status = status;};

⌨️ 快捷键说明

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