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.
50 DESIGN + AUTHOR: Peter E Mellquist
53 This class is fully contained and does not rely on or any other
54 SNMP libraries. This class is portable across any platform
57 =====================================================================*/
58 // $Id: oid.h 1775 2010-07-11 09:59:28Z katz $
63 //------------------------------------------------------------------------
65 #include "snmp_pp/smival.h" // derived class for all values
66 #include "snmp_pp/collect.h"
68 #ifdef SNMP_PP_NAMESPACE
74 * The Object Identifier Class.
76 * The Object Identification (Oid) class is the encapsulation of an
77 * SMI object identifier. The SMI object is a data identifier for a
78 * data element found in a Management Information Base (MIB), as
79 * defined by a MIB definition. The SMI Oid, its related structures
80 * and functions, are a natural fit for object orientation. In fact,
81 * the Oid class shares many common features to the C++ String
82 * class. For those of you familiar with the C++ String class or
83 * Microsoft's Foundation Classes (MFC) CString class, the Oid class
84 * will be familiar and easy to use. The Oid class is designed to be
85 * efficient and fast. The Oid class allows definition and
86 * manipulation of object identifiers.
88 * @note Oid holds two internal buffers for get_printable() functions.
89 * The buffer returned by get_printable() is valid until the
90 * Oid object is modified. The functions get_printable(len) and
91 * get_printable(start, len) share the same buffer which is
92 * freed and newly allocated for each call.
94 class DLLOPT Oid : public SnmpSyntax
99 * Construct an invalid Oid.
104 * Construct an Oid from a string.
106 * Depending on the second param, the oid_string can either be
107 * - a dotted oid string (like "1.3.6.1.6"). An arbitrary part
108 * of the oid can be given as a string value enclosed in
109 * '$' characters. For example the oid string
110 * "1.3.6.1.6.1.12.1.3.$public0$" will result to the oid
111 * 1.3.6.1.6.1.12.1.3.112.117.98.108.105.99.95.48
112 * - a normal string (like "public"). The Oid will have the
113 * ASCII values of the string characters. So "public" will
114 * result to the oid 112.117.98.108.105.99
116 * @param oid_string - for example "1.3.1.6.1.10"
117 * @param is_dotted_oid_string - Select format within oid_string
119 Oid(const char *oid_string, const bool is_dotted_oid_string = true);
122 * Constructor using another oid object (copy constructor).
124 * @param oid - Source Oid
129 * Constructor from array.
131 * @param raw_oid - array of oid values
132 * @param oid_len - length of array
134 Oid(const unsigned long *raw_oid, int oid_len);
142 * Return the current syntax.
144 * @return always sNMP_SYNTAX_OID
146 SmiUINT32 get_syntax() const { return sNMP_SYNTAX_OID; };
149 * Assignment from a string.
151 * @param dotted_oid_string - New value (for example "1.3.6.1.6.0");
153 virtual Oid& operator=(const char *dotted_oid_string);
156 * Assign one Oid to another.
158 virtual Oid& operator=(const Oid &oid);
161 * Return the space needed for serialization.
163 int get_asn1_length() const;
166 * Overloaded equal operator.
168 DLLOPT friend int operator==(const Oid &lhs, const Oid &rhs);
171 * Overloaded not equal operator.
173 DLLOPT friend int operator!=(const Oid &lhs, const Oid &rhs)
174 { return (!(lhs == rhs)); }; // just invert ==
177 * Overloaded less than < operator.
179 DLLOPT friend int operator<(const Oid &lhs, const Oid &rhs);
182 * Overloaded less than <= operator.
184 DLLOPT friend int operator<=(const Oid &lhs, const Oid &rhs)
185 { return ((lhs < rhs) || (lhs == rhs)); };
188 * Overloaded greater than > operator.
190 DLLOPT friend int operator>(const Oid &lhs, const Oid &rhs)
191 { return (!(lhs <= rhs)); }; // just invert existing <=
194 * Overloaded greater than >= operator.
196 DLLOPT friend int operator>=(const Oid &lhs, const Oid &rhs)
197 { return (!(lhs < rhs)); }; // just invert existing <
200 * Overloaded equal operator operator.
202 DLLOPT friend int operator==(const Oid &lhs, const char *rhs);
205 * Overloaded not equal operator.
207 DLLOPT friend int operator!=(const Oid &lhs, const char *rhs);
210 * Overloaded less than < operator.
212 DLLOPT friend int operator<(const Oid &lhs, const char *rhs);
215 * Overloaded less than <= operator.
217 DLLOPT friend int operator<=(const Oid &lhs, char *rhs);
220 * Overloaded greater than > operator.
222 DLLOPT friend int operator>(const Oid &lhs, const char *rhs);
225 * Overloaded greater than >= operator.
227 DLLOPT friend int operator>=(const Oid &lhs, const char *rhs);
230 * Overloaded operator +, Concatenate two Oids.
232 DLLOPT friend Oid operator +(const Oid &lhs, const Oid &rhs)
233 { Oid tmp(lhs); tmp += rhs; return tmp;};
236 * Append operator, appends the dotted oid string.
238 * @param a - dotted oid string, for example "5.192.14.6"
240 Oid& operator+=(const char *a);
245 * @param i - Value to add at the end of the Oid
247 Oid& operator+=(const unsigned long i);
252 * @param o - Oid to add at the end
254 Oid& operator+=(const Oid &o);
257 * Allows element access as an array.
258 * This method behaves like real array: if your index
259 * is out of bounds, you're lost!
261 * @param index - valid index -- 0 to (len() - 1)
263 * @return Value on the given index
265 unsigned long &operator[](const unsigned int index)
266 { m_changed = true; return smival.value.oid.ptr[index]; };
269 * Allows element access as an array for const objects.
270 * This method behaves like real array: if your index
271 * is out of bounds, you're lost!
273 * @param index - valid index -- 0 to (len() - 1)
275 * @return Value on the given position
277 unsigned long operator[](const unsigned int index) const
278 { return (index >= len()) ? 0 : smival.value.oid.ptr[index]; };
281 * Get the WinSnmp oid part.
282 * @note This method returns a pointer to internal data.
283 * If it is modified, the Oid changes too.
285 * @return pointer to the internal oid structure.
287 SmiLPOID oidval() { return (SmiLPOID) &smival.value.oid; };
290 * Set the data from raw form.
292 * @param raw_oid - Array of new values
293 * @param oid_len - Length of the array raw_oid
295 void set_data(const unsigned long *raw_oid, const unsigned int oid_len);
298 * Set the data from raw form.
300 * @param str - Array of new values (a string)
301 * @param str_len - Length of the array raw_oid
303 void set_data(const char *str, const unsigned int str_len);
306 * Get the length of the oid.
308 unsigned long len() const { return smival.value.oid.len; };
311 * Trim off the rightmost values of an oid.
313 * @param n - Trim off n values from the right (default is one)
315 void trim(const unsigned long n = 1);
318 * Compare two Oids from the left in direction left-to-right.
320 * @param n - Subvalues to compare
321 * @param o - The Oid to compare with
323 * @return 0 if equal / -1 if less / 1 if greater
325 int nCompare(const unsigned long n, const Oid &o) const;
328 * Return validity of the object.
330 bool valid() const { return (smival.value.oid.ptr ? true : false); };
333 * Get a printable ASCII string of the whole value.
335 * @return Dotted oid string (for example "1.3.6.1.6.0")
337 const char *get_printable() const
338 { return get_printable(1, smival.value.oid.len, (char*&)iv_str); };
341 * Get a printable ASCII string of the right part of the value.
343 * @param n - positions to print, counted from right.
345 * @return Dotted oid string (for example "6.0")
347 const char *get_printable(const unsigned long n) const
348 { return get_printable(smival.value.oid.len - n + 1, n, (char*&)iv_part_str); };
351 * Get a printable ASCII string of a part of the value.
353 * @param start - First position to print, starting with 1 (not zero!)
354 * @param n - positions to print.
355 * @param buffer - pointer to the returned buffer
357 * @note If buffer is not NULL, this function calls "delete [] buffer",
358 * a new buffer is allocated using "new" and the caller has
361 * @return Dotted oid string (for example "3.6.1.6")
363 const char *get_printable(const unsigned long start,
364 const unsigned long n,
365 char *&buffer) const;
368 * Get a printable ASCII string of a part of the value.
370 * @param start - First position to print, starting with 1 (not zero!)
371 * @param n - positions to print.
373 * @return Dotted oid string (for example "3.6.1.6")
375 const char *get_printable(const unsigned long start,
376 const unsigned long n) const
377 { return get_printable(start, n, (char*&)iv_part_str); };
382 * @return Pointer to the newly created object (allocated through new).
384 SnmpSyntax *clone() const { return (SnmpSyntax *) new Oid(*this); };
387 * Map other SnmpSyntax objects to Oid.
389 SnmpSyntax& operator=(const SnmpSyntax &val);
394 void clear() { delete_oid_ptr(); };
398 * Convert a string to an smi oid.
400 * @param string - input string
401 * @param dstOid - destination oid
403 virtual int StrToOid(const char *string, SmiLPOID dstOid) const;
408 * @param srcOid - source oid
409 * @param dstOid - destination oid
411 virtual int OidCopy(SmiLPOID srcOid, SmiLPOID dstOid) const;
414 * Convert an smi oid to its string representation.
416 * @param srcOid - source oid
417 * @param size - size of string
418 * @param string - pointer to string
420 virtual int OidToStr(const SmiOID *srcOid,
425 * Free the internal oid pointer and set the pointer and the length to zero.
427 inline void delete_oid_ptr();
429 //----[ instance variables ]
431 SNMP_PP_MUTABLE char *iv_str; // used for returning complete oid string
432 SNMP_PP_MUTABLE char *iv_part_str; // used for returning part oid string
433 SNMP_PP_MUTABLE bool m_changed;
436 //-----------[ End Oid Class ]-------------------------------------
438 // create OidCollection type
439 typedef SnmpCollection <Oid> OidCollection;
441 inline void Oid::delete_oid_ptr()
443 // delete the old value
444 if (smival.value.oid.ptr)
446 delete [] smival.value.oid.ptr;
447 smival.value.oid.ptr = 0;
449 smival.value.oid.len = 0;
453 #ifdef SNMP_PP_NAMESPACE
454 } // end of namespace Snmp_pp