]> git.stg.codes - ssmd.git/blob - 3rdparty/snmp++/src/vb.cpp
Initial adding
[ssmd.git] / 3rdparty / snmp++ / src / vb.cpp
1 /*_############################################################################
2   _## 
3   _##  vb.cpp  
4   _##
5   _##  SNMP++v3.2.25
6   _##  -----------------------------------------------
7   _##  Copyright (c) 2001-2010 Jochen Katz, Frank Fock
8   _##
9   _##  This software is based on SNMP++2.6 from Hewlett Packard:
10   _##  
11   _##    Copyright (c) 1996
12   _##    Hewlett-Packard Company
13   _##  
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. 
25   _##  
26   _##  Stuttgart, Germany, Thu Sep  2 00:07:47 CEST 2010 
27   _##  
28   _##########################################################################*/
29 /*===================================================================
30
31   Copyright (c) 1999
32   Hewlett-Packard Company
33
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.
44
45
46   V B . C P P
47
48   VARIABLE BINDING CLASS IMPLEMENTATION
49
50   DESCRIPTION:
51   This module contains the class implementation of the VB class.
52   The Vb class is an encapsulation of the snmp variable binding.
53
54   DESIGN + AUTHOR:  Peter E Mellquist
55 =====================================================================*/
56 char vb_cpp_version[]="#(@) SNMP++ $Id: vb.cpp 341 2008-08-29 21:37:48Z katz $";
57
58 #include "snmp_pp/vb.h"            // include vb class defs
59
60 #ifdef SNMP_PP_NAMESPACE
61 namespace Snmp_pp {
62 #endif
63
64 #define IP_ADDR_SIZE  4
65 #define IPX_ADDR_SIZE 10
66 #define MAC_ADDR_SIZE 6
67
68 //--------------[ Vb::valid() ]-----------------------------------------
69 // returns validity of a Vb object
70 // must have a valid oid and value
71 bool Vb::valid() const
72 {
73   if (iv_vb_oid.valid() &&
74 #ifdef WHEN_WE_HAVE_SNMPNULL_CLASS
75       iv_vb_value && iv_vb_value->valid()
76 #else
77       ((iv_vb_value == NULL) || (iv_vb_value && iv_vb_value->valid()))
78 #endif
79     )
80     return true;
81   return false;
82 }
83
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)
89 {
90   if (this == &vb) return *this;  // check for self assignment
91
92   free_vb(); // free up target to begin with
93
94   //-----[ reassign the Oid portion 1st ]
95   vb.get_oid(iv_vb_oid);
96
97   //-----[ next set the vb value portion ]
98   if (vb.iv_vb_value)
99     iv_vb_value = vb.iv_vb_value->clone();
100
101   exception_status = vb.exception_status;
102
103   return *this; // return self reference
104 }
105
106 //----------------[ void Vb::free_vb() ]--------------------------------
107 // protected method to free memory
108 // this method is used to free memory when assigning new vbs
109 // or destructing
110 // in the case of oids and octets, we need to do a deep free
111 void Vb::free_vb()
112 {
113   if (iv_vb_value)
114   {
115     delete iv_vb_value;
116     iv_vb_value = NULL;
117   }
118   exception_status = SNMP_CLASS_SUCCESS;
119 }
120
121 //---------------------[ Vb::get_value(int &i) ]----------------------
122 // get value int
123 // returns 0 on success and value
124 int Vb::get_value(int &i) const
125 {
126    if (iv_vb_value &&
127        iv_vb_value->valid() &&
128        (iv_vb_value->get_syntax() == sNMP_SYNTAX_INT32 ))
129    {
130      long lval;
131      lval = *((SnmpInt32 *)iv_vb_value);// SnmpInt32 includes cast to long,
132      i = (int) lval;                    // but not to int.
133      return SNMP_CLASS_SUCCESS;
134    }
135    return SNMP_CLASS_INVALID;
136 }
137
138 // get the unsigned int
139 // returns 0 on success and a value
140 int Vb::get_value(unsigned int &i) const
141 {
142   if (iv_vb_value &&
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 )))
148   {
149     unsigned long lval;
150     lval = *((SnmpUInt32 *)iv_vb_value);
151     i = (unsigned int)lval;
152     return SNMP_CLASS_SUCCESS;
153   }
154   return SNMP_CLASS_INVALID;
155 }
156
157
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
162 {
163    if (iv_vb_value &&
164        iv_vb_value->valid() &&
165        (iv_vb_value->get_syntax() == sNMP_SYNTAX_INT32 ))
166    {
167      i = *((SnmpInt32 *)iv_vb_value);   // SnmpInt32 includes cast to long
168      return SNMP_CLASS_SUCCESS;
169    }
170    return SNMP_CLASS_INVALID;
171 }
172
173
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
178 {
179   if (iv_vb_value &&
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 )))
185   {
186     i = *((SnmpUInt32 *)iv_vb_value);   // SnmpUint32 has includes to ulong
187     return SNMP_CLASS_SUCCESS;
188   }
189   return SNMP_CLASS_INVALID;
190 }
191
192
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
197 //
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
201 {
202   if (iv_vb_value &&
203       iv_vb_value->valid() &&
204       (iv_vb_value->get_syntax() == sNMP_SYNTAX_OCTETS))
205   {
206     OctetStr *p_os = (OctetStr *)iv_vb_value;
207     len = p_os->len();
208     memcpy(ptr, p_os->data(), len);
209     ptr[len] = 0;
210     return SNMP_CLASS_SUCCESS;
211   }
212
213   if (ptr) ptr[0] = 0;
214   len = 0;
215   return SNMP_CLASS_INVALID;
216 }
217
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
224 {
225   if (iv_vb_value &&
226       iv_vb_value->valid() &&
227       (iv_vb_value->get_syntax() == sNMP_SYNTAX_OCTETS) &&
228       (maxlen > 0))
229   {
230     OctetStr *p_os = (OctetStr *)iv_vb_value;
231     len = p_os->len();
232     if (len > maxlen) len = maxlen;
233     memcpy(ptr, p_os->data(), len);
234     if (add_null_byte)
235     {
236       if (len == maxlen)
237         ptr[len-1] = 0;
238       else
239         ptr[len] = 0;
240     }
241     return SNMP_CLASS_SUCCESS;
242   }
243
244   if (ptr) ptr[0] = 0;
245   len = 0;
246   return SNMP_CLASS_INVALID;
247 }
248
249
250 //---------------[ Vb::get_value(Value &val) ]--------
251 int Vb::get_value(SnmpSyntax &val) const
252 {
253   if (iv_vb_value)
254   {
255     val = *iv_vb_value;
256     if (val.valid())
257       return SNMP_CLASS_SUCCESS;
258     return SNMP_CLASS_INVALID;
259   }
260   // TM: should set val to be invalid
261   return SNMP_CLASS_INVALID;
262 }
263
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
269 {
270   if (iv_vb_value &&
271       iv_vb_value->valid() &&
272       (iv_vb_value->get_syntax() == sNMP_SYNTAX_OCTETS))
273   {
274     OctetStr *p_os = (OctetStr *)iv_vb_value;
275     unsigned long len = p_os->len();
276     memcpy(ptr, p_os->data(), len);
277     ptr[len] = 0;
278     return SNMP_CLASS_SUCCESS;
279   }
280
281   if (ptr) ptr[0] = 0;
282   return SNMP_CLASS_INVALID;
283 }
284
285
286
287 //-----[ misc]--------------------------------------------------------
288
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
294 {
295   if (exception_status != SNMP_CLASS_SUCCESS)
296     return exception_status;
297   else
298     return (iv_vb_value ? iv_vb_value->get_syntax() : sNMP_SYNTAX_NULL);
299 }
300
301 void Vb::set_syntax(const SmiUINT32 syntax)
302 {
303         free_vb(); // setting to SNMP_SYNTAX_NULL
304
305         exception_status = SNMP_CLASS_SUCCESS;
306
307         switch (syntax) {
308         case sNMP_SYNTAX_INT32:
309                 iv_vb_value = new SnmpInt32();
310                 break;
311         case sNMP_SYNTAX_TIMETICKS:
312                 iv_vb_value = new TimeTicks();
313                 break;
314         case sNMP_SYNTAX_CNTR32:
315                 iv_vb_value = new Counter32();
316                 break;
317         case sNMP_SYNTAX_GAUGE32:
318                 iv_vb_value = new Gauge32();
319                 break;
320 /* Not distinguishable from Gauge32
321         case sNMP_SYNTAX_UINT32:
322                 iv_vb_value = new SnmpUInt32();
323                 break;
324 */
325         case sNMP_SYNTAX_CNTR64:
326                 iv_vb_value = new Counter64();
327                 break;
328         case sNMP_SYNTAX_BITS:
329         case sNMP_SYNTAX_OCTETS:
330                 iv_vb_value = new OctetStr();
331                 break;
332         case sNMP_SYNTAX_OPAQUE:
333                 iv_vb_value = new OpaqueStr();
334                 break;
335         case sNMP_SYNTAX_IPADDR:
336                 iv_vb_value = new IpAddress();
337                 break;
338         case sNMP_SYNTAX_OID:
339                 iv_vb_value = new Oid();
340                 break;
341         case sNMP_SYNTAX_NULL:
342                 break;
343         case sNMP_SYNTAX_NOSUCHINSTANCE:
344                 exception_status = sNMP_SYNTAX_NOSUCHINSTANCE;
345                 break;
346         case sNMP_SYNTAX_NOSUCHOBJECT:
347                 exception_status = sNMP_SYNTAX_NOSUCHOBJECT;
348                 break;
349         case sNMP_SYNTAX_ENDOFMIBVIEW:
350                 exception_status = sNMP_SYNTAX_ENDOFMIBVIEW;
351                 break;
352         case sNMP_SYNTAX_SEQUENCE:
353                 break;
354         }
355 }
356
357 static char blank_string[] = "";
358
359 // return the printabel value
360 const char WINFAR *Vb::get_printable_value() const
361 {
362   if (iv_vb_value)
363     return iv_vb_value->get_printable();
364   return blank_string;
365 }
366
367 int Vb::get_asn1_length() const
368 {
369   // Header for vbs is always 4 Bytes! FIXME
370   if (iv_vb_value)
371     return iv_vb_oid.get_asn1_length() + iv_vb_value->get_asn1_length() + 4;
372
373   return iv_vb_oid.get_asn1_length() + 2 + 4;
374 }
375
376 #ifdef SNMP_PP_NAMESPACE
377 }; // end of namespace Snmp_pp
378 #endif