00001 /*_############################################################################ 00002 _## 00003 _## oid.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++ O I D. H 00047 00048 OID CLASS DEFINITION 00049 00050 DESIGN + AUTHOR: 00051 Peter E Mellquist 00052 00053 LANGUAGE: 00054 ANSI C++ 00055 00056 OPERATING SYSTEMS: 00057 MS-Windows Win32 00058 BSD UNIX 00059 00060 DESCRIPTION: 00061 This class is fully contained and does not rely on or any other 00062 SNMP libraries. This class is portable across any platform 00063 which supports C++. 00064 00065 =====================================================================*/ 00066 // $Id: oid.h,v 1.5 2004/03/26 17:47:48 katz Exp $ 00067 00068 #ifndef _OID_H_ 00069 #define _OID_H_ 00070 00071 //------------------------------------------------------------------------ 00072 00073 #include "snmp_pp/smival.h" // derived class for all values 00074 #include "snmp_pp/collect.h" 00075 00076 #ifdef SNMP_PP_NAMESPACE 00077 namespace Snmp_pp { 00078 #endif 00079 00080 00081 /** 00082 * The Object Identifier Class. 00083 * 00084 * The Object Identification (Oid) class is the encapsulation of an 00085 * SMI object identifier. The SMI object is a data identifier for a 00086 * data element found in a Management Information Base (MIB), as 00087 * defined by a MIB definition. The SMI Oid, its related structures 00088 * and functions, are a natural fit for object orientation. In fact, 00089 * the Oid class shares many common features to the C++ String 00090 * class. For those of you familiar with the C++ String class or 00091 * Microsoft's Foundation Classes (MFC) CString class, the Oid class 00092 * will be familiar and easy to use. The Oid class is designed to be 00093 * efficient and fast. The Oid class allows definition and 00094 * manipulation of object identifiers. 00095 */ 00096 class DLLOPT Oid : public SnmpSyntax 00097 { 00098 public: 00099 00100 /** 00101 * Construct an invalid Oid. 00102 */ 00103 Oid(); 00104 00105 /** 00106 * Construct an Oid from a dotted string. 00107 * 00108 * @param dotted_oid_string - for example "1.3.1.6.1.10" 00109 */ 00110 Oid(const char *dotted_oid_string); 00111 00112 /** 00113 * Constructor using another oid object (copy constructor). 00114 * 00115 * @param oid - Source Oid 00116 */ 00117 Oid (const Oid &oid); 00118 00119 /** 00120 * Constructor from array. 00121 * 00122 * @param raw_oid - array of oid values 00123 * @param oid_len - length of array 00124 */ 00125 Oid(const unsigned long *raw_oid, int oid_len); 00126 00127 /** 00128 * Destructor. 00129 */ 00130 virtual ~Oid(); 00131 00132 /** 00133 * Return the current syntax. 00134 * 00135 * @return always sNMP_SYNTAX_OID 00136 */ 00137 SmiUINT32 get_syntax() const { return sNMP_SYNTAX_OID; }; 00138 00139 /** 00140 * Assignment from a string. 00141 * 00142 * @param dotted_oid_string - New value (for example "1.3.6.1.6.0"); 00143 */ 00144 virtual Oid& operator=(const char *dotted_oid_string); 00145 00146 /** 00147 * Assign one Oid to another. 00148 */ 00149 virtual Oid& operator=(const Oid &oid); 00150 00151 /** 00152 * Return the space needed for serialization. 00153 */ 00154 int get_asn1_length() const; 00155 00156 /** 00157 * Overloaded equal operator. 00158 */ 00159 DLLOPT friend int operator==(const Oid &lhs, const Oid &rhs); 00160 00161 /** 00162 * Overloaded not equal operator. 00163 */ 00164 DLLOPT friend int operator!=(const Oid &lhs, const Oid &rhs); 00165 00166 /** 00167 * Overloaded less than < operator. 00168 */ 00169 DLLOPT friend int operator<(const Oid &lhs, const Oid &rhs); 00170 00171 /** 00172 * Overloaded less than <= operator. 00173 */ 00174 DLLOPT friend int operator<=(const Oid &lhs, const Oid &rhs); 00175 00176 /** 00177 * Overloaded greater than > operator. 00178 */ 00179 DLLOPT friend int operator>(const Oid &lhs, const Oid &rhs); 00180 00181 /** 00182 * Overloaded greater than >= operator. 00183 */ 00184 DLLOPT friend int operator>=(const Oid &lhs, const Oid &rhs); 00185 00186 /** 00187 * Overloaded equal operator operator. 00188 */ 00189 DLLOPT friend int operator==(const Oid &lhs, const char *rhs); 00190 00191 /** 00192 * Overloaded not equal operator. 00193 */ 00194 DLLOPT friend int operator!=(const Oid &lhs, const char *rhs); 00195 00196 /** 00197 * Overloaded less than < operator. 00198 */ 00199 DLLOPT friend int operator<(const Oid &lhs, const char *rhs); 00200 00201 /** 00202 * Overloaded less than <= operator. 00203 */ 00204 DLLOPT friend int operator<=(const Oid &lhs, char *rhs); 00205 00206 /** 00207 * Overloaded greater than > operator. 00208 */ 00209 DLLOPT friend int operator>(const Oid &lhs, const char *rhs); 00210 00211 /** 00212 * Overloaded greater than >= operator. 00213 */ 00214 DLLOPT friend int operator>=(const Oid &lhs, const char *rhs); 00215 00216 /** 00217 * Overloaded operator +, Concatenate two Oids. 00218 */ 00219 DLLOPT friend Oid operator +(const Oid &lhs, const Oid &rhs) 00220 { Oid tmp(lhs); tmp += rhs; return tmp;}; 00221 00222 /** 00223 * Append operator, appends the dotted oid string. 00224 * 00225 * @param a - dotted oid string, for example "5.192.14.6" 00226 */ 00227 Oid& operator+=(const char *a); 00228 00229 /** 00230 * Appends an int. 00231 * 00232 * @param i - Value to add at the end of the Oid 00233 */ 00234 Oid& operator+=(const unsigned long i); 00235 00236 /** 00237 * Appends an Oid. 00238 * 00239 * @param o - Oid to add at the end 00240 */ 00241 Oid& operator+=(const Oid &o); 00242 00243 /** 00244 * Allows element access as an array. 00245 * This method behaves like real array: if your position 00246 * is out of bounds, you're lost! 00247 * 00248 * @param position - valid position -- 0 to (len() - 1) 00249 * 00250 * @return Value on the given position 00251 */ 00252 unsigned long &operator[](int position) 00253 { return smival.value.oid.ptr[position]; }; 00254 00255 /** 00256 * Allows element access as an array for const objects. 00257 * This method behaves like real array: if your position 00258 * is out of bounds, you're lost! 00259 * 00260 * @param position - valid position -- 0 to (len() - 1) 00261 * 00262 * @return Value on the given position 00263 */ 00264 unsigned long operator[](int position) const 00265 { return smival.value.oid.ptr[position]; }; 00266 00267 /** 00268 * Get the WinSnmp oid part. 00269 * @note This method returns a pointer to internal data. 00270 * If it is modified, the Oid changes too. 00271 * 00272 * @return pointer to the internal oid structure. 00273 */ 00274 SmiLPOID oidval() { return (SmiLPOID) &smival.value.oid; }; 00275 00276 /** 00277 * Set the data from raw form. 00278 * 00279 * @param raw_oid - Array of new values 00280 * @param oid_len - Length of the array raw_oid 00281 */ 00282 void set_data(const unsigned long *raw_oid, const unsigned int oid_len); 00283 00284 00285 /** 00286 * Get the length of the oid. 00287 */ 00288 unsigned long len() const { return smival.value.oid.len; }; 00289 00290 /** 00291 * Trim off the rightmost values of an oid. 00292 * 00293 * @param n - Trim off n values from the right (default is one) 00294 */ 00295 void trim(const unsigned long n = 1); 00296 00297 /** 00298 * Compare two Oids from the left in direction left-to-right. 00299 * 00300 * @param n - Subvalues to compare 00301 * @param o - The Oid to compare with 00302 * 00303 * @return 0 if equal / -1 if less / 1 if greater 00304 */ 00305 int nCompare(const unsigned long n, const Oid &o) const; 00306 00307 /** 00308 * Compare two Oids from the right in direction right-to left. 00309 * 00310 * @param n - Subvalues to compare 00311 * @param o - The Oid to compare with 00312 * 00313 * @return 0 if equal / -1 if less / 1 if greater 00314 */ 00315 int RnCompare(const unsigned long n, const Oid &o) const; 00316 00317 /** 00318 * Return validity of the object. 00319 */ 00320 bool valid() const { return (smival.value.oid.ptr ? true : false); }; 00321 00322 /** 00323 * Get a printable ASCII string of the whole value. 00324 * 00325 * @return Dotted oid string (for example "1.3.6.1.6.0") 00326 */ 00327 const char *get_printable() const 00328 { return get_printable(1, smival.value.oid.len); }; 00329 00330 /** 00331 * Get a printable ASCII string of the right part of the value. 00332 * 00333 * @param n - positions to print, counted from right. 00334 * 00335 * @return Dotted oid string (for example "6.0") 00336 */ 00337 const char *get_printable(const unsigned long n) const 00338 { return get_printable(smival.value.oid.len - n + 1, n); }; 00339 00340 /** 00341 * Get a printable ASCII string of a part of the value. 00342 * 00343 * @param start - First position to print, starting with 1 (not zero!) 00344 * @param n - positions to print. 00345 * 00346 * @return Dotted oid string (for example "3.6.1.6") 00347 */ 00348 const char *get_printable(const unsigned long start, 00349 const unsigned long n) const; 00350 00351 /** 00352 * Clone this object. 00353 * 00354 * @return Pointer to the newly created object (allocated through new). 00355 */ 00356 SnmpSyntax *clone() const { return (SnmpSyntax *) new Oid(*this); }; 00357 00358 /** 00359 * Map other SnmpSyntax objects to Oid. 00360 */ 00361 SnmpSyntax& operator=(const SnmpSyntax &val); 00362 00363 /** 00364 * Clear the Oid. 00365 */ 00366 void clear() { delete_oid_ptr(); }; 00367 00368 protected: 00369 /** 00370 * Convert a string to an smi oid. 00371 * 00372 * @param string - input string 00373 * @param dstOid - destination oid 00374 */ 00375 virtual int StrToOid(const char *string, SmiLPOID dstOid); 00376 00377 /** 00378 * Clone an smi oid. 00379 * 00380 * @param srcOid - source oid 00381 * @param dstOid - destination oid 00382 */ 00383 virtual int OidCopy(SmiLPOID srcOid, SmiLPOID dstOid); 00384 00385 /** 00386 * Convert an smi oid to its string representation. 00387 * 00388 * @param srcOid - source oid 00389 * @param size - size of string 00390 * @param string - pointer to string 00391 */ 00392 virtual int OidToStr(const SmiOID *srcOid, 00393 SmiUINT32 size, 00394 char *string) const; 00395 00396 /** 00397 * Free the internal oid pointer and set the pointer and the length to zero. 00398 */ 00399 inline void delete_oid_ptr(); 00400 00401 //----[ instance variables ] 00402 00403 /*mutable*/ char *iv_str; // used for returning oid string 00404 }; 00405 00406 //-----------[ End Oid Class ]------------------------------------- 00407 00408 // create OidCollection type 00409 typedef SnmpCollection <Oid> OidCollection; 00410 00411 inline void Oid::delete_oid_ptr() 00412 { 00413 // delete the old value 00414 if (smival.value.oid.ptr) 00415 { 00416 delete [] smival.value.oid.ptr; 00417 smival.value.oid.ptr = 0; 00418 } 00419 smival.value.oid.len = 0; 00420 }; 00421 00422 //==============[ operator>=(Oid &x,Oid &y) ]============================= 00423 // greater than >= overloaded 00424 inline int operator>=(const Oid &x, const Oid &y) 00425 { 00426 return (!(x<y)); // just invert existing < 00427 } 00428 00429 #ifdef SNMP_PP_NAMESPACE 00430 }; // end of namespace Snmp_pp 00431 #endif 00432 00433 #endif //_OID_H_
1.3.2