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 Pdu class definition. Encapsulation of an SMI Protocol
54 Data Unit (PDU) in C++.
56 =====================================================================*/
57 // $Id: pdu.h 288 2007-03-22 22:37:09Z katz $
62 #include "snmp_pp/config_snmp_pp.h"
63 #include "snmp_pp/address.h"
64 #include "snmp_pp/timetick.h"
65 #include "snmp_pp/octet.h"
66 #include "snmp_pp/oid.h"
68 #ifdef SNMP_PP_NAMESPACE
74 #define PDU_MAX_RID 32767 ///< max request id to use
75 #define PDU_MIN_RID 1000 ///< min request id to use
77 //=======================================================================
79 //=======================================================================
88 * Constructor no args.
90 * This constructor creates a valid empty Pdu object.
95 * Constructor with vbs.
97 * The Pdu class does not take ownership of the array and the Vb
98 * objects, so if these were allocated with new, they must be freed
99 * by te user with delete.
101 * @param pvbs - Array of pointers to Vb objects
102 * @param pvb_count - Length of the array
104 Pdu(Vb* pvbs, const int pvb_count);
107 * Constructor with another Pdu instance.
109 * @param pdu - source pdu object
111 Pdu(const Pdu &pdu) : vbs(0), vbs_size(0), vb_count(0) { *this = pdu; };
119 * Overloaded assignment operator.
121 * @param pdu - Pdu that should be assigned to this object
123 Pdu& operator=(const Pdu &pdu);
126 * Append a vb to the pdu.
128 * @param vb - The Vb that should be added (as last vb) to the pdu
130 Pdu& operator+=(const Vb &vb);
133 * Clone a Pdu object.
135 * @return Pointer to a newly created Pdu object, that is identical to this
137 Pdu *clone() const { return new Pdu(*this); };
140 * Get Pointers to all Vbs from Pdu.
142 * The caller has to allocate the array. The returned pointers point
143 * to the Vb objects that are internally used by the pdu. So any
144 * changes to the returned Vb objects will change the pdu. If the
145 * pdu is modified (e.g. through Pdu::trim()) afterwards, the
146 * returned array will contain invalid pointers.
148 * @param pvbs - Array of empty pointers of size pvb_count
149 * @param pvb_count - Amount of Vb pointers to get
151 * @return TRUE on success
153 int get_vblist(Vb* pvbs, const int pvb_count) const;
156 * Deposit all Vbs to Pdu.
158 * The vb objects of the pdu will be freed and the objects from the
159 * array will be cloned and added to the pdu. If this method returns
160 * FALSE, the pdu will not conatin any Vb objects.
162 * @param pvbs - Array of valid pointers of size pvb_count
163 * @param pvb_count - Amount of Vb pointers i the array
165 * @return TRUE on success
167 int set_vblist(Vb* pvbs, const int pvb_count);
170 * Get a particular Vb.
172 * @param vb - Object to store the vb
173 * @param index - The vb to get (zero is the first vb)
175 * @return TRUE on success
177 int get_vb(Vb &vb, const int index) const;
180 * Return a reference to a particular Vb.
182 * @note Before calling this method, make sure that there
183 * is a Vb using get_vb_count().
185 * @param index - The Vb to return starting with 0.
186 * @return A const reference to the Vb
188 const Vb &get_vb(const int index) const { return *vbs[index]; };
191 * Set a particular vb.
193 * If this method returns FALSE, the pdu has not been modified.
195 * @param vb - Source vb
196 * @param index - The vb to set (zero is the first vb)
198 * @return TRUE on success
200 int set_vb(Vb &vb, const int index);
203 * Get the number of vbs.
205 * @return The number of Vb objects within the pdu.
207 int get_vb_count() const { return vb_count; };
212 * @note The index has to be checked by the caller.
214 * @param i zero based index
216 Vb& operator[](const int i) { return *vbs[i]; };
219 * Get the error status.
221 * @return The SNMP error status
223 int get_error_status() const { return error_status; };
226 * Set the error status.
228 * @param err - The new SNMP error status.
230 void set_error_status(const int err) { error_status = err; };
233 * Clear the error status.
235 void clear_error_status() { error_status = 0; };
238 * Get the error index.
240 * @return The SNMP error index
242 int get_error_index() const { return error_index; };
245 * Set the error index.
247 * @param err - The new SNMP error index.
249 void set_error_index(const int index) { error_index = index; };
252 * Clear the error index.
254 void clear_error_index() { error_index = 0; };
257 * Clear error status and error index.
259 void clear_error() { set_error_status(0); set_error_index(0); }
262 * Get the request id.
264 * @return The SNMP request id
266 unsigned long get_request_id() const { return request_id; };
269 * Set the request id.
271 * @param rid - The new SNMP request id
273 void set_request_id(const unsigned long rid) { request_id = rid; };
278 unsigned short get_type() const { return pdu_type; };
283 void set_type(unsigned short type) { pdu_type = type; };
286 * Returns validity of Pdu instance.
288 bool valid() const { return validity; };
293 * @param count - number of vbs to trim of, starting with the last
294 * @return TRUE on success, FALSE if nothing was done
296 int trim(const int count=1);
299 * Delete a Vb anywhere within the Pdu.
301 * @param position - Delete the Vb at this position (starting with 0)
302 * @return TRUE on success
304 int delete_vb(const int position);
307 * Set notify timestamp.
309 void set_notify_timestamp(const TimeTicks &ts) { notify_timestamp = ts; };
312 * Get notify timestamp.
314 void get_notify_timestamp(TimeTicks &ts) const { ts = notify_timestamp; };
319 * @return true if the set succeeded.
321 bool set_notify_id(const Oid &id)
322 { notify_id = id; return (notify_id.len() == id.len()); };
327 * @return true if the get succeeded.
329 bool get_notify_id(Oid &id) const
330 { id = notify_id; return (notify_id.len() == id.len()); };
333 * Set the notify enterprise.
335 * @return true if the set succeeded.
337 bool set_notify_enterprise(const Oid &e)
338 { notify_enterprise = e; return (notify_enterprise.len() == e.len()); };
341 * Get the notify enterprise.
343 * @return true if the get succeeded.
345 bool get_notify_enterprise(Oid & e) const
346 { e = notify_enterprise; return (notify_enterprise.len() == e.len()); };
350 * Set the security level that should be used when this Pdu is sent.
351 * The default security level of a Pdu is SNMP_SECURITY_LEVEL_NOAUTH_NOPRIV.
353 * @param level - One of SNMP_SECURITY_LEVEL_NOAUTH_NOPRIV,
354 * SNMP_SECURITY_LEVEL_AUTH_NOPRIV,
355 * SNMP_SECURITY_LEVEL_AUTH_PRIV
357 void set_security_level(const int level) { security_level = level; };
360 * Return the security level of the Pdu.
362 * @return - the security level
364 int get_security_level() const { return security_level; };
367 * Set the context name of the Pdu.
369 * @param name - The context name
371 bool set_context_name(const OctetStr &name)
372 { context_name = name; return (context_name.valid() && name.valid()); };
375 * Set the context name of the Pdu.
377 * @param name - The context name
379 bool set_context_name(const char *name)
380 { context_name = name; return context_name.valid(); };
383 * Get the context name of the Pdu.
385 * @param name - Object fot the context name
387 bool get_context_name(OctetStr &name) const
388 { name = context_name; return (context_name.valid() && name.valid()); };
391 * Get the context name of the Pdu.
393 * @return - Return the context name as an OctetStr
395 const OctetStr& get_context_name() const { return context_name; };
398 * Set the context engine id of the Pdu.
400 * @param id - The new context engine id
402 bool set_context_engine_id(const OctetStr &id) { context_engine_id = id;
403 return (context_engine_id.valid() && id.valid()); };
406 * Set the context engine id of the Pdu.
408 * @param id - The new context engine id
410 bool set_context_engine_id(const char *id)
411 { context_engine_id = id; return context_engine_id.valid(); };
414 * Get the context engine id of the Pdu.
416 * @param id - Object for the context engine
418 bool get_context_engine_id(OctetStr &id) const { id = context_engine_id;
419 return (context_engine_id.valid() && id.valid()); };
422 * Get the context engine id of the Pdu.
424 * @return - Return the context engine id as an OctetStr
426 const OctetStr& get_context_engine_id() const { return context_engine_id; };
429 * Set the SNMPv3 message id (msgID)
431 * @param msg_id - the message id of the received message
433 void set_message_id(const unsigned long msg_id) { message_id = msg_id; }
436 * Get the SNMPv3 message id (msgID)
438 * @return - the message id of the received message
440 unsigned long get_message_id() const { return message_id; }
443 * Set the maximum size of the scoped pdu to be included in a
444 * possible response message.
446 * @param l - the maximum size
448 void set_maxsize_scopedpdu(unsigned long l) { maxsize_scopedpdu = l; };
451 * Get the maximum size of the scoped pdu to be included in a
452 * possible response message.
454 * @return - the maximum size
456 unsigned long get_maxsize_scopedpdu() const { return maxsize_scopedpdu; };
461 * Get the SNMPv1 trap address
463 int get_v1_trap_address(GenAddress &address) const;
466 * Set the SNMPv1 trap address
468 int set_v1_trap_address(const Address &address);
471 * Return the length of the encoded vbs with pdu header.
473 * @note this method wll not work for v1 traps.
475 int get_asn1_length() const;
478 * Clear the Pdu contents (destruct and construct in one go)
483 * Does the type of response match the type of request.
485 static bool match_type(const int request, const int response);
487 //-------------[ protected members ]--------------------------
491 * Extend the vbs array.
493 * @return true on success
497 Vb **vbs; // pointer to array of Vbs
498 int vbs_size; // Size of array
499 int vb_count; // count of Vbs
500 int error_status; // SMI error status
501 int error_index; // SMI error index
502 bool validity; // valid boolean
503 unsigned long request_id; // SMI request id
504 unsigned short pdu_type; // derived at run time based on request type
505 // for notify Pdu objects only
507 TimeTicks notify_timestamp; // a timestamp associated with an infor
508 Oid notify_id; // an id
509 Oid notify_enterprise;
510 GenAddress v1_trap_address; // address object
511 int v1_trap_address_set;
513 // specific Objects for SNMPv3
514 int security_level; // the securityLevel with which this Pdu
515 // should be sent or was received
516 unsigned long message_id;
517 unsigned long maxsize_scopedpdu;
518 OctetStr context_name;
519 OctetStr context_engine_id;
523 #ifdef SNMP_PP_NAMESPACE
524 } // end of namespace Snmp_pp