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.10  2007/08/02 18:48:35  shorne
00028  * Added SNMP Server support
00029  *
00030  * Revision 1.9  2002/11/06 22:47:24  robertj
00031  * Fixed header comment (copyright etc)
00032  *
00033  * Revision 1.8  2002/09/16 01:08:59  robertj
00034  * Added #define so can select if #pragma interface/implementation is used on
00035  *   platform basis (eg MacOS) rather than compiler, thanks Robert Monaghan.
00036  *
00037  * Revision 1.7  1999/03/09 08:01:46  robertj
00038  * Changed comments for doc++ support (more to come).
00039  *
00040  * Revision 1.6  1999/02/16 08:07:10  robertj
00041  * MSVC 6.0 compatibility changes.
00042  *
00043  * Revision 1.5  1998/11/30 02:50:55  robertj
00044  * New directory structure
00045  *
00046  * Revision 1.4  1998/09/23 06:27:38  robertj
00047  * Added open source copyright license.
00048  *
00049  * Revision 1.3  1996/11/04 03:56:16  robertj
00050  * Added selectable read buffer size.
00051  *
00052  * Revision 1.2  1996/09/20 12:19:36  robertj
00053  * Used read timeout instead of member variable.
00054  *
00055  * Revision 1.1  1996/09/14 12:58:57  robertj
00056  * Initial revision
00057  *
00058  * Revision 1.6  1996/05/09 13:23:49  craigs
00059  * Added trap functions
00060  *
00061  * Revision 1.5  1996/04/23 12:12:46  craigs
00062  * Changed to use GetErrorText function
00063  *
00064  * Revision 1.4  1996/04/16 13:20:43  craigs
00065  * Final version prior to beta1 release
00066  *
00067  * Revision 1.3  1996/04/15 09:05:30  craigs
00068  * Latest version prior to integration with Robert's changes
00069  *
00070  * Revision 1.2  1996/04/01 12:36:12  craigs
00071  * Fixed RCS header, added IPAddress functions
00072  *
00073  * Revision 1.1  1996/03/02 06:49:51  craigs
00074  * Initial revision
00075  *
00076  */
00077 
00078 #ifndef _PSNMP_H
00079 #define _PSNMP_H
00080 
00081 #ifdef P_USE_PRAGMA
00082 #pragma interface
00083 #endif
00084 
00085 #ifdef P_SNMP
00086 
00087 #include <ptlib/sockets.h>
00088 #include <ptclib/snmp.h>
00089 #include <ptclib/pasn.h>
00090 
00091 #include <list>
00092 #include <vector>
00093 
00095 
00098 class PSNMPVarBindingList : public PObject
00099 {
00100   PCLASSINFO(PSNMPVarBindingList, PObject)
00101   public:
00102 
00103     void Append(const PString & objectID);
00104     void Append(const PString & objectID, PASNObject * obj);
00105     void AppendString(const PString & objectID, const PString & str);
00106 
00107     void RemoveAll();
00108 
00109     PINDEX GetSize() const;
00110 
00111     PString GetObjectID(PINDEX idx) const;
00112     PASNObject & operator[](PINDEX idx) const;
00113 
00114     void PrintOn(ostream & strm) const;
00115 
00116   protected:
00117     PStringList     objectIds;
00118     PASNObjectList  values;
00119 };
00120 
00122 
00125 class PSNMP : public PIndirectChannel
00126 {
00127   PCLASSINFO(PSNMP, PIndirectChannel)
00128   public:
00129     enum ErrorType {
00130        // Standard RFC1157 errors
00131        NoError        = 0,
00132        TooBig         = 1,
00133        NoSuchName     = 2,
00134        BadValue       = 3,
00135        ReadOnly       = 4,
00136        GenErr         = 5,
00137 
00138        // Additional errors
00139        NoResponse,
00140        MalformedResponse,
00141        SendFailed,
00142        RxBufferTooSmall,
00143        TxDataTooBig,
00144        NumErrors
00145     };
00146 
00147     enum RequestType {
00148        GetRequest     = 0,
00149        GetNextRequest = 1,
00150        GetResponse    = 2,
00151        SetRequest     = 3,
00152        Trap           = 4,
00153     };
00154 
00155     enum { TrapPort = 162 };
00156 
00157     enum TrapType {
00158       ColdStart             = 0,
00159       WarmStart             = 1,
00160       LinkDown              = 2,
00161       LinkUp                = 3,
00162       AuthenticationFailure = 4,
00163       EGPNeighbourLoss      = 5,
00164       EnterpriseSpecific    = 6,
00165       NumTrapTypes
00166     };
00167 
00168     static PString GetErrorText(ErrorType err);
00169 
00170     static PString GetTrapTypeText(PINDEX code);
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                                        WORD sendPort = TrapPort);
00179 
00180     static void SendEnterpriseTrap (
00181                  const PIPSocket::Address & addr,
00182                             const PString & community,
00183                             const PString & enterprise,
00184                                      PINDEX specificTrap,
00185                                PASNUnsigned timeTicks,
00186                 const PSNMPVarBindingList & vars,
00187                                        WORD sendPort = TrapPort);
00188 
00189     static void SendTrap (
00190                        const PIPSocket::Address & addr,
00191                                   PSNMP::TrapType trapType,
00192                                   const PString & community,
00193                                   const PString & enterprise,
00194                                            PINDEX specificTrap,
00195                                      PASNUnsigned timeTicks,
00196                       const PSNMPVarBindingList & vars,
00197                                              WORD sendPort = TrapPort);
00198 
00199     static void SendTrap (
00200                       const PIPSocket::Address & addr,
00201                                   PSNMP::TrapType trapType,
00202                                   const PString & community,
00203                                   const PString & enterprise,
00204                                            PINDEX specificTrap,
00205                                      PASNUnsigned timeTicks,
00206                       const PSNMPVarBindingList & vars,
00207                        const PIPSocket::Address & agentAddress,
00208                                              WORD sendPort = TrapPort);
00209                             
00210     static void WriteTrap (           PChannel & channel,
00211                                   PSNMP::TrapType trapType,
00212                                   const PString & community,
00213                                   const PString & enterprise,
00214                                            PINDEX specificTrap,
00215                                      PASNUnsigned timeTicks,
00216                       const PSNMPVarBindingList & vars,
00217                        const PIPSocket::Address & agentAddress);
00218 
00219     static BOOL DecodeTrap(const PBYTEArray & readBuffer,
00220                                        PINDEX & version,
00221                                       PString & community,
00222                                       PString & enterprise,
00223                            PIPSocket::Address & address,
00224                                        PINDEX & genericTrapType,
00225                                       PINDEX  & specificTrapType,
00226                                  PASNUnsigned & timeTicks,
00227                           PSNMPVarBindingList & varsOut);
00228 
00229         typedef list<pair<PString,PRFC1155_ObjectSyntax> > BindingList;
00230 };
00231 
00232 
00234 
00237 class PSNMPClient : public PSNMP
00238 {
00239   PCLASSINFO(PSNMPClient, PSNMP)
00240   public:
00241     PSNMPClient(const PString & host,
00242                 PINDEX retryMax = 5,
00243                 PINDEX timeoutMax = 5,
00244                 PINDEX rxBufferSize = 1500,
00245                 PINDEX txSize = 484);
00246 
00247     PSNMPClient(PINDEX retryMax = 5,
00248                 PINDEX timeoutMax = 5,
00249                 PINDEX rxBufferSize = 1500,
00250                 PINDEX txSize = 484);
00251 
00252     void SetVersion(PASNInt version);
00253     PASNInt GetVersion() const;
00254 
00255     void SetCommunity(const PString & str);
00256     PString GetCommunity() const;
00257 
00258     void SetRequestID(PASNInt requestID);
00259     PASNInt GetRequestID() const;
00260 
00261     BOOL WriteGetRequest (PSNMPVarBindingList & varsIn,
00262                           PSNMPVarBindingList & varsOut);
00263 
00264     BOOL WriteGetNextRequest (PSNMPVarBindingList & varsIn,
00265                               PSNMPVarBindingList & varsOut);
00266 
00267     BOOL WriteSetRequest (PSNMPVarBindingList & varsIn,
00268                           PSNMPVarBindingList & varsOut);
00269 
00270     ErrorType GetLastErrorCode() const;
00271     PINDEX    GetLastErrorIndex() const;
00272     PString   GetLastErrorText() const;
00273 
00274   protected:
00275     BOOL WriteRequest (PASNInt requestCode,
00276                        PSNMPVarBindingList & varsIn,
00277                        PSNMPVarBindingList & varsOut);
00278 
00279 
00280     BOOL ReadRequest(PBYTEArray & readBuffer);
00281 
00282     PString   hostName;
00283     PString   community;
00284     PASNInt   requestId;
00285     PASNInt   version;
00286     PINDEX    retryMax;
00287     PINDEX    lastErrorIndex;
00288     ErrorType lastErrorCode;
00289     PBYTEArray readBuffer;
00290     PINDEX     maxRxSize;
00291     PINDEX     maxTxSize;
00292 };
00293 
00294 
00296 
00299 class PSNMPServer : public PSNMP, PThread
00300 {
00301   PCLASSINFO(PSNMPServer, PSNMP)
00302   public:
00303 
00304     PSNMPServer(PIPSocket::Address binding = PIPSocket::GetDefaultIpAny(), 
00305                         WORD localPort = 161,   
00306                                 PINDEX timeout = 5000, 
00307                                 PINDEX rxSize = 10000, 
00308                                 PINDEX txSize = 10000);
00309 
00310         ~PSNMPServer();
00311 
00312         void Main();
00313 
00314         void SetVersion(PASNInt newVersion);
00315         BOOL HandleChannel();
00316         int ProcessPDU(const PBYTEArray & readBuffer, PBYTEArray & writeBuffer);
00317 
00318         virtual BOOL Authorise(const PIPSocket::Address & received);
00319 
00320         virtual BOOL OnGetRequest     (PINDEX reqID, PSNMP::BindingList & vars, PSNMP::ErrorType & errCode);
00321         virtual BOOL OnGetNextRequest (PINDEX reqID, PSNMP::BindingList & vars, PSNMP::ErrorType & errCode);
00322         virtual BOOL OnSetRequest     (PINDEX reqID, PSNMP::BindingList & vars, PSNMP::ErrorType & errCode);
00323 
00324     BOOL SendGetResponse          (PSNMPVarBindingList & vars);
00325   
00326   protected:
00327     PString   community;
00328     PASNInt   version;
00329     PINDEX    lastErrorIndex;
00330     ErrorType lastErrorCode;
00331     PBYTEArray readBuffer;
00332     PINDEX     maxRxSize;
00333     PINDEX     maxTxSize;
00334         PUDPSocket * baseSocket;
00335 };
00336 
00337 #endif // P_SNMP
00338 
00339 #endif
00340 
00341 
00342 // End of File.

Generated on Fri Mar 7 06:25:02 2008 for PTLib by  doxygen 1.5.1