Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

770 lines
17 KiB

  1. // Copyright (c) 1997-2001 Microsoft Corporation, All Rights Reserved
  2. /*-----------------------------------------------------------------
  3. Filename: value.hpp
  4. Purpose : To specify the classes of various Snmp values and instance
  5. identifiers. These classes represent the different types of
  6. values for variables that may populate a MIB.
  7. Written By: B.Rajeev
  8. -----------------------------------------------------------------*/
  9. #ifndef __VALUE__
  10. #define __VALUE__
  11. /*-----------------------------------------------------------------
  12. General Overview:
  13. A variable instance refers to a MIB object, e.g.
  14. 1.3.6.1.2.1.1.1.0 or 1.3.6.1.2.1.2.1.2.1. The instance is
  15. encoded as an SNMP object identifier and is represented by the
  16. class SnmpObjectIdentifier.
  17. The classes derived from SnmpValue represent the encoding of the
  18. information stored within the MIB object. The value is encoded
  19. as an implementation of the abstract class SnmpValue. The SNMP
  20. class library implements the following derivations of SnmpValue
  21. which refer to SNMP BER encoded types.
  22. SnmpNull
  23. SnmpInteger
  24. SnmpCounter32
  25. SnmpCounter64
  26. SnmpGauge
  27. SnmpTimeTicks
  28. SnmpIPAddress
  29. SnmpNetworkAddress
  30. SnmpBitString
  31. SnmpOctetString
  32. SnmpOpaque
  33. SnmpObjectIdentifier
  34. All the implemented classes provide (in addition to others) -
  35. 1. Constructors to initialize using relevant values or another
  36. instance of the same class.
  37. 2. GetValue, SetValue methods for obtaining and setting
  38. relevant values.
  39. 3. "=" operator to over-ride the default assignment operator and
  40. an Equivalent method to check for equivalence between two instances
  41. of the same (derived) class
  42. 4. Copy methods for obtaining a copy of a specified instance of
  43. the class.
  44. note of caution:
  45. ----------------
  46. Some of the GetValue functions return pointers
  47. to dynamically allocated data. Users of the class must make copies
  48. of the returned values and must not rely on the integrity of this
  49. pointer or values obtained through it in future (because of
  50. SetValue methods, or destruction of corresponding SnmpValue class)
  51. -----------------------------------------------------------------*/
  52. #include <provexpt.h>
  53. // Abstract class at the root of all Snmp Values
  54. class DllImportExport SnmpValue
  55. {
  56. // the "=" operator and the copy constructor have been
  57. // made private to prevent copies of the SnmpValue instance
  58. // from being made
  59. SnmpValue &operator=(IN const SnmpValue &)
  60. {
  61. return *this;
  62. }
  63. SnmpValue(IN const SnmpValue &snmp_value) {}
  64. protected:
  65. virtual BOOL Equivalent(IN const SnmpValue &value) const = 0;
  66. SnmpValue() {}
  67. public:
  68. virtual SnmpValue *Copy () const = 0 ;
  69. BOOL operator==(IN const SnmpValue &value) const
  70. {
  71. return Equivalent(value) ;
  72. }
  73. BOOL operator!=(IN const SnmpValue &value) const
  74. {
  75. return !((*this) == value) ;
  76. }
  77. virtual ~SnmpValue() {}
  78. } ;
  79. // Enables null values for required variables. Its a concrete class
  80. // with dummy constructor and destructors to enable specification of
  81. // null values
  82. class DllImportExport SnmpNull : public SnmpValue
  83. {
  84. protected:
  85. virtual BOOL Equivalent(IN const SnmpValue &value) const ;
  86. public:
  87. // dummy constructor and destructor
  88. SnmpNull() {}
  89. ~SnmpNull() {}
  90. SnmpValue &operator=(IN const SnmpNull &to_copy)
  91. {
  92. return *this;
  93. }
  94. SnmpValue *Copy() const { return new SnmpNull; }
  95. };
  96. // Allows integer values to be specified
  97. class DllImportExport SnmpInteger : public SnmpValue
  98. {
  99. private:
  100. LONG val;
  101. protected:
  102. virtual BOOL Equivalent(IN const SnmpValue &value) const ;
  103. public:
  104. SnmpInteger ( IN const LONG value ) : val(value) {}
  105. SnmpInteger ( IN const SnmpInteger &value );
  106. ~SnmpInteger () {}
  107. LONG GetValue () const;
  108. void SetValue ( IN const LONG value );
  109. SnmpValue &operator=(IN const SnmpInteger &to_copy)
  110. {
  111. SetValue(to_copy.GetValue());
  112. return *this;
  113. }
  114. BOOL Equivalent(IN const SnmpInteger &snmp_integer) const
  115. {
  116. if ( val == snmp_integer.GetValue() )
  117. return TRUE;
  118. else
  119. return FALSE;
  120. }
  121. SnmpValue *Copy () const;
  122. } ;
  123. // Encapsulates gauge value
  124. class DllImportExport SnmpGauge : public SnmpValue
  125. {
  126. private:
  127. ULONG val;
  128. protected:
  129. virtual BOOL Equivalent(IN const SnmpValue &value) const ;
  130. public:
  131. SnmpGauge ( IN const LONG value ) : val(value) {}
  132. SnmpGauge ( IN const SnmpGauge &value );
  133. ~SnmpGauge () {}
  134. ULONG GetValue () const;
  135. void SetValue ( IN const ULONG value );
  136. SnmpValue *Copy () const;
  137. SnmpValue &operator=(IN const SnmpGauge &to_copy)
  138. {
  139. SetValue(to_copy.GetValue());
  140. return *this;
  141. }
  142. BOOL Equivalent(IN const SnmpGauge &snmp_gauge) const
  143. {
  144. if ( val == snmp_gauge.GetValue() )
  145. return TRUE;
  146. else
  147. return FALSE;
  148. }
  149. } ;
  150. // Encapsulates Counter values
  151. class DllImportExport SnmpCounter : public SnmpValue
  152. {
  153. private:
  154. ULONG val;
  155. protected:
  156. virtual BOOL Equivalent(IN const SnmpValue &value) const ;
  157. public:
  158. SnmpCounter ( IN const ULONG value ) : val(value) {}
  159. SnmpCounter ( IN const SnmpCounter &value );
  160. ~SnmpCounter () {}
  161. ULONG GetValue () const;
  162. void SetValue ( IN const ULONG value );
  163. SnmpValue *Copy () const;
  164. SnmpValue &operator=(IN const SnmpCounter &to_copy)
  165. {
  166. SetValue(to_copy.GetValue());
  167. return *this;
  168. }
  169. BOOL Equivalent(IN const SnmpCounter &snmp_counter) const
  170. {
  171. if ( val == snmp_counter.GetValue() )
  172. return TRUE;
  173. else
  174. return FALSE;
  175. }
  176. } ;
  177. // Encapsulates Time Ticks (since an earlier event)
  178. class DllImportExport SnmpTimeTicks : public SnmpValue
  179. {
  180. private:
  181. ULONG val;
  182. protected:
  183. virtual BOOL Equivalent(IN const SnmpValue &value) const ;
  184. public:
  185. SnmpTimeTicks ( IN const ULONG value ) : val(value) {}
  186. SnmpTimeTicks ( IN const SnmpTimeTicks &value );
  187. ~SnmpTimeTicks () {}
  188. ULONG GetValue () const;
  189. void SetValue ( IN const ULONG value );
  190. SnmpValue *Copy () const;
  191. SnmpValue &operator=(IN const SnmpTimeTicks &to_copy)
  192. {
  193. SetValue(to_copy.GetValue());
  194. return *this;
  195. }
  196. BOOL Equivalent(IN const SnmpTimeTicks &snmp_time_ticks) const
  197. {
  198. if ( val == snmp_time_ticks.GetValue() )
  199. return TRUE;
  200. else
  201. return FALSE;
  202. }
  203. } ;
  204. // Encapsulates octet strings that do not have any terminator.
  205. // The octet string is specified by the pair (val,length) where
  206. // 'val' is a pointer to heap data and 'length' provides the number
  207. // of octets in the data string.
  208. class DllImportExport SnmpOctetString : public SnmpValue
  209. {
  210. private:
  211. // in case a new 'value' string has the same length as the stored
  212. // string, the stored string may be overwritten. this avoids
  213. // having to allocate and deallocate heap memory for the purpose.
  214. void OverWrite(IN const UCHAR *value);
  215. protected:
  216. BOOL is_valid;
  217. UCHAR *val;
  218. ULONG length;
  219. virtual BOOL Equivalent(IN const SnmpValue &value) const ;
  220. virtual void Initialize(IN const UCHAR *value, IN const ULONG valueLength);
  221. // The Replicate and UnReplicate methods allocate and deallocate
  222. // heap data. Replicate also copies the contents of the parameter
  223. // 'value' onto the allocated memory. This function may be
  224. // implemented different and, thus, the methods have been declared
  225. // virtual.
  226. virtual UCHAR *Replicate(IN const UCHAR *value, IN const ULONG valueLength);
  227. virtual void UnReplicate(UCHAR *value);
  228. public:
  229. SnmpOctetString ( IN const UCHAR *value , IN const ULONG valueLength );
  230. SnmpOctetString ( IN const SnmpOctetString &value );
  231. ~SnmpOctetString ();
  232. void SetValue ( IN const UCHAR *value , IN const ULONG valueLength );
  233. ULONG GetValueLength () const;
  234. UCHAR *GetValue () const;
  235. SnmpValue *Copy () const;
  236. SnmpValue &operator=(IN const SnmpOctetString &to_copy)
  237. {
  238. if ( to_copy() )
  239. SetValue(to_copy.GetValue(), to_copy.GetValueLength());
  240. return *this;
  241. }
  242. void * operator()(void) const
  243. {
  244. return ( is_valid?(void *)this:NULL );
  245. }
  246. BOOL Equivalent(IN const SnmpOctetString &snmp_octet_string) const;
  247. } ;
  248. // OpaqueValue class encapsulates octet strings
  249. class DllImportExport SnmpOpaque : public SnmpValue
  250. {
  251. private:
  252. SnmpOctetString *octet_string;
  253. protected:
  254. virtual BOOL Equivalent(IN const SnmpValue &value) const ;
  255. public:
  256. SnmpOpaque ( IN const UCHAR *value , IN const ULONG valueLength ) : octet_string ( NULL )
  257. {
  258. octet_string = new SnmpOctetString(value, valueLength);
  259. }
  260. SnmpOpaque ( IN const SnmpOpaque &value ) : octet_string ( NULL )
  261. {
  262. octet_string = new SnmpOctetString(value.GetValue(), value.GetValueLength());
  263. }
  264. ~SnmpOpaque()
  265. {
  266. delete octet_string;
  267. }
  268. void SetValue ( IN const UCHAR *value , IN const ULONG valueLength )
  269. {
  270. octet_string->SetValue(value, valueLength);
  271. }
  272. ULONG GetValueLength () const
  273. {
  274. return octet_string->GetValueLength();
  275. }
  276. UCHAR *GetValue () const
  277. {
  278. return octet_string->GetValue();
  279. }
  280. SnmpValue &operator=(IN const SnmpOpaque &to_copy)
  281. {
  282. if ( to_copy() )
  283. SetValue(to_copy.GetValue(), to_copy.GetValueLength());
  284. return *this;
  285. }
  286. SnmpValue *Copy () const
  287. {
  288. return new SnmpOpaque(octet_string->GetValue(),
  289. octet_string->GetValueLength());
  290. }
  291. void * operator()(void) const
  292. {
  293. return (*octet_string)();
  294. }
  295. BOOL Equivalent(IN const SnmpOpaque &snmp_opaque) const
  296. {
  297. return octet_string->Equivalent(*(snmp_opaque.octet_string));
  298. }
  299. };
  300. #define DEFAULT_OBJECTIDENTIFIER_LENGTH 32
  301. // Encapsulates the object identifier. An object identifier
  302. // identifies a MIB object instance
  303. class DllImportExport SnmpObjectIdentifier : public SnmpValue
  304. {
  305. // describes the legal values for a comparison
  306. enum Comparison {LESS_THAN, EQUAL_TO, GREATER_THAN};
  307. private:
  308. BOOL is_valid;
  309. ULONG m_value[DEFAULT_OBJECTIDENTIFIER_LENGTH];
  310. ULONG *val;
  311. ULONG length;
  312. // in case a new 'value' string has the same length as the stored
  313. // string, the stored string may be overwritten. this avoids
  314. // having to allocate and deallocate heap memory for the purpose.
  315. void OverWrite(IN const ULONG *value);
  316. protected:
  317. virtual BOOL Equivalent(IN const SnmpValue &value) const ;
  318. virtual void Initialize(IN const ULONG *value, IN const ULONG valueLength);
  319. // The Replicate and UnReplicate methods allocate and deallocate
  320. // heap data. Replicate also copies the contents of the parameter
  321. // 'value' onto the allocated memory. This function may be
  322. // implemented different and, thus, the methods have been declared
  323. // virtual.
  324. virtual ULONG *Replicate(IN const ULONG *value, IN const ULONG valueLength) const;
  325. // Allocates enough memory to copy the first value followed by
  326. // the second value to be copied, thus, appending the two values
  327. virtual ULONG *Replicate(IN const ULONG *first_value, IN const ULONG first_length,
  328. IN const ULONG *second_value, IN const ULONG second_length) const;
  329. virtual void UnReplicate(ULONG *value);
  330. // This single function
  331. Comparison Compare(IN const SnmpObjectIdentifier &first,
  332. IN const SnmpObjectIdentifier &second) const;
  333. BOOL Equivalent(IN const SnmpObjectIdentifier &value) const;
  334. public:
  335. SnmpObjectIdentifier ( IN const ULONG *value , IN const ULONG valueLength );
  336. SnmpObjectIdentifier ( IN const char *value );
  337. SnmpObjectIdentifier ( IN const SnmpObjectIdentifier &value );
  338. ~SnmpObjectIdentifier ();
  339. void SetValue ( IN const ULONG *value , IN const ULONG valueLength );
  340. ULONG GetValueLength () const;
  341. ULONG *GetValue () const;
  342. SnmpValue *Copy () const;
  343. BOOL Equivalent(IN const SnmpObjectIdentifier &value,
  344. IN ULONG max_length) const;
  345. BOOL operator<(IN const SnmpObjectIdentifier &value) const
  346. {
  347. return (Compare(*this,value) == LESS_THAN)?TRUE:FALSE;
  348. }
  349. BOOL operator>(IN const SnmpObjectIdentifier &value) const
  350. {
  351. return (Compare(*this,value) == GREATER_THAN)?TRUE:FALSE;
  352. }
  353. BOOL operator<=(IN const SnmpObjectIdentifier &value) const
  354. {
  355. return !(*this > value);
  356. }
  357. BOOL operator>=(IN const SnmpObjectIdentifier &value) const
  358. {
  359. return !(*this < value);
  360. }
  361. BOOL operator==(IN const SnmpObjectIdentifier &value) const
  362. {
  363. if ( this->GetValueLength() == value.GetValueLength() )
  364. return Equivalent(value) ;
  365. else
  366. return FALSE;
  367. }
  368. BOOL operator!=(IN const SnmpObjectIdentifier &value) const
  369. {
  370. return !(*this == value);
  371. }
  372. SnmpObjectIdentifier operator+ ( IN const SnmpObjectIdentifier &value ) const;
  373. BOOL Prefix( IN ULONG index, SnmpObjectIdentifier &prefix ) const
  374. {
  375. if ( index >= length )
  376. return FALSE;
  377. prefix.UnReplicate (val) ;
  378. prefix.Initialize (val, index+1) ;
  379. return TRUE ;
  380. }
  381. BOOL Suffix ( IN ULONG index , SnmpObjectIdentifier &suffix ) const
  382. {
  383. if ( index >= length )
  384. return FALSE;
  385. suffix.UnReplicate (val) ;
  386. suffix.Initialize ( val+index, length-index ) ;
  387. return TRUE ;
  388. }
  389. SnmpObjectIdentifier *Cut (SnmpObjectIdentifier &value) const;
  390. ULONG &operator [] ( IN const ULONG index ) const;
  391. SnmpValue &operator=(IN const SnmpObjectIdentifier &to_copy)
  392. {
  393. if ( to_copy() )
  394. SetValue(to_copy.GetValue(), to_copy.GetValueLength());
  395. return *this;
  396. }
  397. void * operator()(void) const
  398. {
  399. return ( is_valid?(void *)this:NULL );
  400. }
  401. char *GetAllocatedString() const;
  402. } ;
  403. // encapsulates an ip address. represents the 32 bit value in a ULONG
  404. class DllImportExport SnmpIpAddress : public SnmpValue
  405. {
  406. private:
  407. // if the dotted decimal representation passed to the constructor
  408. // is ill-formed, the instance may be invalid
  409. BOOL is_valid;
  410. ULONG val;
  411. protected:
  412. virtual BOOL Equivalent(IN const SnmpValue &value) const ;
  413. public:
  414. SnmpIpAddress ( IN const ULONG value )
  415. :val(value), is_valid(TRUE)
  416. {}
  417. // a dotted decimal representation is parsed to obtain the 32 bit value
  418. SnmpIpAddress ( IN const char *value ) ;
  419. SnmpIpAddress ( IN const SnmpIpAddress &value );
  420. ~SnmpIpAddress () {}
  421. ULONG GetValue () const;
  422. void SetValue ( IN const ULONG value );
  423. SnmpValue *Copy () const;
  424. SnmpValue &operator=(IN const SnmpIpAddress &to_copy)
  425. {
  426. if ( to_copy() )
  427. SetValue(to_copy.GetValue());
  428. return *this;
  429. }
  430. void * operator()(void) const
  431. {
  432. return ( is_valid?(void *)this:NULL );
  433. }
  434. BOOL Equivalent(IN const SnmpIpAddress &snmp_ip_address) const
  435. {
  436. if ( is_valid && snmp_ip_address() )
  437. return ( val == snmp_ip_address.GetValue() );
  438. else
  439. return FALSE;
  440. }
  441. } ;
  442. // Encapsulates UInteger32 value
  443. class DllImportExport SnmpUInteger32 : public SnmpValue
  444. {
  445. private:
  446. ULONG val;
  447. protected:
  448. virtual BOOL Equivalent(IN const SnmpValue &value) const ;
  449. public:
  450. SnmpUInteger32 ( IN const LONG value ) : val(value) {}
  451. SnmpUInteger32 ( IN const SnmpUInteger32 &value );
  452. ~SnmpUInteger32 () {}
  453. ULONG GetValue () const;
  454. void SetValue ( IN const ULONG value );
  455. SnmpValue *Copy () const;
  456. SnmpValue &operator=(IN const SnmpUInteger32 &to_copy)
  457. {
  458. SetValue(to_copy.GetValue());
  459. return *this;
  460. }
  461. BOOL Equivalent(IN const SnmpUInteger32 &snmp_integer) const
  462. {
  463. if ( val == snmp_integer.GetValue() )
  464. return TRUE;
  465. else
  466. return FALSE;
  467. }
  468. } ;
  469. // Encapsulates Counter64 values
  470. class DllImportExport SnmpCounter64 : public SnmpValue
  471. {
  472. private:
  473. ULONG lval;
  474. ULONG hval;
  475. protected:
  476. virtual BOOL Equivalent(IN const SnmpValue &value) const ;
  477. public:
  478. SnmpCounter64 ( IN const ULONG lvalue , IN const ULONG hvalue ) : lval(lvalue),hval(hvalue) {}
  479. SnmpCounter64 ( IN const SnmpCounter64 &value );
  480. ~SnmpCounter64 () {}
  481. ULONG GetLowValue () const;
  482. ULONG GetHighValue () const;
  483. void SetValue ( IN const ULONG lvalue , IN const ULONG hvalue );
  484. SnmpValue *Copy () const;
  485. SnmpValue &operator=(IN const SnmpCounter64 &to_copy)
  486. {
  487. SetValue(to_copy.GetLowValue(),to_copy.GetHighValue());
  488. return *this;
  489. }
  490. BOOL Equivalent(IN const SnmpCounter64 &snmp_counter ) const
  491. {
  492. if ( ( lval == snmp_counter.GetLowValue() ) && ( hval == snmp_counter.GetHighValue() ) )
  493. return TRUE;
  494. else
  495. return FALSE;
  496. }
  497. } ;
  498. // Encapsulates EndOfMibView values
  499. class DllImportExport SnmpEndOfMibView : public SnmpValue
  500. {
  501. private:
  502. protected:
  503. virtual BOOL Equivalent(IN const SnmpValue &value) const ;
  504. public:
  505. SnmpEndOfMibView () {} ;
  506. ~SnmpEndOfMibView () {} ;
  507. SnmpValue *Copy () const { return new SnmpEndOfMibView ; }
  508. SnmpValue &operator=(IN const SnmpEndOfMibView &to_copy)
  509. {
  510. return *this;
  511. }
  512. } ;
  513. // Encapsulates NoSuchObject values
  514. class DllImportExport SnmpNoSuchObject: public SnmpValue
  515. {
  516. private:
  517. protected:
  518. virtual BOOL Equivalent(IN const SnmpValue &value) const ;
  519. public:
  520. SnmpNoSuchObject () {} ;
  521. ~SnmpNoSuchObject () {} ;
  522. SnmpValue *Copy () const { return new SnmpNoSuchObject ; }
  523. SnmpValue &operator=(IN const SnmpNoSuchObject &to_copy)
  524. {
  525. return *this;
  526. }
  527. } ;
  528. // Encapsulates NoSuchInstance values
  529. class DllImportExport SnmpNoSuchInstance: public SnmpValue
  530. {
  531. private:
  532. protected:
  533. virtual BOOL Equivalent(IN const SnmpValue &value) const ;
  534. public:
  535. SnmpNoSuchInstance () {} ;
  536. ~SnmpNoSuchInstance () {} ;
  537. SnmpValue *Copy () const { return new SnmpNoSuchInstance ; }
  538. SnmpValue &operator=(IN const SnmpNoSuchInstance &to_copy)
  539. {
  540. return *this;
  541. }
  542. } ;
  543. #endif // __VALUE__