00001 /*_############################################################################ 00002 _## 00003 _## vb.h 00004 _## 00005 _## SNMP++v3.2.15 00006 _## ----------------------------------------------- 00007 _## Copyright (c) 2001-2004 Jochen Katz, Frank Fock 00008 _## 00009 _## This software is based on SNMP++2.6 from Hewlett Packard: 00010 _## 00011 _## Copyright (c) 1996 00012 _## Hewlett-Packard Company 00013 _## 00014 _## ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS. 00015 _## Permission to use, copy, modify, distribute and/or sell this software 00016 _## and/or its documentation is hereby granted without fee. User agrees 00017 _## to display the above copyright notice and this license notice in all 00018 _## copies of the software and any documentation of the software. User 00019 _## agrees to assume all liability for the use of the software; 00020 _## Hewlett-Packard and Jochen Katz make no representations about the 00021 _## suitability of this software for any purpose. It is provided 00022 _## "AS-IS" without warranty of any kind, either express or implied. User 00023 _## hereby grants a royalty-free license to any and all derivatives based 00024 _## upon this software code base. 00025 _## 00026 _## Stuttgart, Germany, Tue Jan 4 21:42:42 CET 2005 00027 _## 00028 _##########################################################################*/ 00029 /*=================================================================== 00030 00031 Copyright (c) 1999 00032 Hewlett-Packard Company 00033 00034 ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS. 00035 Permission to use, copy, modify, distribute and/or sell this software 00036 and/or its documentation is hereby granted without fee. User agrees 00037 to display the above copyright notice and this license notice in all 00038 copies of the software and any documentation of the software. User 00039 agrees to assume all liability for the use of the software; Hewlett-Packard 00040 makes no representations about the suitability of this software for any 00041 purpose. It is provided "AS-IS without warranty of any kind,either express 00042 or implied. User hereby grants a royalty-free license to any and all 00043 derivatives based upon this software code base. 00044 00045 00046 SNMP++ V B . H 00047 00048 VARIABLE BINDING CLASS DEFINITION 00049 00050 DESCRIPTION: 00051 This module contains the class definition for the variable binding 00052 class. The VB class is an encapsulation of a SNMP VB. A VB object is 00053 composed of an SNMP++ Oid and an SMI value. The Vb class utilizes Oid 00054 objects and thus requires the Oid class. The Vb class may be used 00055 stand alone and does not require use of any other snmp library. 00056 00057 00058 DESIGN + AUTHOR: 00059 Peter E. Mellquist 00060 00061 LANGAUGE: 00062 ANSI C++ 00063 00064 OPERATING SYSTEM: 00065 MS-Windows Win32 00066 BSD UNIX 00067 00068 =====================================================================*/ 00069 // $Id: vb.h,v 1.5 2004/06/20 18:49:21 katz Exp $ 00070 00071 #ifndef _VB_CLS 00072 #define _VB_CLS 00073 00074 #include "snmp_pp/oid.h" // oid class def 00075 #include "snmp_pp/timetick.h" // time ticks 00076 #include "snmp_pp/counter.h" // counter 00077 #include "snmp_pp/gauge.h" // gauge class 00078 #include "snmp_pp/ctr64.h" // 64 bit counters 00079 #include "snmp_pp/octet.h" // octet class 00080 #include "snmp_pp/address.h" // address class def 00081 #include "snmp_pp/integer.h" // integer class 00082 #include "snmp_pp/snmperrs.h" 00083 00084 #ifdef SNMP_PP_NAMESPACE 00085 namespace Snmp_pp { 00086 #endif 00087 00088 00089 //------------[ VB Class Def ]------------------------------------- 00090 /** 00091 * The Vb class is the encapsulation of the SNMP variable binding. 00092 * 00093 * Variable binding lists in SNMP++ are represented as arrays of Vb 00094 * objects. Vb objects are passed to and from SNMP objects to provide 00095 * getting or setting MIB values. The vb class keeps its own memory 00096 * for objects and does not utilize pointers to external data 00097 * structures. 00098 */ 00099 class DLLOPT Vb 00100 { 00101 //-----[ public members ] 00102 public: 00103 00104 //-----[ constructors / destructors ]------------------------------- 00105 00106 /** 00107 * Constructor with no arguments. 00108 * 00109 * This constructor creates an unitialized vb. 00110 */ 00111 Vb() : iv_vb_value(0), exception_status(SNMP_CLASS_SUCCESS) {}; 00112 00113 /** 00114 * Constructor to initialize the oid. 00115 * 00116 * This constructor creates a vb with oid portion initialized. 00117 */ 00118 Vb(const Oid &oid) 00119 : iv_vb_oid(oid), iv_vb_value(0), exception_status(SNMP_CLASS_SUCCESS) {}; 00120 00121 /** 00122 * Copy constructor. 00123 */ 00124 Vb(const Vb &vb) : iv_vb_value(0) { *this = vb; }; 00125 00126 /** 00127 * Destructor that frees all allocated memory. 00128 */ 00129 ~Vb() { free_vb(); }; 00130 00131 /** 00132 * Overloaded assignment operator. 00133 */ 00134 Vb& operator=(const Vb &vb); 00135 00136 /** 00137 * Clone operator. 00138 */ 00139 Vb *clone( ) const { return new Vb(*this); }; 00140 00141 //-----[ set oid / get oid ]------------------------------------------ 00142 00143 /** 00144 * Set the oid from another oid. 00145 */ 00146 void set_oid(const Oid &oid) { iv_vb_oid = oid; }; 00147 00148 /** 00149 * Get the oid portion. 00150 * 00151 * @note Check the validity of the object through Vb::valid() before 00152 * calling this method. 00153 */ 00154 void get_oid(Oid &oid) const { oid = iv_vb_oid; }; 00155 00156 /** 00157 * Get the oid portion as a const. 00158 * 00159 * @note Check the validity of the object through Vb::valid() before 00160 * calling this method. 00161 */ 00162 const Oid &get_oid() const { return iv_vb_oid; }; 00163 00164 //-----[ set value ]-------------------------------------------------- 00165 00166 /** 00167 * Set the value using any SnmpSyntax object. 00168 */ 00169 void set_value(const SnmpSyntax &val) 00170 { free_vb(); iv_vb_value = val.clone(); }; 00171 00172 /** 00173 * Set the value with an int. 00174 * 00175 * The syntax of the Vb will be set to SMI INT32. 00176 */ 00177 void set_value(const int i) { free_vb(); iv_vb_value = new SnmpInt32(i); }; 00178 00179 /** 00180 * Set the value with an int. 00181 * 00182 * The syntax of the Vb will be set to SMI INT32. 00183 */ 00184 void set_value(const long i) 00185 { free_vb(); iv_vb_value = new SnmpInt32(i); }; 00186 00187 /** 00188 * Set the value with an unsigned long int. 00189 * 00190 * The syntax of the Vb will be set to SMI UINT32. 00191 */ 00192 void set_value(const unsigned long i) 00193 { free_vb(); iv_vb_value = new SnmpUInt32(i); }; 00194 00195 /** 00196 * Set value using a null terminated string. 00197 * 00198 * The syntax of the Vb will be set to SMI octet. 00199 */ 00200 void set_value(const char *ptr) 00201 { free_vb(); iv_vb_value = new OctetStr(ptr); }; 00202 00203 /** 00204 * Set value using a string and length. 00205 * 00206 * The syntax of the Vb will be set to SMI octet. 00207 */ 00208 void set_value(const unsigned char *ptr, const unsigned int len) 00209 { free_vb(); iv_vb_value = new OctetStr(ptr, len); }; 00210 00211 /** 00212 * Set the value portion of the vb to null, if its not already. 00213 */ 00214 void set_null() { free_vb(); }; 00215 00216 //----[ get value ]------------------------------------------------ 00217 00218 /** 00219 * Get the value using a SnmpSyntax object. 00220 * 00221 * @param val - An object of a subclass of SnmpSyntax that will be 00222 * assigned the value of the vb. 00223 * 00224 * @return SNMP_CLASS_SUCCESS if the vb value could be assigned to 00225 * the passed SnmpSyntax object, else SNMP_CLASS_INVALID. 00226 */ 00227 int get_value(SnmpSyntax &val) const; 00228 00229 /** 00230 * Get the value. 00231 * 00232 * This method will only return success if the value of the vb is SMI INT32. 00233 * 00234 * @param i - returned value 00235 * 00236 * @return SNMP_CLASS_SUCCESS on success, else SNMP_CLASS_INVALID. 00237 */ 00238 int get_value(int &i) const; 00239 00240 /** 00241 * Get the value. 00242 * 00243 * This method will only return success if the value of the vb is SMI INT32. 00244 * 00245 * @param i - returned value 00246 * 00247 * @return SNMP_CLASS_SUCCESS on success, else SNMP_CLASS_INVALID. 00248 */ 00249 int get_value(long &i) const; 00250 00251 /** 00252 * Get the value. 00253 * 00254 * This method will only return success if the value of the vb can 00255 * be mapped to an unsigned long (SMI types uint32, counter32, gauge 00256 * and timeticks). 00257 * 00258 * @param i - returned value 00259 * 00260 * @return SNMP_CLASS_SUCCESS on success, else SNMP_CLASS_INVALID. 00261 */ 00262 int get_value(unsigned long &i) const; 00263 00264 /** 00265 * Get the value. 00266 * 00267 * This method will only return success if the value of the vb is SMI OCTET. 00268 * 00269 * @note The caller must provide a target string big enough to 00270 * handle the vb string. No length checks are done within 00271 * this method. 00272 * 00273 * @param ptr - Pointer to already allocated space to hold the vb 00274 * value. The first char will be set to zero on failure. 00275 * @param len - Returned length of the string. Will be set to 0 on failure. 00276 * 00277 * @return SNMP_CLASS_SUCCESS on success, else SNMP_CLASS_INVALID. 00278 */ 00279 int get_value(unsigned char *ptr, unsigned long &len) const; 00280 00281 /** 00282 * Get the value. 00283 * 00284 * This method will only return success if the value of the vb is SMI OCTET. 00285 * 00286 * @note If the target space is not big enough to hold the complete 00287 * string only maxlen bytes are copied. In any case the returned 00288 * string is NOT null terminated. 00289 * 00290 * @param ptr - Pointer to already allocated space to hold the vb 00291 * value. The first char will be set to zero on failure. 00292 * @param len - Returned length of the string. Will be set to 0 00293 * on failure. 00294 * @param maxlen - Maximum length of the space that ptr points to. 00295 * 00296 * @return SNMP_CLASS_SUCCESS on success, else SNMP_CLASS_INVALID. 00297 */ 00298 int get_value(unsigned char *ptr, 00299 unsigned long &len, 00300 const unsigned long maxlen) const; 00301 00302 /** 00303 * Get the value. 00304 * 00305 * This method will only return success if the value of the vb is SMI OCTET. 00306 * 00307 * @note The caller must provide a target string big enough to 00308 * handle the vb string. No length checks are done within 00309 * this method. The returned string will be null terminated. 00310 * 00311 * @param ptr - Pointer to already allocated space to hold the vb 00312 * value. The first char will be set to zero on failure. 00313 * 00314 * @return SNMP_CLASS_SUCCESS on success, else SNMP_CLASS_INVALID. 00315 */ 00316 int get_value(char *ptr) const; 00317 00318 //-----[ misc]-------------------------------------------------------- 00319 00320 /** 00321 * Return the syntax or the exception status. 00322 * 00323 * @return If the SNMPv2 exception status is set, it is returned. 00324 * otherwise the syntax of the value object is returned. 00325 */ 00326 SmiUINT32 get_syntax() const; 00327 00328 /** 00329 * Set the syntax. 00330 * 00331 * The Value portion of the Vb will be deleted and a new value portion 00332 * is allocated with it's default value (zero). 00333 * 00334 * @param syntax - The new syntax. 00335 */ 00336 void set_syntax(const SmiUINT32 syntax); 00337 00338 /** 00339 * Set the exception status. 00340 * 00341 * @param status - the new SNMPv2 exception status. 00342 */ 00343 void set_exception_status(const SmiUINT32 status) 00344 { exception_status = status; }; 00345 00346 //! deprecated! Use Vb::set_exception_status() 00347 DLLOPT friend void set_exception_status(Vb *vb, const SmiUINT32 status); 00348 00349 /** 00350 * Return a formatted version of the value. 00351 * 00352 * @return A null terminated string (empty if no value). 00353 */ 00354 const char *get_printable_value() const; 00355 00356 /** 00357 * Return a formatted version of the Oid. 00358 * 00359 * @return A null terminated string (may be empty if no Oid has been set). 00360 */ 00361 const char *get_printable_oid() const 00362 { return iv_vb_oid.get_printable(); }; 00363 00364 /** 00365 * Return the validity of a Vb object. 00366 * 00367 * @return TRUE if oid and value have been set. 00368 */ 00369 bool valid() const; 00370 00371 /** 00372 * Return the space needed for serialization. 00373 * 00374 * @return the length of the BER encoding of this Vb. 00375 */ 00376 int get_asn1_length() const; 00377 00378 /** 00379 * Reset the object. 00380 */ 00381 void clear() { free_vb(); iv_vb_oid.clear(); }; 00382 00383 //-----[ protected members ] 00384 protected: 00385 Oid iv_vb_oid; // a vb is made up of a oid 00386 SnmpSyntax *iv_vb_value; // and a value... 00387 SmiUINT32 exception_status; // are there any vb exceptions?? 00388 00389 /** 00390 * Free the value portion. 00391 */ 00392 void free_vb(); 00393 }; 00394 00395 #ifdef SNMP_PP_NAMESPACE 00396 }; // end of namespace Snmp_pp 00397 #endif 00398 00399 #endif
1.3.2