00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #ifndef _PSNMP_H
00032 #define _PSNMP_H
00033
00034 #ifdef P_USE_PRAGMA
00035 #pragma interface
00036 #endif
00037
00038 #ifdef P_SNMP
00039
00040 #include <ptlib/sockets.h>
00041 #include <ptclib/snmp.h>
00042 #include <ptclib/pasn.h>
00043
00044 #include <list>
00045 #include <vector>
00046
00048
00051 class PSNMPVarBindingList : public PObject
00052 {
00053 PCLASSINFO(PSNMPVarBindingList, PObject)
00054 public:
00055
00056 void Append(const PString & objectID);
00057 void Append(const PString & objectID, PASNObject * obj);
00058 void AppendString(const PString & objectID, const PString & str);
00059
00060 void RemoveAll();
00061
00062 PINDEX GetSize() const;
00063
00064 PString GetObjectID(PINDEX idx) const;
00065 PASNObject & operator[](PINDEX idx) const;
00066
00067 void PrintOn(ostream & strm) const;
00068
00069 protected:
00070 PStringList objectIds;
00071 PASNObjectList values;
00072 };
00073
00075
00078 class PSNMP : public PIndirectChannel
00079 {
00080 PCLASSINFO(PSNMP, PIndirectChannel)
00081 public:
00082 enum ErrorType {
00083
00084 NoError = 0,
00085 TooBig = 1,
00086 NoSuchName = 2,
00087 BadValue = 3,
00088 ReadOnly = 4,
00089 GenErr = 5,
00090
00091
00092 NoResponse,
00093 MalformedResponse,
00094 SendFailed,
00095 RxBufferTooSmall,
00096 TxDataTooBig,
00097 NumErrors
00098 };
00099
00100 enum RequestType {
00101 GetRequest = 0,
00102 GetNextRequest = 1,
00103 GetResponse = 2,
00104 SetRequest = 3,
00105 Trap = 4,
00106 };
00107
00108 enum { TrapPort = 162 };
00109
00110 enum TrapType {
00111 ColdStart = 0,
00112 WarmStart = 1,
00113 LinkDown = 2,
00114 LinkUp = 3,
00115 AuthenticationFailure = 4,
00116 EGPNeighbourLoss = 5,
00117 EnterpriseSpecific = 6,
00118 NumTrapTypes
00119 };
00120
00121 static PString GetErrorText(ErrorType err);
00122
00123 static PString GetTrapTypeText(PINDEX code);
00124
00125 static void SendEnterpriseTrap (
00126 const PIPSocket::Address & addr,
00127 const PString & community,
00128 const PString & enterprise,
00129 PINDEX specificTrap,
00130 PASNUnsigned timeTicks,
00131 WORD sendPort = TrapPort);
00132
00133 static void SendEnterpriseTrap (
00134 const PIPSocket::Address & addr,
00135 const PString & community,
00136 const PString & enterprise,
00137 PINDEX specificTrap,
00138 PASNUnsigned timeTicks,
00139 const PSNMPVarBindingList & vars,
00140 WORD sendPort = TrapPort);
00141
00142 static void SendTrap (
00143 const PIPSocket::Address & addr,
00144 PSNMP::TrapType trapType,
00145 const PString & community,
00146 const PString & enterprise,
00147 PINDEX specificTrap,
00148 PASNUnsigned timeTicks,
00149 const PSNMPVarBindingList & vars,
00150 WORD sendPort = TrapPort);
00151
00152 static void SendTrap (
00153 const PIPSocket::Address & addr,
00154 PSNMP::TrapType trapType,
00155 const PString & community,
00156 const PString & enterprise,
00157 PINDEX specificTrap,
00158 PASNUnsigned timeTicks,
00159 const PSNMPVarBindingList & vars,
00160 const PIPSocket::Address & agentAddress,
00161 WORD sendPort = TrapPort);
00162
00163 static void WriteTrap ( PChannel & channel,
00164 PSNMP::TrapType trapType,
00165 const PString & community,
00166 const PString & enterprise,
00167 PINDEX specificTrap,
00168 PASNUnsigned timeTicks,
00169 const PSNMPVarBindingList & vars,
00170 const PIPSocket::Address & agentAddress);
00171
00172 static PBoolean DecodeTrap(const PBYTEArray & readBuffer,
00173 PINDEX & version,
00174 PString & community,
00175 PString & enterprise,
00176 PIPSocket::Address & address,
00177 PINDEX & genericTrapType,
00178 PINDEX & specificTrapType,
00179 PASNUnsigned & timeTicks,
00180 PSNMPVarBindingList & varsOut);
00181
00182 typedef list<pair<PString,PRFC1155_ObjectSyntax> > BindingList;
00183 };
00184
00185
00187
00190 class PSNMPClient : public PSNMP
00191 {
00192 PCLASSINFO(PSNMPClient, PSNMP)
00193 public:
00194 PSNMPClient(const PString & host,
00195 PINDEX retryMax = 5,
00196 PINDEX timeoutMax = 5,
00197 PINDEX rxBufferSize = 1500,
00198 PINDEX txSize = 484);
00199
00200 PSNMPClient(PINDEX retryMax = 5,
00201 PINDEX timeoutMax = 5,
00202 PINDEX rxBufferSize = 1500,
00203 PINDEX txSize = 484);
00204
00205 void SetVersion(PASNInt version);
00206 PASNInt GetVersion() const;
00207
00208 void SetCommunity(const PString & str);
00209 PString GetCommunity() const;
00210
00211 void SetRequestID(PASNInt requestID);
00212 PASNInt GetRequestID() const;
00213
00214 PBoolean WriteGetRequest (PSNMPVarBindingList & varsIn,
00215 PSNMPVarBindingList & varsOut);
00216
00217 PBoolean WriteGetNextRequest (PSNMPVarBindingList & varsIn,
00218 PSNMPVarBindingList & varsOut);
00219
00220 PBoolean WriteSetRequest (PSNMPVarBindingList & varsIn,
00221 PSNMPVarBindingList & varsOut);
00222
00223 ErrorType GetLastErrorCode() const;
00224 PINDEX GetLastErrorIndex() const;
00225 PString GetLastErrorText() const;
00226
00227 protected:
00228 PBoolean WriteRequest (PASNInt requestCode,
00229 PSNMPVarBindingList & varsIn,
00230 PSNMPVarBindingList & varsOut);
00231
00232
00233 PBoolean ReadRequest(PBYTEArray & readBuffer);
00234
00235 PString hostName;
00236 PString community;
00237 PASNInt requestId;
00238 PASNInt version;
00239 PINDEX retryMax;
00240 PINDEX lastErrorIndex;
00241 ErrorType lastErrorCode;
00242 PBYTEArray readBuffer;
00243 PINDEX maxRxSize;
00244 PINDEX maxTxSize;
00245 };
00246
00247
00249
00252 class PSNMPServer : public PSNMP, PThread
00253 {
00254 PCLASSINFO(PSNMPServer, PSNMP)
00255 public:
00256
00257 PSNMPServer(PIPSocket::Address binding = PIPSocket::GetDefaultIpAny(),
00258 WORD localPort = 161,
00259 PINDEX timeout = 5000,
00260 PINDEX rxSize = 10000,
00261 PINDEX txSize = 10000);
00262
00263 ~PSNMPServer();
00264
00265 void Main();
00266
00267 void SetVersion(PASNInt newVersion);
00268 PBoolean HandleChannel();
00269 int ProcessPDU(const PBYTEArray & readBuffer, PBYTEArray & writeBuffer);
00270
00271 virtual PBoolean Authorise(const PIPSocket::Address & received);
00272
00273 virtual PBoolean OnGetRequest (PINDEX reqID, PSNMP::BindingList & vars, PSNMP::ErrorType & errCode);
00274 virtual PBoolean OnGetNextRequest (PINDEX reqID, PSNMP::BindingList & vars, PSNMP::ErrorType & errCode);
00275 virtual PBoolean OnSetRequest (PINDEX reqID, PSNMP::BindingList & vars, PSNMP::ErrorType & errCode);
00276
00277 PSNMP::ErrorType SendGetResponse (PSNMPVarBindingList & vars);
00278
00279 protected:
00280 PString community;
00281 PASNInt version;
00282 PINDEX lastErrorIndex;
00283 ErrorType lastErrorCode;
00284 PBYTEArray readBuffer;
00285 PINDEX maxRxSize;
00286 PINDEX maxTxSize;
00287 PUDPSocket * baseSocket;
00288 };
00289
00290 #endif // P_SNMP
00291
00292 #endif
00293
00294
00295