1 /*_############################################################################
 
   6   _##  -----------------------------------------------
 
   7   _##  Copyright (c) 2001-2010 Jochen Katz, Frank Fock
 
   9   _##  This software is based on SNMP++2.6 from Hewlett Packard:
 
  11   _##    Copyright (c) 1996
 
  12   _##    Hewlett-Packard Company
 
  14   _##  ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
 
  15   _##  Permission to use, copy, modify, distribute and/or sell this software 
 
  16   _##  and/or its documentation is hereby granted without fee. User agrees 
 
  17   _##  to display the above copyright notice and this license notice in all 
 
  18   _##  copies of the software and any documentation of the software. User 
 
  19   _##  agrees to assume all liability for the use of the software; 
 
  20   _##  Hewlett-Packard and Jochen Katz make no representations about the 
 
  21   _##  suitability of this software for any purpose. It is provided 
 
  22   _##  "AS-IS" without warranty of any kind, either express or implied. User 
 
  23   _##  hereby grants a royalty-free license to any and all derivatives based
 
  24   _##  upon this software code base. 
 
  26   _##  Stuttgart, Germany, Thu Sep  2 00:07:47 CEST 2010 
 
  28   _##########################################################################*/
 
  29 /*===================================================================
 
  32   Hewlett-Packard Company
 
  34   ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
 
  35   Permission to use, copy, modify, distribute and/or sell this software
 
  36   and/or its documentation is hereby granted without fee. User agrees
 
  37   to display the above copyright notice and this license notice in all
 
  38   copies of the software and any documentation of the software. User
 
  39   agrees to assume all liability for the use of the software; Hewlett-Packard
 
  40   makes no representations about the suitability of this software for any
 
  41   purpose. It is provided "AS-IS" without warranty of any kind,either express
 
  42   or implied. User hereby grants a royalty-free license to any and all
 
  43   derivatives based upon this software code base.
 
  48   VARIABLE BINDING CLASS IMPLEMENTATION
 
  51   This module contains the class implementation of the VB class.
 
  52   The Vb class is an encapsulation of the snmp variable binding.
 
  54   DESIGN + AUTHOR:  Peter E Mellquist
 
  55 =====================================================================*/
 
  56 char vb_cpp_version[]="#(@) SNMP++ $Id: vb.cpp 341 2008-08-29 21:37:48Z katz $";
 
  58 #include "snmp_pp/vb.h"            // include vb class defs
 
  60 #ifdef SNMP_PP_NAMESPACE
 
  64 #define IP_ADDR_SIZE  4
 
  65 #define IPX_ADDR_SIZE 10
 
  66 #define MAC_ADDR_SIZE 6
 
  68 //--------------[ Vb::valid() ]-----------------------------------------
 
  69 // returns validity of a Vb object
 
  70 // must have a valid oid and value
 
  71 bool Vb::valid() const
 
  73   if (iv_vb_oid.valid() &&
 
  74 #ifdef WHEN_WE_HAVE_SNMPNULL_CLASS
 
  75       iv_vb_value && iv_vb_value->valid()
 
  77       ((iv_vb_value == NULL) || (iv_vb_value && iv_vb_value->valid()))
 
  84 //---------------[ Vb& Vb::operator=(const Vb &vb) ]--------------------
 
  85 // overloaded assignment allows assigning one Vb to another
 
  86 // this involves deep memory thus target vb needs to be freed
 
  87 // before assigning source
 
  88 Vb& Vb::operator=(const Vb &vb)
 
  90   if (this == &vb) return *this;  // check for self assignment
 
  92   free_vb(); // free up target to begin with
 
  94   //-----[ reassign the Oid portion 1st ]
 
  95   vb.get_oid(iv_vb_oid);
 
  97   //-----[ next set the vb value portion ]
 
  99     iv_vb_value = vb.iv_vb_value->clone();
 
 101   exception_status = vb.exception_status;
 
 103   return *this; // return self reference
 
 106 //----------------[ void Vb::free_vb() ]--------------------------------
 
 107 // protected method to free memory
 
 108 // this method is used to free memory when assigning new vbs
 
 110 // in the case of oids and octets, we need to do a deep free
 
 118   exception_status = SNMP_CLASS_SUCCESS;
 
 121 //---------------------[ Vb::get_value(int &i) ]----------------------
 
 123 // returns 0 on success and value
 
 124 int Vb::get_value(int &i) const
 
 127        iv_vb_value->valid() &&
 
 128        (iv_vb_value->get_syntax() == sNMP_SYNTAX_INT32 ))
 
 131      lval = *((SnmpInt32 *)iv_vb_value);// SnmpInt32 includes cast to long,
 
 132      i = (int) lval;                    // but not to int.
 
 133      return SNMP_CLASS_SUCCESS;
 
 135    return SNMP_CLASS_INVALID;
 
 138 // get the unsigned int
 
 139 // returns 0 on success and a value
 
 140 int Vb::get_value(unsigned int &i) const
 
 143       iv_vb_value->valid() &&
 
 144       ((iv_vb_value->get_syntax() == sNMP_SYNTAX_UINT32 ) ||
 
 145        (iv_vb_value->get_syntax() == sNMP_SYNTAX_CNTR32 ) ||
 
 146        (iv_vb_value->get_syntax() == sNMP_SYNTAX_GAUGE32 ) ||
 
 147        (iv_vb_value->get_syntax() == sNMP_SYNTAX_TIMETICKS )))
 
 150     lval = *((SnmpUInt32 *)iv_vb_value);
 
 151     i = (unsigned int)lval;
 
 152     return SNMP_CLASS_SUCCESS;
 
 154   return SNMP_CLASS_INVALID;
 
 158 //--------------[ Vb::get_value(long int &i) ]-------------------------
 
 159 // get the signed long int
 
 160 // returns 0 on success and a value
 
 161 int Vb::get_value(long &i) const
 
 164        iv_vb_value->valid() &&
 
 165        (iv_vb_value->get_syntax() == sNMP_SYNTAX_INT32 ))
 
 167      i = *((SnmpInt32 *)iv_vb_value);   // SnmpInt32 includes cast to long
 
 168      return SNMP_CLASS_SUCCESS;
 
 170    return SNMP_CLASS_INVALID;
 
 174 //-----------------[  Vb::get_value(unsigned long int &i) ]--------------
 
 175 // get the unsigned long int
 
 176 // returns 0 on success and a value
 
 177 int Vb::get_value(unsigned long &i) const
 
 180       iv_vb_value->valid() &&
 
 181       ((iv_vb_value->get_syntax() == sNMP_SYNTAX_UINT32 ) ||
 
 182        (iv_vb_value->get_syntax() == sNMP_SYNTAX_CNTR32 ) ||
 
 183        (iv_vb_value->get_syntax() == sNMP_SYNTAX_GAUGE32 ) ||
 
 184        (iv_vb_value->get_syntax() == sNMP_SYNTAX_TIMETICKS )))
 
 186     i = *((SnmpUInt32 *)iv_vb_value);   // SnmpUint32 has includes to ulong
 
 187     return SNMP_CLASS_SUCCESS;
 
 189   return SNMP_CLASS_INVALID;
 
 193 //--------------[ Vb::get_value(unsigned char WINFAR * ptr, unsigned long &len)
 
 194 // get a unsigned char string value
 
 195 // destructive, copies into given ptr
 
 196 // also returned is the length
 
 198 // Note!! The user must provide a target string
 
 199 // which is big enough to hold the string
 
 200 int Vb::get_value(unsigned char *ptr, unsigned long &len) const
 
 203       iv_vb_value->valid() &&
 
 204       (iv_vb_value->get_syntax() == sNMP_SYNTAX_OCTETS))
 
 206     OctetStr *p_os = (OctetStr *)iv_vb_value;
 
 208     memcpy(ptr, p_os->data(), len);
 
 210     return SNMP_CLASS_SUCCESS;
 
 215   return SNMP_CLASS_INVALID;
 
 218 //---------------[ Vb::get_value ]-------------------------------------
 
 219 // get an unsigned char array
 
 220 // caller specifies max len of target space
 
 221 int Vb::get_value(unsigned char *ptr, unsigned long &len,
 
 222                   const unsigned long maxlen,
 
 223                   const bool add_null_byte) const
 
 226       iv_vb_value->valid() &&
 
 227       (iv_vb_value->get_syntax() == sNMP_SYNTAX_OCTETS) &&
 
 230     OctetStr *p_os = (OctetStr *)iv_vb_value;
 
 232     if (len > maxlen) len = maxlen;
 
 233     memcpy(ptr, p_os->data(), len);
 
 241     return SNMP_CLASS_SUCCESS;
 
 246   return SNMP_CLASS_INVALID;
 
 250 //---------------[ Vb::get_value(Value &val) ]--------
 
 251 int Vb::get_value(SnmpSyntax &val) const
 
 257       return SNMP_CLASS_SUCCESS;
 
 258     return SNMP_CLASS_INVALID;
 
 260   // TM: should set val to be invalid
 
 261   return SNMP_CLASS_INVALID;
 
 264 //--------------[ Vb::get_value(char WINFAR *ptr) ]-------------------
 
 265 // get a char * from an octet string
 
 266 // the user must provide space or
 
 267 // memory will be stepped on
 
 268 int Vb::get_value(char *ptr) const
 
 271       iv_vb_value->valid() &&
 
 272       (iv_vb_value->get_syntax() == sNMP_SYNTAX_OCTETS))
 
 274     OctetStr *p_os = (OctetStr *)iv_vb_value;
 
 275     unsigned long len = p_os->len();
 
 276     memcpy(ptr, p_os->data(), len);
 
 278     return SNMP_CLASS_SUCCESS;
 
 282   return SNMP_CLASS_INVALID;
 
 287 //-----[ misc]--------------------------------------------------------
 
 289 // return the current syntax
 
 290 // This method violates Object Orientation but may be useful if
 
 291 // the caller has a vb object and does not know what it is.
 
 292 // This would be useful in the implementation of a browser.
 
 293 SmiUINT32 Vb::get_syntax() const
 
 295   if (exception_status != SNMP_CLASS_SUCCESS)
 
 296     return exception_status;
 
 298     return (iv_vb_value ? iv_vb_value->get_syntax() : sNMP_SYNTAX_NULL);
 
 301 void Vb::set_syntax(const SmiUINT32 syntax)
 
 303         free_vb(); // setting to SNMP_SYNTAX_NULL
 
 305         exception_status = SNMP_CLASS_SUCCESS;
 
 308         case sNMP_SYNTAX_INT32:
 
 309                 iv_vb_value = new SnmpInt32();
 
 311         case sNMP_SYNTAX_TIMETICKS:
 
 312                 iv_vb_value = new TimeTicks();
 
 314         case sNMP_SYNTAX_CNTR32:
 
 315                 iv_vb_value = new Counter32();
 
 317         case sNMP_SYNTAX_GAUGE32:
 
 318                 iv_vb_value = new Gauge32();
 
 320 /* Not distinguishable from Gauge32
 
 321         case sNMP_SYNTAX_UINT32:
 
 322                 iv_vb_value = new SnmpUInt32();
 
 325         case sNMP_SYNTAX_CNTR64:
 
 326                 iv_vb_value = new Counter64();
 
 328         case sNMP_SYNTAX_BITS:
 
 329         case sNMP_SYNTAX_OCTETS:
 
 330                 iv_vb_value = new OctetStr();
 
 332         case sNMP_SYNTAX_OPAQUE:
 
 333                 iv_vb_value = new OpaqueStr();
 
 335         case sNMP_SYNTAX_IPADDR:
 
 336                 iv_vb_value = new IpAddress();
 
 338         case sNMP_SYNTAX_OID:
 
 339                 iv_vb_value = new Oid();
 
 341         case sNMP_SYNTAX_NULL:
 
 343         case sNMP_SYNTAX_NOSUCHINSTANCE:
 
 344                 exception_status = sNMP_SYNTAX_NOSUCHINSTANCE;
 
 346         case sNMP_SYNTAX_NOSUCHOBJECT:
 
 347                 exception_status = sNMP_SYNTAX_NOSUCHOBJECT;
 
 349         case sNMP_SYNTAX_ENDOFMIBVIEW:
 
 350                 exception_status = sNMP_SYNTAX_ENDOFMIBVIEW;
 
 352         case sNMP_SYNTAX_SEQUENCE:
 
 357 static char blank_string[] = "";
 
 359 // return the printabel value
 
 360 const char WINFAR *Vb::get_printable_value() const
 
 363     return iv_vb_value->get_printable();
 
 367 int Vb::get_asn1_length() const
 
 369   // Header for vbs is always 4 Bytes! FIXME
 
 371     return iv_vb_oid.get_asn1_length() + iv_vb_value->get_asn1_length() + 4;
 
 373   return iv_vb_oid.get_asn1_length() + 2 + 4;
 
 376 #ifdef SNMP_PP_NAMESPACE
 
 377 }; // end of namespace Snmp_pp