AgentXcpp  Revision:4ac4848
Internals Documentation
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends
/home/tanjeff/projekte/agentxcpp/src/oid.hpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2011 Tanjeff-Nicolai Moos <tanjeff@cccmz.de>
00003  *
00004  * This file is part of the agentXcpp library.
00005  *
00006  * AgentXcpp is free software: you can redistribute it and/or modify
00007  * it under the terms of the AgentXcpp library license, version 1, which 
00008  * consists of the GNU General Public License and some additional 
00009  * permissions.
00010  *
00011  * AgentXcpp is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * See the AgentXcpp library license in the LICENSE file of this package 
00017  * for more details.
00018  */
00019 
00020 #ifndef _OID_H_
00021 #define _OID_H_
00022 
00023 #include <vector>
00024 #include <ostream>
00025 #include "variable.hpp"
00026 #include "types.hpp"
00027 
00028 namespace agentxcpp
00029 {
00030     /**
00031      * \brief Represents an SNMP object identifier (OID)
00032      *
00033      * This class represents an OID. OID's are sequences of integers. This 
00034      * class inherits from std:vector<>, which means that an oid object can be 
00035      * manipulated much the same way as a std::vector<> can be manipulated.
00036      *
00037      * In addition, the OID class provides constructors taking a number of 
00038      * integers to ease creatng such objects. For example, this works:
00039      *
00040      * \code
00041      * oid myCompany = oid(1,3,6,1,4,1,355);
00042      * \endcode
00043      *
00044      * Also a constructor is provided which takes an oid and a number of 
00045      * integers, so this works also:
00046      *
00047      * \code
00048      * oid myObject = oid(myCompany,1,1,3,42);
00049      * \endcode
00050      *
00051      * In addition, some common oid's are provided as constants, e.g. 
00052      * 'enterprises', so the following will also work:
00053      *
00054      * \code
00055      * oid myCompany = oid(enterprises,355);
00056      * \endcode
00057      */
00058     class oid: public variable, public std::vector<uint32_t>
00059     {
00060         private:
00061 
00062             /**
00063              * \brief the 'include' field.
00064              */
00065             bool include;
00066 
00067         public:
00068 
00069             /**
00070              * \brief Initialize an oid object with a sequence of up to 20
00071              *        subidentifiers.
00072              *
00073              * This constructor takes up to 20 subidentifiers which forms up 
00074              * the object identifier. More subidentifiers can be added using 
00075              * functions from the vector<> class.
00076              *
00077              * The 'include' field is initialized to 'false'.
00078              *
00079              * This is also the default constructor.
00080              *
00081              * \note Zero (0) is not allowed for a subidentifier, i.e. the
00082              *       first subidentifier with value 0 and all its successors 
00083              *       are ignored.
00084              */
00085             oid(uint32_t  c1=0, uint32_t  c2=0, uint32_t  c3=0,
00086                 uint32_t  c4=0, uint32_t  c5=0, uint32_t  c6=0,
00087                 uint32_t  c7=0, uint32_t  c8=0, uint32_t  c9=0,
00088                 uint32_t c10=0, uint32_t c11=0, uint32_t c12=0,
00089                 uint32_t c13=0, uint32_t c14=0, uint32_t c15=0,
00090                 uint32_t c16=0, uint32_t c17=0, uint32_t c18=0,
00091                 uint32_t c19=0, uint32_t c20=0 );
00092 
00093             /**
00094              * \brief Initialize an oid object with another oid plus a sequence 
00095              * of up to 19 subidentifiers.
00096              *
00097              * The 'include' field is copied from o.
00098              *
00099              * This is also the copy constructor.
00100              *
00101              * \note Zero (0) is not allowed for a subidentifier, i.e. the
00102              *       first subidentifier with value 0 and all its successors 
00103              *       are ignored.
00104              */
00105             oid(const oid& o,
00106                 uint32_t  c1=0, uint32_t  c2=0, uint32_t  c3=0,
00107                 uint32_t  c4=0, uint32_t  c5=0, uint32_t  c6=0,
00108                 uint32_t  c7=0, uint32_t  c8=0, uint32_t  c9=0,
00109                 uint32_t c10=0, uint32_t c11=0, uint32_t c12=0,
00110                 uint32_t c13=0, uint32_t c14=0, uint32_t c15=0,
00111                 uint32_t c16=0, uint32_t c17=0, uint32_t c18=0,
00112                 uint32_t c19=0 );
00113 
00114             /**
00115              * \brief Assignment operator
00116              */
00117             oid& operator=(const oid& oid);
00118 
00119 
00120             /**
00121              * \internal
00122              *
00123              * \brief get the current include value
00124              *
00125              * The include value is present in the serialized form of an OID.  
00126              * If an OID object is created by parsing a AgentX message, the 
00127              * 'include' member is set accordingly.
00128              *
00129              * See RFC 2741, sections 5.1 and 5.2 for details.
00130              */
00131             bool get_include()
00132             {
00133                 return include;
00134             }
00135 
00136             /**
00137              * \internal
00138              *
00139              * \brief set the include value
00140              *
00141              * The include value is present in the serialized form of an OID.  
00142              * If an OID object is serialized, the include field is encoded 
00143              * into the stream.
00144              *
00145              * See RFC 2741, sections 5.1 and 5.2 for details.
00146              */
00147             void set_include(bool i)
00148             {
00149                 include = i;
00150             }
00151 
00152             /**
00153              * \internal
00154              *
00155              * \brief Encode an OID object as described in RFC 2741,
00156              *        section 5.1.
00157              */
00158             data_t serialize() const;
00159 
00160             /**
00161              * \internal
00162              *
00163              * \brief Construct the object from input stream
00164              *
00165              * This constructor parses the serialized form of the object.
00166              * It takes an iterator, starts parsing at the position of the 
00167              * iterator and advances the iterator to the position right behind 
00168              * the object.
00169              *
00170              * The constructor expects valid data from the stream; if parsing 
00171              * fails, parse_error is thrown. In this case, the iterator is left 
00172              * at an undefined position.
00173              *
00174              * \param pos Iterator pointing to the current stream position.
00175              *            The iterator is advanced while reading the header.
00176              *
00177              * \param end Iterator pointing one element past the end of the
00178              *            current stream. This is needed to mark the end of the 
00179              *            buffer.
00180              *
00181              * \param big_endian Whether the input stream is in big endian
00182              *                   format
00183              *
00184              * \exception parse_error If parsing fails. In this case, the
00185              *                        iterator is left at an undefined 
00186              *                        position.
00187              */
00188             oid(data_t::const_iterator& pos,
00189                 const data_t::const_iterator& end,
00190                 bool big_endian=true);
00191 
00192             /**
00193              * \brief The less-than operator
00194              *
00195              * An OID is less than another OID if either the first 
00196              * not-identical part is lesser or if all parts are identical and 
00197              * it has lesser parts.
00198              *
00199              * Example:\n
00200              * 1.3.6.1.4.1.42.3.3.1 \n
00201              * is less than \n
00202              * 1.3.6.1.4.1.42.3.4.1 \n
00203              * Note the next to last number.
00204              *
00205              * Also,\n
00206              * 1.3.6.1.4.1.42.3.3.1 \n
00207              * is less than \n
00208              * 1.3.6.1.4.1.42.3.3.1.1 \n
00209              * because it is shorter.
00210              *
00211              * However, \n
00212              * 1.3.6.1.4.1.42.3.3.1 \n
00213              * is greater than \n
00214              * 1.3.6.1.4.1.42.3.2.1.1 \n
00215              * because the 9th number is greater (although the first OID has 
00216              * less numbers than the second).
00217              */
00218             bool operator<(const oid& o) const;
00219 
00220             /**
00221              * \brief The equal-operator
00222              *
00223              * See operator<() for a more detailed description about comparing 
00224              * OIDs.
00225              */
00226             bool operator==(const oid& o) const;
00227 
00228             /**
00229              * \brief The not-equal-operator for oids
00230              *
00231              * See operator<() for a more detailed description about comparing 
00232              * OIDs.
00233              */
00234             bool operator!=(const oid& o) const
00235             {
00236                 return ! (*this == o);
00237             }
00238 
00239             /**
00240              * \brief The greater-than operator
00241              *
00242              * See operator<() for a more detailed description about comparing 
00243              * OIDs.
00244              */
00245             bool operator>(const oid& o) const
00246             {
00247                 // a > b is the same as b < a :-)
00248                 return o < *this;
00249             }
00250 
00251             friend std::ostream& operator<<(std::ostream&,
00252                                             const agentxcpp::oid&);
00253     };
00254 
00255     /**
00256      * \brief The output operator for the oid class.
00257      *
00258      * Object identifiers (oid objects) can be output as follows:
00259      * 
00260      * \code
00261      * oid led_state(enterprise,1,3,3,1);
00262      * cout << "LED state OID: " << led_state << endl;
00263      * \endcode
00264      *
00265      * The last line will output "LED state OID:  .1.3.6.1.4.1.3.3.1".
00266      */
00267     std::ostream& operator<<(std::ostream&, const agentxcpp::oid&);
00268 
00269 
00270     // Some oid's according to RFC 1155:
00271     const oid iso(1);
00272     const oid ccitt(0);
00273     const oid joint_iso_ccitt(2);
00274     const oid org(iso,3);
00275     const oid dod(org,6);
00276     const oid internet(dod,1);
00277     const oid directory(internet,1);
00278     const oid mgmt(internet,2);
00279     const oid experimental(internet,3);
00280     // 'private' is a C++ keyword, thus we use private_:
00281     const oid private_(internet,4);
00282     const oid enterprises(private_, 1);
00283 }
00284 
00285 
00286 #endif