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 DEFINITION
51 This module contains the class definition for the variable binding
52 class. The VB class is an encapsulation of a SNMP VB. A VB object is
53 composed of an SNMP++ Oid and an SMI value. The Vb class utilizes Oid
54 objects and thus requires the Oid class. The Vb class may be used
55 stand alone and does not require use of any other snmp library.
57 DESIGN + AUTHOR: Peter E. Mellquist
59 =====================================================================*/
60 // $Id: vb.h 1541 2009-05-29 11:29:22Z katz $
65 #include "snmp_pp/oid.h" // oid class def
66 #include "snmp_pp/timetick.h" // time ticks
67 #include "snmp_pp/counter.h" // counter
68 #include "snmp_pp/gauge.h" // gauge class
69 #include "snmp_pp/ctr64.h" // 64 bit counters
70 #include "snmp_pp/octet.h" // octet class
71 #include "snmp_pp/address.h" // address class def
72 #include "snmp_pp/integer.h" // integer class
73 #include "snmp_pp/snmperrs.h"
75 #ifdef SNMP_PP_NAMESPACE
80 //------------[ VB Class Def ]-------------------------------------
82 * The Vb class is the encapsulation of the SNMP variable binding.
84 * Variable binding lists in SNMP++ are represented as arrays of Vb
85 * objects. Vb objects are passed to and from SNMP objects to provide
86 * getting or setting MIB values. The vb class keeps its own memory
87 * for objects and does not utilize pointers to external data
92 //-----[ public members ]
95 //-----[ constructors / destructors ]-------------------------------
98 * Constructor with no arguments.
100 * This constructor creates an unitialized vb.
102 Vb() : iv_vb_value(0), exception_status(SNMP_CLASS_SUCCESS) {};
105 * Constructor to initialize the oid.
107 * This constructor creates a vb with oid portion initialized.
110 : iv_vb_oid(oid), iv_vb_value(0), exception_status(SNMP_CLASS_SUCCESS) {};
115 Vb(const Vb &vb) : iv_vb_value(0) { *this = vb; };
118 * Destructor that frees all allocated memory.
120 ~Vb() { free_vb(); };
123 * Overloaded assignment operator.
125 Vb& operator=(const Vb &vb);
130 Vb *clone( ) const { return new Vb(*this); };
132 //-----[ set oid / get oid ]------------------------------------------
135 * Set the oid from another oid.
137 void set_oid(const Oid &oid) { iv_vb_oid = oid; };
140 * Get the oid portion.
142 * @note Check the validity of the object through Vb::valid() before
143 * calling this method.
145 void get_oid(Oid &oid) const { oid = iv_vb_oid; };
148 * Get the oid portion as a const.
150 * @note Check the validity of the object through Vb::valid() before
151 * calling this method.
153 const Oid &get_oid() const { return iv_vb_oid; };
155 //-----[ set value ]--------------------------------------------------
158 * Set the value using any SnmpSyntax object.
160 void set_value(const SnmpSyntax &val)
161 { free_vb(); iv_vb_value = val.clone(); };
164 * Set the value with an int.
166 * The syntax of the Vb will be set to SMI INT32.
168 void set_value(const int i) { free_vb(); iv_vb_value = new SnmpInt32(i); };
171 * Set the value with an unsigned int.
173 * The syntax of the Vb will be set to SMI UINT32.
175 void set_value(const unsigned int i)
176 { free_vb(); iv_vb_value = new SnmpUInt32(i); };
179 * Set the value with a long int.
181 * @note Even on 64 bit platforms, only 32 bits are used
183 * The syntax of the Vb will be set to SMI INT32.
185 void set_value(const long i)
186 { free_vb(); iv_vb_value = new SnmpInt32(i); };
189 * Set the value with an unsigned long int.
191 * @note Even on 64 bit platforms, only 32 bits are used
193 * The syntax of the Vb will be set to SMI UINT32.
195 void set_value(const unsigned long i)
196 { free_vb(); iv_vb_value = new SnmpUInt32(i); };
199 * Set value using a null terminated string.
201 * The syntax of the Vb will be set to SMI octet.
203 void set_value(const char *ptr)
204 { free_vb(); iv_vb_value = new OctetStr(ptr); };
207 * Set value using a string and length.
209 * The syntax of the Vb will be set to SMI octet.
211 void set_value(const unsigned char *ptr, const unsigned int len)
212 { free_vb(); iv_vb_value = new OctetStr(ptr, len); };
215 * Set the value portion of the vb to null, if its not already.
217 void set_null() { free_vb(); };
219 //----[ get value ]------------------------------------------------
222 * Get the value using a SnmpSyntax object.
224 * @param val - An object of a subclass of SnmpSyntax that will be
225 * assigned the value of the vb.
227 * @return SNMP_CLASS_SUCCESS if the vb value could be assigned to
228 * the passed SnmpSyntax object, else SNMP_CLASS_INVALID.
230 int get_value(SnmpSyntax &val) const;
235 * This method will only return success if the value of the vb is SMI INT32.
237 * @param i - returned value
239 * @return SNMP_CLASS_SUCCESS on success, else SNMP_CLASS_INVALID.
241 int get_value(int &i) const;
246 * This method will only return success if the value of the vb can
247 * be mapped to an unsigned long (SMI types uint32, counter32, gauge
250 * @param i - returned value
252 * @return SNMP_CLASS_SUCCESS on success, else SNMP_CLASS_INVALID.
254 int get_value(unsigned int &i) const;
259 * This method will only return success if the value of the vb is SMI INT32.
261 * @param i - returned value
263 * @return SNMP_CLASS_SUCCESS on success, else SNMP_CLASS_INVALID.
265 int get_value(long &i) const;
270 * This method will only return success if the value of the vb can
271 * be mapped to an unsigned long (SMI types uint32, counter32, gauge
274 * @param i - returned value
276 * @return SNMP_CLASS_SUCCESS on success, else SNMP_CLASS_INVALID.
278 int get_value(unsigned long &i) const;
283 * This method will only return success if the value of the vb is SMI OCTET.
285 * @note The caller must provide a target string big enough to
286 * handle the vb string. No length checks are done within
287 * this method. The returned string will be null terminated.
289 * @param ptr - Pointer to already allocated space to hold the vb
290 * value. The first char will be set to zero on failure.
291 * @param len - Returned length of the string. Will be set to 0 on failure.
293 * @return SNMP_CLASS_SUCCESS on success, else SNMP_CLASS_INVALID.
295 int get_value(unsigned char *ptr, unsigned long &len) const;
300 * This method will only return success if the value of the vb is SMI OCTET.
302 * @note If the target space is not big enough to hold the complete
303 * string only part of the string is copied.
305 * @param ptr - Pointer to already allocated space to hold the vb
306 * value. The first char will be set to zero on failure.
307 * @param len - Returned length of the string. Will be set to 0
309 * @param maxlen - Maximum length of the space that ptr points to.
310 * @param add_null_byte - Add a null byte at end of output string.
313 * @return SNMP_CLASS_SUCCESS on success, else SNMP_CLASS_INVALID.
315 int get_value(unsigned char *ptr,
317 const unsigned long maxlen,
318 const bool add_null_byte = false) const;
323 * This method will only return success if the value of the vb is SMI OCTET.
325 * @note The caller must provide a target string big enough to
326 * handle the vb string. No length checks are done within
327 * this method. The returned string will be null terminated.
329 * @param ptr - Pointer to already allocated space to hold the vb
330 * value. The first char will be set to zero on failure.
332 * @return SNMP_CLASS_SUCCESS on success, else SNMP_CLASS_INVALID.
334 int get_value(char *ptr) const;
337 * Clone the value portion of the variable binding.
339 * The returned pointer must be deleted by the caller.
342 * a pointer to a clone of the value of the receiver.
344 SnmpSyntax* clone_value() const
345 { return ((iv_vb_value) ? iv_vb_value->clone() : 0); };
348 //-----[ misc]--------------------------------------------------------
351 * Return the syntax or the exception status.
353 * @return If the SNMPv2 exception status is set, it is returned.
354 * otherwise the syntax of the value object is returned.
356 SmiUINT32 get_syntax() const;
361 * The Value portion of the Vb will be deleted and a new value portion
362 * is allocated with it's default value (zero).
364 * @param syntax - The new syntax.
366 void set_syntax(const SmiUINT32 syntax);
369 * Set the exception status.
371 * @param status - the new SNMPv2 exception status.
373 void set_exception_status(const SmiUINT32 status)
374 { exception_status = status; };
377 * Get the exception status.
379 SmiUINT32 get_exception_status() const { return exception_status; };
382 * Return a formatted version of the value.
384 * @return A null terminated string (empty if no value).
386 const char *get_printable_value() const;
389 * Return a formatted version of the Oid.
391 * @return A null terminated string (may be empty if no Oid has been set).
393 const char *get_printable_oid() const
394 { return iv_vb_oid.get_printable(); };
397 * Return the validity of a Vb object.
399 * @return TRUE if oid and value have been set.
404 * Return the space needed for serialization.
406 * @return the length of the BER encoding of this Vb.
408 int get_asn1_length() const;
413 void clear() { free_vb(); iv_vb_oid.clear(); };
415 //-----[ protected members ]
417 Oid iv_vb_oid; // a vb is made up of a oid
418 SnmpSyntax *iv_vb_value; // and a value...
419 SmiUINT32 exception_status; // are there any vb exceptions??
422 * Free the value portion.
427 #ifdef SNMP_PP_NAMESPACE
428 } // end of namespace Snmp_pp