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 PINDEX GetIndex(const PString & objectID) const;
00065 PString GetObjectID(PINDEX idx) const;
00066 PASNObject & operator[](PINDEX idx) const;
00067
00068 void PrintOn(ostream & strm) const;
00069
00070 protected:
00071 PStringArray objectIds;
00072 PASNObjectArray values;
00073 };
00074
00076
00079 class PSNMP : public PIndirectChannel
00080 {
00081 PCLASSINFO(PSNMP, PIndirectChannel)
00082 public:
00083 enum ErrorType {
00084
00085 NoError = 0,
00086 TooBig = 1,
00087 NoSuchName = 2,
00088 BadValue = 3,
00089 ReadOnly = 4,
00090 GenErr = 5,
00091
00092
00093 NoResponse,
00094 MalformedResponse,
00095 SendFailed,
00096 RxBufferTooSmall,
00097 TxDataTooBig,
00098 NumErrors
00099 };
00100
00101 enum RequestType {
00102 GetRequest = 0,
00103 GetNextRequest = 1,
00104 GetResponse = 2,
00105 SetRequest = 3,
00106 Trap = 4,
00107 };
00108
00109 enum { TrapPort = 162 };
00110
00111 enum TrapType {
00112 ColdStart = 0,
00113 WarmStart = 1,
00114 LinkDown = 2,
00115 LinkUp = 3,
00116 AuthenticationFailure = 4,
00117 EGPNeighbourLoss = 5,
00118 EnterpriseSpecific = 6,
00119 NumTrapTypes
00120 };
00121
00122 static PString GetErrorText(ErrorType err);
00123
00124 static PString GetTrapTypeText(PINDEX code);
00125
00126 static void SendEnterpriseTrap (
00127 const PIPSocket::Address & addr,
00128 const PString & community,
00129 const PString & enterprise,
00130 PINDEX specificTrap,
00131 PASNUnsigned timeTicks,
00132 WORD sendPort = TrapPort);
00133
00134 static void SendEnterpriseTrap (
00135 const PIPSocket::Address & addr,
00136 const PString & community,
00137 const PString & enterprise,
00138 PINDEX specificTrap,
00139 PASNUnsigned timeTicks,
00140 const PSNMPVarBindingList & vars,
00141 WORD sendPort = TrapPort);
00142
00143 static void SendTrap (
00144 const PIPSocket::Address & addr,
00145 PSNMP::TrapType trapType,
00146 const PString & community,
00147 const PString & enterprise,
00148 PINDEX specificTrap,
00149 PASNUnsigned timeTicks,
00150 const PSNMPVarBindingList & vars,
00151 WORD sendPort = TrapPort);
00152
00153 static void SendTrap (
00154 const PIPSocket::Address & addr,
00155 PSNMP::TrapType trapType,
00156 const PString & community,
00157 const PString & enterprise,
00158 PINDEX specificTrap,
00159 PASNUnsigned timeTicks,
00160 const PSNMPVarBindingList & vars,
00161 const PIPSocket::Address & agentAddress,
00162 WORD sendPort = TrapPort);
00163
00164 static void WriteTrap ( PChannel & channel,
00165 PSNMP::TrapType trapType,
00166 const PString & community,
00167 const PString & enterprise,
00168 PINDEX specificTrap,
00169 PASNUnsigned timeTicks,
00170 const PSNMPVarBindingList & vars,
00171 const PIPSocket::Address & agentAddress);
00172
00173 static PBoolean DecodeTrap(const PBYTEArray & readBuffer,
00174 PINDEX & version,
00175 PString & community,
00176 PString & enterprise,
00177 PIPSocket::Address & address,
00178 PINDEX & genericTrapType,
00179 PINDEX & specificTrapType,
00180 PASNUnsigned & timeTicks,
00181 PSNMPVarBindingList & varsOut);
00182
00183 typedef list<pair<PString,PRFC1155_ObjectSyntax> > BindingList;
00184 };
00185
00186
00188
00191 class PSNMPClient : public PSNMP
00192 {
00193 PCLASSINFO(PSNMPClient, PSNMP)
00194 public:
00195 PSNMPClient(const PString & host,
00196 PINDEX retryMax = 5,
00197 PINDEX timeoutMax = 5,
00198 PINDEX rxBufferSize = 1500,
00199 PINDEX txSize = 484);
00200
00201 PSNMPClient(PINDEX retryMax = 5,
00202 PINDEX timeoutMax = 5,
00203 PINDEX rxBufferSize = 1500,
00204 PINDEX txSize = 484);
00205
00206 void SetVersion(PASNInt version);
00207 PASNInt GetVersion() const;
00208
00209 void SetCommunity(const PString & str);
00210 PString GetCommunity() const;
00211
00212 void SetRequestID(PASNInt requestID);
00213 PASNInt GetRequestID() const;
00214
00215 PBoolean WriteGetRequest (PSNMPVarBindingList & varsIn,
00216 PSNMPVarBindingList & varsOut);
00217
00218 PBoolean WriteGetNextRequest (PSNMPVarBindingList & varsIn,
00219 PSNMPVarBindingList & varsOut);
00220
00221 PBoolean WriteSetRequest (PSNMPVarBindingList & varsIn,
00222 PSNMPVarBindingList & varsOut);
00223
00224 ErrorType GetLastErrorCode() const;
00225 PINDEX GetLastErrorIndex() const;
00226 PString GetLastErrorText() const;
00227
00228 protected:
00229 PBoolean WriteRequest (PASNInt requestCode,
00230 PSNMPVarBindingList & varsIn,
00231 PSNMPVarBindingList & varsOut);
00232
00233
00234 PBoolean ReadRequest(PBYTEArray & readBuffer);
00235
00236 PString hostName;
00237 PString community;
00238 PASNInt requestId;
00239 PASNInt version;
00240 PINDEX retryMax;
00241 PINDEX lastErrorIndex;
00242 ErrorType lastErrorCode;
00243 PBYTEArray readBuffer;
00244 PINDEX maxRxSize;
00245 PINDEX maxTxSize;
00246 };
00247
00248
00250
00253 class PSNMPServer : public PSNMP, PThread
00254 {
00255 PCLASSINFO(PSNMPServer, PSNMP)
00256 public:
00257
00258 PSNMPServer(PIPSocket::Address binding = PIPSocket::GetDefaultIpAny(),
00259 WORD localPort = 161,
00260 PINDEX timeout = 5000,
00261 PINDEX rxSize = 10000,
00262 PINDEX txSize = 10000);
00263
00264 ~PSNMPServer();
00265
00266 void Main();
00267
00268 void SetVersion(PASNInt newVersion);
00269 PBoolean HandleChannel();
00270 PBoolean ProcessPDU(const PBYTEArray & readBuffer, PBYTEArray & writeBuffer);
00271
00272 virtual PBoolean Authorise(const PIPSocket::Address & received);
00273 virtual PBoolean ConfirmVersion(PASN_Integer vers);
00274 virtual PBoolean ConfirmCommunity(PASN_OctetString & community);
00275
00276 virtual PBoolean MIB_LocalMatch(PSNMP_PDU & pdu);
00277
00278 virtual PBoolean OnGetRequest (PINDEX reqID, PSNMP::BindingList & vars, PSNMP::ErrorType & errCode);
00279 virtual PBoolean OnGetNextRequest (PINDEX reqID, PSNMP::BindingList & vars, PSNMP::ErrorType & errCode);
00280 virtual PBoolean OnSetRequest (PINDEX reqID, PSNMP::BindingList & vars, PSNMP::ErrorType & errCode);
00281
00282 PSNMP::ErrorType SendGetResponse (PSNMPVarBindingList & vars);
00283
00284 protected:
00285 PString community;
00286 PASN_Integer version;
00287 PINDEX lastErrorIndex;
00288 ErrorType lastErrorCode;
00289 PBYTEArray readBuffer;
00290 PINDEX maxRxSize;
00291 PINDEX maxTxSize;
00292 PUDPSocket *baseSocket;
00293 PDictionary<PRFC1155_ObjectName, PRFC1155_ObjectSyntax> objList;
00294 };
00295
00296 #endif // P_SNMP
00297
00298 #endif
00299
00300
00301