Main Page | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members | File Members

pdu.h

Go to the documentation of this file.
00001 /*_############################################################################
00002   _## 
00003   _##  pdu.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++ P D U . H
00047 
00048   PDU 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   Pdu class definition. Encapsulation of an SMI Protocol
00062   Data Unit (PDU) in C++.
00063 
00064 =====================================================================*/
00065 // $Id: pdu.h,v 1.11 2004/12/22 20:56:56 katz Exp $
00066 
00067 #ifndef _PDU_CLS
00068 #define _PDU_CLS
00069 
00070 #include "snmp_pp/config_snmp_pp.h"
00071 #include "snmp_pp/address.h"
00072 #include "snmp_pp/timetick.h"
00073 #include "snmp_pp/octet.h"
00074 #include "snmp_pp/oid.h"
00075 
00076 #ifdef SNMP_PP_NAMESPACE
00077 namespace Snmp_pp {
00078 #endif
00079 
00080 class Vb;
00081 
00082 #define PDU_MAX_RID 32767         ///< max request id to use
00083 #define PDU_MIN_RID 1000          ///< min request id to use
00084 
00085 //=======================================================================
00086 //                   Pdu Class
00087 //=======================================================================
00088 /**
00089  * Pdu class...
00090  */
00091 class DLLOPT Pdu
00092 {
00093  public:
00094 
00095   /**
00096    * Constructor no args.
00097    *
00098    * This constructor creates a valid empty Pdu object.
00099    */
00100   Pdu();
00101 
00102   /**
00103    * Constructor with vbs.
00104    *
00105    * The Pdu class does not take ownership of the array and the Vb
00106    * objects, so if these were allocated with new, they must be freed
00107    * by te user with delete.
00108    *
00109    * @param pvbs      - Array of pointers to Vb objects
00110    * @param pvb_count - Length of the array
00111    */
00112   Pdu(Vb* pvbs, const int pvb_count);
00113 
00114   /**
00115    * Constructor with another Pdu instance.
00116    *
00117    * @param pdu - source pdu object
00118    */
00119   Pdu(const Pdu &pdu) : vb_count(0) { *this = pdu; };
00120 
00121   /**
00122    * Destructor
00123    */
00124   virtual ~Pdu();
00125 
00126   /**
00127    * Overloaded assignment operator.
00128    *
00129    * @param pdu - Pdu that should be assigned to this object
00130    */
00131   Pdu& operator=(const Pdu &pdu);
00132 
00133   /**
00134    * Append a vb to the pdu.
00135    *
00136    * @param vb - The Vb that should be added (as last vb) to the pdu
00137    */
00138   Pdu& operator+=(const Vb &vb);
00139 
00140   /**
00141    * Clone a Pdu object.
00142    *
00143    * @return Pointer to a newly created Pdu object, that is identical to this
00144    */
00145   Pdu *clone() const { return new Pdu(*this); };
00146 
00147   /**
00148    * Get Pointers to all Vbs from Pdu.
00149    *
00150    * The caller has to allocate the array. The returned pointers point
00151    * to the Vb objects that are internally used by the pdu. So any
00152    * changes to the returned Vb objects will change the pdu. If the
00153    * pdu is modified (e.g. through Pdu::trim()) afterwards, the
00154    * returned array will contain invalid pointers.
00155    *
00156    * @param pvbs - Array of empty pointers of size pvb_count
00157    * @param pvb_count - Amount of Vb pointers to get
00158    *
00159    * @return TRUE on success
00160    */
00161   int get_vblist(Vb* pvbs, const int pvb_count) const;
00162 
00163   /**
00164    * Deposit all Vbs to Pdu.
00165    *
00166    * The vb objects of the pdu will be freed and the objects from the
00167    * array will be cloned and added to the pdu. If this method returns
00168    * FALSE, the pdu will not conatin any Vb objects.
00169    *
00170    * @param pvbs - Array of valid pointers of size pvb_count
00171    * @param pvb_count - Amount of Vb pointers i the array
00172    *
00173    * @return TRUE on success
00174    */
00175   int set_vblist(Vb* pvbs, const int pvb_count);
00176 
00177   /**
00178    * Get a particular Vb.
00179    *
00180    * @param vb - Object to store the vb
00181    * @param index - The vb to get (zero is the first vb)
00182    *
00183    * @return TRUE on success
00184    */
00185   int get_vb(Vb &vb, const int index) const;
00186 
00187   /**
00188    * Return a reference to a particular Vb.
00189    *
00190    * @note Before calling this method, make sure that there
00191    *       is a Vb using get_vb_count().
00192    *
00193    * @param index - The Vb to return starting with 0.
00194    * @return A const reference to the Vb
00195    */
00196   const Vb &get_vb(const int index) const { return *vbs[index]; };
00197 
00198   /**
00199    * Set a particular vb.
00200    *
00201    * If this method returns FALSE, the pdu has not been modified.
00202    *
00203    * @param vb - Source vb
00204    * @param index - The vb to set (zero is the first vb)
00205    *
00206    * @return TRUE on success
00207    */
00208   int set_vb(Vb &vb, const int index);
00209 
00210   /**
00211    * Get the number of vbs.
00212    *
00213    * @return The number of Vb objects within the pdu.
00214    */
00215   int get_vb_count() const { return vb_count; };
00216 
00217   /**
00218    * Get a Vb.
00219    *
00220    * @note The index has to be checked by the caller.
00221    *
00222    * @param i zero based index
00223    */
00224   Vb& operator[](int i) { return *vbs[i]; };
00225 
00226   /**
00227    * Get the error status.
00228    *
00229    * @return The SNMP error status
00230    */
00231   int get_error_status() const { return error_status; };
00232 
00233   /**
00234    * Set the error status.
00235    *
00236    * @param err - The SNMP error status.
00237    */
00238   void set_error_status(const int err) {  error_status = err; };
00239 
00240   /**
00241    * Clear the error status.
00242    */
00243   void clear_error_status() { error_status = 0; };
00244 
00245   /**
00246    * Get the error index.
00247    *
00248    * @return The SNMP error index
00249    */
00250   int get_error_index() const { return error_index; };
00251 
00252   /**
00253    * Set the error index.
00254    *
00255    * @param err - The SNMP error index.
00256    */
00257   void set_error_index(const int index) { error_index = index; };
00258 
00259   /**
00260    * Clear the error index.
00261    */
00262   void clear_error_index() { error_index = 0; };
00263 
00264   /**
00265    * Clear error status and error index.
00266    */
00267   void clear_error() { set_error_status(0); set_error_index(0); }
00268 
00269   /**
00270    * Get the request id.
00271    *
00272    * @return The SNMP request id
00273    */
00274   unsigned long get_request_id() const { return request_id; };
00275 
00276   /**
00277    * Set the request id.
00278    *
00279    * @param rid - The SNMP request id
00280    */
00281   void set_request_id(const unsigned long rid) { request_id = rid; };
00282 
00283   /**
00284    * Get the pdu type.
00285    */
00286   unsigned short get_type() const { return pdu_type; };
00287 
00288   /**
00289    * Set the pdu type.
00290    */
00291   void set_type(unsigned short type) { pdu_type = type; };
00292 
00293   /**
00294    * Returns validity of Pdu instance.
00295    */
00296   bool valid() const { return validity; };
00297 
00298   /**
00299    * Trim off vbs.
00300    *
00301    * @param count - number of vbs to trim of, starting with the last
00302    * @return TRUE on success, FALSE if nothing was done
00303    */
00304   int trim(const int count=1);
00305 
00306   /**
00307    * Delete a Vb anywhere within the Pdu.
00308    *
00309    * @param position - Delete the Vb at this position (starting with 0)
00310    * @return TRUE on success
00311    */
00312   int delete_vb(const int position);
00313 
00314   /**
00315    * Set notify timestamp.
00316    */
00317   void set_notify_timestamp(const TimeTicks &ts) { notify_timestamp = ts; };
00318 
00319   /**
00320    * Get notify timestamp.
00321    */
00322   void get_notify_timestamp(TimeTicks &ts) const { ts = notify_timestamp; };
00323 
00324   /**
00325    * Set the notify id.
00326    *
00327    * @return true if the set succeeded.
00328    */
00329   bool set_notify_id(const Oid &id)
00330     { notify_id = id; return (notify_id.len() == id.len()); };
00331 
00332   /**
00333    * Get the notify id.
00334    *
00335    * @return true if the get succeeded.
00336    */
00337   bool get_notify_id(Oid &id) const
00338     { id = notify_id; return (notify_id.len() == id.len()); };
00339 
00340   /**
00341    * Set the notify enterprise.
00342    *
00343    * @return true if the set succeeded.
00344    */
00345   bool set_notify_enterprise(const Oid &e)
00346     { notify_enterprise = e; return (notify_enterprise.len() == e.len()); };
00347 
00348   /**
00349    * Get the notify enterprise.
00350    *
00351    * @return true if the get succeeded.
00352    */
00353   bool get_notify_enterprise(Oid & e) const
00354     { e = notify_enterprise;  return (notify_enterprise.len() == e.len()); };
00355 
00356 #ifdef _SNMPv3
00357   /**
00358    * Set the security level that should be used when this Pdu is sent.
00359    * The default security level of a Pdu is SNMP_SECURITY_LEVEL_NOAUTH_NOPRIV.
00360    *
00361    * @param level - One of SNMP_SECURITY_LEVEL_NOAUTH_NOPRIV,
00362    *                SNMP_SECURITY_LEVEL_AUTH_NOPRIV,
00363    *                SNMP_SECURITY_LEVEL_AUTH_PRIV
00364    */
00365   void set_security_level(const int level) { security_level = level; };
00366 
00367   /**
00368    * Return the security level of the Pdu.
00369    *
00370    * @return - the security level
00371    */
00372   int get_security_level() const { return security_level; };
00373 
00374   /**
00375    * Set the context name of the Pdu.
00376    *
00377    * @param name - The context name
00378    */
00379   bool set_context_name(const OctetStr &name)
00380     { context_name = name; return (context_name.valid() && name.valid()); };
00381 
00382   /**
00383    * Set the context name of the Pdu.
00384    *
00385    * @param name - The context name
00386    */
00387   bool set_context_name(const char *name)
00388     { context_name = name; return context_name.valid(); };
00389 
00390   /**
00391    * Get the context name of the Pdu.
00392    *
00393    * @param name - Object fot the context name
00394    */
00395   bool get_context_name(OctetStr &name) const
00396     { name = context_name; return (context_name.valid() && name.valid()); };
00397 
00398   /**
00399    * Get the context name of the Pdu.
00400    *
00401    * @return - Return the context name as an OctetStr
00402    */
00403   const OctetStr& get_context_name() const { return context_name; };
00404 
00405   /**
00406    * Set the context engine id of the Pdu.
00407    *
00408    * @param id - The new context engine id
00409    */
00410   bool set_context_engine_id(const OctetStr &id) { context_engine_id = id;
00411     return (context_engine_id.valid() && id.valid()); };
00412 
00413   /**
00414    * Set the context engine id of the Pdu.
00415    *
00416    * @param id - The new context engine id
00417    */
00418   bool set_context_engine_id(const char *id)
00419     { context_engine_id = id; return context_engine_id.valid(); };
00420 
00421   /**
00422    * Get the context engine id of the Pdu.
00423    *
00424    * @param id - Object for the context engine
00425    */
00426   bool get_context_engine_id(OctetStr &id) const { id = context_engine_id;
00427     return (context_engine_id.valid() && id.valid()); };
00428 
00429   /**
00430    * Get the context engine id of the Pdu.
00431    *
00432    * @return - Return the context engine id as an OctetStr
00433    */
00434   const OctetStr& get_context_engine_id() const { return context_engine_id; };
00435 
00436   /**
00437    * Set the SNMPv3 message id (msgID)
00438    *
00439    * @param msg_id - the message id of the received message
00440    */
00441   void set_message_id(const unsigned long msg_id) { message_id = msg_id; }
00442 
00443   /**
00444    * Get the SNMPv3 message id (msgID)
00445    *
00446    * @return - the message id of the received message
00447    */
00448   unsigned long get_message_id() const { return message_id; }
00449 
00450   /**
00451    * Set the maximum size of the scoped pdu to be included in a
00452    * possible response message.
00453    *
00454    * @param l - the maximum size
00455    */
00456   void set_maxsize_scopedpdu(unsigned long l) { maxsize_scopedpdu = l; };
00457 
00458   /**
00459    * Get the maximum size of the scoped pdu to be included in a
00460    * possible response message.
00461    *
00462    * @return - the maximum size
00463    */
00464   unsigned long get_maxsize_scopedpdu() const { return maxsize_scopedpdu; };
00465 
00466 #endif // _SNMPv3
00467 
00468   /**
00469    * Get the SNMPv1 trap address
00470    */
00471   int get_v1_trap_address(GenAddress &address) const;
00472 
00473   /**
00474    * Set the SNMPv1 trap address
00475    */
00476   int set_v1_trap_address(const Address &address);
00477 
00478   /**
00479    * Return the length of the encoded vbs with pdu header.
00480    *
00481    * @note this method wll not work for v1 traps.
00482    */
00483   int get_asn1_length() const;
00484 
00485   /**
00486    * Clear the Pdu contents (destruct and construct in one go)
00487    */
00488   void clear();
00489 
00490   /**
00491    * Does the type of response match the type of request.
00492    */
00493   static bool match_type(const int request, const int response);
00494 
00495   //-------------[ protected instance variables ]--------------------------
00496  protected:
00497   Vb *vbs[PDU_MAX_VBS];        // pointer to array of Vbs
00498   int vb_count;                // count of Vbs
00499   int error_status;            // SMI error status
00500   int error_index;             // SMI error index
00501   bool validity;               // valid boolean
00502   unsigned long request_id;      // SMI request id
00503   unsigned short pdu_type;       // derived at run time based on request type
00504   // for notify Pdu objects only
00505   // traps & notifies
00506   TimeTicks notify_timestamp;      // a timestamp associated with an infor
00507   Oid notify_id;                   // an id
00508   Oid notify_enterprise;
00509   GenAddress v1_trap_address;      // address object
00510   int        v1_trap_address_set;
00511 #ifdef _SNMPv3
00512   // specific Objects for SNMPv3
00513   int security_level;            // the securityLevel with which this Pdu
00514                                  // should be sent or was received
00515   unsigned long message_id;
00516   unsigned long maxsize_scopedpdu;
00517   OctetStr context_name;
00518   OctetStr context_engine_id;
00519 #endif // _SNMPv3
00520 };
00521 
00522 #ifdef SNMP_PP_NAMESPACE
00523 }; // end of namespace Snmp_pp
00524 #endif 
00525 
00526 #endif //_PDU_CLS

Generated on Tue Jan 4 22:42:14 2005 for SNMP++ by doxygen 1.3.2