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