SNMP++  3.3.4
pdu.h
Go to the documentation of this file.
1 /*_############################################################################
2  _##
3  _## pdu.h
4  _##
5  _## SNMP++ v3.3
6  _## -----------------------------------------------
7  _## Copyright (c) 2001-2013 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  _##########################################################################*/
27 /*===================================================================
28 
29  Copyright (c) 1999
30  Hewlett-Packard Company
31 
32  ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
33  Permission to use, copy, modify, distribute and/or sell this software
34  and/or its documentation is hereby granted without fee. User agrees
35  to display the above copyright notice and this license notice in all
36  copies of the software and any documentation of the software. User
37  agrees to assume all liability for the use of the software; Hewlett-Packard
38  makes no representations about the suitability of this software for any
39  purpose. It is provided "AS-IS without warranty of any kind,either express
40  or implied. User hereby grants a royalty-free license to any and all
41  derivatives based upon this software code base.
42 
43 
44  SNMP++ P D U . H
45 
46  PDU CLASS DEFINITION
47 
48  DESIGN + AUTHOR: Peter E Mellquist
49 
50  DESCRIPTION:
51  Pdu class definition. Encapsulation of an SMI Protocol
52  Data Unit (PDU) in C++.
53 
54 =====================================================================*/
55 // $Id: pdu.h 2359 2013-05-09 20:07:01Z fock $
56 
57 #ifndef _PDU_CLS
58 #define _PDU_CLS
59 
60 #include "snmp_pp/config_snmp_pp.h"
61 #include "snmp_pp/address.h"
62 #include "snmp_pp/timetick.h"
63 #include "snmp_pp/octet.h"
64 #include "snmp_pp/oid.h"
65 
66 #ifdef SNMP_PP_NAMESPACE
67 namespace Snmp_pp {
68 #endif
69 
70 class Vb;
71 
72 #define PDU_MAX_RID 32767 ///< max request id to use
73 #define PDU_MIN_RID 1000 ///< min request id to use
74 
75 //=======================================================================
76 // Pdu Class
77 //=======================================================================
78 /**
79  * Pdu class...
80  */
81 class DLLOPT Pdu
82 {
83  public:
84 
85  /**
86  * Constructor no args.
87  *
88  * This constructor creates a valid empty Pdu object.
89  */
90  Pdu();
91 
92  /**
93  * Constructor with vbs.
94  *
95  * The Pdu class does not take ownership of the array and the Vb
96  * objects, so if these were allocated with new, they must be freed
97  * by te user with delete.
98  *
99  * @param pvbs - Array of pointers to Vb objects
100  * @param pvb_count - Length of the array
101  */
102  Pdu(Vb* pvbs, const int pvb_count);
103 
104  /**
105  * Constructor with another Pdu instance.
106  *
107  * @param pdu - source pdu object
108  */
109  Pdu(const Pdu &pdu) : vbs(0), vbs_size(0), vb_count(0) { *this = pdu; };
110 
111  /**
112  * Destructor
113  */
114  virtual ~Pdu();
115 
116  /**
117  * Overloaded assignment operator.
118  *
119  * @param pdu - Pdu that should be assigned to this object
120  */
121  Pdu& operator=(const Pdu &pdu);
122 
123  /**
124  * Append a vb to the pdu.
125  *
126  * @param vb - The Vb that should be added (as last vb) to the pdu
127  */
128  Pdu& operator+=(const Vb &vb);
129 
130  /**
131  * Clone a Pdu object.
132  *
133  * @return Pointer to a newly created Pdu object, that is identical to this
134  */
135  Pdu *clone() const { return new Pdu(*this); };
136 
137  /**
138  * Get Pointers to all Vbs from Pdu.
139  *
140  * The caller has to allocate the array. The returned pointers point
141  * to the Vb objects that are internally used by the pdu. So any
142  * changes to the returned Vb objects will change the pdu. If the
143  * pdu is modified (e.g. through Pdu::trim()) afterwards, the
144  * returned array will contain invalid pointers.
145  *
146  * @param pvbs - Array of empty pointers of size pvb_count
147  * @param pvb_count - Amount of Vb pointers to get
148  *
149  * @return TRUE on success
150  */
151  int get_vblist(Vb* pvbs, const int pvb_count) const;
152 
153  /**
154  * Deposit all Vbs to Pdu.
155  *
156  * The vb objects of the pdu will be freed and the objects from the
157  * array will be cloned and added to the pdu. If this method returns
158  * FALSE, the pdu will not conatin any Vb objects.
159  *
160  * @param pvbs - Array of valid pointers of size pvb_count
161  * @param pvb_count - Amount of Vb pointers i the array
162  *
163  * @return TRUE on success
164  */
165  int set_vblist(Vb const * pvbs, const int pvb_count);
166 
167  /**
168  * Get a particular Vb.
169  *
170  * @param vb - Object to store the vb
171  * @param index - The vb to get (zero is the first vb)
172  *
173  * @return TRUE on success
174  */
175  int get_vb(Vb &vb, const int index) const;
176 
177  /**
178  * Return a reference to a particular Vb.
179  *
180  * @note Before calling this method, make sure that there
181  * is a Vb using get_vb_count().
182  *
183  * @param index - The Vb to return starting with 0.
184  * @return A const reference to the Vb
185  */
186  const Vb &get_vb(const int index) const { return *vbs[index]; };
187 
188  /**
189  * Set a particular vb.
190  *
191  * If this method returns FALSE, the pdu has not been modified.
192  *
193  * @param vb - Source vb
194  * @param index - The vb to set (zero is the first vb)
195  *
196  * @return TRUE on success
197  */
198  int set_vb(Vb const &vb, const int index);
199 
200  /**
201  * Get the number of vbs.
202  *
203  * @return The number of Vb objects within the pdu.
204  */
205  int get_vb_count() const { return vb_count; };
206 
207  /**
208  * Get a Vb.
209  *
210  * @note The index has to be checked by the caller.
211  *
212  * @param i zero based index
213  */
214  Vb& operator[](const int i) { return *vbs[i]; };
215 
216  /**
217  * Get the error status.
218  *
219  * @return The SNMP error status
220  */
221  int get_error_status() const { return error_status; };
222 
223  /**
224  * Set the error status.
225  *
226  * @param err - The new SNMP error status.
227  */
228  void set_error_status(const int err) { error_status = err; };
229 
230  /**
231  * Clear the error status.
232  */
233  void clear_error_status() { error_status = 0; };
234 
235  /**
236  * Get the error index.
237  *
238  * @return The SNMP error index
239  */
240  int get_error_index() const { return error_index; };
241 
242  /**
243  * Set the error index.
244  *
245  * @param err - The new SNMP error index.
246  */
247  void set_error_index(const int index) { error_index = index; };
248 
249  /**
250  * Clear the error index.
251  */
252  void clear_error_index() { error_index = 0; };
253 
254  /**
255  * Clear error status and error index.
256  */
257  void clear_error() { set_error_status(0); set_error_index(0); }
258 
259  /**
260  * Get the request id.
261  *
262  * @return The SNMP request id
263  */
264  unsigned long get_request_id() const { return request_id; };
265 
266  /**
267  * Set the request id.
268  *
269  * @param rid - The new SNMP request id
270  */
271  void set_request_id(const unsigned long rid) { request_id = rid; };
272 
273  /**
274  * Get the pdu type.
275  */
276  unsigned short get_type() const { return pdu_type; };
277 
278  /**
279  * Set the pdu type.
280  */
281  void set_type(unsigned short type) { pdu_type = type; };
282 
283  /**
284  * Returns validity of Pdu instance.
285  */
286  bool valid() const { return validity; };
287 
288  /**
289  * Trim off vbs.
290  *
291  * @param count - number of vbs to trim of, starting with the last
292  * @return TRUE on success, FALSE if nothing was done
293  */
294  int trim(const int count=1);
295 
296  /**
297  * Delete a Vb anywhere within the Pdu.
298  *
299  * @param position - Delete the Vb at this position (starting with 0)
300  * @return TRUE on success
301  */
302  int delete_vb(const int position);
303 
304  /**
305  * Set notify timestamp.
306  */
307  void set_notify_timestamp(const TimeTicks &ts) { notify_timestamp = ts; };
308 
309  /**
310  * Get notify timestamp.
311  */
312  void get_notify_timestamp(TimeTicks &ts) const { ts = notify_timestamp; };
313 
314  /**
315  * Set the notify id.
316  *
317  * @return true if the set succeeded.
318  */
319  bool set_notify_id(const Oid &id)
320  { notify_id = id; return (notify_id.len() == id.len()); };
321 
322  /**
323  * Get the notify id.
324  *
325  * @return true if the get succeeded.
326  */
327  bool get_notify_id(Oid &id) const
328  { id = notify_id; return (notify_id.len() == id.len()); };
329 
330  /**
331  * Set the notify enterprise.
332  *
333  * @return true if the set succeeded.
334  */
335  bool set_notify_enterprise(const Oid &e)
336  { notify_enterprise = e; return (notify_enterprise.len() == e.len()); };
337 
338  /**
339  * Get the notify enterprise.
340  *
341  * @return true if the get succeeded.
342  */
343  bool get_notify_enterprise(Oid & e) const
344  { e = notify_enterprise; return (notify_enterprise.len() == e.len()); };
345 
346 #ifdef _SNMPv3
347  /**
348  * Set the security level that should be used when this Pdu is sent.
349  * The default security level of a Pdu is SNMP_SECURITY_LEVEL_NOAUTH_NOPRIV.
350  *
351  * @param level - One of SNMP_SECURITY_LEVEL_NOAUTH_NOPRIV,
352  * SNMP_SECURITY_LEVEL_AUTH_NOPRIV,
353  * SNMP_SECURITY_LEVEL_AUTH_PRIV
354  */
355  void set_security_level(const int level) { security_level = level; };
356 
357  /**
358  * Return the security level of the Pdu.
359  *
360  * @return - the security level
361  */
362  int get_security_level() const { return security_level; };
363 
364  /**
365  * Set the context name of the Pdu.
366  *
367  * @param name - The context name
368  */
369  bool set_context_name(const OctetStr &name)
370  { context_name = name; return (context_name.valid() && name.valid()); };
371 
372  /**
373  * Set the context name of the Pdu.
374  *
375  * @param name - The context name
376  */
377  bool set_context_name(const char *name)
378  { context_name = name; return context_name.valid(); };
379 
380  /**
381  * Get the context name of the Pdu.
382  *
383  * @param name - Object fot the context name
384  */
385  bool get_context_name(OctetStr &name) const
386  { name = context_name; return (context_name.valid() && name.valid()); };
387 
388  /**
389  * Get the context name of the Pdu.
390  *
391  * @return - Return the context name as an OctetStr
392  */
393  const OctetStr& get_context_name() const { return context_name; };
394 
395  /**
396  * Set the context engine id of the Pdu.
397  *
398  * @param id - The new context engine id
399  */
400  bool set_context_engine_id(const OctetStr &id) { context_engine_id = id;
401  return (context_engine_id.valid() && id.valid()); };
402 
403  /**
404  * Set the context engine id of the Pdu.
405  *
406  * @param id - The new context engine id
407  */
408  bool set_context_engine_id(const char *id)
409  { context_engine_id = id; return context_engine_id.valid(); };
410 
411  /**
412  * Get the context engine id of the Pdu.
413  *
414  * @param id - Object for the context engine
415  */
416  bool get_context_engine_id(OctetStr &id) const { id = context_engine_id;
417  return (context_engine_id.valid() && id.valid()); };
418 
419  /**
420  * Get the context engine id of the Pdu.
421  *
422  * @return - Return the context engine id as an OctetStr
423  */
424  const OctetStr& get_context_engine_id() const { return context_engine_id; };
425 
426  /**
427  * Set the SNMPv3 message id (msgID)
428  *
429  * @param msg_id - the message id of the received message
430  */
431  void set_message_id(const unsigned long msg_id) { message_id = msg_id; }
432 
433  /**
434  * Get the SNMPv3 message id (msgID)
435  *
436  * @return - the message id of the received message
437  */
438  unsigned long get_message_id() const { return message_id; }
439 
440  /**
441  * Set the maximum size of the scoped pdu to be included in a
442  * possible response message.
443  *
444  * @param l - the maximum size
445  */
446  void set_maxsize_scopedpdu(unsigned long l) { maxsize_scopedpdu = l; };
447 
448  /**
449  * Get the maximum size of the scoped pdu to be included in a
450  * possible response message.
451  *
452  * @return - the maximum size
453  */
454  unsigned long get_maxsize_scopedpdu() const { return maxsize_scopedpdu; };
455 
456 #endif // _SNMPv3
457 
458  /**
459  * Get the SNMPv1 trap address
460  */
461  int get_v1_trap_address(GenAddress &address) const;
462 
463  /**
464  * Set the SNMPv1 trap address
465  */
466  int set_v1_trap_address(const Address &address);
467 
468  /**
469  * Return the length of the encoded vbs with pdu header.
470  *
471  * @note this method wll not work for v1 traps.
472  */
473  int get_asn1_length() const;
474 
475  /**
476  * Clear the Pdu contents (destruct and construct in one go)
477  */
478  void clear();
479 
480  /**
481  * Does the type of response match the type of request.
482  */
483  static bool match_type(const int request, const int response);
484 
485  //-------------[ protected members ]--------------------------
486  protected:
487 
488  /**
489  * Extend the vbs array.
490  *
491  * @return true on success
492  */
493  bool extend_vbs();
494 
495  Vb **vbs; // pointer to array of Vbs
496  int vbs_size; // Size of array
497  int vb_count; // count of Vbs
498  int error_status; // SMI error status
499  int error_index; // SMI error index
500  bool validity; // valid boolean
501  unsigned long request_id; // SMI request id
502  unsigned short pdu_type; // derived at run time based on request type
503  // for notify Pdu objects only
504  // traps & notifies
505  TimeTicks notify_timestamp; // a timestamp associated with an infor
506  Oid notify_id; // an id
508  GenAddress v1_trap_address; // address object
510 #ifdef _SNMPv3
511  // specific Objects for SNMPv3
512  int security_level; // the securityLevel with which this Pdu
513  // should be sent or was received
514  unsigned long message_id;
515  unsigned long maxsize_scopedpdu;
518 #endif // _SNMPv3
519 };
520 
521 #ifdef SNMP_PP_NAMESPACE
522 } // end of namespace Snmp_pp
523 #endif
524 
525 #endif //_PDU_CLS
int get_security_level() const
Return the security level of the Pdu.
Definition: pdu.h:362
void set_type(unsigned short type)
Set the pdu type.
Definition: pdu.h:281
bool set_notify_enterprise(const Oid &e)
Set the notify enterprise.
Definition: pdu.h:335
void set_notify_timestamp(const TimeTicks &ts)
Set notify timestamp.
Definition: pdu.h:307
Pdu * clone() const
Clone a Pdu object.
Definition: pdu.h:135
Vb & operator[](const int i)
Get a Vb.
Definition: pdu.h:214
bool validity
Definition: pdu.h:500
void get_notify_timestamp(TimeTicks &ts) const
Get notify timestamp.
Definition: pdu.h:312
int security_level
Definition: pdu.h:512
void clear_error()
Clear error status and error index.
Definition: pdu.h:257
Pdu(const Pdu &pdu)
Constructor with another Pdu instance.
Definition: pdu.h:109
bool set_context_name(const char *name)
Set the context name of the Pdu.
Definition: pdu.h:377
bool get_context_engine_id(OctetStr &id) const
Get the context engine id of the Pdu.
Definition: pdu.h:416
bool set_context_engine_id(const OctetStr &id)
Set the context engine id of the Pdu.
Definition: pdu.h:400
The Vb class is the encapsulation of the SNMP variable binding.
Definition: vb.h:88
int get_error_status() const
Get the error status.
Definition: pdu.h:221
unsigned long get_request_id() const
Get the request id.
Definition: pdu.h:264
unsigned long get_message_id() const
Get the SNMPv3 message id (msgID)
Definition: pdu.h:438
bool valid() const
Returns validity of Pdu instance.
Definition: pdu.h:286
GenAddress v1_trap_address
Definition: pdu.h:508
int vb_count
Definition: pdu.h:497
Vb ** vbs
Definition: pdu.h:495
void set_error_status(const int err)
Set the error status.
Definition: pdu.h:228
unsigned short pdu_type
Definition: pdu.h:502
void clear_error_index()
Clear the error index.
Definition: pdu.h:252
OctetStr context_name
Definition: pdu.h:516
The timeticks class allows all the functionality of unsigned integers but is recognized as a distinct...
Definition: timetick.h:73
#define DLLOPT
bool set_context_engine_id(const char *id)
Set the context engine id of the Pdu.
Definition: pdu.h:408
unsigned long request_id
Definition: pdu.h:501
bool get_context_name(OctetStr &name) const
Get the context name of the Pdu.
Definition: pdu.h:385
const Vb & get_vb(const int index) const
Return a reference to a particular Vb.
Definition: pdu.h:186
bool valid() const
Return validity of the object.
Definition: octet.h:235
The Object Identifier Class.
Definition: oid.h:94
Oid notify_id
Definition: pdu.h:506
void set_error_index(const int index)
Set the error index.
Definition: pdu.h:247
Base class of all Address classes.
Definition: address.h:125
void clear_error_status()
Clear the error status.
Definition: pdu.h:233
const OctetStr & get_context_engine_id() const
Get the context engine id of the Pdu.
Definition: pdu.h:424
int vbs_size
Definition: pdu.h:496
TimeTicks notify_timestamp
Definition: pdu.h:505
bool set_context_name(const OctetStr &name)
Set the context name of the Pdu.
Definition: pdu.h:369
Definition: octet.h:67
const OctetStr & get_context_name() const
Get the context name of the Pdu.
Definition: pdu.h:393
void set_security_level(const int level)
Set the security level that should be used when this Pdu is sent.
Definition: pdu.h:355
bool get_notify_enterprise(Oid &e) const
Get the notify enterprise.
Definition: pdu.h:343
int error_index
Definition: pdu.h:499
bool get_notify_id(Oid &id) const
Get the notify id.
Definition: pdu.h:327
int get_vb_count() const
Get the number of vbs.
Definition: pdu.h:205
void set_message_id(const unsigned long msg_id)
Set the SNMPv3 message id (msgID)
Definition: pdu.h:431
unsigned long len() const
Get the length of the oid.
Definition: oid.h:463
unsigned long message_id
Definition: pdu.h:514
void set_request_id(const unsigned long rid)
Set the request id.
Definition: pdu.h:271
void set_maxsize_scopedpdu(unsigned long l)
Set the maximum size of the scoped pdu to be included in a possible response message.
Definition: pdu.h:446
int v1_trap_address_set
Definition: pdu.h:509
unsigned short get_type() const
Get the pdu type.
Definition: pdu.h:276
bool set_notify_id(const Oid &id)
Set the notify id.
Definition: pdu.h:319
unsigned long get_maxsize_scopedpdu() const
Get the maximum size of the scoped pdu to be included in a possible response message.
Definition: pdu.h:454
Oid notify_enterprise
Definition: pdu.h:507
Pdu class...
Definition: pdu.h:81
int get_error_index() const
Get the error index.
Definition: pdu.h:240
unsigned long maxsize_scopedpdu
Definition: pdu.h:515
int error_status
Definition: pdu.h:498
OctetStr context_engine_id
Definition: pdu.h:517