psnmp.h

Go to the documentation of this file.
00001 /*
00002  * psnmp.h
00003  *
00004  * Simple Network Management Protocol classes.
00005  *
00006  * Portable Windows Library
00007  *
00008  * Copyright (c) 1993-2002 Equivalence Pty. Ltd.
00009  *
00010  * The contents of this file are subject to the Mozilla Public License
00011  * Version 1.0 (the "License"); you may not use this file except in
00012  * compliance with the License. You may obtain a copy of the License at
00013  * http://www.mozilla.org/MPL/
00014  *
00015  * Software distributed under the License is distributed on an "AS IS"
00016  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
00017  * the License for the specific language governing rights and limitations
00018  * under the License.
00019  *
00020  * The Original Code is Portable Windows Library.
00021  *
00022  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
00023  *
00024  * Contributor(s): ______________________________________.
00025  *
00026  * $Log: psnmp.h,v $
00027  * Revision 1.9  2002/11/06 22:47:24  robertj
00028  * Fixed header comment (copyright etc)
00029  *
00030  * Revision 1.8  2002/09/16 01:08:59  robertj
00031  * Added #define so can select if #pragma interface/implementation is used on
00032  *   platform basis (eg MacOS) rather than compiler, thanks Robert Monaghan.
00033  *
00034  * Revision 1.7  1999/03/09 08:01:46  robertj
00035  * Changed comments for doc++ support (more to come).
00036  *
00037  * Revision 1.6  1999/02/16 08:07:10  robertj
00038  * MSVC 6.0 compatibility changes.
00039  *
00040  * Revision 1.5  1998/11/30 02:50:55  robertj
00041  * New directory structure
00042  *
00043  * Revision 1.4  1998/09/23 06:27:38  robertj
00044  * Added open source copyright license.
00045  *
00046  * Revision 1.3  1996/11/04 03:56:16  robertj
00047  * Added selectable read buffer size.
00048  *
00049  * Revision 1.2  1996/09/20 12:19:36  robertj
00050  * Used read timeout instead of member variable.
00051  *
00052  * Revision 1.1  1996/09/14 12:58:57  robertj
00053  * Initial revision
00054  *
00055  * Revision 1.6  1996/05/09 13:23:49  craigs
00056  * Added trap functions
00057  *
00058  * Revision 1.5  1996/04/23 12:12:46  craigs
00059  * Changed to use GetErrorText function
00060  *
00061  * Revision 1.4  1996/04/16 13:20:43  craigs
00062  * Final version prior to beta1 release
00063  *
00064  * Revision 1.3  1996/04/15 09:05:30  craigs
00065  * Latest version prior to integration with Robert's changes
00066  *
00067  * Revision 1.2  1996/04/01 12:36:12  craigs
00068  * Fixed RCS header, added IPAddress functions
00069  *
00070  * Revision 1.1  1996/03/02 06:49:51  craigs
00071  * Initial revision
00072  *
00073  */
00074 
00075 #ifndef _PSNMP_H
00076 #define _PSNMP_H
00077 
00078 #ifdef P_USE_PRAGMA
00079 #pragma interface
00080 #endif
00081 
00082 #include <ptlib/sockets.h>
00083 #include <ptclib/pasn.h>
00084 
00085 
00087 
00090 class PSNMPVarBindingList : public PObject
00091 {
00092   PCLASSINFO(PSNMPVarBindingList, PObject)
00093   public:
00094 
00095     void Append(const PString & objectID);
00096     void Append(const PString & objectID, PASNObject * obj);
00097     void AppendString(const PString & objectID, const PString & str);
00098 
00099     void RemoveAll();
00100 
00101     PINDEX GetSize() const;
00102 
00103     PString GetObjectID(PINDEX idx) const;
00104     PASNObject & operator[](PINDEX idx) const;
00105 
00106     void PrintOn(ostream & strm) const;
00107 
00108   protected:
00109     PStringList     objectIds;
00110     PASNObjectList  values;
00111 };
00112 
00114 
00117 class PSNMP : public PIndirectChannel
00118 {
00119   PCLASSINFO(PSNMP, PIndirectChannel)
00120   public:
00121     enum ErrorType {
00122        // Standard RFC1157 errors
00123        NoError        = 0,
00124        TooBig         = 1,
00125        NoSuchName     = 2,
00126        BadValue       = 3,
00127        ReadOnly       = 4,
00128        GenErr         = 5,
00129 
00130        // Additional errors
00131        NoResponse,
00132        MalformedResponse,
00133        SendFailed,
00134        RxBufferTooSmall,
00135        TxDataTooBig,
00136        NumErrors
00137     };
00138 
00139     enum RequestType {
00140        GetRequest     = 0,
00141        GetNextRequest = 1,
00142        GetResponse    = 2,
00143        SetRequest     = 3,
00144        Trap           = 4,
00145     };
00146 
00147     enum { TrapPort = 162 };
00148 
00149     enum TrapType {
00150       ColdStart             = 0,
00151       WarmStart             = 1,
00152       LinkDown              = 2,
00153       LinkUp                = 3,
00154       AuthenticationFailure = 4,
00155       EGPNeighbourLoss      = 5,
00156       EnterpriseSpecific    = 6,
00157       NumTrapTypes
00158     };
00159 
00160     static PString GetErrorText(ErrorType err);
00161 
00162     static PString GetTrapTypeText(PINDEX code);
00163 
00164     static void SendEnterpriseTrap (
00165                  const PIPSocket::Address & addr,
00166                             const PString & community,
00167                             const PString & enterprise,
00168                                      PINDEX specificTrap,
00169                                PASNUnsigned timeTicks,
00170                                        WORD sendPort = TrapPort);
00171 
00172     static void SendEnterpriseTrap (
00173                  const PIPSocket::Address & addr,
00174                             const PString & community,
00175                             const PString & enterprise,
00176                                      PINDEX specificTrap,
00177                                PASNUnsigned timeTicks,
00178                 const PSNMPVarBindingList & vars,
00179                                        WORD sendPort = TrapPort);
00180 
00181     static void SendTrap (
00182                        const PIPSocket::Address & addr,
00183                                   PSNMP::TrapType trapType,
00184                                   const PString & community,
00185                                   const PString & enterprise,
00186                                            PINDEX specificTrap,
00187                                      PASNUnsigned timeTicks,
00188                       const PSNMPVarBindingList & vars,
00189                                              WORD sendPort = TrapPort);
00190 
00191     static void SendTrap (
00192                       const PIPSocket::Address & addr,
00193                                   PSNMP::TrapType trapType,
00194                                   const PString & community,
00195                                   const PString & enterprise,
00196                                            PINDEX specificTrap,
00197                                      PASNUnsigned timeTicks,
00198                       const PSNMPVarBindingList & vars,
00199                        const PIPSocket::Address & agentAddress,
00200                                              WORD sendPort = TrapPort);
00201                             
00202     static void WriteTrap (           PChannel & channel,
00203                                   PSNMP::TrapType trapType,
00204                                   const PString & community,
00205                                   const PString & enterprise,
00206                                            PINDEX specificTrap,
00207                                      PASNUnsigned timeTicks,
00208                       const PSNMPVarBindingList & vars,
00209                        const PIPSocket::Address & agentAddress);
00210 
00211     static BOOL DecodeTrap(const PBYTEArray & readBuffer,
00212                                        PINDEX & version,
00213                                       PString & community,
00214                                       PString & enterprise,
00215                            PIPSocket::Address & address,
00216                                        PINDEX & genericTrapType,
00217                                       PINDEX  & specificTrapType,
00218                                  PASNUnsigned & timeTicks,
00219                           PSNMPVarBindingList & varsOut);
00220 };
00221 
00222 
00224 
00227 class PSNMPClient : public PSNMP
00228 {
00229   PCLASSINFO(PSNMPClient, PSNMP)
00230   public:
00231     PSNMPClient(const PString & host,
00232                 PINDEX retryMax = 5,
00233                 PINDEX timeoutMax = 5,
00234                 PINDEX rxBufferSize = 1500,
00235                 PINDEX txSize = 484);
00236 
00237     PSNMPClient(PINDEX retryMax = 5,
00238                 PINDEX timeoutMax = 5,
00239                 PINDEX rxBufferSize = 1500,
00240                 PINDEX txSize = 484);
00241 
00242     void SetVersion(PASNInt version);
00243     PASNInt GetVersion() const;
00244 
00245     void SetCommunity(const PString & str);
00246     PString GetCommunity() const;
00247 
00248     void SetRequestID(PASNInt requestID);
00249     PASNInt GetRequestID() const;
00250 
00251     BOOL WriteGetRequest (PSNMPVarBindingList & varsIn,
00252                           PSNMPVarBindingList & varsOut);
00253 
00254     BOOL WriteGetNextRequest (PSNMPVarBindingList & varsIn,
00255                               PSNMPVarBindingList & varsOut);
00256 
00257     BOOL WriteSetRequest (PSNMPVarBindingList & varsIn,
00258                           PSNMPVarBindingList & varsOut);
00259 
00260     ErrorType GetLastErrorCode() const;
00261     PINDEX    GetLastErrorIndex() const;
00262     PString   GetLastErrorText() const;
00263 
00264   protected:
00265     BOOL WriteRequest (PASNInt requestCode,
00266                        PSNMPVarBindingList & varsIn,
00267                        PSNMPVarBindingList & varsOut);
00268 
00269 
00270     BOOL ReadRequest(PBYTEArray & readBuffer);
00271 
00272     PString   hostName;
00273     PString   community;
00274     PASNInt   requestId;
00275     PASNInt   version;
00276     PINDEX    retryMax;
00277     PINDEX    lastErrorIndex;
00278     ErrorType lastErrorCode;
00279     PBYTEArray readBuffer;
00280     PINDEX     maxRxSize;
00281     PINDEX     maxTxSize;
00282 };
00283 
00284 
00286 
00289 class PSNMPServer : public PSNMP
00290 {
00291   PCLASSINFO(PSNMPServer, PSNMP)
00292   public:
00293 
00294     virtual void OnGetRequest     (PSNMPVarBindingList & vars);
00295     virtual void OnGetNextRequest (PSNMPVarBindingList & vars);
00296     virtual void OnSetRequest     (PSNMPVarBindingList & vars);
00297 
00298     BOOL SendGetResponse          (PSNMPVarBindingList & vars);
00299 };
00300 
00301 #endif
00302 
00303 
00304 // End of File.

Generated on Fri Sep 21 14:40:11 2007 for PWLib by  doxygen 1.5.3